Skip to content

Commit

Permalink
[modbus] Correct exception type in implementation. (openhab#8218)
Browse files Browse the repository at this point in the history
Interface remains the same.

Signed-off-by: Sami Salonen <[email protected]>
  • Loading branch information
ssalonen authored and markus7017 committed Sep 18, 2020
1 parent 295fcee commit 244221c
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit 244221c

Please sign in to comment.