You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by cd...@apache.org on 2020/10/27 18:01:05 UTC

[plc4x] 02/02: - Implemented a first Modbus Integration-Test - Extended the testsuite-runner to support tests with parser-arguments

This is an automated email from the ASF dual-hosted git repository.

cdutz pushed a commit to branch feature/plc4go
in repository https://gitbox.apache.org/repos/asf/plc4x.git

commit d2519e5d2a557fb27bf3784ac5684e87465ae4a1
Author: Christofer Dutz <ch...@c-ware.de>
AuthorDate: Tue Oct 27 19:00:50 2020 +0100

    - Implemented a first Modbus Integration-Test
    - Extended the testsuite-runner to support tests with parser-arguments
---
 .../apache/plc4x/java/modbus/ModbusDriverIT.java}  | 28 ++-----
 .../plc4x/test/driver/DriverTestsuiteRunner.java   | 29 +++++--
 .../apache/plc4x/test/driver/model/TestStep.java   | 10 ++-
 .../resources/protocols/modbus/DriverTestsuite.xml | 97 ++++++++++++++++++++++
 4 files changed, 132 insertions(+), 32 deletions(-)

diff --git a/plc4j/utils/test-utils/src/main/java/org/apache/plc4x/test/driver/model/TestStep.java b/plc4j/drivers/modbus/src/test/java/org/apache/plc4x/java/modbus/ModbusDriverIT.java
similarity index 59%
copy from plc4j/utils/test-utils/src/main/java/org/apache/plc4x/test/driver/model/TestStep.java
copy to plc4j/drivers/modbus/src/test/java/org/apache/plc4x/java/modbus/ModbusDriverIT.java
index 0a44522..ec2052c 100644
--- a/plc4j/utils/test-utils/src/main/java/org/apache/plc4x/test/driver/model/TestStep.java
+++ b/plc4j/drivers/modbus/src/test/java/org/apache/plc4x/java/modbus/ModbusDriverIT.java
@@ -16,32 +16,14 @@ KIND, either express or implied.  See the License for the
 specific language governing permissions and limitations
 under the License.
 */
-package org.apache.plc4x.test.driver.model;
+package org.apache.plc4x.java.modbus;
 
-import org.dom4j.Element;
+import org.apache.plc4x.test.driver.DriverTestsuiteRunner;
 
-public class TestStep {
+public class ModbusDriverIT extends DriverTestsuiteRunner {
 
-    private final StepType type;
-    private final String name;
-    private final Element payload;
-
-    public TestStep(StepType type, String name, Element payload) {
-        this.type = type;
-        this.name = name;
-        this.payload = payload;
-    }
-
-    public StepType getType() {
-        return type;
-    }
-
-    public String getName() {
-        return name;
-    }
-
-    public Element getPayload() {
-        return payload;
+    public ModbusDriverIT() {
+        super("/protocols/modbus/DriverTestsuite.xml");
     }
 
 }
diff --git a/plc4j/utils/test-utils/src/main/java/org/apache/plc4x/test/driver/DriverTestsuiteRunner.java b/plc4j/utils/test-utils/src/main/java/org/apache/plc4x/test/driver/DriverTestsuiteRunner.java
index 9f922fd..c47bf77 100644
--- a/plc4j/utils/test-utils/src/main/java/org/apache/plc4x/test/driver/DriverTestsuiteRunner.java
+++ b/plc4j/utils/test-utils/src/main/java/org/apache/plc4x/test/driver/DriverTestsuiteRunner.java
@@ -222,7 +222,7 @@ public class DriverTestsuiteRunner {
                     // Prepare a ByteBuf that contains the data which would have been sent to the PLC.
                     final byte[] data = getOutboundBytes(embeddedChannel);
                     // Validate the data actually matches the expected message.
-                    validateMessage(payload, data, bigEndian);
+                    validateMessage(payload, testStep.getParserArguments(), data, bigEndian);
                     break;
                 }
                 case INCOMING_PLC_BYTES: {
@@ -291,15 +291,28 @@ public class DriverTestsuiteRunner {
         LOGGER.info("    Done");
     }
 
-    private TestStep parseTestStep(Element curElement) {
+    private TestStep parseTestStep(Element curElement) throws DriverTestsuiteException {
         final String elementName = curElement.getName();
         final StepType stepType = StepType.valueOf(elementName.toUpperCase().replace("-", "_"));
         final String stepName = curElement.attributeValue(new QName("name"));
-        Element definition = null;
-        if(curElement.hasMixedContent()) {
-            definition = curElement.elementIterator().next();
+        Element parserArgumentsNode = null;
+        Element definitionNode = null;
+        for (Element element : curElement.elements()) {
+            if(element.getName().equals("parser-arguments")) {
+                parserArgumentsNode = element;
+            } else if (definitionNode == null) {
+                definitionNode = element;
+            } else {
+                throw new DriverTestsuiteException("Error processing the xml. Only one content node allowed.");
+            }
+        }
+        final List<String> parserArguments = new ArrayList<>();
+        if(parserArgumentsNode != null) {
+            for (Element parserArgumentNode : parserArgumentsNode.elements()) {
+                parserArguments.add(parserArgumentNode.getTextTrim());
+            }
         }
-        return new TestStep(stepType, stepName, definition);
+        return new TestStep(stepType, stepName, parserArguments, definitionNode);
     }
 
     private Plc4xEmbeddedChannel getEmbeddedChannel(PlcConnection plcConnection) {
@@ -384,13 +397,13 @@ public class DriverTestsuiteRunner {
         // TODO: Implement this ...
     }
 
-    private void validateMessage(Element referenceXml, byte[] data, boolean bigEndian) throws DriverTestsuiteException {
+    private void validateMessage(Element referenceXml, List<String> parserArguments, byte[] data, boolean bigEndian) throws DriverTestsuiteException {
         final ObjectMapper mapper = new XmlMapper().enableDefaultTyping();
         final ReadBuffer readBuffer = new ReadBuffer(data, !bigEndian);
         try {
             final String className = referenceXml.attributeValue(new QName("className"));
             final MessageIO<?,?> messageIO = getMessageIOType(className).newInstance();
-            final Object parsedOutput = messageIO.parse(readBuffer);
+            final Object parsedOutput = messageIO.parse(readBuffer, parserArguments.toArray(new String[0]));
             final String xmlString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(parsedOutput);
             final String referenceXmlString = referenceXml.asXML();
             final Diff diff = DiffBuilder.compare(referenceXmlString).withTest(xmlString).ignoreComments().ignoreWhitespace().build();
diff --git a/plc4j/utils/test-utils/src/main/java/org/apache/plc4x/test/driver/model/TestStep.java b/plc4j/utils/test-utils/src/main/java/org/apache/plc4x/test/driver/model/TestStep.java
index 0a44522..cd8d297 100644
--- a/plc4j/utils/test-utils/src/main/java/org/apache/plc4x/test/driver/model/TestStep.java
+++ b/plc4j/utils/test-utils/src/main/java/org/apache/plc4x/test/driver/model/TestStep.java
@@ -20,15 +20,19 @@ package org.apache.plc4x.test.driver.model;
 
 import org.dom4j.Element;
 
+import java.util.List;
+
 public class TestStep {
 
     private final StepType type;
     private final String name;
+    private final List<String> parserArguments;
     private final Element payload;
 
-    public TestStep(StepType type, String name, Element payload) {
+    public TestStep(StepType type, String name, List<String> parserArguments, Element payload) {
         this.type = type;
         this.name = name;
+        this.parserArguments = parserArguments;
         this.payload = payload;
     }
 
@@ -40,6 +44,10 @@ public class TestStep {
         return name;
     }
 
+    public List<String> getParserArguments() {
+        return parserArguments;
+    }
+
     public Element getPayload() {
         return payload;
     }
diff --git a/protocols/modbus/src/test/resources/protocols/modbus/DriverTestsuite.xml b/protocols/modbus/src/test/resources/protocols/modbus/DriverTestsuite.xml
new file mode 100644
index 0000000..429997d
--- /dev/null
+++ b/protocols/modbus/src/test/resources/protocols/modbus/DriverTestsuite.xml
@@ -0,0 +1,97 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one
+  or more contributor license agreements.  See the NOTICE file
+  distributed with this work for additional information
+  regarding copyright ownership.  The ASF licenses this file
+  to you under the Apache License, Version 2.0 (the
+  "License"); you may not use this file except in compliance
+  with the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing,
+  software distributed under the License is distributed on an
+  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+  KIND, either express or implied.  See the License for the
+  specific language governing permissions and limitations
+  under the License.
+  -->
+<test:driver-testsuite xmlns:test="https://plc4x.apache.org/schemas/driver-testsuite.xsd"
+                       bigEndian="true">
+
+  <name>Modbus</name>
+
+  <driver-name>modbus</driver-name>
+
+  <testcase>
+    <name>Single element read request</name>
+    <steps>
+      <api-request name="Receive Read Request from application">
+        <TestReadRequest className="org.apache.plc4x.test.driver.model.api.TestReadRequest">
+          <fields>
+            <field className="org.apache.plc4x.test.driver.model.api.TestField">
+              <name>hurz</name>
+              <address>holding-register:1:REAL[2]</address>
+            </field>
+          </fields>
+        </TestReadRequest>
+      </api-request>
+      <outgoing-plc-message name="Send Modbus Input-Register Read Request">
+        <parser-arguments>
+          <response>false</response>
+        </parser-arguments>
+        <ModbusTcpADU className="org.apache.plc4x.java.modbus.readwrite.ModbusTcpADU">
+          <transactionIdentifier>10</transactionIdentifier>
+          <unitIdentifier>1</unitIdentifier>
+          <pdu className="org.apache.plc4x.java.modbus.readwrite.ModbusPDUReadHoldingRegistersRequest">
+            <startingAddress>0</startingAddress>
+            <quantity>4</quantity>
+          </pdu>
+        </ModbusTcpADU>
+      </outgoing-plc-message>
+      <incoming-plc-message name="Receive Modbus Input-Register Read Response">
+        <parser-arguments>
+          <response>true</response>
+        </parser-arguments>
+        <ModbusTcpADU className="org.apache.plc4x.java.modbus.readwrite.ModbusTcpADU">
+          <transactionIdentifier>10</transactionIdentifier>
+          <unitIdentifier>1</unitIdentifier>
+          <pdu className="org.apache.plc4x.java.modbus.readwrite.ModbusPDUReadHoldingRegistersResponse">
+            <value>QEkP20BJD9s=</value>
+          </pdu>
+        </ModbusTcpADU>
+      </incoming-plc-message>
+      <api-response name="Report Read Response to application">
+        <DefaultPlcReadResponse className="org.apache.plc4x.java.spi.messages.DefaultPlcReadResponse">
+          <request className="org.apache.plc4x.java.spi.messages.DefaultPlcReadRequest">
+            <hurz className="org.apache.plc4x.java.modbus.field.ModbusFieldHoldingRegister">
+              <address>0</address>
+              <dataType>IEC61131_REAL</dataType>
+              <lengthBytes>8</lengthBytes>
+              <lengthWords>4</lengthWords>
+              <dataTypeSize>4</dataTypeSize>
+            </hurz>
+          </request>
+          <hurz>
+            <code>OK</code>
+            <value className="org.apache.plc4x.java.api.value.PlcList">
+              <object>java.util.Collections..UnmodifiableRandomAccessList</object>
+              <object>org.apache.plc4x.java.api.value.PlcREAL</object>
+              <object>
+                <object>java.lang.Float</object>
+                <object>3.1415927</object>
+              </object>
+              <object>org.apache.plc4x.java.api.value.PlcREAL</object>
+              <object>
+                <object>java.lang.Float</object>
+                <object>3.1415927</object>
+              </object>
+            </value>
+          </hurz>
+        </DefaultPlcReadResponse>
+      </api-response>
+    </steps>
+  </testcase>
+
+</test:driver-testsuite>