]> git.basschouten.com Git - openhab-addons.git/blob
175bb21cf6ed9d25954265b8bceab4a6a5c8f52b
[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.mapdb;
14
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.hamcrest.beans.HasPropertyWithValue.hasProperty;
18 import static org.hamcrest.collection.IsEmptyIterable.emptyIterable;
19 import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.nio.file.Files;
24 import java.nio.file.Path;
25 import java.nio.file.Paths;
26
27 import org.junit.jupiter.api.AfterAll;
28 import org.junit.jupiter.api.BeforeEach;
29 import org.junit.jupiter.api.Test;
30 import org.openhab.core.items.GenericItem;
31 import org.openhab.core.library.items.ColorItem;
32 import org.openhab.core.library.items.DimmerItem;
33 import org.openhab.core.library.items.SwitchItem;
34 import org.openhab.core.library.types.HSBType;
35 import org.openhab.core.library.types.OnOffType;
36 import org.openhab.core.library.types.PercentType;
37 import org.openhab.core.persistence.FilterCriteria;
38 import org.openhab.core.persistence.QueryablePersistenceService;
39 import org.openhab.core.test.java.JavaOSGiTest;
40 import org.openhab.core.types.State;
41 import org.openhab.persistence.mapdb.internal.MapDbPersistenceService;
42
43 /**
44  *
45  * @author Martin Kühl - Initial contribution
46  */
47 public class MapDbPersistenceServiceOSGiTest extends JavaOSGiTest {
48     private MapDbPersistenceService persistenceService;
49
50     @BeforeEach
51     public void setUp() {
52         persistenceService = getService(QueryablePersistenceService.class, MapDbPersistenceService.class);
53     }
54
55     @AfterAll
56     public static void tearDown() throws IOException {
57         // clean up database files ...
58         removeDirRecursive("userdata");
59         removeDirRecursive("runtime");
60     }
61
62     private static void removeDirRecursive(final String dir) throws IOException {
63         final Path path = Paths.get(dir);
64         if (Files.exists(path)) {
65             Files.walk(path).map(Path::toFile).sorted().forEach(File::delete);
66         }
67     }
68
69     @Test
70     public void storeShouldStoreTheItem() {
71         String name = "switch1";
72         String alias = "switch2";
73         State state = OnOffType.ON;
74
75         GenericItem item = new SwitchItem(name);
76         item.setState(state);
77
78         assertThat(persistenceService.getItemInfo(), not(hasItem(hasProperty("name", equalTo(name)))));
79
80         persistenceService.store(item);
81
82         assertThat(persistenceService.getItemInfo(), hasItem(hasProperty("name", equalTo(name))));
83
84         persistenceService.store(item, alias);
85
86         assertThat(persistenceService.getItemInfo(),
87                 hasItems(hasProperty("name", equalTo(name)), hasProperty("name", equalTo(alias))));
88     }
89
90     @Test
91     public void queryShouldFindStoredItemsByName() {
92         String name = "dimmer";
93         State state = PercentType.HUNDRED;
94
95         GenericItem item = new DimmerItem(name);
96         item.setState(state);
97
98         FilterCriteria filter = new FilterCriteria();
99         filter.setItemName(name);
100
101         assertThat(persistenceService.query(filter), is(emptyIterable()));
102
103         persistenceService.store(item);
104
105         assertThat(persistenceService.query(filter),
106                 contains(allOf(hasProperty("name", equalTo(name)), hasProperty("state", equalTo(state)))));
107     }
108
109     @Test
110     public void queryShouldFindStoredItemsByAlias() {
111         String name = "color";
112         String alias = "alias";
113         State state = HSBType.GREEN;
114
115         GenericItem item = new ColorItem(name);
116         item.setState(state);
117
118         FilterCriteria filterByName = new FilterCriteria();
119         filterByName.setItemName(name);
120
121         FilterCriteria filterByAlias = new FilterCriteria();
122         filterByAlias.setItemName(alias);
123
124         assertThat(persistenceService.query(filterByName), is(emptyIterable()));
125         assertThat(persistenceService.query(filterByAlias), is(emptyIterable()));
126
127         persistenceService.store(item, alias);
128
129         assertThat(persistenceService.query(filterByName), is(emptyIterable()));
130         assertThat(persistenceService.query(filterByAlias),
131                 contains(allOf(hasProperty("name", equalTo(alias)), hasProperty("state", equalTo(state)))));
132     }
133 }