Skip to content

Commit

Permalink
added TestCase to cover General Exception
Browse files Browse the repository at this point in the history
Trigger IllegalArgumentException by adding a new Channel, and filling it with Integer.MAX value (not fitting in short range), catch the log and check for "IllegalArgumentException"
-> For me : easiest way to catch the logError
  • Loading branch information
DerStoecki committed Nov 20, 2024
1 parent 8bfaf50 commit 0547606
Showing 1 changed file with 58 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.openems.edge.bridge.modbus;

import static io.openems.edge.bridge.modbus.api.ModbusComponent.ChannelId.MODBUS_COMMUNICATION_FAILED;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

Expand All @@ -24,6 +25,9 @@
import io.openems.edge.common.test.ComponentTest;
import io.openems.edge.common.test.TestUtils;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

public class BridgeModbusTcpImplTest {

private static final int UNIT_ID = 1;
Expand All @@ -43,6 +47,8 @@ public void test() throws Exception {
var processImage = new SimpleProcessImage(UNIT_ID);
Register register100 = new SimpleRegister(123);
processImage.addRegister(100, register100);
Register register101 = new SimpleRegister(321);
processImage.addRegister(101, register101);
slave.addProcessImage(UNIT_ID, processImage);
slave.open();

Expand Down Expand Up @@ -91,15 +97,63 @@ public void test() throws Exception {
}
}

@Test
public void testTriggerLogIllegalArgumentException() throws Exception {
final ThrowingRunnable<Exception> sleep = () -> Thread.sleep(CYCLE_TIME);
var port = TestUtils.findRandomOpenPortOnAllLocalInterfaces();
ModbusSlave slave = null;
PrintStream originalOut = System.out;
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
try {
/*
* Open Modbus/TCP Slave
*/
slave = ModbusSlaveFactory.createTCPSlave(port, 1);
var processImage = new SimpleProcessImage(UNIT_ID);
Register register100 = new SimpleRegister(123);
Register register101 = new SimpleRegister(Integer.MAX_VALUE); // this will cause the IllegalArgumentException
processImage.addRegister(100, register100);
processImage.addRegister(101, register101);
slave.addProcessImage(UNIT_ID, processImage);
slave.open();
System.setOut(new PrintStream(outContent));
/*
* Instantiate Modbus-Bridge
*/
var sut = new BridgeModbusTcpImpl();
var test = new ComponentTest(sut) //
.activate(MyConfigTcp.create() //
.setId("modbus0") //
.setIp("127.0.0.1") //
.setPort(port) //
.setInvalidateElementsAfterReadErrors(1) //
.setLogVerbosity(LogVerbosity.NONE) //
.build());
test.addComponent(new MyModbusComponent("device0", sut, UNIT_ID));
test //
.next(new TestCase() //
.onAfterProcessImage(sleep)); //
assertTrue(outContent.toString().contains("IllegalArgumentException"));

} finally {
if (slave != null) {
slave.close();
System.setOut(originalOut);
System.out.println(outContent);
}
}
}

private static class MyModbusComponent extends DummyModbusComponent {

public MyModbusComponent(String id, AbstractModbusBridge bridge, int unitId) throws OpenemsException {
super(id, bridge, unitId, ChannelId.values());
}

public enum ChannelId implements io.openems.edge.common.channel.ChannelId {
REGISTER_100(Doc.of(OpenemsType.INTEGER)); //

REGISTER_100(Doc.of(OpenemsType.INTEGER)), //
REGISTER_101(Doc.of(OpenemsType.SHORT)), //
;
private final Doc doc;

private ChannelId(Doc doc) {
Expand All @@ -116,7 +170,8 @@ public Doc doc() {
protected ModbusProtocol defineModbusProtocol() {
return new ModbusProtocol(this, //
new FC3ReadRegistersTask(100, Priority.HIGH, //
m(ChannelId.REGISTER_100, new UnsignedWordElement(100)))); //
m(ChannelId.REGISTER_100, new UnsignedWordElement(100)),
m(ChannelId.REGISTER_101, new UnsignedWordElement(101)))); //
}

}
Expand Down

0 comments on commit 0547606

Please sign in to comment.