2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.mqtt.ruuvigateway;
15 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
17 import java.util.HashMap;
18 import java.util.List;
20 import java.util.Objects;
22 import java.util.concurrent.CopyOnWriteArrayList;
24 import org.eclipse.jdt.annotation.NonNull;
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.openhab.core.events.Event;
28 import org.openhab.core.events.EventFilter;
29 import org.openhab.core.events.EventSubscriber;
30 import org.openhab.core.thing.ThingStatusInfo;
31 import org.openhab.core.thing.ThingUID;
32 import org.openhab.core.thing.events.ThingStatusInfoChangedEvent;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * Test utility capturing thing status updates
39 * @author Sami Salonen - Initial contribution
42 public class ThingStatusInfoChangedSubscriber implements EventSubscriber {
44 private final Logger logger = LoggerFactory.getLogger(ThingStatusInfoChangedSubscriber.class);
46 public Map<ThingUID, List<ThingStatusInfo>> statusUpdates = new HashMap<>();
49 public Set<@NonNull String> getSubscribedEventTypes() {
50 return Set.of(ThingStatusInfoChangedEvent.TYPE);
54 public @Nullable EventFilter getEventFilter() {
59 public void receive(Event event) {
60 // Expecting only state updates in the tests
61 assertInstanceOf(ThingStatusInfoChangedEvent.class, event);
62 ThingStatusInfoChangedEvent statusEvent = (ThingStatusInfoChangedEvent) event;
63 logger.trace("Captured event: {} ", event);
64 List<ThingStatusInfo> updates = statusUpdates.computeIfAbsent(statusEvent.getThingUID(),
65 item -> new CopyOnWriteArrayList<>());
66 Objects.requireNonNull(updates); // To make compiler happy
67 updates.add(statusEvent.getStatusInfo());