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