]> git.basschouten.com Git - openhab-addons.git/blob
cc94ac246fa2524754d7f1ee73ef371727ad4c1c
[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.revogi.internal.api;
14
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;
19
20 import java.util.Collections;
21 import java.util.List;
22 import java.util.concurrent.CompletableFuture;
23
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;
29
30 /**
31  * @author Andi Bräu - Initial contribution
32  */
33 @NonNullByDefault
34 public class SwitchServiceTest {
35
36     private UdpSenderService udpSenderService = mock(UdpSenderService.class);
37     private SwitchService switchService = new SwitchService(udpSenderService);
38
39     @Test
40     public void getStatusSuccesfully() {
41         // given
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));
46
47         // when
48         CompletableFuture<SwitchResponseDTO> switchResponse = switchService.switchPort("serial", "127.0.0.1", 1, 1);
49
50         // then
51         assertThat(switchResponse.getNow(new SwitchResponseDTO(0, 0)), equalTo(new SwitchResponseDTO(20, 200)));
52     }
53
54     @Test
55     public void getStatusSuccesfullyWithBroadcast() {
56         // given
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));
61
62         // when
63         CompletableFuture<SwitchResponseDTO> switchResponse = switchService.switchPort("serial", "", 1, 1);
64
65         // then
66         assertThat(switchResponse.getNow(new SwitchResponseDTO(0, 0)), equalTo(new SwitchResponseDTO(20, 200)));
67     }
68
69     @Test
70     public void invalidUdpResponse() {
71         // given
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));
75
76         // when
77         CompletableFuture<SwitchResponseDTO> switchResponse = switchService.switchPort("serial", "127.0.0.1", 1, 1);
78
79         // then
80         assertThat(switchResponse.getNow(new SwitchResponseDTO(0, 0)), equalTo(new SwitchResponseDTO(0, 503)));
81     }
82
83     @Test
84     public void getExceptionOnWrongState() {
85         Assertions.assertThrows(IllegalArgumentException.class,
86                 () -> switchService.switchPort("serial", "127.0.0.1", 1, 12));
87     }
88
89     @Test
90     public void getExceptionOnWrongPort() {
91         Assertions.assertThrows(IllegalArgumentException.class,
92                 () -> switchService.switchPort("serial", "127.0.0.1", -1, 1));
93     }
94 }