]> git.basschouten.com Git - openhab-addons.git/blob
56a2afd671bca0410c27e5b52edca0d0e0c9987f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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;
14
15 import static org.hamcrest.CoreMatchers.is;
16 import static org.hamcrest.CoreMatchers.nullValue;
17 import static org.hamcrest.MatcherAssert.assertThat;
18
19 import java.util.Map;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.junit.jupiter.api.Test;
23 import org.openhab.binding.dbquery.internal.config.QueryConfiguration;
24 import org.openhab.binding.dbquery.internal.domain.QueryResult;
25 import org.openhab.binding.dbquery.internal.domain.QueryResultExtractor;
26 import org.openhab.binding.dbquery.internal.domain.ResultRow;
27
28 /**
29  *
30  * @author Joan Pujol - Initial contribution
31  */
32 @NonNullByDefault
33 class Influx2QueryResultExtractorTest {
34     public static final QueryResult ONE_ROW_ONE_COLUMN_RESULT = QueryResult.ofSingleValue("AnyValueName", "value");
35     public static final QueryResult SEVERAL_ROWS_COLUMNS_RESULT = QueryResult.of(
36             new ResultRow(Map.of("valueName", "value1", "column2", "value2")),
37             new ResultRow(Map.of("valueName", "value1", "column2", "value2")));
38     public static final QueryResult ONE_ROW_SEVERAL_COLUMNS_RESULT = QueryResult
39             .of(new ResultRow(Map.of("valueName", "value1", "column2", "value2")));
40     public static final QueryResult INCORRECT_RESULT = QueryResult.ofIncorrectResult("Incorrect result");
41
42     private static final QueryConfiguration SCALAR_VALUE_CONFIG = new QueryConfiguration("query", 10, 10, true, null,
43             false);
44     private static final QueryConfiguration NON_SCALAR_VALUE_CONFIG = new QueryConfiguration("query", 10, 10, false,
45             null, false);
46     private static final QueryConfiguration SCALAR_VALUE_CONFIG_WITH_SCALAR_COLUMN = new QueryConfiguration("query", 10,
47             10, true, "valueName", false);
48
49     @Test
50     void givenAResultWithOneRowAndOneColumnAndScalarConfigurationScalarValueIsReturned() {
51         var extracted = new QueryResultExtractor(SCALAR_VALUE_CONFIG).extractResult(ONE_ROW_ONE_COLUMN_RESULT);
52
53         assertThat(extracted.isCorrect(), is(Boolean.TRUE));
54         assertThat(extracted.getResult(), is("value"));
55     }
56
57     @Test
58     void givenAResultWithSeveralRowsAndScalarConfigurationIncorrectValueIsReturned() {
59         var extracted = new QueryResultExtractor(SCALAR_VALUE_CONFIG).extractResult(SEVERAL_ROWS_COLUMNS_RESULT);
60
61         assertThat(extracted.isCorrect(), is(false));
62         assertThat(extracted.getResult(), nullValue());
63     }
64
65     @Test
66     void givenAResultWithSeveralColumnsAndScalarConfigurationIncorrectValueIsReturned() {
67         var extracted = new QueryResultExtractor(SCALAR_VALUE_CONFIG).extractResult(ONE_ROW_SEVERAL_COLUMNS_RESULT);
68
69         assertThat(extracted.isCorrect(), is(false));
70         assertThat(extracted.getResult(), nullValue());
71     }
72
73     @Test
74     void givenAResultWithSeveralColumnsAndScalarConfigurationAndScalarColumnDefinedValueIsReturned() {
75         var extracted = new QueryResultExtractor(SCALAR_VALUE_CONFIG_WITH_SCALAR_COLUMN)
76                 .extractResult(ONE_ROW_SEVERAL_COLUMNS_RESULT);
77
78         assertThat(extracted.isCorrect(), is(true));
79         assertThat(extracted.getResult(), is("value1"));
80     }
81
82     @Test
83     void givenAResultWithSeveralRowsAndNonScalarConfigQueryResultIsReturned() {
84         var extracted = new QueryResultExtractor(NON_SCALAR_VALUE_CONFIG).extractResult(SEVERAL_ROWS_COLUMNS_RESULT);
85
86         assertThat(extracted.isCorrect(), is(true));
87         assertThat(extracted.getResult(), is(SEVERAL_ROWS_COLUMNS_RESULT));
88     }
89
90     @Test
91     void givenAIncorrectResultIncorrectValueIsReturned() {
92         var extracted = new QueryResultExtractor(NON_SCALAR_VALUE_CONFIG).extractResult(INCORRECT_RESULT);
93
94         assertThat(extracted.isCorrect(), is(false));
95         assertThat(extracted.getResult(), nullValue());
96     }
97 }