Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Florian Hotze <dev@florianhotze.com>
  • Loading branch information
florian-h05 committed Jan 9, 2025
1 parent 0ee3dd8 commit 636ba6f
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
@@ -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")));
}
}

0 comments on commit 636ba6f

Please sign in to comment.