Skip to content

Commit

Permalink
fix(plc4j/codgen): fixed s7 io test
Browse files Browse the repository at this point in the history
  • Loading branch information
sruehl authored and chrisdutz committed Nov 17, 2021
1 parent 9bfbdfe commit 736c0fe
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,9 @@ public class ${type.name}IO implements <#if outputFlavor != "passive">MessageIO<
<#if helper.isEnumField(field)>
${helper.getWriteBufferWriteMethodCall(optionalField.name, helper.getEnumBaseTypeReference(optionalField.type), "(" + optionalField.name + ".getValue())", optionalField)};
<#else>
writeBuffer.pushContext("${optionalField.name}");
${complexTypeReference.name}IO.staticSerialize(writeBuffer, ${optionalField.name});
writeBuffer.popContext("${optionalField.name}");
</#if>
</#if>
}
Expand Down
5 changes: 3 additions & 2 deletions plc4j/drivers/s7/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,9 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
<version>1.5.0</version>
<scope>test</scope>
</dependency>

Expand Down
232 changes: 126 additions & 106 deletions plc4j/drivers/s7/src/test/java/S7IoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
* specific language governing permissions and limitations
* under the License.
*/

import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationConfig;
import org.apache.plc4x.java.s7.readwrite.*;
import org.apache.plc4x.java.s7.readwrite.io.TPKTPacketIO;
import org.apache.plc4x.java.s7.readwrite.types.COTPTpduSize;
Expand All @@ -28,6 +31,8 @@
import org.apache.plc4x.java.spi.generation.WriteBufferXmlBased;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;

import java.io.ByteArrayInputStream;
import java.util.Collections;
Expand All @@ -38,7 +43,6 @@
public class S7IoTest {

@Test
@Disabled("Needs to be ported first")
void TestS7MessageBytes() throws Exception {
String wantXml =
"<TPKTPacket>\n" +
Expand All @@ -64,43 +68,49 @@ void TestS7MessageBytes() throws Exception {
" </COTPParameterTpduSize>\n" +
" </COTPParameter>\n" +
" </parameters>\n" +
" <S7Message>\n" +
" <protocolId dataType=\"uint\" bitLength=\"8\">50</protocolId>\n" +
" <messageType dataType=\"uint\" bitLength=\"8\">3</messageType>\n" +
" <reserved dataType=\"uint\" bitLength=\"16\">0</reserved>\n" +
" <tpduReference dataType=\"uint\" bitLength=\"16\">11</tpduReference>\n" +
" <parameterLength dataType=\"uint\" bitLength=\"16\">2</parameterLength>\n" +
" <payloadLength dataType=\"uint\" bitLength=\"16\">6</payloadLength>\n" +
" <S7MessageResponseData>\n" +
" <errorClass dataType=\"uint\" bitLength=\"8\">0</errorClass>\n" +
" <errorCode dataType=\"uint\" bitLength=\"8\">0</errorCode>\n" +
" </S7MessageResponseData>\n" +
" <S7Parameter>\n" +
" <parameterType dataType=\"uint\" bitLength=\"8\">4</parameterType>\n" +
" <S7ParameterReadVarResponse>\n" +
" <numItems dataType=\"uint\" bitLength=\"8\">1</numItems>\n" +
" </S7ParameterReadVarResponse>\n" +
" </S7Parameter>\n" +
" <S7Payload>\n" +
" <S7PayloadReadVarResponse>\n" +
" <items isList=\"true\">\n" +
" <S7VarPayloadDataItem>\n" +
" <returnCode>\n" +
" <DataTransportErrorCode dataType=\"uint\" bitLength=\"8\" stringRepresentation=\"OK\">255</DataTransportErrorCode>\n" +
" </returnCode>\n" +
" <transportSize>\n" +
" <DataTransportSize dataType=\"uint\" bitLength=\"8\" stringRepresentation=\"BIT\">3</DataTransportSize>\n" +
" </transportSize>\n" +
" <dataLength dataType=\"uint\" bitLength=\"16\">1</dataLength>\n" +
" <data dataType=\"byte\" bitLength=\"8\">0x01</data>\n" +
" <padding isList=\"true\">\n" +
" <value dataType=\"uint\" bitLength=\"8\">0</value>\n" +
" </padding>\n" +
" </S7VarPayloadDataItem>\n" +
" </items>\n" +
" </S7PayloadReadVarResponse>\n" +
" </S7Payload>\n" +
" </S7Message>\n" +
" <payload>\n" +
" <S7Message>\n" +
" <protocolId dataType=\"uint\" bitLength=\"8\">50</protocolId>\n" +
" <messageType dataType=\"uint\" bitLength=\"8\">3</messageType>\n" +
" <reserved dataType=\"uint\" bitLength=\"16\">0</reserved>\n" +
" <tpduReference dataType=\"uint\" bitLength=\"16\">11</tpduReference>\n" +
" <parameterLength dataType=\"uint\" bitLength=\"16\">2</parameterLength>\n" +
" <payloadLength dataType=\"uint\" bitLength=\"16\">6</payloadLength>\n" +
" <S7MessageResponseData>\n" +
" <errorClass dataType=\"uint\" bitLength=\"8\">0</errorClass>\n" +
" <errorCode dataType=\"uint\" bitLength=\"8\">0</errorCode>\n" +
" </S7MessageResponseData>\n" +
" <parameter>\n" +
" <S7Parameter>\n" +
" <parameterType dataType=\"uint\" bitLength=\"8\">4</parameterType>\n" +
" <S7ParameterReadVarResponse>\n" +
" <numItems dataType=\"uint\" bitLength=\"8\">1</numItems>\n" +
" </S7ParameterReadVarResponse>\n" +
" </S7Parameter>\n" +
" </parameter>\n" +
" <payload>\n" +
" <S7Payload>\n" +
" <S7PayloadReadVarResponse>\n" +
" <items isList=\"true\">\n" +
" <S7VarPayloadDataItem>\n" +
" <returnCode>\n" +
" <DataTransportErrorCode dataType=\"uint\" bitLength=\"8\" stringRepresentation=\"OK\">255</DataTransportErrorCode>\n" +
" </returnCode>\n" +
" <transportSize>\n" +
" <DataTransportSize dataType=\"uint\" bitLength=\"8\" stringRepresentation=\"BIT\">3</DataTransportSize>\n" +
" </transportSize>\n" +
" <dataLength dataType=\"uint\" bitLength=\"16\">1</dataLength>\n" +
" <data dataType=\"byte\" bitLength=\"8\">0x01</data>\n" +
" <padding isList=\"true\">\n" +
" <value dataType=\"uint\" bitLength=\"8\">0</value>\n" +
" </padding>\n" +
" </S7VarPayloadDataItem>\n" +
" </items>\n" +
" </S7PayloadReadVarResponse>\n" +
" </S7Payload>\n" +
" </payload>\n" +
" </S7Message>\n" +
" </payload>\n" +
" </COTPPacket>\n" +
" </payload>\n" +
"</TPKTPacket>";
Expand All @@ -119,73 +129,84 @@ void TestS7MessageBytes() throws Exception {
" \"tpduRef__plc4x_bitLength\": 7,\n" +
" \"tpduRef__plc4x_dataType\": \"uint\"\n" +
" },\n" +
" \"S7Message\": {\n" +
" \"S7MessageResponseData\": {\n" +
" \"errorClass\": 0,\n" +
" \"errorClass__plc4x_bitLength\": 8,\n" +
" \"errorClass__plc4x_dataType\": \"uint\",\n" +
" \"errorCode\": 0,\n" +
" \"errorCode__plc4x_bitLength\": 8,\n" +
" \"errorCode__plc4x_dataType\": \"uint\"\n" +
" },\n" +
" \"S7Parameter\": {\n" +
" \"S7ParameterReadVarResponse\": {\n" +
" \"numItems\": 1,\n" +
" \"numItems__plc4x_bitLength\": 8,\n" +
" \"numItems__plc4x_dataType\": \"uint\"\n" +
" \"payload\": {\n" +
" \"S7Message\": {\n" +
" \"S7MessageResponseData\": {\n" +
" \"errorClass\": 0,\n" +
" \"errorClass__plc4x_bitLength\": 8,\n" +
" \"errorClass__plc4x_dataType\": \"uint\",\n" +
" \"errorCode\": 0,\n" +
" \"errorCode__plc4x_bitLength\": 8,\n" +
" \"errorCode__plc4x_dataType\": \"uint\"\n" +
" },\n" +
" \"parameter\": {\n" +
" \"S7Parameter\": {\n" +
" \"S7ParameterReadVarResponse\": {\n" +
" \"numItems\": 1,\n" +
" \"numItems__plc4x_bitLength\": 8,\n" +
" \"numItems__plc4x_dataType\": \"uint\"\n" +
" },\n" +
" \"parameterType\": 4,\n" +
" \"parameterType__plc4x_bitLength\": 8,\n" +
" \"parameterType__plc4x_dataType\": \"uint\"\n" +
" }\n" +
" },\n" +
" \"parameterType\": 4,\n" +
" \"parameterType__plc4x_bitLength\": 8,\n" +
" \"parameterType__plc4x_dataType\": \"uint\"\n" +
" },\n" +
" \"S7Payload\": {\n" +
" \"S7PayloadReadVarResponse\": {\n" +
" \"items\": [\n" +
" {\n" +
" \"S7VarPayloadDataItem\": {\n" +
" \"data\": \"0x01\",\n" +
" \"dataLength\": 1,\n" +
" \"dataLength__plc4x_bitLength\": 16,\n" +
" \"dataLength__plc4x_dataType\": \"uint\",\n" +
" \"data__plc4x_bitLength\": 8,\n" +
" \"data__plc4x_dataType\": \"byte\",\n" +
" \"padding\": [" +
" ],\n" +
" \"returnCode\": {\n" +
" \"DataTransportErrorCode\": 255,\n" +
" \"DataTransportErrorCode__plc4x_bitLength\": 8,\n" +
" \"DataTransportErrorCode__plc4x_dataType\": \"uint\",\n" +
" \"DataTransportErrorCode__plc4x_stringRepresentation\": \"OK\"\n" +
" },\n" +
" \"transportSize\": {\n" +
" \"DataTransportSize\": 3,\n" +
" \"DataTransportSize__plc4x_bitLength\": 8,\n" +
" \"DataTransportSize__plc4x_dataType\": \"uint\",\n" +
" \"DataTransportSize__plc4x_stringRepresentation\": \"BIT\"\n" +
" \"payload\": {\n" +
" \"S7Payload\": {\n" +
" \"S7PayloadReadVarResponse\": {\n" +
" \"items\": [\n" +
" {\n" +
" \"S7VarPayloadDataItem\": {\n" +
" \"data\": \"0x01\",\n" +
" \"dataLength\": 1,\n" +
" \"dataLength__plc4x_bitLength\": 16,\n" +
" \"dataLength__plc4x_dataType\": \"uint\",\n" +
" \"data__plc4x_bitLength\": 8,\n" +
" \"data__plc4x_dataType\": \"byte\",\n" +
" \"padding\": [\n" +
" {\n" +
" \"value__plc4x_dataType\": \"uint\",\n" +
" \"value__plc4x_bitLength\": 8,\n" +
" \"\": 0\n" +
" }\n" +
" ],\n" +
" \"returnCode\": {\n" +
" \"DataTransportErrorCode\": 255,\n" +
" \"DataTransportErrorCode__plc4x_bitLength\": 8,\n" +
" \"DataTransportErrorCode__plc4x_dataType\": \"uint\",\n" +
" \"DataTransportErrorCode__plc4x_stringRepresentation\": \"OK\"\n" +
" },\n" +
" \"transportSize\": {\n" +
" \"DataTransportSize\": 3,\n" +
" \"DataTransportSize__plc4x_bitLength\": 8,\n" +
" \"DataTransportSize__plc4x_dataType\": \"uint\",\n" +
" \"DataTransportSize__plc4x_stringRepresentation\": \"BIT\"\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
" ]\n" +
" }\n" +
" ]\n" +
" }\n" +
" },\n" +
" \"messageType\": 3,\n" +
" \"messageType__plc4x_bitLength\": 8,\n" +
" \"messageType__plc4x_dataType\": \"uint\",\n" +
" \"parameterLength\": 2,\n" +
" \"parameterLength__plc4x_bitLength\": 16,\n" +
" \"parameterLength__plc4x_dataType\": \"uint\",\n" +
" \"payloadLength\": 6,\n" +
" \"payloadLength__plc4x_bitLength\": 16,\n" +
" \"payloadLength__plc4x_dataType\": \"uint\",\n" +
" \"protocolId\": 50,\n" +
" \"protocolId__plc4x_bitLength\": 8,\n" +
" \"protocolId__plc4x_dataType\": \"uint\",\n" +
" \"reserved\": 0,\n" +
" \"reserved__plc4x_bitLength\": 16,\n" +
" \"reserved__plc4x_dataType\": \"uint\",\n" +
" \"tpduReference\": 11,\n" +
" \"tpduReference__plc4x_bitLength\": 16,\n" +
" \"tpduReference__plc4x_dataType\": \"uint\"\n" +
" }\n" +
" },\n" +
" \"messageType\": 3,\n" +
" \"messageType__plc4x_bitLength\": 8,\n" +
" \"messageType__plc4x_dataType\": \"uint\",\n" +
" \"parameterLength\": 2,\n" +
" \"parameterLength__plc4x_bitLength\": 16,\n" +
" \"parameterLength__plc4x_dataType\": \"uint\",\n" +
" \"payloadLength\": 6,\n" +
" \"payloadLength__plc4x_bitLength\": 16,\n" +
" \"payloadLength__plc4x_dataType\": \"uint\",\n" +
" \"protocolId\": 50,\n" +
" \"protocolId__plc4x_bitLength\": 8,\n" +
" \"protocolId__plc4x_dataType\": \"uint\",\n" +
" \"reserved\": 0,\n" +
" \"reserved__plc4x_bitLength\": 16,\n" +
" \"reserved__plc4x_dataType\": \"uint\",\n" +
" \"tpduReference\": 11,\n" +
" \"tpduReference__plc4x_bitLength\": 16,\n" +
" \"tpduReference__plc4x_dataType\": \"uint\"\n" +
" }\n" +
" },\n" +
" \"headerLength\": 5,\n" +
" \"headerLength__plc4x_bitLength\": 8,\n" +
Expand Down Expand Up @@ -254,16 +275,15 @@ void TestS7MessageBytes() throws Exception {
String gotXml = writeBufferXmlBased.getXmlString();
assertEquals(wantXml, gotXml);
ReadBufferXmlBased readBufferXmlBased = new ReadBufferXmlBased(new ByteArrayInputStream(gotXml.getBytes()));
//TPKTPacket reReadTpktPacket = TPKTPacketIO.staticParse(readBufferXmlBased);
//assertThat(reReadTpktPacket).usingRecursiveComparison().isEqualTo(tpktPacket);
TPKTPacket reReadTpktPacket = TPKTPacketIO.staticParse(readBufferXmlBased);
assertThat(reReadTpktPacket).usingRecursiveComparison().isEqualTo(tpktPacket);
}
// json
{
WriteBufferJsonBased writeBufferJsonBased = new WriteBufferJsonBased();
TPKTPacketIO.staticSerialize(writeBufferJsonBased, tpktPacket);
String gotJson = writeBufferJsonBased.getJsonString();
ObjectMapper objectMapper = new ObjectMapper();
assertEquals(objectMapper.readTree(wantJson), objectMapper.readTree(gotJson));
JSONAssert.assertEquals(wantJson, gotJson, JSONCompareMode.LENIENT);
ReadBufferJsonBased readBufferXmlBased = new ReadBufferJsonBased(new ByteArrayInputStream(gotJson.getBytes()));
TPKTPacket reReadTpktPacket = TPKTPacketIO.staticParse(readBufferXmlBased);
assertThat(reReadTpktPacket).usingRecursiveComparison().isEqualTo(tpktPacket);
Expand Down

0 comments on commit 736c0fe

Please sign in to comment.