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/09/14 14:12:42 UTC

[incubator-plc4x] 02/02: some refactoring on the ADS decoder/encoder

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

sruehl pushed a commit to branch feature/api-redesign-chris-c
in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git

commit 6b25a0e41dc818a842e5c375c8dbd0d273fd83e2
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Fri Sep 14 16:12:35 2018 +0200

    some refactoring on the ADS decoder/encoder
---
 .../ads/protocol/util/LittleEndianDecoderTest.java | 25 ++++++++++++++-----
 .../ads/protocol/util/LittleEndianEncoderTest.java | 28 +++++++++++-----------
 2 files changed, 33 insertions(+), 20 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 467ccc8..0d2e773 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,19 +18,21 @@
  */
 package org.apache.plc4x.java.ads.protocol.util;
 
+import org.apache.commons.lang3.reflect.MethodUtils;
 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.apache.plc4x.java.base.messages.items.FieldItem;
 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;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
-import java.util.Arrays;
-import java.util.Calendar;
-import java.util.Collection;
-import java.util.Date;
+import java.lang.reflect.Method;
+import java.util.*;
 import java.util.stream.Stream;
 
 import static java.util.Arrays.asList;
@@ -41,10 +43,21 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
 @Disabled("needs finishing")
 public class LittleEndianDecoderTest {
 
+    private static Logger LOG = LoggerFactory.getLogger(LittleEndianDecoderTest.class);
+
     @ParameterizedTest
     @MethodSource("createAdsDataTypePears")
-    public void decodeData(AdsDataType adsDataType, Collection expectedTypes, Class<?> clazz, byte[] adsData) {
-        assertEquals(expectedTypes, Arrays.asList(LittleEndianDecoder.decodeData(adsDataType, adsData).getValues()));
+    public void decodeData(AdsDataType adsDataType, Collection expectedTypes, Class<?> clazz, byte[] adsData) throws Exception {
+        FieldItem<?> fieldItem = LittleEndianDecoder.decodeData(adsDataType, adsData);
+
+        Method getterMethod = MethodUtils.getAccessibleMethod(FieldItem.class, "get" + clazz.getSimpleName(), int.class);
+        LOG.info("Using {} to map", getterMethod);
+
+        List<? super Object> actualTypes = new LinkedList<>();
+        for (int i = 0; i < fieldItem.getNumberOfValues(); i++) {
+            actualTypes.add(getterMethod.invoke(fieldItem, i));
+        }
+        assertEquals(expectedTypes, actualTypes);
     }
 
     @Test
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 7370bee..86d4249 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
@@ -56,20 +56,20 @@ public class LittleEndianEncoderTest {
         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!")
+                Arguments.of(new byte[]{0x01, 0x00, 0x01, 0x00}, Boolean.class, new Object[]{true, false, true, false}),
+                Arguments.of(new byte[]{0x12, 0x03, 0x05, 0x7f}, Byte.class, new Object[]{(byte) 0x12, (byte) 0x03, (byte) 0x05, (byte) 0x7f}),
+                Arguments.of(new byte[]{0x1, 0x00}, Short.class, new Object[]{(short) 1}),
+                Arguments.of(new byte[]{0x0e, 0x00, 0x50, 0x00}, Short.class, new Object[]{(short) 14, (short) 80}),
+                Arguments.of(new byte[]{0x5a, 0x0a, 0x00, 0x00}, Integer.class, new Object[]{2650}),
+                Arguments.of(new byte[]{0x5a, 0x0a, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00}, Integer.class, new Object[]{2650, 80}),
+                Arguments.of(new byte[]{(byte) 0xc3, (byte) 0xf5, 0x48, 0x40}, Float.class, new Object[]{3.14f}),
+                Arguments.of(new byte[]{(byte) 0xc3, (byte) 0xf5, 0x48, 0x40, 0x14, (byte) 0xae, 0x07, 0x40}, Float.class, new Object[]{3.14f, 2.12f}),
+                Arguments.of(new byte[]{0x1F, (byte) 0x85, (byte) 0xEB, 0x51, (byte) 0xB8, 0x1E, 0x09, 0x40}, Double.class, new Object[]{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, new Object[]{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, new Object[]{calendar1}),
+                Arguments.of(new byte[]{0x70, 0x6c, 0x63, 0x34, 0x78, 0x00}, String.class, new Object[]{"plc4x"}),
+                Arguments.of(new byte[]{0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0x00}, String.class, new Object[]{"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, new Object[]{"plc4x", "HelloWorld!"})
             ).map(arguments -> Arguments.of(adsDataType, arguments.get()[0], arguments.get()[1], arguments.get()[2])));
     }
 }
\ No newline at end of file