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.persistence.mapdb;
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;
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;
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;
46 * @author Martin Kühl - Initial contribution
48 public class MapDbPersistenceServiceOSGiTest extends JavaOSGiTest {
49 private MapDbPersistenceService persistenceService;
53 persistenceService = getService(QueryablePersistenceService.class, MapDbPersistenceService.class);
57 public static void tearDown() throws IOException {
58 // clean up database files ...
59 removeDirRecursive("userdata");
60 removeDirRecursive("runtime");
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);
73 public void storeShouldStoreTheItem() {
74 String name = "switch1";
75 String alias = "switch2";
76 State state = OnOffType.ON;
78 GenericItem item = new SwitchItem(name);
81 assertThat(persistenceService.getItemInfo(), not(hasItem(hasProperty("name", equalTo(name)))));
83 persistenceService.store(item);
85 waitForAssert(() -> assertThat(persistenceService.getItemInfo(), hasItem(hasProperty("name", equalTo(name)))));
87 persistenceService.store(item, alias);
89 waitForAssert(() -> assertThat(persistenceService.getItemInfo(),
90 hasItems(hasProperty("name", equalTo(name)), hasProperty("name", equalTo(alias)))));
94 public void queryShouldFindStoredItemsByName() {
95 String name = "dimmer";
96 State state = PercentType.HUNDRED;
98 GenericItem item = new DimmerItem(name);
101 FilterCriteria filter = new FilterCriteria();
102 filter.setItemName(name);
104 assertThat(persistenceService.query(filter), is(emptyIterable()));
106 persistenceService.store(item);
108 waitForAssert(() -> assertThat(persistenceService.query(filter),
109 contains(allOf(hasProperty("name", equalTo(name)), hasProperty("state", equalTo(state))))));
113 public void queryShouldFindStoredItemsByAlias() {
114 String name = "color";
115 String alias = "alias";
116 State state = HSBType.GREEN;
118 GenericItem item = new ColorItem(name);
119 item.setState(state);
121 FilterCriteria filterByName = new FilterCriteria();
122 filterByName.setItemName(name);
124 FilterCriteria filterByAlias = new FilterCriteria();
125 filterByAlias.setItemName(alias);
127 assertThat(persistenceService.query(filterByName), is(emptyIterable()));
128 assertThat(persistenceService.query(filterByAlias), is(emptyIterable()));
130 persistenceService.store(item, alias);
132 assertThat(persistenceService.query(filterByName), is(emptyIterable()));
133 waitForAssert(() -> assertThat(persistenceService.query(filterByAlias),
134 contains(allOf(hasProperty("name", equalTo(alias)), hasProperty("state", equalTo(state))))));