|
| 1 | +/* |
| 2 | + * Copyright (c) 2010-2025 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.core.thing.link; |
| 14 | + |
| 15 | +import static org.junit.jupiter.api.Assertions.*; |
| 16 | + |
| 17 | +import java.io.ByteArrayInputStream; |
| 18 | +import java.util.Set; |
| 19 | + |
| 20 | +import org.eclipse.jdt.annotation.NonNullByDefault; |
| 21 | +import org.junit.jupiter.api.BeforeEach; |
| 22 | +import org.junit.jupiter.api.Disabled; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.openhab.core.items.Item; |
| 25 | +import org.openhab.core.items.ItemRegistry; |
| 26 | +import org.openhab.core.model.core.ModelRepository; |
| 27 | +import org.openhab.core.test.java.JavaOSGiTest; |
| 28 | +import org.openhab.core.thing.ThingRegistry; |
| 29 | + |
| 30 | +/** |
| 31 | + * Tests for {@link ItemChannelLinkRegistry} tagging. |
| 32 | + * |
| 33 | + * @author Andrew Fiddian-Green - Initial contribution |
| 34 | + */ |
| 35 | +@NonNullByDefault |
| 36 | +public class ItemChannelLinkTaggingOSGiTest extends JavaOSGiTest { |
| 37 | + |
| 38 | + private static final String THINGS_MODEL_ID = "test.things"; |
| 39 | + private static final String ITEMS_MODEL_ID = "test.items"; |
| 40 | + |
| 41 | + private static final String THINGS_MODEL = """ |
| 42 | + Bridge hue:bridge-api2:bridge "Bridge" [ipAddress="123.123.123.123", applicationKey="0123456789ABCDEF"] { |
| 43 | + Thing device light "Light" [resourceId="11111111-1111-1111-1111-111111111111"] |
| 44 | + Thing device dimmer "Dimmer" [resourceId="22222222-2222-2222-2222-222222222222"] |
| 45 | + } |
| 46 | + """; |
| 47 | + |
| 48 | + private static final String ITEMS_MODEL = """ |
| 49 | + String Item_01 {channel="hue:device:bridge:light:color" } |
| 50 | + String Item_02 {channel="hue:device:bridge:light:color" [useTags=false] } |
| 51 | + String Item_03 {channel="hue:device:bridge:light:color" [useTags="false"] } |
| 52 | + String Item_04 "Control, Color" {channel="hue:device:bridge:light:color" [useTags=true] } |
| 53 | + String Item_05 "Control, Color" {channel="hue:device:bridge:light:color" [useTags="true"] } |
| 54 | + String Item_06 "Control Color, Custom" ["Custom"] {channel="hue:device:bridge:light:color" [useTags=true] } |
| 55 | + String Item_07 "'Control', Power, Custom" ["Power", "Custom"] {channel="hue:device:bridge:light:color" } |
| 56 | + String Item_08 "'Control', Power, Custom" ["Power", "Custom"] {channel="hue:device:bridge:light:color" [useTags=true] } |
| 57 | + String Item_09 "Switch, Custom" ["Switch", "Custom"] {channel="hue:device:bridge:light:color" } |
| 58 | + String Item_10 "Switch, Custom" ["Switch", "Custom"] {channel="hue:device:bridge:light:color" [useTags=true] } |
| 59 | + String Item_11 "Switch, Power, Custom" ["Switch", "Power", "Custom"] {channel="hue:device:bridge:light:color" } |
| 60 | + String Item_12 "Switch, Power, Custom" ["Switch", "Power", "Custom"] {channel="hue:device:bridge:light:color" [useTags=true] } |
| 61 | + String Item_13 "Alarm, LowBattery" {channel="hue:device:bridge:dimmer:battery-low" [useTags=true] } |
| 62 | + String Item_14 "Control, Color" {channel="hue:device:bridge:light:color" [useTags=true], channel="hue:device:bridge:dimmer:battery-low" [useTags=true] } |
| 63 | + String Item_15 "Alarm, LowBattery" {channel="hue:device:bridge:light:color" [useTags=false], channel="hue:device:bridge:dimmer:battery-low" [useTags=true] } |
| 64 | + """; |
| 65 | + |
| 66 | + private @NonNullByDefault({}) ItemRegistry itemRegistry; |
| 67 | + private @NonNullByDefault({}) ThingRegistry thingRegistry; |
| 68 | + private @NonNullByDefault({}) ModelRepository modelRepository; |
| 69 | + private @NonNullByDefault({}) ItemChannelLinkRegistry itemChannelLinkRegistry; |
| 70 | + |
| 71 | + @BeforeEach |
| 72 | + public void setup() { |
| 73 | + registerVolatileStorageService(); |
| 74 | + |
| 75 | + itemRegistry = getService(ItemRegistry.class); |
| 76 | + assertNotNull(itemRegistry); |
| 77 | + |
| 78 | + thingRegistry = getService(ThingRegistry.class); |
| 79 | + assertNotNull(thingRegistry); |
| 80 | + |
| 81 | + itemChannelLinkRegistry = getService(ItemChannelLinkRegistry.class); |
| 82 | + assertNotNull(itemChannelLinkRegistry); |
| 83 | + |
| 84 | + modelRepository = getService(ModelRepository.class); |
| 85 | + assertNotNull(modelRepository); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * TODO I wrote this to test the channel / item tagging but it fails with the stack trace below ("resource" is null) |
| 90 | + * and in spite of many hours trying I was unable to find a solution to make it work. So in the menatime the test is |
| 91 | + * currently disabled until some kind maintainer can help me to fix it. |
| 92 | + * |
| 93 | + * <pre> |
| 94 | + * {@code |
| 95 | + * TEST org.openhab.core.thing.link.ItemChannelLinkTaggingOSGiTest#assertTagsAreCorrect() <<< ERROR: Cannot invoke |
| 96 | + * "org.eclipse.emf.ecore.resource.Resource.load(java.io.InputStream, java.util.Map)" because "resource" is null |
| 97 | + * java.lang.NullPointerException: Cannot invoke "org.eclipse.emf.ecore.resource.Resource.load(java.io.InputStream, java.util.Map)" because "resource" is null |
| 98 | + * at org.openhab.core.model.core.internal.ModelRepositoryImpl.validateModel(ModelRepositoryImpl.java:280) |
| 99 | + * at org.openhab.core.model.core.internal.ModelRepositoryImpl.addOrRefreshModel(ModelRepositoryImpl.java:108) |
| 100 | + * at org.openhab.core.thing.link.ItemChannelLinkTaggingOSGiTest.assertTagsAreCorrect( ItemChannelLinkTaggingOSGiTest.java:107) |
| 101 | + * } |
| 102 | + * </pre> |
| 103 | + */ |
| 104 | + @Test |
| 105 | + @Disabled |
| 106 | + public void assertTagsAreCorrect() { |
| 107 | + modelRepository.addOrRefreshModel(THINGS_MODEL_ID, new ByteArrayInputStream(THINGS_MODEL.getBytes())); // <== !! |
| 108 | + waitForAssert(() -> { |
| 109 | + assertEquals(3, thingRegistry.getAll().size()); |
| 110 | + }); |
| 111 | + |
| 112 | + modelRepository.addOrRefreshModel(ITEMS_MODEL_ID, new ByteArrayInputStream(ITEMS_MODEL.getBytes())); // <== !! |
| 113 | + waitForAssert(() -> { |
| 114 | + assertEquals(15, itemRegistry.getAll().size()); |
| 115 | + assertEquals(17, itemChannelLinkRegistry.getAll().size()); |
| 116 | + }); |
| 117 | + |
| 118 | + Item item; |
| 119 | + |
| 120 | + item = itemRegistry.get("Item_01"); |
| 121 | + assertNotNull(item); |
| 122 | + assertTrue(item.getTags().isEmpty()); |
| 123 | + |
| 124 | + item = itemRegistry.get("Item_02"); |
| 125 | + assertNotNull(item); |
| 126 | + assertTrue(item.getTags().isEmpty()); |
| 127 | + |
| 128 | + item = itemRegistry.get("Item_03"); |
| 129 | + assertNotNull(item); |
| 130 | + assertTrue(item.getTags().isEmpty()); |
| 131 | + |
| 132 | + item = itemRegistry.get("Item_04"); |
| 133 | + assertNotNull(item); |
| 134 | + assertEquals(Set.of("Control", "Color"), item.getTags()); |
| 135 | + |
| 136 | + item = itemRegistry.get("Item_05"); |
| 137 | + assertNotNull(item); |
| 138 | + assertEquals(Set.of("Control", "Color"), item.getTags()); |
| 139 | + |
| 140 | + item = itemRegistry.get("Item_06"); |
| 141 | + assertNotNull(item); |
| 142 | + assertEquals(Set.of("Control", "Color", "Custom"), item.getTags()); |
| 143 | + |
| 144 | + item = itemRegistry.get("Item_07"); |
| 145 | + assertNotNull(item); |
| 146 | + assertTrue(item.getTags().contains("Power")); |
| 147 | + assertTrue(item.getTags().contains("Custom")); |
| 148 | + |
| 149 | + item = itemRegistry.get("Item_08"); |
| 150 | + assertNotNull(item); |
| 151 | + assertTrue(item.getTags().contains("Power")); |
| 152 | + assertTrue(item.getTags().contains("Custom")); |
| 153 | + |
| 154 | + item = itemRegistry.get("Item_09"); |
| 155 | + assertNotNull(item); |
| 156 | + assertEquals(Set.of("Switch", "Custom"), item.getTags()); |
| 157 | + |
| 158 | + item = itemRegistry.get("Item_10"); |
| 159 | + assertNotNull(item); |
| 160 | + assertEquals(Set.of("Switch", "Custom"), item.getTags()); |
| 161 | + |
| 162 | + item = itemRegistry.get("Item_11"); |
| 163 | + assertNotNull(item); |
| 164 | + assertEquals(Set.of("Switch", "Power", "Custom"), item.getTags()); |
| 165 | + |
| 166 | + item = itemRegistry.get("Item_12"); |
| 167 | + assertNotNull(item); |
| 168 | + assertEquals(Set.of("Switch", "Power", "Custom"), item.getTags()); |
| 169 | + |
| 170 | + item = itemRegistry.get("Item_13"); |
| 171 | + assertNotNull(item); |
| 172 | + assertEquals(Set.of("Alarm", "LowBattery"), item.getTags()); |
| 173 | + |
| 174 | + item = itemRegistry.get("Item_14"); |
| 175 | + assertNotNull(item); |
| 176 | + assertTrue(item.getTags().equals(Set.of("Control", "Color")) // |
| 177 | + || item.getTags().equals(Set.of("Alarm", "LowBattery"))); |
| 178 | + |
| 179 | + item = itemRegistry.get("Item_15"); |
| 180 | + assertNotNull(item); |
| 181 | + assertEquals(Set.of("Alarm", "LowBattery"), item.getTags()); |
| 182 | + } |
| 183 | +} |
0 commit comments