]> git.basschouten.com Git - openhab-addons.git/blob
75a7a744f24792422f4e12564bf13ff7fdafc028
[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.dbquery.internal.dbimpl.influx2;
14
15 import static org.hamcrest.CoreMatchers.equalTo;
16 import static org.hamcrest.CoreMatchers.notNullValue;
17 import static org.hamcrest.MatcherAssert.assertThat;
18 import static org.hamcrest.Matchers.*;
19 import static org.hamcrest.core.Is.is;
20
21 import org.eclipse.jdt.annotation.DefaultLocation;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.junit.jupiter.api.AfterEach;
24 import org.junit.jupiter.api.BeforeEach;
25 import org.junit.jupiter.api.Test;
26 import org.openhab.binding.dbquery.internal.config.InfluxDB2BridgeConfiguration;
27 import org.openhab.binding.dbquery.internal.domain.Query;
28 import org.openhab.binding.dbquery.internal.domain.QueryParameters;
29
30 /**
31  *
32  * @author Joan Pujol - Initial contribution
33  */
34 @NonNullByDefault(value = { DefaultLocation.PARAMETER })
35 class Influx2DatabaseTest {
36     private Influx2Database instance;
37
38     @BeforeEach
39     public void setup() {
40         instance = new Influx2Database(new InfluxDB2BridgeConfiguration(), new InfluxDBClientFacadeMock());
41     }
42
43     @AfterEach
44     public void clearDown() {
45         instance = null;
46     }
47
48     @Test
49     public void givenQueryThatReturnsScalarResultGetValidScalarResult() throws Exception {
50         instance.connect().get();
51         Query query = instance.queryFactory().createQuery(InfluxDBClientFacadeMock.SCALAR_QUERY, QueryParameters.EMPTY,
52                 null);
53         var future = instance.executeQuery(query);
54         var queryResult = future.get();
55
56         assertThat(queryResult, notNullValue());
57         assertThat(queryResult.isCorrect(), is(true));
58         assertThat(queryResult.getData(), hasSize(1));
59         assertThat(queryResult.getData().get(0).getColumnsSize(), is(1));
60     }
61
62     @Test
63     public void givenQueryThatReturnsMultipleRowsGetValidQueryResult() throws Exception {
64         instance.connect().get();
65         Query query = instance.queryFactory().createQuery(InfluxDBClientFacadeMock.MULTIPLE_ROWS_QUERY,
66                 QueryParameters.EMPTY, null);
67         var future = instance.executeQuery(query);
68         var queryResult = future.get();
69
70         assertThat(queryResult, notNullValue());
71         assertThat(queryResult.isCorrect(), is(true));
72         assertThat(queryResult.getData(), hasSize(InfluxDBClientFacadeMock.MULTIPLE_ROWS_SIZE));
73         assertThat("contains expected result data", queryResult.getData().stream().allMatch(row -> {
74             var value = (String) row.getValue(InfluxDBClientFacadeMock.VALUE_COLUMN);
75             var time = row.getValue(InfluxDBClientFacadeMock.TIME_COLUMN);
76             return value != null && value.contains(InfluxDBClientFacadeMock.MULTIPLE_ROWS_VALUE_PREFIX) && time != null;
77         }));
78     }
79
80     @Test
81     public void givenQueryThatReturnsErrorGetErroneusResult() throws Exception {
82         instance.connect().get();
83         Query query = instance.queryFactory().createQuery(InfluxDBClientFacadeMock.INVALID_QUERY, QueryParameters.EMPTY,
84                 null);
85         var future = instance.executeQuery(query);
86         var queryResult = future.get();
87
88         assertThat(queryResult, notNullValue());
89         assertThat(queryResult.isCorrect(), equalTo(false));
90         assertThat(queryResult.getData(), is(empty()));
91     }
92
93     @Test
94     public void givenQueryThatReturnsNoRowsGetEmptyResult() throws Exception {
95         instance.connect().get();
96         Query query = instance.queryFactory().createQuery(InfluxDBClientFacadeMock.EMPTY_QUERY, QueryParameters.EMPTY,
97                 null);
98         var future = instance.executeQuery(query);
99         var queryResult = future.get();
100
101         assertThat(queryResult, notNullValue());
102         assertThat(queryResult.isCorrect(), equalTo(true));
103         assertThat(queryResult.getData(), is(empty()));
104     }
105
106     @Test
107     public void givenNotConnectedClientShouldGetIncorrectQuery() {
108         Query query = instance.queryFactory().createQuery(InfluxDBClientFacadeMock.SCALAR_QUERY, QueryParameters.EMPTY,
109                 null);
110         var future = instance.executeQuery(query);
111         assertThat(future.isCompletedExceptionally(), is(Boolean.TRUE));
112     }
113 }