Search in sources :

Example 1 with StorageService

use of org.openhab.core.storage.StorageService in project openhab-core by openhab.

the class ChangeThingTypeOSGiTest method assertLoadingSpecializedThingTypeWorksDirectly.

@Test
public void assertLoadingSpecializedThingTypeWorksDirectly() {
    // println "[ChangeThingTypeOSGiTest] ======== assert loading specialized thing type works directly"
    StorageService storage = getService(StorageService.class);
    Map<String, Object> properties = Map.of("providedspecific", "there");
    Thing persistedThing = ThingFactory.createThing(thingTypeSpecific, new ThingUID("testBinding", "persistedThing"), new Configuration(properties), null, null);
    persistedThing.setProperty("universal", "survives");
    storage.getStorage(Thing.class.getName()).put("testBinding::persistedThing", persistedThing);
    selfChanging = true;
    unregisterService(storage);
    managedThingProvider = getService(ManagedThingProvider.class);
    assertThat(managedThingProvider, is(nullValue()));
    registerService(storage);
    managedThingProvider = getService(ManagedThingProvider.class);
    assertThat(managedThingProvider, is(notNullValue()));
    Collection<Thing> res = managedThingProvider.getAll();
    assertThat(res.size(), is(1));
    Thing thing = res.iterator().next();
    assertThat(thing.getUID().toString(), is("testBinding::persistedThing"));
    // Ensure that the ThingHandler has been registered as an OSGi service correctly
    waitForAssert(() -> {
        assertThat(thing.getHandler(), instanceOf(SpecificThingHandler.class));
    }, 4000, 100);
    ThingHandlerFactory handlerFactory = getService(ThingHandlerFactory.class, SampleThingHandlerFactory.class);
    assertThat(handlerFactory, not(nullValue()));
    // Ensure it's initialized
    waitForAssert(() -> {
        assertThat(specificInits, is(1));
        assertThat(genericInits, is(0));
    });
    // Ensure the Thing is ONLINE again
    assertThat(thing.getStatus(), is(ThingStatus.ONLINE));
}
Also used : Configuration(org.openhab.core.config.core.Configuration) ThingUID(org.openhab.core.thing.ThingUID) ManagedThingProvider(org.openhab.core.thing.ManagedThingProvider) Thing(org.openhab.core.thing.Thing) StorageService(org.openhab.core.storage.StorageService) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Example 2 with StorageService

use of org.openhab.core.storage.StorageService in project openhab-core by openhab.

the class ManagedItemProviderOSGiTest method assertItemsAreAddedAsGroupMembersOnDeferredCreation.

@SuppressWarnings("null")
@Test
public void assertItemsAreAddedAsGroupMembersOnDeferredCreation() throws ItemNotFoundException {
    StorageService storageService = getService(StorageService.class);
    assertThat(storageService, is(notNullValue()));
    Storage<PersistedItem> storage = storageService.getStorage(Item.class.getName());
    StrangeItem item = new StrangeItem("SomeStrangeItem");
    GroupItem groupItem = new GroupItem("SomeGroupItem");
    item.addGroupName(groupItem.getName());
    groupItem.addMember(item);
    String itemKey = item.getUID();
    // put items into the storage that cannot be handled (yet)
    storage.put(itemKey, itemProvider.toPersistableElement(item));
    itemProvider.add(groupItem);
    // start without the appropriate item factory - it only creates the group item
    assertThat(itemProvider.getAll().size(), is(1));
    assertThat(itemRegistry.getItems().size(), is(1));
    assertThat(itemProvider.get("SomeStrangeItem"), is(nullValue()));
    assertThat(itemProvider.get("SomeGroupItem"), is(notNullValue()));
    assertThat(itemRegistry.getItem("SomeGroupItem"), is(notNullValue()));
    try {
        assertThat(itemRegistry.getItem("SomeStrangeItem"), is(nullValue()));
        fail("the item is not (yet) expected to be there");
    } catch (ItemNotFoundException e) {
    // all good
    }
    // now register the item factory. Both items should be there...
    StrangeItemFactory factory = new StrangeItemFactory();
    registerService(factory);
    try {
        assertThat(itemProvider.getAll().size(), is(2));
        assertThat(itemRegistry.getItems().size(), is(2));
        Item item1 = itemRegistry.get("SomeStrangeItem");
        Item item2 = itemRegistry.get("SomeGroupItem");
        assertThat(itemRegistry.get("SomeStrangeItem"), is(notNullValue()));
        assertThat(item1, is(notNullValue()));
        assertThat(item1.getGroupNames().size(), is(1));
        assertThat(((GroupItem) item2).getMembers().size(), is(1));
    } finally {
        unregisterService(factory);
    }
}
Also used : PersistedItem(org.openhab.core.items.ManagedItemProvider.PersistedItem) SwitchItem(org.openhab.core.library.items.SwitchItem) NumberItem(org.openhab.core.library.items.NumberItem) PersistedItem(org.openhab.core.items.ManagedItemProvider.PersistedItem) StringItem(org.openhab.core.library.items.StringItem) StorageService(org.openhab.core.storage.StorageService) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Example 3 with StorageService

use of org.openhab.core.storage.StorageService in project openhab-core by openhab.

the class AutomationIntegrationJsonTest method before.

@BeforeEach
public void before() {
    logger.info("@Before.begin");
    getService(ItemRegistry.class);
    ItemProvider itemProvider = new ItemProvider() {

        @Override
        public void addProviderChangeListener(ProviderChangeListener<Item> listener) {
        }

        @Override
        public Collection<Item> getAll() {
            return Set.of(new SwitchItem("myMotionItem"), new SwitchItem("myPresenceItem"), new SwitchItem("myLampItem"), new SwitchItem("myMotionItem2"), new SwitchItem("myPresenceItem2"), new SwitchItem("myLampItem2"), new SwitchItem("myMotionItem11"), new SwitchItem("myLampItem11"), new SwitchItem("myMotionItem3"), new SwitchItem("templ_MotionItem"), new SwitchItem("templ_LampItem"));
        }

        @Override
        public void removeProviderChangeListener(ProviderChangeListener<Item> listener) {
        }
    };
    registerService(itemProvider);
    registerVolatileStorageService();
    EventSubscriber ruleEventHandler = new EventSubscriber() {

        @Override
        public Set<String> getSubscribedEventTypes() {
            return Set.of(RuleStatusInfoEvent.TYPE);
        }

        @Override
        @Nullable
        public EventFilter getEventFilter() {
            return null;
        }

        @Override
        public void receive(Event e) {
            logger.info("RuleEvent: {} --> {}", e.getTopic(), e.getPayload());
            if (e.getPayload().contains("RUNNING")) {
                ruleEvent = e;
            }
        }
    };
    registerService(ruleEventHandler);
    StorageService storageService = getService(StorageService.class);
    managedRuleProvider = getService(ManagedRuleProvider.class);
    eventPublisher = getService(EventPublisher.class);
    itemRegistry = getService(ItemRegistry.class);
    ruleRegistry = getService(RuleRegistry.class);
    ruleManager = getService(RuleManager.class);
    moduleTypeRegistry = getService(ModuleTypeRegistry.class);
    waitForAssert(() -> {
        assertThat(storageService, is(notNullValue()));
        // sometimes assert fails because EventPublisher service is null
        assertThat(eventPublisher, is(notNullValue()));
        assertThat(itemRegistry, is(notNullValue()));
        assertThat(ruleRegistry, is(notNullValue()));
        assertThat(ruleManager, is(notNullValue()));
        assertThat(managedRuleProvider, is(notNullValue()));
        assertThat(moduleTypeRegistry, is(notNullValue()));
    }, 9000, 1000);
    // start rule engine
    RuleEngineImpl ruleEngine = (RuleEngineImpl) ruleManager;
    ruleEngine.onReadyMarkerAdded(new ReadyMarker("", ""));
    waitForAssert(() -> assertTrue(ruleEngine.isStarted()));
    logger.info("@Before.finish");
}
Also used : ItemProvider(org.openhab.core.items.ItemProvider) EventSubscriber(org.openhab.core.events.EventSubscriber) EventPublisher(org.openhab.core.events.EventPublisher) ModuleTypeRegistry(org.openhab.core.automation.type.ModuleTypeRegistry) RuleEngineImpl(org.openhab.core.automation.internal.RuleEngineImpl) RuleRegistry(org.openhab.core.automation.RuleRegistry) ManagedRuleProvider(org.openhab.core.automation.ManagedRuleProvider) ItemRegistry(org.openhab.core.items.ItemRegistry) VolatileStorageService(org.openhab.core.test.storage.VolatileStorageService) StorageService(org.openhab.core.storage.StorageService) SwitchItem(org.openhab.core.library.items.SwitchItem) Item(org.openhab.core.items.Item) ProviderChangeListener(org.openhab.core.common.registry.ProviderChangeListener) RuleManager(org.openhab.core.automation.RuleManager) RuleStatusInfoEvent(org.openhab.core.automation.events.RuleStatusInfoEvent) ItemCommandEvent(org.openhab.core.items.events.ItemCommandEvent) Event(org.openhab.core.events.Event) ReadyMarker(org.openhab.core.service.ReadyMarker) SwitchItem(org.openhab.core.library.items.SwitchItem) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 4 with StorageService

use of org.openhab.core.storage.StorageService in project openhab-core by openhab.

the class AutomationIntegrationTest method before.

@BeforeEach
public void before() {
    logger.info("@Before.begin");
    getService(ItemRegistry.class);
    ItemProvider itemProvider = new ItemProvider() {

        @Override
        public void addProviderChangeListener(ProviderChangeListener<Item> listener) {
        }

        @Override
        public Collection<Item> getAll() {
            Set<Item> items = new HashSet<>();
            items.add(new SwitchItem("myMotionItem"));
            items.add(new SwitchItem("myPresenceItem"));
            items.add(new SwitchItem("myLampItem"));
            items.add(new SwitchItem("myMotionItem2"));
            items.add(new SwitchItem("myPresenceItem2"));
            items.add(new SwitchItem("myLampItem2"));
            items.add(new SwitchItem("myMotionItem3"));
            items.add(new SwitchItem("templ_MotionItem"));
            items.add(new SwitchItem("templ_LampItem"));
            items.add(new SwitchItem("myMotionItem3"));
            items.add(new SwitchItem("myPresenceItem3"));
            items.add(new SwitchItem("myLampItem3"));
            items.add(new SwitchItem("myMotionItem4"));
            items.add(new SwitchItem("myPresenceItem4"));
            items.add(new SwitchItem("myLampItem4"));
            items.add(new SwitchItem("myMotionItem5"));
            items.add(new SwitchItem("myPresenceItem5"));
            items.add(new SwitchItem("myLampItem5"));
            items.add(new SwitchItem("xtempl_MotionItem"));
            items.add(new SwitchItem("xtempl_LampItem"));
            return items;
        }

        @Override
        public void removeProviderChangeListener(ProviderChangeListener<Item> listener) {
        }
    };
    registerService(itemProvider);
    registerVolatileStorageService();
    StorageService storageService = getService(StorageService.class);
    eventPublisher = getService(EventPublisher.class);
    itemRegistry = getService(ItemRegistry.class);
    ruleRegistry = getService(RuleRegistry.class);
    ruleEngine = getService(RuleManager.class);
    managedRuleProvider = getService(ManagedRuleProvider.class);
    moduleTypeRegistry = getService(ModuleTypeRegistry.class);
    templateRegistry = getService(TemplateRegistry.class);
    waitForAssert(() -> {
        assertThat(eventPublisher, is(notNullValue()));
        assertThat(storageService, is(notNullValue()));
        assertThat(itemRegistry, is(notNullValue()));
        assertThat(ruleRegistry, is(notNullValue()));
        assertThat(ruleEngine, is(notNullValue()));
        assertThat(moduleTypeRegistry, is(notNullValue()));
        assertThat(templateRegistry, is(notNullValue()));
        assertThat(managedRuleProvider, is(notNullValue()));
    }, 9000, 1000);
    // start rule engine
    ((RuleEngineImpl) ruleEngine).onReadyMarkerAdded(new ReadyMarker("", ""));
    waitForAssert(() -> {
        assertThat(((RuleEngineImpl) ruleEngine).isStarted(), is(true));
    }, 5000, 1000);
    logger.info("@Before.finish");
}
Also used : ItemProvider(org.openhab.core.items.ItemProvider) EventPublisher(org.openhab.core.events.EventPublisher) ModuleTypeRegistry(org.openhab.core.automation.type.ModuleTypeRegistry) RuleEngineImpl(org.openhab.core.automation.internal.RuleEngineImpl) RuleRegistry(org.openhab.core.automation.RuleRegistry) ManagedRuleProvider(org.openhab.core.automation.ManagedRuleProvider) ItemRegistry(org.openhab.core.items.ItemRegistry) StorageService(org.openhab.core.storage.StorageService) SwitchItem(org.openhab.core.library.items.SwitchItem) Item(org.openhab.core.items.Item) TemplateRegistry(org.openhab.core.automation.template.TemplateRegistry) ProviderChangeListener(org.openhab.core.common.registry.ProviderChangeListener) RuleManager(org.openhab.core.automation.RuleManager) ReadyMarker(org.openhab.core.service.ReadyMarker) SwitchItem(org.openhab.core.library.items.SwitchItem) HashSet(java.util.HashSet) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 5 with StorageService

use of org.openhab.core.storage.StorageService in project openhab-core by openhab.

the class ThingManagerOSGiJavaTest method setUp.

@BeforeEach
public void setUp() throws Exception {
    configDescriptionChannel = new URI("test:channel");
    configDescriptionThing = new URI("test:test");
    thing = ThingBuilder.create(THING_TYPE_UID, THING_UID).withChannels(// 
    List.of(ChannelBuilder.create(CHANNEL_UID, CoreItemFactory.SWITCH).withLabel("Test Label").withDescription("Test Description").withType(CHANNEL_TYPE_UID).withDefaultTags(Set.of("Test Tag")).build())).build();
    registerVolatileStorageService();
    managedThingProvider = getService(ManagedThingProvider.class);
    itemRegistry = getService(ItemRegistry.class);
    assertNotNull(itemRegistry);
    thingRegistry = getService(ThingRegistry.class);
    assertNotNull(thingRegistry);
    StorageService storageService;
    storageService = getService(StorageService.class);
    assertNotNull(storageService);
    storage = storageService.getStorage("thing_status_storage");
    itemChannelLinkRegistry = getService(ItemChannelLinkRegistry.class);
    assertNotNull(itemChannelLinkRegistry);
    readyService = getService(ReadyService.class);
    assertNotNull(readyService);
    thingManager = getService(ThingManager.class);
    assertNotNull(thingManager);
    waitForAssert(() -> {
        try {
            assertThat(bundleContext.getServiceReferences(ReadyMarker.class, "(openhab.xmlThingTypes=" + bundleContext.getBundle().getSymbolicName() + ")"), is(notNullValue()));
        } catch (InvalidSyntaxException e) {
            throw new IllegalArgumentException(e);
        }
    });
}
Also used : ManagedThingProvider(org.openhab.core.thing.ManagedThingProvider) ReadyService(org.openhab.core.service.ReadyService) ItemChannelLinkRegistry(org.openhab.core.thing.link.ItemChannelLinkRegistry) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ThingManager(org.openhab.core.thing.ThingManager) ItemRegistry(org.openhab.core.items.ItemRegistry) ReadyMarker(org.openhab.core.service.ReadyMarker) URI(java.net.URI) ThingRegistry(org.openhab.core.thing.ThingRegistry) StorageService(org.openhab.core.storage.StorageService) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

StorageService (org.openhab.core.storage.StorageService)8 BeforeEach (org.junit.jupiter.api.BeforeEach)5 SwitchItem (org.openhab.core.library.items.SwitchItem)4 ReadyMarker (org.openhab.core.service.ReadyMarker)4 Test (org.junit.jupiter.api.Test)3 RuleManager (org.openhab.core.automation.RuleManager)3 RuleRegistry (org.openhab.core.automation.RuleRegistry)3 RuleEngineImpl (org.openhab.core.automation.internal.RuleEngineImpl)3 ModuleTypeRegistry (org.openhab.core.automation.type.ModuleTypeRegistry)3 ItemRegistry (org.openhab.core.items.ItemRegistry)3 JavaOSGiTest (org.openhab.core.test.java.JavaOSGiTest)3 ManagedRuleProvider (org.openhab.core.automation.ManagedRuleProvider)2 ProviderChangeListener (org.openhab.core.common.registry.ProviderChangeListener)2 EventPublisher (org.openhab.core.events.EventPublisher)2 Item (org.openhab.core.items.Item)2 ItemProvider (org.openhab.core.items.ItemProvider)2 PersistedItem (org.openhab.core.items.ManagedItemProvider.PersistedItem)2 NumberItem (org.openhab.core.library.items.NumberItem)2 StringItem (org.openhab.core.library.items.StringItem)2 ManagedThingProvider (org.openhab.core.thing.ManagedThingProvider)2