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.hamcrest.CoreMatchers.equalTo;
16 import static org.hamcrest.CoreMatchers.is;
17 import static org.hamcrest.MatcherAssert.assertThat;
18 import static org.mockito.Mockito.mock;
19 import static org.mockito.Mockito.when;
21 import java.util.Collections;
22 import java.util.List;
23 import java.util.concurrent.CompletableFuture;
24 import java.util.concurrent.ExecutionException;
26 import org.eclipse.jdt.annotation.NonNullByDefault;
27 import org.junit.jupiter.api.Test;
28 import org.openhab.binding.revogi.internal.udp.UdpResponseDTO;
29 import org.openhab.binding.revogi.internal.udp.UdpSenderService;
32 * @author Andi Bräu - Initial contribution
35 public class RevogiDiscoveryServiceTest {
37 private final UdpSenderService udpSenderService = mock(UdpSenderService.class);
38 private final RevogiDiscoveryService revogiDiscoveryService = new RevogiDiscoveryService(udpSenderService);
41 public void discoverSmartStripSuccesfully() {
43 DiscoveryResponseDTO discoveryResponse = new DiscoveryResponseDTO("1234", "reg", "sak", "Strip", "mac", "5.11");
44 List<UdpResponseDTO> discoveryString = List.of(new UdpResponseDTO(
45 "{\"response\":0,\"data\":{\"sn\":\"1234\",\"regid\":\"reg\",\"sak\":\"sak\",\"name\":\"Strip\",\"mac\":\"mac\",\"ver\":\"5.11\"}}",
47 when(udpSenderService.broadcastUdpDatagram("00sw=all,,,;"))
48 .thenReturn(CompletableFuture.completedFuture(discoveryString));
51 CompletableFuture<List<DiscoveryRawResponseDTO>> discoverSmartStripsFutures = revogiDiscoveryService
52 .discoverSmartStrips();
55 List<DiscoveryRawResponseDTO> discoverSmartStrips = discoverSmartStripsFutures.getNow(Collections.emptyList());
56 assertThat(discoverSmartStrips.size(), equalTo(1));
57 assertThat(discoverSmartStrips.get(0).getData(), equalTo(discoveryResponse));
58 assertThat(discoverSmartStrips.get(0).getIpAddress(), equalTo("127.0.0.1"));
62 public void invalidUdpResponse() throws ExecutionException, InterruptedException {
64 List<UdpResponseDTO> discoveryString = List.of(new UdpResponseDTO("something invalid", "12345"));
65 when(udpSenderService.broadcastUdpDatagram("00sw=all,,,;"))
66 .thenReturn(CompletableFuture.completedFuture(discoveryString));
69 CompletableFuture<List<DiscoveryRawResponseDTO>> futureList = revogiDiscoveryService.discoverSmartStrips();
72 List<DiscoveryRawResponseDTO> discoverSmartStrips = futureList.get();
73 assertThat(discoverSmartStrips.isEmpty(), is(true));