]> git.basschouten.com Git - openhab-addons.git/blob
7673057cc518c894066b16b0d90faefb18865714
[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.dwdunwetter;
14
15 import static org.hamcrest.CoreMatchers.is;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.mockito.ArgumentMatchers.eq;
18 import static org.mockito.Mockito.*;
19
20 import java.io.InputStream;
21 import java.util.List;
22 import java.util.Objects;
23
24 import javax.xml.parsers.DocumentBuilder;
25 import javax.xml.parsers.DocumentBuilderFactory;
26
27 import org.hamcrest.CoreMatchers;
28 import org.junit.jupiter.api.BeforeEach;
29 import org.junit.jupiter.api.Test;
30 import org.junit.jupiter.api.extension.ExtendWith;
31 import org.mockito.ArgumentCaptor;
32 import org.mockito.Mock;
33 import org.mockito.junit.jupiter.MockitoExtension;
34 import org.mockito.junit.jupiter.MockitoSettings;
35 import org.mockito.quality.Strictness;
36 import org.openhab.binding.dwdunwetter.internal.DwdUnwetterBindingConstants;
37 import org.openhab.binding.dwdunwetter.internal.handler.DwdUnwetterHandler;
38 import org.openhab.core.config.core.Configuration;
39 import org.openhab.core.test.java.JavaTest;
40 import org.openhab.core.thing.Channel;
41 import org.openhab.core.thing.Thing;
42 import org.openhab.core.thing.ThingStatus;
43 import org.openhab.core.thing.ThingStatusInfo;
44 import org.openhab.core.thing.ThingUID;
45 import org.openhab.core.thing.binding.ThingHandler;
46 import org.openhab.core.thing.binding.ThingHandlerCallback;
47 import org.openhab.core.thing.type.ChannelTypeUID;
48 import org.w3c.dom.Document;
49 import org.w3c.dom.Node;
50 import org.w3c.dom.NodeList;
51
52 /**
53  * Test cases for {@link DwdUnwetterHandler}. The tests provide mocks for supporting entities using Mockito.
54  *
55  * @author Martin Koehler - Initial contribution
56  */
57 @ExtendWith(MockitoExtension.class)
58 @MockitoSettings(strictness = Strictness.WARN)
59 public class DwdUnwetterHandlerTest extends JavaTest {
60
61     private ThingHandler handler;
62
63     private @Mock ThingHandlerCallback callback;
64     private @Mock Thing thing;
65
66     @BeforeEach
67     public void setUp() {
68         handler = new DwdUnwetterHandler(thing);
69         handler.setCallback(callback);
70         // mock getConfiguration to prevent NPEs
71         when(thing.getUID()).thenReturn(new ThingUID(DwdUnwetterBindingConstants.BINDING_ID, "test"));
72         Configuration configuration = new Configuration();
73         configuration.put("refresh", Integer.valueOf("1"));
74         configuration.put("warningCount", Integer.valueOf("1"));
75         when(thing.getConfiguration()).thenReturn(configuration);
76     }
77
78     @Test
79     public void testInitializeShouldCallTheCallback() {
80         // we expect the handler#initialize method to call the callback during execution and
81         // pass it the thing and a ThingStatusInfo object containing the ThingStatus of the thing.
82         handler.initialize();
83
84         // the argument captor will capture the argument of type ThingStatusInfo given to the
85         // callback#statusUpdated method.
86         ArgumentCaptor<ThingStatusInfo> statusInfoCaptor = ArgumentCaptor.forClass(ThingStatusInfo.class);
87
88         // verify the interaction with the callback and capture the ThingStatusInfo argument:
89         waitForAssert(() -> {
90             verify(callback, times(1)).statusUpdated(eq(thing), statusInfoCaptor.capture());
91         });
92
93         // assert that the (temporary) UNKNOWN status was to the mocked thing first:
94         assertThat(statusInfoCaptor.getAllValues().get(0).getStatus(), is(ThingStatus.UNKNOWN));
95     }
96
97     /**
98      * Tests that the labels of the channels are equal to the ChannelType Definition
99      */
100     @Test
101     public void testLabels() throws Exception {
102         handler.initialize();
103
104         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
105         DocumentBuilder builder = factory.newDocumentBuilder();
106         InputStream stream = getClass().getResourceAsStream("/OH-INF/thing/thing-types.xml");
107         Document document = builder.parse(stream);
108         NodeList nodeList = document.getElementsByTagName("channel-type");
109
110         thing = handler.getThing();
111         List<Channel> channels = thing.getChannels();
112         for (Channel channel : channels) {
113             String label = getLabel(nodeList, channel.getChannelTypeUID());
114             assertThat(channel.getLabel(), CoreMatchers.startsWith(label));
115         }
116     }
117
118     private String getLabel(NodeList nodeList, ChannelTypeUID uuid) {
119         for (int i = 0; i < nodeList.getLength(); i++) {
120             Node node = nodeList.item(i);
121             Node nodeId = node.getAttributes().getNamedItem("id");
122             if (nodeId == null) {
123                 continue;
124             }
125             if (Objects.equals(nodeId.getTextContent(), uuid.getId())) {
126                 return getLabel(node.getChildNodes());
127             }
128         }
129         return null;
130     }
131
132     private String getLabel(NodeList nodeList) {
133         for (int i = 0; i < nodeList.getLength(); i++) {
134             Node node = nodeList.item(i);
135             if (node.getNodeName().equals("label")) {
136                 return node.getTextContent();
137             }
138         }
139         return null;
140     }
141 }