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.List;
21 import java.util.concurrent.CompletableFuture;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.junit.jupiter.api.Assertions;
25 import org.junit.jupiter.api.Test;
26 import org.openhab.binding.revogi.internal.udp.UdpResponseDTO;
27 import org.openhab.binding.revogi.internal.udp.UdpSenderService;
30 * @author Andi Bräu - Initial contribution
33 public class SwitchServiceTest {
35 private UdpSenderService udpSenderService = mock(UdpSenderService.class);
36 private SwitchService switchService = new SwitchService(udpSenderService);
39 public void getStatusSuccesfully() {
41 List<UdpResponseDTO> response = List.of(new UdpResponseDTO("V3{\"response\":20,\"code\":200}", "127.0.0.1"));
42 when(udpSenderService.sendMessage("V3{\"sn\":\"serial\", \"cmd\": 20, \"port\": 1, \"state\": 1}", "127.0.0.1"))
43 .thenReturn(CompletableFuture.completedFuture(response));
46 CompletableFuture<SwitchResponseDTO> switchResponse = switchService.switchPort("serial", "127.0.0.1", 1, 1);
49 assertThat(switchResponse.getNow(new SwitchResponseDTO(0, 0)), equalTo(new SwitchResponseDTO(20, 200)));
53 public void getStatusSuccesfullyWithBroadcast() {
55 List<UdpResponseDTO> response = List.of(new UdpResponseDTO("V3{\"response\":20,\"code\":200}", "127.0.0.1"));
56 when(udpSenderService.broadcastUdpDatagram("V3{\"sn\":\"serial\", \"cmd\": 20, \"port\": 1, \"state\": 1}"))
57 .thenReturn(CompletableFuture.completedFuture(response));
60 CompletableFuture<SwitchResponseDTO> switchResponse = switchService.switchPort("serial", "", 1, 1);
63 assertThat(switchResponse.getNow(new SwitchResponseDTO(0, 0)), equalTo(new SwitchResponseDTO(20, 200)));
67 public void invalidUdpResponse() {
69 List<UdpResponseDTO> response = List.of(new UdpResponseDTO("something invalid", "12345"));
70 when(udpSenderService.sendMessage("V3{\"sn\":\"serial\", \"cmd\": 20, \"port\": 1, \"state\": 1}", "127.0.0.1"))
71 .thenReturn(CompletableFuture.completedFuture(response));
74 CompletableFuture<SwitchResponseDTO> switchResponse = switchService.switchPort("serial", "127.0.0.1", 1, 1);
77 assertThat(switchResponse.getNow(new SwitchResponseDTO(0, 0)), equalTo(new SwitchResponseDTO(0, 503)));
81 public void getExceptionOnWrongState() {
82 Assertions.assertThrows(IllegalArgumentException.class,
83 () -> switchService.switchPort("serial", "127.0.0.1", 1, 12));
87 public void getExceptionOnWrongPort() {
88 Assertions.assertThrows(IllegalArgumentException.class,
89 () -> switchService.switchPort("serial", "127.0.0.1", -1, 1));