You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by ot...@apache.org on 2023/01/20 15:00:00 UTC
[plc4x] branch develop updated: Feature/nifi integration type mapping (#752)
This is an automated email from the ASF dual-hosted git repository.
otto pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git
The following commit(s) were added to refs/heads/develop by this push:
new 91bf9fa46f Feature/nifi integration type mapping (#752)
91bf9fa46f is described below
commit 91bf9fa46fd26155d9c8c4d8db57ecfeeb593448
Author: Unai LerĂa Fortea <67...@users.noreply.github.com>
AuthorDate: Fri Jan 20 15:59:52 2023 +0100
Feature/nifi integration type mapping (#752)
* fix(plc4j/integrations/apache-nifi): Update mapping
between PlcType and Avro types.
* Update PlcType to Avro type mapping
* Added unit test for Plc4xSourceRecordProcessor
* Added common test utils.
* fix(plc4j/integrations/apache-nifi): Add license
to Plc4xCommonTest
* Update readme
* Add explicit source of Avro types
* Change output of example to actual output
---
plc4j/integrations/apache-nifi/README.md | 26 ++-
.../org/apache/plc4x/nifi/util/Plc4xCommon.java | 209 +++++++++------------
.../plc4x/nifi/Plc4xSourceRecordProcessorTest.java | 64 +++----
.../apache/plc4x/nifi/util/Plc4xCommonTest.java | 130 +++++++++++++
4 files changed, 252 insertions(+), 177 deletions(-)
diff --git a/plc4j/integrations/apache-nifi/README.md b/plc4j/integrations/apache-nifi/README.md
index 3bb0d4d7e5..8721d07131 100644
--- a/plc4j/integrations/apache-nifi/README.md
+++ b/plc4j/integrations/apache-nifi/README.md
@@ -74,40 +74,36 @@ The output flowfile will contain the PLC read values. This information is includ
[ {
"var1" : true,
"var2" : false,
- "var3" : [ false, false, false, false, false, false, true, true ],
+ "var3" : "\u0005",
"var5" : 1992,
- "var4" : [ false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, true ],
+ "var4" : "4",
"ts" : 1628783058433
} ]
```
Also, it is important to keep in mind the Processor Scheduling Configuration. Using the parameter **Run Schedule** (for example to *1 sec*), the reading frequency can be set. Note that by default, this value is defined to 0 sec (as fast as possible).
-Table of data mapping between plc data and Avro
+Table of data mapping between plc data and Avro types (as specified in [Avro specification](https://avro.apache.org/docs/1.11.1/specification/#primitive-types)).
+
| PLC type | Avro Type |
-|----------|-----------|
-| PlcBigDecimal | float |
-| PlcBigInteger | long |
-| PlcBitString | string |
+|----------:|-----------|
| PlcBOOL | boolean |
-| PlcBYTE | string |
+| PlcBYTE | bytes |
+| PlcSINT | int |
+| PlcINT | int |
+| PlcLINT | long |
+| PlcREAL | float |
+| PlcLREAL | double |
| PlcCHAR | string |
| PlcDATE_AND_TIME | string |
| PlcDATE | string |
| PlcDINT | string |
| PlcDWORD | string |
-| PlcINT | int |
-| PlcLINT | string |
-| PlcList | string |
-| PlcLREAL | string |
| PlcLTIME | string |
| PlcLWORD | string |
| PlcNull | string |
-| PlcREAL | double |
-| PlcSINT | int |
| PlcSTRING | string |
-| PlcStruct | string |
| PlcTIME_OF_DAY | string |
| PlcTIME | string |
| PlcUDINT | string |
diff --git a/plc4j/integrations/apache-nifi/nifi-plc4x-processors/src/main/java/org/apache/plc4x/nifi/util/Plc4xCommon.java b/plc4j/integrations/apache-nifi/nifi-plc4x-processors/src/main/java/org/apache/plc4x/nifi/util/Plc4xCommon.java
index 01448ef109..a0fd6c9258 100644
--- a/plc4j/integrations/apache-nifi/nifi-plc4x-processors/src/main/java/org/apache/plc4x/nifi/util/Plc4xCommon.java
+++ b/plc4j/integrations/apache-nifi/nifi-plc4x-processors/src/main/java/org/apache/plc4x/nifi/util/Plc4xCommon.java
@@ -23,8 +23,10 @@ import java.util.Map;
import org.apache.avro.Schema;
import org.apache.avro.SchemaBuilder;
+import org.apache.avro.SchemaBuilder.BaseTypeBuilder;
import org.apache.avro.SchemaBuilder.FieldAssembler;
-//TODO review remaining datatypes
+import org.apache.avro.SchemaBuilder.NullDefault;
+import org.apache.avro.SchemaBuilder.UnionAccumulator;
import org.apache.plc4x.java.api.value.PlcValue;
import org.apache.plc4x.java.spi.values.PlcBOOL;
import org.apache.plc4x.java.spi.values.PlcBYTE;
@@ -36,14 +38,10 @@ import org.apache.plc4x.java.spi.values.PlcDWORD;
import org.apache.plc4x.java.spi.values.PlcINT;
import org.apache.plc4x.java.spi.values.PlcLINT;
import org.apache.plc4x.java.spi.values.PlcLREAL;
-import org.apache.plc4x.java.spi.values.PlcLTIME;
import org.apache.plc4x.java.spi.values.PlcLWORD;
import org.apache.plc4x.java.spi.values.PlcList;
-import org.apache.plc4x.java.spi.values.PlcNull;
import org.apache.plc4x.java.spi.values.PlcREAL;
import org.apache.plc4x.java.spi.values.PlcSINT;
-import org.apache.plc4x.java.spi.values.PlcSTRING;
-import org.apache.plc4x.java.spi.values.PlcStruct;
import org.apache.plc4x.java.spi.values.PlcTIME;
import org.apache.plc4x.java.spi.values.PlcTIME_OF_DAY;
import org.apache.plc4x.java.spi.values.PlcUDINT;
@@ -75,68 +73,38 @@ public class Plc4xCommon {
for (Map.Entry<String, ? extends PlcValue> entry : responseDataStructure.entrySet()) {
fieldName = entry.getKey();
- if (entry.getValue() instanceof PlcBOOL) {
- builder.name(fieldName).type().unionOf().nullBuilder().endNull().and().booleanType().endUnion().noDefault();
- }else if (entry.getValue() instanceof PlcBYTE) {
- builder.name(fieldName).type().unionOf().nullBuilder().endNull().and().bytesType().endUnion().noDefault();
- }else if (entry.getValue() instanceof PlcCHAR) {
- builder.name(fieldName).type().unionOf().nullBuilder().endNull().and().stringType().endUnion().noDefault();
- }else if (entry.getValue() instanceof PlcDATE_AND_TIME) {
- builder.name(fieldName).type().unionOf().nullBuilder().endNull().and().stringType().endUnion().noDefault();
- }else if (entry.getValue() instanceof PlcDATE) {
- builder.name(fieldName).type().unionOf().nullBuilder().endNull().and().stringType().endUnion().noDefault();
- }else if (entry.getValue() instanceof PlcDINT) {
- builder.name(fieldName).type().unionOf().nullBuilder().endNull().and().stringType().endUnion().noDefault();
- }else if (entry.getValue() instanceof PlcDWORD) {
- builder.name(fieldName).type().unionOf().nullBuilder().endNull().and().stringType().endUnion().noDefault();
- }else if (entry.getValue() instanceof PlcINT) {
- builder.name(fieldName).type().unionOf().nullBuilder().endNull().and().intType().endUnion().noDefault();
- }else if (entry.getValue() instanceof PlcLINT) {
- builder.name(fieldName).type().unionOf().nullBuilder().endNull().and().stringType().endUnion().noDefault();
- }else if (entry.getValue() instanceof PlcLREAL) {
- builder.name(fieldName).type().unionOf().nullBuilder().endNull().and().stringType().endUnion().noDefault();
- }else if (entry.getValue() instanceof PlcLTIME) {
- builder.name(fieldName).type().unionOf().nullBuilder().endNull().and().stringType().endUnion().noDefault();
- }else if (entry.getValue() instanceof PlcLWORD) {
- builder.name(fieldName).type().unionOf().nullBuilder().endNull().and().stringType().endUnion().noDefault();
- }else if (entry.getValue() instanceof PlcNull) {
- builder.name(fieldName).type().unionOf().nullBuilder().endNull().and().stringType().endUnion().noDefault();
- }else if (entry.getValue() instanceof PlcREAL) {
- builder.name(fieldName).type().unionOf().nullBuilder().endNull().and().doubleType().endUnion().noDefault();
- }else if (entry.getValue() instanceof PlcSINT) {
- builder.name(fieldName).type().unionOf().nullBuilder().endNull().and().intType().endUnion().noDefault();
- }else if (entry.getValue() instanceof PlcSTRING) {
- builder.name(fieldName).type().unionOf().nullBuilder().endNull().and().stringType().endUnion().noDefault();
- }else if (entry.getValue() instanceof PlcStruct) {
- builder.name(fieldName).type().unionOf().nullBuilder().endNull().and().stringType().endUnion().noDefault();
- }else if (entry.getValue() instanceof PlcTIME_OF_DAY) {
- builder.name(fieldName).type().unionOf().nullBuilder().endNull().and().stringType().endUnion().noDefault();
- }else if (entry.getValue() instanceof PlcTIME) {
- builder.name(fieldName).type().unionOf().nullBuilder().endNull().and().stringType().endUnion().noDefault();
- }else if (entry.getValue() instanceof PlcUDINT) {
- builder.name(fieldName).type().unionOf().nullBuilder().endNull().and().stringType().endUnion().noDefault();
- }else if (entry.getValue() instanceof PlcUINT) {
- builder.name(fieldName).type().unionOf().nullBuilder().endNull().and().stringType().endUnion().noDefault();
- }else if (entry.getValue() instanceof PlcULINT) {
- builder.name(fieldName).type().unionOf().nullBuilder().endNull().and().stringType().endUnion().noDefault();
- }else if (entry.getValue() instanceof PlcUSINT) {
- builder.name(fieldName).type().unionOf().nullBuilder().endNull().and().stringType().endUnion().noDefault();
- }else if (entry.getValue() instanceof PlcWCHAR) {
- builder.name(fieldName).type().unionOf().nullBuilder().endNull().and().stringType().endUnion().noDefault();
- }else if (entry.getValue() instanceof PlcWORD) {
- builder.name(fieldName).type().unionOf().nullBuilder().endNull().and().stringType().endUnion().noDefault();
- }else if(entry.getValue() instanceof PlcList) {
- if(!entry.getValue().getList().isEmpty()) {
- if(entry.getValue().getList().get(0) instanceof PlcBOOL) {
- builder.name(fieldName).type().unionOf().nullBuilder().endNull().and().array().items().booleanType().endUnion().noDefault();
- }
- } else {
- builder.name(fieldName).type().nullBuilder().endNull();
+ PlcValue value = entry.getValue();
+ BaseTypeBuilder<UnionAccumulator<NullDefault<Schema>>> fieldBuilder =
+ builder.name(fieldName).type().unionOf().nullType().and();
+
+ if (value instanceof PlcList) {
+ if(!value.getList().isEmpty()) {
+ fieldBuilder = fieldBuilder.array().items();
+ value = value.getList().get(0);
}
}
- else { //TODO try forcing any other datatype to string...
- builder.name(fieldName).type().unionOf().nullBuilder().endNull().and().stringType().endUnion().noDefault();
+
+ // PlcTYPEs not in here are casted to avro string type.
+ UnionAccumulator<NullDefault<Schema>> buildedField = null;
+ if (value instanceof PlcBOOL) {
+ buildedField = fieldBuilder.booleanType();
+ }else if (value instanceof PlcBYTE) {
+ buildedField = fieldBuilder.bytesType();
+ }else if (value instanceof PlcINT) {
+ buildedField = fieldBuilder.intType();
+ }else if (value instanceof PlcLINT) {
+ buildedField = fieldBuilder.longType();
+ }else if (value instanceof PlcLREAL) {
+ buildedField = fieldBuilder.doubleType();
+ }else if (value instanceof PlcREAL) {
+ buildedField = fieldBuilder.floatType();
+ }else if (value instanceof PlcSINT) {
+ buildedField = fieldBuilder.intType();
+ }else {// Default to string:
+ fieldBuilder.stringType().endUnion().nullDefault();
+ continue;// In case of null default continue
}
+ buildedField.endUnion().noDefault();
}
//add timestamp tag to schema
@@ -144,47 +112,66 @@ public class Plc4xCommon {
return builder.endRecord();
-
}
private static Object normalizeBasicTypes(final Object valueOriginal) {
- if (valueOriginal == null) {
+ if (valueOriginal == null)
return null;
- } else if (valueOriginal instanceof PlcValue) {
+
+ if (valueOriginal instanceof PlcValue) {
PlcValue value = (PlcValue) valueOriginal;
- if (value.isBoolean() && value instanceof PlcBOOL)
+ // 8 bits
+ if (value instanceof PlcBOOL && value.isBoolean())
return value.getBoolean();
- else if (value.isByte() && value instanceof PlcBYTE)
- return value.getByte();
- else if (value.isDate() && value instanceof PlcDATE)
- return value.getDate();
- else if (value.isDateTime() && value instanceof PlcDATE_AND_TIME)
- return value.getDateTime();
- else if (value.isFloat() && value instanceof PlcLREAL)
+ if (value instanceof PlcBYTE && (value.isByte() || value.isShort()))
+ return new byte[]{value.getByte()};
+ if (value instanceof PlcCHAR && value.isShort())
+ return value.getString();
+ if ((value instanceof PlcSINT || value instanceof PlcUSINT) && value.isShort())
+ return value.getShort();
+
+
+ // 16 bits
+ if (value instanceof PlcWORD && (value.isInteger() || value.isShort()))
+ return value.getString();
+ if (value instanceof PlcINT && value.isInteger())
+ return value.getInteger();
+ if (value instanceof PlcUINT && value.isInteger())
+ return value.getInteger();
+ if ((value instanceof PlcWCHAR || value instanceof PlcDWORD) && value.isInteger())
+ return value.getString();
+
+ // 32 bits
+ if (value instanceof PlcREAL && value.isFloat())
return value.getFloat();
- else if (value.isInteger() && value instanceof PlcINT)
+ if ((value instanceof PlcDINT || value instanceof PlcUDINT) && value.isInteger())
return value.getInteger();
- else if (value.isList() && value instanceof PlcList) // TODO
- return value.getList().toArray();
- else if (value.isDouble())
- return value.getDouble();
- else if (value.isDuration())
- return value.getDuration();
- else if (value.isLong())
+ if (value instanceof PlcDWORD && value.isInteger())
+ return value.getString();
+
+ // 64 bits
+ if ((value instanceof PlcLINT || value instanceof PlcULINT) && value.isLong())
return value.getLong();
- else if (value.isShort())
- return value.getShort();
- else if (value.isString())
+ if (value instanceof PlcLREAL && value.isDouble())
+ return value.getDouble();
+ if (value instanceof PlcLWORD && (value.isLong() || value.isBigInteger()))
return value.getString();
- else if (value.isTime())
+
+ // Dates and time
+ if (value instanceof PlcDATE && value.isDate())
+ return value.getDate();
+ if (value instanceof PlcDATE_AND_TIME && value.isDateTime())
+ return value.getDateTime();
+ if (value instanceof PlcTIME && value.isTime())
return value.getTime();
- else
- return value.getString();
- } else {
- return valueOriginal;
- }
-
+ if (value instanceof PlcTIME_OF_DAY && value.isTime())
+ return value.getTime();
+
+ // Everything else to string
+ return value.getString();
+ }
+ return valueOriginal;
}
public static Object normalizeValue(final Object valueOriginal) {
@@ -192,22 +179,11 @@ public class Plc4xCommon {
return null;
}
if (valueOriginal instanceof List) {
- return ((List) valueOriginal).toArray();
+ return ((List<?>) valueOriginal).toArray();
} else if (valueOriginal instanceof PlcValue) {
- PlcValue value = (PlcValue) valueOriginal;
- if(value.isBoolean() && value instanceof PlcBOOL)
- return value.getBoolean();
- else if (value.isByte() && value instanceof PlcBYTE)
- return value.getByte();
- else if (value.isDate() && value instanceof PlcDATE)
- return value.getDate();
- else if (value.isDateTime() && value instanceof PlcDATE_AND_TIME)
- return value.getDateTime();
- else if (value.isFloat() && value instanceof PlcLREAL)
- return value.getFloat();
- else if (value.isInteger() && value instanceof PlcINT)
- return value.getInteger();
- else if (value.isList() && value instanceof PlcList) { //TODO
+ PlcValue value = (PlcValue) valueOriginal;
+
+ if (value.isList() && value instanceof PlcList) {
Object[] r = new Object[value.getList().size()];
int i = 0;
for (Object element : value.getList()) {
@@ -215,21 +191,8 @@ public class Plc4xCommon {
i++;
}
return r;
- }
- else if (value.isDouble())
- return value.getDouble();
- else if (value.isDuration())
- return value.getDuration();
- else if (value.isLong())
- return value.getLong();
- else if (value.isShort())
- return value.getShort();
- else if (value.isString())
- return value.getString();
- else if (value.isTime())
- return value.getTime();
- else
- return value.getString();
+ }
+ return normalizeBasicTypes(value);
} else {
return valueOriginal;
}
diff --git a/plc4j/integrations/apache-nifi/nifi-plc4x-processors/src/test/java/org/apache/plc4x/nifi/Plc4xSourceRecordProcessorTest.java b/plc4j/integrations/apache-nifi/nifi-plc4x-processors/src/test/java/org/apache/plc4x/nifi/Plc4xSourceRecordProcessorTest.java
index f09195dee6..e508318571 100644
--- a/plc4j/integrations/apache-nifi/nifi-plc4x-processors/src/test/java/org/apache/plc4x/nifi/Plc4xSourceRecordProcessorTest.java
+++ b/plc4j/integrations/apache-nifi/nifi-plc4x-processors/src/test/java/org/apache/plc4x/nifi/Plc4xSourceRecordProcessorTest.java
@@ -18,73 +18,59 @@
*/
package org.apache.plc4x.nifi;
+import java.util.Map;
+
import org.apache.nifi.avro.AvroRecordSetWriter;
-import org.apache.nifi.json.JsonRecordSetWriter;
import org.apache.nifi.reporting.InitializationException;
-import org.apache.nifi.serialization.record.MockRecordWriter;
import org.apache.nifi.util.TestRunner;
import org.apache.nifi.util.TestRunners;
+import org.apache.plc4x.nifi.util.Plc4xCommonTest;
import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
public class Plc4xSourceRecordProcessorTest {
private TestRunner testRunner;
private static int NUMBER_OF_CALLS = 5;
+
+ private final AvroRecordSetWriter writerService = new AvroRecordSetWriter();
@BeforeEach
public void init() throws InitializationException {
testRunner = TestRunners.newTestRunner(Plc4xSourceRecordProcessor.class);
testRunner.setIncomingConnection(false);
testRunner.setValidateExpressionUsage(false);
+
testRunner.setProperty(Plc4xSourceRecordProcessor.PLC_READ_FUTURE_TIMEOUT_MILISECONDS, "100");
testRunner.setProperty(Plc4xSourceRecordProcessor.PLC_CONNECTION_STRING, "simulated://127.0.0.1");
- testRunner.setProperty("var1", "STATE/foo1:BOOL");
- testRunner.setProperty("var2", "STATE/foo2:BOOL");
- testRunner.setProperty("var3", "STATE/foo3:BYTE");
- testRunner.setProperty("var4", "STATE/foo4:WORD");
- testRunner.setProperty("var5", "STATE/foo5:INT");
+
testRunner.addConnection(Plc4xSourceRecordProcessor.REL_SUCCESS);
testRunner.addConnection(Plc4xSourceRecordProcessor.REL_FAILURE);
- }
- @Test
- public void testMockRecordWriterProcessor() throws InitializationException {
- final MockRecordWriter writerService = new MockRecordWriter("header", false);
- testRunner.addControllerService("writer", writerService);
- testRunner.enableControllerService(writerService);
- testRunner.setProperty(Plc4xSourceRecordProcessor.PLC_RECORD_WRITER_FACTORY.getName(), "writer");
- testRunner.run(NUMBER_OF_CALLS,true, true);
- //validations
- testRunner.assertTransferCount(Plc4xSourceRecordProcessor.REL_FAILURE, 0);
- testRunner.assertTransferCount(Plc4xSourceRecordProcessor.REL_SUCCESS, NUMBER_OF_CALLS);
- }
-
- @Test
- @Disabled("Disabled for now")
- public void testJsonRecordWriterProcessor() throws InitializationException {
- final JsonRecordSetWriter writerService = new JsonRecordSetWriter();
- testRunner.addControllerService("writer", writerService);
+ testRunner.addControllerService("writer", writerService);
testRunner.enableControllerService(writerService);
- testRunner.setProperty(Plc4xSourceRecordProcessor.PLC_RECORD_WRITER_FACTORY.getName(), "writer");
- testRunner.run(NUMBER_OF_CALLS,true, true);
- //validations
- testRunner.assertTransferCount(Plc4xSourceRecordProcessor.REL_FAILURE, 0);
- testRunner.assertTransferCount(Plc4xSourceRecordProcessor.REL_SUCCESS, NUMBER_OF_CALLS);
+ testRunner.setProperty(Plc4xSourceRecordProcessor.PLC_RECORD_WRITER_FACTORY.getName(), "writer");
+
+ for (Map.Entry<String,String> address :Plc4xCommonTest.addressMap.entrySet()) {
+ // TODO: Random generation not working with this types
+ if (address.getValue().startsWith("RANDOM/")) {
+ if (address.getValue().endsWith("BYTE") ||
+ address.getValue().endsWith("CHAR") ||
+ address.getValue().endsWith("STRING"))
+ continue;
+ }
+ testRunner.setProperty(address.getKey(), address.getValue());
+ }
}
-
+
@Test
- @Disabled("Disabled for now")
- public void testAvroRecordWriterProcessor() throws InitializationException {
- final AvroRecordSetWriter writerService = new AvroRecordSetWriter();
- testRunner.addControllerService("writer", writerService);
- testRunner.enableControllerService(writerService);
- testRunner.setProperty(Plc4xSourceRecordProcessor.PLC_RECORD_WRITER_FACTORY.getName(), "writer");
+ public void testAvroRecordWriterProcessor() throws InitializationException {
+
testRunner.run(NUMBER_OF_CALLS,true, true);
//validations
testRunner.assertTransferCount(Plc4xSourceRecordProcessor.REL_FAILURE, 0);
testRunner.assertTransferCount(Plc4xSourceRecordProcessor.REL_SUCCESS, NUMBER_OF_CALLS);
- }
+ Plc4xCommonTest.assertAvroContent(testRunner.getFlowFilesForRelationship(Plc4xSourceProcessor.REL_SUCCESS), false, true);
+ }
}
diff --git a/plc4j/integrations/apache-nifi/nifi-plc4x-processors/src/test/java/org/apache/plc4x/nifi/util/Plc4xCommonTest.java b/plc4j/integrations/apache-nifi/nifi-plc4x-processors/src/test/java/org/apache/plc4x/nifi/util/Plc4xCommonTest.java
new file mode 100644
index 0000000000..425bd0b30e
--- /dev/null
+++ b/plc4j/integrations/apache-nifi/nifi-plc4x-processors/src/test/java/org/apache/plc4x/nifi/util/Plc4xCommonTest.java
@@ -0,0 +1,130 @@
+/*
+ * 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
+ *
+ * https://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.
+ */
+
+package org.apache.plc4x.nifi.util;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Consumer;
+
+import org.apache.avro.file.DataFileReader;
+import org.apache.avro.file.SeekableByteArrayInput;
+import org.apache.avro.generic.GenericDatumReader;
+import org.apache.avro.generic.GenericRecord;
+import org.apache.avro.io.DatumReader;
+import org.apache.avro.util.Utf8;
+import org.apache.nifi.util.MockFlowFile;
+
+public class Plc4xCommonTest {
+ public static Map<String, Object> originalMap = new HashMap<>();
+ public static Map<String, String> addressMap = new HashMap<>();
+ public static Map<String, Class> typeMap = new HashMap<>();
+
+ static {
+ // originalMap values are in the type needed to check type mapping between PlcType and Avro
+ originalMap.put("BOOL", true);
+ originalMap.put("BYTE", "\u0001");
+ originalMap.put("WORD", "4");
+ originalMap.put("SINT", Short.valueOf((short)-5));
+ originalMap.put("USINT", "6");
+ originalMap.put("INT", 2000);
+ originalMap.put("UINT", "3000");
+ originalMap.put("DINT", "4000");
+ originalMap.put("UDINT", "5000");
+ originalMap.put("DWORD", "0");
+ originalMap.put("LINT", 6000L);
+ originalMap.put("ULINT", "7000");
+ originalMap.put("LWORD", "ab");
+ originalMap.put("REAL", 1.23456F);
+ originalMap.put("LREAL", 2.34567);
+ originalMap.put("CHAR", "c");
+ originalMap.put("WCHAR", "d");
+ originalMap.put("STRING", "this is a string");
+
+ addressMap.put("BOOL", "RANDOM/v1:BOOL");
+ addressMap.put("BYTE", "RANDOM/v2:BYTE");
+ addressMap.put("WORD", "RANDOM/v3:WORD");
+ addressMap.put("SINT", "RANDOM/v4:SINT");
+ addressMap.put("USINT", "RANDOM/v5:USINT");
+ addressMap.put("INT", "RANDOM/v6:INT");
+ addressMap.put("UINT", "RANDOM/v7:UINT");
+ addressMap.put("DINT", "RANDOM/v8:DINT");
+ addressMap.put("UDINT", "RANDOM/v9:UDINT");
+ addressMap.put("DWORD", "RANDOM/v10:DWORD");
+ addressMap.put("LINT", "RANDOM/v11:LINT");
+ addressMap.put("ULINT", "RANDOM/v12:ULINT");
+ addressMap.put("LWORD", "RANDOM/v13:LWORD");
+ addressMap.put("REAL", "RANDOM/v14:REAL");
+ addressMap.put("LREAL", "RANDOM/v15:LREAL");
+ addressMap.put("CHAR", "RANDOM/v16:CHAR");
+ addressMap.put("WCHAR", "RANDOM/v17:WCHAR");
+ addressMap.put("STRING", "RANDOM/v18:STRING");
+
+ typeMap.put("BOOL", Boolean.class);
+ typeMap.put("BYTE", ByteBuffer.class);
+ typeMap.put("WORD", Utf8.class);
+ typeMap.put("SINT", Integer.class);
+ typeMap.put("USINT", Utf8.class);
+ typeMap.put("INT", Integer.class);
+ typeMap.put("UINT", Utf8.class);
+ typeMap.put("DINT", Utf8.class);
+ typeMap.put("UDINT", Utf8.class);
+ typeMap.put("DWORD", Utf8.class);
+ typeMap.put("LINT", Long.class);
+ typeMap.put("ULINT", Utf8.class);
+ typeMap.put("LWORD", Utf8.class);
+ typeMap.put("REAL", Float.class);
+ typeMap.put("LREAL", Double.class);
+ typeMap.put("CHAR", Utf8.class);
+ typeMap.put("WCHAR", Utf8.class);
+ typeMap.put("STRING", Utf8.class);
+ }
+
+ public static void assertAvroContent(List<MockFlowFile> flowfiles, boolean checkValue, boolean checkType) {
+ flowfiles.forEach(new Consumer<MockFlowFile>() {
+ @Override
+ public void accept(MockFlowFile t) {
+ DatumReader<GenericRecord> dr = new GenericDatumReader<>();
+ try (DataFileReader<GenericRecord> dfr = new DataFileReader<GenericRecord>(new SeekableByteArrayInput(t.toByteArray()), dr)) {
+ GenericRecord data = null;
+ while (dfr.hasNext()) {
+ data = dfr.next(data);
+
+ for (String tag : Plc4xCommonTest.addressMap.keySet()) {
+ if (data.hasField(tag)) {
+ // Check value after string conversion
+ if (checkValue)
+ assert data.get(tag).toString().equalsIgnoreCase(Plc4xCommonTest.originalMap.get(tag).toString());
+
+ // Check type
+ if (checkType)
+ assert data.get(tag).getClass().equals(Plc4xCommonTest.typeMap.get(tag));
+ }
+ }
+ }
+ dfr.close();
+
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ });
+ }
+}