-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[aWATTar] Refactor AwattarApi initialization to include configuration…
… and adapt tests Signed-off-by: Thomas Leber <thomas@tl-photography.at>
- Loading branch information
1 parent
b5644fe
commit b5afeac
Showing
7 changed files
with
632 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,10 +24,10 @@ | |
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
import org.eclipse.jetty.client.HttpClient; | ||
import org.openhab.binding.api.AwattarApi; | ||
import org.openhab.binding.api.AwattarApi.AwattarApiException; | ||
import org.openhab.binding.awattar.internal.AwattarBridgeConfiguration; | ||
import org.openhab.binding.awattar.internal.AwattarPrice; | ||
import org.openhab.binding.awattar.internal.api.AwattarApi; | ||
import org.openhab.binding.awattar.internal.api.AwattarApi.AwattarApiException; | ||
import org.openhab.core.i18n.TimeZoneProvider; | ||
import org.openhab.core.thing.Bridge; | ||
import org.openhab.core.thing.ChannelUID; | ||
|
@@ -62,13 +62,14 @@ public class AwattarBridgeHandler extends BaseBridgeHandler { | |
private @Nullable SortedSet<AwattarPrice> prices; | ||
private ZoneId zone; | ||
|
||
private AwattarApi awattarApi; | ||
private @NonNullByDefault AwattarApi awattarApi; | ||
private HttpClient httpClient; | ||
|
||
public AwattarBridgeHandler(Bridge thing, HttpClient httpClient, TimeZoneProvider timeZoneProvider) { | ||
Check failure on line 68 in bundles/org.openhab.binding.awattar/src/main/java/org/openhab/binding/awattar/internal/handler/AwattarBridgeHandler.java
|
||
super(thing); | ||
zone = timeZoneProvider.getTimeZone(); | ||
|
||
awattarApi = new AwattarApi(httpClient, zone); | ||
this.httpClient = httpClient; | ||
} | ||
|
||
@Override | ||
|
@@ -77,7 +78,7 @@ public void initialize() { | |
AwattarBridgeConfiguration config = getConfigAs(AwattarBridgeConfiguration.class); | ||
|
||
try { | ||
awattarApi.initialize(config); | ||
awattarApi = new AwattarApi(httpClient, zone, config); | ||
} catch (IllegalArgumentException e) { | ||
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "@text/error.unsupported.country"); | ||
return; | ||
|
110 changes: 110 additions & 0 deletions
110
...inding.awattar/src/test/java/org/openhab/binding/awattar/internal/api/AwattarApiTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/** | ||
* Copyright (c) 2010-2024 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.binding.awattar.internal.api; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.hasSize; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.time.ZoneId; | ||
import java.util.Objects; | ||
import java.util.SortedSet; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jetty.client.HttpClient; | ||
import org.eclipse.jetty.client.api.ContentResponse; | ||
import org.eclipse.jetty.client.api.Request; | ||
import org.eclipse.jetty.http.HttpMethod; | ||
import org.eclipse.jetty.http.HttpStatus; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.mockito.junit.jupiter.MockitoSettings; | ||
import org.mockito.quality.Strictness; | ||
import org.openhab.binding.awattar.internal.AwattarBridgeConfiguration; | ||
import org.openhab.binding.awattar.internal.AwattarPrice; | ||
import org.openhab.binding.awattar.internal.api.AwattarApi.AwattarApiException; | ||
import org.openhab.binding.awattar.internal.handler.AwattarBridgeHandler; | ||
import org.openhab.binding.awattar.internal.handler.AwattarBridgeHandlerTest; | ||
import org.openhab.core.i18n.TimeZoneProvider; | ||
import org.openhab.core.test.java.JavaTest; | ||
|
||
/** | ||
* The {@link AwattarBridgeHandlerTest} contains tests for the {@link AwattarBridgeHandler} | ||
* | ||
* @author Jan N. Klug - Initial contribution | ||
*/ | ||
@ExtendWith(MockitoExtension.class) | ||
@MockitoSettings(strictness = Strictness.LENIENT) | ||
@NonNullByDefault | ||
class AwattarApiTest extends JavaTest { | ||
// API Mocks | ||
private @Mock @NonNullByDefault({}) HttpClient httpClientMock; | ||
private @Mock @NonNullByDefault({}) TimeZoneProvider timeZoneProviderMock; | ||
private @Mock @NonNullByDefault({}) Request requestMock; | ||
private @Mock @NonNullByDefault({}) ContentResponse contentResponseMock; | ||
private @Mock @NonNullByDefault({}) AwattarBridgeConfiguration config; | ||
|
||
// sut | ||
private @NonNullByDefault({}) AwattarApi api; | ||
|
||
@BeforeEach | ||
public void setUp() throws IOException, ExecutionException, InterruptedException, TimeoutException { | ||
try (InputStream inputStream = AwattarApiTest.class.getResourceAsStream("api_response.json")) { | ||
if (inputStream == null) { | ||
throw new IOException("inputstream is null"); | ||
} | ||
byte[] bytes = inputStream.readAllBytes(); | ||
if (bytes == null) { | ||
throw new IOException("Resulting byte-array empty"); | ||
} | ||
when(contentResponseMock.getContentAsString()).thenReturn(new String(bytes, StandardCharsets.UTF_8)); | ||
} | ||
when(contentResponseMock.getStatus()).thenReturn(HttpStatus.OK_200); | ||
when(httpClientMock.newRequest(anyString())).thenReturn(requestMock); | ||
when(requestMock.method(HttpMethod.GET)).thenReturn(requestMock); | ||
when(requestMock.timeout(10, TimeUnit.SECONDS)).thenReturn(requestMock); | ||
when(requestMock.send()).thenReturn(contentResponseMock); | ||
|
||
when(timeZoneProviderMock.getTimeZone()).thenReturn(ZoneId.of("GMT+2")); | ||
|
||
config.basePrice = 0.0; | ||
config.vatPercent = 0.0; | ||
config.country = "DE"; | ||
|
||
api = new AwattarApi(httpClientMock, ZoneId.of("GMT+2"), config); | ||
} | ||
|
||
@Test | ||
void testPricesRetrieval() throws AwattarApiException { | ||
SortedSet<AwattarPrice> prices = api.getData(); | ||
|
||
assertThat(prices, hasSize(72)); | ||
|
||
Objects.requireNonNull(prices); | ||
|
||
// check if first and last element are correct | ||
assertThat(prices.first().timerange().start(), is(1718316000000L)); | ||
assertThat(prices.last().timerange().end(), is(1718575200000L)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.