2 * Copyright (c) 2010-2023 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.dbimpl.influx2;
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;
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;
32 * @author Joan Pujol - Initial contribution
34 @NonNullByDefault(value = { DefaultLocation.PARAMETER })
35 class Influx2DatabaseTest {
36 private Influx2Database instance;
40 instance = new Influx2Database(new InfluxDB2BridgeConfiguration(), new InfluxDBClientFacadeMock());
44 public void clearDown() {
49 public void givenQueryThatReturnsScalarResultGetValidScalarResult() throws Exception {
50 instance.connect().get();
51 Query query = instance.queryFactory().createQuery(InfluxDBClientFacadeMock.SCALAR_QUERY, QueryParameters.EMPTY,
53 var future = instance.executeQuery(query);
54 var queryResult = future.get();
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));
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();
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;
81 public void givenQueryThatReturnsErrorGetErroneusResult() throws Exception {
82 instance.connect().get();
83 Query query = instance.queryFactory().createQuery(InfluxDBClientFacadeMock.INVALID_QUERY, QueryParameters.EMPTY,
85 var future = instance.executeQuery(query);
86 var queryResult = future.get();
88 assertThat(queryResult, notNullValue());
89 assertThat(queryResult.isCorrect(), equalTo(false));
90 assertThat(queryResult.getData(), is(empty()));
94 public void givenQueryThatReturnsNoRowsGetEmptyResult() throws Exception {
95 instance.connect().get();
96 Query query = instance.queryFactory().createQuery(InfluxDBClientFacadeMock.EMPTY_QUERY, QueryParameters.EMPTY,
98 var future = instance.executeQuery(query);
99 var queryResult = future.get();
101 assertThat(queryResult, notNullValue());
102 assertThat(queryResult.isCorrect(), equalTo(true));
103 assertThat(queryResult.getData(), is(empty()));
107 public void givenNotConnectedClientShouldGetIncorrectQuery() {
108 Query query = instance.queryFactory().createQuery(InfluxDBClientFacadeMock.SCALAR_QUERY, QueryParameters.EMPTY,
110 var future = instance.executeQuery(query);
111 assertThat(future.isCompletedExceptionally(), is(Boolean.TRUE));