]> git.basschouten.com Git - openhab-addons.git/blob
baf73902ae4916d1d3398076949dcd70cb5e51c2
[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.io.hueemulation.internal.rest;
14
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.mockito.Mockito.mock;
18
19 import java.io.IOException;
20 import java.util.Collections;
21 import java.util.concurrent.ScheduledExecutorService;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.glassfish.jersey.server.ResourceConfig;
25 import org.hamcrest.CoreMatchers;
26 import org.junit.jupiter.api.AfterEach;
27 import org.junit.jupiter.api.BeforeEach;
28 import org.junit.jupiter.api.Test;
29 import org.openhab.core.items.ItemRegistry;
30 import org.openhab.core.items.Metadata;
31 import org.openhab.core.items.MetadataKey;
32 import org.openhab.core.library.items.SwitchItem;
33 import org.openhab.io.hueemulation.internal.ConfigStore;
34 import org.openhab.io.hueemulation.internal.dto.HueLightEntry;
35 import org.openhab.io.hueemulation.internal.dto.HueStatePlug;
36 import org.openhab.io.hueemulation.internal.rest.mocks.DummyItemRegistry;
37
38 /**
39  * Tests for the metadata provided hue ID mapping
40  *
41  * @author David Graeff - Initial contribution
42  */
43 @NonNullByDefault
44 public class ItemUIDtoHueIDMappingTests {
45     protected @NonNullByDefault({}) CommonSetup commonSetup;
46     protected @NonNullByDefault({}) ItemRegistry itemRegistry;
47
48     LightsAndGroups lightsAndGroups = new LightsAndGroups();
49
50     @BeforeEach
51     public void setUp() throws IOException {
52         commonSetup = new CommonSetup(true);
53         commonSetup.start(new ResourceConfig());
54
55         itemRegistry = new DummyItemRegistry();
56
57         lightsAndGroups.cs = commonSetup.cs;
58         lightsAndGroups.eventPublisher = commonSetup.eventPublisher;
59         lightsAndGroups.userManagement = commonSetup.userManagement;
60         lightsAndGroups.itemRegistry = itemRegistry;
61         lightsAndGroups.activate();
62     }
63
64     @AfterEach
65     public void tearDown() throws Exception {
66         commonSetup.dispose();
67     }
68
69     @Test
70     public void determineHighestHueID() {
71         ConfigStore cs = new ConfigStore(commonSetup.networkAddressService, commonSetup.configAdmin,
72                 commonSetup.metadataRegistry, mock(ScheduledExecutorService.class));
73
74         // Pretend there is a metadata entry for the imaginary item "demo1" with hueid 10
75         commonSetup.metadataRegistry.add(new Metadata(new MetadataKey(ConfigStore.METAKEY, "demo1"), "10", null));
76         cs.activate(Collections.singletonMap("uuid", "demouuid"));
77
78         assertThat(cs.getHighestAssignedHueID(), CoreMatchers.is(10));
79     }
80
81     @Test
82     public void mapItemWithoutHueID() {
83         ConfigStore cs = commonSetup.cs;
84         assertThat(cs.getHighestAssignedHueID(), CoreMatchers.is(1));
85
86         SwitchItem item = new SwitchItem("switch1");
87         item.setCategory("Light");
88         itemRegistry.add(item);
89
90         String hueID = cs.mapItemUIDtoHueID(item);
91         assertThat(hueID, CoreMatchers.is("2"));
92
93         HueLightEntry device = cs.ds.lights.get(hueID);
94         assertThat(device.item, is(item));
95         assertThat(device.state, is(instanceOf(HueStatePlug.class)));
96
97         assertThat(cs.getHighestAssignedHueID(), CoreMatchers.is(2));
98     }
99
100     @Test
101     public void mapItemWithHueID() {
102         ConfigStore cs = commonSetup.cs;
103         assertThat(cs.getHighestAssignedHueID(), CoreMatchers.is(1));
104
105         SwitchItem item = new SwitchItem("switch1");
106         item.setCategory("Light");
107         commonSetup.metadataRegistry.add(new Metadata(new MetadataKey(ConfigStore.METAKEY, "switch1"), "10", null));
108         itemRegistry.add(item);
109
110         String hueID = cs.mapItemUIDtoHueID(item);
111         assertThat(hueID, CoreMatchers.is("10"));
112
113         HueLightEntry device = cs.ds.lights.get(hueID);
114         assertThat(device.item, is(item));
115         assertThat(device.state, is(instanceOf(HueStatePlug.class)));
116
117         assertThat(cs.getHighestAssignedHueID(), CoreMatchers.is(1));
118     }
119
120     @Test
121     public void uniqueIdForLargeHueID() {
122         ConfigStore cs = commonSetup.cs;
123         assertThat(cs.getHighestAssignedHueID(), CoreMatchers.is(1));
124
125         SwitchItem item = new SwitchItem("switch1");
126         item.setCategory("Light");
127         commonSetup.metadataRegistry.add(new Metadata(new MetadataKey(ConfigStore.METAKEY, "switch1"), "255", null));
128         itemRegistry.add(item);
129
130         String hueID = cs.mapItemUIDtoHueID(item);
131         assertThat(hueID, CoreMatchers.is("255"));
132
133         HueLightEntry device = cs.ds.lights.get(hueID);
134         assertThat(device.item, is(item));
135         assertThat(device.state, is(instanceOf(HueStatePlug.class)));
136         assertThat(device.uniqueid, CoreMatchers.is("A6:68:DC:9B:71:72:00:00-FF"));
137
138         item = new SwitchItem("switch2");
139         item.setCategory("Light");
140         commonSetup.metadataRegistry.add(new Metadata(new MetadataKey(ConfigStore.METAKEY, "switch2"), "256000", null));
141         itemRegistry.add(item);
142
143         hueID = cs.mapItemUIDtoHueID(item);
144         assertThat(hueID, CoreMatchers.is("256000"));
145
146         device = cs.ds.lights.get(hueID);
147         assertThat(device.item, is(item));
148         assertThat(device.state, is(instanceOf(HueStatePlug.class)));
149         assertThat(device.uniqueid, CoreMatchers.is("A6:68:DC:9B:71:72:03:E8-00"));
150     }
151 }