Search in sources :

Example 56 with Event

use of org.openhab.core.events.Event in project openhab-core by openhab.

the class ExpireManagerTest method testCancelExpiry.

@Test
void testCancelExpiry() throws InterruptedException, ItemNotFoundException {
    Item testItem = new SwitchItem(ITEMNAME);
    when(itemRegistryMock.getItem(ITEMNAME)).thenReturn(testItem);
    when(metadataRegistryMock.get(METADATA_KEY)).thenReturn(config("1s,ON"));
    Event event = ItemEventFactory.createCommandEvent(ITEMNAME, OnOffType.OFF);
    expireManager.receive(event);
    Thread.sleep(500L);
    event = ItemEventFactory.createCommandEvent(ITEMNAME, OnOffType.ON);
    expireManager.receive(event);
    Thread.sleep(2000L);
    verify(eventPublisherMock, never()).post(any());
}
Also used : SwitchItem(org.openhab.core.library.items.SwitchItem) NumberItem(org.openhab.core.library.items.NumberItem) Item(org.openhab.core.items.Item) StringItem(org.openhab.core.library.items.StringItem) Event(org.openhab.core.events.Event) SwitchItem(org.openhab.core.library.items.SwitchItem) Test(org.junit.jupiter.api.Test)

Example 57 with Event

use of org.openhab.core.events.Event in project openhab-core by openhab.

the class ExpireManagerTest method testStateExpiryWithCustomState.

@Test
void testStateExpiryWithCustomState() throws InterruptedException, ItemNotFoundException {
    Item testItem = new SwitchItem(ITEMNAME);
    when(itemRegistryMock.getItem(ITEMNAME)).thenReturn(testItem);
    when(metadataRegistryMock.get(METADATA_KEY)).thenReturn(config("1s,state=OFF"));
    Event event = ItemEventFactory.createCommandEvent(ITEMNAME, OnOffType.ON);
    expireManager.receive(event);
    verify(eventPublisherMock, never()).post(any());
    Thread.sleep(2500L);
    verify(eventPublisherMock).post(eq(ItemEventFactory.createStateEvent(ITEMNAME, OnOffType.OFF, ExpireManager.EVENT_SOURCE)));
}
Also used : SwitchItem(org.openhab.core.library.items.SwitchItem) NumberItem(org.openhab.core.library.items.NumberItem) Item(org.openhab.core.items.Item) StringItem(org.openhab.core.library.items.StringItem) Event(org.openhab.core.events.Event) SwitchItem(org.openhab.core.library.items.SwitchItem) Test(org.junit.jupiter.api.Test)

Example 58 with Event

use of org.openhab.core.events.Event in project openhab-core by openhab.

the class ExpireManagerTest method testMetadataChange.

@Test
void testMetadataChange() throws InterruptedException, ItemNotFoundException {
    Metadata md = new Metadata(METADATA_KEY, "1s", null);
    when(metadataRegistryMock.get(METADATA_KEY)).thenReturn(md);
    Event event = ItemEventFactory.createCommandEvent(ITEMNAME, OnOffType.ON);
    expireManager.receive(event);
    verify(eventPublisherMock, never()).post(any());
    Thread.sleep(2500L);
    verify(eventPublisherMock).post(eq(ItemEventFactory.createStateEvent(ITEMNAME, UnDefType.UNDEF, ExpireManager.EVENT_SOURCE)));
    when(metadataRegistryMock.get(METADATA_KEY)).thenReturn(null);
    expireManager.metadataChangeListener.removed(md);
    reset(eventPublisherMock);
    event = ItemEventFactory.createCommandEvent(ITEMNAME, OnOffType.ON);
    expireManager.receive(event);
    verify(eventPublisherMock, never()).post(any());
    Thread.sleep(2500L);
    verify(eventPublisherMock, never()).post(any());
}
Also used : Metadata(org.openhab.core.items.Metadata) Event(org.openhab.core.events.Event) Test(org.junit.jupiter.api.Test)

Example 59 with Event

use of org.openhab.core.events.Event in project openhab-core by openhab.

the class ExpireManagerTest method testDefaultStateExpiry.

@Test
void testDefaultStateExpiry() throws InterruptedException {
    when(metadataRegistryMock.get(METADATA_KEY)).thenReturn(new Metadata(METADATA_KEY, "1s", null));
    Event event = ItemEventFactory.createCommandEvent(ITEMNAME, OnOffType.ON);
    expireManager.receive(event);
    verify(eventPublisherMock, never()).post(any());
    Thread.sleep(2500L);
    verify(eventPublisherMock).post(eq(ItemEventFactory.createStateEvent(ITEMNAME, UnDefType.UNDEF, ExpireManager.EVENT_SOURCE)));
}
Also used : Metadata(org.openhab.core.items.Metadata) Event(org.openhab.core.events.Event) Test(org.junit.jupiter.api.Test)

Example 60 with Event

use of org.openhab.core.events.Event in project openhab-core by openhab.

the class ExpireManagerTest method testIgnoreStateUpdateExtendsExpiryOnStateChange.

@Test
void testIgnoreStateUpdateExtendsExpiryOnStateChange() throws InterruptedException, ItemNotFoundException {
    Item testItem = new NumberItem(ITEMNAME);
    when(itemRegistryMock.getItem(ITEMNAME)).thenReturn(testItem);
    when(metadataRegistryMock.get(METADATA_KEY)).thenReturn(config("2s", Map.of(ExpireConfig.CONFIG_IGNORE_STATE_UPDATES, true)));
    Event event = ItemEventFactory.createCommandEvent(ITEMNAME, new DecimalType(1));
    expireManager.receive(event);
    Thread.sleep(1500L);
    event = ItemEventFactory.createStateChangedEvent(ITEMNAME, new DecimalType(2), new DecimalType(1));
    expireManager.receive(event);
    Thread.sleep(1500L);
    verify(eventPublisherMock, never()).post(any());
    Thread.sleep(2000L);
    verify(eventPublisherMock, times(1)).post(any());
}
Also used : NumberItem(org.openhab.core.library.items.NumberItem) SwitchItem(org.openhab.core.library.items.SwitchItem) NumberItem(org.openhab.core.library.items.NumberItem) Item(org.openhab.core.items.Item) StringItem(org.openhab.core.library.items.StringItem) Event(org.openhab.core.events.Event) DecimalType(org.openhab.core.library.types.DecimalType) Test(org.junit.jupiter.api.Test)

Aggregations

Event (org.openhab.core.events.Event)120 Test (org.junit.jupiter.api.Test)93 JavaOSGiTest (org.openhab.core.test.java.JavaOSGiTest)58 EventSubscriber (org.openhab.core.events.EventSubscriber)46 ItemCommandEvent (org.openhab.core.items.events.ItemCommandEvent)38 Nullable (org.eclipse.jdt.annotation.Nullable)28 SwitchItem (org.openhab.core.library.items.SwitchItem)28 EventPublisher (org.openhab.core.events.EventPublisher)27 NonNullByDefault (org.eclipse.jdt.annotation.NonNullByDefault)25 BeforeEach (org.junit.jupiter.api.BeforeEach)22 Item (org.openhab.core.items.Item)22 Set (java.util.Set)21 Rule (org.openhab.core.automation.Rule)20 RuleStatusInfoEvent (org.openhab.core.automation.events.RuleStatusInfoEvent)20 EventFilter (org.openhab.core.events.EventFilter)20 NumberItem (org.openhab.core.library.items.NumberItem)18 LinkedList (java.util.LinkedList)17 List (java.util.List)17 ArrayList (java.util.ArrayList)16 Configuration (org.openhab.core.config.core.Configuration)16