Search in sources :

Example 11 with Event

use of org.eclipse.smarthome.core.events.Event in project smarthome by eclipse.

the class ThingEventFactoryTest method testCreateEvent_ChannelTriggeredEvent.

@Test
public void testCreateEvent_ChannelTriggeredEvent() throws Exception {
    Event event = factory.createEvent(ChannelTriggeredEvent.TYPE, CHANNEL_TRIGGERED_EVENT_TOPIC, CHANNEL_TRIGGERED_EVENT_PAYLOAD, null);
    assertThat(event, is(instanceOf(ChannelTriggeredEvent.class)));
    ChannelTriggeredEvent triggeredEvent = (ChannelTriggeredEvent) event;
    assertEquals(ChannelTriggeredEvent.TYPE, triggeredEvent.getType());
    assertEquals(CHANNEL_TRIGGERED_EVENT_TOPIC, triggeredEvent.getTopic());
    assertEquals(CHANNEL_TRIGGERED_EVENT_PAYLOAD, triggeredEvent.getPayload());
    assertNotNull(triggeredEvent.getEvent());
    assertEquals(CommonTriggerEvents.PRESSED, triggeredEvent.getEvent());
}
Also used : Event(org.eclipse.smarthome.core.events.Event) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 12 with Event

use of org.eclipse.smarthome.core.events.Event in project smarthome by eclipse.

the class ThingEventFactoryTest method testCreateEvent_ThingAddedEvent.

@Test
public void testCreateEvent_ThingAddedEvent() throws Exception {
    Event event = factory.createEvent(ThingAddedEvent.TYPE, THING_ADDED_EVENT_TOPIC, THING_ADDED_EVENT_PAYLOAD, null);
    assertThat(event, is(instanceOf(ThingAddedEvent.class)));
    ThingAddedEvent addedEvent = (ThingAddedEvent) event;
    assertEquals(ThingAddedEvent.TYPE, addedEvent.getType());
    assertEquals(THING_ADDED_EVENT_TOPIC, addedEvent.getTopic());
    assertEquals(THING_ADDED_EVENT_PAYLOAD, addedEvent.getPayload());
    assertNotNull(addedEvent.getThing());
    assertEquals(THING_UID.getAsString(), addedEvent.getThing().UID);
}
Also used : Event(org.eclipse.smarthome.core.events.Event) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 13 with Event

use of org.eclipse.smarthome.core.events.Event in project smarthome by eclipse.

the class FirmwareUpdateServiceTest method testEvents.

@Test
public void testEvents() {
    doAnswer(invocation -> {
        Firmware firmware = (Firmware) invocation.getArguments()[0];
        ProgressCallback progressCallback = (ProgressCallback) invocation.getArguments()[1];
        progressCallback.defineSequence(SEQUENCE);
        progressCallback.next();
        progressCallback.next();
        progressCallback.next();
        progressCallback.next();
        thing1.setProperty(Thing.PROPERTY_FIRMWARE_VERSION, firmware.getVersion());
        return null;
    }).when(handler1).updateFirmware(any(Firmware.class), any(ProgressCallback.class));
    // getFirmwareStatusInfo() method will internally generate and post one FirmwareStatusInfoEvent event.
    assertThat(firmwareUpdateService.getFirmwareStatusInfo(THING1_UID), is(updateExecutableInfoFw112));
    firmwareUpdateService.updateFirmware(THING1_UID, FW112_EN.getUID(), null);
    AtomicReference<List<Event>> events = new AtomicReference<>(new ArrayList<>());
    ArgumentCaptor<Event> eventCaptor = ArgumentCaptor.forClass(Event.class);
    waitForAssert(() -> {
        // Wait for four FirmwareUpdateProgressInfoEvents plus one FirmwareStatusInfoEvent event.
        verify(mockPublisher, atLeast(SEQUENCE.length + 1)).post(eventCaptor.capture());
    });
    events.get().addAll(eventCaptor.getAllValues());
    List<Event> list = events.get().stream().filter(event -> event instanceof FirmwareUpdateProgressInfoEvent).collect(Collectors.toList());
    assertTrue(list.size() >= SEQUENCE.length);
    for (int i = 0; i < SEQUENCE.length; i++) {
        FirmwareUpdateProgressInfoEvent event = (FirmwareUpdateProgressInfoEvent) list.get(i);
        assertThat(event.getTopic(), containsString(THING1_UID.getAsString()));
        assertThat(event.getThingUID(), is(THING1_UID));
        assertThat(event.getProgressInfo().getProgressStep(), is(SEQUENCE[i]));
    }
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) CoreMatchers(org.hamcrest.CoreMatchers) MockitoAnnotations.initMocks(org.mockito.MockitoAnnotations.initMocks) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Mock(org.mockito.Mock) ProgressCallback(org.eclipse.smarthome.core.thing.binding.firmware.ProgressCallback) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) Firmware(org.eclipse.smarthome.core.thing.binding.firmware.Firmware) AtomicReference(java.util.concurrent.atomic.AtomicReference) FirmwareUpdateBackgroundTransferHandler(org.eclipse.smarthome.core.thing.binding.firmware.FirmwareUpdateBackgroundTransferHandler) ArrayList(java.util.ArrayList) ArgumentCaptor(org.mockito.ArgumentCaptor) FirmwareStatusInfo(org.eclipse.smarthome.core.thing.firmware.FirmwareStatusInfo) SafeCaller(org.eclipse.smarthome.core.common.SafeCaller) Locale(java.util.Locale) FirmwareUID(org.eclipse.smarthome.core.thing.binding.firmware.FirmwareUID) Map(java.util.Map) Thing(org.eclipse.smarthome.core.thing.Thing) After(org.junit.After) FirmwareUpdateHandler(org.eclipse.smarthome.core.thing.binding.firmware.FirmwareUpdateHandler) NoSuchElementException(java.util.NoSuchElementException) Bundle(org.osgi.framework.Bundle) ExpectedException(org.junit.rules.ExpectedException) Hashtable(java.util.Hashtable) Before(org.junit.Before) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) TranslationProvider(org.eclipse.smarthome.core.i18n.TranslationProvider) ConfigDescriptionValidator(org.eclipse.smarthome.config.core.validation.ConfigDescriptionValidator) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) IOException(java.io.IOException) Test(org.junit.Test) Constants(org.eclipse.smarthome.core.thing.firmware.Constants) Collectors(java.util.stream.Collectors) TimeUnit(java.util.concurrent.TimeUnit) Mockito(org.mockito.Mockito) List(java.util.List) Stream(java.util.stream.Stream) Rule(org.junit.Rule) ThingBuilder(org.eclipse.smarthome.core.thing.binding.builder.ThingBuilder) LocaleProvider(org.eclipse.smarthome.core.i18n.LocaleProvider) ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) ProgressStep(org.eclipse.smarthome.core.thing.binding.firmware.ProgressStep) Assert(org.junit.Assert) Event(org.eclipse.smarthome.core.events.Event) Collections(java.util.Collections) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) ProgressCallback(org.eclipse.smarthome.core.thing.binding.firmware.ProgressCallback) Event(org.eclipse.smarthome.core.events.Event) ArrayList(java.util.ArrayList) List(java.util.List) AtomicReference(java.util.concurrent.atomic.AtomicReference) Firmware(org.eclipse.smarthome.core.thing.binding.firmware.Firmware) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 14 with Event

use of org.eclipse.smarthome.core.events.Event in project smarthome by eclipse.

the class GroupItemTest method testItemUpdateWithItemRegistry.

@Ignore
@Test
public void testItemUpdateWithItemRegistry() {
    GroupItem item = new GroupItem("mySimpleGroupItem");
    item.setLabel("firstLabel");
    itemRegistry.add(item);
    GroupItem updatedItem = (GroupItem) itemRegistry.get("mySimpleGroupItem");
    assertNotNull(updatedItem);
    events.clear();
    updatedItem.setLabel("secondLabel");
    itemRegistry.update(updatedItem);
    waitForAssert(() -> assertThat(events.size(), is(1)));
    List<Event> stateChanges = events.stream().filter(it -> it instanceof ItemUpdatedEvent).collect(Collectors.toList());
    assertThat(stateChanges.size(), is(1));
    ItemUpdatedEvent change = (ItemUpdatedEvent) stateChanges.get(0);
    assertThat(change.getItem().label, is("secondLabel"));
}
Also used : SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) CoreMatchers(org.hamcrest.CoreMatchers) MockitoAnnotations.initMocks(org.mockito.MockitoAnnotations.initMocks) RollershutterItem(org.eclipse.smarthome.core.library.items.RollershutterItem) NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) Mock(org.mockito.Mock) OnOffType(org.eclipse.smarthome.core.library.types.OnOffType) UnDefType(org.eclipse.smarthome.core.types.UnDefType) EventFilter(org.eclipse.smarthome.core.events.EventFilter) EventSubscriber(org.eclipse.smarthome.core.events.EventSubscriber) GroupFunctionDTO(org.eclipse.smarthome.core.items.dto.GroupFunctionDTO) GroupFunctionHelper(org.eclipse.smarthome.core.internal.items.GroupFunctionHelper) HashSet(java.util.HashSet) ColorItem(org.eclipse.smarthome.core.library.items.ColorItem) GroupItemStateChangedEvent(org.eclipse.smarthome.core.items.events.GroupItemStateChangedEvent) Temperature(javax.measure.quantity.Temperature) Quantity(javax.measure.Quantity) Pressure(javax.measure.quantity.Pressure) ItemStateConverterImpl(org.eclipse.smarthome.core.internal.items.ItemStateConverterImpl) QuantityType(org.eclipse.smarthome.core.library.types.QuantityType) CoreItemFactory(org.eclipse.smarthome.core.library.CoreItemFactory) StringType(org.eclipse.smarthome.core.library.types.StringType) LinkedList(java.util.LinkedList) State(org.eclipse.smarthome.core.types.State) Dimensionless(javax.measure.quantity.Dimensionless) Before(org.junit.Before) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) RefreshType(org.eclipse.smarthome.core.types.RefreshType) Set(java.util.Set) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) ItemUpdatedEvent(org.eclipse.smarthome.core.items.events.ItemUpdatedEvent) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) Collectors(java.util.stream.Collectors) RawType(org.eclipse.smarthome.core.library.types.RawType) IsCollectionWithSize.hasSize(org.hamcrest.collection.IsCollectionWithSize.hasSize) List(java.util.List) HSBType(org.eclipse.smarthome.core.library.types.HSBType) Ignore(org.junit.Ignore) PercentType(org.eclipse.smarthome.core.library.types.PercentType) Units(tec.uom.se.unit.Units) ArithmeticGroupFunction(org.eclipse.smarthome.core.library.types.ArithmeticGroupFunction) UnitProvider(org.eclipse.smarthome.core.i18n.UnitProvider) DimmerItem(org.eclipse.smarthome.core.library.items.DimmerItem) Assert(org.junit.Assert) Event(org.eclipse.smarthome.core.events.Event) Collections(java.util.Collections) GroupItemStateChangedEvent(org.eclipse.smarthome.core.items.events.GroupItemStateChangedEvent) ItemUpdatedEvent(org.eclipse.smarthome.core.items.events.ItemUpdatedEvent) Event(org.eclipse.smarthome.core.events.Event) ItemUpdatedEvent(org.eclipse.smarthome.core.items.events.ItemUpdatedEvent) Ignore(org.junit.Ignore) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 15 with Event

use of org.eclipse.smarthome.core.events.Event in project smarthome by eclipse.

the class GroupItemTest method assertThatGroupItemwithDimmeritemAcceptsGetsPercentTypeStateIfMembersHavePercentTypeStates.

@Test
public void assertThatGroupItemwithDimmeritemAcceptsGetsPercentTypeStateIfMembersHavePercentTypeStates() {
    events.clear();
    GroupItem groupItem = new GroupItem("root", new DimmerItem("myDimmer"), new ArithmeticGroupFunction.Avg());
    groupItem.setItemStateConverter(itemStateConverter);
    DimmerItem member1 = new DimmerItem("dimmer1");
    groupItem.addMember(member1);
    DimmerItem member2 = new DimmerItem("dimmer2");
    groupItem.addMember(member2);
    groupItem.setEventPublisher(publisher);
    member1.setState(new PercentType(50));
    waitForAssert(() -> assertThat(events.size(), is(1)));
    List<Event> changes = events.stream().filter(it -> it instanceof GroupItemStateChangedEvent).collect(Collectors.toList());
    GroupItemStateChangedEvent change = (GroupItemStateChangedEvent) changes.get(0);
    assertTrue(change.getItemName().equals(groupItem.getName()));
    State newEventState = change.getItemState();
    assertTrue(newEventState instanceof PercentType);
    assertThat(((PercentType) newEventState).intValue(), is(50));
    State newGroupState = groupItem.getState();
    assertTrue(newGroupState instanceof PercentType);
    assertThat(((PercentType) newGroupState).intValue(), is(50));
    events.clear();
    member2.setState(new PercentType(10));
    waitForAssert(() -> assertThat(events.size(), is(1)));
    changes = events.stream().filter(it -> it instanceof GroupItemStateChangedEvent).collect(Collectors.toList());
    assertThat(changes.size(), is(1));
    change = (GroupItemStateChangedEvent) changes.get(0);
    assertTrue(change.getItemName().equals(groupItem.getName()));
    newEventState = change.getItemState();
    assertTrue(newEventState instanceof PercentType);
    assertThat(((PercentType) newEventState).intValue(), is(30));
    newGroupState = groupItem.getState();
    assertTrue(newGroupState instanceof PercentType);
    assertThat(((PercentType) newGroupState).intValue(), is(30));
}
Also used : SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) CoreMatchers(org.hamcrest.CoreMatchers) MockitoAnnotations.initMocks(org.mockito.MockitoAnnotations.initMocks) RollershutterItem(org.eclipse.smarthome.core.library.items.RollershutterItem) NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) Mock(org.mockito.Mock) OnOffType(org.eclipse.smarthome.core.library.types.OnOffType) UnDefType(org.eclipse.smarthome.core.types.UnDefType) EventFilter(org.eclipse.smarthome.core.events.EventFilter) EventSubscriber(org.eclipse.smarthome.core.events.EventSubscriber) GroupFunctionDTO(org.eclipse.smarthome.core.items.dto.GroupFunctionDTO) GroupFunctionHelper(org.eclipse.smarthome.core.internal.items.GroupFunctionHelper) HashSet(java.util.HashSet) ColorItem(org.eclipse.smarthome.core.library.items.ColorItem) GroupItemStateChangedEvent(org.eclipse.smarthome.core.items.events.GroupItemStateChangedEvent) Temperature(javax.measure.quantity.Temperature) Quantity(javax.measure.Quantity) Pressure(javax.measure.quantity.Pressure) ItemStateConverterImpl(org.eclipse.smarthome.core.internal.items.ItemStateConverterImpl) QuantityType(org.eclipse.smarthome.core.library.types.QuantityType) CoreItemFactory(org.eclipse.smarthome.core.library.CoreItemFactory) StringType(org.eclipse.smarthome.core.library.types.StringType) LinkedList(java.util.LinkedList) State(org.eclipse.smarthome.core.types.State) Dimensionless(javax.measure.quantity.Dimensionless) Before(org.junit.Before) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) RefreshType(org.eclipse.smarthome.core.types.RefreshType) Set(java.util.Set) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) ItemUpdatedEvent(org.eclipse.smarthome.core.items.events.ItemUpdatedEvent) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) Collectors(java.util.stream.Collectors) RawType(org.eclipse.smarthome.core.library.types.RawType) IsCollectionWithSize.hasSize(org.hamcrest.collection.IsCollectionWithSize.hasSize) List(java.util.List) HSBType(org.eclipse.smarthome.core.library.types.HSBType) Ignore(org.junit.Ignore) PercentType(org.eclipse.smarthome.core.library.types.PercentType) Units(tec.uom.se.unit.Units) ArithmeticGroupFunction(org.eclipse.smarthome.core.library.types.ArithmeticGroupFunction) UnitProvider(org.eclipse.smarthome.core.i18n.UnitProvider) DimmerItem(org.eclipse.smarthome.core.library.items.DimmerItem) Assert(org.junit.Assert) Event(org.eclipse.smarthome.core.events.Event) Collections(java.util.Collections) GroupItemStateChangedEvent(org.eclipse.smarthome.core.items.events.GroupItemStateChangedEvent) State(org.eclipse.smarthome.core.types.State) DimmerItem(org.eclipse.smarthome.core.library.items.DimmerItem) GroupItemStateChangedEvent(org.eclipse.smarthome.core.items.events.GroupItemStateChangedEvent) ItemUpdatedEvent(org.eclipse.smarthome.core.items.events.ItemUpdatedEvent) Event(org.eclipse.smarthome.core.events.Event) PercentType(org.eclipse.smarthome.core.library.types.PercentType) ArithmeticGroupFunction(org.eclipse.smarthome.core.library.types.ArithmeticGroupFunction) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Aggregations

Event (org.eclipse.smarthome.core.events.Event)30 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)19 Test (org.junit.Test)19 EventSubscriber (org.eclipse.smarthome.core.events.EventSubscriber)15 EventPublisher (org.eclipse.smarthome.core.events.EventPublisher)14 Set (java.util.Set)13 EventFilter (org.eclipse.smarthome.core.events.EventFilter)13 LinkedList (java.util.LinkedList)11 Collections (java.util.Collections)9 HashSet (java.util.HashSet)9 List (java.util.List)8 Collectors (java.util.stream.Collectors)8 Ignore (org.junit.Ignore)8 GroupFunctionHelper (org.eclipse.smarthome.core.internal.items.GroupFunctionHelper)7 ItemStateConverterImpl (org.eclipse.smarthome.core.internal.items.ItemStateConverterImpl)7 GroupItemStateChangedEvent (org.eclipse.smarthome.core.items.events.GroupItemStateChangedEvent)7 ItemUpdatedEvent (org.eclipse.smarthome.core.items.events.ItemUpdatedEvent)7 SwitchItem (org.eclipse.smarthome.core.library.items.SwitchItem)7 OnOffType (org.eclipse.smarthome.core.library.types.OnOffType)7 StringType (org.eclipse.smarthome.core.library.types.StringType)7