]> git.basschouten.com Git - openhab-addons.git/blob
30d900a3161a533edfcba7c03b10bb60748d0915
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.dbquery.internal.domain;
14
15 import static org.hamcrest.CoreMatchers.instanceOf;
16 import static org.hamcrest.CoreMatchers.is;
17 import static org.hamcrest.CoreMatchers.notNullValue;
18 import static org.hamcrest.MatcherAssert.assertThat;
19 import static org.hamcrest.Matchers.closeTo;
20 import static org.hamcrest.Matchers.lessThan;
21
22 import java.time.Duration;
23 import java.time.Instant;
24 import java.time.format.DateTimeFormatter;
25 import java.util.Date;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Objects;
30
31 import org.eclipse.jdt.annotation.NonNullByDefault;
32 import org.eclipse.jdt.annotation.Nullable;
33 import org.hamcrest.Matchers;
34 import org.junit.jupiter.api.Test;
35
36 import com.google.gson.Gson;
37 import com.google.gson.JsonParser;
38
39 /**
40  *
41  * @author Joan Pujol - Initial contribution
42  */
43 @NonNullByDefault
44 class QueryResultJSONEncoderTest {
45     public static final double TOLERANCE = 0.001d;
46     private final DBQueryJSONEncoder instance = new DBQueryJSONEncoder();
47     private final Gson gson = new Gson();
48     private final JsonParser jsonParser = new JsonParser();
49
50     @Test
51     void givenQueryResultIsSerializedToJson() {
52         String json = instance.encode(givenQueryResultWithResults());
53
54         assertThat(jsonParser.parse(json), notNullValue());
55     }
56
57     @Test
58     @SuppressWarnings({ "rawtypes", "unchecked" })
59     void givenQueryResultItsContentIsCorrectlySerializedToJson() {
60         String json = instance.encode(givenQueryResultWithResults());
61
62         Map<String, Object> map = gson.fromJson(json, Map.class);
63         assertThat(map, Matchers.hasEntry("correct", Boolean.TRUE));
64         assertThat(map, Matchers.hasKey("data"));
65         List<Map> data = (List<Map>) map.get("data");
66         assertThat(data, Matchers.hasSize(2));
67         Map firstRow = data.get(0);
68
69         assertReadGivenValuesDecodedFromJson(firstRow);
70     }
71
72     private void assertReadGivenValuesDecodedFromJson(Map<?, ?> firstRow) {
73         assertThat(firstRow.get("strValue"), is("a string"));
74
75         Object doubleValue = firstRow.get("doubleValue");
76         assertThat(doubleValue, instanceOf(Number.class));
77         assertThat(((Number) doubleValue).doubleValue(), closeTo(2.3d, TOLERANCE));
78
79         Object intValue = firstRow.get("intValue");
80         assertThat(intValue, instanceOf(Number.class));
81         assertThat(((Number) intValue).intValue(), is(3));
82
83         Object longValue = firstRow.get("longValue");
84         assertThat(longValue, instanceOf(Number.class));
85         assertThat(((Number) longValue).longValue(), is(Long.MAX_VALUE));
86
87         Object date = Objects.requireNonNull(firstRow.get("date"));
88         assertThat(date, instanceOf(String.class));
89         var parsedDate = Instant.from(DateTimeFormatter.ISO_INSTANT.parse((String) date));
90         assertThat(Duration.between(parsedDate, Instant.now()).getSeconds(), lessThan(10L));
91
92         Object instant = Objects.requireNonNull(firstRow.get("instant"));
93         assertThat(instant, instanceOf(String.class));
94         var parsedInstant = Instant.from(DateTimeFormatter.ISO_INSTANT.parse((String) instant));
95         assertThat(Duration.between(parsedInstant, Instant.now()).getSeconds(), lessThan(10L));
96
97         assertThat(firstRow.get("booleanValue"), is(Boolean.TRUE));
98         assertThat(firstRow.get("object"), is("an object"));
99     }
100
101     @Test
102     @SuppressWarnings({ "unchecked" })
103     void givenQueryResultWithIncorrectResultItsContentIsCorrectlySerializedToJson() {
104         String json = instance.encode(QueryResult.ofIncorrectResult("Incorrect"));
105
106         Map<String, Object> map = gson.fromJson(json, Map.class);
107         assertThat(map, Matchers.hasEntry("correct", Boolean.FALSE));
108         assertThat(map.get("errorMessage"), is("Incorrect"));
109     }
110
111     @Test
112     void givenQueryParametersAreCorrectlySerializedToJson() {
113         QueryParameters queryParameters = new QueryParameters(givenRowValues());
114
115         String json = instance.encode(queryParameters);
116
117         Map<?, ?> map = Objects.requireNonNull(gson.fromJson(json, Map.class));
118         assertReadGivenValuesDecodedFromJson(map);
119     }
120
121     private QueryResult givenQueryResultWithResults() {
122         return QueryResult.of(new ResultRow(givenRowValues()), new ResultRow(givenRowValues()));
123     }
124
125     private Map<String, @Nullable Object> givenRowValues() {
126         Map<String, @Nullable Object> values = new HashMap<>();
127         values.put("strValue", "a string");
128         values.put("doubleValue", 2.3d);
129         values.put("intValue", 3);
130         values.put("longValue", Long.MAX_VALUE);
131         values.put("date", new Date());
132         values.put("instant", Instant.now());
133         values.put("booleanValue", Boolean.TRUE);
134         values.put("object", new Object() {
135             @Override
136             public String toString() {
137                 return "an object";
138             }
139         });
140         return values;
141     }
142 }