]> git.basschouten.com Git - openhab-addons.git/blob
72c97205dcaf64613be534a72463dbae3d950356
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.MatcherAssert.assertThat;
16 import static org.hamcrest.core.Is.is;
17 import static org.mockito.Mockito.doReturn;
18
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.io.OutputStream;
22 import java.net.Socket;
23 import java.net.URISyntaxException;
24 import java.nio.file.Files;
25 import java.nio.file.Paths;
26 import java.util.Map;
27
28 import org.eclipse.jdt.annotation.NonNull;
29 import org.junit.jupiter.api.BeforeEach;
30 import org.junit.jupiter.api.Test;
31 import org.junit.jupiter.api.extension.ExtendWith;
32 import org.mockito.Mock;
33 import org.mockito.junit.jupiter.MockitoExtension;
34
35 /**
36  * Unit test to test the {@link NutApi} using a mock Socket connection.
37  *
38  * @author Hilbrand Bouwkamp - Initial contribution
39  */
40 @ExtendWith(MockitoExtension.class)
41 public class NutApiTest {
42
43     private @Mock Socket socket;
44     private NutConnector connector;
45
46     @BeforeEach
47     public void setUp() throws IOException {
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 }