AutomaticInboxProcessor extends AbstractTypedEventSubscriber<ThingStatusInfoChangedEvent> implements InboxListener, RegistryChangeListener<Thing> { @Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC) protected void addInboxAutoApprovePredicate(InboxAutoApprovePredicate inboxAutoApprovePredicate) { inboxAutoApprovePredicates.add(inboxAutoApprovePredicate); for (DiscoveryResult result : inbox.getAll()) { if (DiscoveryResultFlag.NEW.equals(result.getFlag()) && inboxAutoApprovePredicate.test(result)) { inbox.approve(result.getThingUID(), result.getLabel()); } } } @Activate  AutomaticInboxProcessor(final @Reference ThingTypeRegistry thingTypeRegistry,
            final @Reference ThingRegistry thingRegistry, final @Reference Inbox inbox); @Override void receiveTypedEvent(ThingStatusInfoChangedEvent event); @Override void thingAdded(Inbox inbox, DiscoveryResult result); @Override void thingUpdated(Inbox inbox, DiscoveryResult result); @Override void thingRemoved(Inbox inbox, DiscoveryResult result); @Override void added(Thing element); @Override void removed(Thing element); @Override void updated(Thing oldElement, Thing element); static final String AUTO_IGNORE_CONFIG_PROPERTY; static final String ALWAYS_AUTO_APPROVE_CONFIG_PROPERTY; }
@Test public void testAutomaticDiscoveryResultApprovalIfInboxEntriesAddedAfterApprovalPredicatesAreAdded() { automaticInboxProcessor.addInboxAutoApprovePredicate( discoveryResult -> THING_TYPE_UID.equals(discoveryResult.getThingTypeUID())); inbox.add(DiscoveryResultBuilder.create(THING_UID).build()); verify(thingRegistry, times(1)).add(argThat(thing -> THING_UID.equals(thing.getUID()))); inbox.add(DiscoveryResultBuilder.create(THING_UID3).build()); verify(thingRegistry, never()).add(argThat(thing -> THING_UID3.equals(thing.getUID()))); }
@Test public void testAutomaticDiscoveryResultApprovalIfInboxEntriesExistBeforeApprovalPredicatesAreAdded() { inbox.add(DiscoveryResultBuilder.create(THING_UID).build()); inbox.add(DiscoveryResultBuilder.create(THING_UID2).build()); inbox.add(DiscoveryResultBuilder.create(THING_UID3).build()); automaticInboxProcessor.addInboxAutoApprovePredicate( discoveryResult -> THING_TYPE_UID.equals(discoveryResult.getThingTypeUID())); verify(thingRegistry, times(1)).add(argThat(thing -> THING_UID.equals(thing.getUID()))); verify(thingRegistry, times(1)).add(argThat(thing -> THING_UID2.equals(thing.getUID()))); verify(thingRegistry, never()).add(argThat(thing -> THING_UID3.equals(thing.getUID()))); automaticInboxProcessor.addInboxAutoApprovePredicate( discoveryResult -> THING_TYPE_UID3.equals(discoveryResult.getThingTypeUID())); verify(thingRegistry, times(1)).add(argThat(thing -> THING_UID.equals(thing.getUID()))); verify(thingRegistry, times(1)).add(argThat(thing -> THING_UID2.equals(thing.getUID()))); verify(thingRegistry, times(1)).add(argThat(thing -> THING_UID3.equals(thing.getUID()))); }
@Test @Disabled("Should this test pass? It will fail currently, as RuntimeExceptions are not explicitly caught in AutomaticInboxProcessor#isToBeAutoApproved") public void testRogueInboxAutoApprovePredicatesDoNoHarm() { automaticInboxProcessor.addInboxAutoApprovePredicate(discoveryResult -> { throw new RuntimeException("I am an evil inboxAutoApprovePredicate"); }); automaticInboxProcessor.addInboxAutoApprovePredicate( discoveryResult -> THING_TYPE_UID.equals(discoveryResult.getThingTypeUID())); automaticInboxProcessor.addInboxAutoApprovePredicate(discoveryResult -> { throw new RuntimeException("I am another evil inboxAutoApprovePredicate"); }); inbox.add(DiscoveryResultBuilder.create(THING_UID).build()); verify(thingRegistry, times(1)).add(argThat(thing -> THING_UID.equals(thing.getUID()))); }