Search in sources :

Example 6 with StorageService

use of org.eclipse.smarthome.core.storage.StorageService in project smarthome by eclipse.

the class ManagedItemProviderOSGiTest method assertItemsAreThereOnceTheFactoryGetsAdded.

@Test
public void assertItemsAreThereOnceTheFactoryGetsAdded() throws ItemNotFoundException {
    StorageService storageService = getService(StorageService.class);
    assertThat(storageService, is(notNullValue()));
    Storage<PersistedItem> storage = storageService.getStorage(Item.class.getName());
    StrangeItem item = new StrangeItem("SomeStrangeItem");
    String key = item.getUID();
    // put an item into the storage that cannot be handled (yet)
    storage.put(key, itemProvider.toPersistableElement(item));
    // start without the appropriate item factory - it's going to fail silently, leaving a debug log
    assertThat(itemProvider.getAll().size(), is(0));
    assertThat(itemRegistry.getItems().size(), is(0));
    assertThat(itemProvider.get("SomeStrangeItem"), is(nullValue()));
    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. The item should be there...
    StrangeItemFactory factory = new StrangeItemFactory();
    registerService(factory);
    try {
        assertThat(itemProvider.getAll().size(), is(1));
        assertThat(itemRegistry.getItems().size(), is(1));
        assertThat(itemProvider.get("SomeStrangeItem"), is(notNullValue()));
        assertThat(itemRegistry.getItem("SomeStrangeItem"), is(notNullValue()));
    } finally {
        unregisterService(factory);
    }
}
Also used : PersistedItem(org.eclipse.smarthome.core.items.ManagedItemProvider.PersistedItem) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) StringItem(org.eclipse.smarthome.core.library.items.StringItem) PersistedItem(org.eclipse.smarthome.core.items.ManagedItemProvider.PersistedItem) StorageService(org.eclipse.smarthome.core.storage.StorageService) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 7 with StorageService

use of org.eclipse.smarthome.core.storage.StorageService in project smarthome by eclipse.

the class AutomationIntegrationJsonTest method before.

// rules imported from
// json files
@Before
public void before() {
    logger.info("@Before.begin");
    getService(ItemRegistry.class);
    ItemProvider itemProvider = new ItemProvider() {

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

        @Override
        @NonNull
        public Collection<@NonNull Item> getAll() {
            HashSet<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("myMotionItem11"));
            items.add(new SwitchItem("myLampItem11"));
            items.add(new SwitchItem("myMotionItem3"));
            items.add(new SwitchItem("templ_MotionItem"));
            items.add(new SwitchItem("templ_LampItem"));
            return items;
        }

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

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

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

        @Override
        public void receive(@NonNull Event e) {
            logger.info("RuleEvent: " + e.getTopic() + " --> " + e.getPayload());
            System.out.println("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);
    logger.info("@Before.finish");
}
Also used : ItemProvider(org.eclipse.smarthome.core.items.ItemProvider) EventSubscriber(org.eclipse.smarthome.core.events.EventSubscriber) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) ModuleTypeRegistry(org.eclipse.smarthome.automation.type.ModuleTypeRegistry) RuleRegistry(org.eclipse.smarthome.automation.RuleRegistry) ManagedRuleProvider(org.eclipse.smarthome.automation.core.ManagedRuleProvider) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) StorageService(org.eclipse.smarthome.core.storage.StorageService) VolatileStorageService(org.eclipse.smarthome.test.storage.VolatileStorageService) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) Item(org.eclipse.smarthome.core.items.Item) NonNull(org.eclipse.jdt.annotation.NonNull) ProviderChangeListener(org.eclipse.smarthome.core.common.registry.ProviderChangeListener) RuleManager(org.eclipse.smarthome.automation.RuleManager) ItemCommandEvent(org.eclipse.smarthome.core.items.events.ItemCommandEvent) RuleStatusInfoEvent(org.eclipse.smarthome.automation.events.RuleStatusInfoEvent) Event(org.eclipse.smarthome.core.events.Event) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) HashSet(java.util.HashSet) Before(org.junit.Before)

Aggregations

StorageService (org.eclipse.smarthome.core.storage.StorageService)7 SwitchItem (org.eclipse.smarthome.core.library.items.SwitchItem)4 Before (org.junit.Before)4 ModuleTypeRegistry (org.eclipse.smarthome.automation.type.ModuleTypeRegistry)3 ItemRegistry (org.eclipse.smarthome.core.items.ItemRegistry)3 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)3 Test (org.junit.Test)3 HashSet (java.util.HashSet)2 NonNull (org.eclipse.jdt.annotation.NonNull)2 RuleManager (org.eclipse.smarthome.automation.RuleManager)2 RuleRegistry (org.eclipse.smarthome.automation.RuleRegistry)2 ManagedRuleProvider (org.eclipse.smarthome.automation.core.ManagedRuleProvider)2 ProviderChangeListener (org.eclipse.smarthome.core.common.registry.ProviderChangeListener)2 EventPublisher (org.eclipse.smarthome.core.events.EventPublisher)2 Item (org.eclipse.smarthome.core.items.Item)2 ItemProvider (org.eclipse.smarthome.core.items.ItemProvider)2 PersistedItem (org.eclipse.smarthome.core.items.ManagedItemProvider.PersistedItem)2 NumberItem (org.eclipse.smarthome.core.library.items.NumberItem)2 StringItem (org.eclipse.smarthome.core.library.items.StringItem)2 ManagedThingProvider (org.eclipse.smarthome.core.thing.ManagedThingProvider)2