Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[modbus] Correct exception type in implementation. #8218

Merged
merged 2 commits into from
Jul 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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