2 * Copyright (c) 2010-2023 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;
27 import java.util.stream.Collectors;
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;
37 * Unit test to test the {@link NutApi} using a mock Socket connection.
39 * @author Hilbrand Bouwkamp - Initial contribution
41 @ExtendWith(MockitoExtension.class)
42 public class NutApiTest {
44 private @Mock Socket socket;
45 private NutConnector connector;
48 public void setUp() throws IOException {
49 connector = new NutConnector("localhost", 0, "test", "pwd") {
51 protected Socket newSocket() {
58 * Test if retrieving a list of variables is correctly done.
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() {
68 public void write(int b) throws IOException {
69 actualCommands.append((char) b);
72 doReturn(in).when(socket).getInputStream();
73 doReturn(out).when(socket).getOutputStream();
75 final NutApi api = new NutApi(connector);
76 final Map<@NonNull String, @NonNull String> variables = api.getVariables("ups1");
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));
85 * Test if retrieving a single variable is correctly done.
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() {
95 public void write(int b) throws IOException {
96 actualCommands.append((char) b);
99 doReturn(in).when(socket).getInputStream();
100 doReturn(out).when(socket).getOutputStream();
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));