]> git.basschouten.com Git - openhab-addons.git/blob
35f1ade4227e5b840f60cbca30cd6adc4fff2454
[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.nest.handler;
14
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.mockito.ArgumentMatchers.eq;
18 import static org.mockito.Mockito.*;
19 import static org.mockito.MockitoAnnotations.openMocks;
20
21 import javax.ws.rs.client.ClientBuilder;
22
23 import org.junit.jupiter.api.AfterEach;
24 import org.junit.jupiter.api.BeforeEach;
25 import org.junit.jupiter.api.Test;
26 import org.mockito.ArgumentCaptor;
27 import org.mockito.Mock;
28 import org.openhab.binding.nest.internal.config.NestBridgeConfiguration;
29 import org.openhab.binding.nest.internal.handler.NestBridgeHandler;
30 import org.openhab.binding.nest.internal.handler.NestRedirectUrlSupplier;
31 import org.openhab.binding.nest.test.NestTestBridgeHandler;
32 import org.openhab.core.config.core.Configuration;
33 import org.openhab.core.thing.Bridge;
34 import org.openhab.core.thing.ThingStatus;
35 import org.openhab.core.thing.ThingStatusInfo;
36 import org.openhab.core.thing.binding.ThingHandler;
37 import org.openhab.core.thing.binding.ThingHandlerCallback;
38 import org.osgi.service.jaxrs.client.SseEventSourceFactory;
39
40 /**
41  * Tests cases for {@link NestBridgeHandler}.
42  *
43  * @author David Bennett - Initial contribution
44  */
45 public class NestBridgeHandlerTest {
46
47     private ThingHandler handler;
48
49     private AutoCloseable mocksCloseable;
50
51     private @Mock Bridge bridge;
52     private @Mock ThingHandlerCallback callback;
53     private @Mock ClientBuilder clientBuilder;
54     private @Mock Configuration configuration;
55     private @Mock SseEventSourceFactory eventSourceFactory;
56     private @Mock NestRedirectUrlSupplier redirectUrlSupplier;
57
58     @BeforeEach
59     public void beforeEach() {
60         mocksCloseable = openMocks(this);
61         handler = new NestTestBridgeHandler(bridge, clientBuilder, eventSourceFactory, "http://localhost");
62         handler.setCallback(callback);
63     }
64
65     @AfterEach
66     public void afterEach() throws Exception {
67         mocksCloseable.close();
68     }
69
70     @SuppressWarnings("null")
71     @Test
72     public void initializeShouldCallTheCallback() {
73         when(bridge.getConfiguration()).thenReturn(configuration);
74         NestBridgeConfiguration bridgeConfig = new NestBridgeConfiguration();
75         when(configuration.as(eq(NestBridgeConfiguration.class))).thenReturn(bridgeConfig);
76         bridgeConfig.accessToken = "my token";
77
78         // we expect the handler#initialize method to call the callback during execution and
79         // pass it the thing and a ThingStatusInfo object containing the ThingStatus of the thing.
80         handler.initialize();
81
82         // the argument captor will capture the argument of type ThingStatusInfo given to the
83         // callback#statusUpdated method.
84         ArgumentCaptor<ThingStatusInfo> statusInfoCaptor = ArgumentCaptor.forClass(ThingStatusInfo.class);
85
86         // verify the interaction with the callback and capture the ThingStatusInfo argument:
87         verify(callback).statusUpdated(eq(bridge), statusInfoCaptor.capture());
88         // assert that the ThingStatusInfo given to the callback was build with the UNKNOWN status:
89         ThingStatusInfo thingStatusInfo = statusInfoCaptor.getValue();
90         assertThat(thingStatusInfo.getStatus(), is(equalTo(ThingStatus.UNKNOWN)));
91     }
92 }