]> git.basschouten.com Git - openhab-addons.git/blob
6f1b3de5258cd2b844e67f64753266062f24356e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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 import java.util.stream.Collectors;
28
29 import org.eclipse.jdt.annotation.NonNull;
30 import org.junit.jupiter.api.BeforeEach;
31 import org.junit.jupiter.api.Test;
32 import org.junit.jupiter.api.extension.ExtendWith;
33 import org.mockito.Mock;
34 import org.mockito.junit.jupiter.MockitoExtension;
35
36 /**
37  * Unit test to test the {@link NutApi} using a mock Socket connection.
38  *
39  * @author Hilbrand Bouwkamp - Initial contribution
40  */
41 @ExtendWith(MockitoExtension.class)
42 public class NutApiTest {
43
44     private @Mock Socket socket;
45     private NutConnector connector;
46
47     @BeforeEach
48     public void setUp() throws IOException {
49         connector = new NutConnector("localhost", 0, "test", "pwd") {
50             @Override
51             protected Socket newSocket() {
52                 return socket;
53             }
54         };
55     }
56
57     /**
58      * Test if retrieving a list of variables is correctly done.
59      */
60     @Test
61     public void testListVariables() throws IOException, NutException, URISyntaxException {
62         final String expectedCommands = Files
63                 .readAllLines(Paths.get(getClass().getResource("var_list_commands.txt").toURI())).stream()
64                 .collect(Collectors.joining(System.lineSeparator()));
65         final StringBuilder actualCommands = new StringBuilder();
66         try (InputStream in = getClass().getResourceAsStream("var_list.txt"); OutputStream out = new OutputStream() {
67             @Override
68             public void write(int b) throws IOException {
69                 actualCommands.append((char) b);
70             }
71         }) {
72             doReturn(in).when(socket).getInputStream();
73             doReturn(out).when(socket).getOutputStream();
74
75             final NutApi api = new NutApi(connector);
76             final Map<@NonNull String, @NonNull String> variables = api.getVariables("ups1");
77
78             assertThat("Should have variables", variables.size(), is(4));
79             assertThat("Should read variable correctly", variables.get("output.voltage.nominal"), is("115"));
80             assertThat("Should send commands correctly", actualCommands.toString(), is(expectedCommands));
81         }
82     }
83
84     /**
85      * Test if retrieving a single variable is correctly done.
86      */
87     @Test
88     public void testGetVariable() throws IOException, NutException, URISyntaxException {
89         final String expectedCommands = Files
90                 .readAllLines(Paths.get(getClass().getResource("var_get_commands.txt").toURI())).stream()
91                 .collect(Collectors.joining(System.lineSeparator()));
92         final StringBuilder actualCommands = new StringBuilder();
93         try (InputStream in = getClass().getResourceAsStream("var_get.txt"); OutputStream out = new OutputStream() {
94             @Override
95             public void write(int b) throws IOException {
96                 actualCommands.append((char) b);
97             }
98         }) {
99             doReturn(in).when(socket).getInputStream();
100             doReturn(out).when(socket).getOutputStream();
101
102             final NutApi api = new NutApi(connector);
103             final String variable = api.getVariable("ups1", "ups.status");
104             assertThat("Should read ups.status variable correctly", variable, is("OL"));
105             assertThat("Should send commands correctly", actualCommands.toString(), is(expectedCommands));
106         }
107     }
108 }