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.Collections;
18 import java.util.HashMap;
19 import java.util.List;
21 import java.util.Objects;
23 import java.util.concurrent.CopyOnWriteArrayList;
25 import org.eclipse.jdt.annotation.NonNull;
26 import org.eclipse.jdt.annotation.NonNullByDefault;
27 import org.eclipse.jdt.annotation.Nullable;
28 import org.openhab.core.events.Event;
29 import org.openhab.core.events.EventFilter;
30 import org.openhab.core.events.EventSubscriber;
31 import org.openhab.core.thing.ThingStatusInfo;
32 import org.openhab.core.thing.ThingUID;
33 import org.openhab.core.thing.events.ThingStatusInfoChangedEvent;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * Test utility capturing thing status updates
40 * @author Sami Salonen - Initial contribution
43 public class ThingStatusInfoChangedSubscriber implements EventSubscriber {
45 private final Logger logger = LoggerFactory.getLogger(ThingStatusInfoChangedSubscriber.class);
47 public Map<ThingUID, List<ThingStatusInfo>> statusUpdates = new HashMap<>();
50 public Set<@NonNull String> getSubscribedEventTypes() {
51 return Collections.singleton(ThingStatusInfoChangedEvent.TYPE);
55 public @Nullable EventFilter getEventFilter() {
60 public void receive(Event event) {
61 // Expecting only state updates in the tests
62 assertInstanceOf(ThingStatusInfoChangedEvent.class, event);
63 ThingStatusInfoChangedEvent statusEvent = (ThingStatusInfoChangedEvent) event;
64 logger.trace("Captured event: {} ", event);
65 List<ThingStatusInfo> updates = statusUpdates.computeIfAbsent(statusEvent.getThingUID(),
66 item -> new CopyOnWriteArrayList<>());
67 Objects.requireNonNull(updates); // To make compiler happy
68 updates.add(statusEvent.getStatusInfo());