]> git.basschouten.com Git - openhab-addons.git/blob
3265a40b6b02682d03417ac1e08e4de9bb1d5b87
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.homeassistant.internal;
14
15 import static org.hamcrest.CoreMatchers.is;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.hamcrest.core.IsIterableContaining.hasItem;
18
19 import java.util.Collection;
20 import java.util.List;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.junit.jupiter.api.Test;
24 import org.openhab.core.config.core.Configuration;
25
26 /**
27  * @author Jochen Klein - Initial contribution
28  */
29 @NonNullByDefault
30 public class HaIDTests {
31
32     @Test
33     public void testWithoutNode() {
34         HaID subject = new HaID("homeassistant/switch/name/config");
35
36         assertThat(subject.objectID, is("name"));
37
38         assertThat(subject.component, is("switch"));
39         assertThat(subject.getTopic("suffix"), is("homeassistant/switch/name/suffix"));
40
41         Configuration config = new Configuration();
42         subject.toConfig(config);
43
44         HaID restore = HaID.fromConfig("homeassistant", config);
45
46         assertThat(restore, is(subject));
47
48         HandlerConfiguration haConfig = new HandlerConfiguration(subject.baseTopic, List.of(subject.toShortTopic()));
49
50         Collection<HaID> restoreList = HaID.fromConfig(haConfig);
51         assertThat(restoreList, hasItem(new HaID("homeassistant/switch/name/config")));
52     }
53
54     @Test
55     public void testWithNode() {
56         HaID subject = new HaID("homeassistant/switch/node/name/config");
57
58         assertThat(subject.objectID, is("name"));
59
60         assertThat(subject.component, is("switch"));
61         assertThat(subject.getTopic("suffix"), is("homeassistant/switch/node/name/suffix"));
62
63         Configuration config = new Configuration();
64         subject.toConfig(config);
65
66         HaID restore = HaID.fromConfig("homeassistant", config);
67
68         assertThat(restore, is(subject));
69
70         HandlerConfiguration haConfig = new HandlerConfiguration(subject.baseTopic, List.of(subject.toShortTopic()));
71
72         Collection<HaID> restoreList = HaID.fromConfig(haConfig);
73         assertThat(restoreList, hasItem(new HaID("homeassistant/switch/node/name/config")));
74     }
75 }