From c4355a2a0356028ad95c4ff2570e611676a31dd4 Mon Sep 17 00:00:00 2001 From: Sami Salonen Date: Tue, 28 Jul 2020 20:46:46 +0300 Subject: [PATCH 1/2] [modbus] Correct exception type in implementation. Interface remains the same. Signed-off-by: Sami Salonen --- .../internal/handler/AbstractModbusEndpointThingHandler.java | 3 ++- .../modbus/internal/handler/ModbusSerialThingHandler.java | 5 +++-- .../modbus/internal/handler/ModbusTcpThingHandler.java | 5 +++-- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/bundles/org.openhab.binding.modbus/src/main/java/org/openhab/binding/modbus/internal/handler/AbstractModbusEndpointThingHandler.java b/bundles/org.openhab.binding.modbus/src/main/java/org/openhab/binding/modbus/internal/handler/AbstractModbusEndpointThingHandler.java index 48d437d59a89d..3f9d94024f8a6 100644 --- a/bundles/org.openhab.binding.modbus/src/main/java/org/openhab/binding/modbus/internal/handler/AbstractModbusEndpointThingHandler.java +++ b/bundles/org.openhab.binding.modbus/src/main/java/org/openhab/binding/modbus/internal/handler/AbstractModbusEndpointThingHandler.java @@ -20,6 +20,7 @@ import org.eclipse.smarthome.core.thing.ThingStatusDetail; import org.eclipse.smarthome.core.thing.binding.BaseBridgeHandler; import org.eclipse.smarthome.core.types.Command; +import org.openhab.binding.modbus.handler.EndpointNotInitializedException; import org.openhab.binding.modbus.handler.ModbusEndpointThingHandler; import org.openhab.binding.modbus.internal.ModbusConfigurationException; import org.openhab.io.transport.modbus.ModbusCommunicationInterface; @@ -115,7 +116,7 @@ public E getEndpoint() { } @Override - public abstract int getSlaveId(); + public abstract int getSlaveId() throws EndpointNotInitializedException; /** * Must be overriden by subclasses to initialize config, endpoint, and poolConfiguration diff --git a/bundles/org.openhab.binding.modbus/src/main/java/org/openhab/binding/modbus/internal/handler/ModbusSerialThingHandler.java b/bundles/org.openhab.binding.modbus/src/main/java/org/openhab/binding/modbus/internal/handler/ModbusSerialThingHandler.java index 1b2d7dfbd86f3..f07ebe1d9f098 100644 --- a/bundles/org.openhab.binding.modbus/src/main/java/org/openhab/binding/modbus/internal/handler/ModbusSerialThingHandler.java +++ b/bundles/org.openhab.binding.modbus/src/main/java/org/openhab/binding/modbus/internal/handler/ModbusSerialThingHandler.java @@ -21,6 +21,7 @@ import org.eclipse.smarthome.core.thing.ThingUID; import org.eclipse.smarthome.core.thing.binding.ThingHandlerService; import org.openhab.binding.modbus.discovery.internal.ModbusEndpointDiscoveryService; +import org.openhab.binding.modbus.handler.EndpointNotInitializedException; import org.openhab.binding.modbus.internal.ModbusConfigurationException; import org.openhab.binding.modbus.internal.config.ModbusSerialConfiguration; import org.openhab.io.transport.modbus.ModbusManager; @@ -94,10 +95,10 @@ protected String formatConflictingParameterError() { } @Override - public int getSlaveId() { + public int getSlaveId() throws EndpointNotInitializedException { ModbusSerialConfiguration config = this.config; if (config == null) { - throw new IllegalStateException("Poller not configured, but slave id is queried!"); + throw new EndpointNotInitializedException(); } return config.getId(); } diff --git a/bundles/org.openhab.binding.modbus/src/main/java/org/openhab/binding/modbus/internal/handler/ModbusTcpThingHandler.java b/bundles/org.openhab.binding.modbus/src/main/java/org/openhab/binding/modbus/internal/handler/ModbusTcpThingHandler.java index 9f38887116e95..24d5b07e5b568 100644 --- a/bundles/org.openhab.binding.modbus/src/main/java/org/openhab/binding/modbus/internal/handler/ModbusTcpThingHandler.java +++ b/bundles/org.openhab.binding.modbus/src/main/java/org/openhab/binding/modbus/internal/handler/ModbusTcpThingHandler.java @@ -21,6 +21,7 @@ import org.eclipse.smarthome.core.thing.ThingUID; import org.eclipse.smarthome.core.thing.binding.ThingHandlerService; import org.openhab.binding.modbus.discovery.internal.ModbusEndpointDiscoveryService; +import org.openhab.binding.modbus.handler.EndpointNotInitializedException; import org.openhab.binding.modbus.internal.ModbusConfigurationException; import org.openhab.binding.modbus.internal.config.ModbusTcpConfiguration; import org.openhab.io.transport.modbus.ModbusManager; @@ -72,10 +73,10 @@ protected String formatConflictingParameterError() { } @Override - public int getSlaveId() { + public int getSlaveId() throws EndpointNotInitializedException { ModbusTcpConfiguration localConfig = config; if (localConfig == null) { - throw new IllegalStateException("Poller not configured, but slave id is queried!"); + throw new EndpointNotInitializedException(); } return localConfig.getId(); } From b0359c338d991ce6c8af21782210a3f58561a194 Mon Sep 17 00:00:00 2001 From: Sami Salonen Date: Tue, 28 Jul 2020 21:31:33 +0300 Subject: [PATCH 2/2] [modbus] fixes for tests Signed-off-by: Sami Salonen --- .../binding/modbus/tests/ModbusDataHandlerTest.java | 8 +++++++- .../binding/modbus/tests/ModbusTcpThingHandlerTest.java | 3 ++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/itests/org.openhab.binding.modbus.tests/src/main/java/org/openhab/binding/modbus/tests/ModbusDataHandlerTest.java b/itests/org.openhab.binding.modbus.tests/src/main/java/org/openhab/binding/modbus/tests/ModbusDataHandlerTest.java index 7b3e4cab07f85..db2ff85c78349 100644 --- a/itests/org.openhab.binding.modbus.tests/src/main/java/org/openhab/binding/modbus/tests/ModbusDataHandlerTest.java +++ b/itests/org.openhab.binding.modbus.tests/src/main/java/org/openhab/binding/modbus/tests/ModbusDataHandlerTest.java @@ -61,6 +61,7 @@ import org.junit.runner.RunWith; import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; +import org.openhab.binding.modbus.handler.EndpointNotInitializedException; import org.openhab.binding.modbus.handler.ModbusPollerThingHandler; import org.openhab.binding.modbus.internal.handler.ModbusDataThingHandler; import org.openhab.binding.modbus.internal.handler.ModbusTcpThingHandler; @@ -163,7 +164,12 @@ private Bridge createTcpMock() { tcpBridge.setStatusInfo(new ThingStatusInfo(ThingStatus.ONLINE, ThingStatusDetail.NONE, "")); tcpBridge.setHandler(tcpThingHandler); doReturn(comms).when(tcpThingHandler).getCommunicationInterface(); - doReturn(0).when(tcpThingHandler).getSlaveId(); + try { + doReturn(0).when(tcpThingHandler).getSlaveId(); + } catch (EndpointNotInitializedException e) { + // not raised -- we are mocking return value only, not actually calling the method + throw new IllegalStateException(); + } tcpThingHandler.initialize(); assertThat(tcpBridge.getStatus(), is(equalTo(ThingStatus.ONLINE))); return tcpBridge; diff --git a/itests/org.openhab.binding.modbus.tests/src/main/java/org/openhab/binding/modbus/tests/ModbusTcpThingHandlerTest.java b/itests/org.openhab.binding.modbus.tests/src/main/java/org/openhab/binding/modbus/tests/ModbusTcpThingHandlerTest.java index e93dfaf46ec1d..84c2ed8d22a79 100644 --- a/itests/org.openhab.binding.modbus.tests/src/main/java/org/openhab/binding/modbus/tests/ModbusTcpThingHandlerTest.java +++ b/itests/org.openhab.binding.modbus.tests/src/main/java/org/openhab/binding/modbus/tests/ModbusTcpThingHandlerTest.java @@ -28,6 +28,7 @@ import org.mockito.InOrder; import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; +import org.openhab.binding.modbus.handler.EndpointNotInitializedException; import org.openhab.binding.modbus.internal.ModbusBindingConstantsInternal; import org.openhab.binding.modbus.internal.handler.ModbusTcpThingHandler; import org.openhab.io.transport.modbus.endpoint.EndpointPoolConfiguration; @@ -46,7 +47,7 @@ private static BridgeBuilder createTcpThingBuilder(String id) { } @Test - public void testInitializeAndSlaveEndpoint() { + public void testInitializeAndSlaveEndpoint() throws EndpointNotInitializedException { Configuration thingConfig = new Configuration(); thingConfig.put("host", "thisishost"); thingConfig.put("port", 44);