Skip to content

Commit 159a960

Browse files
committed
Enable and fix tests
Signed-off-by: Leo Siepel <leosiepel@gmail.com>
1 parent 252bfa6 commit 159a960

1 file changed

Lines changed: 21 additions & 15 deletions

File tree

itests/org.openhab.binding.homie.tests/src/main/java/org/openhab/binding/mqtt/homie/HomieImplementationTest.java

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static org.mockito.ArgumentMatchers.*;
1919
import static org.mockito.Mockito.*;
2020

21+
import java.nio.charset.StandardCharsets;
2122
import java.util.ArrayList;
2223
import java.util.Collections;
2324
import java.util.List;
@@ -30,7 +31,6 @@
3031
import org.eclipse.jdt.annotation.NonNullByDefault;
3132
import org.junit.jupiter.api.AfterEach;
3233
import org.junit.jupiter.api.BeforeEach;
33-
import org.junit.jupiter.api.Disabled;
3434
import org.junit.jupiter.api.Test;
3535
import org.junit.jupiter.api.extension.ExtendWith;
3636
import org.mockito.Mock;
@@ -55,6 +55,7 @@
5555
import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
5656
import org.openhab.core.io.transport.mqtt.MqttConnectionObserver;
5757
import org.openhab.core.io.transport.mqtt.MqttConnectionState;
58+
import org.openhab.core.io.transport.mqtt.MqttMessageSubscriber;
5859
import org.openhab.core.library.types.OnOffType;
5960
import org.openhab.core.library.types.QuantityType;
6061
import org.openhab.core.library.unit.SIUnits;
@@ -72,6 +73,7 @@ public class HomieImplementationTest extends MqttOSGiTest {
7273
private static final String BASE_TOPIC = "homie";
7374
private static final String DEVICE_ID = ThingChannelConstants.TEST_HOMIE_THING.getId();
7475
private static final String DEVICE_TOPIC = BASE_TOPIC + "/" + DEVICE_ID;
76+
private static final int MQTT_TIMEOUT = 10000;
7577

7678
private @NonNullByDefault({}) MqttBrokerConnection homieConnection;
7779
private int registeredTopics = 100;
@@ -165,15 +167,13 @@ public void retrieveAllTopics() throws Exception {
165167
"Connection " + homieConnection.getClientId() + " not retrieving all topics ");
166168
}
167169

168-
@Disabled("https://github.com/openhab/openhab-addons/issues/12667")
169170
@Test
170171
public void retrieveOneAttribute() throws Exception {
171172
WaitForTopicValue watcher = new WaitForTopicValue(homieConnection, DEVICE_TOPIC + "/$homie");
172-
assertThat(watcher.waitForTopicValue(1000), is("3.0"));
173+
assertThat(watcher.waitForTopicValue(MQTT_TIMEOUT), is("3.0"));
173174
}
174175

175176
@SuppressWarnings("null")
176-
@Disabled("Temporarily disabled: unstable")
177177
@Test
178178
public void retrieveAttributes() throws Exception {
179179
assertThat(homieConnection.hasSubscribers(), is(false));
@@ -183,9 +183,6 @@ public void retrieveAttributes() throws Exception {
183183
Property property = spy(
184184
new Property(DEVICE_TOPIC + "/testnode", node, "temperature", callback, new PropertyAttributes()));
185185

186-
// Create a scheduler
187-
ScheduledExecutorService scheduler = new ScheduledThreadPoolExecutor(4);
188-
189186
property.subscribe(homieConnection, scheduler, 500).get();
190187

191188
assertThat(property.attributes.settable, is(true));
@@ -202,8 +199,8 @@ public void retrieveAttributes() throws Exception {
202199

203200
property.startChannel(homieConnection, scheduler, 500).get();
204201
verify(channelState).start(any(), any(), anyInt());
205-
verify(channelState, timeout(500)).processMessage(any(), any());
206-
verify(callback).updateChannelState(any(), any());
202+
verify(callback, timeout(MQTT_TIMEOUT)).updateChannelState(any(), any());
203+
verify(channelState).processMessage(any(), any());
207204

208205
assertThat(property.getChannelState().getCache().getChannelState(),
209206
is(new QuantityType<>(10, SIUnits.CELSIUS)));
@@ -231,7 +228,6 @@ public Node createSpyNode(InvocationOnMock invocation) {
231228
}
232229

233230
@SuppressWarnings("null")
234-
@Disabled("Temporarily disabled: unstable")
235231
@Test
236232
public void parseHomieTree() throws Exception {
237233
// Create a Homie Device object. Because spied Nodes are required for call verification,
@@ -297,14 +293,24 @@ public void parseHomieTree() throws Exception {
297293
device.startChannels(homieConnection, scheduler, 50, handler).get();
298294
assertThat(propertyBell.getChannelState().isStateful(), is(false));
299295
assertThat(propertyBell.getChannelState().getCache().getChannelState(), is(UnDefType.UNDEF));
300-
assertThat(property.getChannelState().getCache().getChannelState(),
301-
is(new QuantityType<>(10, SIUnits.CELSIUS)));
296+
Property temperatureProperty = property;
297+
waitForAssert(() -> assertThat(temperatureProperty.getChannelState().getCache().getChannelState(),
298+
is(new QuantityType<>(10, SIUnits.CELSIUS))));
302299

303300
property = node.properties.get("testRetain");
304-
WaitForTopicValue watcher = new WaitForTopicValue(brokerConnection, propertyTestTopic + "/set");
301+
String commandTopic = propertyTestTopic + "/set";
302+
CompletableFuture<String> receivedValue = new CompletableFuture<>();
303+
MqttMessageSubscriber subscriber = (topic, payload) -> receivedValue
304+
.complete(new String(payload, StandardCharsets.UTF_8));
305+
assertTrue(brokerConnection.subscribe(commandTopic, subscriber).get(5, TimeUnit.SECONDS));
306+
305307
// Watch the topic. Publish a retain=false value to MQTT
306-
property.getChannelState().publishValue(OnOffType.OFF).get();
307-
assertThat(watcher.waitForTopicValue(10000), is("false"));
308+
try {
309+
property.getChannelState().publishValue(OnOffType.OFF).get();
310+
assertThat(receivedValue.get(MQTT_TIMEOUT, TimeUnit.MILLISECONDS), is("false"));
311+
} finally {
312+
brokerConnection.unsubscribe(commandTopic, subscriber).get(5, TimeUnit.SECONDS);
313+
}
308314

309315
// Publish a retain=false value to MQTT.
310316
property.getChannelState().publishValue(OnOffType.ON).get();

0 commit comments

Comments
 (0)