]> git.basschouten.com Git - openhab-addons.git/blob
fab4aae7eb41daec93605ebf2c5be9f553fa298b
[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.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.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 StatusServiceTest {
34
35     private final UdpSenderService udpSenderService = mock(UdpSenderService.class);
36     private final StatusService statusService = new StatusService(udpSenderService);
37
38     @Test
39     public void getStatusSuccessfully() {
40         // given
41         StatusDTO status = new StatusDTO(true, 200, Arrays.asList(0, 0, 0, 0, 0, 0), Arrays.asList(0, 0, 0, 0, 0, 0),
42                 Arrays.asList(0, 0, 0, 0, 0, 0));
43         List<UdpResponseDTO> statusString = Collections.singletonList(new UdpResponseDTO(
44                 "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]}}",
45                 "127.0.0.1"));
46         when(udpSenderService.sendMessage("V3{\"sn\":\"serial\", \"cmd\": 90}", "127.0.0.1"))
47                 .thenReturn(CompletableFuture.completedFuture(statusString));
48
49         // when
50         CompletableFuture<StatusDTO> statusResponse = statusService.queryStatus("serial", "127.0.0.1");
51
52         // then
53         assertEquals(status, statusResponse.getNow(new StatusDTO()));
54     }
55
56     @Test
57     public void invalidUdpResponse() {
58         // given
59         List<UdpResponseDTO> statusString = Collections.singletonList(new UdpResponseDTO("something invalid", "12345"));
60         when(udpSenderService.sendMessage("V3{\"sn\":\"serial\", \"cmd\": 90}", "127.0.0.1"))
61                 .thenReturn(CompletableFuture.completedFuture(statusString));
62
63         // when
64         CompletableFuture<StatusDTO> futureStatus = statusService.queryStatus("serial", "127.0.0.1");
65
66         // then
67         StatusDTO status = futureStatus.getNow(new StatusDTO());
68         assertEquals(503, status.getResponseCode());
69     }
70 }