2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.io.hueemulation.internal.rest;
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.mockito.Mockito.mock;
19 import java.io.IOException;
21 import java.util.concurrent.ScheduledExecutorService;
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;
39 * Tests for the metadata provided hue ID mapping
41 * @author David Graeff - Initial contribution
44 public class ItemUIDtoHueIDMappingTests {
45 protected @NonNullByDefault({}) CommonSetup commonSetup;
46 protected @NonNullByDefault({}) ItemRegistry itemRegistry;
48 LightsAndGroups lightsAndGroups = new LightsAndGroups();
51 public void setUp() throws IOException {
52 commonSetup = new CommonSetup(true);
53 commonSetup.start(new ResourceConfig());
55 itemRegistry = new DummyItemRegistry();
57 lightsAndGroups.cs = commonSetup.cs;
58 lightsAndGroups.eventPublisher = commonSetup.eventPublisher;
59 lightsAndGroups.userManagement = commonSetup.userManagement;
60 lightsAndGroups.itemRegistry = itemRegistry;
61 lightsAndGroups.activate();
65 public void tearDown() throws Exception {
66 commonSetup.dispose();
70 public void determineHighestHueID() {
71 ConfigStore cs = new ConfigStore(commonSetup.networkAddressService, commonSetup.configAdmin,
72 commonSetup.metadataRegistry, mock(ScheduledExecutorService.class));
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(Map.of("uuid", "demouuid"));
78 assertThat(cs.getHighestAssignedHueID(), CoreMatchers.is(10));
82 public void mapItemWithoutHueID() {
83 ConfigStore cs = commonSetup.cs;
84 assertThat(cs.getHighestAssignedHueID(), CoreMatchers.is(1));
86 SwitchItem item = new SwitchItem("switch1");
87 item.setCategory("Light");
88 itemRegistry.add(item);
90 String hueID = cs.mapItemUIDtoHueID(item);
91 assertThat(hueID, CoreMatchers.is("2"));
93 HueLightEntry device = cs.ds.lights.get(hueID);
94 assertThat(device.item, is(item));
95 assertThat(device.state, is(instanceOf(HueStatePlug.class)));
97 assertThat(cs.getHighestAssignedHueID(), CoreMatchers.is(2));
101 public void mapItemWithHueID() {
102 ConfigStore cs = commonSetup.cs;
103 assertThat(cs.getHighestAssignedHueID(), CoreMatchers.is(1));
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);
110 String hueID = cs.mapItemUIDtoHueID(item);
111 assertThat(hueID, CoreMatchers.is("10"));
113 HueLightEntry device = cs.ds.lights.get(hueID);
114 assertThat(device.item, is(item));
115 assertThat(device.state, is(instanceOf(HueStatePlug.class)));
117 assertThat(cs.getHighestAssignedHueID(), CoreMatchers.is(1));
121 public void uniqueIdForLargeHueID() {
122 ConfigStore cs = commonSetup.cs;
123 assertThat(cs.getHighestAssignedHueID(), CoreMatchers.is(1));
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);
130 String hueID = cs.mapItemUIDtoHueID(item);
131 assertThat(hueID, CoreMatchers.is("255"));
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"));
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);
143 hueID = cs.mapItemUIDtoHueID(item);
144 assertThat(hueID, CoreMatchers.is("256000"));
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"));