2 * Copyright (c) 2010-2021 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.mqtt.homie.generic.internal.mapping;
15 import static org.hamcrest.CoreMatchers.is;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.mockito.ArgumentMatchers.*;
18 import static org.mockito.Mockito.*;
20 import java.util.concurrent.CompletableFuture;
21 import java.util.function.Function;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.junit.jupiter.api.Test;
25 import org.junit.jupiter.api.extension.ExtendWith;
26 import org.mockito.Mock;
27 import org.mockito.junit.jupiter.MockitoExtension;
28 import org.mockito.junit.jupiter.MockitoSettings;
29 import org.mockito.quality.Strictness;
30 import org.openhab.binding.mqtt.generic.tools.ChildMap;
31 import org.openhab.binding.mqtt.homie.internal.handler.ThingChannelConstants;
32 import org.openhab.binding.mqtt.homie.internal.homie300.DeviceCallback;
33 import org.openhab.binding.mqtt.homie.internal.homie300.Node;
34 import org.openhab.binding.mqtt.homie.internal.homie300.NodeAttributes;
37 * Tests cases for {@link HomieChildMap}.
39 * @author David Graeff - Initial contribution
41 @ExtendWith(MockitoExtension.class)
42 @MockitoSettings(strictness = Strictness.WARN)
43 public class HomieChildMapTests {
44 private @Mock DeviceCallback callback;
46 private final String deviceID = ThingChannelConstants.TEST_HOMIE_THING.getId();
47 private final String deviceTopic = "homie/" + deviceID;
49 // A completed future is returned for a subscribe call to the attributes
50 final CompletableFuture<@Nullable Void> future = CompletableFuture.completedFuture(null);
52 ChildMap<Node> subject = new ChildMap<>();
54 private Node createNode(String id) {
55 Node node = new Node(deviceTopic, id, ThingChannelConstants.TEST_HOMIE_THING, callback,
56 spy(new NodeAttributes()));
57 doReturn(future).when(node.attributes).subscribeAndReceive(any(), any(), anyString(), any(), anyInt());
58 doReturn(future).when(node.attributes).unsubscribe();
62 private void removedNode(Node node) {
63 callback.nodeRemoved(node);
66 public static class AddedAction implements Function<Node, CompletableFuture<Void>> {
68 public CompletableFuture<Void> apply(Node t) {
69 return CompletableFuture.completedFuture(null);
74 public void testArrayToSubtopicCreateAndRemove() {
75 AddedAction addedAction = spy(new AddedAction());
77 // Assign "abc,def" to the
78 subject.apply(new String[] { "abc", "def" }, addedAction, this::createNode, this::removedNode);
80 assertThat(future.isDone(), is(true));
81 assertThat(subject.get("abc").nodeID, is("abc"));
82 assertThat(subject.get("def").nodeID, is("def"));
84 verify(addedAction, times(2)).apply(any());
86 Node soonToBeRemoved = subject.get("def");
87 subject.apply(new String[] { "abc" }, addedAction, this::createNode, this::removedNode);
88 verify(callback).nodeRemoved(eq(soonToBeRemoved));