From 0547606bfa3bfb92c3b5aef7419bb6ac82cd9909 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20St=C3=B6cker?= Date: Wed, 20 Nov 2024 12:12:42 +0100 Subject: [PATCH] added TestCase to cover General Exception 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 --- .../modbus/BridgeModbusTcpImplTest.java | 61 ++++++++++++++++++- 1 file changed, 58 insertions(+), 3 deletions(-) diff --git a/io.openems.edge.bridge.modbus/test/io/openems/edge/bridge/modbus/BridgeModbusTcpImplTest.java b/io.openems.edge.bridge.modbus/test/io/openems/edge/bridge/modbus/BridgeModbusTcpImplTest.java index 33c3496c64..cff2999447 100644 --- a/io.openems.edge.bridge.modbus/test/io/openems/edge/bridge/modbus/BridgeModbusTcpImplTest.java +++ b/io.openems.edge.bridge.modbus/test/io/openems/edge/bridge/modbus/BridgeModbusTcpImplTest.java @@ -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; @@ -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; @@ -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(); @@ -91,6 +97,53 @@ public void test() throws Exception { } } + @Test + public void testTriggerLogIllegalArgumentException() throws Exception { + final ThrowingRunnable 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 { @@ -98,8 +151,9 @@ public MyModbusComponent(String id, AbstractModbusBridge bridge, int unitId) thr } 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) { @@ -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)))); // } }