2 * Copyright (c) 2010-2021 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.nest.handler;
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;
21 import javax.ws.rs.client.ClientBuilder;
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;
41 * Tests cases for {@link NestBridgeHandler}.
43 * @author David Bennett - Initial contribution
45 public class NestBridgeHandlerTest {
47 private ThingHandler handler;
49 private AutoCloseable mocksCloseable;
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;
59 public void beforeEach() {
60 mocksCloseable = openMocks(this);
61 handler = new NestTestBridgeHandler(bridge, clientBuilder, eventSourceFactory, "http://localhost");
62 handler.setCallback(callback);
66 public void afterEach() throws Exception {
67 mocksCloseable.close();
70 @SuppressWarnings("null")
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";
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.
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);
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)));