]> git.basschouten.com Git - openhab-addons.git/blob
44590a02ffc52dd8e36abbbca16e763eb7abf4ad
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.internal.wwn.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.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;
38
39 /**
40  * Tests cases for {@link WWNAccountHandler}.
41  *
42  * @author David Bennett - Initial contribution
43  */
44 @ExtendWith(MockitoExtension.class)
45 @NonNullByDefault
46 public class WWNAccountHandlerTest {
47
48     private @NonNullByDefault({}) ThingHandler handler;
49
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;
55
56     @BeforeEach
57     public void beforeEach() {
58         handler = new WWNTestAccountHandler(bridge, clientBuilder, eventSourceFactory, "http://localhost");
59         handler.setCallback(callback);
60     }
61
62     @Test
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";
68
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.
71         handler.initialize();
72
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);
76
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)));
82     }
83 }