]> git.basschouten.com Git - openhab-addons.git/blob
7cb1ccc46dbbde94e935f5760ff32a41e6d6a8c3
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.awattar.internal.handler;
14
15 import static org.mockito.ArgumentMatchers.*;
16 import static org.mockito.Mockito.*;
17
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.nio.charset.StandardCharsets;
21 import java.time.ZoneId;
22 import java.util.concurrent.ExecutionException;
23 import java.util.concurrent.TimeUnit;
24 import java.util.concurrent.TimeoutException;
25
26 import org.eclipse.jdt.annotation.NonNullByDefault;
27 import org.eclipse.jetty.client.HttpClient;
28 import org.eclipse.jetty.client.api.ContentResponse;
29 import org.eclipse.jetty.client.api.Request;
30 import org.eclipse.jetty.http.HttpMethod;
31 import org.eclipse.jetty.http.HttpStatus;
32 import org.junit.jupiter.api.BeforeEach;
33 import org.junit.jupiter.api.Test;
34 import org.junit.jupiter.api.extension.ExtendWith;
35 import org.mockito.Mock;
36 import org.mockito.junit.jupiter.MockitoExtension;
37 import org.mockito.junit.jupiter.MockitoSettings;
38 import org.mockito.quality.Strictness;
39 import org.openhab.binding.awattar.internal.AwattarBindingConstants;
40 import org.openhab.core.i18n.TimeZoneProvider;
41 import org.openhab.core.test.java.JavaTest;
42 import org.openhab.core.thing.Bridge;
43 import org.openhab.core.thing.Thing;
44 import org.openhab.core.thing.ThingStatus;
45 import org.openhab.core.thing.ThingStatusDetail;
46 import org.openhab.core.thing.ThingStatusInfo;
47 import org.openhab.core.thing.ThingUID;
48 import org.openhab.core.thing.binding.ThingHandlerCallback;
49
50 /**
51  * The {@link AwattarBridgeHandlerRefreshTest} contains tests for the {@link AwattarBridgeHandler} refresh logic.
52  *
53  * @author Thomas Leber - Initial contribution
54  */
55 @ExtendWith(MockitoExtension.class)
56 @MockitoSettings(strictness = Strictness.LENIENT)
57 @NonNullByDefault
58 public class AwattarBridgeHandlerRefreshTest extends JavaTest {
59     public static final ThingUID BRIDGE_UID = new ThingUID(AwattarBindingConstants.THING_TYPE_BRIDGE, "testBridge");
60
61     // bridge mocks
62     private @Mock @NonNullByDefault({}) Bridge bridgeMock;
63     private @Mock @NonNullByDefault({}) ThingHandlerCallback bridgeCallbackMock;
64     private @Mock @NonNullByDefault({}) HttpClient httpClientMock;
65     private @Mock @NonNullByDefault({}) TimeZoneProvider timeZoneProviderMock;
66     private @Mock @NonNullByDefault({}) Request requestMock;
67     private @Mock @NonNullByDefault({}) ContentResponse contentResponseMock;
68
69     // best price handler mocks
70     private @Mock @NonNullByDefault({}) Thing bestpriceMock;
71     private @Mock @NonNullByDefault({}) ThingHandlerCallback bestPriceCallbackMock;
72
73     private @NonNullByDefault({}) AwattarBridgeHandler bridgeHandler;
74
75     @BeforeEach
76     public void setUp() throws IOException, ExecutionException, InterruptedException, TimeoutException {
77         try (InputStream inputStream = AwattarBridgeHandlerRefreshTest.class.getResourceAsStream("api_response.json")) {
78             if (inputStream == null) {
79                 throw new IOException("inputstream is null");
80             }
81             byte[] bytes = inputStream.readAllBytes();
82             if (bytes == null) {
83                 throw new IOException("Resulting byte-array empty");
84             }
85             when(contentResponseMock.getContentAsString()).thenReturn(new String(bytes, StandardCharsets.UTF_8));
86         }
87         when(contentResponseMock.getStatus()).thenReturn(HttpStatus.OK_200);
88         when(httpClientMock.newRequest(anyString())).thenReturn(requestMock);
89         when(requestMock.method(HttpMethod.GET)).thenReturn(requestMock);
90         when(requestMock.timeout(10, TimeUnit.SECONDS)).thenReturn(requestMock);
91         when(requestMock.send()).thenReturn(contentResponseMock);
92
93         when(timeZoneProviderMock.getTimeZone()).thenReturn(ZoneId.of("GMT+2"));
94
95         when(bridgeMock.getUID()).thenReturn(BRIDGE_UID);
96         bridgeHandler = new AwattarBridgeHandler(bridgeMock, httpClientMock, timeZoneProviderMock);
97         bridgeHandler.setCallback(bridgeCallbackMock);
98
99         when(bridgeMock.getHandler()).thenReturn(bridgeHandler);
100
101         // other mocks
102         when(bestpriceMock.getBridgeUID()).thenReturn(BRIDGE_UID);
103
104         when(bestPriceCallbackMock.getBridge(any())).thenReturn(bridgeMock);
105         when(bestPriceCallbackMock.isChannelLinked(any())).thenReturn(true);
106     }
107
108     /**
109      * Test the refreshIfNeeded method with a bridge that is offline.
110      *
111      * @throws SecurityException
112      */
113     @Test
114     void testRefreshIfNeeded_ThingOffline() throws SecurityException {
115         when(bridgeMock.getStatus()).thenReturn(ThingStatus.OFFLINE);
116
117         bridgeHandler.refreshIfNeeded();
118
119         verify(bridgeCallbackMock).statusUpdated(bridgeMock,
120                 new ThingStatusInfo(ThingStatus.ONLINE, ThingStatusDetail.NONE, null));
121     }
122
123     /**
124      * Test the refreshIfNeeded method with a bridge that is online and the data is empty.
125      *
126      * @throws SecurityException
127      */
128     @Test
129     void testRefreshIfNeeded_DataEmptry() throws SecurityException {
130         when(bridgeMock.getStatus()).thenReturn(ThingStatus.ONLINE);
131
132         bridgeHandler.refreshIfNeeded();
133
134         verify(bridgeCallbackMock).statusUpdated(bridgeMock,
135                 new ThingStatusInfo(ThingStatus.ONLINE, ThingStatusDetail.NONE, null));
136     }
137 }