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.Collections;
21 import java.util.List;
22 import java.util.concurrent.CompletableFuture;
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;
30 * @author Andi Bräu - Initial contribution
33 public class StatusServiceTest {
35 private final UdpSenderService udpSenderService = mock(UdpSenderService.class);
36 private final StatusService statusService = new StatusService(udpSenderService);
39 public void getStatusSuccessfully() {
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]}}",
46 when(udpSenderService.sendMessage("V3{\"sn\":\"serial\", \"cmd\": 90}", "127.0.0.1"))
47 .thenReturn(CompletableFuture.completedFuture(statusString));
50 CompletableFuture<StatusDTO> statusResponse = statusService.queryStatus("serial", "127.0.0.1");
53 assertEquals(status, statusResponse.getNow(new StatusDTO()));
57 public void invalidUdpResponse() {
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));
64 CompletableFuture<StatusDTO> futureStatus = statusService.queryStatus("serial", "127.0.0.1");
67 StatusDTO status = futureStatus.getNow(new StatusDTO());
68 assertEquals(503, status.getResponseCode());