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