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.windcentrale.internal.dto;
15 import static org.hamcrest.CoreMatchers.notNullValue;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.hamcrest.core.Is.is;
19 import java.io.IOException;
20 import java.util.List;
22 import java.util.Objects;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.junit.jupiter.api.Test;
26 import org.openhab.binding.windcentrale.internal.dto.Project.Participation;
29 * Tests deserialization of Windcentrale API responses from JSON.
31 * @author Wouter Born - Initial contribution
34 public class WindcentraleGsonTest {
36 private static final DataUtil DATA_UTIL = new DataUtil(WindcentraleGson.GSON);
39 public void deserializeKeyResponse() throws IOException {
40 KeyResponse key = DATA_UTIL.fromJson("key-response.json", KeyResponse.class);
41 assertThat(key, is(notNullValue()));
43 assertThat(key.clientId, is("715j3r0trk7o8dqg3md57il7q0"));
44 assertThat(key.region, is("eu-west-1"));
45 assertThat(key.userPoolId, is("eu-west-1_U7eYBPrBd"));
49 public void deserializeProjectsResponse() throws IOException {
50 List<Project> projects = DATA_UTIL.fromJson("projects-response.json", WindcentraleGson.PROJECTS_RESPONSE_TYPE);
52 assertThat(projects, is(notNullValue()));
53 assertThat(projects.size(), is(1));
55 Project project = projects.get(0);
57 assertThat(project.projectName, is("De Grote Geert"));
58 assertThat(project.projectCode, is("WND-GG"));
60 List<Participation> participations = Objects.requireNonNull(project.participations);
61 assertThat(participations.size(), is(2));
63 assertThat(participations.get(0).share, is(20));
64 assertThat(participations.get(1).share, is(50));
68 public void deserializeLiveDataResponseEmpty() throws IOException {
69 Map<Windmill, WindmillStatus> map = DATA_UTIL.fromJson("live-data-response-empty.json",
70 WindcentraleGson.LIVE_DATA_RESPONSE_TYPE);
72 assertThat(map, is(notNullValue()));
73 assertThat(map.size(), is(0));
77 public void deserializeLiveDataResponseSingle() throws IOException {
78 Map<Windmill, WindmillStatus> map = DATA_UTIL.fromJson("live-data-response-single.json",
79 WindcentraleGson.LIVE_DATA_RESPONSE_TYPE);
81 assertThat(map, is(notNullValue()));
82 assertThat(map.size(), is(1));
84 assertDeJongeHeldStatus(map);
88 public void deserializeLiveDataResponseMultiple() throws IOException {
89 Map<Windmill, WindmillStatus> map = DATA_UTIL.fromJson("live-data-response-multiple.json",
90 WindcentraleGson.LIVE_DATA_RESPONSE_TYPE);
92 assertThat(map, is(notNullValue()));
93 assertThat(map.size(), is(11));
95 assertDeBlauweReigerStatus(map);
96 assertDeJongeHeldStatus(map);
97 assertDeWitteJufferStatus(map);
100 private void assertDeBlauweReigerStatus(Map<Windmill, WindmillStatus> map) {
101 WindmillStatus status = Objects.requireNonNull(map.get(Windmill.DE_BLAUWE_REIGER));
103 assertThat(status.powerPerShare, is(150));
104 assertThat(status.timestamp.toEpochSecond(), is(1680425425L));
105 assertThat(status.windPower, is(7));
106 assertThat(status.power, is(827));
107 assertThat(status.windDirection, is("O"));
108 assertThat(status.yearProduction, is(872488));
109 assertThat(status.totalRuntime, is(29470));
110 assertThat(status.yearRuntime, is(-98268833.015556d));
111 assertThat(status.powerPercentage, is(98));
114 private void assertDeJongeHeldStatus(Map<Windmill, WindmillStatus> map) {
115 WindmillStatus status = Objects.requireNonNull(map.get(Windmill.DE_JONGE_HELD));
117 assertThat(status.powerPerShare, is(52));
118 assertThat(status.timestamp.toEpochSecond(), is(1680425425L));
119 assertThat(status.windPower, is(5));
120 assertThat(status.power, is(522));
121 assertThat(status.windDirection, is("O"));
122 assertThat(status.yearProduction, is(1508090));
123 assertThat(status.totalRuntime, is(122330));
124 assertThat(status.yearRuntime, is(2089d));
125 assertThat(status.powerPercentage, is(23));
128 private void assertDeWitteJufferStatus(Map<Windmill, WindmillStatus> map) {
129 WindmillStatus status = Objects.requireNonNull(map.get(Windmill.DE_WITTE_JUFFER));
131 assertThat(status.powerPerShare, is(134));
132 assertThat(status.timestamp.toEpochSecond(), is(1680425425L));
133 assertThat(status.windPower, is(5));
134 assertThat(status.power, is(764));
135 assertThat(status.windDirection, is("NO"));
136 assertThat(status.yearProduction, is(1233164));
137 assertThat(status.totalRuntime, is(111171));
138 assertThat(status.yearRuntime, is(2118.266667d));
139 assertThat(status.powerPercentage, is(39));