From 56d0061da482def0e5b908dcc1b38bfc8d21ad93 Mon Sep 17 00:00:00 2001 From: Tamas Palfy Date: Fri, 4 Nov 2022 19:36:01 +0100 Subject: [PATCH] NIFI-10764 Fixed handling BIT STRING in nifi-asn1-services Representing as a string of 1s and 0s (e.g. "010011000") This closes #6622 Signed-off-by: David Handermann --- .../convert/converters/BerBitStringConverter.java | 4 ++-- .../apache/nifi/jasn1/ExampleDataGenerator.java | 2 ++ .../apache/nifi/jasn1/TestJASN1RecordReader.java | 1 + .../TestJASN1RecordReaderWithComplexTypes.java | 9 +++++++-- .../TestJASN1RecordReaderWithSimpleTypes.java | 4 ++-- .../src/test/resources/example.asn | 3 ++- .../src/test/resources/examples/basic-types.dat | Bin 61 -> 65 bytes 7 files changed, 16 insertions(+), 7 deletions(-) diff --git a/nifi-nar-bundles/nifi-asn1-bundle/nifi-asn1-services/src/main/java/org/apache/nifi/jasn1/convert/converters/BerBitStringConverter.java b/nifi-nar-bundles/nifi-asn1-bundle/nifi-asn1-services/src/main/java/org/apache/nifi/jasn1/convert/converters/BerBitStringConverter.java index 63572f6c02..22d3245141 100644 --- a/nifi-nar-bundles/nifi-asn1-bundle/nifi-asn1-services/src/main/java/org/apache/nifi/jasn1/convert/converters/BerBitStringConverter.java +++ b/nifi-nar-bundles/nifi-asn1-bundle/nifi-asn1-services/src/main/java/org/apache/nifi/jasn1/convert/converters/BerBitStringConverter.java @@ -31,7 +31,7 @@ public class BerBitStringConverter implements JASN1TypeAndValueConverter { @Override public DataType convertType(Class berType, JASN1Converter converter) { - return RecordFieldType.ARRAY.getArrayDataType(RecordFieldType.BOOLEAN.getDataType()); + return RecordFieldType.STRING.getDataType(); } @Override @@ -43,6 +43,6 @@ public class BerBitStringConverter implements JASN1TypeAndValueConverter { public Object convertValue(BerType value, DataType dataType, JASN1Converter converter) { final BerBitString berValue = (BerBitString) value; - return berValue.getValueAsBooleans(); + return berValue.toString(); } } diff --git a/nifi-nar-bundles/nifi-asn1-bundle/nifi-asn1-services/src/test/java/org/apache/nifi/jasn1/ExampleDataGenerator.java b/nifi-nar-bundles/nifi-asn1-bundle/nifi-asn1-services/src/test/java/org/apache/nifi/jasn1/ExampleDataGenerator.java index 878e821ccd..f73ad45ab4 100644 --- a/nifi-nar-bundles/nifi-asn1-bundle/nifi-asn1-services/src/test/java/org/apache/nifi/jasn1/ExampleDataGenerator.java +++ b/nifi-nar-bundles/nifi-asn1-bundle/nifi-asn1-services/src/test/java/org/apache/nifi/jasn1/ExampleDataGenerator.java @@ -17,6 +17,7 @@ package org.apache.nifi.jasn1; import com.beanit.asn1bean.ber.ReverseByteArrayOutputStream; +import com.beanit.asn1bean.ber.types.BerBitString; import com.beanit.asn1bean.ber.types.BerBoolean; import com.beanit.asn1bean.ber.types.BerInteger; import com.beanit.asn1bean.ber.types.BerOctetString; @@ -62,6 +63,7 @@ public class ExampleDataGenerator { basicTypes.setI(new BerInteger(789)); basicTypes.setOctStr(new BerOctetString(new byte[]{1, 2, 3, 4, 5})); basicTypes.setUtf8Str(new BerUTF8String("Some UTF-8 String. こんにちは世界。")); + basicTypes.setBitStr(new BerBitString(new boolean[]{false, true, true, false, true, false, false, false})); final int encoded = basicTypes.encode(rev); out.write(rev.getArray(), 0, encoded); LOG.info("Generated {} bytes to {}", encoded, file); diff --git a/nifi-nar-bundles/nifi-asn1-bundle/nifi-asn1-services/src/test/java/org/apache/nifi/jasn1/TestJASN1RecordReader.java b/nifi-nar-bundles/nifi-asn1-bundle/nifi-asn1-services/src/test/java/org/apache/nifi/jasn1/TestJASN1RecordReader.java index e40027c149..c1fb12c24d 100644 --- a/nifi-nar-bundles/nifi-asn1-bundle/nifi-asn1-services/src/test/java/org/apache/nifi/jasn1/TestJASN1RecordReader.java +++ b/nifi-nar-bundles/nifi-asn1-bundle/nifi-asn1-services/src/test/java/org/apache/nifi/jasn1/TestJASN1RecordReader.java @@ -59,6 +59,7 @@ public class TestJASN1RecordReader implements JASN1ReadRecordTester { assertEquals(789, record.getAsInt("i").intValue()); assertEquals("0102030405", record.getValue("octStr")); assertEquals("Some UTF-8 String. こんにちは世界。", record.getValue("utf8Str")); + assertEquals("01101000", record.getValue("bitStr")); record = reader.nextRecord(true, false); assertNull(record); diff --git a/nifi-nar-bundles/nifi-asn1-bundle/nifi-asn1-services/src/test/java/org/apache/nifi/jasn1/TestJASN1RecordReaderWithComplexTypes.java b/nifi-nar-bundles/nifi-asn1-bundle/nifi-asn1-services/src/test/java/org/apache/nifi/jasn1/TestJASN1RecordReaderWithComplexTypes.java index b438998530..f90b63228a 100644 --- a/nifi-nar-bundles/nifi-asn1-bundle/nifi-asn1-services/src/test/java/org/apache/nifi/jasn1/TestJASN1RecordReaderWithComplexTypes.java +++ b/nifi-nar-bundles/nifi-asn1-bundle/nifi-asn1-services/src/test/java/org/apache/nifi/jasn1/TestJASN1RecordReaderWithComplexTypes.java @@ -16,6 +16,7 @@ */ package org.apache.nifi.jasn1; +import com.beanit.asn1bean.ber.types.BerBitString; import com.beanit.asn1bean.ber.types.BerBoolean; import com.beanit.asn1bean.ber.types.BerInteger; import com.beanit.asn1bean.ber.types.BerOctetString; @@ -79,19 +80,22 @@ public class TestJASN1RecordReaderWithComplexTypes implements JASN1ReadRecordTes basicTypes.setI(new BerInteger(789)); basicTypes.setOctStr(new BerOctetString(new byte[]{1, 2, 3, 4, 5})); basicTypes.setUtf8Str(new BerUTF8String("Some UTF-8 String. こんにちは世界。")); + basicTypes.setBitStr(new BerBitString(new boolean[] { true, false, true, true})); Map expectedValues = new HashMap() {{ put("b", true); put("i", BigInteger.valueOf(789)); put("octStr", "0102030405"); put("utf8Str", "Some UTF-8 String. こんにちは世界。"); + put("bitStr", "1011"); }}; RecordSchema expectedSchema = new SimpleRecordSchema(Arrays.asList( new RecordField("b", RecordFieldType.BOOLEAN.getDataType()), new RecordField("i", RecordFieldType.BIGINT.getDataType()), new RecordField("octStr", RecordFieldType.STRING.getDataType()), - new RecordField("utf8Str", RecordFieldType.STRING.getDataType()) + new RecordField("utf8Str", RecordFieldType.STRING.getDataType()), + new RecordField("bitStr", RecordFieldType.STRING.getDataType()) )); testReadRecord(dataFile, basicTypes, expectedValues, expectedSchema); @@ -156,7 +160,8 @@ public class TestJASN1RecordReaderWithComplexTypes implements JASN1ReadRecordTes new RecordField("b", RecordFieldType.BOOLEAN.getDataType()), new RecordField("i", RecordFieldType.BIGINT.getDataType()), new RecordField("octStr", RecordFieldType.STRING.getDataType()), - new RecordField("utf8Str", RecordFieldType.STRING.getDataType()) + new RecordField("utf8Str", RecordFieldType.STRING.getDataType()), + new RecordField("bitStr", RecordFieldType.STRING.getDataType()) )); RecordSchema expectedSchema = new SimpleRecordSchema(Arrays.asList( diff --git a/nifi-nar-bundles/nifi-asn1-bundle/nifi-asn1-services/src/test/java/org/apache/nifi/jasn1/TestJASN1RecordReaderWithSimpleTypes.java b/nifi-nar-bundles/nifi-asn1-bundle/nifi-asn1-services/src/test/java/org/apache/nifi/jasn1/TestJASN1RecordReaderWithSimpleTypes.java index 67cf5c6419..90aed38331 100644 --- a/nifi-nar-bundles/nifi-asn1-bundle/nifi-asn1-services/src/test/java/org/apache/nifi/jasn1/TestJASN1RecordReaderWithSimpleTypes.java +++ b/nifi-nar-bundles/nifi-asn1-bundle/nifi-asn1-services/src/test/java/org/apache/nifi/jasn1/TestJASN1RecordReaderWithSimpleTypes.java @@ -106,11 +106,11 @@ public class TestJASN1RecordReaderWithSimpleTypes implements JASN1ReadRecordTest berValue.setValue(new BerBitString(new boolean[]{false, true, false, false, true, true, true, true, false, true, false, false})); Map expectedValues = new HashMap() {{ - put("value", new boolean[]{false, true, false, false, true, true, true, true, false, true, false, false}); + put("value", "010011110100"); }}; RecordSchema expectedSchema = new SimpleRecordSchema(Arrays.asList( - new RecordField("value", RecordFieldType.ARRAY.getArrayDataType(RecordFieldType.BOOLEAN.getDataType()))) + new RecordField("value", RecordFieldType.STRING.getDataType())) ); testReadRecord(dataFile, berValue, expectedValues, expectedSchema); diff --git a/nifi-nar-bundles/nifi-asn1-bundle/nifi-asn1-services/src/test/resources/example.asn b/nifi-nar-bundles/nifi-asn1-bundle/nifi-asn1-services/src/test/resources/example.asn index 82eed88f87..66fd9b3d8c 100644 --- a/nifi-nar-bundles/nifi-asn1-bundle/nifi-asn1-services/src/test/resources/example.asn +++ b/nifi-nar-bundles/nifi-asn1-bundle/nifi-asn1-services/src/test/resources/example.asn @@ -13,7 +13,8 @@ BasicTypes ::= SEQUENCE b [0] BOOLEAN, i [1] INTEGER, octStr [2] OCTET STRING (SIZE (1..10)), - utf8Str [3] UTF8String OPTIONAL + utf8Str [3] UTF8String OPTIONAL, + bitStr [4] BIT STRING OPTIONAL } Composite ::= SEQUENCE diff --git a/nifi-nar-bundles/nifi-asn1-bundle/nifi-asn1-services/src/test/resources/examples/basic-types.dat b/nifi-nar-bundles/nifi-asn1-bundle/nifi-asn1-services/src/test/resources/examples/basic-types.dat index a0fb624a03e9a95be338c53f640265fbc2553a7e..42d965910bd9436b166fa95cfafcbdb1e77c3883 100644 GIT binary patch delta 13 UcmcC@WHPXy$YjmZ!o-jP021i}umAu6 delta 8 PcmZ>?Wiqgy$Yc!w2bBT3