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.ecowatt.internal;
15 import static org.junit.jupiter.api.Assertions.*;
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.nio.charset.StandardCharsets;
20 import java.time.OffsetDateTime;
21 import java.time.ZonedDateTime;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.junit.jupiter.api.BeforeEach;
26 import org.junit.jupiter.api.Test;
27 import org.openhab.binding.ecowatt.internal.handler.EcowattHandler;
28 import org.openhab.binding.ecowatt.internal.restapi.EcowattApiResponse;
29 import org.openhab.core.library.types.DecimalType;
30 import org.openhab.core.types.State;
31 import org.openhab.core.types.UnDefType;
33 import com.google.gson.Gson;
34 import com.google.gson.GsonBuilder;
35 import com.google.gson.JsonDeserializer;
38 * Tests of methods getting channel state from API response
40 * @author Laurent Garnier - Initial contribution
43 public class EcowattApiResponseTest {
45 private static final DecimalType STATE_ONE = new DecimalType(1);
46 private static final DecimalType STATE_TWO = new DecimalType(2);
47 private static final DecimalType STATE_THREE = new DecimalType(3);
49 private final Gson gson = new GsonBuilder().registerTypeAdapter(ZonedDateTime.class,
50 (JsonDeserializer<ZonedDateTime>) (json, type, jsonDeserializationContext) -> OffsetDateTime
51 .parse(json.getAsJsonPrimitive().getAsString()).toZonedDateTime())
53 private static @Nullable EcowattApiResponse apiResponse;
56 public void loadApiResponse() throws IOException {
57 InputStream resourceStream = getClass().getResourceAsStream("/ApiResponse.json");
58 assertNotNull(resourceStream);
59 final String response = new String(resourceStream.readAllBytes(), StandardCharsets.UTF_8);
60 apiResponse = gson.fromJson(response, EcowattApiResponse.class);
61 assertNotNull(apiResponse);
65 public void getDaySignalStateWithNullResponse() {
66 assertEquals(UnDefType.UNDEF,
67 EcowattHandler.getDaySignalState(null, ZonedDateTime.parse("2022-09-19T21:30:00+02:00")));
71 public void getDaySignalStateWithSameOffset() {
72 ZonedDateTime dateTime = ZonedDateTime.parse("2022-09-19T21:30:00+02:00");
73 assertEquals(UnDefType.UNDEF, EcowattHandler.getDaySignalState(apiResponse, dateTime.minusDays(1)));
74 assertEquals(STATE_THREE, EcowattHandler.getDaySignalState(apiResponse, dateTime));
75 assertEquals(STATE_TWO, EcowattHandler.getDaySignalState(apiResponse, dateTime.plusDays(1)));
76 assertEquals(STATE_ONE, EcowattHandler.getDaySignalState(apiResponse, dateTime.plusDays(2)));
77 assertEquals(STATE_ONE, EcowattHandler.getDaySignalState(apiResponse, dateTime.plusDays(3)));
78 assertEquals(UnDefType.UNDEF, EcowattHandler.getDaySignalState(apiResponse, dateTime.plusDays(4)));
82 public void getDaySignalStateWithOtherOffset() {
83 ZonedDateTime dateTime = ZonedDateTime.parse("2022-09-20T00:30:00+05:00");
84 assertEquals(UnDefType.UNDEF, EcowattHandler.getDaySignalState(apiResponse, dateTime.minusDays(1)));
85 assertEquals(STATE_THREE, EcowattHandler.getDaySignalState(apiResponse, dateTime));
86 assertEquals(STATE_TWO, EcowattHandler.getDaySignalState(apiResponse, dateTime.plusDays(1)));
87 assertEquals(STATE_ONE, EcowattHandler.getDaySignalState(apiResponse, dateTime.plusDays(2)));
88 assertEquals(STATE_ONE, EcowattHandler.getDaySignalState(apiResponse, dateTime.plusDays(3)));
89 assertEquals(UnDefType.UNDEF, EcowattHandler.getDaySignalState(apiResponse, dateTime.plusDays(4)));
93 public void getHourSignalStateWithNullResponse() {
94 assertEquals(UnDefType.UNDEF,
95 EcowattHandler.getHourSignalState(null, ZonedDateTime.parse("2022-09-19T00:30:00+02:00")));
99 public void getHourSignalStateWithSameOffset() {
100 ZonedDateTime dateTime = ZonedDateTime.parse("2022-09-19T00:30:00+02:00");
102 assertEquals(UnDefType.UNDEF, EcowattHandler.getHourSignalState(apiResponse, dateTime.minusHours(1)));
105 for (int h = 0; h < 24; h++) {
111 expectedState = STATE_TWO;
117 expectedState = STATE_THREE;
120 expectedState = STATE_ONE;
122 assertEquals(expectedState, EcowattHandler.getHourSignalState(apiResponse, dateTime.plusHours(h)));
125 dateTime = dateTime.plusDays(1);
126 for (int h = 0; h < 24; h++) {
127 expectedState = h == 20 ? STATE_TWO : STATE_ONE;
128 assertEquals(expectedState, EcowattHandler.getHourSignalState(apiResponse, dateTime.plusHours(h)));
131 dateTime = dateTime.plusDays(1);
132 for (int h = 0; h < 24; h++) {
133 expectedState = STATE_ONE;
134 assertEquals(expectedState, EcowattHandler.getHourSignalState(apiResponse, dateTime.plusHours(h)));
137 dateTime = dateTime.plusDays(1);
138 for (int h = 0; h < 24; h++) {
139 expectedState = h == 23 ? UnDefType.UNDEF : STATE_ONE;
140 assertEquals(expectedState, EcowattHandler.getHourSignalState(apiResponse, dateTime.plusHours(h)));
143 dateTime = dateTime.plusDays(1);
144 assertEquals(UnDefType.UNDEF, EcowattHandler.getHourSignalState(apiResponse, dateTime));
148 public void getHourSignalStateWithOtherOffset() {
149 ZonedDateTime dateTime = ZonedDateTime.parse("2022-09-19T00:30:00+05:00");
151 for (int h = 0; h < 24; h++) {
156 expectedState = UnDefType.UNDEF;
161 expectedState = STATE_TWO;
167 expectedState = STATE_THREE;
170 expectedState = STATE_ONE;
172 assertEquals(expectedState, EcowattHandler.getHourSignalState(apiResponse, dateTime.plusHours(h)));