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.revogi.internal.api;
15 import static org.hamcrest.CoreMatchers.equalTo;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.mockito.Mockito.mock;
18 import static org.mockito.Mockito.when;
20 import java.util.Collections;
21 import java.util.List;
22 import java.util.concurrent.CompletableFuture;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.junit.jupiter.api.Assertions;
26 import org.junit.jupiter.api.Test;
27 import org.openhab.binding.revogi.internal.udp.UdpResponseDTO;
28 import org.openhab.binding.revogi.internal.udp.UdpSenderService;
31 * @author Andi Bräu - Initial contribution
34 public class SwitchServiceTest {
36 private UdpSenderService udpSenderService = mock(UdpSenderService.class);
37 private SwitchService switchService = new SwitchService(udpSenderService);
40 public void getStatusSuccesfully() {
42 List<UdpResponseDTO> response = Collections
43 .singletonList(new UdpResponseDTO("V3{\"response\":20,\"code\":200}", "127.0.0.1"));
44 when(udpSenderService.sendMessage("V3{\"sn\":\"serial\", \"cmd\": 20, \"port\": 1, \"state\": 1}", "127.0.0.1"))
45 .thenReturn(CompletableFuture.completedFuture(response));
48 CompletableFuture<SwitchResponseDTO> switchResponse = switchService.switchPort("serial", "127.0.0.1", 1, 1);
51 assertThat(switchResponse.getNow(new SwitchResponseDTO(0, 0)), equalTo(new SwitchResponseDTO(20, 200)));
55 public void getStatusSuccesfullyWithBroadcast() {
57 List<UdpResponseDTO> response = Collections
58 .singletonList(new UdpResponseDTO("V3{\"response\":20,\"code\":200}", "127.0.0.1"));
59 when(udpSenderService.broadcastUdpDatagram("V3{\"sn\":\"serial\", \"cmd\": 20, \"port\": 1, \"state\": 1}"))
60 .thenReturn(CompletableFuture.completedFuture(response));
63 CompletableFuture<SwitchResponseDTO> switchResponse = switchService.switchPort("serial", "", 1, 1);
66 assertThat(switchResponse.getNow(new SwitchResponseDTO(0, 0)), equalTo(new SwitchResponseDTO(20, 200)));
70 public void invalidUdpResponse() {
72 List<UdpResponseDTO> response = Collections.singletonList(new UdpResponseDTO("something invalid", "12345"));
73 when(udpSenderService.sendMessage("V3{\"sn\":\"serial\", \"cmd\": 20, \"port\": 1, \"state\": 1}", "127.0.0.1"))
74 .thenReturn(CompletableFuture.completedFuture(response));
77 CompletableFuture<SwitchResponseDTO> switchResponse = switchService.switchPort("serial", "127.0.0.1", 1, 1);
80 assertThat(switchResponse.getNow(new SwitchResponseDTO(0, 0)), equalTo(new SwitchResponseDTO(0, 503)));
84 public void getExceptionOnWrongState() {
85 Assertions.assertThrows(IllegalArgumentException.class,
86 () -> switchService.switchPort("serial", "127.0.0.1", 1, 12));
90 public void getExceptionOnWrongPort() {
91 Assertions.assertThrows(IllegalArgumentException.class,
92 () -> switchService.switchPort("serial", "127.0.0.1", -1, 1));