]> git.basschouten.com Git - openhab-addons.git/blob
086301e45b1a03b05a1b09355a4b374d6f274d75
[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.junit.jupiter.api.Assertions.assertEquals;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.when;
18
19 import java.util.Arrays;
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.Test;
25 import org.openhab.binding.revogi.internal.udp.UdpResponseDTO;
26 import org.openhab.binding.revogi.internal.udp.UdpSenderService;
27
28 /**
29  * @author Andi Bräu - Initial contribution
30  */
31 @NonNullByDefault
32 public class StatusServiceTest {
33
34     private final UdpSenderService udpSenderService = mock(UdpSenderService.class);
35     private final StatusService statusService = new StatusService(udpSenderService);
36
37     @Test
38     public void getStatusSuccessfully() {
39         // given
40         StatusDTO status = new StatusDTO(true, 200, Arrays.asList(0, 0, 0, 0, 0, 0), Arrays.asList(0, 0, 0, 0, 0, 0),
41                 Arrays.asList(0, 0, 0, 0, 0, 0));
42         List<UdpResponseDTO> statusString = List.of(new UdpResponseDTO(
43                 "V3{\"response\":90,\"code\":200,\"data\":{\"switch\":[0,0,0,0,0,0],\"watt\":[0,0,0,0,0,0],\"amp\":[0,0,0,0,0,0]}}",
44                 "127.0.0.1"));
45         when(udpSenderService.sendMessage("V3{\"sn\":\"serial\", \"cmd\": 90}", "127.0.0.1"))
46                 .thenReturn(CompletableFuture.completedFuture(statusString));
47
48         // when
49         CompletableFuture<StatusDTO> statusResponse = statusService.queryStatus("serial", "127.0.0.1");
50
51         // then
52         assertEquals(status, statusResponse.getNow(new StatusDTO()));
53     }
54
55     @Test
56     public void invalidUdpResponse() {
57         // given
58         List<UdpResponseDTO> statusString = List.of(new UdpResponseDTO("something invalid", "12345"));
59         when(udpSenderService.sendMessage("V3{\"sn\":\"serial\", \"cmd\": 90}", "127.0.0.1"))
60                 .thenReturn(CompletableFuture.completedFuture(statusString));
61
62         // when
63         CompletableFuture<StatusDTO> futureStatus = statusService.queryStatus("serial", "127.0.0.1");
64
65         // then
66         StatusDTO status = futureStatus.getNow(new StatusDTO());
67         assertEquals(503, status.getResponseCode());
68     }
69 }