]> git.basschouten.com Git - openhab-addons.git/blob
032f2645b48236e0af9f2ddc470463d86390be32
[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.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;
20
21 import java.util.Collections;
22 import java.util.List;
23 import java.util.concurrent.CompletableFuture;
24 import java.util.concurrent.ExecutionException;
25
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;
30
31 /**
32  * @author Andi Bräu - Initial contribution
33  */
34 @NonNullByDefault
35 public class RevogiDiscoveryServiceTest {
36
37     private final UdpSenderService udpSenderService = mock(UdpSenderService.class);
38     private final RevogiDiscoveryService revogiDiscoveryService = new RevogiDiscoveryService(udpSenderService);
39
40     @Test
41     public void discoverSmartStripSuccesfully() {
42         // given
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\"}}",
46                 "127.0.0.1"));
47         when(udpSenderService.broadcastUdpDatagram("00sw=all,,,;"))
48                 .thenReturn(CompletableFuture.completedFuture(discoveryString));
49
50         // when
51         CompletableFuture<List<DiscoveryRawResponseDTO>> discoverSmartStripsFutures = revogiDiscoveryService
52                 .discoverSmartStrips();
53
54         // then
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"));
59     }
60
61     @Test
62     public void invalidUdpResponse() throws ExecutionException, InterruptedException {
63         // given
64         List<UdpResponseDTO> discoveryString = List.of(new UdpResponseDTO("something invalid", "12345"));
65         when(udpSenderService.broadcastUdpDatagram("00sw=all,,,;"))
66                 .thenReturn(CompletableFuture.completedFuture(discoveryString));
67
68         // when
69         CompletableFuture<List<DiscoveryRawResponseDTO>> futureList = revogiDiscoveryService.discoverSmartStrips();
70
71         // then
72         List<DiscoveryRawResponseDTO> discoverSmartStrips = futureList.get();
73         assertThat(discoverSmartStrips.isEmpty(), is(true));
74     }
75 }