2 * Copyright (c) 2010-2022 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.tplinksmarthome.internal;
15 import static org.junit.jupiter.api.Assertions.assertEquals;
16 import static org.mockito.ArgumentMatchers.any;
17 import static org.mockito.Mockito.*;
19 import java.io.IOException;
20 import java.net.DatagramPacket;
21 import java.net.DatagramSocket;
22 import java.net.InetAddress;
23 import java.net.SocketTimeoutException;
24 import java.util.Arrays;
25 import java.util.List;
27 import org.junit.jupiter.api.extension.ExtendWith;
28 import org.junit.jupiter.params.ParameterizedTest;
29 import org.junit.jupiter.params.provider.MethodSource;
30 import org.mockito.ArgumentCaptor;
31 import org.mockito.Mock;
32 import org.mockito.invocation.InvocationOnMock;
33 import org.mockito.junit.jupiter.MockitoExtension;
34 import org.mockito.stubbing.Answer;
35 import org.openhab.binding.tplinksmarthome.internal.model.ModelTestUtil;
36 import org.openhab.core.config.discovery.DiscoveryListener;
37 import org.openhab.core.config.discovery.DiscoveryResult;
40 * Test class for {@link TPLinkSmartHomeDiscoveryService} class.
42 * @author Hilbrand Bouwkamp - Initial contribution
44 @ExtendWith(MockitoExtension.class)
45 public class TPLinkSmartHomeDiscoveryServiceTest {
47 private static final List<Object[]> TESTS = Arrays.asList(
48 new Object[][] { { "bulb_get_sysinfo_response_on", 11 }, { "rangeextender_get_sysinfo_response", 11 } });
50 private @Mock DatagramSocket discoverSocket;
51 private @Mock DiscoveryListener discoveryListener;
53 private TPLinkSmartHomeDiscoveryService discoveryService;
55 public static List<Object[]> data() {
59 public void setUp(String filename) throws IOException {
60 discoveryService = new TPLinkSmartHomeDiscoveryService() {
62 protected DatagramSocket sendDiscoveryPacket() throws IOException {
63 return discoverSocket;
66 doAnswer(new Answer<Void>() {
70 public Void answer(InvocationOnMock invocation) throws Throwable {
72 throw new SocketTimeoutException("Only test 1 thing discovery");
74 DatagramPacket packet = (DatagramPacket) invocation.getArguments()[0];
75 packet.setAddress(InetAddress.getLocalHost());
76 packet.setData(CryptUtil.encrypt(ModelTestUtil.readJson(filename)));
79 }).when(discoverSocket).receive(any());
80 discoveryService.addDiscoveryListener(discoveryListener);
84 * Test if startScan method finds a device with expected properties.
90 public void testScan(String filename, int propertiesSize) throws IOException {
92 discoveryService.startScan();
93 ArgumentCaptor<DiscoveryResult> discoveryResultCaptor = ArgumentCaptor.forClass(DiscoveryResult.class);
94 verify(discoveryListener).thingDiscovered(any(), discoveryResultCaptor.capture());
95 DiscoveryResult discoveryResult = discoveryResultCaptor.getValue();
96 assertEquals(TPLinkSmartHomeBindingConstants.BINDING_ID, discoveryResult.getBindingId(),
97 "Check if correct binding id found");
98 assertEquals(propertiesSize, discoveryResult.getProperties().size(),
99 "Check if expected number of properties found");