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;
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;
45 * @author Martin Kühl - Initial contribution
47 public class MapDbPersistenceServiceOSGiTest extends JavaOSGiTest {
48 private MapDbPersistenceService persistenceService;
52 persistenceService = getService(QueryablePersistenceService.class, MapDbPersistenceService.class);
56 public static void tearDown() throws IOException {
57 // clean up database files ...
58 removeDirRecursive("userdata");
59 removeDirRecursive("runtime");
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);
70 public void storeShouldStoreTheItem() {
71 String name = "switch1";
72 String alias = "switch2";
73 State state = OnOffType.ON;
75 GenericItem item = new SwitchItem(name);
78 assertThat(persistenceService.getItemInfo(), not(hasItem(hasProperty("name", equalTo(name)))));
80 persistenceService.store(item);
82 assertThat(persistenceService.getItemInfo(), hasItem(hasProperty("name", equalTo(name))));
84 persistenceService.store(item, alias);
86 assertThat(persistenceService.getItemInfo(),
87 hasItems(hasProperty("name", equalTo(name)), hasProperty("name", equalTo(alias))));
91 public void queryShouldFindStoredItemsByName() {
92 String name = "dimmer";
93 State state = PercentType.HUNDRED;
95 GenericItem item = new DimmerItem(name);
98 FilterCriteria filter = new FilterCriteria();
99 filter.setItemName(name);
101 assertThat(persistenceService.query(filter), is(emptyIterable()));
103 persistenceService.store(item);
105 assertThat(persistenceService.query(filter),
106 contains(allOf(hasProperty("name", equalTo(name)), hasProperty("state", equalTo(state)))));
110 public void queryShouldFindStoredItemsByAlias() {
111 String name = "color";
112 String alias = "alias";
113 State state = HSBType.GREEN;
115 GenericItem item = new ColorItem(name);
116 item.setState(state);
118 FilterCriteria filterByName = new FilterCriteria();
119 filterByName.setItemName(name);
121 FilterCriteria filterByAlias = new FilterCriteria();
122 filterByAlias.setItemName(alias);
124 assertThat(persistenceService.query(filterByName), is(emptyIterable()));
125 assertThat(persistenceService.query(filterByAlias), is(emptyIterable()));
127 persistenceService.store(item, alias);
129 assertThat(persistenceService.query(filterByName), is(emptyIterable()));
130 assertThat(persistenceService.query(filterByAlias),
131 contains(allOf(hasProperty("name", equalTo(alias)), hasProperty("state", equalTo(state)))));