|
19 | 19 | import static org.mockito.Mockito.*; |
20 | 20 | import static org.openhab.binding.mqtt.generic.internal.handler.ThingChannelConstants.*; |
21 | 21 |
|
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
22 | 24 | import java.util.concurrent.CompletableFuture; |
23 | 25 |
|
24 | 26 | import org.eclipse.jdt.annotation.NonNullByDefault; |
|
42 | 44 | import org.openhab.core.config.core.Configuration; |
43 | 45 | import org.openhab.core.io.transport.mqtt.MqttBrokerConnection; |
44 | 46 | import org.openhab.core.library.types.StringType; |
| 47 | +import org.openhab.core.thing.Channel; |
45 | 48 | import org.openhab.core.thing.ChannelUID; |
46 | 49 | import org.openhab.core.thing.Thing; |
47 | 50 | import org.openhab.core.thing.ThingStatus; |
48 | 51 | import org.openhab.core.thing.ThingStatusDetail; |
49 | 52 | import org.openhab.core.thing.ThingStatusInfo; |
50 | 53 | import org.openhab.core.thing.binding.ThingHandlerCallback; |
| 54 | +import org.openhab.core.thing.binding.builder.ChannelBuilder; |
| 55 | +import org.openhab.core.thing.type.ChannelKind; |
51 | 56 | import org.openhab.core.types.RefreshType; |
52 | 57 | import org.openhab.core.types.UnDefType; |
53 | 58 |
|
@@ -125,6 +130,55 @@ public void initialize() { |
125 | 130 | && ThingStatusDetail.NONE.equals(arg.getStatusDetail()))); |
126 | 131 | } |
127 | 132 |
|
| 133 | + @Test |
| 134 | + public void initializeWithStateOnlyChannel() { |
| 135 | + Channel channel = cb("stateOnly", "String", new Configuration(Map.of("stateTopic", "test/state")), |
| 136 | + TEXT_CHANNEL); |
| 137 | + when(thingMock.getChannels()).thenReturn(List.of(channel)); |
| 138 | + |
| 139 | + thingHandler.initialize(); |
| 140 | + |
| 141 | + assertThat(thingHandler.channelStateByChannelUID.containsKey(channel.getUID()), is(true)); |
| 142 | + verify(connectionMock).subscribe(eq("test/state"), any()); |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + public void initializeWithCommandOnlyChannel() { |
| 147 | + Channel channel = cb("commandOnly", "String", new Configuration(Map.of("commandTopic", "test/command")), |
| 148 | + TEXT_CHANNEL); |
| 149 | + when(thingMock.getChannels()).thenReturn(List.of(channel)); |
| 150 | + |
| 151 | + thingHandler.initialize(); |
| 152 | + |
| 153 | + assertThat(thingHandler.channelStateByChannelUID.containsKey(channel.getUID()), is(true)); |
| 154 | + verify(connectionMock, never()).subscribe(any(), any()); |
| 155 | + } |
| 156 | + |
| 157 | + @Test |
| 158 | + public void initializeRejectsChannelWithUnknownPropertiesInsteadOfTopics() { |
| 159 | + Channel channel = cb("invalid", "String", |
| 160 | + new Configuration(Map.of("status", "test/state", "trans", "JSONPATH:$.value")), TEXT_CHANNEL); |
| 161 | + |
| 162 | + assertConfigurationError(channel); |
| 163 | + } |
| 164 | + |
| 165 | + @Test |
| 166 | + public void initializeRejectsTypedTriggerWithoutStateTopic() { |
| 167 | + Channel channel = cb("invalidTypedTrigger", "String", |
| 168 | + new Configuration(Map.of("commandTopic", "test/command", "trigger", true)), TEXT_CHANNEL); |
| 169 | + |
| 170 | + assertConfigurationError(channel); |
| 171 | + } |
| 172 | + |
| 173 | + @Test |
| 174 | + public void initializeRejectsTriggerWithoutStateTopic() { |
| 175 | + ChannelUID channelUID = new ChannelUID(TEST_GENERIC_THING, "invalidTrigger"); |
| 176 | + Channel channel = ChannelBuilder.create(channelUID).withType(TRIGGER_CHANNEL).withKind(ChannelKind.TRIGGER) |
| 177 | + .withConfiguration(new Configuration(Map.of("commandTopic", "test/command"))).build(); |
| 178 | + |
| 179 | + assertConfigurationError(channel); |
| 180 | + } |
| 181 | + |
128 | 182 | @Test |
129 | 183 | public void handleCommandRefresh() { |
130 | 184 | TextValue value = spy(new TextValue()); |
@@ -206,4 +260,17 @@ public void handleBridgeStatusChange() { |
206 | 260 | thingHandler.bridgeStatusChanged(new ThingStatusInfo(ThingStatus.ONLINE, ThingStatusDetail.NONE, null)); |
207 | 261 | verify(connectionMock, times(2)).subscribe(eq("test/LWT"), any()); |
208 | 262 | } |
| 263 | + |
| 264 | + private void assertConfigurationError(Channel channel) { |
| 265 | + when(thingMock.getChannels()).thenReturn(List.of(channel)); |
| 266 | + |
| 267 | + thingHandler.initialize(); |
| 268 | + |
| 269 | + assertThat(thingHandler.channelStateByChannelUID.containsKey(channel.getUID()), is(false)); |
| 270 | + verify(thingHandler, never()).start(any()); |
| 271 | + verify(callbackMock).statusUpdated(eq(thingMock), |
| 272 | + argThat(arg -> ThingStatus.OFFLINE.equals(arg.getStatus()) |
| 273 | + && ThingStatusDetail.CONFIGURATION_ERROR.equals(arg.getStatusDetail()) |
| 274 | + && ("Invalid channel configuration: " + channel.getUID()).equals(arg.getDescription()))); |
| 275 | + } |
209 | 276 | } |
0 commit comments