]> git.basschouten.com Git - openhab-addons.git/blob
dbcc2749a6416b157d1d9199a255eb36f3c661fe
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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
20 import javax.ws.rs.client.ClientBuilder;
21
22 import org.junit.jupiter.api.BeforeEach;
23 import org.junit.jupiter.api.Test;
24 import org.junit.jupiter.api.extension.ExtendWith;
25 import org.mockito.ArgumentCaptor;
26 import org.mockito.Mock;
27 import org.mockito.junit.jupiter.MockitoExtension;
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 @ExtendWith(MockitoExtension.class)
46 public class NestBridgeHandlerTest {
47
48     private ThingHandler handler;
49
50     private @Mock Bridge bridge;
51     private @Mock ThingHandlerCallback callback;
52     private @Mock ClientBuilder clientBuilder;
53     private @Mock Configuration configuration;
54     private @Mock SseEventSourceFactory eventSourceFactory;
55     private @Mock NestRedirectUrlSupplier redirectUrlSupplier;
56
57     @BeforeEach
58     public void beforeEach() {
59         handler = new NestTestBridgeHandler(bridge, clientBuilder, eventSourceFactory, "http://localhost");
60         handler.setCallback(callback);
61     }
62
63     @SuppressWarnings("null")
64     @Test
65     public void initializeShouldCallTheCallback() {
66         when(bridge.getConfiguration()).thenReturn(configuration);
67         NestBridgeConfiguration bridgeConfig = new NestBridgeConfiguration();
68         when(configuration.as(eq(NestBridgeConfiguration.class))).thenReturn(bridgeConfig);
69         bridgeConfig.accessToken = "my token";
70
71         // we expect the handler#initialize method to call the callback during execution and
72         // pass it the thing and a ThingStatusInfo object containing the ThingStatus of the thing.
73         handler.initialize();
74
75         // the argument captor will capture the argument of type ThingStatusInfo given to the
76         // callback#statusUpdated method.
77         ArgumentCaptor<ThingStatusInfo> statusInfoCaptor = ArgumentCaptor.forClass(ThingStatusInfo.class);
78
79         // verify the interaction with the callback and capture the ThingStatusInfo argument:
80         verify(callback).statusUpdated(eq(bridge), statusInfoCaptor.capture());
81         // assert that the ThingStatusInfo given to the callback was build with the UNKNOWN status:
82         ThingStatusInfo thingStatusInfo = statusInfoCaptor.getValue();
83         assertThat(thingStatusInfo.getStatus(), is(equalTo(ThingStatus.UNKNOWN)));
84     }
85 }