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