forked from openhab/openhab-addons
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…hab#17206) * openhab#16940: Added check in handler for invalid configuration values. Signed-off-by: Sönke Küper <[email protected]>
- Loading branch information
1 parent
198b9b1
commit c7a2026
Showing
4 changed files
with
130 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
.../test/java/org/openhab/binding/homematic/internal/handler/HomematicBridgeHandlerMock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* Copyright (c) 2010-2024 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.binding.homematic.internal.handler; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.doAnswer; | ||
|
||
import java.util.concurrent.ScheduledExecutorService; | ||
|
||
import org.eclipse.jdt.annotation.NonNull; | ||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jetty.client.HttpClient; | ||
import org.mockito.Mockito; | ||
import org.mockito.invocation.InvocationOnMock; | ||
import org.openhab.binding.homematic.internal.type.HomematicTypeGenerator; | ||
import org.openhab.core.thing.Bridge; | ||
|
||
/** | ||
* The {@link HomematicBridgeHandlerMock} is responsible for mocking {@link HomematicBridgeHandler} | ||
* | ||
* @author Sönke Küper - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
public class HomematicBridgeHandlerMock extends HomematicBridgeHandler { | ||
|
||
public HomematicBridgeHandlerMock(@NonNull Bridge bridge, HomematicTypeGenerator typeGenerator, String ipv4Address, | ||
HttpClient httpClient) { | ||
super(bridge, typeGenerator, ipv4Address, httpClient); | ||
executorService = Mockito.mock(ScheduledExecutorService.class); | ||
doAnswer((InvocationOnMock invocation) -> { | ||
((Runnable) invocation.getArguments()[0]).run(); | ||
return null; | ||
}).when(executorService).submit(any(Runnable.class)); | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
.../test/java/org/openhab/binding/homematic/internal/handler/HomematicBridgeHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/** | ||
* Copyright (c) 2010-2024 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.binding.homematic.internal.handler; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.mockito.ArgumentMatchers.argThat; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
|
||
import java.util.Objects; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jetty.client.HttpClient; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
import org.openhab.binding.homematic.internal.HomematicBindingConstants; | ||
import org.openhab.binding.homematic.internal.type.HomematicTypeGenerator; | ||
import org.openhab.core.thing.Bridge; | ||
import org.openhab.core.thing.ThingStatus; | ||
import org.openhab.core.thing.ThingStatusDetail; | ||
import org.openhab.core.thing.binding.ThingHandlerCallback; | ||
import org.openhab.core.thing.internal.BridgeImpl; | ||
|
||
/** | ||
* @author Sönke Küper - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
public class HomematicBridgeHandlerTest { | ||
|
||
@Test | ||
public void testGetRpcCallbackUrlDoesNotContainsSpaces() { | ||
HttpClient httpClient = Mockito.mock(HttpClient.class); | ||
|
||
Bridge bridge = new BridgeImpl(HomematicBindingConstants.THING_TYPE_BRIDGE, "1234"); | ||
bridge.getConfiguration().put("callbackHost", " 192. 168.1.1"); | ||
assertThat(bridge.getStatus(), is(ThingStatus.UNINITIALIZED)); | ||
HomematicTypeGenerator typeGenerator = mock(HomematicTypeGenerator.class); | ||
|
||
HomematicBridgeHandlerMock handler = new HomematicBridgeHandlerMock(bridge, typeGenerator, "1.2.3.4", | ||
httpClient); | ||
ThingHandlerCallback callback = mock(ThingHandlerCallback.class); | ||
handler.setCallback(callback); | ||
handler.initialize(); | ||
|
||
try { | ||
verify(callback).statusUpdated(eq(bridge), argThat(arg -> arg.getStatus().equals(ThingStatus.OFFLINE) | ||
&& arg.getStatusDetail().equals(ThingStatusDetail.CONFIGURATION_ERROR) | ||
&& Objects.equals(arg.getDescription(), "The callback host mut not contain white spaces."))); | ||
} finally { | ||
handler.dispose(); | ||
} | ||
} | ||
} |