]> git.basschouten.com Git - openhab-addons.git/blob
ac092abe65ff3ab3b80cdb9ed8ef0d3d29a297f4
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.hamcrest.MatcherAssert.assertThat;
17 import static org.mockito.ArgumentMatchers.*;
18 import static org.mockito.Mockito.*;
19
20 import java.util.concurrent.CompletableFuture;
21 import java.util.function.Function;
22
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;
35
36 /**
37  * Tests cases for {@link HomieChildMap}.
38  *
39  * @author David Graeff - Initial contribution
40  */
41 @ExtendWith(MockitoExtension.class)
42 @MockitoSettings(strictness = Strictness.WARN)
43 public class HomieChildMapTests {
44     private @Mock DeviceCallback callback;
45
46     private final String deviceID = ThingChannelConstants.TEST_HOMIE_THING.getId();
47     private final String deviceTopic = "homie/" + deviceID;
48
49     // A completed future is returned for a subscribe call to the attributes
50     final CompletableFuture<@Nullable Void> future = CompletableFuture.completedFuture(null);
51
52     ChildMap<Node> subject = new ChildMap<>();
53
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();
59         return node;
60     }
61
62     private void removedNode(Node node) {
63         callback.nodeRemoved(node);
64     }
65
66     public static class AddedAction implements Function<Node, CompletableFuture<Void>> {
67         @Override
68         public CompletableFuture<Void> apply(Node t) {
69             return CompletableFuture.completedFuture(null);
70         }
71     }
72
73     @Test
74     public void testArrayToSubtopicCreateAndRemove() {
75         AddedAction addedAction = spy(new AddedAction());
76
77         // Assign "abc,def" to the
78         subject.apply(new String[] { "abc", "def" }, addedAction, this::createNode, this::removedNode);
79
80         assertThat(future.isDone(), is(true));
81         assertThat(subject.get("abc").nodeID, is("abc"));
82         assertThat(subject.get("def").nodeID, is("def"));
83
84         verify(addedAction, times(2)).apply(any());
85
86         Node soonToBeRemoved = subject.get("def");
87         subject.apply(new String[] { "abc" }, addedAction, this::createNode, this::removedNode);
88         verify(callback).nodeRemoved(eq(soonToBeRemoved));
89     }
90 }