]> git.basschouten.com Git - openhab-addons.git/blob
b90ff77412023a84a0e03c0dd3ca65e06b7c2df4
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.networkupstools.internal.nut;
14
15 import static org.hamcrest.core.Is.is;
16 import static org.junit.Assert.assertThat;
17 import static org.mockito.Mockito.doReturn;
18 import static org.mockito.MockitoAnnotations.initMocks;
19
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.OutputStream;
23 import java.net.Socket;
24 import java.net.URISyntaxException;
25 import java.nio.file.Files;
26 import java.nio.file.Paths;
27 import java.util.Map;
28
29 import org.eclipse.jdt.annotation.NonNull;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.mockito.Mock;
33
34 /**
35  * Unit test to test the {@link NutApi} using a mock Socket connection.
36  *
37  * @author Hilbrand Bouwkamp - Initial contribution
38  */
39 public class NutApiTest {
40
41     @Mock
42     private Socket socket;
43     private NutConnector connector;
44
45     @Before
46     public void setUp() throws IOException {
47         initMocks(this);
48         connector = new NutConnector("localhost", 0, "test", "pwd") {
49             @Override
50             protected Socket newSocket() {
51                 return socket;
52             };
53         };
54     }
55
56     /**
57      * Test if retrieving a list of variables is correctly done.
58      */
59     @Test
60     public void testListVariables() throws IOException, NutException, URISyntaxException {
61         final String expectedCommands = new String(
62                 Files.readAllBytes(Paths.get(getClass().getResource("var_list_commands.txt").toURI())));
63         final StringBuffer actualCommands = new StringBuffer();
64         try (InputStream in = getClass().getResourceAsStream("var_list.txt"); OutputStream out = new OutputStream() {
65             @Override
66             public void write(int b) throws IOException {
67                 actualCommands.append((char) b);
68             }
69         }) {
70             doReturn(in).when(socket).getInputStream();
71             doReturn(out).when(socket).getOutputStream();
72
73             final NutApi api = new NutApi(connector);
74             final Map<@NonNull String, @NonNull String> variables = api.getVariables("ups1");
75
76             assertThat("Should have variables", variables.size(), is(4));
77             assertThat("Should read variable correctly", variables.get("output.voltage.nominal"), is("115"));
78             assertThat("Should send commands correctly", actualCommands.toString(), is(expectedCommands));
79         }
80     }
81
82     /**
83      * Test if retrieving a single variable is correctly done.
84      */
85     @Test
86     public void testGetVariable() throws IOException, NutException, URISyntaxException {
87         final String expectedCommands = new String(
88                 Files.readAllBytes(Paths.get(getClass().getResource("var_get_commands.txt").toURI())));
89         final StringBuffer actualCommands = new StringBuffer();
90         try (InputStream in = getClass().getResourceAsStream("var_get.txt"); OutputStream out = new OutputStream() {
91             @Override
92             public void write(int b) throws IOException {
93                 actualCommands.append((char) b);
94             }
95         }) {
96             doReturn(in).when(socket).getInputStream();
97             doReturn(out).when(socket).getOutputStream();
98
99             final NutApi api = new NutApi(connector);
100             final String variable = api.getVariable("ups1", "ups.status");
101             assertThat("Should read ups.status variable correctly", variable, is("OL"));
102             assertThat("Should send commands correctly", actualCommands.toString(), is(expectedCommands));
103         }
104     }
105 }