2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.networkupstools.internal.nut;
15 import static org.hamcrest.MatcherAssert.assertThat;
16 import static org.hamcrest.core.Is.is;
17 import static org.mockito.Mockito.doReturn;
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;
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;
36 * Unit test to test the {@link NutApi} using a mock Socket connection.
38 * @author Hilbrand Bouwkamp - Initial contribution
40 @ExtendWith(MockitoExtension.class)
41 public class NutApiTest {
43 private @Mock Socket socket;
44 private NutConnector connector;
47 public void setUp() throws IOException {
48 connector = new NutConnector("localhost", 0, "test", "pwd") {
50 protected Socket newSocket() {
57 * Test if retrieving a list of variables is correctly done.
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() {
66 public void write(int b) throws IOException {
67 actualCommands.append((char) b);
70 doReturn(in).when(socket).getInputStream();
71 doReturn(out).when(socket).getOutputStream();
73 final NutApi api = new NutApi(connector);
74 final Map<@NonNull String, @NonNull String> variables = api.getVariables("ups1");
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));
83 * Test if retrieving a single variable is correctly done.
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() {
92 public void write(int b) throws IOException {
93 actualCommands.append((char) b);
96 doReturn(in).when(socket).getInputStream();
97 doReturn(out).when(socket).getOutputStream();
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));