You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by sr...@apache.org on 2018/01/04 16:40:44 UTC
[incubator-plc4x] branch master updated: Test extension: - added
Tests for Plc4XS7Protocol - added basic test implementation for S7Protocol
This is an automated email from the ASF dual-hosted git repository.
sruehl pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git
The following commit(s) were added to refs/heads/master by this push:
new b3d4564 Test extension: - added Tests for Plc4XS7Protocol - added basic test implementation for S7Protocol
b3d4564 is described below
commit b3d4564d7f88fa4a612b78af8cbaf934d6b40fdd
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Thu Jan 4 17:40:39 2018 +0100
Test extension:
- added Tests for Plc4XS7Protocol
- added basic test implementation for S7Protocol
---
plc4j/pom.xml | 7 +
.../plc4x/java/s7/netty/Plc4XS7ProtocolTest.java | 205 ++++++++++++++++++++-
.../apache/plc4x/java/s7/netty/S7ProtocolTest.java | 62 ++++++-
3 files changed, 264 insertions(+), 10 deletions(-)
diff --git a/plc4j/pom.xml b/plc4j/pom.xml
index fb73861..9f607cf 100644
--- a/plc4j/pom.xml
+++ b/plc4j/pom.xml
@@ -50,6 +50,13 @@
</dependency>
<dependency>
+ <groupId>org.junit.jupiter</groupId>
+ <artifactId>junit-jupiter-params</artifactId>
+ <version>${junit-jupiter.version}</version>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.8.0</version>
diff --git a/plc4j/protocols/s7/src/test/java/org/apache/plc4x/java/s7/netty/Plc4XS7ProtocolTest.java b/plc4j/protocols/s7/src/test/java/org/apache/plc4x/java/s7/netty/Plc4XS7ProtocolTest.java
index e4db05a..adafac3 100644
--- a/plc4j/protocols/s7/src/test/java/org/apache/plc4x/java/s7/netty/Plc4XS7ProtocolTest.java
+++ b/plc4j/protocols/s7/src/test/java/org/apache/plc4x/java/s7/netty/Plc4XS7ProtocolTest.java
@@ -18,22 +18,215 @@ under the License.
*/
package org.apache.plc4x.java.s7.netty;
+import org.apache.plc4x.java.api.messages.PlcReadRequest;
+import org.apache.plc4x.java.api.messages.PlcRequest;
+import org.apache.plc4x.java.api.messages.PlcRequestContainer;
+import org.apache.plc4x.java.api.messages.PlcWriteRequest;
import org.apache.plc4x.java.netty.NettyTestBase;
+import org.apache.plc4x.java.s7.model.S7Address;
+import org.apache.plc4x.java.s7.model.S7BitAddress;
+import org.apache.plc4x.java.s7.model.S7DataBlockAddress;
+import org.apache.plc4x.java.s7.netty.model.messages.S7ResponseMessage;
+import org.apache.plc4x.java.s7.netty.model.params.VarParameter;
+import org.apache.plc4x.java.s7.netty.model.payloads.VarPayload;
+import org.apache.plc4x.java.s7.netty.model.payloads.items.VarPayloadItem;
+import org.apache.plc4x.java.s7.netty.model.types.DataTransportErrorCode;
+import org.apache.plc4x.java.s7.netty.model.types.DataTransportSize;
+import org.apache.plc4x.java.s7.netty.model.types.MessageType;
+import org.apache.plc4x.java.s7.netty.model.types.ParameterType;
+import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
-import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+import java.lang.reflect.Field;
+import java.util.*;
+import java.util.function.Consumer;
+import java.util.stream.Stream;
+
+import static java.util.Collections.singletonList;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.*;
+
+@SuppressWarnings("unchecked")
public class Plc4XS7ProtocolTest extends NettyTestBase {
- @Test
- @Tag("fast")
- public void encode() {
+ private Plc4XS7Protocol SUT;
+
+ @BeforeEach
+ void setUp() {
+ SUT = new Plc4XS7Protocol();
+ }
+ @ParameterizedTest
+ @MethodSource("typeAndAddressProvider")
+ @Tag("fast")
+ public void encode(Class<?> type, S7Address address) throws Exception {
+ // TODO: finish me
+ // Read Request Tests
+ {
+ LinkedList<Object> out = new LinkedList<>();
+ SUT.encode(null, createMockedContainer(new PlcReadRequest(type, address)), out);
+ // TODO: finish the asserts
+ assertThat(out).hasSize(1);
+ }
+ // Write Request Tests
+ {
+ LinkedList<Object> out = new LinkedList<>();
+ SUT.encode(null, createMockedContainer(new PlcWriteRequest(type, address, fakeValueFor(type))), out);
+ // TODO: finish the asserts
+ assertThat(out).hasSize(1);
+ }
}
- @Test
+ @ParameterizedTest
+ @MethodSource("typeAndAddressProvider")
@Tag("fast")
- public void decode() {
+ public void decode(Class<?> type, S7Address address) throws Exception {
+ // TODO: finish me
+ if (type == String.class) {
+ // String seems not yet decodable
+ return;
+ }
+ // Read Test
+ {
+ short fakeTpduReference = (short) 1;
+ {
+ // We need to put in a fake tpdu reference
+ Field requests = Plc4XS7Protocol.class.getDeclaredField("requests");
+ requests.setAccessible(true);
+ Map<Short, PlcRequestContainer> requestContainerMap = (Map<Short, PlcRequestContainer>) requests.get(SUT);
+ requestContainerMap.put(fakeTpduReference, createMockedContainer(new PlcReadRequest(type, address)));
+ }
+ S7ResponseMessage msg = new S7ResponseMessage(
+ MessageType.ACK,
+ fakeTpduReference,
+ singletonList(mock(VarParameter.class)),
+ singletonList(new VarPayload(ParameterType.READ_VAR, singletonList(varPayloadItemFor(type)))),
+ (byte) 0x00,
+ (byte) 0x00);
+ LinkedList<Object> out = new LinkedList<>();
+ SUT.decode(null, msg, out);
+ // TODO: finish the asserts
+ assertThat(out).hasSize(0);
+ }
+ // Write Test
+ {
+ short fakeTpduReference = (short) 2;
+ {
+ // We need to put in a fake tpdu reference
+ Field requests = Plc4XS7Protocol.class.getDeclaredField("requests");
+ requests.setAccessible(true);
+ Map<Short, PlcRequestContainer> requestContainerMap = (Map<Short, PlcRequestContainer>) requests.get(SUT);
+ requestContainerMap.put(fakeTpduReference, createMockedContainer(new PlcWriteRequest(type, address, fakeValueFor(type))));
+ }
+ S7ResponseMessage msg = new S7ResponseMessage(
+ MessageType.ACK,
+ fakeTpduReference,
+ singletonList(mock(VarParameter.class)),
+ singletonList(new VarPayload(ParameterType.WRITE_VAR, singletonList(varPayloadItemFor(type)))),
+ (byte) 0x00,
+ (byte) 0x00);
+ LinkedList<Object> out = new LinkedList<>();
+ SUT.decode(null, msg, out);
+ // TODO: finish the asserts
+ assertThat(out).hasSize(0);
+ }
+ }
+
+ private static Stream<Arguments> typeAndAddressProvider() {
+ List<Arguments> arguments = new LinkedList<>();
+ Arrays.asList(
+ Boolean.class,
+ Byte.class,
+ Short.class,
+ // TODO: enable once Calender in implemented
+ //Calendar.class,
+ Float.class,
+ Integer.class,
+ String.class)
+ .forEach(
+ aClass -> Arrays.asList(
+ mock(S7Address.class),
+ mock(S7BitAddress.class),
+ mock(S7DataBlockAddress.class))
+ .forEach(s7Address -> arguments.add(Arguments.of(aClass, s7Address)))
+ );
+ return arguments.stream();
+ }
+
+ private <T> T fakeValueFor(Class<T> type) {
+ if (type == Boolean.class) {
+ return (T) Boolean.TRUE;
+ } else if (type == Byte.class) {
+ return (T) Byte.valueOf((byte) 0x0000_0000);
+ } else if (type == Short.class) {
+ return (T) Short.valueOf((short) 123);
+ } else if (type == Calendar.class) {
+ return (T) Calendar.getInstance();
+ } else if (type == Float.class) {
+ return (T) Float.valueOf(123f);
+ } else if (type == Integer.class) {
+ return (T) Integer.valueOf(123);
+ } else if (type == String.class) {
+ return (T) "string";
+ } else {
+ throw new IllegalArgumentException("Type t not supported " + type);
+ }
+ }
+
+ private VarPayloadItem varPayloadItemFor(Class type) {
+ // TODO: fix example
+ final DataTransportSize size;
+ final byte[] data;
+ if (type == Boolean.class) {
+ size = DataTransportSize.BIT;
+ data = new byte[]{(byte) 0b0};
+ } else if (type == Byte.class) {
+ size = DataTransportSize.BYTE_WORD_DWORD;
+ data = new byte[]{(byte) 0b0000_0000};
+ } else if (type == Short.class) {
+ size = DataTransportSize.BYTE_WORD_DWORD;
+ data = new byte[]{(byte) 0b0000_0000, (byte) 0b0000_0000};
+ } else if (type == Calendar.class) {
+ size = DataTransportSize.BYTE_WORD_DWORD;
+ // TODO: what size is calender?
+ data = new byte[]{(byte) 0b0000_0000};
+ } else if (type == Float.class) {
+ size = DataTransportSize.BYTE_WORD_DWORD;
+ data = new byte[]{(byte) 0b0000_0000, (byte) 0b0000_0000, (byte) 0b0000_0000, (byte) 0b0000_0000};
+ } else if (type == Integer.class) {
+ size = DataTransportSize.INTEGER;
+ data = new byte[]{(byte) 0b0000_0000, (byte) 0b0000_0000, (byte) 0b0000_0000, (byte) 0b0000_0000};
+ } else if (type == String.class) {
+ size = DataTransportSize.BYTE_WORD_DWORD;
+ // TODO: what size is string?
+ data = new byte[]{(byte) 0b0000_0000};
+ } else {
+ throw new IllegalArgumentException("Type t not supported " + type);
+ }
+ return new VarPayloadItem(DataTransportErrorCode.OK, size, data);
+ }
+
+ private <T extends PlcRequest> PlcRequestContainer createMockedContainer(T initialRequest) {
+ return createMockedContainer(initialRequest, null);
+ }
+ private <T extends PlcRequest> PlcRequestContainer createMockedContainer(T initialRequest, Consumer<T> requestEnricher) {
+ Objects.requireNonNull(initialRequest);
+ PlcRequestContainer mock = mock(PlcRequestContainer.class, RETURNS_DEEP_STUBS);
+ if (requestEnricher != null) {
+ requestEnricher.accept(initialRequest);
+ }
+ when(mock.getRequest()).thenReturn(initialRequest);
+ if (initialRequest.getClass() == PlcReadRequest.class) {
+ return mock;
+ } else if (initialRequest.getClass() == PlcWriteRequest.class) {
+ return mock;
+ } else {
+ throw new IllegalArgumentException("Unsupported Type: " + initialRequest.getClass());
+ }
}
}
diff --git a/plc4j/protocols/s7/src/test/java/org/apache/plc4x/java/s7/netty/S7ProtocolTest.java b/plc4j/protocols/s7/src/test/java/org/apache/plc4x/java/s7/netty/S7ProtocolTest.java
index b65b6e1..13b3b4c 100644
--- a/plc4j/protocols/s7/src/test/java/org/apache/plc4x/java/s7/netty/S7ProtocolTest.java
+++ b/plc4j/protocols/s7/src/test/java/org/apache/plc4x/java/s7/netty/S7ProtocolTest.java
@@ -18,22 +18,76 @@ under the License.
*/
package org.apache.plc4x.java.s7.netty;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import org.apache.plc4x.java.isotp.netty.model.IsoTPMessage;
+import org.apache.plc4x.java.isotp.netty.model.tpdus.Tpdu;
import org.apache.plc4x.java.netty.NettyTestBase;
+import org.apache.plc4x.java.s7.netty.model.messages.S7RequestMessage;
+import org.apache.plc4x.java.s7.netty.model.params.VarParameter;
+import org.apache.plc4x.java.s7.netty.model.params.items.S7AnyVarParameterItem;
+import org.apache.plc4x.java.s7.netty.model.payloads.VarPayload;
+import org.apache.plc4x.java.s7.netty.model.payloads.items.VarPayloadItem;
+import org.apache.plc4x.java.s7.netty.model.types.*;
+import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
+import java.util.LinkedList;
+
+import static java.util.Collections.singletonList;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+
public class S7ProtocolTest extends NettyTestBase {
- @Test
- @Tag("fast")
- public void encode() {
+ private S7Protocol SUT;
+ @BeforeEach
+ void setUp() {
+ SUT = new S7Protocol((short) 1, (short) 1, (short) 1);
}
@Test
@Tag("fast")
- public void decode() {
+ public void encode() throws Exception {
+ //TODO: finish me
+ LinkedList<Object> out = new LinkedList<>();
+ SUT.encode(null, new S7RequestMessage(
+ MessageType.ACK,
+ (short) 1,
+ singletonList(new VarParameter(ParameterType.WRITE_VAR, singletonList(new S7AnyVarParameterItem(
+ SpecificationType.VARIABLE_SPECIFICATION, MemoryArea.DATA_BLOCKS, TransportSize.BIT, (short) 1, (short) 1, (short) 1, (byte) 1
+ )))),
+ singletonList(new VarPayload(
+ ParameterType.WRITE_VAR,
+ singletonList(new VarPayloadItem(
+ DataTransportErrorCode.OK,
+ DataTransportSize.BYTE_WORD_DWORD, new byte[]{0})
+ ))
+ )), out);
+ assertThat(out).hasSize(1);
+ }
+ @Test
+ @Tag("fast")
+ public void decode() throws Exception {
+ //TODO: finish me
+ LinkedList<Object> out = new LinkedList<>();
+ ByteBuf buffer = Unpooled.buffer();
+ // Magic Number
+ buffer.writeByte(0x32);
+ buffer.writeByte(MessageType.JOB.getCode());
+ // Reserved magic value
+ buffer.writeShort(0x0000);
+ // tpduReference
+ buffer.writeShort(0x0000);
+ // headerParametersLength
+ buffer.writeShort(0x0000);
+ // userDataLength
+ buffer.writeShort(0x0000);
+ SUT.decode(null, new IsoTPMessage(mock(Tpdu.class), buffer), out);
+ assertThat(out).hasSize(1);
}
}
--
To stop receiving notification emails like this one, please contact
['"commits@plc4x.apache.org" <co...@plc4x.apache.org>'].