2 * Copyright (c) 2010-2020 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.Assert.assertEquals;
16 import static org.mockito.ArgumentMatchers.any;
17 import static org.mockito.Mockito.*;
18 import static org.mockito.MockitoAnnotations.initMocks;
20 import java.io.IOException;
21 import java.net.DatagramPacket;
22 import java.net.DatagramSocket;
23 import java.net.InetAddress;
24 import java.net.SocketTimeoutException;
25 import java.util.Arrays;
26 import java.util.List;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.junit.runners.Parameterized;
32 import org.junit.runners.Parameterized.Parameters;
33 import org.mockito.ArgumentCaptor;
34 import org.mockito.Mock;
35 import org.mockito.invocation.InvocationOnMock;
36 import org.mockito.stubbing.Answer;
37 import org.openhab.binding.tplinksmarthome.internal.model.ModelTestUtil;
38 import org.openhab.core.config.discovery.DiscoveryListener;
39 import org.openhab.core.config.discovery.DiscoveryResult;
42 * Test class for {@link TPLinkSmartHomeDiscoveryService} class.
44 * @author Hilbrand Bouwkamp - Initial contribution
46 @RunWith(value = Parameterized.class)
47 public class TPLinkSmartHomeDiscoveryServiceTest {
49 private static final List<Object[]> TESTS = Arrays.asList(
50 new Object[][] { { "bulb_get_sysinfo_response_on", 11 }, { "rangeextender_get_sysinfo_response", 11 } });
53 private DatagramSocket discoverSocket;
56 private DiscoveryListener discoveryListener;
58 private TPLinkSmartHomeDiscoveryService discoveryService;
60 private final String filename;
61 private final int propertiesSize;
63 public TPLinkSmartHomeDiscoveryServiceTest(String filename, int propertiesSize) {
64 this.filename = filename;
65 this.propertiesSize = propertiesSize;
68 @Parameters(name = "{0}")
69 public static List<Object[]> data() {
74 public void setUp() throws IOException {
76 discoveryService = new TPLinkSmartHomeDiscoveryService() {
78 protected DatagramSocket sendDiscoveryPacket() throws IOException {
79 return discoverSocket;
82 doAnswer(new Answer<Void>() {
86 public Void answer(InvocationOnMock invocation) throws Throwable {
88 throw new SocketTimeoutException("Only test 1 thing discovery");
90 DatagramPacket packet = (DatagramPacket) invocation.getArguments()[0];
91 packet.setAddress(InetAddress.getLocalHost());
92 packet.setData(CryptUtil.encrypt(ModelTestUtil.readJson(filename)));
95 }).when(discoverSocket).receive(any());
96 discoveryService.addDiscoveryListener(discoveryListener);
100 * Test if startScan method finds a device with expected properties.
103 public void testScan() {
104 discoveryService.startScan();
105 ArgumentCaptor<DiscoveryResult> discoveryResultCaptor = ArgumentCaptor.forClass(DiscoveryResult.class);
106 verify(discoveryListener).thingDiscovered(any(), discoveryResultCaptor.capture());
107 DiscoveryResult discoveryResult = discoveryResultCaptor.getValue();
108 assertEquals("Check if correct binding id found", TPLinkSmartHomeBindingConstants.BINDING_ID,
109 discoveryResult.getBindingId());
110 assertEquals("Check if expected number of properties found", propertiesSize,
111 discoveryResult.getProperties().size());