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