]> git.basschouten.com Git - openhab-addons.git/blob
d04ca1d0270937609d1952d427dcf3c78e705b1b
[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.semsportal.internal;
14
15 import static org.junit.jupiter.api.Assertions.*;
16
17 import java.nio.file.Files;
18 import java.nio.file.Paths;
19 import java.time.ZoneId;
20 import java.time.ZonedDateTime;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.junit.jupiter.api.Test;
24 import org.junit.jupiter.params.ParameterizedTest;
25 import org.junit.jupiter.params.provider.ValueSource;
26 import org.openhab.binding.semsportal.internal.dto.BaseResponse;
27 import org.openhab.binding.semsportal.internal.dto.LoginResponse;
28 import org.openhab.binding.semsportal.internal.dto.StationListResponse;
29 import org.openhab.binding.semsportal.internal.dto.StatusResponse;
30
31 import com.google.gson.Gson;
32 import com.google.gson.GsonBuilder;
33
34 /**
35  * @author Iwan Bron - Initial contribution
36  */
37 @NonNullByDefault
38 public class SEMSJsonParserTest {
39
40     @ParameterizedTest
41     @ValueSource(strings = { "success_status.json", "success_status_br.json" })
42     public void testParseSuccessStatusResult(String resourceName) throws Exception {
43         String json = Files.readString(Paths.get("src/test/resources/" + resourceName));
44         StatusResponse response = getGson().fromJson(json, StatusResponse.class);
45         assertNotNull(response, "Expected deserialized StatusResponse");
46         if (response != null) {// response cannot be null, was asserted before, but code check produces a warning
47             assertTrue(response.isOk(), "Successresponse should be OK");
48             assertNotNull(response.getStatus(), "Expected deserialized StatusResponse.status");
49             assertEquals(381.0, response.getStatus().getCurrentOutput(), "Current Output parsed correctly");
50             assertEquals(0.11, response.getStatus().getDayIncome(), "Day income parsed correctly");
51             assertEquals(0.5, response.getStatus().getDayTotal(), "Day total parsed correctly");
52             assertEquals(ZonedDateTime.of(2021, 2, 6, 11, 22, 48, 0, ZoneId.systemDefault()),
53                     response.getStatus().getLastUpdate(), "Last update parsed correctly");
54             assertEquals(17.2, response.getStatus().getMonthTotal(), "Month total parsed correctly");
55             assertEquals(7379.0, response.getStatus().getOverallTotal(), "Overall total parsed correctly");
56             assertEquals(823.38, response.getStatus().getTotalIncome(), "Total income parsed correctly");
57         }
58     }
59
60     @Test
61     public void testParseErrorStatusResult() throws Exception {
62         String json = Files.readString(Paths.get("src/test/resources/error_status.json"));
63         BaseResponse response = getGson().fromJson(json, BaseResponse.class);
64         assertNotNull(response, "Expected deserialized StatusResponse");
65         if (response != null) {// response cannot be null, was asserted before, but code check produces a warning
66             assertEquals(response.getCode(), BaseResponse.EXCEPTION, "Error response shoud have error code");
67             assertTrue(response.isError(), "Error response should have isError = true");
68         }
69     }
70
71     @Test
72     public void testParseSuccessLoginResult() throws Exception {
73         String json = Files.readString(Paths.get("src/test/resources/success_login.json"));
74         LoginResponse response = getGson().fromJson(json, LoginResponse.class);
75         assertNotNull(response, "Expected deserialized LoginResponse");
76         if (response != null) {// response cannot be null, was asserted before, but code check produces a warning
77             assertTrue(response.isOk(), "Success response should result in OK");
78             assertNotNull(response.getToken(), "Success response should result in token");
79         }
80     }
81
82     @Test
83     public void testParseErrorLoginResult() throws Exception {
84         String json = Files.readString(Paths.get("src/test/resources/error_login.json"));
85         LoginResponse response = getGson().fromJson(json, LoginResponse.class);
86         assertNotNull(response, "Expected deserialized LoginResponse");
87         if (response != null) {// response cannot be null, was asserted before, but code check produces a warning
88             assertFalse(response.isOk(), "Error response should not result in OK");
89             assertNull(response.getToken(), "Error response should have null token");
90         }
91     }
92
93     @Test
94     public void testParseSuccessListResult() throws Exception {
95         String json = Files.readString(Paths.get("src/test/resources/success_list.json"));
96         StationListResponse response = getGson().fromJson(json, StationListResponse.class);
97         assertNotNull(response, "Expected deserialized StationListResponse");
98         if (response != null) {// response cannot be null, was asserted before, but code check produces a warning
99             assertTrue(response.isOk(), "Success response should result in OK");
100             assertNotNull(response.getStations(), "List response should have station list");
101             assertEquals(1, response.getStations().size(), "List response should have station list");
102         }
103     }
104
105     private Gson getGson() {
106         return new GsonBuilder().create();
107     }
108 }