diff --git a/bundles/org.openhab.core.io.websocket/src/test/java/org/openhab/core/io/websocket/EventWebSocketTest.java b/bundles/org.openhab.core.io.websocket/src/test/java/org/openhab/core/io/websocket/EventWebSocketTest.java index c045a671237..d700ad279b4 100644 --- a/bundles/org.openhab.core.io.websocket/src/test/java/org/openhab/core/io/websocket/EventWebSocketTest.java +++ b/bundles/org.openhab.core.io.websocket/src/test/java/org/openhab/core/io/websocket/EventWebSocketTest.java @@ -240,6 +240,31 @@ public void eventFromBusFilterSource() throws IOException { verify(remoteEndpoint, times(2)).sendString(any()); } + @Test + public void eventFromBusFilterTopic() throws IOException { + EventDTO eventDTO = new EventDTO(WEBSOCKET_EVENT_TYPE, WEBSOCKET_TOPIC_PREFIX + "filter/topic", + "[\"openhab/items/*/command\", \"openhab/items/*/statechanged\"]", null, null); + EventDTO responseEventDTO = new EventDTO(WEBSOCKET_EVENT_TYPE, WEBSOCKET_TOPIC_PREFIX + "filter/topic", + eventDTO.payload, null, null); + eventWebSocket.onText(gson.toJson(eventDTO)); + verify(remoteEndpoint).sendString(gson.toJson(responseEventDTO)); + + // subscribed topics are sent + Event event = ItemEventFactory.createCommandEvent(TEST_ITEM_NAME, DecimalType.ZERO, + REMOTE_WEBSOCKET_IMPLEMENTATION); + eventWebSocket.processEvent(event); + verify(remoteEndpoint).sendString(gson.toJson(new EventDTO(event))); + + event = ItemEventFactory.createStateChangedEvent(TEST_ITEM_NAME, DecimalType.ZERO, DecimalType.ZERO); + eventWebSocket.processEvent(event); + verify(remoteEndpoint).sendString(gson.toJson(new EventDTO(event))); + + // not subscribed event not sent + event = ItemEventFactory.createStateEvent(TEST_ITEM_NAME, DecimalType.ZERO, REMOTE_WEBSOCKET_IMPLEMENTATION); + eventWebSocket.processEvent(event); + verify(remoteEndpoint, times(3)).sendString(any()); + } + private void assertEventProcessing(EventDTO incoming, @Nullable Event expectedEvent, @Nullable EventDTO expectedResponse) throws IOException { eventWebSocket.onText(gson.toJson(incoming)); diff --git a/bundles/org.openhab.core/src/test/java/org/openhab/core/events/TopicEventFilterTest.java b/bundles/org.openhab.core/src/test/java/org/openhab/core/events/TopicEventFilterTest.java new file mode 100644 index 00000000000..8618e4a817e --- /dev/null +++ b/bundles/org.openhab.core/src/test/java/org/openhab/core/events/TopicEventFilterTest.java @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2010-2025 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.core.events; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.List; + +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.junit.jupiter.api.Test; + +/** + * {@link TopicEventFilterTest} tests the {@link TopicEventFilter}. + * + * @author Florian Hotze - Initial contribution + */ +@NonNullByDefault +public class TopicEventFilterTest { + public Event createEvent(String topic) { + Event event = mock(Event.class); + when(event.getTopic()).thenReturn(topic); + return event; + } + + @Test + public void testSingle() { + EventFilter filter = new TopicEventFilter("openhab/items/.*/.*"); + assertTrue(filter.apply(createEvent("openhab/items/test/command"))); + assertFalse(filter.apply(createEvent("somewhereElse"))); + assertFalse(filter.apply(createEvent("preopenhab/items/test/state"))); + + filter = new TopicEventFilter("openhab/items/test/command"); + assertTrue(filter.apply(createEvent("openhab/items/test/command"))); + assertFalse(filter.apply(createEvent("openhab/items/test/state"))); + } + + @Test + public void testMultiple() { + EventFilter filter = new TopicEventFilter(List.of("openhab/items/.*/.*", "openhab/things/.*/.*")); + assertTrue(filter.apply(createEvent("openhab/items/test/command"))); + assertTrue(filter.apply(createEvent("openhab/things/test/added"))); + assertFalse(filter.apply(createEvent("somewhereElse"))); + assertFalse(filter.apply(createEvent("preopenhab/items/test/command"))); + + filter = new TopicEventFilter(List.of("openhab/items/test/command", "openhab/things/test/added")); + assertTrue(filter.apply(createEvent("openhab/items/test/command"))); + assertTrue(filter.apply(createEvent("openhab/things/test/added"))); + assertFalse(filter.apply(createEvent("openhab/items/test/state"))); + assertFalse(filter.apply(createEvent("openhab/things/test/removed"))); + } +}