]> git.basschouten.com Git - openhab-addons.git/blob
2dc0fc06f7473819b004d587f0ccda1faeb656f3
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.windcentrale.internal.dto;
14
15 import static org.hamcrest.CoreMatchers.notNullValue;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.hamcrest.core.Is.is;
18
19 import java.io.IOException;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Objects;
23
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.junit.jupiter.api.Test;
26 import org.openhab.binding.windcentrale.internal.dto.Project.Participation;
27
28 /**
29  * Tests deserialization of Windcentrale API responses from JSON.
30  *
31  * @author Wouter Born - Initial contribution
32  */
33 @NonNullByDefault
34 public class WindcentraleGsonTest {
35
36     private static final DataUtil DATA_UTIL = new DataUtil(WindcentraleGson.GSON);
37
38     @Test
39     public void deserializeKeyResponse() throws IOException {
40         KeyResponse key = DATA_UTIL.fromJson("key-response.json", KeyResponse.class);
41         assertThat(key, is(notNullValue()));
42
43         assertThat(key.clientId, is("715j3r0trk7o8dqg3md57il7q0"));
44         assertThat(key.region, is("eu-west-1"));
45         assertThat(key.userPoolId, is("eu-west-1_U7eYBPrBd"));
46     }
47
48     @Test
49     public void deserializeProjectsResponse() throws IOException {
50         List<Project> projects = DATA_UTIL.fromJson("projects-response.json", WindcentraleGson.PROJECTS_RESPONSE_TYPE);
51
52         assertThat(projects, is(notNullValue()));
53         assertThat(projects.size(), is(1));
54
55         Project project = projects.get(0);
56
57         assertThat(project.projectName, is("De Grote Geert"));
58         assertThat(project.projectCode, is("WND-GG"));
59
60         List<Participation> participations = Objects.requireNonNull(project.participations);
61         assertThat(participations.size(), is(2));
62
63         assertThat(participations.get(0).share, is(20));
64         assertThat(participations.get(1).share, is(50));
65     }
66
67     @Test
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);
71
72         assertThat(map, is(notNullValue()));
73         assertThat(map.size(), is(0));
74     }
75
76     @Test
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);
80
81         assertThat(map, is(notNullValue()));
82         assertThat(map.size(), is(1));
83
84         assertDeJongeHeldStatus(map);
85     }
86
87     @Test
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);
91
92         assertThat(map, is(notNullValue()));
93         assertThat(map.size(), is(11));
94
95         assertDeBlauweReigerStatus(map);
96         assertDeJongeHeldStatus(map);
97         assertDeWitteJufferStatus(map);
98     }
99
100     private void assertDeBlauweReigerStatus(Map<Windmill, WindmillStatus> map) {
101         WindmillStatus status = Objects.requireNonNull(map.get(Windmill.DE_BLAUWE_REIGER));
102
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));
112     }
113
114     private void assertDeJongeHeldStatus(Map<Windmill, WindmillStatus> map) {
115         WindmillStatus status = Objects.requireNonNull(map.get(Windmill.DE_JONGE_HELD));
116
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));
126     }
127
128     private void assertDeWitteJufferStatus(Map<Windmill, WindmillStatus> map) {
129         WindmillStatus status = Objects.requireNonNull(map.get(Windmill.DE_WITTE_JUFFER));
130
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));
140     }
141 }