]> git.basschouten.com Git - openhab-addons.git/blob
776c2ed6a2e6b048091e61a3fd515fdbc1c15d28
[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.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.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,
49                 Collections.singletonList(subject.toShortTopic()));
50
51         Collection<HaID> restoreList = HaID.fromConfig(haConfig);
52         assertThat(restoreList, hasItem(new HaID("homeassistant/switch/name/config")));
53     }
54
55     @Test
56     public void testWithNode() {
57         HaID subject = new HaID("homeassistant/switch/node/name/config");
58
59         assertThat(subject.objectID, is("name"));
60
61         assertThat(subject.component, is("switch"));
62         assertThat(subject.getTopic("suffix"), is("homeassistant/switch/node/name/suffix"));
63
64         Configuration config = new Configuration();
65         subject.toConfig(config);
66
67         HaID restore = HaID.fromConfig("homeassistant", config);
68
69         assertThat(restore, is(subject));
70
71         HandlerConfiguration haConfig = new HandlerConfiguration(subject.baseTopic,
72                 Collections.singletonList(subject.toShortTopic()));
73
74         Collection<HaID> restoreList = HaID.fromConfig(haConfig);
75         assertThat(restoreList, hasItem(new HaID("homeassistant/switch/node/name/config")));
76     }
77 }