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