2 * Copyright (c) 2010-2020 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.dwdunwetter;
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.*;
20 import java.io.InputStream;
21 import java.util.List;
22 import java.util.Objects;
24 import javax.xml.parsers.DocumentBuilder;
25 import javax.xml.parsers.DocumentBuilderFactory;
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;
53 * Test cases for {@link DwdUnwetterHandler}. The tests provide mocks for supporting entities using Mockito.
55 * @author Martin Koehler - Initial contribution
57 @ExtendWith(MockitoExtension.class)
58 @MockitoSettings(strictness = Strictness.WARN)
59 public class DwdUnwetterHandlerTest extends JavaTest {
61 private ThingHandler handler;
63 private @Mock ThingHandlerCallback callback;
64 private @Mock Thing thing;
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);
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.
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);
88 // verify the interaction with the callback and capture the ThingStatusInfo argument:
90 verify(callback, times(1)).statusUpdated(eq(thing), statusInfoCaptor.capture());
93 // assert that the (temporary) UNKNOWN status was to the mocked thing first:
94 assertThat(statusInfoCaptor.getAllValues().get(0).getStatus(), is(ThingStatus.UNKNOWN));
98 * Tests that the labels of the channels are equal to the ChannelType Definition
101 public void testLabels() throws Exception {
102 handler.initialize();
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");
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));
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) {
125 if (Objects.equals(nodeId.getTextContent(), uuid.getId())) {
126 return getLabel(node.getChildNodes());
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();