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 2018/09/17 07:08:01 UTC

[incubator-plc4x] 17/29: some work on ADS tests

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

cdutz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git

commit 9d5f2e2b2d4f05382f2564f143f6d6d18d431800
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Thu Sep 13 16:39:00 2018 +0200

    some work on ADS tests
---
 .../ads/protocol/util/LittleEndianDecoderTest.java | 84 ++++++++++++++--------
 .../ads/protocol/util/LittleEndianEncoderTest.java | 66 +++++++++++------
 2 files changed, 97 insertions(+), 53 deletions(-)

diff --git a/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/protocol/util/LittleEndianDecoderTest.java b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/protocol/util/LittleEndianDecoderTest.java
index b5c4fcf..467ccc8 100644
--- a/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/protocol/util/LittleEndianDecoderTest.java
+++ b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/protocol/util/LittleEndianDecoderTest.java
@@ -18,46 +18,70 @@
  */
 package org.apache.plc4x.java.ads.protocol.util;
 
-public class LittleEndianDecoderTest {
-
-    /*
-    TODO: complete broken fix after refactoring
-    @Test
-    public void decodeData() throws Exception {
-        assertEquals(asList(true, false), LittleEndianDecoder.decodeData(Boolean.class, new byte[]{0x1, 0x0}));
+import org.apache.plc4x.java.ads.model.AdsDataType;
+import org.apache.plc4x.java.api.exceptions.PlcProtocolException;
+import org.apache.plc4x.java.api.exceptions.PlcUnsupportedDataTypeException;
+import org.junit.jupiter.api.Disabled;
+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;
 
-        assertEquals(asList((byte) 0x1, (byte) 0x0), LittleEndianDecoder.decodeData(Byte.class, new byte[]{0x1, 0x0}));
+import java.util.Arrays;
+import java.util.Calendar;
+import java.util.Collection;
+import java.util.Date;
+import java.util.stream.Stream;
 
-        assertEquals(singletonList((short) 1), LittleEndianDecoder.decodeData(Short.class, new byte[]{0x1}));
-        assertEquals(singletonList((short) 256), LittleEndianDecoder.decodeData(Short.class, new byte[]{0x0, 0x1}));
-        assertEquals(asList((short) 256, (short) 256), LittleEndianDecoder.decodeData(Short.class, new byte[]{0x0, 0x1, 0x0, 0x1}));
+import static java.util.Arrays.asList;
+import static java.util.Collections.singletonList;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 
-        assertEquals(singletonList(1), LittleEndianDecoder.decodeData(Integer.class, new byte[]{0x1}));
-        assertEquals(singletonList(16777216), LittleEndianDecoder.decodeData(Integer.class, new byte[]{0x0, 0x0, 0x0, 0x1}));
-        assertEquals(asList(16777216, 16777216), LittleEndianDecoder.decodeData(Integer.class, new byte[]{0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x1}));
+@Disabled("needs finishing")
+public class LittleEndianDecoderTest {
 
-        assertEquals(singletonList(1.4E-45f), LittleEndianDecoder.decodeData(Float.class, new byte[]{0x1}));
-        assertEquals(singletonList(2.3509887E-38f), LittleEndianDecoder.decodeData(Float.class, new byte[]{0x0, 0x0, 0x0, 0x1}));
-        assertEquals(asList(2.3509887E-38f, 2.3509887E-38f), LittleEndianDecoder.decodeData(Float.class, new byte[]{0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x1}));
+    @ParameterizedTest
+    @MethodSource("createAdsDataTypePears")
+    public void decodeData(AdsDataType adsDataType, Collection expectedTypes, Class<?> clazz, byte[] adsData) {
+        assertEquals(expectedTypes, Arrays.asList(LittleEndianDecoder.decodeData(adsDataType, adsData).getValues()));
+    }
 
-        assertEquals(singletonList(4.9E-324), LittleEndianDecoder.decodeData(Double.class, new byte[]{0x1}));
-        assertEquals(singletonList(7.2911220195563975E-304), LittleEndianDecoder.decodeData(Double.class, new byte[]{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1}));
-        assertEquals(asList(7.2911220195563975E-304, 7.2911220195563975E-304), LittleEndianDecoder.decodeData(Double.class, new byte[]{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1}));
+    @Test
+    public void negativeTest() {
+        assertThrows(PlcProtocolException.class, () -> LittleEndianDecoder.decodeData(AdsDataType.STRING, new byte[]{0x01}));
+        assertThrows(PlcUnsupportedDataTypeException.class, () -> LittleEndianDecoder.decodeData(AdsDataType.UNKNOWN, new byte[10]));
+    }
 
+    private static Stream<Arguments> createAdsDataTypePears() {
         Calendar calendar1 = Calendar.getInstance();
         calendar1.setTime(new Date(-11644473600000L));
-        assertEquals(singletonList(calendar1), LittleEndianDecoder.decodeData(Calendar.class, new byte[]{0x1}));
         Calendar calendar0x0001 = Calendar.getInstance();
         calendar0x0001.setTime(new Date(-4438714196208L));
-        assertEquals(singletonList(calendar0x0001), LittleEndianDecoder.decodeData(Calendar.class, new byte[]{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1}));
-        assertEquals(asList(calendar0x0001, calendar0x0001), LittleEndianDecoder.decodeData(Calendar.class, new byte[]{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1}));
-
-        assertEquals(singletonList("plc4x"), LittleEndianDecoder.decodeData(String.class, new byte[]{0x70, 0x6c, 0x63, 0x34, 0x78, 0x0}));
-        assertEquals(singletonList("plc4xplc4x"), LittleEndianDecoder.decodeData(String.class, new byte[]{0x70, 0x6c, 0x63, 0x34, 0x78, 0x70, 0x6c, 0x63, 0x34, 0x78, 0x0}));
-        assertEquals(asList("plc4x", "plc4x"), LittleEndianDecoder.decodeData(String.class, new byte[]{0x70, 0x6c, 0x63, 0x34, 0x78, 0x0, 0x70, 0x6c, 0x63, 0x34, 0x78, 0x0}));
 
-        assertThrows(PlcProtocolException.class, () -> LittleEndianDecoder.decodeData(String.class, new byte[]{0x01}));
-        assertThrows(PlcUnsupportedDataTypeException.class, () -> LittleEndianDecoder.decodeData(this.getClass(), new byte[10]));
+        return Arrays.stream(AdsDataType.values())
+            .filter(adsDataType -> adsDataType != AdsDataType.UNKNOWN)
+            .flatMap(adsDataType -> Stream.of(
+                Arguments.of(asList(true, false), Boolean.class, new byte[]{0x1, 0x0}),
+                Arguments.of(asList((byte) 0x1, (byte) 0x0), Byte.class, new byte[]{0x1, 0x0}),
+                Arguments.of(singletonList((short) 1), Short.class, new byte[]{0x1}),
+                Arguments.of(singletonList((short) 256), Short.class, new byte[]{0x0, 0x1}),
+                Arguments.of(asList((short) 256, (short) 256), Short.class, new byte[]{0x0, 0x1, 0x0, 0x1}),
+                Arguments.of(singletonList(1), Integer.class, new byte[]{0x1}),
+                Arguments.of(singletonList(16777216), Integer.class, new byte[]{0x0, 0x0, 0x0, 0x1}),
+                Arguments.of(asList(16777216, 16777216), Integer.class, new byte[]{0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x1}),
+                Arguments.of(singletonList(1.4E-45f), Float.class, new byte[]{0x1}),
+                Arguments.of(singletonList(2.3509887E-38f), Float.class, new byte[]{0x0, 0x0, 0x0, 0x1}),
+                Arguments.of(asList(2.3509887E-38f, 2.3509887E-38f), Float.class, new byte[]{0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x1}),
+                Arguments.of(singletonList(4.9E-324), Double.class, new byte[]{0x1}),
+                Arguments.of(singletonList(7.2911220195563975E-304), Double.class, new byte[]{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1}),
+                Arguments.of(asList(7.2911220195563975E-304, 7.2911220195563975E-304), Double.class, new byte[]{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1}),
+                Arguments.of(singletonList(calendar1), Calendar.class, new byte[]{0x1}),
+                Arguments.of(singletonList(calendar0x0001), Calendar.class, new byte[]{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1}),
+                Arguments.of(asList(calendar0x0001, calendar0x0001), Calendar.class, new byte[]{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1}),
+                Arguments.of(singletonList("plc4x"), String.class, new byte[]{0x70, 0x6c, 0x63, 0x34, 0x78, 0x0}),
+                Arguments.of(singletonList("plc4xplc4x"), String.class, new byte[]{0x70, 0x6c, 0x63, 0x34, 0x78, 0x70, 0x6c, 0x63, 0x34, 0x78, 0x0}),
+                Arguments.of(asList("plc4x", "plc4x"), String.class, new byte[]{0x70, 0x6c, 0x63, 0x34, 0x78, 0x0, 0x70, 0x6c, 0x63, 0x34, 0x78, 0x0})
+            ).map(arguments -> Arguments.of(adsDataType, arguments.get()[0], arguments.get()[1], arguments.get()[2])));
     }
-*/
 }
\ No newline at end of file
diff --git a/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/protocol/util/LittleEndianEncoderTest.java b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/protocol/util/LittleEndianEncoderTest.java
index 65c479b..7370bee 100644
--- a/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/protocol/util/LittleEndianEncoderTest.java
+++ b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/protocol/util/LittleEndianEncoderTest.java
@@ -18,38 +18,58 @@
  */
 package org.apache.plc4x.java.ads.protocol.util;
 
-public class LittleEndianEncoderTest {
-
-    /*
-    TODO: complete broken fix after refactoring
-    @Test
-    public void encodeData() throws Exception {
-        assertByteEquals(new byte[]{0x01, 0x00, 0x01, 0x00}, LittleEndianEncoder.encodeData(Boolean.class, true, false, true, false));
+import org.apache.plc4x.java.ads.model.AdsDataType;
+import org.apache.plc4x.java.api.exceptions.PlcUnsupportedDataTypeException;
+import org.junit.jupiter.api.Disabled;
+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;
 
-        assertByteEquals(new byte[]{0x12, 0x03, 0x05, 0x7f}, LittleEndianEncoder.encodeData(Byte.class, (byte) 0x12, (byte) 0x03, (byte) 0x05, (byte) 0x7f));
+import java.util.Arrays;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.stream.Stream;
 
-        assertByteEquals(new byte[]{0x1, 0x00}, LittleEndianEncoder.encodeData(Short.class, (short) 1));
-        assertByteEquals(new byte[]{0x0e, 0x00, 0x50, 0x00}, LittleEndianEncoder.encodeData(Short.class, (short) 14, (short) 80));
+import static org.apache.plc4x.java.base.util.Assert.assertByteEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 
-        assertByteEquals(new byte[]{0x5a, 0x0a, 0x00, 0x00}, LittleEndianEncoder.encodeData(Integer.class, 2650));
-        assertByteEquals(new byte[]{0x5a, 0x0a, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00}, LittleEndianEncoder.encodeData(Integer.class, 2650, 80));
+@Disabled("needs finishing")
+public class LittleEndianEncoderTest {
 
-        assertByteEquals(new byte[]{(byte) 0xc3, (byte) 0xf5, 0x48, 0x40}, LittleEndianEncoder.encodeData(Float.class, 3.14f));
-        assertByteEquals(new byte[]{(byte) 0xc3, (byte) 0xf5, 0x48, 0x40, 0x14, (byte) 0xae, 0x07, 0x40}, LittleEndianEncoder.encodeData(Float.class, 3.14f, 2.12f));
+    @ParameterizedTest
+    @MethodSource("createAdsDataTypePears")
+    public void decodeData(AdsDataType adsDataType, byte[] expectedData, Class<?> clazz, Object[] values) throws Exception {
+        assertByteEquals(expectedData, LittleEndianEncoder.encodeData(adsDataType, values));
+    }
 
-        assertByteEquals(new byte[]{0x1F, (byte) 0x85, (byte) 0xEB, 0x51, (byte) 0xB8, 0x1E, 0x09, 0x40}, LittleEndianEncoder.encodeData(Double.class, 3.14));
-        assertByteEquals(new byte[]{0x1F, (byte) 0x85, (byte) 0xEB, 0x51, (byte) 0xB8, 0x1E, 0x09, 0x40, (byte) 0xF6, 0x28, 0x5C, (byte) 0x8F, (byte) 0xC2, (byte) 0xF5, 0x00, 0x40}, LittleEndianEncoder.encodeData(Double.class, 3.14, 2.12));
+    @Test
+    public void negativeTest() {
+        assertThrows(PlcUnsupportedDataTypeException.class, () -> LittleEndianEncoder.encodeData(AdsDataType.UNKNOWN, ""));
+    }
 
+    private static Stream<Arguments> createAdsDataTypePears() {
         Calendar calendar1 = Calendar.getInstance();
         //calendar1.set(2003, Calendar.DECEMBER, 23, 13, 3, 0);
         calendar1.setTime(new Date(1072180980436L));
-        assertByteEquals(new byte[]{(byte) 0x40, (byte) 0x79, (byte) 0xFB, (byte) 0xB5, (byte) 0x4C, (byte) 0xC9, (byte) 0xC3, (byte) 0x01}, LittleEndianEncoder.encodeData(Calendar.class, calendar1));
-
-        assertByteEquals(new byte[]{0x70, 0x6c, 0x63, 0x34, 0x78, 0x00}, LittleEndianEncoder.encodeData(String.class, "plc4x"));
-        assertByteEquals(new byte[]{0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0x00}, LittleEndianEncoder.encodeData(String.class, "HelloWorld!"));
-        assertByteEquals(new byte[]{0x70, 0x6c, 0x63, 0x34, 0x78, 0x00, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0x00}, LittleEndianEncoder.encodeData(String.class, "plc4x", "HelloWorld!"));
 
-        assertThrows(PlcUnsupportedDataTypeException.class, () -> LittleEndianEncoder.encodeData(this.getClass(), ""));
+        return Arrays.stream(AdsDataType.values())
+            .filter(adsDataType -> adsDataType != AdsDataType.UNKNOWN)
+            .flatMap(adsDataType -> Stream.of(
+                Arguments.of(new byte[]{0x01, 0x00, 0x01, 0x00}, Boolean.class, true, false, true, false),
+                Arguments.of(new byte[]{0x12, 0x03, 0x05, 0x7f}, Byte.class, (byte) 0x12, (byte) 0x03, (byte) 0x05, (byte) 0x7f),
+                Arguments.of(new byte[]{0x1, 0x00}, Short.class, (short) 1),
+                Arguments.of(new byte[]{0x0e, 0x00, 0x50, 0x00}, Short.class, (short) 14, (short) 80),
+                Arguments.of(new byte[]{0x5a, 0x0a, 0x00, 0x00}, Integer.class, 2650),
+                Arguments.of(new byte[]{0x5a, 0x0a, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00}, Integer.class, 2650, 80),
+                Arguments.of(new byte[]{(byte) 0xc3, (byte) 0xf5, 0x48, 0x40}, Float.class, 3.14f),
+                Arguments.of(new byte[]{(byte) 0xc3, (byte) 0xf5, 0x48, 0x40, 0x14, (byte) 0xae, 0x07, 0x40}, Float.class, 3.14f, 2.12f),
+                Arguments.of(new byte[]{0x1F, (byte) 0x85, (byte) 0xEB, 0x51, (byte) 0xB8, 0x1E, 0x09, 0x40}, Double.class, 3.14),
+                Arguments.of(new byte[]{0x1F, (byte) 0x85, (byte) 0xEB, 0x51, (byte) 0xB8, 0x1E, 0x09, 0x40, (byte) 0xF6, 0x28, 0x5C, (byte) 0x8F, (byte) 0xC2, (byte) 0xF5, 0x00, 0x40}, Double.class, 3.14, 2.12),
+                Arguments.of(new byte[]{(byte) 0x40, (byte) 0x79, (byte) 0xFB, (byte) 0xB5, (byte) 0x4C, (byte) 0xC9, (byte) 0xC3, (byte) 0x01}, Calendar.class, calendar1),
+                Arguments.of(new byte[]{0x70, 0x6c, 0x63, 0x34, 0x78, 0x00}, String.class, "plc4x"),
+                Arguments.of(new byte[]{0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0x00}, String.class, "HelloWorld!"),
+                Arguments.of(new byte[]{0x70, 0x6c, 0x63, 0x34, 0x78, 0x00, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0x00}, String.class, "plc4x", "HelloWorld!")
+            ).map(arguments -> Arguments.of(adsDataType, arguments.get()[0], arguments.get()[1], arguments.get()[2])));
     }
-    */
 }
\ No newline at end of file