]> git.basschouten.com Git - openhab-addons.git/blob
05ead8fb55e0d2db2ec2606bed4938fc348b2e2f
[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.persistence.influxdb.internal;
14
15 import static org.junit.jupiter.api.Assertions.assertThrows;
16 import static org.mockito.ArgumentMatchers.any;
17 import static org.mockito.Mockito.*;
18 import static org.openhab.persistence.influxdb.internal.InfluxDBConfiguration.DATABASE_PARAM;
19 import static org.openhab.persistence.influxdb.internal.InfluxDBConfiguration.PASSWORD_PARAM;
20 import static org.openhab.persistence.influxdb.internal.InfluxDBConfiguration.RETENTION_POLICY_PARAM;
21 import static org.openhab.persistence.influxdb.internal.InfluxDBConfiguration.TOKEN_PARAM;
22 import static org.openhab.persistence.influxdb.internal.InfluxDBConfiguration.URL_PARAM;
23 import static org.openhab.persistence.influxdb.internal.InfluxDBConfiguration.USER_PARAM;
24 import static org.openhab.persistence.influxdb.internal.InfluxDBConfiguration.VERSION_PARAM;
25
26 import java.util.Map;
27
28 import org.eclipse.jdt.annotation.NonNullByDefault;
29 import org.junit.jupiter.api.Test;
30 import org.junit.jupiter.api.extension.ExtendWith;
31 import org.mockito.Mock;
32 import org.mockito.junit.jupiter.MockitoExtension;
33 import org.openhab.core.items.ItemRegistry;
34 import org.openhab.core.items.MetadataRegistry;
35 import org.openhab.persistence.influxdb.InfluxDBPersistenceService;
36
37 /**
38  * @author Joan Pujol Espinar - Initial contribution
39  */
40 @ExtendWith(MockitoExtension.class)
41 @NonNullByDefault
42 public class InfluxDBPersistenceServiceTest {
43     private static final Map<String, Object> VALID_V1_CONFIGURATION = Map.of( //
44             URL_PARAM, "http://localhost:8086", //
45             VERSION_PARAM, InfluxDBVersion.V1.name(), //
46             USER_PARAM, "user", PASSWORD_PARAM, "password", //
47             DATABASE_PARAM, "openhab", //
48             RETENTION_POLICY_PARAM, "default");
49
50     private static final Map<String, Object> VALID_V2_CONFIGURATION = Map.of( //
51             URL_PARAM, "http://localhost:8086", //
52             VERSION_PARAM, InfluxDBVersion.V2.name(), //
53             TOKEN_PARAM, "sampletoken", //
54             DATABASE_PARAM, "openhab", //
55             RETENTION_POLICY_PARAM, "default");
56
57     private static final Map<String, Object> INVALID_V1_CONFIGURATION = Map.of(//
58             URL_PARAM, "http://localhost:8086", //
59             VERSION_PARAM, InfluxDBVersion.V1.name(), //
60             USER_PARAM, "user", //
61             DATABASE_PARAM, "openhab", //
62             RETENTION_POLICY_PARAM, "default");
63
64     private static final Map<String, Object> INVALID_V2_CONFIGURATION = Map.of( //
65             URL_PARAM, "http://localhost:8086", //
66             VERSION_PARAM, InfluxDBVersion.V2.name(), //
67             DATABASE_PARAM, "openhab", //
68             RETENTION_POLICY_PARAM, "default");
69
70     private @Mock @NonNullByDefault({}) InfluxDBRepository influxDBRepositoryMock;
71
72     private final InfluxDBMetadataService influxDBMetadataService = new InfluxDBMetadataService(
73             mock(MetadataRegistry.class));
74
75     @Test
76     public void activateWithValidV1ConfigShouldConnectRepository() {
77         getService(VALID_V1_CONFIGURATION);
78         verify(influxDBRepositoryMock).connect();
79     }
80
81     @Test
82     public void activateWithValidV2ConfigShouldConnectRepository() {
83         getService(VALID_V2_CONFIGURATION);
84         verify(influxDBRepositoryMock).connect();
85     }
86
87     @Test
88     public void activateWithInvalidV1ConfigShouldFail() {
89         assertThrows(IllegalArgumentException.class, () -> getService(INVALID_V1_CONFIGURATION));
90     }
91
92     @Test
93     public void activateWithInvalidV2ShouldFail() {
94         assertThrows(IllegalArgumentException.class, () -> getService(INVALID_V2_CONFIGURATION));
95     }
96
97     @Test
98     public void deactivateShouldDisconnectRepository() {
99         InfluxDBPersistenceService instance = getService(VALID_V2_CONFIGURATION);
100         instance.deactivate();
101         verify(influxDBRepositoryMock).disconnect();
102     }
103
104     @Test
105     public void storeItemWithConnectedRepository() throws UnexpectedConditionException {
106         InfluxDBPersistenceService instance = getService(VALID_V2_CONFIGURATION);
107         when(influxDBRepositoryMock.isConnected()).thenReturn(true);
108         instance.store(ItemTestHelper.createNumberItem("number", 5));
109         verify(influxDBRepositoryMock).write(any());
110     }
111
112     @Test
113     public void storeItemWithDisconnectedRepositoryIsIgnored() throws UnexpectedConditionException {
114         InfluxDBPersistenceService instance = getService(VALID_V2_CONFIGURATION);
115         when(influxDBRepositoryMock.isConnected()).thenReturn(false);
116         instance.store(ItemTestHelper.createNumberItem("number", 5));
117         verify(influxDBRepositoryMock, never()).write(any());
118     }
119
120     private InfluxDBPersistenceService getService(Map<String, Object> config) {
121         return new InfluxDBPersistenceService(mock(ItemRegistry.class), influxDBMetadataService, config) {
122             @Override
123             protected InfluxDBRepository createInfluxDBRepository() {
124                 return influxDBRepositoryMock;
125             }
126         };
127     }
128 }