Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.time.Instant;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
Expand All @@ -31,7 +33,6 @@
import org.eclipse.jdt.annotation.Nullable;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.openhab.binding.avmfritz.internal.dto.AVMFritzBaseModel;
import org.openhab.binding.avmfritz.internal.dto.DeviceListModel;
Expand All @@ -53,12 +54,12 @@
* @author Ulrich Mertin - Added support for HAN-FUN blinds
*/
@NonNullByDefault
@Disabled("Often blocks indefinitely, see: https://github.com/openhab/openhab-addons/issues/16536")
public class AVMFritzDiscoveryServiceOSGiTest extends AVMFritzThingHandlerOSGiTest {

private static final ThingUID BRIGE_THING_ID = new ThingUID("avmfritz:fritzbox:1");

private @Nullable DiscoveryResult discoveryResult;
private volatile @Nullable DiscoveryResult discoveryResult;
private CountDownLatch discoveryResultAvailable = new CountDownLatch(1);
Comment thread
lsiepel marked this conversation as resolved.
Outdated
private @NonNullByDefault({}) AVMFritzDiscoveryService discovery;

private final DiscoveryListener listener = new DiscoveryListener() {
Expand All @@ -70,6 +71,7 @@ public void thingRemoved(DiscoveryService source, ThingUID thingUID) {
@Override
public void thingDiscovered(DiscoveryService source, DiscoveryResult result) {
discoveryResult = result;
discoveryResultAvailable.countDown();
}

@Override
Expand All @@ -83,6 +85,7 @@ public void thingDiscovered(DiscoveryService source, DiscoveryResult result) {
@BeforeEach
public void setUp() {
super.setUp();
discoveryResultAvailable = new CountDownLatch(1);
discovery = new AVMFritzDiscoveryService(mock(LocaleProvider.class), mock(TranslationProvider.class));
discovery.setThingHandler(bridgeHandler);
discovery.addDiscoveryListener(listener);
Expand All @@ -93,6 +96,15 @@ public void cleanUp() {
discoveryResult = null;
}

private void awaitDiscoveryResult() {
try {
assertTrue(discoveryResultAvailable.await(5, TimeUnit.SECONDS), "Timed out waiting for discovery result");
Comment thread
lsiepel marked this conversation as resolved.
Outdated
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
fail("Interrupted while waiting for discovery result", e);
}
}

@Test
public void correctSupportedTypes() {
assertEquals(23, discovery.getSupportedThingTypes().size());
Expand Down Expand Up @@ -182,6 +194,7 @@ public void validDECTRepeater100Result() throws JAXBException, XMLStreamExceptio
assertEquals(1, device.getPresent());

discovery.onDeviceAdded(device);
awaitDiscoveryResult();
assertNotNull(discoveryResult);

assertEquals(DiscoveryResultFlag.NEW, discoveryResult.getFlag());
Expand Down Expand Up @@ -242,6 +255,7 @@ public void validSmart250DiscoveryResult() throws JAXBException, XMLStreamExcept
assertEquals(1, device.getPresent());

discovery.onDeviceAdded(device);
awaitDiscoveryResult();
assertNotNull(discoveryResult);

assertEquals(DiscoveryResultFlag.NEW, discoveryResult.getFlag());
Expand Down Expand Up @@ -307,6 +321,7 @@ public void validDECT200DiscoveryResult() throws JAXBException, XMLStreamExcepti
assertNotNull(device);

discovery.onDeviceAdded(device);
awaitDiscoveryResult();
assertNotNull(discoveryResult);

assertEquals(DiscoveryResultFlag.NEW, discoveryResult.getFlag());
Expand Down Expand Up @@ -360,6 +375,7 @@ public void validDECT210DiscoveryResult() throws JAXBException, XMLStreamExcepti
assertNotNull(device);

discovery.onDeviceAdded(device);
awaitDiscoveryResult();
assertNotNull(discoveryResult);

assertEquals(DiscoveryResultFlag.NEW, discoveryResult.getFlag());
Expand Down Expand Up @@ -416,6 +432,7 @@ public void validCometDECTDiscoveryResult() throws JAXBException, XMLStreamExcep
assertNotNull(device);

discovery.onDeviceAdded(device);
awaitDiscoveryResult();
assertNotNull(discoveryResult);

assertEquals(DiscoveryResultFlag.NEW, discoveryResult.getFlag());
Expand Down Expand Up @@ -472,6 +489,7 @@ public void validDECT300DiscoveryResult() throws JAXBException, XMLStreamExcepti
assertNotNull(device);

discovery.onDeviceAdded(device);
awaitDiscoveryResult();
assertNotNull(discoveryResult);

assertEquals(DiscoveryResultFlag.NEW, discoveryResult.getFlag());
Expand Down Expand Up @@ -528,6 +546,7 @@ public void validDECT301DiscoveryResult() throws JAXBException, XMLStreamExcepti
assertNotNull(device);

discovery.onDeviceAdded(device);
awaitDiscoveryResult();
assertNotNull(discoveryResult);

assertEquals(DiscoveryResultFlag.NEW, discoveryResult.getFlag());
Expand Down Expand Up @@ -577,6 +596,7 @@ public void validPowerline546EDiscoveryResult() throws JAXBException, XMLStreamE
assertNotNull(device);

discovery.onDeviceAdded(device);
awaitDiscoveryResult();
assertNotNull(discoveryResult);

assertEquals(DiscoveryResultFlag.NEW, discoveryResult.getFlag());
Expand Down Expand Up @@ -650,6 +670,7 @@ public void validHANFUNMagneticContactDiscoveryResult() throws JAXBException, XM
assertNotNull(device);

discovery.onDeviceAdded(device);
awaitDiscoveryResult();
assertNotNull(discoveryResult);

assertEquals(DiscoveryResultFlag.NEW, discoveryResult.getFlag());
Expand Down Expand Up @@ -696,6 +717,7 @@ public void validHANFUNOpticalContactDiscoveryResult() throws JAXBException, XML
assertNotNull(device);

discovery.onDeviceAdded(device);
awaitDiscoveryResult();
assertNotNull(discoveryResult);

assertEquals(DiscoveryResultFlag.NEW, discoveryResult.getFlag());
Expand Down Expand Up @@ -742,6 +764,7 @@ public void validHANFUNMotionSensorDiscoveryResult() throws JAXBException, XMLSt
assertNotNull(device);

discovery.onDeviceAdded(device);
awaitDiscoveryResult();
assertNotNull(discoveryResult);

assertEquals(DiscoveryResultFlag.NEW, discoveryResult.getFlag());
Expand Down Expand Up @@ -788,6 +811,7 @@ public void validHANFUNMSmokeDetectorDiscoveryResult() throws JAXBException, XML
assertNotNull(device);

discovery.onDeviceAdded(device);
awaitDiscoveryResult();
assertNotNull(discoveryResult);

assertEquals(DiscoveryResultFlag.NEW, discoveryResult.getFlag());
Expand Down Expand Up @@ -834,6 +858,7 @@ public void validHANFUNSwitchtDiscoveryResult() throws JAXBException, XMLStreamE
assertNotNull(device);

discovery.onDeviceAdded(device);
awaitDiscoveryResult();
assertNotNull(discoveryResult);

assertEquals(DiscoveryResultFlag.NEW, discoveryResult.getFlag());
Expand Down Expand Up @@ -890,6 +915,7 @@ public void validHANFUNBlindDiscoveryResult() throws JAXBException, XMLStreamExc
assertNotNull(device);

discovery.onDeviceAdded(device);
awaitDiscoveryResult();
assertNotNull(discoveryResult);

assertEquals(DiscoveryResultFlag.NEW, discoveryResult.getFlag());
Expand Down Expand Up @@ -950,6 +976,7 @@ public void validHeatingGroupDiscoveryResult() throws JAXBException, XMLStreamEx
assertNotNull(device);

discovery.onDeviceAdded(device);
awaitDiscoveryResult();
assertNotNull(discoveryResult);

assertEquals(DiscoveryResultFlag.NEW, discoveryResult.getFlag());
Expand Down Expand Up @@ -1005,6 +1032,7 @@ public void validSwitchGroupDiscoveryResult() throws JAXBException, XMLStreamExc
assertNotNull(device);

discovery.onDeviceAdded(device);
awaitDiscoveryResult();
assertNotNull(discoveryResult);

assertEquals(DiscoveryResultFlag.NEW, discoveryResult.getFlag());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,11 @@
import java.util.Map;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jetty.client.HttpClient;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.openhab.binding.avmfritz.internal.AVMFritzDynamicCommandDescriptionProvider;
import org.openhab.core.config.core.Configuration;
import org.openhab.core.test.java.JavaOSGiTest;
import org.openhab.core.test.storage.VolatileStorageService;
import org.openhab.core.thing.Bridge;
import org.openhab.core.thing.ManagedThingProvider;
import org.openhab.core.thing.ThingProvider;
import org.openhab.core.thing.binding.ThingHandler;
import org.openhab.core.thing.binding.ThingHandlerCallback;
import org.openhab.core.thing.binding.builder.BridgeBuilder;

Expand All @@ -43,60 +35,25 @@
@NonNullByDefault
public abstract class AVMFritzThingHandlerOSGiTest extends JavaOSGiTest {

private static HttpClient httpClient = new HttpClient();

private VolatileStorageService volatileStorageService = new VolatileStorageService();
private @NonNullByDefault({}) ManagedThingProvider managedThingProvider;

protected @NonNullByDefault({}) Bridge bridge;
protected @NonNullByDefault({}) BoxHandler bridgeHandler;

@BeforeAll
public static void setUpClass() throws Exception {
httpClient.start();
}

@BeforeEach
public void setUp() {
registerService(volatileStorageService);

managedThingProvider = getService(ThingProvider.class, ManagedThingProvider.class);
assertNotNull(managedThingProvider, "Could not get ManagedThingProvider");

bridge = buildBridge();
assertNotNull(bridge.getConfiguration());

managedThingProvider.add(bridge);

ThingHandlerCallback callback = mock(ThingHandlerCallback.class);

bridgeHandler = new BoxHandler(bridge, httpClient, mock(AVMFritzDynamicCommandDescriptionProvider.class));
bridgeHandler = new BoxHandler(bridge, mock(), mock(AVMFritzDynamicCommandDescriptionProvider.class));
Comment thread
lsiepel marked this conversation as resolved.
assertNotNull(bridgeHandler);

bridgeHandler.setCallback(callback);

ThingHandler oldHandler = bridge.getHandler();
if (oldHandler != null) {
oldHandler.dispose();
}
bridge.setHandler(bridgeHandler);
assertNotNull(bridge.getHandler());

bridgeHandler.initialize();
}

@AfterEach
public void tearDown() {
if (bridge != null) {
managedThingProvider.remove(bridge.getUID());
}

unregisterService(volatileStorageService);
}

@AfterAll
public static void tearDownClass() throws Exception {
httpClient.stop();
// Discovery tests only need the handler's type and UID mapping. Initializing it would authenticate against
// the configured host and make these tests depend on the network.
}

private Bridge buildBridge() {
Expand Down
Loading