2 * Copyright (c) 2010-2023 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.internal.wwn.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.*;
20 import javax.ws.rs.client.ClientBuilder;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.junit.jupiter.api.BeforeEach;
24 import org.junit.jupiter.api.Test;
25 import org.junit.jupiter.api.extension.ExtendWith;
26 import org.mockito.ArgumentCaptor;
27 import org.mockito.Mock;
28 import org.mockito.junit.jupiter.MockitoExtension;
29 import org.openhab.binding.nest.internal.wwn.config.WWNAccountConfiguration;
30 import org.openhab.binding.nest.internal.wwn.test.WWNTestAccountHandler;
31 import org.openhab.core.config.core.Configuration;
32 import org.openhab.core.thing.Bridge;
33 import org.openhab.core.thing.ThingStatus;
34 import org.openhab.core.thing.ThingStatusInfo;
35 import org.openhab.core.thing.binding.ThingHandler;
36 import org.openhab.core.thing.binding.ThingHandlerCallback;
37 import org.osgi.service.jaxrs.client.SseEventSourceFactory;
40 * Tests cases for {@link WWNAccountHandler}.
42 * @author David Bennett - Initial contribution
44 @ExtendWith(MockitoExtension.class)
46 public class WWNAccountHandlerTest {
48 private @NonNullByDefault({}) ThingHandler handler;
50 private @Mock @NonNullByDefault({}) Bridge bridge;
51 private @Mock @NonNullByDefault({}) ThingHandlerCallback callback;
52 private @Mock @NonNullByDefault({}) ClientBuilder clientBuilder;
53 private @Mock @NonNullByDefault({}) Configuration configuration;
54 private @Mock @NonNullByDefault({}) SseEventSourceFactory eventSourceFactory;
57 public void beforeEach() {
58 handler = new WWNTestAccountHandler(bridge, clientBuilder, eventSourceFactory, "http://localhost");
59 handler.setCallback(callback);
63 public void initializeShouldCallTheCallback() {
64 when(bridge.getConfiguration()).thenReturn(configuration);
65 WWNAccountConfiguration bridgeConfig = new WWNAccountConfiguration();
66 when(configuration.as(eq(WWNAccountConfiguration.class))).thenReturn(bridgeConfig);
67 bridgeConfig.accessToken = "my token";
69 // we expect the handler#initialize method to call the callback during execution and
70 // pass it the thing and a ThingStatusInfo object containing the ThingStatus of the thing.
73 // the argument captor will capture the argument of type ThingStatusInfo given to the
74 // callback#statusUpdated method.
75 ArgumentCaptor<ThingStatusInfo> statusInfoCaptor = ArgumentCaptor.forClass(ThingStatusInfo.class);
77 // verify the interaction with the callback and capture the ThingStatusInfo argument:
78 verify(callback).statusUpdated(eq(bridge), statusInfoCaptor.capture());
79 // assert that the ThingStatusInfo given to the callback was build with the UNKNOWN status:
80 ThingStatusInfo thingStatusInfo = statusInfoCaptor.getValue();
81 assertThat(thingStatusInfo.getStatus(), is(equalTo(ThingStatus.UNKNOWN)));