]> git.basschouten.com Git - openhab-addons.git/blob
1443661c0a369e923624ce3b393332f78470bcfa
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.mqtt.homie.generic.internal.mapping;
14
15 import static org.hamcrest.CoreMatchers.is;
16 import static org.junit.Assert.assertThat;
17 import static org.mockito.ArgumentMatchers.*;
18 import static org.mockito.Mockito.*;
19 import static org.mockito.MockitoAnnotations.initMocks;
20
21 import java.util.concurrent.CompletableFuture;
22 import java.util.function.Function;
23
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.Mock;
28 import org.openhab.binding.mqtt.generic.tools.ChildMap;
29 import org.openhab.binding.mqtt.homie.internal.handler.ThingChannelConstants;
30 import org.openhab.binding.mqtt.homie.internal.homie300.DeviceCallback;
31 import org.openhab.binding.mqtt.homie.internal.homie300.Node;
32 import org.openhab.binding.mqtt.homie.internal.homie300.NodeAttributes;
33
34 /**
35  * Tests cases for {@link HomieChildMap}.
36  *
37  * @author David Graeff - Initial contribution
38  */
39 public class HomieChildMapTests {
40     private @Mock DeviceCallback callback;
41
42     private final String deviceID = ThingChannelConstants.TEST_HOMIE_THING.getId();
43     private final String deviceTopic = "homie/" + deviceID;
44
45     // A completed future is returned for a subscribe call to the attributes
46     final CompletableFuture<@Nullable Void> future = CompletableFuture.completedFuture(null);
47
48     ChildMap<Node> subject = new ChildMap<>();
49
50     private Node createNode(String id) {
51         Node node = new Node(deviceTopic, id, ThingChannelConstants.TEST_HOMIE_THING, callback,
52                 spy(new NodeAttributes()));
53         doReturn(future).when(node.attributes).subscribeAndReceive(any(), any(), anyString(), any(), anyInt());
54         doReturn(future).when(node.attributes).unsubscribe();
55         return node;
56     }
57
58     private void removedNode(Node node) {
59         callback.nodeRemoved(node);
60     }
61
62     @Before
63     public void setUp() {
64         initMocks(this);
65     }
66
67     public static class AddedAction implements Function<Node, CompletableFuture<Void>> {
68         @Override
69         public CompletableFuture<Void> apply(Node t) {
70             return CompletableFuture.completedFuture(null);
71         }
72     }
73
74     @Test
75     public void testArrayToSubtopicCreateAndRemove() {
76         AddedAction addedAction = spy(new AddedAction());
77
78         // Assign "abc,def" to the
79         subject.apply(new String[] { "abc", "def" }, addedAction, this::createNode, this::removedNode);
80
81         assertThat(future.isDone(), is(true));
82         assertThat(subject.get("abc").nodeID, is("abc"));
83         assertThat(subject.get("def").nodeID, is("def"));
84
85         verify(addedAction, times(2)).apply(any());
86
87         Node soonToBeRemoved = subject.get("def");
88         subject.apply(new String[] { "abc" }, addedAction, this::createNode, this::removedNode);
89         verify(callback).nodeRemoved(eq(soonToBeRemoved));
90     }
91 }