]> git.basschouten.com Git - openhab-addons.git/blob
a6ad5d51970c057a0c5d9706e01da1d242456d3b
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.mqtt.ruuvigateway;
14
15 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
16
17 import java.util.HashMap;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.Objects;
21 import java.util.Set;
22 import java.util.concurrent.CopyOnWriteArrayList;
23
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;
35
36 /**
37  * Test utility capturing thing status updates
38  *
39  * @author Sami Salonen - Initial contribution
40  */
41 @NonNullByDefault
42 public class ThingStatusInfoChangedSubscriber implements EventSubscriber {
43
44     private final Logger logger = LoggerFactory.getLogger(ThingStatusInfoChangedSubscriber.class);
45
46     public Map<ThingUID, List<ThingStatusInfo>> statusUpdates = new HashMap<>();
47
48     @Override
49     public Set<@NonNull String> getSubscribedEventTypes() {
50         return Set.of(ThingStatusInfoChangedEvent.TYPE);
51     }
52
53     @Override
54     public @Nullable EventFilter getEventFilter() {
55         return null;
56     }
57
58     @Override
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());
68     }
69 }