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.junit.jupiter.api.Assertions.assertEquals;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.when;
19 import java.util.Arrays;
20 import java.util.List;
21 import java.util.concurrent.CompletableFuture;
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;
29 * @author Andi Bräu - Initial contribution
32 public class StatusServiceTest {
34 private final UdpSenderService udpSenderService = mock(UdpSenderService.class);
35 private final StatusService statusService = new StatusService(udpSenderService);
38 public void getStatusSuccessfully() {
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]}}",
45 when(udpSenderService.sendMessage("V3{\"sn\":\"serial\", \"cmd\": 90}", "127.0.0.1"))
46 .thenReturn(CompletableFuture.completedFuture(statusString));
49 CompletableFuture<StatusDTO> statusResponse = statusService.queryStatus("serial", "127.0.0.1");
52 assertEquals(status, statusResponse.getNow(new StatusDTO()));
56 public void invalidUdpResponse() {
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));
63 CompletableFuture<StatusDTO> futureStatus = statusService.queryStatus("serial", "127.0.0.1");
66 StatusDTO status = futureStatus.getNow(new StatusDTO());
67 assertEquals(503, status.getResponseCode());