2 * Copyright (c) 2010-2024 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.dbquery.internal.domain;
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;
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;
29 import java.util.Objects;
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;
36 import com.google.gson.Gson;
37 import com.google.gson.JsonParser;
41 * @author Joan Pujol - Initial contribution
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();
51 void givenQueryResultIsSerializedToJson() {
52 String json = instance.encode(givenQueryResultWithResults());
54 assertThat(jsonParser.parse(json), notNullValue());
58 @SuppressWarnings({ "rawtypes", "unchecked" })
59 void givenQueryResultItsContentIsCorrectlySerializedToJson() {
60 String json = instance.encode(givenQueryResultWithResults());
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);
69 assertReadGivenValuesDecodedFromJson(firstRow);
72 private void assertReadGivenValuesDecodedFromJson(Map<?, ?> firstRow) {
73 assertThat(firstRow.get("strValue"), is("a string"));
75 Object doubleValue = firstRow.get("doubleValue");
76 assertThat(doubleValue, instanceOf(Number.class));
77 assertThat(((Number) doubleValue).doubleValue(), closeTo(2.3d, TOLERANCE));
79 Object intValue = firstRow.get("intValue");
80 assertThat(intValue, instanceOf(Number.class));
81 assertThat(((Number) intValue).intValue(), is(3));
83 Object longValue = firstRow.get("longValue");
84 assertThat(longValue, instanceOf(Number.class));
85 assertThat(((Number) longValue).longValue(), is(Long.MAX_VALUE));
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));
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));
97 assertThat(firstRow.get("booleanValue"), is(Boolean.TRUE));
98 assertThat(firstRow.get("object"), is("an object"));
102 @SuppressWarnings({ "unchecked" })
103 void givenQueryResultWithIncorrectResultItsContentIsCorrectlySerializedToJson() {
104 String json = instance.encode(QueryResult.ofIncorrectResult("Incorrect"));
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"));
112 void givenQueryParametersAreCorrectlySerializedToJson() {
113 QueryParameters queryParameters = new QueryParameters(givenRowValues());
115 String json = instance.encode(queryParameters);
117 Map<?, ?> map = Objects.requireNonNull(gson.fromJson(json, Map.class));
118 assertReadGivenValuesDecodedFromJson(map);
121 private QueryResult givenQueryResultWithResults() {
122 return QueryResult.of(new ResultRow(givenRowValues()), new ResultRow(givenRowValues()));
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() {
136 public String toString() {