2 * Copyright (c) 2010-2024 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.awattar.internal.handler;
15 import static org.mockito.ArgumentMatchers.*;
16 import static org.mockito.Mockito.*;
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;
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;
51 * The {@link AwattarBridgeHandlerRefreshTest} contains tests for the {@link AwattarBridgeHandler} refresh logic.
53 * @author Thomas Leber - Initial contribution
55 @ExtendWith(MockitoExtension.class)
56 @MockitoSettings(strictness = Strictness.LENIENT)
58 public class AwattarBridgeHandlerRefreshTest extends JavaTest {
59 public static final ThingUID BRIDGE_UID = new ThingUID(AwattarBindingConstants.THING_TYPE_BRIDGE, "testBridge");
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;
69 // best price handler mocks
70 private @Mock @NonNullByDefault({}) Thing bestpriceMock;
71 private @Mock @NonNullByDefault({}) ThingHandlerCallback bestPriceCallbackMock;
73 private @NonNullByDefault({}) AwattarBridgeHandler bridgeHandler;
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");
81 byte[] bytes = inputStream.readAllBytes();
83 throw new IOException("Resulting byte-array empty");
85 when(contentResponseMock.getContentAsString()).thenReturn(new String(bytes, StandardCharsets.UTF_8));
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);
93 when(timeZoneProviderMock.getTimeZone()).thenReturn(ZoneId.of("GMT+2"));
95 when(bridgeMock.getUID()).thenReturn(BRIDGE_UID);
96 bridgeHandler = new AwattarBridgeHandler(bridgeMock, httpClientMock, timeZoneProviderMock);
97 bridgeHandler.setCallback(bridgeCallbackMock);
99 when(bridgeMock.getHandler()).thenReturn(bridgeHandler);
102 when(bestpriceMock.getBridgeUID()).thenReturn(BRIDGE_UID);
104 when(bestPriceCallbackMock.getBridge(any())).thenReturn(bridgeMock);
105 when(bestPriceCallbackMock.isChannelLinked(any())).thenReturn(true);
109 * Test the refreshIfNeeded method with a bridge that is offline.
111 * @throws SecurityException
114 void testRefreshIfNeeded_ThingOffline() throws SecurityException {
115 when(bridgeMock.getStatus()).thenReturn(ThingStatus.OFFLINE);
117 bridgeHandler.refreshIfNeeded();
119 verify(bridgeCallbackMock).statusUpdated(bridgeMock,
120 new ThingStatusInfo(ThingStatus.ONLINE, ThingStatusDetail.NONE, null));
124 * Test the refreshIfNeeded method with a bridge that is online and the data is empty.
126 * @throws SecurityException
129 void testRefreshIfNeeded_DataEmptry() throws SecurityException {
130 when(bridgeMock.getStatus()).thenReturn(ThingStatus.ONLINE);
132 bridgeHandler.refreshIfNeeded();
134 verify(bridgeCallbackMock).statusUpdated(bridgeMock,
135 new ThingStatusInfo(ThingStatus.ONLINE, ThingStatusDetail.NONE, null));