]> git.basschouten.com Git - openhab-addons.git/blob
9afa8b62b82b911fc884838d6f613bb1460225f2
[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.Collections;
18 import java.util.HashMap;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.Objects;
22 import java.util.Set;
23 import java.util.concurrent.CopyOnWriteArrayList;
24
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;
36
37 /**
38  * Test utility capturing thing status updates
39  *
40  * @author Sami Salonen - Initial contribution
41  */
42 @NonNullByDefault
43 public class ThingStatusInfoChangedSubscriber implements EventSubscriber {
44
45     private final Logger logger = LoggerFactory.getLogger(ThingStatusInfoChangedSubscriber.class);
46
47     public Map<ThingUID, List<ThingStatusInfo>> statusUpdates = new HashMap<>();
48
49     @Override
50     public Set<@NonNull String> getSubscribedEventTypes() {
51         return Collections.singleton(ThingStatusInfoChangedEvent.TYPE);
52     }
53
54     @Override
55     public @Nullable EventFilter getEventFilter() {
56         return null;
57     }
58
59     @Override
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());
69     }
70 }