diff options
author | CGantert345 <57003061+CGantert345@users.noreply.github.com> | 2021-03-29 14:08:45 +0200 |
---|---|---|
committer | CGantert345 <57003061+CGantert345@users.noreply.github.com> | 2021-03-29 14:08:45 +0200 |
commit | e023e674020f1a435f7b8c8b9276704f576ea6e5 (patch) | |
tree | 011863c021df3e5356f07b056d30d0e7476061e4 /src/test/java/org | |
parent | Merge pull request #13 from UnionInternationalCheminsdeFer/time_zone_fix_3 (diff) | |
download | UIC-barcode-e023e674020f1a435f7b8c8b9276704f576ea6e5.tar UIC-barcode-e023e674020f1a435f7b8c8b9276704f576ea6e5.tar.gz UIC-barcode-e023e674020f1a435f7b8c8b9276704f576ea6e5.tar.bz2 UIC-barcode-e023e674020f1a435f7b8c8b9276704f576ea6e5.tar.lz UIC-barcode-e023e674020f1a435f7b8c8b9276704f576ea6e5.tar.xz UIC-barcode-e023e674020f1a435f7b8c8b9276704f576ea6e5.tar.zst UIC-barcode-e023e674020f1a435f7b8c8b9276704f576ea6e5.zip |
Diffstat (limited to 'src/test/java/org')
59 files changed, 9471 insertions, 0 deletions
diff --git a/src/test/java/org/uic/barcode/asn1/test/TestSequenceOfLong.java b/src/test/java/org/uic/barcode/asn1/test/TestSequenceOfLong.java new file mode 100644 index 0000000..61af5fa --- /dev/null +++ b/src/test/java/org/uic/barcode/asn1/test/TestSequenceOfLong.java @@ -0,0 +1,24 @@ +package org.uic.barcode.asn1.test;
+
+import java.util.Collection;
+import java.util.List;
+
+import org.uic.barcode.asn1.datatypes.Asn1SequenceOf;
+
+public class TestSequenceOfLong extends Asn1SequenceOf<Long> {
+ public TestSequenceOfLong() { super(); }
+ public TestSequenceOfLong(Collection<Long> coll) { super(coll); }
+
+
+ public TestSequenceOfLong(List<Long> numbers) {
+ super();
+ for (Long number: numbers){
+ this.add(new Long(number));
+ }
+ }
+
+ public static TestSequenceOfLong getSequence(List<Long> numList) {
+ if (numList == null || numList.isEmpty()) return null;
+ return new TestSequenceOfLong(numList);
+ }
+}
diff --git a/src/test/java/org/uic/barcode/asn1/test/UperEncodeBooleanTest.java b/src/test/java/org/uic/barcode/asn1/test/UperEncodeBooleanTest.java new file mode 100644 index 0000000..b5e73b0 --- /dev/null +++ b/src/test/java/org/uic/barcode/asn1/test/UperEncodeBooleanTest.java @@ -0,0 +1,82 @@ +package org.uic.barcode.asn1.test; + +import static org.junit.Assert.assertEquals; + +import java.util.logging.Level; + +import org.junit.Test; +import org.uic.barcode.asn1.datatypes.Asn1Optional; +import org.uic.barcode.asn1.datatypes.FieldOrder; +import org.uic.barcode.asn1.datatypes.Sequence; +import org.uic.barcode.asn1.uper.UperEncoder; + + +public class UperEncodeBooleanTest { + + /** + * Example from the Standard on UPER. + <pre> + TestRecord ::= [APPLICATION 0] IMPLICIT SEQUENCE { + value BOOLEAN OPTIONAL, +} + </pre> + */ + @Sequence + public static class TestRecord { + + @FieldOrder(order = 0) + @Asn1Optional() Boolean value; + + public TestRecord() { + this(false); + } + + public TestRecord(Boolean value) { + this.value = value; + } + } + + + @Test public void testTrue() throws IllegalArgumentException, IllegalAccessException { + TestRecord record = new TestRecord(new Boolean(true)); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + assertEquals("C0",hex); + } + + @Test public void testFalse() throws IllegalArgumentException, IllegalAccessException { + TestRecord record = new TestRecord(new Boolean(false)); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + assertEquals("80",hex); + } + + @Test public void testDecodeTrue() throws IllegalArgumentException, IllegalAccessException { + TestRecord record = new TestRecord(new Boolean(true)); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + assertEquals("C0",hex); + TestRecord result = UperEncoder.decode(encoded, TestRecord.class); + assertEquals(result.value,record.value); + + } + + @Test public void testDecodeFalse() throws IllegalArgumentException, IllegalAccessException { + TestRecord record = new TestRecord(new Boolean(false)); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + assertEquals("80",hex); + TestRecord result = UperEncoder.decode(encoded, TestRecord.class); + assertEquals(result.value,record.value); + } + + + + + + +} diff --git a/src/test/java/org/uic/barcode/asn1/test/UperEncodeChoiceExtensionTest.java b/src/test/java/org/uic/barcode/asn1/test/UperEncodeChoiceExtensionTest.java new file mode 100644 index 0000000..3833ff2 --- /dev/null +++ b/src/test/java/org/uic/barcode/asn1/test/UperEncodeChoiceExtensionTest.java @@ -0,0 +1,92 @@ +package org.uic.barcode.asn1.test; + +import static org.junit.Assert.assertEquals; + +import java.util.logging.Level; + +import org.junit.Test; +import org.uic.barcode.asn1.datatypes.Asn1Optional; +import org.uic.barcode.asn1.datatypes.CharacterRestriction; +import org.uic.barcode.asn1.datatypes.Choice; +import org.uic.barcode.asn1.datatypes.FieldOrder; +import org.uic.barcode.asn1.datatypes.HasExtensionMarker; +import org.uic.barcode.asn1.datatypes.IsExtension; +import org.uic.barcode.asn1.datatypes.RestrictedString; +import org.uic.barcode.asn1.uper.UperEncoder; + + +public class UperEncodeChoiceExtensionTest { + + /** Example for extended sequence + TestRecord ::= [APPLICATION 0] CHOICE { + value1 IA5String + ,... + ,value2 IA5String + } + + value TestRecord ::= value2: "extension" + */ + @Choice + @HasExtensionMarker + public static class TestRecordExtended { + + @FieldOrder(order = 0) + @RestrictedString(CharacterRestriction.IA5String) + String value1 = null; + + @FieldOrder(order = 1) + @IsExtension + @RestrictedString(CharacterRestriction.IA5String) + String value2 = "extension"; + + public TestRecordExtended() { } + } + + /** Example for extended sequence + TestRecord ::= [APPLICATION 0] CHOICE { + value1 IA5String, + ,... + } + */ + @Choice + @HasExtensionMarker + public static class TestRecord { + + @RestrictedString(CharacterRestriction.IA5String) + @Asn1Optional() String value1 = "regular"; + + public TestRecord() { } + } + + + @Test public void testEncode() throws IllegalArgumentException, IllegalAccessException { + TestRecordExtended record = new TestRecordExtended(); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + assertEquals("800909CBE3A65DDCF4EFDC",hex); + } + + @Test public void testDecodeExtended() throws IllegalArgumentException, IllegalAccessException { + TestRecordExtended record = new TestRecordExtended(); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + assertEquals("800909CBE3A65DDCF4EFDC",hex); + TestRecordExtended result = UperEncoder.decode(encoded, TestRecordExtended.class); + assertEquals(result.value2,record.value2); + } + + @Test public void testDecode() throws IllegalArgumentException, IllegalAccessException { + TestRecordExtended record = new TestRecordExtended(); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + assertEquals("800909CBE3A65DDCF4EFDC",hex); + TestRecord result = UperEncoder.decode(encoded, TestRecord.class); + assert(result == null); + } + + + +} diff --git a/src/test/java/org/uic/barcode/asn1/test/UperEncodeChoiceTest.java b/src/test/java/org/uic/barcode/asn1/test/UperEncodeChoiceTest.java new file mode 100644 index 0000000..69b92d1 --- /dev/null +++ b/src/test/java/org/uic/barcode/asn1/test/UperEncodeChoiceTest.java @@ -0,0 +1,74 @@ +package org.uic.barcode.asn1.test; + +import static org.junit.Assert.assertEquals; + +import java.util.logging.Level; + +import org.junit.Test; +import org.uic.barcode.asn1.datatypes.CharacterRestriction; +import org.uic.barcode.asn1.datatypes.Choice; +import org.uic.barcode.asn1.datatypes.FieldOrder; +import org.uic.barcode.asn1.datatypes.RestrictedString; +import org.uic.barcode.asn1.uper.UperEncoder; + + +public class UperEncodeChoiceTest { + + /** + * Example from the Standard on UPER. + <pre> + TestRecord ::= [APPLICATION 0] IMPLICIT SEQUENCE { + value EnumType DEFAULT value2, + } + + EnumType ::= ENUMERATED { + value1 (0), + value2 (1) + ,... + } + </pre> + */ + @Choice + public static class TestRecord { + + @FieldOrder(order = 0) + @RestrictedString(CharacterRestriction.UTF8String) + String valueUtf8; + + @FieldOrder(order = 1) + @RestrictedString(CharacterRestriction.IA5String) + String valueIA5; + + public TestRecord() { + } + + public TestRecord(String utf8, String ia5) { + this.valueUtf8 = utf8; + this.valueIA5 = ia5; + } + } + + @Test public void testEncode() throws IllegalArgumentException, IllegalAccessException { + TestRecord record = new TestRecord(null, "Meier"); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + assertEquals("82CDCBA72F20",hex); + } + + @Test public void testDecode() throws IllegalArgumentException, IllegalAccessException { + TestRecord record = new TestRecord(null, "Meier"); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + assertEquals("82CDCBA72F20",hex); + + TestRecord result = UperEncoder.decode(encoded, TestRecord.class); + assertEquals(null,record.valueUtf8); + assertEquals(result.valueIA5,record.valueIA5); + } + + + + +} diff --git a/src/test/java/org/uic/barcode/asn1/test/UperEncodeEnumExtensionTest.java b/src/test/java/org/uic/barcode/asn1/test/UperEncodeEnumExtensionTest.java new file mode 100644 index 0000000..a2ecbe1 --- /dev/null +++ b/src/test/java/org/uic/barcode/asn1/test/UperEncodeEnumExtensionTest.java @@ -0,0 +1,146 @@ +package org.uic.barcode.asn1.test; + +import static org.junit.Assert.assertEquals; + +import java.util.logging.Level; + +import org.junit.Test; +import org.uic.barcode.asn1.datatypes.Asn1Optional; +import org.uic.barcode.asn1.datatypes.FieldOrder; +import org.uic.barcode.asn1.datatypes.HasExtensionMarker; +import org.uic.barcode.asn1.datatypes.IsExtension; +import org.uic.barcode.asn1.datatypes.Sequence; +import org.uic.barcode.asn1.uper.UperEncoder; + + +public class UperEncodeEnumExtensionTest { + + /*** Example from the Standard on UPER. + <pre> + World-Schema DEFINITIONS AUTOMATIC TAGS ::= + BEGIN + TestRecord ::= [APPLICATION 0] IMPLICIT SEQUENCE { + value EnumType DEFAULT value2 + } + + EnumType ::= ENUMERATED { + value1 (0), + value2 (1) + ,... + } + END + </pre> + */ + @Sequence + public static class TestRecord { + + @FieldOrder(order = 0) + @Asn1Optional EnumType value = EnumType.value1; + public TestRecord() {} + public void setValue(EnumType value) { + this.value = value; + } + } + + + /*** Example from the Standard on UPER. + TestRecord ::= [APPLICATION 0] IMPLICIT SEQUENCE { + value EnumType DEFAULT value2, + } + + EnumType ::= ENUMERATED { + value1 (0), + value2 (1) + ,... + value3 (2) + } + */ + @Sequence + public static class TestRecordExtended { + + @Asn1Optional EnumTypeExtended value = EnumTypeExtended.value3; + + public TestRecordExtended() {} + + public void setValue(EnumTypeExtended value) { + this.value = value; + } + + + } + + @HasExtensionMarker + public enum EnumType { + value1("value1"), + value2("value2"); + + public String text; + + EnumType(String text) { + this.text = text; + } + + public String toString(){ + return text; + } + } + + + @HasExtensionMarker + public enum EnumTypeExtended { + value1("value1"), + value2("value2"), + + @IsExtension + value3("value3"); + + public String text; + + EnumTypeExtended(String text) { + this.text = text; + } + + public String toString(){ + return text; + } + } + + + + @Test public void testExtension() throws IllegalArgumentException, IllegalAccessException { + + TestRecordExtended record = new TestRecordExtended(); + record.setValue(EnumTypeExtended.value3); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("Enum value3: data hex: %s", hex)); + assertEquals("C000", hex); + } + + @Test public void testExtensionDecoding() throws IllegalArgumentException, IllegalAccessException { + + TestRecordExtended record = new TestRecordExtended(); + record.setValue(EnumTypeExtended.value3); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("Enum value3: data hex: %s", hex)); + assertEquals("C000", hex); + + TestRecordExtended result = UperEncoder.decode(encoded, TestRecordExtended.class); + assertEquals(result.value,EnumTypeExtended.value3); + } + + @Test public void testUnknownExtensionDecoding() throws IllegalArgumentException, IllegalAccessException { + + TestRecordExtended record = new TestRecordExtended(); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("Enum value3: data hex: %s", hex)); + assertEquals("C000", hex); + + TestRecord result = UperEncoder.decode(encoded, TestRecord.class); + assert(result.value == null); + } + + +} diff --git a/src/test/java/org/uic/barcode/asn1/test/UperEncodeEnumTest.java b/src/test/java/org/uic/barcode/asn1/test/UperEncodeEnumTest.java new file mode 100644 index 0000000..d1d2d82 --- /dev/null +++ b/src/test/java/org/uic/barcode/asn1/test/UperEncodeEnumTest.java @@ -0,0 +1,126 @@ +package org.uic.barcode.asn1.test; + +import static org.junit.Assert.assertEquals; + +import java.util.logging.Level; + +import org.junit.Test; +import org.uic.barcode.asn1.datatypes.Asn1Default; +import org.uic.barcode.asn1.datatypes.Asn1Optional; +import org.uic.barcode.asn1.datatypes.FieldOrder; +import org.uic.barcode.asn1.datatypes.Sequence; +import org.uic.barcode.asn1.uper.UperEncoder; + + +public class UperEncodeEnumTest { + + /** + * Example from the Standard on UPER. + <pre> + TestRecord ::= [APPLICATION 0] IMPLICIT SEQUENCE { + value EnumType DEFAULT value2, + } + + EnumType ::= ENUMERATED { + value1 (0), + value2 (1) + ,... + } + </pre> + */ + @Sequence + public static class TestRecord { + + @FieldOrder(order = 0) + @Asn1Default(value="value2") + @Asn1Optional EnumType value = EnumType.value2; + + + public TestRecord() {} + + public TestRecord(EnumType value) { + this.value = value; + } + } + + public enum EnumType { + value1("value1"), + value2("value2"), + value3("value3"), + value4("value4"), + value5("value5"), + value6("value6"), + value7("value7"), + value8("value8"), + value9("value9"), + value10("value10"), + value11("value11"), + value12("value12"), + value13("value13"), + value14("value14"), + value15("value15"), + value16("value16"), + value17("value17"), + value18("value18"), + value19("value19"), + value20("value20"), + value21("value21"), + value22("value22"); + + + public String text; + + EnumType(String text) { + this.text = text; + } + + public String toString(){ + return text; + } + } + + + + @Test public void testNonDefaultValue() throws IllegalArgumentException, IllegalAccessException { + + TestRecord record = new TestRecord(EnumType.value4); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("Enum value4: data hex: %s", hex)); + assertEquals("8C", hex); + } + + @Test public void testDefaultValue() throws IllegalArgumentException, IllegalAccessException { + + TestRecord record = new TestRecord(EnumType.value2); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("Enum value2: data hex: %s", hex)); + assertEquals("00", UperEncoder.hexStringFromBytes(encoded)); + } + + @Test public void testDecodeNonDefaultValue() throws IllegalArgumentException, IllegalAccessException { + + TestRecord record = new TestRecord(EnumType.value4); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("Enum value4: data hex: %s", hex)); + assertEquals("8C", hex); + + TestRecord result = UperEncoder.decode(encoded, TestRecord.class); + assertEquals(result.value,EnumType.value4); + } + + @Test public void testDecodeDefaultValue() throws IllegalArgumentException, IllegalAccessException { + + TestRecord record = new TestRecord(EnumType.value2); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("Enum value2: data hex: %s", hex)); + assertEquals("00", UperEncoder.hexStringFromBytes(encoded)); + + TestRecord result = UperEncoder.decode(encoded, TestRecord.class); + assertEquals(result.value,EnumType.value2); + } + +} diff --git a/src/test/java/org/uic/barcode/asn1/test/UperEncodeExtensionFieldOrderTest.java b/src/test/java/org/uic/barcode/asn1/test/UperEncodeExtensionFieldOrderTest.java new file mode 100644 index 0000000..ff97c27 --- /dev/null +++ b/src/test/java/org/uic/barcode/asn1/test/UperEncodeExtensionFieldOrderTest.java @@ -0,0 +1,100 @@ +package org.uic.barcode.asn1.test; + +import static org.junit.Assert.assertEquals; + +import java.util.logging.Level; + +import org.junit.Test; +import org.uic.barcode.asn1.datatypes.Asn1BigInteger; +import org.uic.barcode.asn1.datatypes.FieldOrder; +import org.uic.barcode.asn1.datatypes.HasExtensionMarker; +import org.uic.barcode.asn1.datatypes.IsExtension; +import org.uic.barcode.asn1.datatypes.Sequence; +import org.uic.barcode.asn1.uper.UperEncoder; + + +public class UperEncodeExtensionFieldOrderTest { + + /** + * Example from the Standard on UPER. + <pre> + TestRecord ::= [APPLICATION 0] IMPLICIT SEQUENCE { + number1 INTEGER, + ..., + number2 INTEGER, + number3 INTEGER + } + + value TestRecord ::= { + value1 12345678909999899, + value2 5555555555, + value3 32001 + } + +Encoding to the file 'data.uper' using PER UNALIGNED encoding rule... +TestRecord SEQUENCE [root fieldcount (not encoded) = 1] + value1 INTEGER [length = 7.0] + 12345678909999899 + value2 INTEGER [length = 5.0] + 5555555555 + value3 INTEGER [length = 2.0] + 32001 +Total encoded length = 20.2 +Encoded successfully in 21 bytes: +8395EE2A 2EF8858D 81C18140 52C8C338 C0C09F40 40 + + + </pre> + */ + @Sequence + @HasExtensionMarker + public static class TestRecord { + + @FieldOrder(order = 0) + Asn1BigInteger value1; + + @FieldOrder(order = 1) + @IsExtension + Asn1BigInteger value2; + + @FieldOrder(order = 2) + @IsExtension + Asn1BigInteger value3; + + public TestRecord() { + value1 = new Asn1BigInteger(12345678909999899L); + value2 = new Asn1BigInteger(5555555555L); + value3 = new Asn1BigInteger(32001L); + } + + + } + + + @Test public void test() throws IllegalArgumentException, IllegalAccessException { + + TestRecord record = new TestRecord(); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + assertEquals("8395EE2A2EF8858D81C1814052C8C338C0C09F4040",hex); + + + } + + @Test public void testDecode() throws IllegalArgumentException, IllegalAccessException { + + TestRecord record = new TestRecord(); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + assertEquals("8395EE2A2EF8858D81C1814052C8C338C0C09F4040",hex); + + TestRecord result = UperEncoder.decode(encoded, TestRecord.class); + assertEquals(result.value1.longValue(),record.value1.longValue()); + assertEquals(result.value2.longValue(),record.value2.longValue()); + assertEquals(result.value3.longValue(),record.value3.longValue()); + + } + +} diff --git a/src/test/java/org/uic/barcode/asn1/test/UperEncodeFieldOrderTest.java b/src/test/java/org/uic/barcode/asn1/test/UperEncodeFieldOrderTest.java new file mode 100644 index 0000000..5a23f24 --- /dev/null +++ b/src/test/java/org/uic/barcode/asn1/test/UperEncodeFieldOrderTest.java @@ -0,0 +1,66 @@ +package org.uic.barcode.asn1.test; + +import static org.junit.Assert.assertEquals; + +import java.util.logging.Level; + +import org.junit.Test; +import org.uic.barcode.asn1.datatypes.Asn1Optional; +import org.uic.barcode.asn1.datatypes.CharacterRestriction; +import org.uic.barcode.asn1.datatypes.FieldOrder; +import org.uic.barcode.asn1.datatypes.RestrictedString; +import org.uic.barcode.asn1.datatypes.Sequence; +import org.uic.barcode.asn1.uper.UperEncoder; + + +public class UperEncodeFieldOrderTest { + + /** + * Example from the Standard on UPER. + <pre> + World-Schema DEFINITIONS AUTOMATIC TAGS ::= + BEGIN + TestRecord ::= [APPLICATION 0] IMPLICIT SEQUENCE { + testString1 UTF8String OPTIONAL, + testString2 IA5String OPTIONAL + } + END + </pre> + */ + @Sequence + public static class TestRecord { + + @FieldOrder(order = 1) + @RestrictedString(CharacterRestriction.IA5String) + @Asn1Optional() String string2; + + @FieldOrder(order = 0) + @RestrictedString(CharacterRestriction.UTF8String) + @Asn1Optional() String string1; + + + public TestRecord() { + } + + public TestRecord(String utf8, String ia5) { + this.string1 = utf8; + this.string2 = ia5; + } + } + + + @Test public void test() throws IllegalArgumentException, IllegalAccessException { + + TestRecord record = new TestRecord("String1", "String2"); + byte[] encoded = UperEncoder.encode(record); + + TestRecord result = UperEncoder.decode(encoded, TestRecord.class); + assertEquals(result.string1,"String1"); + assertEquals(result.string2,"String2"); + } + + + + + +} diff --git a/src/test/java/org/uic/barcode/asn1/test/UperEncodeIntegerConstrainedTest.java b/src/test/java/org/uic/barcode/asn1/test/UperEncodeIntegerConstrainedTest.java new file mode 100644 index 0000000..575597f --- /dev/null +++ b/src/test/java/org/uic/barcode/asn1/test/UperEncodeIntegerConstrainedTest.java @@ -0,0 +1,71 @@ +package org.uic.barcode.asn1.test; + +import static org.junit.Assert.assertEquals; + +import java.util.logging.Level; + +import org.junit.Test; +import org.uic.barcode.asn1.datatypes.FieldOrder; +import org.uic.barcode.asn1.datatypes.IntRange; +import org.uic.barcode.asn1.datatypes.Sequence; +import org.uic.barcode.asn1.uper.UperEncoder; + + +public class UperEncodeIntegerConstrainedTest { + + /** + * Example from the Standard on UPER. + <pre> + TestRecord ::= [APPLICATION 0] IMPLICIT SEQUENCE { + number1 INTEGER (1..999), + number2 INTEGER (0..999), + number3 INTEGER (63..999) + } + </pre> + */ + @Sequence + public static class TestRecord { + + @FieldOrder(order = 0) + @IntRange(minValue=1, maxValue=999) + public Long value1; + + @FieldOrder(order = 1) + @IntRange(minValue=0, maxValue=999) + public Long value2; + + @FieldOrder(order = 2) + @IntRange(minValue=63, maxValue=999) + public Long value3; + + + public TestRecord() { + this(new Long(63L)); + } + + public TestRecord(Long num) { + value1 = num; + value2 = num; + value3 = num; + } + } + + + + @Test public void test() throws IllegalArgumentException, IllegalAccessException { + + TestRecord record = new TestRecord(63L); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + assertEquals("0F83F000",hex); + TestRecord result = UperEncoder.decode(encoded, TestRecord.class); + assertEquals(result.value1.longValue(),record.value1.longValue()); + assertEquals(result.value2.longValue(),record.value2.longValue()); + assertEquals(result.value3.longValue(),record.value3.longValue()); + + } + + + +} diff --git a/src/test/java/org/uic/barcode/asn1/test/UperEncodeIntegerExtensionTest.java b/src/test/java/org/uic/barcode/asn1/test/UperEncodeIntegerExtensionTest.java new file mode 100644 index 0000000..d0acd20 --- /dev/null +++ b/src/test/java/org/uic/barcode/asn1/test/UperEncodeIntegerExtensionTest.java @@ -0,0 +1,89 @@ +package org.uic.barcode.asn1.test; + +import static org.junit.Assert.assertEquals; + +import java.util.logging.Level; + +import org.junit.Test; +import org.uic.barcode.asn1.datatypes.Asn1BigInteger; +import org.uic.barcode.asn1.datatypes.FieldOrder; +import org.uic.barcode.asn1.datatypes.HasExtensionMarker; +import org.uic.barcode.asn1.datatypes.IsExtension; +import org.uic.barcode.asn1.datatypes.Sequence; +import org.uic.barcode.asn1.uper.UperEncoder; + + +public class UperEncodeIntegerExtensionTest { + + /** + * Example from the Standard on UPER. + <pre> + TestRecord ::= [APPLICATION 0] IMPLICIT SEQUENCE { + number1 INTEGER, + ..., + number2 INTEGER, + number3 INTEGER + } + + value TestRecord ::= { + value1 12345678909999899, + value2 5555555555, + value3 32001 + } + +Encoding to the file 'data.uper' using PER UNALIGNED encoding rule... +TestRecord SEQUENCE [root fieldcount (not encoded) = 1] + value1 INTEGER [length = 7.0] + 12345678909999899 + value2 INTEGER [length = 5.0] + 5555555555 + value3 INTEGER [length = 2.0] + 32001 +Total encoded length = 20.2 +Encoded successfully in 21 bytes: +8395EE2A 2EF8858D 81C18140 52C8C338 C0C09F40 40 + + + </pre> + */ + @Sequence + @HasExtensionMarker + public static class TestRecord { + + @FieldOrder(order = 0) + Asn1BigInteger value1; + + @FieldOrder(order = 2) + @IsExtension + Asn1BigInteger value3; + + @FieldOrder(order = 1) + @IsExtension + Asn1BigInteger value2; + + + public TestRecord() { + value1 = new Asn1BigInteger(12345678909999899L); + value2 = new Asn1BigInteger(5555555555L); + value3 = new Asn1BigInteger(32001L); + } + + + } + + + @Test public void test() throws IllegalArgumentException, IllegalAccessException { + + TestRecord record = new TestRecord(); + byte[] encoded = UperEncoder.encode(record); + TestRecord result = UperEncoder.decode(encoded, TestRecord.class); + assertEquals(result.value1.longValue(),record.value1.longValue()); + assertEquals(result.value2.longValue(),record.value2.longValue()); + assertEquals(result.value3.longValue(),record.value3.longValue()); + + + } + + + +} diff --git a/src/test/java/org/uic/barcode/asn1/test/UperEncodeIntegerSmallTest.java b/src/test/java/org/uic/barcode/asn1/test/UperEncodeIntegerSmallTest.java new file mode 100644 index 0000000..5ada54d --- /dev/null +++ b/src/test/java/org/uic/barcode/asn1/test/UperEncodeIntegerSmallTest.java @@ -0,0 +1,130 @@ +package org.uic.barcode.asn1.test; + +import static org.junit.Assert.assertEquals; + +import java.util.logging.Level; + +import org.junit.Test; +import org.uic.barcode.asn1.datatypes.FieldOrder; +import org.uic.barcode.asn1.datatypes.Sequence; +import org.uic.barcode.asn1.uper.UperEncoder; + + +public class UperEncodeIntegerSmallTest { + + /** + * Example from the Standard on UPER. + <pre> + TestRecord ::= [APPLICATION 0] IMPLICIT SEQUENCE { + number1 INTEGER, + number2 INTEGER + } + </pre> + */ + @Sequence + public static class TestRecord { + + @FieldOrder(order = 0) + public Long value1; + + @FieldOrder(order = 1) + public Integer value2; + + public TestRecord() { + this(new Long(12345678909999899L)); + } + + public TestRecord(Long num) { + value1 = num; + value2 = Integer.valueOf(num.intValue()); + } + } + + + + @Test public void test1() throws IllegalArgumentException, IllegalAccessException { + + TestRecord record = new TestRecord(1L); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + assertEquals("01010101",hex); + + TestRecord result = UperEncoder.decode(encoded, TestRecord.class); + assertEquals(result.value1.longValue(),record.value1.longValue()); + assertEquals(result.value2.longValue(),record.value2.longValue()); + + } + + @Test public void test16() throws IllegalArgumentException, IllegalAccessException { + + TestRecord record = new TestRecord(16L); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + assertEquals("01100110",hex); + + TestRecord result = UperEncoder.decode(encoded, TestRecord.class); + assertEquals(result.value1.longValue(),record.value1.longValue()); + assertEquals(result.value2.longValue(),record.value2.longValue()); + + } + + + @Test public void test63() throws IllegalArgumentException, IllegalAccessException { + + TestRecord record = new TestRecord(63L); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + assertEquals("013F013F",hex); + + TestRecord result = UperEncoder.decode(encoded, TestRecord.class); + assertEquals(result.value1.longValue(),record.value1.longValue()); + assertEquals(result.value2.longValue(),record.value2.longValue()); + + } + + @Test public void test64() throws IllegalArgumentException, IllegalAccessException { + + TestRecord record = new TestRecord(64L); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + assertEquals("01400140",hex); + + TestRecord result = UperEncoder.decode(encoded, TestRecord.class); + assertEquals(result.value1.longValue(),record.value1.longValue()); + assertEquals(result.value2.longValue(),record.value2.longValue()); + + } + + @Test public void test127() throws IllegalArgumentException, IllegalAccessException { + + TestRecord record = new TestRecord(127L); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + assertEquals("017F017F",hex); + + TestRecord result = UperEncoder.decode(encoded, TestRecord.class); + assertEquals(result.value1.longValue(),record.value1.longValue()); + assertEquals(result.value2.longValue(),record.value2.longValue()); + + } + + @Test public void test128() throws IllegalArgumentException, IllegalAccessException { + + TestRecord record = new TestRecord(128L); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + assertEquals("020080020080",hex); + + TestRecord result = UperEncoder.decode(encoded, TestRecord.class); + assertEquals(result.value1.longValue(),record.value1.longValue()); + assertEquals(result.value2.longValue(),record.value2.longValue()); + + } + +} diff --git a/src/test/java/org/uic/barcode/asn1/test/UperEncodeIntegerTest.java b/src/test/java/org/uic/barcode/asn1/test/UperEncodeIntegerTest.java new file mode 100644 index 0000000..d57fd00 --- /dev/null +++ b/src/test/java/org/uic/barcode/asn1/test/UperEncodeIntegerTest.java @@ -0,0 +1,64 @@ +package org.uic.barcode.asn1.test; + +import static org.junit.Assert.assertEquals; + +import java.util.logging.Level; + +import org.junit.Test; +import org.uic.barcode.asn1.datatypes.Asn1BigInteger; +import org.uic.barcode.asn1.datatypes.FieldOrder; +import org.uic.barcode.asn1.datatypes.Sequence; +import org.uic.barcode.asn1.uper.UperEncoder; + + +public class UperEncodeIntegerTest { + + /** + * Example from the Standard on UPER. + <pre> + TestRecord ::= [APPLICATION 0] IMPLICIT SEQUENCE { + number INTEGER, + } + </pre> + */ + @Sequence + public static class TestRecord { + + @FieldOrder(order = 0) + Asn1BigInteger value; + + public TestRecord() { + this(new Long(12345678909999899L)); + } + + public TestRecord(Long num) { + value = new Asn1BigInteger(num); + } + } + + + + @Test public void test() throws IllegalArgumentException, IllegalAccessException { + + TestRecord record = new TestRecord(12345678909999899L); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + assertEquals("072BDC545DF10B1B",hex); + + } + + @Test public void testDecode() throws IllegalArgumentException, IllegalAccessException { + + TestRecord record = new TestRecord(12345678909999899L); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + assertEquals("072BDC545DF10B1B",hex); + + TestRecord result = UperEncoder.decode(encoded, TestRecord.class); + assertEquals(result.value.longValue(),record.value.longValue()); + + } + +} diff --git a/src/test/java/org/uic/barcode/asn1/test/UperEncodeObjectIdentifierTest.java b/src/test/java/org/uic/barcode/asn1/test/UperEncodeObjectIdentifierTest.java new file mode 100644 index 0000000..edf3348 --- /dev/null +++ b/src/test/java/org/uic/barcode/asn1/test/UperEncodeObjectIdentifierTest.java @@ -0,0 +1,76 @@ +package org.uic.barcode.asn1.test;
+
+import static org.junit.Assert.assertEquals;
+import java.util.logging.Level;
+
+import org.junit.jupiter.api.Test;
+import org.uic.barcode.asn1.datatypes.Asn1Optional;
+import org.uic.barcode.asn1.datatypes.CharacterRestriction;
+import org.uic.barcode.asn1.datatypes.FieldOrder;
+import org.uic.barcode.asn1.datatypes.RestrictedString;
+import org.uic.barcode.asn1.datatypes.Sequence;
+import org.uic.barcode.asn1.uper.UperEncoder;
+
+class UperEncodeObjectIdentifierTest {
+
+
+ /**
+ * Example from the Standard on UPER.
+ <pre>
+ TestRecord ::= [APPLICATION 0] IMPLICIT SEQUENCE {
+ value1 OBJECT IDENTIFIER,
+ value2 OBJECT IDENTIFIER,
+ value3 OBJECT IDENTIFIER
+ }
+
+ value TestRecord ::= {
+ value1 2.16.840.1.101.3.4.3.1,
+ value2 2.16.840.1.101.3.4.3.2,
+ value3 1.2.840.10045.3.1.7
+ }
+ </pre>
+ */
+
+ @Sequence
+ public static class TestRecord {
+
+ @FieldOrder(order = 0)
+ @RestrictedString(CharacterRestriction.ObjectIdentifier)
+ String value1 = "2.16.840.1.101.3.4.3.1"; //DSA SHA224
+
+ @FieldOrder(order = 1)
+ @RestrictedString(CharacterRestriction.ObjectIdentifier)
+ String value2 = "2.16.840.1.101.3.4.3.2"; //DSA SHA248
+
+ @FieldOrder(order = 2)
+ @RestrictedString(CharacterRestriction.ObjectIdentifier)
+ String value3 = "1.2.840.10045.3.1.7"; //ECC
+
+ public TestRecord() {}
+
+ }
+
+ @Test
+ public void testEncode() throws IllegalArgumentException, IllegalAccessException {
+ TestRecord record = new TestRecord();
+ byte[] encoded = UperEncoder.encode(record);
+ String hex = UperEncoder.hexStringFromBytes(encoded);
+ UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex));
+ assertEquals("0960864801650304030109608648016503040302082A8648CE3D030107",hex);
+ }
+
+ @Test
+ public void testDecode() throws IllegalArgumentException, IllegalAccessException {
+ TestRecord record = new TestRecord();
+ byte[] encoded = UperEncoder.encode(record);
+ String hex = UperEncoder.hexStringFromBytes(encoded);
+ UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex));
+ assertEquals("0960864801650304030109608648016503040302082A8648CE3D030107",hex);
+ TestRecord result = UperEncoder.decode(encoded, TestRecord.class);
+ assertEquals(result.value1,record.value1);
+ assertEquals(result.value2,record.value2);
+ assertEquals(result.value3,record.value3);
+ }
+
+
+}
diff --git a/src/test/java/org/uic/barcode/asn1/test/UperEncodeOctetStringTest.java b/src/test/java/org/uic/barcode/asn1/test/UperEncodeOctetStringTest.java new file mode 100644 index 0000000..af2a42e --- /dev/null +++ b/src/test/java/org/uic/barcode/asn1/test/UperEncodeOctetStringTest.java @@ -0,0 +1,80 @@ +package org.uic.barcode.asn1.test; + +import static org.junit.Assert.assertEquals; + +import java.util.logging.Level; + +import org.junit.Test; +import org.uic.barcode.asn1.datatypes.FieldOrder; +import org.uic.barcode.asn1.datatypes.Sequence; +import org.uic.barcode.asn1.datatypesimpl.OctetString; +import org.uic.barcode.asn1.uper.UperEncoder; + + +public class UperEncodeOctetStringTest { + + /** + * Example from the Standard on UPER. + <pre> + World-Schema DEFINITIONS AUTOMATIC TAGS ::= + BEGIN + TestRecord ::= [APPLICATION 0] IMPLICIT SEQUENCE { + value OCTET STRING + } + END + + value TestRecord ::= { value '83DA'H } + + Encoding to the file 'data.uper' using PER UNALIGNED encoding rule... + TestRecord SEQUENCE [fieldcount (not encoded) = 1] + value OCTET STRING [length = 2.0] + 0x83da + Total encoded length = 3.0 + Encoded successfully in 3 bytes: + 0283DA + + </pre> + */ + @Sequence + public static class TestRecord { + + @FieldOrder(order = 0) + OctetString value; + + public TestRecord() { + value = new OctetString(); + value.add(hexToByte("83")); + value.add(hexToByte("DA")); + } + } + + + @Test public void test() throws IllegalArgumentException, IllegalAccessException { + + TestRecord record = new TestRecord(); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + assertEquals("0283DA",hex); + + } + + @Test public void testDecode() throws IllegalArgumentException, IllegalAccessException { + + TestRecord record = new TestRecord(); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + assertEquals("0283DA",hex); + + TestRecord result = UperEncoder.decode(encoded, TestRecord.class); + assertEquals(result.value,record.value); + + } + + public static byte hexToByte(String s){ + return (byte) ((Character.digit(s.charAt(0), 16) << 4) + + Character.digit(s.charAt(1), 16)); + } + +} diff --git a/src/test/java/org/uic/barcode/asn1/test/UperEncodeOptionalSequenceExtensionTest.java b/src/test/java/org/uic/barcode/asn1/test/UperEncodeOptionalSequenceExtensionTest.java new file mode 100644 index 0000000..55e6026 --- /dev/null +++ b/src/test/java/org/uic/barcode/asn1/test/UperEncodeOptionalSequenceExtensionTest.java @@ -0,0 +1,118 @@ +package org.uic.barcode.asn1.test; + +import static org.junit.Assert.assertEquals; + +import java.util.logging.Level; + +import org.junit.Test; +import org.uic.barcode.asn1.datatypes.Asn1Optional; +import org.uic.barcode.asn1.datatypes.CharacterRestriction; +import org.uic.barcode.asn1.datatypes.FieldOrder; +import org.uic.barcode.asn1.datatypes.HasExtensionMarker; +import org.uic.barcode.asn1.datatypes.IsExtension; +import org.uic.barcode.asn1.datatypes.RestrictedString; +import org.uic.barcode.asn1.datatypes.Sequence; +import org.uic.barcode.asn1.uper.UperEncoder; + + +public class UperEncodeOptionalSequenceExtensionTest { + + /** Example for extended sequence including extension + World-Schema DEFINITIONS AUTOMATIC TAGS ::= + BEGIN + TestRecord ::= [APPLICATION 0] IMPLICIT SEQUENCE { + value1 IA5String, + ..., + value2 IA5String OPTIONAL + } + END + */ + @Sequence + @HasExtensionMarker + public static class TestRecordExtended { + + @FieldOrder(order = 0) + @RestrictedString(CharacterRestriction.IA5String) + String value1; + + @FieldOrder(order = 1) + @IsExtension + @RestrictedString(CharacterRestriction.IA5String) + @Asn1Optional() String value2; + + public TestRecordExtended() { } + + public void setValue1(String value1) { + this.value1 = value1; + } + + public void setValue2(String value2) { + this.value2 = value2; + } + + + + } + + /** Example for extended sequence + TestRecord ::= [APPLICATION 0] IMPLICIT SEQUENCE { + value1 IA5String, + ,... + } + */ + @Sequence + @HasExtensionMarker + public static class TestRecord { + + @RestrictedString(CharacterRestriction.IA5String) + String value1 = "regular"; + public TestRecord() { } + } + + + @Test public void testEncode() throws IllegalArgumentException, IllegalAccessException { + TestRecordExtended record = new TestRecordExtended(); + record.setValue1("regular"); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + assertEquals("03F2CB9FAECC3C80",hex); + } + + @Test public void testEncodeWithoutOptionalElement() throws IllegalArgumentException, IllegalAccessException { + TestRecordExtended record = new TestRecordExtended(); + record.setValue1("regular"); + record.setValue2("extension"); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + assertEquals("83F2CB9FAECC3C80424272F8E997773D3BF700",hex); + } + + @Test public void testDecodeExtended() throws IllegalArgumentException, IllegalAccessException { + TestRecordExtended record = new TestRecordExtended(); + record.setValue1("regular"); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + assertEquals("03F2CB9FAECC3C80",hex); + TestRecordExtended result = UperEncoder.decode(encoded, TestRecordExtended.class); + assertEquals(result.value1,record.value1); + } + + @Test public void testDecode() throws IllegalArgumentException, IllegalAccessException { + TestRecordExtended record = new TestRecordExtended(); + record.setValue1("regular"); + record.setValue2("extension"); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + assertEquals("83F2CB9FAECC3C80424272F8E997773D3BF700",hex); + TestRecord result = UperEncoder.decode(encoded, TestRecord.class); + assertEquals(result.value1,record.value1); + } + + + + +} diff --git a/src/test/java/org/uic/barcode/asn1/test/UperEncodeRestrictedIntegerTest.java b/src/test/java/org/uic/barcode/asn1/test/UperEncodeRestrictedIntegerTest.java new file mode 100644 index 0000000..bc82621 --- /dev/null +++ b/src/test/java/org/uic/barcode/asn1/test/UperEncodeRestrictedIntegerTest.java @@ -0,0 +1,62 @@ +package org.uic.barcode.asn1.test; + +import static org.junit.Assert.assertEquals; + +import java.util.logging.Level; + +import org.junit.Test; +import org.uic.barcode.asn1.datatypes.FieldOrder; +import org.uic.barcode.asn1.datatypes.IntRange; +import org.uic.barcode.asn1.datatypes.Sequence; +import org.uic.barcode.asn1.uper.UperEncoder; + + +public class UperEncodeRestrictedIntegerTest { + + /** + * Example from the Standard on UPER. + <pre> +TestRecord ::= [APPLICATION 0] IMPLICIT SEQUENCE { + number INTEGER(32000..63000), +} + </pre> + */ + @Sequence + public static class TestRecord { + + @FieldOrder(order = 0) + @IntRange(maxValue = 63000, minValue = 33000) + Long value; + + public TestRecord() { + this(new Long(33005)); + } + + public TestRecord(Long num) { + value = num; + } + } + + + @Test public void test() throws IllegalArgumentException, IllegalAccessException { + + TestRecord record = new TestRecord(33005L); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + assertEquals("000A",hex); + } + + @Test public void testDecode() throws IllegalArgumentException, IllegalAccessException { + + TestRecord record = new TestRecord(33005L); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + assertEquals("000A",hex); + + TestRecord result = UperEncoder.decode(encoded, TestRecord.class); + assertEquals(result.value,record.value); + } + +} diff --git a/src/test/java/org/uic/barcode/asn1/test/UperEncodeSequenceExtensionTest.java b/src/test/java/org/uic/barcode/asn1/test/UperEncodeSequenceExtensionTest.java new file mode 100644 index 0000000..29d203b --- /dev/null +++ b/src/test/java/org/uic/barcode/asn1/test/UperEncodeSequenceExtensionTest.java @@ -0,0 +1,93 @@ +package org.uic.barcode.asn1.test; + +import static org.junit.Assert.assertEquals; + +import java.util.logging.Level; + +import org.junit.Test; +import org.uic.barcode.asn1.datatypes.Asn1Optional; +import org.uic.barcode.asn1.datatypes.CharacterRestriction; +import org.uic.barcode.asn1.datatypes.FieldOrder; +import org.uic.barcode.asn1.datatypes.HasExtensionMarker; +import org.uic.barcode.asn1.datatypes.IsExtension; +import org.uic.barcode.asn1.datatypes.RestrictedString; +import org.uic.barcode.asn1.datatypes.Sequence; +import org.uic.barcode.asn1.uper.UperEncoder; + + +public class UperEncodeSequenceExtensionTest { + + /** Example for extended sequence including extension + TestRecord ::= [APPLICATION 0] IMPLICIT SEQUENCE { + value1 IA5String, + ,... + value2 IA5String + } + */ + @Sequence + @HasExtensionMarker + public static class TestRecordExtended { + + @FieldOrder(order = 0) + @RestrictedString(CharacterRestriction.IA5String) + @Asn1Optional() String value1 = "regular"; + + @FieldOrder(order = 1) + @IsExtension + @RestrictedString(CharacterRestriction.IA5String) + @Asn1Optional() String value2 = "extension"; + + + public TestRecordExtended() { } + } + + /** Example for extended sequence + TestRecord ::= [APPLICATION 0] IMPLICIT SEQUENCE { + value1 IA5String, + ,... + } + */ + @Sequence + @HasExtensionMarker + public static class TestRecord { + + @RestrictedString(CharacterRestriction.IA5String) + @Asn1Optional() String value1 = "regular"; + + public TestRecord() { } + } + + + @Test public void testEncode() throws IllegalArgumentException, IllegalAccessException { + TestRecordExtended record = new TestRecordExtended(); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + assertEquals("C1F965CFD7661E402121397C74CBBB9E9DFB80",hex); + } + + @Test public void testDecodeExtended() throws IllegalArgumentException, IllegalAccessException { + TestRecordExtended record = new TestRecordExtended(); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + assertEquals("C1F965CFD7661E402121397C74CBBB9E9DFB80",hex); + TestRecordExtended result = UperEncoder.decode(encoded, TestRecordExtended.class); + assertEquals(result.value1,record.value1); + assertEquals(result.value2,record.value2); + } + + @Test public void testDecode() throws IllegalArgumentException, IllegalAccessException { + TestRecordExtended record = new TestRecordExtended(); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + assertEquals("C1F965CFD7661E402121397C74CBBB9E9DFB80",hex); + TestRecord result = UperEncoder.decode(encoded, TestRecord.class); + assertEquals(result.value1,record.value1); + } + + + + +} diff --git a/src/test/java/org/uic/barcode/asn1/test/UperEncodeSequenceOfIntegerTest.java b/src/test/java/org/uic/barcode/asn1/test/UperEncodeSequenceOfIntegerTest.java new file mode 100644 index 0000000..9194dca --- /dev/null +++ b/src/test/java/org/uic/barcode/asn1/test/UperEncodeSequenceOfIntegerTest.java @@ -0,0 +1,72 @@ +package org.uic.barcode.asn1.test; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Level; + +import org.junit.Test; +import org.uic.barcode.asn1.datatypes.FieldOrder; +import org.uic.barcode.asn1.datatypes.Sequence; +import org.uic.barcode.asn1.datatypesimpl.SequenceOfUnrestrictedLong; +import org.uic.barcode.asn1.uper.UperEncoder; + + +public class UperEncodeSequenceOfIntegerTest { + + /** + * Example from the Standard on UPER. + <pre> +TestRecord ::= [APPLICATION 0] IMPLICIT SEQUENCE { + number INTEGER, +} + </pre> + */ + @Sequence + public static class TestRecord { + + @FieldOrder(order = 0) + SequenceOfUnrestrictedLong numbers; + + public TestRecord() { + } + + public TestRecord(List<Long> nums) { + numbers = new SequenceOfUnrestrictedLong(nums); + } + } + + + @Test public void test() throws IllegalArgumentException, IllegalAccessException { + + ArrayList<Long> nums = new ArrayList<Long>(); + nums.add(new Long(12345678909999899L)); + nums.add(new Long(12345678909999899L)); + TestRecord record = new TestRecord(nums); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", UperEncoder.hexStringFromBytes(encoded))); + assertEquals("02072BDC545DF10B1B072BDC545DF10B1B",hex); + + } + + @Test public void testDecode() throws IllegalArgumentException, IllegalAccessException { + + ArrayList<Long> nums = new ArrayList<Long>(); + nums.add(new Long(12345678909999899L)); + nums.add(new Long(12345678909999899L)); + TestRecord record = new TestRecord(nums); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", UperEncoder.hexStringFromBytes(encoded))); + assertEquals("02072BDC545DF10B1B072BDC545DF10B1B",hex); + + TestRecord result = UperEncoder.decode(encoded, TestRecord.class); + assertEquals(result.numbers.get(0).longValue(),record.numbers.get(0).longValue()); + assertEquals(result.numbers.get(1).longValue(),record.numbers.get(1).longValue()); + + + } + +} diff --git a/src/test/java/org/uic/barcode/asn1/test/UperEncodeSequenceOfRestrictedIntegerTest.java b/src/test/java/org/uic/barcode/asn1/test/UperEncodeSequenceOfRestrictedIntegerTest.java new file mode 100644 index 0000000..d1834d0 --- /dev/null +++ b/src/test/java/org/uic/barcode/asn1/test/UperEncodeSequenceOfRestrictedIntegerTest.java @@ -0,0 +1,77 @@ +package org.uic.barcode.asn1.test; + +import static org.junit.Assert.assertEquals; + +import java.util.logging.Level; + +import org.junit.Test; +import org.uic.barcode.asn1.datatypes.FieldOrder; +import org.uic.barcode.asn1.datatypes.IntRange; +import org.uic.barcode.asn1.datatypes.Sequence; +import org.uic.barcode.asn1.uper.UperEncoder; + + +public class UperEncodeSequenceOfRestrictedIntegerTest { + + /** + * Example from the Standard on UPER. + <pre> +TestRecord ::= [APPLICATION 0] IMPLICIT SEQUENCE { + numbers SEQUENCE OF INTEGER(0..9999999), +} + </pre> + */ + @Sequence + public static class TestRecord { + + @FieldOrder(order = 0) + @IntRange(minValue=9500000,maxValue=99900001) + TestSequenceOfLong numbers = null;; + + public TestRecord() { + } + + public void addNumber(Long longValue){ + if (numbers == null) { + numbers = new TestSequenceOfLong(); + } + numbers.add(longValue); + } + } + + + @Test public void test() throws IllegalArgumentException, IllegalAccessException { + + TestRecord record = new TestRecord(); + + record.addNumber(new Long(9500001L)); + record.addNumber(new Long(9699999L)); + + + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", UperEncoder.hexStringFromBytes(encoded))); + assertEquals("02000000200C34FC",hex); + } + + @Test public void testDecode() throws IllegalArgumentException, IllegalAccessException { + + TestRecord record = new TestRecord(); + + record.addNumber(new Long(9500001L)); + record.addNumber(new Long(9699999L)); + + + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", UperEncoder.hexStringFromBytes(encoded))); + assertEquals("02000000200C34FC",hex); + + + TestRecord result = UperEncoder.decode(encoded, TestRecord.class); + assertEquals(result.numbers.get(0).longValue(),record.numbers.get(0).longValue()); + assertEquals(result.numbers.get(1).longValue(),record.numbers.get(1).longValue()); + } + + +} diff --git a/src/test/java/org/uic/barcode/asn1/test/UperEncodeSequenceOfStringListTest.java b/src/test/java/org/uic/barcode/asn1/test/UperEncodeSequenceOfStringListTest.java new file mode 100644 index 0000000..49350cc --- /dev/null +++ b/src/test/java/org/uic/barcode/asn1/test/UperEncodeSequenceOfStringListTest.java @@ -0,0 +1,78 @@ +package org.uic.barcode.asn1.test; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Level; + +import org.junit.Test; +import org.uic.barcode.asn1.datatypes.CharacterRestriction; +import org.uic.barcode.asn1.datatypes.FieldOrder; +import org.uic.barcode.asn1.datatypes.RestrictedString; +import org.uic.barcode.asn1.datatypes.Sequence; +import org.uic.barcode.asn1.uper.UperEncoder; + + +public class UperEncodeSequenceOfStringListTest { + + /** + * Example from the Standard on UPER. + <pre> + TestRecord ::= [APPLICATION 0] IMPLICIT SEQUENCE { + strings SEQUENCE OF IA5String, + } + </pre> + */ + @Sequence + public static class TestRecord { + + @FieldOrder(order = 0) + @RestrictedString(CharacterRestriction.IA5String) + ArrayList<String> strings = new ArrayList<String>(); + + public TestRecord() { + } + + public List<String> getStrings() { + return strings; + } + + + } + + + @Test public void test() throws IllegalArgumentException, IllegalAccessException { + + TestRecord record = new TestRecord(); + record.getStrings().add("test1"); + record.getStrings().add("test2"); + record.getStrings().add("test3"); + + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", UperEncoder.hexStringFromBytes(encoded))); + assertEquals("0305E9979F4620BD32F3E8C817A65E7D1980",hex); + } + + @Test public void testDecode() throws IllegalArgumentException, IllegalAccessException { + + TestRecord record = new TestRecord(); + record.getStrings().add("test1"); + record.getStrings().add("test2"); + record.getStrings().add("test3"); + + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", UperEncoder.hexStringFromBytes(encoded))); + assertEquals("0305E9979F4620BD32F3E8C817A65E7D1980",hex); + + TestRecord result = UperEncoder.decode(encoded, TestRecord.class, null); + assert(result.getStrings().contains("test1")); + assert(result.getStrings().contains("test2")); + assert(result.getStrings().contains("test3")); + + + } + +} diff --git a/src/test/java/org/uic/barcode/asn1/test/UperEncodeSequenceOfStringTest.java b/src/test/java/org/uic/barcode/asn1/test/UperEncodeSequenceOfStringTest.java new file mode 100644 index 0000000..f8eccc6 --- /dev/null +++ b/src/test/java/org/uic/barcode/asn1/test/UperEncodeSequenceOfStringTest.java @@ -0,0 +1,76 @@ +package org.uic.barcode.asn1.test; + +import static org.junit.Assert.assertEquals; + +import java.util.logging.Level; + +import org.junit.Test; +import org.uic.barcode.asn1.datatypes.CharacterRestriction; +import org.uic.barcode.asn1.datatypes.FieldOrder; +import org.uic.barcode.asn1.datatypes.RestrictedString; +import org.uic.barcode.asn1.datatypes.Sequence; +import org.uic.barcode.asn1.datatypesimpl.SequenceOfStringIA5; +import org.uic.barcode.asn1.uper.UperEncoder; + + +public class UperEncodeSequenceOfStringTest { + + /** + * Example from the Standard on UPER. + <pre> + TestRecord ::= [APPLICATION 0] IMPLICIT SEQUENCE { + strings SEQUENCE OF IA5String, + } + </pre> + */ + @Sequence + public static class TestRecord { + + @FieldOrder(order = 0) + SequenceOfStringIA5 strings = new SequenceOfStringIA5(); + + public TestRecord() { + } + + public SequenceOfStringIA5 getStrings() { + return strings; + } + + + } + + + @Test public void test() throws IllegalArgumentException, IllegalAccessException { + + TestRecord record = new TestRecord(); + record.getStrings().add("test1"); + record.getStrings().add("test2"); + record.getStrings().add("test3"); + + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", UperEncoder.hexStringFromBytes(encoded))); + assertEquals("0305E9979F4620BD32F3E8C817A65E7D1980",hex); + } + + @Test public void testDecode() throws IllegalArgumentException, IllegalAccessException { + + TestRecord record = new TestRecord(); + record.getStrings().add("test1"); + record.getStrings().add("test2"); + record.getStrings().add("test3"); + + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", UperEncoder.hexStringFromBytes(encoded))); + assertEquals("0305E9979F4620BD32F3E8C817A65E7D1980",hex); + + TestRecord result = UperEncoder.decode(encoded, TestRecord.class); + assert(result.getStrings().contains("test1")); + assert(result.getStrings().contains("test2")); + assert(result.getStrings().contains("test3")); + + + } + +} diff --git a/src/test/java/org/uic/barcode/asn1/test/UperEncodeSequenceOfUtf8StringTest.java b/src/test/java/org/uic/barcode/asn1/test/UperEncodeSequenceOfUtf8StringTest.java new file mode 100644 index 0000000..ab8fa2a --- /dev/null +++ b/src/test/java/org/uic/barcode/asn1/test/UperEncodeSequenceOfUtf8StringTest.java @@ -0,0 +1,96 @@ +package org.uic.barcode.asn1.test; + +import static org.junit.Assert.assertEquals; + +import java.util.logging.Level; + +import org.junit.Test; +import org.uic.barcode.asn1.datatypes.FieldOrder; +import org.uic.barcode.asn1.datatypes.Sequence; +import org.uic.barcode.asn1.datatypesimpl.SequenceOfStringUTF8; +import org.uic.barcode.asn1.uper.UperEncoder; + + +public class UperEncodeSequenceOfUtf8StringTest { + + /** + * Example from the Standard on UPER. + <pre> + World-Schema DEFINITIONS AUTOMATIC TAGS ::= + BEGIN + TestRecord ::= SEQUENCE { + strings SEQUENCE OF UTF8String + } + END + + value TestRecord ::= { + strings {"test1" , "test2" , "test3" } + } + + +Encoding to the file 'data.uper' using PER UNALIGNED encoding rule... +TestRecord SEQUENCE [fieldcount (not encoded) = 1] + strings SEQUENCE OF [count = 3] + UTF8String [length = 5.0] + 0x7465737431 + UTF8String [length = 5.0] + 0x7465737432 + UTF8String [length = 5.0] + 0x7465737433 +Total encoded length = 19.0 +Encoded successfully in 19 bytes: +03057465 73743105 74657374 32057465 737433 + + </pre> + */ + @Sequence + public static class TestRecord { + + @FieldOrder(order = 0) + SequenceOfStringUTF8 strings = new SequenceOfStringUTF8(); + + public TestRecord() { + } + + public SequenceOfStringUTF8 getStrings() { + return strings; + } + + + } + + + @Test public void test() throws IllegalArgumentException, IllegalAccessException { + + TestRecord record = new TestRecord(); + record.getStrings().add("test1"); + record.getStrings().add("test2"); + record.getStrings().add("test3"); + + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", UperEncoder.hexStringFromBytes(encoded))); + assertEquals("03057465737431057465737432057465737433",hex); + } + + @Test public void testDecode() throws IllegalArgumentException, IllegalAccessException { + + TestRecord record = new TestRecord(); + record.getStrings().add("test1"); + record.getStrings().add("test2"); + record.getStrings().add("test3"); + + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", UperEncoder.hexStringFromBytes(encoded))); + assertEquals("03057465737431057465737432057465737433",hex); + + TestRecord result = UperEncoder.decode(encoded, TestRecord.class); + assert(result.getStrings().contains("test1")); + assert(result.getStrings().contains("test2")); + assert(result.getStrings().contains("test3")); + + + } + +} diff --git a/src/test/java/org/uic/barcode/asn1/test/UperEncodeStringDefaultTest.java b/src/test/java/org/uic/barcode/asn1/test/UperEncodeStringDefaultTest.java new file mode 100644 index 0000000..d8e5029 --- /dev/null +++ b/src/test/java/org/uic/barcode/asn1/test/UperEncodeStringDefaultTest.java @@ -0,0 +1,76 @@ +package org.uic.barcode.asn1.test; + +import static org.junit.Assert.assertEquals; + +import java.util.logging.Level; + +import org.junit.Test; +import org.uic.barcode.asn1.datatypes.Asn1Default; +import org.uic.barcode.asn1.datatypes.Asn1Optional; +import org.uic.barcode.asn1.datatypes.CharacterRestriction; +import org.uic.barcode.asn1.datatypes.FieldOrder; +import org.uic.barcode.asn1.datatypes.RestrictedString; +import org.uic.barcode.asn1.datatypes.Sequence; +import org.uic.barcode.asn1.uper.UperEncoder; + + +public class UperEncodeStringDefaultTest { + + /** + * Example from the Standard on UPER. + <pre> + TestRecord ::= [APPLICATION 0] IMPLICIT SEQUENCE { + testString1 UTF8String OPTIONAL, + testString2 IA5String DEFAULT("TestString") + } + </pre> + */ + @Sequence + public static class TestRecord { + + @FieldOrder(order = 0) + @RestrictedString(CharacterRestriction.UTF8String) + @Asn1Optional() String valueUtf8; + + @FieldOrder(order = 1) + @RestrictedString(CharacterRestriction.IA5String) + @Asn1Default(value="testString") String valueIA5; + + public TestRecord() { + } + + public TestRecord(String utf8, String ia5) { + this.valueUtf8 = utf8; + this.valueIA5 = ia5; + } + } + + + @Test public void testEncode() throws IllegalArgumentException, IllegalAccessException { + TestRecord record = new TestRecord("Müller", "testString"); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + assertEquals("81D370EF1B1B195C80",hex); + } + + @Test public void testEncodeDefault() throws IllegalArgumentException, IllegalAccessException { + TestRecord record = new TestRecord("Müller", "testString"); + byte[] encoded = UperEncoder.encode(record); + TestRecord result = UperEncoder.decode(encoded, TestRecord.class); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + assertEquals(result.valueIA5,"testString"); + } + + @Test public void testEncodeDefault2() throws IllegalArgumentException, IllegalAccessException { + TestRecord record = new TestRecord("Müller", null); + byte[] encoded = UperEncoder.encode(record); + TestRecord result = UperEncoder.decode(encoded, TestRecord.class); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + assertEquals(result.valueIA5,"testString"); + } + + +} diff --git a/src/test/java/org/uic/barcode/asn1/test/UperEncodeStringLengthTest.java b/src/test/java/org/uic/barcode/asn1/test/UperEncodeStringLengthTest.java new file mode 100644 index 0000000..9dd4389 --- /dev/null +++ b/src/test/java/org/uic/barcode/asn1/test/UperEncodeStringLengthTest.java @@ -0,0 +1,151 @@ +package org.uic.barcode.asn1.test; + +import static org.junit.Assert.assertEquals; + +import java.util.logging.Level; + +import org.junit.Test; +import org.uic.barcode.asn1.datatypes.Asn1Optional; +import org.uic.barcode.asn1.datatypes.CharacterRestriction; +import org.uic.barcode.asn1.datatypes.FieldOrder; +import org.uic.barcode.asn1.datatypes.RestrictedString; +import org.uic.barcode.asn1.datatypes.Sequence; +import org.uic.barcode.asn1.uper.UperEncoder; + + +public class UperEncodeStringLengthTest { + + /** + * Example from the Standard on UPER. + <pre> + World-Schema DEFINITIONS AUTOMATIC TAGS ::= + BEGIN + TestRecord ::= [APPLICATION 0] IMPLICIT SEQUENCE { + testString1 UTF8String OPTIONAL + } + END + + value TestRecord ::= { + testString1 "A" + } + + </pre> + */ + + + + @Sequence + public static class TestRecord { + + @FieldOrder(order = 0) + @RestrictedString(CharacterRestriction.UTF8String) + @Asn1Optional() String valueUtf8; + + public TestRecord() { + } + + public TestRecord(String utf8) { + this.valueUtf8 = utf8; + } + } + + + @Test public void testEncode1() throws IllegalArgumentException, IllegalAccessException { + TestRecord record = new TestRecord("A"); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + assertEquals("80A080",hex); + TestRecord result = UperEncoder.decode(encoded, TestRecord.class); + assertEquals(record.valueUtf8,result.valueUtf8); + } + + + @Test public void testEncode63() throws IllegalArgumentException, IllegalAccessException { + TestRecord record = new TestRecord("123456789012345678901234567890123456789012345678901234567890123"); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + assertEquals("9F9899199A1A9B1B9C1C981899199A1A9B1B9C1C981899199A1A9B1B9C1C981899199A1A9B1B9C1C981899199A1A9B1B9C1C981899199A1A9B1B9C1C9818991980",hex); + TestRecord result = UperEncoder.decode(encoded, TestRecord.class); + assertEquals(record.valueUtf8,result.valueUtf8); + } + + @Test public void testEncode64() throws IllegalArgumentException, IllegalAccessException { + TestRecord record = new TestRecord("1234567890123456789012345678901234567890123456789012345678901234"); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + assertEquals("A01899199A1A9B1B9C1C981899199A1A9B1B9C1C981899199A1A9B1B9C1C981899199A1A9B1B9C1C981899199A1A9B1B9C1C981899199A1A9B1B9C1C981899199A00",hex); + TestRecord result = UperEncoder.decode(encoded, TestRecord.class); + assertEquals(record.valueUtf8,result.valueUtf8); + } + + @Test public void testEncode65() throws IllegalArgumentException, IllegalAccessException { + TestRecord record = new TestRecord("12345678901234567890123456789012345678901234567890123456789012345"); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + assertEquals("A09899199A1A9B1B9C1C981899199A1A9B1B9C1C981899199A1A9B1B9C1C981899199A1A9B1B9C1C981899199A1A9B1B9C1C981899199A1A9B1B9C1C981899199A1A80",hex); + TestRecord result = UperEncoder.decode(encoded, TestRecord.class); + assertEquals(record.valueUtf8,result.valueUtf8); + } + + @Test public void testEncode126() throws IllegalArgumentException, IllegalAccessException { + TestRecord record = new TestRecord("123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456"); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + assertEquals("BF1899199A1A9B1B9C1C981899199A1A9B1B9C1C981899199A1A9B1B9C1C9818" + + "99199A1A9B1B9C1C981899199A1A9B1B9C1C981899199A1A9B1B9C1C98189919" + + "9A1A9B1B9C1C981899199A1A9B1B9C1C981899199A1A9B1B9C1C981899199A1A" + + "9B1B9C1C981899199A1A9B1B9C1C981899199A1A9B1B9C1C981899199A1A9B00",hex); + TestRecord result = UperEncoder.decode(encoded, TestRecord.class); + assertEquals(record.valueUtf8,result.valueUtf8); + } + + + @Test public void testEncode127() throws IllegalArgumentException, IllegalAccessException { + TestRecord record = new TestRecord("1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567"); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + assertEquals("BF9899199A1A9B1B9C1C981899199A1A9B1B9C1C981899199A1A9B1B9C1C9818" + + "99199A1A9B1B9C1C981899199A1A9B1B9C1C981899199A1A9B1B9C1C98189919" + + "9A1A9B1B9C1C981899199A1A9B1B9C1C981899199A1A9B1B9C1C981899199A1A" + + "9B1B9C1C981899199A1A9B1B9C1C981899199A1A9B1B9C1C981899199A1A9B1B" + + "80",hex); + TestRecord result = UperEncoder.decode(encoded, TestRecord.class); + assertEquals(record.valueUtf8,result.valueUtf8); + } + + @Test public void testEncode128() throws IllegalArgumentException, IllegalAccessException { + TestRecord record = new TestRecord("12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678"); + + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + assertEquals("C0401899199A1A9B1B9C1C981899199A1A9B1B9C1C981899199A1A9B1B9C1C98" + + "1899199A1A9B1B9C1C981899199A1A9B1B9C1C981899199A1A9B1B9C1C981899" + + "199A1A9B1B9C1C981899199A1A9B1B9C1C981899199A1A9B1B9C1C981899199A" + + "1A9B1B9C1C981899199A1A9B1B9C1C981899199A1A9B1B9C1C981899199A1A9B" + + "1B9C00",hex); + TestRecord result = UperEncoder.decode(encoded, TestRecord.class); + assertEquals(record.valueUtf8,result.valueUtf8); + } + + @Test public void testEncode129() throws IllegalArgumentException, IllegalAccessException { + TestRecord record = new TestRecord("123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + assertEquals("C0409899199A1A9B1B9C1C981899199A1A9B1B9C1C981899199A1A9B1B9C1C98" + + "1899199A1A9B1B9C1C981899199A1A9B1B9C1C981899199A1A9B1B9C1C981899" + + "199A1A9B1B9C1C981899199A1A9B1B9C1C981899199A1A9B1B9C1C981899199A" + + "1A9B1B9C1C981899199A1A9B1B9C1C981899199A1A9B1B9C1C981899199A1A9B" + + "1B9C1C80",hex); + TestRecord result = UperEncoder.decode(encoded, TestRecord.class); + assertEquals(record.valueUtf8,result.valueUtf8); + } + +} diff --git a/src/test/java/org/uic/barcode/asn1/test/UperEncodeStringTest.java b/src/test/java/org/uic/barcode/asn1/test/UperEncodeStringTest.java new file mode 100644 index 0000000..5f4edc8 --- /dev/null +++ b/src/test/java/org/uic/barcode/asn1/test/UperEncodeStringTest.java @@ -0,0 +1,110 @@ +package org.uic.barcode.asn1.test; + +import static org.junit.Assert.assertEquals; + +import java.util.logging.Level; + +import org.junit.Test; +import org.uic.barcode.asn1.datatypes.Asn1Optional; +import org.uic.barcode.asn1.datatypes.CharacterRestriction; +import org.uic.barcode.asn1.datatypes.FieldOrder; +import org.uic.barcode.asn1.datatypes.RestrictedString; +import org.uic.barcode.asn1.datatypes.Sequence; +import org.uic.barcode.asn1.uper.UperEncoder; + + +public class UperEncodeStringTest { + + /** + * Example from the Standard on UPER. + <pre> + World-Schema DEFINITIONS AUTOMATIC TAGS ::= + BEGIN + TestRecord ::= [APPLICATION 0] IMPLICIT SEQUENCE { + testString1 UTF8String OPTIONAL, + testString2 IA5String OPTIONAL + } + END + </pre> + */ + @Sequence + public static class TestRecord { + + @FieldOrder(order = 0) + @RestrictedString(CharacterRestriction.UTF8String) + @Asn1Optional() String valueUtf8; + + @FieldOrder(order = 1) + @RestrictedString(CharacterRestriction.IA5String) + @Asn1Optional() String valueIA5; + + public TestRecord() { + } + + public TestRecord(String utf8, String ia5) { + this.valueUtf8 = utf8; + this.valueIA5 = ia5; + } + } + + + @Test public void testEncode() throws IllegalArgumentException, IllegalAccessException { + + //Teststring: AêñüC + String original = new String("A" + "\u00ea" + "\u00f1" + "\u00fc" + "C"); + + TestRecord record = new TestRecord(original, "Meier"); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + assertEquals("C21070EAB0EC70EF10C166E5D39790",hex); + + } + + @Test public void testEncodeUtf8() throws IllegalArgumentException, IllegalAccessException { + + //"ä½ å¥½å?—" + String original = new String("\u00e4" + "\u00bd" + "\u00a0" + "\u00e5" + "\u00a5" + "\u00bd" + "\u00e5" + "\u0090" + "\u0097"); + TestRecord record = new TestRecord(original, "Meier"); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + assertEquals("C4B0E930AF70A830E970A970AF70E970A430A5C166E5D39790",hex); + } + + + + @Test public void testDecode() throws IllegalArgumentException, IllegalAccessException { + + //Teststring: AêñüC + String original = new String("A" + "\u00ea" + "\u00f1" + "\u00fc" + "C"); + + TestRecord record = new TestRecord(original, "Meier"); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + assertEquals("C21070EAB0EC70EF10C166E5D39790",hex); + TestRecord result = UperEncoder.decode(encoded, TestRecord.class); + assertEquals(result.valueUtf8,record.valueUtf8); + assertEquals(result.valueIA5,record.valueIA5); + } + + + @Test public void testDecodeUtf8() throws IllegalArgumentException, IllegalAccessException { + //"ä½ å¥½å?—" + String original = new String("\u00e4" + "\u00bd" + "\u00a0" + "\u00e5" + "\u00a5" + "\u00bd" + "\u00e5" + "\u0090" + "\u0097"); + TestRecord record = new TestRecord(original, "Meier"); + byte[] encoded = UperEncoder.encode(record); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + assertEquals("C4B0E930AF70A830E970A970AF70E970A430A5C166E5D39790",hex); + TestRecord result = UperEncoder.decode(encoded, TestRecord.class); + assertEquals(result.valueUtf8,record.valueUtf8); + assertEquals(result.valueIA5,record.valueIA5); + } + + + + + +} diff --git a/src/test/java/org/uic/barcode/test/DynamicFrameDoubleSignatureTest.java b/src/test/java/org/uic/barcode/test/DynamicFrameDoubleSignatureTest.java new file mode 100644 index 0000000..aa377e8 --- /dev/null +++ b/src/test/java/org/uic/barcode/test/DynamicFrameDoubleSignatureTest.java @@ -0,0 +1,212 @@ +package org.uic.barcode.test;
+
+import java.io.IOException;
+import java.security.InvalidAlgorithmParameterException;
+import java.security.InvalidKeyException;
+import java.security.KeyPair;
+import java.security.KeyPairGenerator;
+import java.security.NoSuchAlgorithmException;
+import java.security.NoSuchProviderException;
+import java.security.SecureRandom;
+import java.security.Security;
+import java.security.SignatureException;
+import java.util.Arrays;
+import java.util.zip.DataFormatException;
+
+import org.bouncycastle.jce.ECNamedCurveTable;
+import org.bouncycastle.jce.provider.BouncyCastleProvider;
+import org.bouncycastle.jce.spec.ECParameterSpec;
+import org.junit.Before;
+import org.junit.Test;
+import org.uic.barcode.Decoder;
+import org.uic.barcode.Encoder;
+import org.uic.barcode.dynamicFrame.Constants;
+import org.uic.barcode.dynamicFrame.DataType;
+import org.uic.barcode.test.utils.Level2TestDataFactory;
+import org.uic.barcode.test.utils.SimpleUICTestTicket;
+import org.uic.barcode.ticket.EncodingFormatException;
+import org.uic.barcode.ticket.api.spec.IUicRailTicket;
+import org.uic.barcode.utils.AlgorithmNameResolver;
+
+public class DynamicFrameDoubleSignatureTest {
+
+ public String signatureAlgorithmOID = null;
+ public String elipticCurve = null;
+ public String keyPairAlgorithmOID = null;
+
+ public KeyPair keyPairLevel1 = null;
+ public KeyPair keyPairLevel2 = null;
+
+ public IUicRailTicket testFCBticket = null;
+
+
+ @Before public void initialize() {
+
+ signatureAlgorithmOID = Constants.ECDSA_SHA256;
+ keyPairAlgorithmOID = Constants.KG_EC_256;
+ elipticCurve = "secp256k1";
+
+ testFCBticket = SimpleUICTestTicket.getUicTestTicket();
+
+ Security.addProvider(new BouncyCastleProvider());
+
+ try {
+ keyPairLevel1 = generateECKeys(keyPairAlgorithmOID, elipticCurve);
+ keyPairLevel2 = generateECKeys(keyPairAlgorithmOID, elipticCurve);
+ } catch (Exception e) {
+ assert(false);
+ }
+
+ assert(keyPairLevel1 != null);
+
+ }
+
+
+ @Test public void testDynamicHeaderBarcodeEncoding() {
+
+ IUicRailTicket ticket = testFCBticket;
+
+ Encoder enc = null;
+
+ try {
+ enc = new Encoder(ticket, null, Encoder.UIC_BARCODE_TYPE_DOSIPAS, 1, 13);
+ } catch (IOException | EncodingFormatException e1) {
+ assert(false);
+ }
+
+ assert(enc != null);
+
+ try {
+ enc.setLevel1Algs(signatureAlgorithmOID, keyPairAlgorithmOID);
+ enc.setLevel2Algs(signatureAlgorithmOID, keyPairAlgorithmOID,keyPairLevel2.getPublic());
+ enc.signLevel1("1080", keyPairLevel1.getPrivate(), signatureAlgorithmOID, "1");
+ } catch (Exception e) {
+ assert(false);
+ }
+
+ assert(enc != null);
+
+ try {
+ enc.setLevel2Data(Level2TestDataFactory.getLevel2SimpleTestData());
+ enc.signLevel2(keyPairLevel2.getPrivate());
+ } catch (Exception e) {
+ assert(false);
+ }
+
+
+ byte[] encoded = null;
+ try {
+ encoded = enc.encode();
+ } catch (Exception e) {
+ assert(false);
+ }
+
+ assert(encoded != null);
+
+
+
+ }
+
+ @Test public void testDynamicHeaderBarcodeDecoding() {
+
+ IUicRailTicket ticket = testFCBticket;
+
+ Encoder enc = null;
+
+ try {
+ enc = new Encoder(ticket, null, Encoder.UIC_BARCODE_TYPE_DOSIPAS, 1, 13);
+ } catch (IOException | EncodingFormatException e1) {
+ assert(false);
+ }
+
+ assert(enc != null);
+
+ try {
+ enc.setLevel1Algs(signatureAlgorithmOID, keyPairAlgorithmOID);
+ enc.setLevel2Algs(signatureAlgorithmOID, keyPairAlgorithmOID,keyPairLevel2.getPublic());
+ enc.signLevel1("1080", keyPairLevel1.getPrivate(), signatureAlgorithmOID, "1");
+ } catch (Exception e) {
+ assert(false);
+ }
+
+ assert(enc != null);
+
+
+ DataType level2Data = Level2TestDataFactory.getLevel2SimpleTestData();
+ try {
+ enc.setLevel2Data(level2Data);
+ enc.signLevel2(keyPairLevel2.getPrivate());
+ } catch (Exception e) {
+ assert(false);
+ }
+
+
+ byte[] encoded = null;
+ try {
+ encoded = enc.encode();
+ } catch (Exception e) {
+ assert(false);
+ }
+
+ assert(encoded != null);
+
+ Decoder dec = null;
+ try {
+ dec = new Decoder(encoded);
+ } catch (IOException e) {
+ assert(false);
+ } catch (EncodingFormatException e) {
+ assert(false);
+ } catch (DataFormatException e) {
+ assert(false);
+ }
+ assert(dec != null);
+
+ int signatureCheck = 0;
+ try {
+ signatureCheck = dec.validateLevel1(keyPairLevel1.getPublic(),null);
+ } catch (InvalidKeyException | NoSuchAlgorithmException | SignatureException | IllegalArgumentException
+ | UnsupportedOperationException | IOException | EncodingFormatException e) {
+ assert(false);
+ }
+ assert(signatureCheck == Constants.LEVEL1_VALIDATION_OK);
+
+ signatureCheck = 0;
+ try {
+ signatureCheck = dec.validateLevel2();
+ } catch (IllegalArgumentException | UnsupportedOperationException e) {
+ assert(false);
+ }
+ assert(signatureCheck == Constants.LEVEL2_VALIDATION_OK);
+
+
+ DataType level2DataDec = dec.getLevel2Data();
+
+ assert(level2Data.getFormat().equals(level2DataDec.getFormat()));
+ assert(Arrays.equals(level2Data.getData().toByteArray(),level2DataDec.getData().toByteArray()));
+
+ SimpleUICTestTicket.compare(ticket, dec.getUicTicket());
+
+
+ }
+
+ public KeyPair generateECDSAKeys(String keyAlgorithmName, String paramName) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException{
+ ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(paramName);
+ KeyPairGenerator g = KeyPairGenerator.getInstance(keyAlgorithmName, "BC");
+ g.initialize(ecSpec, new SecureRandom());
+ return g.generateKeyPair();
+ }
+
+ public KeyPair generateECKeys(String keyAlgorithmOid, String curve) throws Exception{
+
+ String keyAlgorithmName = AlgorithmNameResolver.getName(AlgorithmNameResolver.TYPE_KEY_GENERATOR_ALG, keyAlgorithmOid, "BC");
+
+ keyAlgorithmName = "ECDSA";
+ ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(curve);
+ KeyPairGenerator g = KeyPairGenerator.getInstance(keyAlgorithmName, "BC");
+ g.initialize(ecSpec, new SecureRandom());
+ return g.generateKeyPair();
+ }
+
+
+}
diff --git a/src/test/java/org/uic/barcode/test/DynamicFrameLOwLevelTest.java b/src/test/java/org/uic/barcode/test/DynamicFrameLOwLevelTest.java new file mode 100644 index 0000000..5dd9668 --- /dev/null +++ b/src/test/java/org/uic/barcode/test/DynamicFrameLOwLevelTest.java @@ -0,0 +1,115 @@ +package org.uic.barcode.test;
+
+import java.security.InvalidAlgorithmParameterException;
+import java.security.KeyFactory;
+import java.security.KeyPair;
+import java.security.KeyPairGenerator;
+import java.security.NoSuchAlgorithmException;
+import java.security.NoSuchProviderException;
+import java.security.PrivateKey;
+import java.security.PublicKey;
+import java.security.SecureRandom;
+import java.security.Security;
+import java.security.spec.InvalidKeySpecException;
+import java.security.spec.PKCS8EncodedKeySpec;
+import java.security.spec.X509EncodedKeySpec;
+
+import org.bouncycastle.jce.ECNamedCurveTable;
+import org.bouncycastle.jce.provider.BouncyCastleProvider;
+import org.bouncycastle.jce.spec.ECParameterSpec;
+import org.junit.Before;
+import org.junit.Test;
+import org.uic.barcode.asn1.uper.UperEncoder;
+import org.uic.barcode.dynamicFrame.Constants;
+import org.uic.barcode.dynamicFrame.DynamicFrame;
+import org.uic.barcode.test.utils.SimpleDynamicFrameTestBarcode;
+
+public class DynamicFrameLOwLevelTest {
+
+ public String algorithmOID = Constants.ECDSA_SHA256;
+ public KeyPair keyPair = null;
+ public String publicKeyHex = "3081A7301006072A8648CE3D020106052B81040027038192000405B2797BB27F96EC3769B81E563EEB97A4CE3B5BB4EE19BC90A3F496B805644AA042736E5FA9B3A5FBEA5B01CD1D9EC13C009F9655B31FFF9AA52AC6D90B5D6220B58853A1D18BF20779BE5C52356AE70B19242065F82B76961E2A079F42CA9A41A1AB4D5518446AC3721953AE6323C60E15389498DE5F592A24DDDA45F736D93695C797C0F28A712EC25B9CD8078457";
+ public String privateKeyHex = "30820109020100301006072A8648CE3D020106052B810400270481F13081EE020101044801EF44914319A5DD528C06419D7B0FD0CDD7F62A231BEB197E45A0074C02E11FE82ABAD916BE94FD8256260AA9191F19241CFC7E372B3A4E0ADA06CCA51678C54198667DFC9B0DA8A00706052B81040027A18195038192000405B2797BB27F96EC3769B81E563EEB97A4CE3B5BB4EE19BC90A3F496B805644AA042736E5FA9B3A5FBEA5B01CD1D9EC13C009F9655B31FFF9AA52AC6D90B5D6220B58853A1D18BF20779BE5C52356AE70B19242065F82B76961E2A079F42CA9A41A1AB4D5518446AC3721953AE6323C60E15389498DE5F592A24DDDA45F736D93695C797C0F28A712EC25B9CD8078457";
+
+ public String hexReferenceContent = "400EAC986010DF80A021DE008808014374F3E7D72F2A9979F4A13A90086280B40020894DED0DC0688DEEE0AC593368D60220DCF2EED3BF903B6BCA3B937BAB801280EB44AC0505B4200000000100E6F70656E5469636B6574496E666F120220103B830B9B9B0B3B28084A0B6B9BA32B93230B680202F698040100B20004C6C8020404E9979F40201620505B402C80A0F68020AA192338F4100C08008097308194024800DA0C61105BAD7E13ADF9D5A00CBC47732865EA67E8371A5FBE38B4FABBBABD37459D12048DA6664700E787C32962A607A784FD2FC669A9A8EC9F91CD53AF2B922EFECE24FF3D68024800A1F7CF1C0625FB19CF089E74D668F5E8C15179BEF7BA79D9D169A12FA47F6340ED50BADB57CD83110060FEC08B1EF978C6DB08A172B0DE20C442D4507442623A74A624457590040";
+
+
+ @Before public void initialize() {
+
+ Security.addProvider(new BouncyCastleProvider());
+
+ try {
+ keyPair = generateECDSAKeys();
+ } catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidAlgorithmParameterException e) {
+ e.printStackTrace();
+ }
+ privateKeyHex = UperEncoder.hexStringFromBytes(keyPair.getPrivate().getEncoded());
+ publicKeyHex = UperEncoder.hexStringFromBytes(keyPair.getPublic().getEncoded());
+
+ assert(keyPair != null);
+
+ try {
+ PublicKey publicKey = KeyFactory.getInstance("ECDSA").generatePublic(new X509EncodedKeySpec(UperEncoder.bytesFromHexString(publicKeyHex)));
+ PrivateKey privateKey = KeyFactory.getInstance("ECDSA").generatePrivate(new PKCS8EncodedKeySpec(UperEncoder.bytesFromHexString(privateKeyHex)));
+ keyPair = new KeyPair(publicKey,privateKey);
+ } catch (InvalidKeySpecException | NoSuchAlgorithmException e) {
+ e.printStackTrace();
+ }
+ assert(keyPair != null);
+
+ }
+
+
+ @Test public void testDynamicHeaderBarcodeEncoding() {
+
+ DynamicFrame barcode1 = SimpleDynamicFrameTestBarcode.getSimpleDynamicHeaderBarcode(algorithmOID, keyPair);
+
+ byte[] encoded = barcode1.encode();
+
+ //String hex = UperEncoder.hexStringFromBytes(encoded);
+
+ /*
+ * check the available implementations
+ String s = null;
+ try {
+ s = AlgorithmNameResolver.getSecurityNames();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ */
+
+
+ //note: hex is different each time due to the random seed in the signature
+ assert(encoded != null);
+
+ }
+
+ @Test public void testDynamicHeaderBarcodeDecoding() {
+
+ DynamicFrame barcode1 = SimpleDynamicFrameTestBarcode.getSimpleDynamicHeaderBarcode(algorithmOID, keyPair);
+
+ byte[] encoded = barcode1.encode();
+
+ DynamicFrame barcode = DynamicFrame.decode(encoded);
+
+ int signatureCheck = barcode.validateLevel1(keyPair.getPublic());
+
+ assert(signatureCheck == Constants.LEVEL1_VALIDATION_OK);
+
+ SimpleDynamicFrameTestBarcode.compareFrame(barcode1, barcode);
+
+
+
+
+ }
+
+ public KeyPair generateECDSAKeys() throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException{
+ ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("B-571");
+ KeyPairGenerator g = KeyPairGenerator.getInstance("ECDSA", "BC");
+ g.initialize(ecSpec, new SecureRandom());
+ return g.generateKeyPair();
+ }
+
+
+
+}
diff --git a/src/test/java/org/uic/barcode/test/DynamicFrameSimpleTest.java b/src/test/java/org/uic/barcode/test/DynamicFrameSimpleTest.java new file mode 100644 index 0000000..a3a1b0e --- /dev/null +++ b/src/test/java/org/uic/barcode/test/DynamicFrameSimpleTest.java @@ -0,0 +1,171 @@ +package org.uic.barcode.test;
+
+import java.io.IOException;
+import java.security.InvalidAlgorithmParameterException;
+import java.security.InvalidKeyException;
+import java.security.KeyPair;
+import java.security.KeyPairGenerator;
+import java.security.NoSuchAlgorithmException;
+import java.security.NoSuchProviderException;
+import java.security.SecureRandom;
+import java.security.Security;
+import java.security.SignatureException;
+import java.util.zip.DataFormatException;
+
+import org.bouncycastle.jce.ECNamedCurveTable;
+import org.bouncycastle.jce.provider.BouncyCastleProvider;
+import org.bouncycastle.jce.spec.ECParameterSpec;
+import org.junit.Before;
+import org.junit.Test;
+import org.uic.barcode.Decoder;
+import org.uic.barcode.Encoder;
+import org.uic.barcode.dynamicFrame.Constants;
+import org.uic.barcode.test.utils.SimpleUICTestTicket;
+import org.uic.barcode.ticket.EncodingFormatException;
+import org.uic.barcode.ticket.api.spec.IUicRailTicket;
+import org.uic.barcode.utils.AlgorithmNameResolver;
+
+public class DynamicFrameSimpleTest {
+
+ public String signatureAlgorithmOID = null;
+ public String elipticCurve = null;
+ public String keyPairAlgorithmOID = null;
+
+ public KeyPair keyPair = null;
+
+ public IUicRailTicket testFCBticket = null;
+
+
+ @Before public void initialize() {
+
+ signatureAlgorithmOID = Constants.ECDSA_SHA256;
+ keyPairAlgorithmOID = Constants.KG_EC_256;
+ elipticCurve = "secp256k1";
+
+ testFCBticket = SimpleUICTestTicket.getUicTestTicket();
+
+ Security.addProvider(new BouncyCastleProvider());
+
+ try {
+ keyPair = generateECKeys(Constants.KG_EC, elipticCurve);
+ //keyPair = generateECDSAKeys("ECDSA", "B-571");
+ } catch (Exception e) {
+ assert(false);
+ }
+
+ assert(keyPair != null);
+
+ }
+
+
+ @Test public void testDynamicHeaderBarcodeEncoding() {
+
+ IUicRailTicket ticket = testFCBticket;
+
+ Encoder enc = null;
+
+ try {
+ enc = new Encoder(ticket, null, Encoder.UIC_BARCODE_TYPE_DOSIPAS, 1, 13);
+ } catch (IOException | EncodingFormatException e1) {
+ assert(false);
+ }
+
+ assert(enc != null);
+
+ try {
+ enc.signLevel1("1080", keyPair.getPrivate(), signatureAlgorithmOID, "1");
+ } catch (Exception e) {
+ assert(false);
+ }
+
+
+ byte[] encoded = null;
+ try {
+ encoded = enc.encode();
+ } catch (Exception e) {
+ assert(false);
+ }
+
+ assert(encoded != null);
+
+
+
+ }
+
+ @Test public void testDynamicHeaderBarcodeDecoding() {
+
+ IUicRailTicket ticket = testFCBticket;
+
+ Encoder enc = null;
+
+ try {
+ enc = new Encoder(ticket, null, Encoder.UIC_BARCODE_TYPE_DOSIPAS, 1, 13);
+ } catch (IOException | EncodingFormatException e1) {
+ assert(false);
+ }
+
+ assert(enc != null);
+
+ try {
+ enc.signLevel1("1080", keyPair.getPrivate(), signatureAlgorithmOID, "1");
+ } catch (Exception e) {
+ assert(false);
+ }
+
+
+ byte[] encoded = null;
+ try {
+ encoded = enc.encode();
+ } catch (Exception e) {
+ assert(false);
+ }
+
+ assert(encoded != null);
+
+ Decoder dec = null;
+ try {
+ dec = new Decoder(encoded);
+ } catch (IOException e) {
+ assert(false);
+ } catch (EncodingFormatException e) {
+ assert(false);
+ } catch (DataFormatException e) {
+ assert(false);
+ }
+ assert(dec != null);
+
+ int signatureCheck = 0;
+ try {
+ signatureCheck = dec.validateLevel1(keyPair.getPublic(),null);
+ } catch (InvalidKeyException | NoSuchAlgorithmException | SignatureException | IllegalArgumentException
+ | UnsupportedOperationException | IOException | EncodingFormatException e) {
+ assert(false);
+ }
+
+ assert(signatureCheck == Constants.LEVEL1_VALIDATION_OK);
+
+ SimpleUICTestTicket.compare(ticket, dec.getUicTicket());
+
+
+ }
+
+ public KeyPair generateECDSAKeys(String keyAlgorithmName, String paramName) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException{
+ ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(paramName);
+ KeyPairGenerator g = KeyPairGenerator.getInstance(keyAlgorithmName, "BC");
+ g.initialize(ecSpec, new SecureRandom());
+ return g.generateKeyPair();
+ }
+
+ public KeyPair generateECKeys(String keyAlgorithmOid, String curve) throws Exception{
+
+ String keyAlgorithmName = AlgorithmNameResolver.getName(AlgorithmNameResolver.TYPE_KEY_GENERATOR_ALG, keyAlgorithmOid, "BC");
+
+ keyAlgorithmName = "ECDSA";
+ ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(curve);
+ KeyPairGenerator g = KeyPairGenerator.getInstance(keyAlgorithmName, "BC");
+ g.initialize(ecSpec, new SecureRandom());
+ return g.generateKeyPair();
+ }
+
+
+}
diff --git a/src/test/java/org/uic/barcode/test/SignatureSplitTest.java b/src/test/java/org/uic/barcode/test/SignatureSplitTest.java new file mode 100644 index 0000000..08fe3ad --- /dev/null +++ b/src/test/java/org/uic/barcode/test/SignatureSplitTest.java @@ -0,0 +1,76 @@ +package org.uic.barcode.test;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.IOException;
+import java.math.BigInteger;
+
+import org.junit.Test;
+import org.uic.barcode.staticFrame.StaticFrame;
+import org.uic.barcode.test.utils.TestUtils;
+import org.uic.barcode.ticket.EncodingFormatException;
+
+public class SignatureSplitTest {
+
+
+ /*
+ *
+ *
+ * World-Schema DEFINITIONS AUTOMATIC TAGS ::=
+ BEGIN
+ Signature ::= SEQUENCE OF INTEGER
+ END
+
+
+ value Signature ::= {
+ 340282366920938463,
+ 134515671861986
+ }
+
+
+ Encoding using DER encoding rule
+ Signature SEQUENCE OF: tag = [UNIVERSAL 16] constructed; length = 18
+ INTEGER: tag = [UNIVERSAL 2] primitive; length = 8
+ 340282366920938463
+ INTEGER: tag = [UNIVERSAL 2] primitive; length = 6
+ 134515671861986
+ Encoded successfully in 20 bytes: 30120208 04B8ED02 83A6D3DF 02067A57 5ED68AE2
+
+
+ *
+ *
+ */
+
+
+ @Test public void testSplitSignature() throws IOException, EncodingFormatException{
+
+ BigInteger i1 = BigInteger.valueOf(340282366920938463L);
+ BigInteger i2 = BigInteger.valueOf(134515671861986L);
+
+ byte[] encoded = StaticFrame.encodeSignatureIntegerSequence(i1,i2);
+
+
+ String hex = TestUtils.hexStringFromBytes(encoded);
+
+ assertEquals(hex,"3012020804B8ED0283A6D3DF02067A575ED68AE2");
+
+ BigInteger[] ints = null;
+ try {
+ ints = StaticFrame.decodeSignatureIntegerSequence(encoded);
+ } catch (Exception e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+ assert(ints != null);
+
+ assert(ints.length == 2);
+
+ assert(i1.equals(ints[0]));
+
+ assert(i2.equals(ints[1]));
+
+ }
+
+
+}
\ No newline at end of file diff --git a/src/test/java/org/uic/barcode/test/StaticFrameBarcodeTest.java b/src/test/java/org/uic/barcode/test/StaticFrameBarcodeTest.java new file mode 100644 index 0000000..9600191 --- /dev/null +++ b/src/test/java/org/uic/barcode/test/StaticFrameBarcodeTest.java @@ -0,0 +1,193 @@ +package org.uic.barcode.test;
+
+import java.io.IOException;
+import java.security.InvalidAlgorithmParameterException;
+import java.security.InvalidKeyException;
+import java.security.KeyPair;
+import java.security.KeyPairGenerator;
+import java.security.NoSuchAlgorithmException;
+import java.security.NoSuchProviderException;
+import java.security.SecureRandom;
+import java.security.Security;
+import java.security.SignatureException;
+import java.util.zip.DataFormatException;
+
+import org.bouncycastle.jce.provider.BouncyCastleProvider;
+import org.junit.Before;
+import org.junit.Test;
+import org.uic.barcode.Decoder;
+import org.uic.barcode.Encoder;
+import org.uic.barcode.dynamicFrame.Constants;
+import org.uic.barcode.staticFrame.ticketLayoutBarcode.TicketLayout;
+import org.uic.barcode.test.utils.SimpleTestTicketLayout;
+import org.uic.barcode.test.utils.SimpleUICTestTicket;
+import org.uic.barcode.ticket.EncodingFormatException;
+import org.uic.barcode.ticket.api.spec.IUicRailTicket;
+
+/**
+ * The Class StaticFrameBarcodeTest.
+ */
+public class StaticFrameBarcodeTest {
+
+ /** The algorithm OID. */
+ public String algorithmOID = Constants.DSA_SHA224;
+
+ public int keySize = 2048;
+
+ /** The key pair. */
+ public KeyPair keyPair = null;
+
+
+ public IUicRailTicket testFCBticket = null;
+
+ public TicketLayout testLayout = null;
+
+
+ /**
+ * Initialize.
+ *
+ * set the signature algorithm
+ * generate a key pair
+ * set the test content
+ * for ticket and layout
+ */
+ @Before public void initialize() {
+
+ algorithmOID = Constants.DSA_SHA224;
+ keySize = 2048;
+ testFCBticket = SimpleUICTestTicket.getUicTestTicket();
+ testLayout = SimpleTestTicketLayout.getSimpleTestTicketLayout();
+
+ Security.addProvider(new BouncyCastleProvider());
+
+ try {
+ keyPair = generateDSAKeys(keySize);
+ } catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidAlgorithmParameterException e) {
+ e.printStackTrace();
+ }
+
+ assert(keyPair != null);
+
+ }
+
+
+ /**
+ * Test dynamic header barcode encoding.
+ */
+ @Test public void testDynamicHeaderBarcodeEncoding() {
+
+ IUicRailTicket ticket = testFCBticket;
+
+ TicketLayout layout = testLayout;
+
+ Encoder enc = null;
+
+ try {
+ enc = new Encoder(ticket, layout, Encoder.UIC_BARCODE_TYPE_CLASSIC, 2, 13);
+ } catch (IOException | EncodingFormatException e1) {
+ assert(false);
+ }
+
+ enc.setStaticHeaderParams("123456789012", "de");
+
+ assert(enc != null);
+
+ try {
+ enc.signLevel1("1080", keyPair.getPrivate(), algorithmOID, "1");
+ } catch (Exception e) {
+ assert(false);
+ }
+
+ byte[] encoded = null;
+ try {
+ encoded = enc.encode();
+ } catch (Exception e) {
+ assert(false);
+ }
+
+ assert(encoded != null);
+
+ }
+
+ /**
+ * Test dynamic header barcode decoding.
+ */
+ @Test public void testDynamicHeaderBarcodeDecoding() {
+
+
+ IUicRailTicket ticket = testFCBticket;
+
+ TicketLayout layout = testLayout;
+
+
+ Encoder enc = null;
+
+ try {
+ enc = new Encoder(ticket, layout, Encoder.UIC_BARCODE_TYPE_CLASSIC, 2, 13);
+ } catch (IOException | EncodingFormatException e1) {
+ assert(false);
+ }
+
+ enc.setStaticHeaderParams("123456789012", "de");
+
+ assert(enc != null);
+
+ try {
+ enc.signLevel1("1080", keyPair.getPrivate(), algorithmOID, "1");
+ } catch (Exception e) {
+ assert(false);
+ }
+
+
+ byte[] encoded = null;
+ try {
+ encoded = enc.encode();
+ } catch (Exception e) {
+ assert(false);
+ }
+
+ assert(encoded != null);
+
+ Decoder dec = null;
+ try {
+ dec = new Decoder(encoded);
+ } catch (IOException e) {
+ assert(false);
+ } catch (EncodingFormatException e) {
+ assert(false);
+ } catch (DataFormatException e) {
+ assert(false);
+ }
+ assert(dec != null);
+
+ int signatureCheck = 0;
+ try {
+ signatureCheck = dec.validateLevel1(keyPair.getPublic(),algorithmOID);
+ } catch (InvalidKeyException | NoSuchAlgorithmException | SignatureException | IllegalArgumentException
+ | UnsupportedOperationException | IOException | EncodingFormatException e) {
+ assert(false);
+ }
+
+ assert(signatureCheck == Constants.LEVEL1_VALIDATION_OK);
+
+ SimpleUICTestTicket.compare(ticket, dec.getUicTicket());
+
+ SimpleTestTicketLayout.compare(layout, dec.getLayout());
+
+ }
+
+ /**
+ * Generate DSA keys.
+ *
+ * @return the key pair
+ * @throws NoSuchAlgorithmException the no such algorithm exception
+ * @throws NoSuchProviderException the no such provider exception
+ * @throws InvalidAlgorithmParameterException the invalid algorithm parameter exception
+ */
+ public KeyPair generateDSAKeys(int keySize) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException{
+ KeyPairGenerator g = KeyPairGenerator.getInstance("DSA", "BC");
+ g.initialize(keySize, new SecureRandom());
+ return g.generateKeyPair();
+ }
+
+}
diff --git a/src/test/java/org/uic/barcode/test/StaticFrameBarcodeTestFCB2.java b/src/test/java/org/uic/barcode/test/StaticFrameBarcodeTestFCB2.java new file mode 100644 index 0000000..048d684 --- /dev/null +++ b/src/test/java/org/uic/barcode/test/StaticFrameBarcodeTestFCB2.java @@ -0,0 +1,193 @@ +package org.uic.barcode.test;
+
+import java.io.IOException;
+import java.security.InvalidAlgorithmParameterException;
+import java.security.InvalidKeyException;
+import java.security.KeyPair;
+import java.security.KeyPairGenerator;
+import java.security.NoSuchAlgorithmException;
+import java.security.NoSuchProviderException;
+import java.security.SecureRandom;
+import java.security.Security;
+import java.security.SignatureException;
+import java.util.zip.DataFormatException;
+
+import org.bouncycastle.jce.provider.BouncyCastleProvider;
+import org.junit.Before;
+import org.junit.Test;
+import org.uic.barcode.Decoder;
+import org.uic.barcode.Encoder;
+import org.uic.barcode.dynamicFrame.Constants;
+import org.uic.barcode.staticFrame.ticketLayoutBarcode.TicketLayout;
+import org.uic.barcode.test.utils.SimpleTestTicketLayout;
+import org.uic.barcode.test.utils.SimpleUICTestTicket;
+import org.uic.barcode.ticket.EncodingFormatException;
+import org.uic.barcode.ticket.api.spec.IUicRailTicket;
+
+/**
+ * The Class StaticFrameBarcodeTest.
+ */
+public class StaticFrameBarcodeTestFCB2 {
+
+ /** The algorithm OID. */
+ public String algorithmOID = Constants.DSA_SHA224;
+
+ public int keySize = 2048;
+
+ /** The key pair. */
+ public KeyPair keyPair = null;
+
+
+ public IUicRailTicket testFCBticket = null;
+
+ public TicketLayout testLayout = null;
+
+
+ /**
+ * Initialize.
+ *
+ * set the signature algorithm
+ * generate a key pair
+ * set the test content
+ * for ticket and layout
+ */
+ @Before public void initialize() {
+
+ algorithmOID = Constants.DSA_SHA224;
+ keySize = 2048;
+ testFCBticket = SimpleUICTestTicket.getUicTestTicket();
+ testLayout = SimpleTestTicketLayout.getSimpleTestTicketLayout();
+
+ Security.addProvider(new BouncyCastleProvider());
+
+ try {
+ keyPair = generateDSAKeys(keySize);
+ } catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidAlgorithmParameterException e) {
+ e.printStackTrace();
+ }
+
+ assert(keyPair != null);
+
+ }
+
+
+ /**
+ * Test dynamic header barcode encoding.
+ */
+ @Test public void testStaticHeaderBarcodeEncoding() {
+
+ IUicRailTicket ticket = testFCBticket;
+
+ TicketLayout layout = testLayout;
+
+ Encoder enc = null;
+
+ try {
+ enc = new Encoder(ticket, layout, Encoder.UIC_BARCODE_TYPE_CLASSIC, 2, 2);
+ } catch (IOException | EncodingFormatException e1) {
+ assert(false);
+ }
+
+ enc.setStaticHeaderParams("123456789012", "de");
+
+ assert(enc != null);
+
+ try {
+ enc.signLevel1("1080", keyPair.getPrivate(), algorithmOID, "1");
+ } catch (Exception e) {
+ assert(false);
+ }
+
+ byte[] encoded = null;
+ try {
+ encoded = enc.encode();
+ } catch (Exception e) {
+ assert(false);
+ }
+
+ assert(encoded != null);
+
+ }
+
+ /**
+ * Test dynamic header barcode decoding.
+ */
+ @Test public void testStaticHeaderBarcodeDecoding() {
+
+
+ IUicRailTicket ticket = testFCBticket;
+
+ TicketLayout layout = testLayout;
+
+
+ Encoder enc = null;
+
+ try {
+ enc = new Encoder(ticket, layout, Encoder.UIC_BARCODE_TYPE_CLASSIC, 2, 2);
+ } catch (IOException | EncodingFormatException e1) {
+ assert(false);
+ }
+
+ enc.setStaticHeaderParams("123456789012", "de");
+
+ assert(enc != null);
+
+ try {
+ enc.signLevel1("1080", keyPair.getPrivate(), algorithmOID, "1");
+ } catch (Exception e) {
+ assert(false);
+ }
+
+
+ byte[] encoded = null;
+ try {
+ encoded = enc.encode();
+ } catch (Exception e) {
+ assert(false);
+ }
+
+ assert(encoded != null);
+
+ Decoder dec = null;
+ try {
+ dec = new Decoder(encoded);
+ } catch (IOException e) {
+ assert(false);
+ } catch (EncodingFormatException e) {
+ assert(false);
+ } catch (DataFormatException e) {
+ assert(false);
+ }
+ assert(dec != null);
+
+ int signatureCheck = 0;
+ try {
+ signatureCheck = dec.validateLevel1(keyPair.getPublic(),algorithmOID);
+ } catch (InvalidKeyException | NoSuchAlgorithmException | SignatureException | IllegalArgumentException
+ | UnsupportedOperationException | IOException | EncodingFormatException e) {
+ assert(false);
+ }
+
+ assert(signatureCheck == Constants.LEVEL1_VALIDATION_OK);
+
+ SimpleUICTestTicket.compare(ticket, dec.getUicTicket());
+
+ SimpleTestTicketLayout.compare(layout, dec.getLayout());
+
+ }
+
+ /**
+ * Generate DSA keys.
+ *
+ * @return the key pair
+ * @throws NoSuchAlgorithmException the no such algorithm exception
+ * @throws NoSuchProviderException the no such provider exception
+ * @throws InvalidAlgorithmParameterException the invalid algorithm parameter exception
+ */
+ public KeyPair generateDSAKeys(int keySize) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException{
+ KeyPairGenerator g = KeyPairGenerator.getInstance("DSA", "BC");
+ g.initialize(keySize, new SecureRandom());
+ return g.generateKeyPair();
+ }
+
+}
diff --git a/src/test/java/org/uic/barcode/test/TicketLayoutTest.java b/src/test/java/org/uic/barcode/test/TicketLayoutTest.java new file mode 100644 index 0000000..3d053e7 --- /dev/null +++ b/src/test/java/org/uic/barcode/test/TicketLayoutTest.java @@ -0,0 +1,72 @@ +package org.uic.barcode.test;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.IOException;
+
+import org.junit.Test;
+import org.uic.barcode.staticFrame.UTLAYDataRecord;
+import org.uic.barcode.staticFrame.ticketLayoutBarcode.LayoutElement;
+import org.uic.barcode.staticFrame.ticketLayoutBarcode.TicketLayout;
+import org.uic.barcode.test.utils.SimpleTestTicketLayout;
+import org.uic.barcode.test.utils.TestUtils;
+import org.uic.barcode.ticket.EncodingFormatException;
+
+
+public class TicketLayoutTest {
+
+
+ @Test public void testTicketLayout() throws IOException, EncodingFormatException{
+
+ UTLAYDataRecord tl1 = new UTLAYDataRecord();
+
+ TicketLayout layout = SimpleTestTicketLayout.getSimpleTestTicketLayout();
+ tl1.setLayout(layout);
+
+ byte[] encoded = null;
+ try {
+ encoded = tl1.encode();
+ } catch (IOException e) {
+ throw (e);
+ }
+
+ String hex = TestUtils.hexStringFromBytes(encoded);
+
+ assertEquals(hex,"555F544C41593031303034305243543230303031303130313031323030303030374DC3BC6C6C6572");
+
+ UTLAYDataRecord tl2 = new UTLAYDataRecord();
+
+ tl2.decode(tl1.encode());
+
+
+ assertEquals(tl1.toString(),tl2.toString());
+
+ compare(layout, tl2.getLayout());
+
+ }
+
+ public static void compare(TicketLayout layout1, TicketLayout layout2) {
+
+ assert(layout1.getLayoutStandard().equals(layout2.getLayoutStandard()));
+
+ for (LayoutElement e1: layout1.getElements() ) {
+
+ for (LayoutElement e2 :layout2.getElements()) {
+
+ boolean found = false;
+ if (e1.getLine() == e2.getLine() && e1.getColumn() == e2.getColumn()) {
+ found = true;
+ assert(e1.getText().equals(e2.getText()));
+ }
+ assert(found == true);
+
+ }
+
+ }
+
+ }
+
+
+
+
+}
diff --git a/src/test/java/org/uic/barcode/test/utils/Level2TestDataFactory.java b/src/test/java/org/uic/barcode/test/utils/Level2TestDataFactory.java new file mode 100644 index 0000000..b1eb0f5 --- /dev/null +++ b/src/test/java/org/uic/barcode/test/utils/Level2TestDataFactory.java @@ -0,0 +1,19 @@ +package org.uic.barcode.test.utils;
+
+import org.uic.barcode.asn1.datatypesimpl.OctetString;
+import org.uic.barcode.dynamicFrame.DataType;
+
+public class Level2TestDataFactory {
+
+ public static DataType getLevel2SimpleTestData() {
+
+ String testContent = "2020.10.01T12:12.20";
+
+ DataType level2Data = new DataType();
+ level2Data.setFormat("TEST");
+ level2Data.setData(new OctetString(testContent.getBytes()));
+
+ return level2Data;
+ }
+
+}
diff --git a/src/test/java/org/uic/barcode/test/utils/SimpleDynamicFrameTestBarcode.java b/src/test/java/org/uic/barcode/test/utils/SimpleDynamicFrameTestBarcode.java new file mode 100644 index 0000000..0060fcd --- /dev/null +++ b/src/test/java/org/uic/barcode/test/utils/SimpleDynamicFrameTestBarcode.java @@ -0,0 +1,80 @@ +package org.uic.barcode.test.utils;
+
+import java.security.KeyPair;
+
+import org.uic.barcode.dynamicFrame.Constants;
+import org.uic.barcode.dynamicFrame.DataType;
+import org.uic.barcode.dynamicFrame.DynamicFrame;
+import org.uic.barcode.dynamicFrame.Level1DataType;
+import org.uic.barcode.dynamicFrame.Level2DataType;
+import org.uic.barcode.dynamicFrame.SequenceOfDataType;
+import org.uic.barcode.ticket.EncodingFormatException;
+import org.uic.barcode.ticket.api.asn.omv1.UicRailTicketData;
+import org.uic.barcode.ticket.api.test.testtickets.SimpleUicTestTicket;
+
+
+public class SimpleDynamicFrameTestBarcode {
+
+ public static DynamicFrame getSimpleDynamicHeaderBarcode(String algorithm, KeyPair keyPair) {
+
+
+
+ DynamicFrame barcode = null;
+
+
+
+ try {
+ barcode = new DynamicFrame();
+ barcode.setFormat(Constants.DYNAMIC_BARCODE_FORMAT_DEFAULT);
+ Level2DataType level2Data = new Level2DataType();
+ barcode.setLevel2SignedData(level2Data);
+
+ Level1DataType level1Data = new Level1DataType();
+ level2Data.setLevel1Data(level1Data);
+
+ level1Data.setSecurityProvider("1080");
+ level1Data.setKeyId(1L);
+
+ level1Data.setLevel1SigningAlg(Constants.ECDSA_SHA256);
+
+ DataType data = new DataType();
+ UicRailTicketData ticket = SimpleUicTestTicket.getUicTestTicket();
+ byte[] ticketData = ticket.encode();
+ data.setByteData(ticketData);
+ data.setFormat(Constants.DATA_TYPE_FCB_VERSION_1);
+ SequenceOfDataType dataSequence = new SequenceOfDataType();
+ level1Data.setData(dataSequence);
+ level1Data.getData().add(data);
+
+ try {
+ level2Data.signLevel1(keyPair.getPrivate());
+ } catch (Exception e) {
+ assert(false);
+ }
+
+
+ } catch (EncodingFormatException e) {
+ e.printStackTrace();
+ return null;
+ }
+
+ return barcode;
+ }
+
+
+ public static void compareFrame(DynamicFrame frame1, DynamicFrame frame2) {
+
+ assert(frame1.getLevel2SignedData().getLevel1Data().getKeyId() == frame2.getLevel2SignedData().getLevel1Data().getKeyId());
+
+ assert(frame1.getLevel2SignedData().getLevel1Data().getLevel1SigningAlg().equals(frame2.getLevel2SignedData().getLevel1Data().level1SigningAlg));
+
+ assert(frame1.getLevel2SignedData().getLevel1Data().getSecurityProvider().equals(frame2.getLevel2SignedData().getLevel1Data().getSecurityProvider()));
+
+ DataType data1 = frame1.getLevel2SignedData().getLevel1Data().getData().get(0);
+ DataType data2 = frame2.getLevel2SignedData().getLevel1Data().getData().get(0);
+
+ assert(data1.getFormat().equals(data2.getFormat()));
+
+ }
+
+}
diff --git a/src/test/java/org/uic/barcode/test/utils/SimpleTestTicketLayout.java b/src/test/java/org/uic/barcode/test/utils/SimpleTestTicketLayout.java new file mode 100644 index 0000000..7b9ae1b --- /dev/null +++ b/src/test/java/org/uic/barcode/test/utils/SimpleTestTicketLayout.java @@ -0,0 +1,48 @@ +package org.uic.barcode.test.utils;
+
+import org.uic.barcode.staticFrame.ticketLayoutBarcode.LayoutElement;
+import org.uic.barcode.staticFrame.ticketLayoutBarcode.TicketLayout;
+
+public class SimpleTestTicketLayout {
+
+
+ public static TicketLayout getSimpleTestTicketLayout() {
+
+ TicketLayout layout = new TicketLayout();
+
+ layout.setLayoutStandard("RCT2");
+
+ LayoutElement element = new LayoutElement();
+ element.setColumn(1);
+ element.setLine(1);
+ element.setHeight(1);
+ element.setWidth(20);
+ element.setText("Müller");
+ layout.addLayoutElement(element);
+
+ return layout;
+
+ }
+
+ public static void compare(TicketLayout layout1, TicketLayout layout2) {
+
+ assert(layout1.getLayoutStandard().equals(layout2.getLayoutStandard()));
+
+ for (LayoutElement e1: layout1.getElements() ) {
+
+ for (LayoutElement e2 :layout2.getElements()) {
+
+ boolean found = false;
+ if (e1.getLine() == e2.getLine() && e1.getColumn() == e2.getColumn()) {
+ found = true;
+ assert(e1.getText().equals(e2.getText()));
+ }
+ assert(found == true);
+
+ }
+
+ }
+
+ }
+
+}
diff --git a/src/test/java/org/uic/barcode/test/utils/SimpleUICTestTicket.java b/src/test/java/org/uic/barcode/test/utils/SimpleUICTestTicket.java new file mode 100644 index 0000000..be34535 --- /dev/null +++ b/src/test/java/org/uic/barcode/test/utils/SimpleUICTestTicket.java @@ -0,0 +1,254 @@ +package org.uic.barcode.test.utils;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+import org.uic.barcode.ticket.api.impl.SimpleCardReference;
+import org.uic.barcode.ticket.api.impl.SimpleControlDetail;
+import org.uic.barcode.ticket.api.impl.SimpleCustomerStatusDescription;
+import org.uic.barcode.ticket.api.impl.SimpleExtension;
+import org.uic.barcode.ticket.api.impl.SimpleIssuingDetail;
+import org.uic.barcode.ticket.api.impl.SimpleOpenTicket;
+import org.uic.barcode.ticket.api.impl.SimpleStationPassage;
+import org.uic.barcode.ticket.api.impl.SimpleTicketLink;
+import org.uic.barcode.ticket.api.impl.SimpleToken;
+import org.uic.barcode.ticket.api.impl.SimpleTraveler;
+import org.uic.barcode.ticket.api.impl.SimpleTravelerDetail;
+import org.uic.barcode.ticket.api.impl.SimpleUicRailTicket;
+import org.uic.barcode.ticket.api.spec.ICardReference;
+import org.uic.barcode.ticket.api.spec.IControlDetail;
+import org.uic.barcode.ticket.api.spec.ICustomerStatusDescription;
+import org.uic.barcode.ticket.api.spec.IExtension;
+import org.uic.barcode.ticket.api.spec.IIssuingDetail;
+import org.uic.barcode.ticket.api.spec.ILinkMode;
+import org.uic.barcode.ticket.api.spec.IOpenTicket;
+import org.uic.barcode.ticket.api.spec.IStationPassage;
+import org.uic.barcode.ticket.api.spec.ITicketLink;
+import org.uic.barcode.ticket.api.spec.IToken;
+import org.uic.barcode.ticket.api.spec.ITraveler;
+import org.uic.barcode.ticket.api.spec.IUicRailTicket;
+
+public class SimpleUICTestTicket {
+
+
+ public static IUicRailTicket getUicTestTicket() {
+ IUicRailTicket ticket = new SimpleUicRailTicket();
+ populateTicket(ticket);
+ return ticket;
+ }
+
+
+ private static void populateTicket(IUicRailTicket ticket) {
+
+ ticket.setControlDetails(new SimpleControlDetail());
+ populate(ticket.getControlDetails());
+
+
+ ticket.setIssuerDetails(new SimpleIssuingDetail());
+ populateIssuingData(ticket.getIssuerDetails());
+
+ SimpleTravelerDetail td = new SimpleTravelerDetail();
+ populateTravelerData(td);
+ ticket.setTravelerDetails(td);
+
+
+ //OpenTicket
+ IOpenTicket do1 = new SimpleOpenTicket();
+ populate(do1);
+ ticket.addOpenTicket(do1);
+
+ //StationPassage
+ IStationPassage do2 = new SimpleStationPassage();
+ populateStationPassage(do2);
+ ticket.addStationPassage(do2);
+
+ //token
+ IToken to = new SimpleToken();
+ to.setTokenProvider("VDV");
+ byte[] ba = { (byte) 0x82, (byte) 0xDA };
+ to.setToken(ba);
+
+
+ ticket.addExtension(populateExtension());
+
+ }
+
+ /*
+ ticket stationPassage : {
+ productName "passage"
+ ,station {8312345}
+ ,stationNameUTF8 { "Amsterdam" }
+ ,validFromDay 0
+ ,validUntilDay 4
+ }
+ */
+ private static void populateStationPassage(IStationPassage sp) {
+ sp.setProductName("passage");
+
+ try {
+ Date date = new SimpleDateFormat("dd/MM/yyyy").parse("01/01/2018");
+ sp.setValidFrom(date);
+ } catch (ParseException e) {
+ e.printStackTrace();
+ }
+ try {
+ Date date = new SimpleDateFormat("dd/MM/yyyy").parse("04/01/2018");
+ sp.setValidUntil(date);
+ } catch (ParseException e) {
+ e.printStackTrace();
+ }
+
+ sp.addStation("8312345");
+ sp.addStationName("Amsterdam");
+
+ }
+
+ /*
+ {
+ token {tokenProviderIA5 "VDV", token '82DA'H }
+ ,ticket openTicket : {
+ returnIncluded FALSE
+ infoText "openTicketInfo"
+ }
+ }
+ */
+
+ private static void populate(IOpenTicket otd) {
+ otd.setInfoText("openTicketInfo");
+ otd.setReturnIncluded(false);
+ }
+
+
+ /*
+ ,travelerDetail{
+ traveler {
+ {
+ firstName "John"
+ ,secondName "Dow"
+ ,idCard "12345"
+ ,ticketHolder TRUE
+ ,status {{customerStatusDescr "senior" }}
+ }
+ }
+ ,groupName "myGroup"
+ }
+ */
+
+ private static void populateTravelerData(SimpleTravelerDetail td) {
+ td.setGroupName("myGroup");
+ ITraveler tr = new SimpleTraveler();
+ tr.setIdCard("12345");
+ tr.setFirstName("John");
+ tr.setSecondName("Dow");
+ tr.setTicketHolder(true);
+ ICustomerStatusDescription cst = new SimpleCustomerStatusDescription();
+ tr.addStatusDescription(cst);
+ cst.setDescription("senior");
+ td.addTraveler(tr);
+ }
+
+ /*
+ ,issuingDetail {
+ issuerNum 1080
+ issuingYear 2018
+ issuingDay 1
+ specimen TRUE,
+ securePaperTicket FALSE,
+ activated TRUE,
+ issuerPNR "issuerTestPNR",
+ issuedOnLine 12
+ }
+ */
+ private static void populateIssuingData(IIssuingDetail iIssuingDetail) {
+
+ try {
+ Date date = new SimpleDateFormat("dd/MM/yyyy").parse("01/01/2018");
+ iIssuingDetail.setIssuer("1080");
+ iIssuingDetail.setIssuingDate(date);
+ } catch (ParseException e) {
+ e.printStackTrace();
+ }
+
+
+ iIssuingDetail.setIssuerPNR("issuerTestPNR");
+ iIssuingDetail.setSpecimen(true);
+ iIssuingDetail.setSecurePaperTicket(false);
+ iIssuingDetail.setActivated(true);
+ iIssuingDetail.setIssuedOnLine(12);
+ }
+
+ /*
+ ,extension {
+ { extensionId "1", extensionData '82DA'H }
+ ,{ extensionId "2", extensionData '83DA'H }
+ }
+ */
+ private static IExtension populateExtension() {
+ IExtension ed1 = new SimpleExtension();
+ ed1.setId("1");
+ byte[] ba1 = { (byte) 0x82, (byte) 0xDA };
+ ed1.setBinarydata(ba1);
+ return ed1;
+ }
+
+ /*
+ ,controlDetail {
+ identificationByCardReference {
+ { trailingCardIdNum 100 }
+ }
+ ,identificationByIdCard FALSE
+ ,identificationByPassportId FALSE
+ ,passportValidationRequired FALSE
+ ,onlineValidationRequired FALSE
+ ,ageCheckRequired FALSE
+ ,reductionCardCheckRequired FALSE
+ ,infoText "cd"
+ ,includedTickets {
+ { productOwnerIA5 "test" }
+ }
+ }
+ */
+ private static void populate(IControlDetail iControlDetail) {
+ iControlDetail.setInfoText("cd");
+ iControlDetail.setAgeCheckRequired(false);
+ iControlDetail.setIdentificationByIdCard(false);
+ iControlDetail.setIdentificationByPassportId(false);
+ iControlDetail.setOnlineValidationRequired(false);
+ iControlDetail.setPassportValidationRequired(false);
+ iControlDetail.setReductionCardCheckRequired(false);
+ iControlDetail.getIdentificationByCardReference().add(populateCardRefrence());
+ iControlDetail.addLinkedTicket(populateLinkedTicket());
+ }
+
+
+ /*
+ *
+ */
+ private static ITicketLink populateLinkedTicket() {
+ ITicketLink it = new SimpleTicketLink();
+ it.setProductOwner("test");
+ it.setLinkMode(ILinkMode.issuedTogether);
+ return it;
+ }
+
+ /*
+ {
+ trailingCardIdNum 100
+ }
+ */
+ private static ICardReference populateCardRefrence() {
+ ICardReference cr = new SimpleCardReference();
+ cr.setTrailingCardId("100");
+ return cr;
+ }
+
+
+ public static void compare(IUicRailTicket ticket1, IUicRailTicket ticket2) {
+
+ assert(ticket1.getIssuerDetails().getIssuer().equals(ticket2.getIssuerDetails().getIssuer()));
+
+ }
+
+
+}
diff --git a/src/test/java/org/uic/barcode/test/utils/TestUtils.java b/src/test/java/org/uic/barcode/test/utils/TestUtils.java new file mode 100644 index 0000000..7311ae4 --- /dev/null +++ b/src/test/java/org/uic/barcode/test/utils/TestUtils.java @@ -0,0 +1,38 @@ +package org.uic.barcode.test.utils;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+public class TestUtils {
+
+ final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
+
+ public static String hexStringFromBytes(byte[] bytes) {
+ char[] hexChars = new char[bytes.length * 2];
+ for (int j = 0; j < bytes.length; j++) {
+ int v = bytes[j] & 0xFF;
+ hexChars[j * 2] = hexArray[v >>> 4];
+ hexChars[j * 2 + 1] = hexArray[v & 0x0F];
+ }
+ return new String(hexChars);
+ }
+
+
+ public static Date parseDate (String source){
+
+ SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
+
+ try {
+ return formatter.parse(source);
+ } catch (ParseException e) {
+ try {
+ return formatter.parse("2001-01-01");
+ } catch (ParseException e1) {
+ return null;
+ }
+ }
+
+ }
+
+}
diff --git a/src/test/java/org/uic/barcode/ticket/api/test/CustomerCardTimeZoneTestV1.java b/src/test/java/org/uic/barcode/ticket/api/test/CustomerCardTimeZoneTestV1.java new file mode 100644 index 0000000..315774e --- /dev/null +++ b/src/test/java/org/uic/barcode/ticket/api/test/CustomerCardTimeZoneTestV1.java @@ -0,0 +1,312 @@ +package org.uic.barcode.ticket.api.test;
+
+import java.io.IOException;
+import java.text.DateFormat;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.TimeZone;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.uic.barcode.asn1.uper.UperEncoder;
+import org.uic.barcode.ticket.EncodingFormatException;
+import org.uic.barcode.ticket.api.asn.omv1.UicRailTicketData;
+import org.uic.barcode.ticket.api.spec.ICustomerCard;
+import org.uic.barcode.ticket.api.spec.IUicRailTicket;
+import org.uic.barcode.ticket.api.test.testtickets.CustomerCardTestTicketV1;
+import org.uic.barcode.ticket.api.utils.Api2OpenAsnEncoder;
+import org.uic.barcode.ticket.api.utils.OpenAsn2ApiDecoder;
+
+
+/**
+ * The Class CustomerCardTestV1.
+ *
+ *
+ *
+ */
+public class CustomerCardTimeZoneTestV1 {
+
+ /** The low level encoded test ticket test case 1 . */
+ private byte[] encoded1 = null;
+
+ /** The low level encoded test ticket test case 2 . */
+ private byte[] encoded2 = null;
+
+ /** The low level encoded test ticket test case 3 . */
+ private byte[] encoded3 = null;
+
+ /** The decoder. */
+ OpenAsn2ApiDecoder decoder = new OpenAsn2ApiDecoder();
+
+ /** The encoder. */
+ Api2OpenAsnEncoder encoder = new Api2OpenAsnEncoder();
+
+ /** The API ticket low level encoded for case 1. */
+ IUicRailTicket iTicketDecodedFromAsn1Case1 = null;
+
+ /** The API ticket low level encoded for case 2. */
+ IUicRailTicket iTicketDecodedFromAsn1Case2 = null;
+
+ /** The API ticket low level encoded for case 3. */
+ IUicRailTicket iTicketDecodedFromAsn1Case3 = null;
+
+
+ /** The ticket decoded 1. */
+ IUicRailTicket iTicketDecodedCase1 = null;
+
+ /** The i ticket decoded 2. */
+ IUicRailTicket iTicketDecodedCase2 = null;
+
+ /** The i ticket decoded 3. */
+ IUicRailTicket iTicketDecodedCase3 = null;
+
+ byte[] encodedInTimeZone1 = null;
+ byte[] encodedInTimeZone2 = null;
+ byte[] encodedInTimeZone3 = null;
+
+ String issuingDate1 = null;
+ String issuingDate2 = null;
+ String issuingDate3 = null;
+
+ String validFromDate1 = null;
+ String validFromDate2 = null;
+ String validFromDate3 = null;
+
+ String validUntilDate1 = null;
+ String validUntilDate2 = null;
+ String validUntilDate3 = null;
+
+ TimeZone defaulttimeZone = null;
+
+ /**
+ * Prepare tickets.
+ */
+ @Before public void prepare() {
+
+ defaulttimeZone = TimeZone.getDefault();
+
+ UicRailTicketData ticket1 = CustomerCardTestTicketV1.getUicTestTicket();
+ UicRailTicketData ticket2 = CustomerCardTestTicketV1.getUicTestTicket();
+ UicRailTicketData ticket3 = CustomerCardTestTicketV1.getUicTestTicket();
+
+ //encode in UTC time zone
+ TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
+
+ encoded1 = UperEncoder.encode(ticket1);
+ encoded2 = UperEncoder.encode(ticket2);
+ encoded3 = UperEncoder.encode(ticket3);
+
+ }
+
+ /**
+ * clean up
+ */
+ @After public void resetTimeZone() {
+ TimeZone.setDefault(defaulttimeZone);
+ }
+
+
+ /**
+ * Test encode test tickets in UTC and decode in CET.
+ *
+ * @throws IllegalArgumentException the illegal argument exception
+ * @throws IllegalAccessException the illegal access exception
+ * @throws ParseException
+ */
+ @Test public void testCETCETencoding() throws IllegalArgumentException, IllegalAccessException, ParseException {
+
+ //get tickets
+ decodeTestTicketUTC();
+
+ //set the dates in the time zone CET
+ TimeZone.setDefault(TimeZone.getTimeZone("CET"));
+ iTicketDecodedFromAsn1Case1.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-12:30" ));
+ iTicketDecodedFromAsn1Case2.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-00:30" ));
+ iTicketDecodedFromAsn1Case3.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.03-23:30" ));
+
+ setValidFromDateTo("2021.03.14-00:00");
+ setValidUntilDateTo("2022.03.24-23:59");
+
+ encode("CET");
+ decode("CET");
+ decodedDateToStrings();
+
+ assert(issuingDate1.equals("Thu Mar 04 13:30:00 CET 2021" ));
+ assert(issuingDate2.equals("Thu Mar 04 01:30:00 CET 2021" ));
+ assert(issuingDate3.equals("Thu Mar 04 00:30:00 CET 2021" ));
+
+ assert(validFromDate1.equals("Sun Mar 14 00:00:00 CET 2021" ));
+ assert(validFromDate2.equals("Sun Mar 14 00:00:00 CET 2021" ));
+ assert(validFromDate3.equals("Sun Mar 14 00:00:00 CET 2021" ));
+
+ assert(validUntilDate1.equals("Thu Mar 24 23:59:00 CET 2022" ));
+ assert(validUntilDate2.equals("Thu Mar 24 23:59:00 CET 2022" ));
+ assert(validUntilDate3.equals("Thu Mar 24 23:59:00 CET 2022" ));
+
+
+ }
+
+
+ /**
+ * Test encode test tickets in UTC and decode in CET.
+ *
+ * @throws IllegalArgumentException the illegal argument exception
+ * @throws IllegalAccessException the illegal access exception
+ * @throws ParseException
+ */
+ @Test public void testGMTCETencoding() throws IllegalArgumentException, IllegalAccessException, ParseException {
+
+ //get tickets
+ decodeTestTicketUTC();
+
+ //set the dates in the time zone GMT
+ TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
+ iTicketDecodedFromAsn1Case1.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-12:30" ));
+ iTicketDecodedFromAsn1Case2.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-00:30" ));
+ iTicketDecodedFromAsn1Case3.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.03-23:30" ));
+
+ setValidFromDateTo("2021.03.14-00:00");
+ setValidUntilDateTo("2021.03.24-23:59");
+
+ encode("GMT");
+ decode("CET");
+ decodedDateToStrings();
+
+ assert(issuingDate1.equals("Thu Mar 04 13:30:00 CET 2021" ));
+ assert(issuingDate2.equals("Thu Mar 04 01:30:00 CET 2021" ));
+ assert(issuingDate3.equals("Thu Mar 04 00:30:00 CET 2021" ));
+
+ assert(validFromDate1.equals("Sun Mar 14 00:00:00 CET 2021" ));
+ assert(validFromDate2.equals("Sun Mar 14 00:00:00 CET 2021" ));
+ assert(validFromDate3.equals("Sun Mar 14 00:00:00 CET 2021" ));
+
+ assert(validUntilDate1.equals("Wed Mar 24 23:59:00 CET 2021" ));
+ assert(validUntilDate2.equals("Wed Mar 24 23:59:00 CET 2021" ));
+ assert(validUntilDate3.equals("Wed Mar 24 23:59:00 CET 2021" ));
+
+
+ }
+
+ /**
+ * Test encode test tickets in UTC and decode in CET.
+ *
+ * @throws IllegalArgumentException the illegal argument exception
+ * @throws IllegalAccessException the illegal access exception
+ * @throws ParseException
+ */
+ @Test public void testCETGMTencoding() throws IllegalArgumentException, IllegalAccessException, ParseException {
+
+ //get tickets
+ decodeTestTicketUTC();
+
+ //set the dates in the time zone GMT
+ TimeZone.setDefault(TimeZone.getTimeZone("CET"));
+ iTicketDecodedFromAsn1Case1.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-12:30" ));
+ iTicketDecodedFromAsn1Case2.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-00:30" ));
+ iTicketDecodedFromAsn1Case3.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.03-23:30" ));
+
+ setValidFromDateTo("2021.03.14-00:00");
+ setValidUntilDateTo("2022.03.24-23:59");
+
+ encode("CET");
+ decode("GMT");
+ decodedDateToStrings();
+
+ assert(issuingDate1.equals("Thu Mar 04 12:30:00 GMT 2021" ));
+ assert(issuingDate2.equals("Thu Mar 04 00:30:00 GMT 2021" ));
+ assert(issuingDate3.equals("Wed Mar 03 23:30:00 GMT 2021" ));
+
+ assert(validFromDate1.equals("Sun Mar 14 00:00:00 GMT 2021" ));
+ assert(validFromDate2.equals("Sun Mar 14 00:00:00 GMT 2021" ));
+ assert(validFromDate3.equals("Sun Mar 14 00:00:00 GMT 2021" ));
+
+ assert(validUntilDate1.equals("Thu Mar 24 23:59:00 GMT 2022" ));
+ assert(validUntilDate2.equals("Thu Mar 24 23:59:00 GMT 2022" ));
+ assert(validUntilDate3.equals("Thu Mar 24 23:59:00 GMT 2022" ));
+
+
+ }
+
+ private void decodedDateToStrings() {
+
+ issuingDate1 = iTicketDecodedCase1.getIssuerDetails().getIssuingDate().toString();
+ issuingDate2 = iTicketDecodedCase2.getIssuerDetails().getIssuingDate().toString();
+ issuingDate3 = iTicketDecodedCase3.getIssuerDetails().getIssuingDate().toString();
+
+ validFromDate1 = ((ICustomerCard) iTicketDecodedCase1.getDocumentData().iterator().next()).getValidFrom().toString();
+ validFromDate2 = ((ICustomerCard) iTicketDecodedCase2.getDocumentData().iterator().next()).getValidFrom().toString();
+ validFromDate3 = ((ICustomerCard) iTicketDecodedCase3.getDocumentData().iterator().next()).getValidFrom().toString();
+
+ validUntilDate1 = ((ICustomerCard) iTicketDecodedCase1.getDocumentData().iterator().next()).getValidUntil().toString();
+ validUntilDate2 = ((ICustomerCard) iTicketDecodedCase2.getDocumentData().iterator().next()).getValidUntil().toString();
+ validUntilDate3 = ((ICustomerCard) iTicketDecodedCase3.getDocumentData().iterator().next()).getValidUntil().toString();
+
+
+ }
+
+
+ private void setValidFromDateTo(String dateString) throws ParseException {
+ DateFormat dateFormat = new SimpleDateFormat( "yyyy.MM.dd-HH:mm" );
+ Date date = dateFormat.parse(dateString);
+ ((ICustomerCard) iTicketDecodedFromAsn1Case1.getDocumentData().iterator().next()).setValidFrom(date);
+ ((ICustomerCard) iTicketDecodedFromAsn1Case2.getDocumentData().iterator().next()).setValidFrom(date);
+ ((ICustomerCard) iTicketDecodedFromAsn1Case3.getDocumentData().iterator().next()).setValidFrom(date);
+ }
+
+ private void setValidUntilDateTo(String dateString) throws ParseException {
+ DateFormat dateFormat = new SimpleDateFormat( "yyyy.MM.dd-HH:mm" );
+ Date date = dateFormat.parse(dateString);
+ ((ICustomerCard) iTicketDecodedFromAsn1Case1.getDocumentData().iterator().next()).setValidUntil(date);
+ ((ICustomerCard) iTicketDecodedFromAsn1Case2.getDocumentData().iterator().next()).setValidUntil(date);
+ ((ICustomerCard) iTicketDecodedFromAsn1Case3.getDocumentData().iterator().next()).setValidUntil(date);
+ }
+
+
+ /**
+ * Encode in Time Zone
+ */
+ private void encode(String timeZone) {
+ TimeZone.setDefault(TimeZone.getTimeZone(timeZone));
+ try {
+ encodedInTimeZone1 = encoder.encode(iTicketDecodedFromAsn1Case1);
+ encodedInTimeZone2 = encoder.encode(iTicketDecodedFromAsn1Case2);
+ encodedInTimeZone3 = encoder.encode(iTicketDecodedFromAsn1Case3);
+ } catch (EncodingFormatException e) {
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * Decode in Time Zone
+ */
+ private void decode(String timeZone) {
+ TimeZone.setDefault(TimeZone.getTimeZone(timeZone));
+
+ try {
+ iTicketDecodedCase1 = decoder.decodeFromAsn(encodedInTimeZone1);
+ iTicketDecodedCase2 = decoder.decodeFromAsn(encodedInTimeZone2);
+ iTicketDecodedCase3 = decoder.decodeFromAsn(encodedInTimeZone3);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+
+ /**
+ * Encode decode.
+ */
+ private void decodeTestTicketUTC() {
+ TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
+ try {
+ iTicketDecodedFromAsn1Case1 = decoder.decodeFromAsn(encoded1);
+ iTicketDecodedFromAsn1Case2 = decoder.decodeFromAsn(encoded2);
+ iTicketDecodedFromAsn1Case3 = decoder.decodeFromAsn(encoded3);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ }
+
+}
diff --git a/src/test/java/org/uic/barcode/ticket/api/test/CustomerCardTimeZoneTestV2.java b/src/test/java/org/uic/barcode/ticket/api/test/CustomerCardTimeZoneTestV2.java new file mode 100644 index 0000000..e8857ab --- /dev/null +++ b/src/test/java/org/uic/barcode/ticket/api/test/CustomerCardTimeZoneTestV2.java @@ -0,0 +1,312 @@ +package org.uic.barcode.ticket.api.test;
+
+import java.io.IOException;
+import java.text.DateFormat;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.TimeZone;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.uic.barcode.asn1.uper.UperEncoder;
+import org.uic.barcode.ticket.EncodingFormatException;
+import org.uic.barcode.ticket.api.asn.omv2.UicRailTicketData;
+import org.uic.barcode.ticket.api.spec.ICustomerCard;
+import org.uic.barcode.ticket.api.spec.IUicRailTicket;
+import org.uic.barcode.ticket.api.test.testtickets.CustomerCardTestTicketV2;
+import org.uic.barcode.ticket.api.utils.Api2OpenAsnEncoderV2;
+import org.uic.barcode.ticket.api.utils.OpenAsn2ApiDecoderV2;
+
+
+/**
+ * The Class CustomerCardTestV1.
+ *
+ *
+ *
+ */
+public class CustomerCardTimeZoneTestV2 {
+
+ /** The low level encoded test ticket test case 1 . */
+ private byte[] encoded1 = null;
+
+ /** The low level encoded test ticket test case 2 . */
+ private byte[] encoded2 = null;
+
+ /** The low level encoded test ticket test case 3 . */
+ private byte[] encoded3 = null;
+
+ /** The decoder. */
+ OpenAsn2ApiDecoderV2 decoder = new OpenAsn2ApiDecoderV2();
+
+ /** The encoder. */
+ Api2OpenAsnEncoderV2 encoder = new Api2OpenAsnEncoderV2();
+
+ /** The API ticket low level encoded for case 1. */
+ IUicRailTicket iTicketDecodedFromAsn1Case1 = null;
+
+ /** The API ticket low level encoded for case 2. */
+ IUicRailTicket iTicketDecodedFromAsn1Case2 = null;
+
+ /** The API ticket low level encoded for case 3. */
+ IUicRailTicket iTicketDecodedFromAsn1Case3 = null;
+
+
+ /** The ticket decoded 1. */
+ IUicRailTicket iTicketDecodedCase1 = null;
+
+ /** The i ticket decoded 2. */
+ IUicRailTicket iTicketDecodedCase2 = null;
+
+ /** The i ticket decoded 3. */
+ IUicRailTicket iTicketDecodedCase3 = null;
+
+ byte[] encodedInTimeZone1 = null;
+ byte[] encodedInTimeZone2 = null;
+ byte[] encodedInTimeZone3 = null;
+
+ String issuingDate1 = null;
+ String issuingDate2 = null;
+ String issuingDate3 = null;
+
+ String validFromDate1 = null;
+ String validFromDate2 = null;
+ String validFromDate3 = null;
+
+ String validUntilDate1 = null;
+ String validUntilDate2 = null;
+ String validUntilDate3 = null;
+
+ TimeZone defaulttimeZone = null;
+
+ /**
+ * Prepare tickets.
+ */
+ @Before public void prepare() {
+
+ defaulttimeZone = TimeZone.getDefault();
+
+ UicRailTicketData ticket1 = CustomerCardTestTicketV2.getUicTestTicket();
+ UicRailTicketData ticket2 = CustomerCardTestTicketV2.getUicTestTicket();
+ UicRailTicketData ticket3 = CustomerCardTestTicketV2.getUicTestTicket();
+
+ //encode in UTC time zone
+ TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
+
+ encoded1 = UperEncoder.encode(ticket1);
+ encoded2 = UperEncoder.encode(ticket2);
+ encoded3 = UperEncoder.encode(ticket3);
+
+ }
+
+ /**
+ * clean up
+ */
+ @After public void resetTimeZone() {
+ TimeZone.setDefault(defaulttimeZone);
+ }
+
+
+ /**
+ * Test encode test tickets in UTC and decode in CET.
+ *
+ * @throws IllegalArgumentException the illegal argument exception
+ * @throws IllegalAccessException the illegal access exception
+ * @throws ParseException
+ */
+ @Test public void testCETCETencoding() throws IllegalArgumentException, IllegalAccessException, ParseException {
+
+ //get tickets
+ decodeTestTicketUTC();
+
+ //set the dates in the time zone CET
+ TimeZone.setDefault(TimeZone.getTimeZone("CET"));
+ iTicketDecodedFromAsn1Case1.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-12:30" ));
+ iTicketDecodedFromAsn1Case2.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-00:30" ));
+ iTicketDecodedFromAsn1Case3.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.03-23:30" ));
+
+ setValidFromDateTo("2021.03.14-00:00");
+ setValidUntilDateTo("2022.03.24-23:59");
+
+ encode("CET");
+ decode("CET");
+ decodedDateToStrings();
+
+ assert(issuingDate1.equals("Thu Mar 04 13:30:00 CET 2021" ));
+ assert(issuingDate2.equals("Thu Mar 04 01:30:00 CET 2021" ));
+ assert(issuingDate3.equals("Thu Mar 04 00:30:00 CET 2021" ));
+
+ assert(validFromDate1.equals("Sun Mar 14 00:00:00 CET 2021" ));
+ assert(validFromDate2.equals("Sun Mar 14 00:00:00 CET 2021" ));
+ assert(validFromDate3.equals("Sun Mar 14 00:00:00 CET 2021" ));
+
+ assert(validUntilDate1.equals("Thu Mar 24 23:59:00 CET 2022" ));
+ assert(validUntilDate2.equals("Thu Mar 24 23:59:00 CET 2022" ));
+ assert(validUntilDate3.equals("Thu Mar 24 23:59:00 CET 2022" ));
+
+
+ }
+
+
+ /**
+ * Test encode test tickets in UTC and decode in CET.
+ *
+ * @throws IllegalArgumentException the illegal argument exception
+ * @throws IllegalAccessException the illegal access exception
+ * @throws ParseException
+ */
+ @Test public void testGMTCETencoding() throws IllegalArgumentException, IllegalAccessException, ParseException {
+
+ //get tickets
+ decodeTestTicketUTC();
+
+ //set the dates in the time zone GMT
+ TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
+ iTicketDecodedFromAsn1Case1.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-12:30" ));
+ iTicketDecodedFromAsn1Case2.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-00:30" ));
+ iTicketDecodedFromAsn1Case3.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.03-23:30" ));
+
+ setValidFromDateTo("2021.03.14-00:00");
+ setValidUntilDateTo("2021.03.24-23:59");
+
+ encode("GMT");
+ decode("CET");
+ decodedDateToStrings();
+
+ assert(issuingDate1.equals("Thu Mar 04 13:30:00 CET 2021" ));
+ assert(issuingDate2.equals("Thu Mar 04 01:30:00 CET 2021" ));
+ assert(issuingDate3.equals("Thu Mar 04 00:30:00 CET 2021" ));
+
+ assert(validFromDate1.equals("Sun Mar 14 00:00:00 CET 2021" ));
+ assert(validFromDate2.equals("Sun Mar 14 00:00:00 CET 2021" ));
+ assert(validFromDate3.equals("Sun Mar 14 00:00:00 CET 2021" ));
+
+ assert(validUntilDate1.equals("Wed Mar 24 23:59:00 CET 2021" ));
+ assert(validUntilDate2.equals("Wed Mar 24 23:59:00 CET 2021" ));
+ assert(validUntilDate3.equals("Wed Mar 24 23:59:00 CET 2021" ));
+
+
+ }
+
+ /**
+ * Test encode test tickets in UTC and decode in CET.
+ *
+ * @throws IllegalArgumentException the illegal argument exception
+ * @throws IllegalAccessException the illegal access exception
+ * @throws ParseException
+ */
+ @Test public void testCETGMTencoding() throws IllegalArgumentException, IllegalAccessException, ParseException {
+
+ //get tickets
+ decodeTestTicketUTC();
+
+ //set the dates in the time zone GMT
+ TimeZone.setDefault(TimeZone.getTimeZone("CET"));
+ iTicketDecodedFromAsn1Case1.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-12:30" ));
+ iTicketDecodedFromAsn1Case2.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-00:30" ));
+ iTicketDecodedFromAsn1Case3.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.03-23:30" ));
+
+ setValidFromDateTo("2021.03.14-00:00");
+ setValidUntilDateTo("2022.03.24-23:59");
+
+ encode("CET");
+ decode("GMT");
+ decodedDateToStrings();
+
+ assert(issuingDate1.equals("Thu Mar 04 12:30:00 GMT 2021" ));
+ assert(issuingDate2.equals("Thu Mar 04 00:30:00 GMT 2021" ));
+ assert(issuingDate3.equals("Wed Mar 03 23:30:00 GMT 2021" ));
+
+ assert(validFromDate1.equals("Sun Mar 14 00:00:00 GMT 2021" ));
+ assert(validFromDate2.equals("Sun Mar 14 00:00:00 GMT 2021" ));
+ assert(validFromDate3.equals("Sun Mar 14 00:00:00 GMT 2021" ));
+
+ assert(validUntilDate1.equals("Thu Mar 24 23:59:00 GMT 2022" ));
+ assert(validUntilDate2.equals("Thu Mar 24 23:59:00 GMT 2022" ));
+ assert(validUntilDate3.equals("Thu Mar 24 23:59:00 GMT 2022" ));
+
+
+ }
+
+ private void decodedDateToStrings() {
+
+ issuingDate1 = iTicketDecodedCase1.getIssuerDetails().getIssuingDate().toString();
+ issuingDate2 = iTicketDecodedCase2.getIssuerDetails().getIssuingDate().toString();
+ issuingDate3 = iTicketDecodedCase3.getIssuerDetails().getIssuingDate().toString();
+
+ validFromDate1 = ((ICustomerCard) iTicketDecodedCase1.getDocumentData().iterator().next()).getValidFrom().toString();
+ validFromDate2 = ((ICustomerCard) iTicketDecodedCase2.getDocumentData().iterator().next()).getValidFrom().toString();
+ validFromDate3 = ((ICustomerCard) iTicketDecodedCase3.getDocumentData().iterator().next()).getValidFrom().toString();
+
+ validUntilDate1 = ((ICustomerCard) iTicketDecodedCase1.getDocumentData().iterator().next()).getValidUntil().toString();
+ validUntilDate2 = ((ICustomerCard) iTicketDecodedCase2.getDocumentData().iterator().next()).getValidUntil().toString();
+ validUntilDate3 = ((ICustomerCard) iTicketDecodedCase3.getDocumentData().iterator().next()).getValidUntil().toString();
+
+
+ }
+
+
+ private void setValidFromDateTo(String dateString) throws ParseException {
+ DateFormat dateFormat = new SimpleDateFormat( "yyyy.MM.dd-HH:mm" );
+ Date date = dateFormat.parse(dateString);
+ ((ICustomerCard) iTicketDecodedFromAsn1Case1.getDocumentData().iterator().next()).setValidFrom(date);
+ ((ICustomerCard) iTicketDecodedFromAsn1Case2.getDocumentData().iterator().next()).setValidFrom(date);
+ ((ICustomerCard) iTicketDecodedFromAsn1Case3.getDocumentData().iterator().next()).setValidFrom(date);
+ }
+
+ private void setValidUntilDateTo(String dateString) throws ParseException {
+ DateFormat dateFormat = new SimpleDateFormat( "yyyy.MM.dd-HH:mm" );
+ Date date = dateFormat.parse(dateString);
+ ((ICustomerCard) iTicketDecodedFromAsn1Case1.getDocumentData().iterator().next()).setValidUntil(date);
+ ((ICustomerCard) iTicketDecodedFromAsn1Case2.getDocumentData().iterator().next()).setValidUntil(date);
+ ((ICustomerCard) iTicketDecodedFromAsn1Case3.getDocumentData().iterator().next()).setValidUntil(date);
+ }
+
+
+ /**
+ * Encode in Time Zone
+ */
+ private void encode(String timeZone) {
+ TimeZone.setDefault(TimeZone.getTimeZone(timeZone));
+ try {
+ encodedInTimeZone1 = encoder.encode(iTicketDecodedFromAsn1Case1);
+ encodedInTimeZone2 = encoder.encode(iTicketDecodedFromAsn1Case2);
+ encodedInTimeZone3 = encoder.encode(iTicketDecodedFromAsn1Case3);
+ } catch (EncodingFormatException e) {
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * Decode in Time Zone
+ */
+ private void decode(String timeZone) {
+ TimeZone.setDefault(TimeZone.getTimeZone(timeZone));
+
+ try {
+ iTicketDecodedCase1 = decoder.decodeFromAsn(encodedInTimeZone1);
+ iTicketDecodedCase2 = decoder.decodeFromAsn(encodedInTimeZone2);
+ iTicketDecodedCase3 = decoder.decodeFromAsn(encodedInTimeZone3);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+
+ /**
+ * Encode decode.
+ */
+ private void decodeTestTicketUTC() {
+ TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
+ try {
+ iTicketDecodedFromAsn1Case1 = decoder.decodeFromAsn(encoded1);
+ iTicketDecodedFromAsn1Case2 = decoder.decodeFromAsn(encoded2);
+ iTicketDecodedFromAsn1Case3 = decoder.decodeFromAsn(encoded3);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ }
+
+}
diff --git a/src/test/java/org/uic/barcode/ticket/api/test/DateTimeUtilsTest.java b/src/test/java/org/uic/barcode/ticket/api/test/DateTimeUtilsTest.java new file mode 100644 index 0000000..5b2e578 --- /dev/null +++ b/src/test/java/org/uic/barcode/ticket/api/test/DateTimeUtilsTest.java @@ -0,0 +1,45 @@ +package org.uic.barcode.ticket.api.test; + +import java.util.Calendar; +import java.util.TimeZone; +import org.junit.Test; +import org.uic.barcode.ticket.api.utils.DateTimeUtils; + + +public class DateTimeUtilsTest{ + + + + @Test public void testDateDiff1() throws IllegalArgumentException, IllegalAccessException { + + Calendar issuing = Calendar.getInstance(); + issuing.setTimeZone(TimeZone.getTimeZone("UTC")); + issuing.set(Calendar.YEAR,2017); + issuing.set(Calendar.DAY_OF_MONTH, 31); + issuing.set(Calendar.MONTH, 12); + issuing.set(Calendar.HOUR_OF_DAY, 23); + issuing.set(Calendar.MINUTE, 5); + + Calendar local = Calendar.getInstance(); + local.set(Calendar.YEAR,2018); + local.set(Calendar.DAY_OF_MONTH, 1); + local.set(Calendar.MONTH, 1); + issuing.set(Calendar.HOUR_OF_DAY, 0); + issuing.set(Calendar.MINUTE, 5); + + Long diff = DateTimeUtils.getDateDifference(issuing.getTime(), local.getTime()); + + + assert(diff.intValue() == 1); + } + + + + + + + + + + +} diff --git a/src/test/java/org/uic/barcode/ticket/api/test/FipTimeZoneTestV1.java b/src/test/java/org/uic/barcode/ticket/api/test/FipTimeZoneTestV1.java new file mode 100644 index 0000000..c9b0f31 --- /dev/null +++ b/src/test/java/org/uic/barcode/ticket/api/test/FipTimeZoneTestV1.java @@ -0,0 +1,345 @@ +package org.uic.barcode.ticket.api.test; + +import java.io.IOException; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.TimeZone; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.uic.barcode.asn1.uper.UperEncoder; +import org.uic.barcode.ticket.EncodingFormatException; +import org.uic.barcode.ticket.api.asn.omv1.UicRailTicketData; +import org.uic.barcode.ticket.api.spec.IFipTicket; +import org.uic.barcode.ticket.api.spec.IUicRailTicket; +import org.uic.barcode.ticket.api.test.testtickets.FipTimeZoneTestTicketV1; +import org.uic.barcode.ticket.api.utils.Api2OpenAsnEncoder; +import org.uic.barcode.ticket.api.utils.OpenAsn2ApiDecoder; + + +/** + * The Class FipTimeZoneTestV1. + * + * + * + */ +public class FipTimeZoneTestV1 { + + /** The low level encoded test ticket test case 1 . */ + private byte[] encoded1 = null; + + /** The low level encoded test ticket test case 2 . */ + private byte[] encoded2 = null; + + /** The low level encoded test ticket test case 3 . */ + private byte[] encoded3 = null; + + /** The decoder. */ + OpenAsn2ApiDecoder decoder = new OpenAsn2ApiDecoder(); + + /** The encoder. */ + Api2OpenAsnEncoder encoder = new Api2OpenAsnEncoder(); + + /** The API ticket low level encoded for case 1. */ + IUicRailTicket iTicketDecodedFromAsn1Case1 = null; + + /** The API ticket low level encoded for case 2. */ + IUicRailTicket iTicketDecodedFromAsn1Case2 = null; + + /** The API ticket low level encoded for case 3. */ + IUicRailTicket iTicketDecodedFromAsn1Case3 = null; + + + /** The ticket decoded 1. */ + IUicRailTicket iTicketDecodedCase1 = null; + + /** The i ticket decoded 2. */ + IUicRailTicket iTicketDecodedCase2 = null; + + /** The i ticket decoded 3. */ + IUicRailTicket iTicketDecodedCase3 = null; + + byte[] encodedInTimeZone1 = null; + byte[] encodedInTimeZone2 = null; + byte[] encodedInTimeZone3 = null; + + String issuingDate1 = null; + String issuingDate2 = null; + String issuingDate3 = null; + + String validFrom1 = null; + String validFrom2 = null; + String validFrom3 = null; + + String validUntil1 = null; + String validUntil2 = null; + String validUntil3 = null; + + String activationDate1 = null; + String activationDate2 = null; + String activationDate3 = null; + + TimeZone defaulttimeZone = null; + + /** + * Prepare tickets. + */ + @Before public void prepare() { + + defaulttimeZone = TimeZone.getDefault(); + + UicRailTicketData ticket1 = FipTimeZoneTestTicketV1.getUicTestTicket(); + UicRailTicketData ticket2 = FipTimeZoneTestTicketV1.getUicTestTicket(); + UicRailTicketData ticket3 = FipTimeZoneTestTicketV1.getUicTestTicket(); + + //encode in UTC time zone + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); + + encoded1 = UperEncoder.encode(ticket1); + encoded2 = UperEncoder.encode(ticket2); + encoded3 = UperEncoder.encode(ticket3); + + } + + /** + * clean up + */ + @After public void resetTimeZone() { + TimeZone.setDefault(defaulttimeZone); + } + + + /** + * Test encode test tickets in UTC and decode in CET. + * + * @throws IllegalArgumentException the illegal argument exception + * @throws IllegalAccessException the illegal access exception + * @throws ParseException + */ + @Test public void testCETCETencoding() throws IllegalArgumentException, IllegalAccessException, ParseException { + + //get tickets + decodeTestTicketUTC(); + + //set the dates in the time zone CET + TimeZone.setDefault(TimeZone.getTimeZone("CET")); + iTicketDecodedFromAsn1Case1.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-12:30" )); + iTicketDecodedFromAsn1Case2.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-00:30" )); + iTicketDecodedFromAsn1Case3.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.03-23:30" )); + + setValidFromTo("2021.03.14-00:00"); + setValidUntilTo("2021.03.24-23:59"); + setActivatedDayTo("2021.03.14-00:00"); + + encode("CET"); + decode("CET"); + decodedDateToStrings(); + + assert(issuingDate1.equals("Thu Mar 04 13:30:00 CET 2021" )); + assert(issuingDate2.equals("Thu Mar 04 01:30:00 CET 2021" )); + assert(issuingDate3.equals("Thu Mar 04 00:30:00 CET 2021" )); + + assert(validFrom1.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(validFrom2.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(validFrom3.equals("Sun Mar 14 00:00:00 CET 2021" )); + + assert(validUntil1.equals("Wed Mar 24 23:59:00 CET 2021" )); + assert(validUntil2.equals("Wed Mar 24 23:59:00 CET 2021" )); + assert(validUntil3.equals("Wed Mar 24 23:59:00 CET 2021" )); + + assert(activationDate1.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(activationDate2.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(activationDate3.equals("Sun Mar 14 00:00:00 CET 2021" )); + + } + + + /** + * Test encode test tickets in UTC and decode in CET. + * + * @throws IllegalArgumentException the illegal argument exception + * @throws IllegalAccessException the illegal access exception + * @throws ParseException + */ + @Test public void testGMTCETencoding() throws IllegalArgumentException, IllegalAccessException, ParseException { + + //get tickets + decodeTestTicketUTC(); + + //set the dates in the time zone GMT + TimeZone.setDefault(TimeZone.getTimeZone("GMT")); + iTicketDecodedFromAsn1Case1.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-12:30" )); + iTicketDecodedFromAsn1Case2.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-00:30" )); + iTicketDecodedFromAsn1Case3.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.03-23:30" )); + + setValidFromTo("2021.03.14-00:00"); + setValidUntilTo("2021.03.24-23:59"); + setActivatedDayTo("2021.03.14-00:00"); + + encode("GMT"); + decode("CET"); + decodedDateToStrings(); + + assert(issuingDate1.equals("Thu Mar 04 13:30:00 CET 2021" )); + assert(issuingDate2.equals("Thu Mar 04 01:30:00 CET 2021" )); + assert(issuingDate3.equals("Thu Mar 04 00:30:00 CET 2021" )); + + assert(validFrom1.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(validFrom2.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(validFrom3.equals("Sun Mar 14 00:00:00 CET 2021" )); + + assert(validUntil1.equals("Wed Mar 24 23:59:00 CET 2021" )); + assert(validUntil2.equals("Wed Mar 24 23:59:00 CET 2021" )); + assert(validUntil3.equals("Wed Mar 24 23:59:00 CET 2021" )); + + assert(activationDate1.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(activationDate2.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(activationDate3.equals("Sun Mar 14 00:00:00 CET 2021" )); + + } + + /** + * Test encode test tickets in UTC and decode in CET. + * + * @throws IllegalArgumentException the illegal argument exception + * @throws IllegalAccessException the illegal access exception + * @throws ParseException + */ + @Test public void testCETGMTencoding() throws IllegalArgumentException, IllegalAccessException, ParseException { + + //get tickets + decodeTestTicketUTC(); + + //set the dates in the time zone GMT + TimeZone.setDefault(TimeZone.getTimeZone("CET")); + iTicketDecodedFromAsn1Case1.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-12:30" )); + iTicketDecodedFromAsn1Case2.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-00:30" )); + iTicketDecodedFromAsn1Case3.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.03-23:30" )); + + setValidFromTo("2021.03.14-00:00"); + setValidUntilTo("2021.03.24-23:59"); + setActivatedDayTo("2021.03.14-00:00"); + + encode("CET"); + decode("GMT"); + decodedDateToStrings(); + + assert(issuingDate1.equals("Thu Mar 04 12:30:00 GMT 2021" )); + assert(issuingDate2.equals("Thu Mar 04 00:30:00 GMT 2021" )); + assert(issuingDate3.equals("Wed Mar 03 23:30:00 GMT 2021" )); + + assert(validFrom1.equals("Sun Mar 14 00:00:00 GMT 2021" )); + assert(validFrom2.equals("Sun Mar 14 00:00:00 GMT 2021" )); + assert(validFrom3.equals("Sun Mar 14 00:00:00 GMT 2021" )); + + assert(validUntil1.equals("Wed Mar 24 23:59:00 GMT 2021" )); + assert(validUntil2.equals("Wed Mar 24 23:59:00 GMT 2021" )); + assert(validUntil3.equals("Wed Mar 24 23:59:00 GMT 2021" )); + + assert(activationDate1.equals("Sun Mar 14 00:00:00 GMT 2021" )); + assert(activationDate2.equals("Sun Mar 14 00:00:00 GMT 2021" )); + assert(activationDate3.equals("Sun Mar 14 00:00:00 GMT 2021" )); + + } + + private void decodedDateToStrings() { + + issuingDate1 = iTicketDecodedCase1.getIssuerDetails().getIssuingDate().toString(); + issuingDate2 = iTicketDecodedCase2.getIssuerDetails().getIssuingDate().toString(); + issuingDate3 = iTicketDecodedCase3.getIssuerDetails().getIssuingDate().toString(); + + validFrom1 = ((IFipTicket) iTicketDecodedCase1.getDocumentData().iterator().next()).getValidFrom().toString(); + validFrom2 = ((IFipTicket) iTicketDecodedCase2.getDocumentData().iterator().next()).getValidFrom().toString(); + validFrom3 = ((IFipTicket) iTicketDecodedCase3.getDocumentData().iterator().next()).getValidFrom().toString(); + + validUntil1 = ((IFipTicket) iTicketDecodedCase1.getDocumentData().iterator().next()).getValidUntil().toString(); + validUntil2 = ((IFipTicket) iTicketDecodedCase2.getDocumentData().iterator().next()).getValidUntil().toString(); + validUntil3 = ((IFipTicket) iTicketDecodedCase3.getDocumentData().iterator().next()).getValidUntil().toString(); + + activationDate1 = ((IFipTicket) iTicketDecodedCase1.getDocumentData().iterator().next()).getActivatedDays().iterator().next().toString(); + activationDate2 = ((IFipTicket) iTicketDecodedCase2.getDocumentData().iterator().next()).getActivatedDays().iterator().next().toString(); + activationDate3 = ((IFipTicket) iTicketDecodedCase3.getDocumentData().iterator().next()).getActivatedDays().iterator().next().toString(); + + } + + + private void setActivatedDayTo(String dateString) throws ParseException { + DateFormat dateFormat = new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ); + Date date = dateFormat.parse(dateString); + ((IFipTicket) iTicketDecodedFromAsn1Case1.getDocumentData().iterator().next()).getActivatedDays().clear(); + ((IFipTicket) iTicketDecodedFromAsn1Case1.getDocumentData().iterator().next()).getActivatedDays().add(date); + + ((IFipTicket) iTicketDecodedFromAsn1Case2.getDocumentData().iterator().next()).getActivatedDays().clear(); + ((IFipTicket) iTicketDecodedFromAsn1Case2.getDocumentData().iterator().next()).getActivatedDays().add(date); + + ((IFipTicket) iTicketDecodedFromAsn1Case3.getDocumentData().iterator().next()).getActivatedDays().clear(); + ((IFipTicket) iTicketDecodedFromAsn1Case3.getDocumentData().iterator().next()).getActivatedDays().add(date); + + } + + private void setValidFromTo(String dateString) throws ParseException { + DateFormat dateFormat = new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ); + Date date = dateFormat.parse(dateString); + ((IFipTicket) iTicketDecodedFromAsn1Case1.getDocumentData().iterator().next()).setValidFrom(date); + ((IFipTicket) iTicketDecodedFromAsn1Case2.getDocumentData().iterator().next()).setValidFrom(date); + ((IFipTicket) iTicketDecodedFromAsn1Case3.getDocumentData().iterator().next()).setValidFrom(date); + } + + private void setValidUntilTo(String dateString) throws ParseException { + DateFormat dateFormat = new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ); + Date date = dateFormat.parse(dateString); + ((IFipTicket) iTicketDecodedFromAsn1Case1.getDocumentData().iterator().next()).setValidUntil(date); + ((IFipTicket) iTicketDecodedFromAsn1Case2.getDocumentData().iterator().next()).setValidUntil(date); + ((IFipTicket) iTicketDecodedFromAsn1Case3.getDocumentData().iterator().next()).setValidUntil(date); + } + + + /** + * Encode in Time Zone + */ + private void encode(String timeZone) { + TimeZone.setDefault(TimeZone.getTimeZone(timeZone)); + try { + encodedInTimeZone1 = encoder.encode(iTicketDecodedFromAsn1Case1); + encodedInTimeZone2 = encoder.encode(iTicketDecodedFromAsn1Case2); + encodedInTimeZone3 = encoder.encode(iTicketDecodedFromAsn1Case3); + } catch (EncodingFormatException e) { + e.printStackTrace(); + } + } + + /** + * Decode in Time Zone + */ + private void decode(String timeZone) { + TimeZone.setDefault(TimeZone.getTimeZone(timeZone)); + + try { + iTicketDecodedCase1 = decoder.decodeFromAsn(encodedInTimeZone1); + iTicketDecodedCase2 = decoder.decodeFromAsn(encodedInTimeZone2); + iTicketDecodedCase3 = decoder.decodeFromAsn(encodedInTimeZone3); + } catch (IOException e) { + e.printStackTrace(); + } + } + + + /** + * Encode decode. + */ + private void decodeTestTicketUTC() { + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); + try { + iTicketDecodedFromAsn1Case1 = decoder.decodeFromAsn(encoded1); + iTicketDecodedFromAsn1Case2 = decoder.decodeFromAsn(encoded2); + iTicketDecodedFromAsn1Case3 = decoder.decodeFromAsn(encoded3); + } catch (IOException e) { + e.printStackTrace(); + } + + } + +} diff --git a/src/test/java/org/uic/barcode/ticket/api/test/FipTimeZoneTestV2.java b/src/test/java/org/uic/barcode/ticket/api/test/FipTimeZoneTestV2.java new file mode 100644 index 0000000..89b6562 --- /dev/null +++ b/src/test/java/org/uic/barcode/ticket/api/test/FipTimeZoneTestV2.java @@ -0,0 +1,345 @@ +package org.uic.barcode.ticket.api.test; + +import java.io.IOException; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.TimeZone; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.uic.barcode.asn1.uper.UperEncoder; +import org.uic.barcode.ticket.EncodingFormatException; +import org.uic.barcode.ticket.api.asn.omv2.UicRailTicketData; +import org.uic.barcode.ticket.api.spec.IFipTicket; +import org.uic.barcode.ticket.api.spec.IUicRailTicket; +import org.uic.barcode.ticket.api.test.testtickets.FipTimeZoneTestTicketV2; +import org.uic.barcode.ticket.api.utils.Api2OpenAsnEncoderV2; +import org.uic.barcode.ticket.api.utils.OpenAsn2ApiDecoderV2; + + +/** + * The Class FipTimeZoneTestV1. + * + * + * + */ +public class FipTimeZoneTestV2 { + + /** The low level encoded test ticket test case 1 . */ + private byte[] encoded1 = null; + + /** The low level encoded test ticket test case 2 . */ + private byte[] encoded2 = null; + + /** The low level encoded test ticket test case 3 . */ + private byte[] encoded3 = null; + + /** The decoder. */ + OpenAsn2ApiDecoderV2 decoder = new OpenAsn2ApiDecoderV2(); + + /** The encoder. */ + Api2OpenAsnEncoderV2 encoder = new Api2OpenAsnEncoderV2(); + + /** The API ticket low level encoded for case 1. */ + IUicRailTicket iTicketDecodedFromAsn1Case1 = null; + + /** The API ticket low level encoded for case 2. */ + IUicRailTicket iTicketDecodedFromAsn1Case2 = null; + + /** The API ticket low level encoded for case 3. */ + IUicRailTicket iTicketDecodedFromAsn1Case3 = null; + + + /** The ticket decoded 1. */ + IUicRailTicket iTicketDecodedCase1 = null; + + /** The i ticket decoded 2. */ + IUicRailTicket iTicketDecodedCase2 = null; + + /** The i ticket decoded 3. */ + IUicRailTicket iTicketDecodedCase3 = null; + + byte[] encodedInTimeZone1 = null; + byte[] encodedInTimeZone2 = null; + byte[] encodedInTimeZone3 = null; + + String issuingDate1 = null; + String issuingDate2 = null; + String issuingDate3 = null; + + String validFrom1 = null; + String validFrom2 = null; + String validFrom3 = null; + + String validUntil1 = null; + String validUntil2 = null; + String validUntil3 = null; + + String activationDate1 = null; + String activationDate2 = null; + String activationDate3 = null; + + TimeZone defaulttimeZone = null; + + /** + * Prepare tickets. + */ + @Before public void prepare() { + + defaulttimeZone = TimeZone.getDefault(); + + UicRailTicketData ticket1 = FipTimeZoneTestTicketV2.getUicTestTicket(); + UicRailTicketData ticket2 = FipTimeZoneTestTicketV2.getUicTestTicket(); + UicRailTicketData ticket3 = FipTimeZoneTestTicketV2.getUicTestTicket(); + + //encode in UTC time zone + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); + + encoded1 = UperEncoder.encode(ticket1); + encoded2 = UperEncoder.encode(ticket2); + encoded3 = UperEncoder.encode(ticket3); + + } + + /** + * clean up + */ + @After public void resetTimeZone() { + TimeZone.setDefault(defaulttimeZone); + } + + + /** + * Test encode test tickets in UTC and decode in CET. + * + * @throws IllegalArgumentException the illegal argument exception + * @throws IllegalAccessException the illegal access exception + * @throws ParseException + */ + @Test public void testCETCETencoding() throws IllegalArgumentException, IllegalAccessException, ParseException { + + //get tickets + decodeTestTicketUTC(); + + //set the dates in the time zone CET + TimeZone.setDefault(TimeZone.getTimeZone("CET")); + iTicketDecodedFromAsn1Case1.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-12:30" )); + iTicketDecodedFromAsn1Case2.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-00:30" )); + iTicketDecodedFromAsn1Case3.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.03-23:30" )); + + setValidFromTo("2021.03.14-00:00"); + setValidUntilTo("2021.03.24-23:59"); + setActivatedDayTo("2021.03.14-00:00"); + + encode("CET"); + decode("CET"); + decodedDateToStrings(); + + assert(issuingDate1.equals("Thu Mar 04 13:30:00 CET 2021" )); + assert(issuingDate2.equals("Thu Mar 04 01:30:00 CET 2021" )); + assert(issuingDate3.equals("Thu Mar 04 00:30:00 CET 2021" )); + + assert(validFrom1.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(validFrom2.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(validFrom3.equals("Sun Mar 14 00:00:00 CET 2021" )); + + assert(validUntil1.equals("Wed Mar 24 23:59:00 CET 2021" )); + assert(validUntil2.equals("Wed Mar 24 23:59:00 CET 2021" )); + assert(validUntil3.equals("Wed Mar 24 23:59:00 CET 2021" )); + + assert(activationDate1.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(activationDate2.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(activationDate3.equals("Sun Mar 14 00:00:00 CET 2021" )); + + } + + + /** + * Test encode test tickets in UTC and decode in CET. + * + * @throws IllegalArgumentException the illegal argument exception + * @throws IllegalAccessException the illegal access exception + * @throws ParseException + */ + @Test public void testGMTCETencoding() throws IllegalArgumentException, IllegalAccessException, ParseException { + + //get tickets + decodeTestTicketUTC(); + + //set the dates in the time zone GMT + TimeZone.setDefault(TimeZone.getTimeZone("GMT")); + iTicketDecodedFromAsn1Case1.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-12:30" )); + iTicketDecodedFromAsn1Case2.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-00:30" )); + iTicketDecodedFromAsn1Case3.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.03-23:30" )); + + setValidFromTo("2021.03.14-00:00"); + setValidUntilTo("2021.03.24-23:59"); + setActivatedDayTo("2021.03.14-00:00"); + + encode("GMT"); + decode("CET"); + decodedDateToStrings(); + + assert(issuingDate1.equals("Thu Mar 04 13:30:00 CET 2021" )); + assert(issuingDate2.equals("Thu Mar 04 01:30:00 CET 2021" )); + assert(issuingDate3.equals("Thu Mar 04 00:30:00 CET 2021" )); + + assert(validFrom1.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(validFrom2.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(validFrom3.equals("Sun Mar 14 00:00:00 CET 2021" )); + + assert(validUntil1.equals("Wed Mar 24 23:59:00 CET 2021" )); + assert(validUntil2.equals("Wed Mar 24 23:59:00 CET 2021" )); + assert(validUntil3.equals("Wed Mar 24 23:59:00 CET 2021" )); + + assert(activationDate1.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(activationDate2.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(activationDate3.equals("Sun Mar 14 00:00:00 CET 2021" )); + + } + + /** + * Test encode test tickets in UTC and decode in CET. + * + * @throws IllegalArgumentException the illegal argument exception + * @throws IllegalAccessException the illegal access exception + * @throws ParseException + */ + @Test public void testCETGMTencoding() throws IllegalArgumentException, IllegalAccessException, ParseException { + + //get tickets + decodeTestTicketUTC(); + + //set the dates in the time zone GMT + TimeZone.setDefault(TimeZone.getTimeZone("CET")); + iTicketDecodedFromAsn1Case1.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-12:30" )); + iTicketDecodedFromAsn1Case2.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-00:30" )); + iTicketDecodedFromAsn1Case3.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.03-23:30" )); + + setValidFromTo("2021.03.14-00:00"); + setValidUntilTo("2021.03.24-23:59"); + setActivatedDayTo("2021.03.14-00:00"); + + encode("CET"); + decode("GMT"); + decodedDateToStrings(); + + assert(issuingDate1.equals("Thu Mar 04 12:30:00 GMT 2021" )); + assert(issuingDate2.equals("Thu Mar 04 00:30:00 GMT 2021" )); + assert(issuingDate3.equals("Wed Mar 03 23:30:00 GMT 2021" )); + + assert(validFrom1.equals("Sun Mar 14 00:00:00 GMT 2021" )); + assert(validFrom2.equals("Sun Mar 14 00:00:00 GMT 2021" )); + assert(validFrom3.equals("Sun Mar 14 00:00:00 GMT 2021" )); + + assert(validUntil1.equals("Wed Mar 24 23:59:00 GMT 2021" )); + assert(validUntil2.equals("Wed Mar 24 23:59:00 GMT 2021" )); + assert(validUntil3.equals("Wed Mar 24 23:59:00 GMT 2021" )); + + assert(activationDate1.equals("Sun Mar 14 00:00:00 GMT 2021" )); + assert(activationDate2.equals("Sun Mar 14 00:00:00 GMT 2021" )); + assert(activationDate3.equals("Sun Mar 14 00:00:00 GMT 2021" )); + + } + + private void decodedDateToStrings() { + + issuingDate1 = iTicketDecodedCase1.getIssuerDetails().getIssuingDate().toString(); + issuingDate2 = iTicketDecodedCase2.getIssuerDetails().getIssuingDate().toString(); + issuingDate3 = iTicketDecodedCase3.getIssuerDetails().getIssuingDate().toString(); + + validFrom1 = ((IFipTicket) iTicketDecodedCase1.getDocumentData().iterator().next()).getValidFrom().toString(); + validFrom2 = ((IFipTicket) iTicketDecodedCase2.getDocumentData().iterator().next()).getValidFrom().toString(); + validFrom3 = ((IFipTicket) iTicketDecodedCase3.getDocumentData().iterator().next()).getValidFrom().toString(); + + validUntil1 = ((IFipTicket) iTicketDecodedCase1.getDocumentData().iterator().next()).getValidUntil().toString(); + validUntil2 = ((IFipTicket) iTicketDecodedCase2.getDocumentData().iterator().next()).getValidUntil().toString(); + validUntil3 = ((IFipTicket) iTicketDecodedCase3.getDocumentData().iterator().next()).getValidUntil().toString(); + + activationDate1 = ((IFipTicket) iTicketDecodedCase1.getDocumentData().iterator().next()).getActivatedDays().iterator().next().toString(); + activationDate2 = ((IFipTicket) iTicketDecodedCase2.getDocumentData().iterator().next()).getActivatedDays().iterator().next().toString(); + activationDate3 = ((IFipTicket) iTicketDecodedCase3.getDocumentData().iterator().next()).getActivatedDays().iterator().next().toString(); + + } + + + private void setActivatedDayTo(String dateString) throws ParseException { + DateFormat dateFormat = new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ); + Date date = dateFormat.parse(dateString); + ((IFipTicket) iTicketDecodedFromAsn1Case1.getDocumentData().iterator().next()).getActivatedDays().clear(); + ((IFipTicket) iTicketDecodedFromAsn1Case1.getDocumentData().iterator().next()).getActivatedDays().add(date); + + ((IFipTicket) iTicketDecodedFromAsn1Case2.getDocumentData().iterator().next()).getActivatedDays().clear(); + ((IFipTicket) iTicketDecodedFromAsn1Case2.getDocumentData().iterator().next()).getActivatedDays().add(date); + + ((IFipTicket) iTicketDecodedFromAsn1Case3.getDocumentData().iterator().next()).getActivatedDays().clear(); + ((IFipTicket) iTicketDecodedFromAsn1Case3.getDocumentData().iterator().next()).getActivatedDays().add(date); + + } + + private void setValidFromTo(String dateString) throws ParseException { + DateFormat dateFormat = new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ); + Date date = dateFormat.parse(dateString); + ((IFipTicket) iTicketDecodedFromAsn1Case1.getDocumentData().iterator().next()).setValidFrom(date); + ((IFipTicket) iTicketDecodedFromAsn1Case2.getDocumentData().iterator().next()).setValidFrom(date); + ((IFipTicket) iTicketDecodedFromAsn1Case3.getDocumentData().iterator().next()).setValidFrom(date); + } + + private void setValidUntilTo(String dateString) throws ParseException { + DateFormat dateFormat = new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ); + Date date = dateFormat.parse(dateString); + ((IFipTicket) iTicketDecodedFromAsn1Case1.getDocumentData().iterator().next()).setValidUntil(date); + ((IFipTicket) iTicketDecodedFromAsn1Case2.getDocumentData().iterator().next()).setValidUntil(date); + ((IFipTicket) iTicketDecodedFromAsn1Case3.getDocumentData().iterator().next()).setValidUntil(date); + } + + + /** + * Encode in Time Zone + */ + private void encode(String timeZone) { + TimeZone.setDefault(TimeZone.getTimeZone(timeZone)); + try { + encodedInTimeZone1 = encoder.encode(iTicketDecodedFromAsn1Case1); + encodedInTimeZone2 = encoder.encode(iTicketDecodedFromAsn1Case2); + encodedInTimeZone3 = encoder.encode(iTicketDecodedFromAsn1Case3); + } catch (EncodingFormatException e) { + e.printStackTrace(); + } + } + + /** + * Decode in Time Zone + */ + private void decode(String timeZone) { + TimeZone.setDefault(TimeZone.getTimeZone(timeZone)); + + try { + iTicketDecodedCase1 = decoder.decodeFromAsn(encodedInTimeZone1); + iTicketDecodedCase2 = decoder.decodeFromAsn(encodedInTimeZone2); + iTicketDecodedCase3 = decoder.decodeFromAsn(encodedInTimeZone3); + } catch (IOException e) { + e.printStackTrace(); + } + } + + + /** + * Encode decode. + */ + private void decodeTestTicketUTC() { + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); + try { + iTicketDecodedFromAsn1Case1 = decoder.decodeFromAsn(encoded1); + iTicketDecodedFromAsn1Case2 = decoder.decodeFromAsn(encoded2); + iTicketDecodedFromAsn1Case3 = decoder.decodeFromAsn(encoded3); + } catch (IOException e) { + e.printStackTrace(); + } + + } + +} diff --git a/src/test/java/org/uic/barcode/ticket/api/test/OpenTicketTimeZoneTestV1.java b/src/test/java/org/uic/barcode/ticket/api/test/OpenTicketTimeZoneTestV1.java new file mode 100644 index 0000000..c2d288e --- /dev/null +++ b/src/test/java/org/uic/barcode/ticket/api/test/OpenTicketTimeZoneTestV1.java @@ -0,0 +1,345 @@ +package org.uic.barcode.ticket.api.test; + +import java.io.IOException; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.TimeZone; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.uic.barcode.asn1.uper.UperEncoder; +import org.uic.barcode.ticket.EncodingFormatException; +import org.uic.barcode.ticket.api.asn.omv1.UicRailTicketData; +import org.uic.barcode.ticket.api.spec.IOpenTicket; +import org.uic.barcode.ticket.api.spec.IUicRailTicket; +import org.uic.barcode.ticket.api.test.testtickets.OpenTicketTimeZoneTestTicketV1; +import org.uic.barcode.ticket.api.utils.Api2OpenAsnEncoder; +import org.uic.barcode.ticket.api.utils.OpenAsn2ApiDecoder; + + +/** + * The Class FipTimeZoneTestV1. + * + * + * + */ +public class OpenTicketTimeZoneTestV1 { + + /** The low level encoded test ticket test case 1 . */ + private byte[] encoded1 = null; + + /** The low level encoded test ticket test case 2 . */ + private byte[] encoded2 = null; + + /** The low level encoded test ticket test case 3 . */ + private byte[] encoded3 = null; + + /** The decoder. */ + OpenAsn2ApiDecoder decoder = new OpenAsn2ApiDecoder(); + + /** The encoder. */ + Api2OpenAsnEncoder encoder = new Api2OpenAsnEncoder(); + + /** The API ticket low level encoded for case 1. */ + IUicRailTicket iTicketDecodedFromAsn1Case1 = null; + + /** The API ticket low level encoded for case 2. */ + IUicRailTicket iTicketDecodedFromAsn1Case2 = null; + + /** The API ticket low level encoded for case 3. */ + IUicRailTicket iTicketDecodedFromAsn1Case3 = null; + + + /** The ticket decoded 1. */ + IUicRailTicket iTicketDecodedCase1 = null; + + /** The i ticket decoded 2. */ + IUicRailTicket iTicketDecodedCase2 = null; + + /** The i ticket decoded 3. */ + IUicRailTicket iTicketDecodedCase3 = null; + + byte[] encodedInTimeZone1 = null; + byte[] encodedInTimeZone2 = null; + byte[] encodedInTimeZone3 = null; + + String issuingDate1 = null; + String issuingDate2 = null; + String issuingDate3 = null; + + String validFrom1 = null; + String validFrom2 = null; + String validFrom3 = null; + + String validUntil1 = null; + String validUntil2 = null; + String validUntil3 = null; + + String activationDate1 = null; + String activationDate2 = null; + String activationDate3 = null; + + TimeZone defaulttimeZone = null; + + /** + * Prepare tickets. + */ + @Before public void prepare() { + + defaulttimeZone = TimeZone.getDefault(); + + UicRailTicketData ticket1 = OpenTicketTimeZoneTestTicketV1.getUicTestTicket(); + UicRailTicketData ticket2 = OpenTicketTimeZoneTestTicketV1.getUicTestTicket(); + UicRailTicketData ticket3 = OpenTicketTimeZoneTestTicketV1.getUicTestTicket(); + + //encode in UTC time zone + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); + + encoded1 = UperEncoder.encode(ticket1); + encoded2 = UperEncoder.encode(ticket2); + encoded3 = UperEncoder.encode(ticket3); + + } + + /** + * clean up + */ + @After public void resetTimeZone() { + TimeZone.setDefault(defaulttimeZone); + } + + + /** + * Test encode test tickets in UTC and decode in CET. + * + * @throws IllegalArgumentException the illegal argument exception + * @throws IllegalAccessException the illegal access exception + * @throws ParseException + */ + @Test public void testCETCETencoding() throws IllegalArgumentException, IllegalAccessException, ParseException { + + //get tickets + decodeTestTicketUTC(); + + //set the dates in the time zone CET + TimeZone.setDefault(TimeZone.getTimeZone("CET")); + iTicketDecodedFromAsn1Case1.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-12:30" )); + iTicketDecodedFromAsn1Case2.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-00:30" )); + iTicketDecodedFromAsn1Case3.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.03-23:30" )); + + setValidFromTo("2021.03.14-00:00"); + setValidUntilTo("2021.03.24-23:59"); + setActivatedDayTo("2021.03.14-00:00"); + + encode("CET"); + decode("CET"); + decodedDateToStrings(); + + assert(issuingDate1.equals("Thu Mar 04 13:30:00 CET 2021" )); + assert(issuingDate2.equals("Thu Mar 04 01:30:00 CET 2021" )); + assert(issuingDate3.equals("Thu Mar 04 00:30:00 CET 2021" )); + + assert(validFrom1.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(validFrom2.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(validFrom3.equals("Sun Mar 14 00:00:00 CET 2021" )); + + assert(validUntil1.equals("Wed Mar 24 23:59:00 CET 2021" )); + assert(validUntil2.equals("Wed Mar 24 23:59:00 CET 2021" )); + assert(validUntil3.equals("Wed Mar 24 23:59:00 CET 2021" )); + + assert(activationDate1.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(activationDate2.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(activationDate3.equals("Sun Mar 14 00:00:00 CET 2021" )); + + } + + + /** + * Test encode test tickets in UTC and decode in CET. + * + * @throws IllegalArgumentException the illegal argument exception + * @throws IllegalAccessException the illegal access exception + * @throws ParseException + */ + @Test public void testGMTCETencoding() throws IllegalArgumentException, IllegalAccessException, ParseException { + + //get tickets + decodeTestTicketUTC(); + + //set the dates in the time zone GMT + TimeZone.setDefault(TimeZone.getTimeZone("GMT")); + iTicketDecodedFromAsn1Case1.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-12:30" )); + iTicketDecodedFromAsn1Case2.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-00:30" )); + iTicketDecodedFromAsn1Case3.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.03-23:30" )); + + setValidFromTo("2021.03.14-00:00"); + setValidUntilTo("2021.03.24-23:59"); + setActivatedDayTo("2021.03.14-00:00"); + + encode("GMT"); + decode("CET"); + decodedDateToStrings(); + + assert(issuingDate1.equals("Thu Mar 04 13:30:00 CET 2021" )); + assert(issuingDate2.equals("Thu Mar 04 01:30:00 CET 2021" )); + assert(issuingDate3.equals("Thu Mar 04 00:30:00 CET 2021" )); + + assert(validFrom1.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(validFrom2.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(validFrom3.equals("Sun Mar 14 00:00:00 CET 2021" )); + + assert(validUntil1.equals("Wed Mar 24 23:59:00 CET 2021" )); + assert(validUntil2.equals("Wed Mar 24 23:59:00 CET 2021" )); + assert(validUntil3.equals("Wed Mar 24 23:59:00 CET 2021" )); + + assert(activationDate1.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(activationDate2.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(activationDate3.equals("Sun Mar 14 00:00:00 CET 2021" )); + + } + + /** + * Test encode test tickets in UTC and decode in CET. + * + * @throws IllegalArgumentException the illegal argument exception + * @throws IllegalAccessException the illegal access exception + * @throws ParseException + */ + @Test public void testCETGMTencoding() throws IllegalArgumentException, IllegalAccessException, ParseException { + + //get tickets + decodeTestTicketUTC(); + + //set the dates in the time zone GMT + TimeZone.setDefault(TimeZone.getTimeZone("CET")); + iTicketDecodedFromAsn1Case1.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-12:30" )); + iTicketDecodedFromAsn1Case2.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-00:30" )); + iTicketDecodedFromAsn1Case3.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.03-23:30" )); + + setValidFromTo("2021.03.14-00:00"); + setValidUntilTo("2021.03.24-23:59"); + setActivatedDayTo("2021.03.14-00:00"); + + encode("CET"); + decode("GMT"); + decodedDateToStrings(); + + assert(issuingDate1.equals("Thu Mar 04 12:30:00 GMT 2021" )); + assert(issuingDate2.equals("Thu Mar 04 00:30:00 GMT 2021" )); + assert(issuingDate3.equals("Wed Mar 03 23:30:00 GMT 2021" )); + + assert(validFrom1.equals("Sun Mar 14 00:00:00 GMT 2021" )); + assert(validFrom2.equals("Sun Mar 14 00:00:00 GMT 2021" )); + assert(validFrom3.equals("Sun Mar 14 00:00:00 GMT 2021" )); + + assert(validUntil1.equals("Wed Mar 24 23:59:00 GMT 2021" )); + assert(validUntil2.equals("Wed Mar 24 23:59:00 GMT 2021" )); + assert(validUntil3.equals("Wed Mar 24 23:59:00 GMT 2021" )); + + assert(activationDate1.equals("Sun Mar 14 00:00:00 GMT 2021" )); + assert(activationDate2.equals("Sun Mar 14 00:00:00 GMT 2021" )); + assert(activationDate3.equals("Sun Mar 14 00:00:00 GMT 2021" )); + + } + + private void decodedDateToStrings() { + + issuingDate1 = iTicketDecodedCase1.getIssuerDetails().getIssuingDate().toString(); + issuingDate2 = iTicketDecodedCase2.getIssuerDetails().getIssuingDate().toString(); + issuingDate3 = iTicketDecodedCase3.getIssuerDetails().getIssuingDate().toString(); + + validFrom1 = ((IOpenTicket) iTicketDecodedCase1.getDocumentData().iterator().next()).getValidFrom().toString(); + validFrom2 = ((IOpenTicket) iTicketDecodedCase2.getDocumentData().iterator().next()).getValidFrom().toString(); + validFrom3 = ((IOpenTicket) iTicketDecodedCase3.getDocumentData().iterator().next()).getValidFrom().toString(); + + validUntil1 = ((IOpenTicket) iTicketDecodedCase1.getDocumentData().iterator().next()).getValidUntil().toString(); + validUntil2 = ((IOpenTicket) iTicketDecodedCase2.getDocumentData().iterator().next()).getValidUntil().toString(); + validUntil3 = ((IOpenTicket) iTicketDecodedCase3.getDocumentData().iterator().next()).getValidUntil().toString(); + + activationDate1 = ((IOpenTicket) iTicketDecodedCase1.getDocumentData().iterator().next()).getActivatedDays().iterator().next().toString(); + activationDate2 = ((IOpenTicket) iTicketDecodedCase2.getDocumentData().iterator().next()).getActivatedDays().iterator().next().toString(); + activationDate3 = ((IOpenTicket) iTicketDecodedCase3.getDocumentData().iterator().next()).getActivatedDays().iterator().next().toString(); + + } + + + private void setActivatedDayTo(String dateString) throws ParseException { + DateFormat dateFormat = new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ); + Date date = dateFormat.parse(dateString); + ((IOpenTicket) iTicketDecodedFromAsn1Case1.getDocumentData().iterator().next()).getActivatedDays().clear(); + ((IOpenTicket) iTicketDecodedFromAsn1Case1.getDocumentData().iterator().next()).getActivatedDays().add(date); + + ((IOpenTicket) iTicketDecodedFromAsn1Case2.getDocumentData().iterator().next()).getActivatedDays().clear(); + ((IOpenTicket) iTicketDecodedFromAsn1Case2.getDocumentData().iterator().next()).getActivatedDays().add(date); + + ((IOpenTicket) iTicketDecodedFromAsn1Case3.getDocumentData().iterator().next()).getActivatedDays().clear(); + ((IOpenTicket) iTicketDecodedFromAsn1Case3.getDocumentData().iterator().next()).getActivatedDays().add(date); + + } + + private void setValidFromTo(String dateString) throws ParseException { + DateFormat dateFormat = new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ); + Date date = dateFormat.parse(dateString); + ((IOpenTicket) iTicketDecodedFromAsn1Case1.getDocumentData().iterator().next()).setValidFrom(date); + ((IOpenTicket) iTicketDecodedFromAsn1Case2.getDocumentData().iterator().next()).setValidFrom(date); + ((IOpenTicket) iTicketDecodedFromAsn1Case3.getDocumentData().iterator().next()).setValidFrom(date); + } + + private void setValidUntilTo(String dateString) throws ParseException { + DateFormat dateFormat = new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ); + Date date = dateFormat.parse(dateString); + ((IOpenTicket) iTicketDecodedFromAsn1Case1.getDocumentData().iterator().next()).setValidUntil(date); + ((IOpenTicket) iTicketDecodedFromAsn1Case2.getDocumentData().iterator().next()).setValidUntil(date); + ((IOpenTicket) iTicketDecodedFromAsn1Case3.getDocumentData().iterator().next()).setValidUntil(date); + } + + + /** + * Encode in Time Zone + */ + private void encode(String timeZone) { + TimeZone.setDefault(TimeZone.getTimeZone(timeZone)); + try { + encodedInTimeZone1 = encoder.encode(iTicketDecodedFromAsn1Case1); + encodedInTimeZone2 = encoder.encode(iTicketDecodedFromAsn1Case2); + encodedInTimeZone3 = encoder.encode(iTicketDecodedFromAsn1Case3); + } catch (EncodingFormatException e) { + e.printStackTrace(); + } + } + + /** + * Decode in Time Zone + */ + private void decode(String timeZone) { + TimeZone.setDefault(TimeZone.getTimeZone(timeZone)); + + try { + iTicketDecodedCase1 = decoder.decodeFromAsn(encodedInTimeZone1); + iTicketDecodedCase2 = decoder.decodeFromAsn(encodedInTimeZone2); + iTicketDecodedCase3 = decoder.decodeFromAsn(encodedInTimeZone3); + } catch (IOException e) { + e.printStackTrace(); + } + } + + + /** + * Encode decode. + */ + private void decodeTestTicketUTC() { + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); + try { + iTicketDecodedFromAsn1Case1 = decoder.decodeFromAsn(encoded1); + iTicketDecodedFromAsn1Case2 = decoder.decodeFromAsn(encoded2); + iTicketDecodedFromAsn1Case3 = decoder.decodeFromAsn(encoded3); + } catch (IOException e) { + e.printStackTrace(); + } + + } + +} diff --git a/src/test/java/org/uic/barcode/ticket/api/test/OpenTicketTimeZoneTestV2.java b/src/test/java/org/uic/barcode/ticket/api/test/OpenTicketTimeZoneTestV2.java new file mode 100644 index 0000000..ab16716 --- /dev/null +++ b/src/test/java/org/uic/barcode/ticket/api/test/OpenTicketTimeZoneTestV2.java @@ -0,0 +1,345 @@ +package org.uic.barcode.ticket.api.test; + +import java.io.IOException; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.TimeZone; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.uic.barcode.asn1.uper.UperEncoder; +import org.uic.barcode.ticket.EncodingFormatException; +import org.uic.barcode.ticket.api.asn.omv2.UicRailTicketData; +import org.uic.barcode.ticket.api.spec.IOpenTicket; +import org.uic.barcode.ticket.api.spec.IUicRailTicket; +import org.uic.barcode.ticket.api.test.testtickets.OpenTicketTimeZoneTestTicketV2; +import org.uic.barcode.ticket.api.utils.Api2OpenAsnEncoderV2; +import org.uic.barcode.ticket.api.utils.OpenAsn2ApiDecoderV2; + + +/** + * The Class FipTimeZoneTestV2. + * + * + * + */ +public class OpenTicketTimeZoneTestV2 { + + /** The low level encoded test ticket test case 1 . */ + private byte[] encoded1 = null; + + /** The low level encoded test ticket test case 2 . */ + private byte[] encoded2 = null; + + /** The low level encoded test ticket test case 3 . */ + private byte[] encoded3 = null; + + /** The decoder. */ + OpenAsn2ApiDecoderV2 decoder = new OpenAsn2ApiDecoderV2(); + + /** The encoder. */ + Api2OpenAsnEncoderV2 encoder = new Api2OpenAsnEncoderV2(); + + /** The API ticket low level encoded for case 1. */ + IUicRailTicket iTicketDecodedFromAsn1Case1 = null; + + /** The API ticket low level encoded for case 2. */ + IUicRailTicket iTicketDecodedFromAsn1Case2 = null; + + /** The API ticket low level encoded for case 3. */ + IUicRailTicket iTicketDecodedFromAsn1Case3 = null; + + + /** The ticket decoded 1. */ + IUicRailTicket iTicketDecodedCase1 = null; + + /** The i ticket decoded 2. */ + IUicRailTicket iTicketDecodedCase2 = null; + + /** The i ticket decoded 3. */ + IUicRailTicket iTicketDecodedCase3 = null; + + byte[] encodedInTimeZone1 = null; + byte[] encodedInTimeZone2 = null; + byte[] encodedInTimeZone3 = null; + + String issuingDate1 = null; + String issuingDate2 = null; + String issuingDate3 = null; + + String validFrom1 = null; + String validFrom2 = null; + String validFrom3 = null; + + String validUntil1 = null; + String validUntil2 = null; + String validUntil3 = null; + + String activationDate1 = null; + String activationDate2 = null; + String activationDate3 = null; + + TimeZone defaulttimeZone = null; + + /** + * Prepare tickets. + */ + @Before public void prepare() { + + defaulttimeZone = TimeZone.getDefault(); + + UicRailTicketData ticket1 = OpenTicketTimeZoneTestTicketV2.getUicTestTicket(); + UicRailTicketData ticket2 = OpenTicketTimeZoneTestTicketV2.getUicTestTicket(); + UicRailTicketData ticket3 = OpenTicketTimeZoneTestTicketV2.getUicTestTicket(); + + //encode in UTC time zone + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); + + encoded1 = UperEncoder.encode(ticket1); + encoded2 = UperEncoder.encode(ticket2); + encoded3 = UperEncoder.encode(ticket3); + + } + + /** + * clean up + */ + @After public void resetTimeZone() { + TimeZone.setDefault(defaulttimeZone); + } + + + /** + * Test encode test tickets in UTC and decode in CET. + * + * @throws IllegalArgumentException the illegal argument exception + * @throws IllegalAccessException the illegal access exception + * @throws ParseException + */ + @Test public void testCETCETencoding() throws IllegalArgumentException, IllegalAccessException, ParseException { + + //get tickets + decodeTestTicketUTC(); + + //set the dates in the time zone CET + TimeZone.setDefault(TimeZone.getTimeZone("CET")); + iTicketDecodedFromAsn1Case1.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-12:30" )); + iTicketDecodedFromAsn1Case2.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-00:30" )); + iTicketDecodedFromAsn1Case3.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.03-23:30" )); + + setValidFromTo("2021.03.14-00:00"); + setValidUntilTo("2021.03.24-23:59"); + setActivatedDayTo("2021.03.14-00:00"); + + encode("CET"); + decode("CET"); + decodedDateToStrings(); + + assert(issuingDate1.equals("Thu Mar 04 13:30:00 CET 2021" )); + assert(issuingDate2.equals("Thu Mar 04 01:30:00 CET 2021" )); + assert(issuingDate3.equals("Thu Mar 04 00:30:00 CET 2021" )); + + assert(validFrom1.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(validFrom2.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(validFrom3.equals("Sun Mar 14 00:00:00 CET 2021" )); + + assert(validUntil1.equals("Wed Mar 24 23:59:00 CET 2021" )); + assert(validUntil2.equals("Wed Mar 24 23:59:00 CET 2021" )); + assert(validUntil3.equals("Wed Mar 24 23:59:00 CET 2021" )); + + assert(activationDate1.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(activationDate2.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(activationDate3.equals("Sun Mar 14 00:00:00 CET 2021" )); + + } + + + /** + * Test encode test tickets in UTC and decode in CET. + * + * @throws IllegalArgumentException the illegal argument exception + * @throws IllegalAccessException the illegal access exception + * @throws ParseException + */ + @Test public void testGMTCETencoding() throws IllegalArgumentException, IllegalAccessException, ParseException { + + //get tickets + decodeTestTicketUTC(); + + //set the dates in the time zone GMT + TimeZone.setDefault(TimeZone.getTimeZone("GMT")); + iTicketDecodedFromAsn1Case1.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-12:30" )); + iTicketDecodedFromAsn1Case2.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-00:30" )); + iTicketDecodedFromAsn1Case3.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.03-23:30" )); + + setValidFromTo("2021.03.14-00:00"); + setValidUntilTo("2021.03.24-23:59"); + setActivatedDayTo("2021.03.14-00:00"); + + encode("GMT"); + decode("CET"); + decodedDateToStrings(); + + assert(issuingDate1.equals("Thu Mar 04 13:30:00 CET 2021" )); + assert(issuingDate2.equals("Thu Mar 04 01:30:00 CET 2021" )); + assert(issuingDate3.equals("Thu Mar 04 00:30:00 CET 2021" )); + + assert(validFrom1.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(validFrom2.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(validFrom3.equals("Sun Mar 14 00:00:00 CET 2021" )); + + assert(validUntil1.equals("Wed Mar 24 23:59:00 CET 2021" )); + assert(validUntil2.equals("Wed Mar 24 23:59:00 CET 2021" )); + assert(validUntil3.equals("Wed Mar 24 23:59:00 CET 2021" )); + + assert(activationDate1.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(activationDate2.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(activationDate3.equals("Sun Mar 14 00:00:00 CET 2021" )); + + } + + /** + * Test encode test tickets in UTC and decode in CET. + * + * @throws IllegalArgumentException the illegal argument exception + * @throws IllegalAccessException the illegal access exception + * @throws ParseException + */ + @Test public void testCETGMTencoding() throws IllegalArgumentException, IllegalAccessException, ParseException { + + //get tickets + decodeTestTicketUTC(); + + //set the dates in the time zone GMT + TimeZone.setDefault(TimeZone.getTimeZone("CET")); + iTicketDecodedFromAsn1Case1.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-12:30" )); + iTicketDecodedFromAsn1Case2.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-00:30" )); + iTicketDecodedFromAsn1Case3.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.03-23:30" )); + + setValidFromTo("2021.03.14-00:00"); + setValidUntilTo("2021.03.24-23:59"); + setActivatedDayTo("2021.03.14-00:00"); + + encode("CET"); + decode("GMT"); + decodedDateToStrings(); + + assert(issuingDate1.equals("Thu Mar 04 12:30:00 GMT 2021" )); + assert(issuingDate2.equals("Thu Mar 04 00:30:00 GMT 2021" )); + assert(issuingDate3.equals("Wed Mar 03 23:30:00 GMT 2021" )); + + assert(validFrom1.equals("Sun Mar 14 00:00:00 GMT 2021" )); + assert(validFrom2.equals("Sun Mar 14 00:00:00 GMT 2021" )); + assert(validFrom3.equals("Sun Mar 14 00:00:00 GMT 2021" )); + + assert(validUntil1.equals("Wed Mar 24 23:59:00 GMT 2021" )); + assert(validUntil2.equals("Wed Mar 24 23:59:00 GMT 2021" )); + assert(validUntil3.equals("Wed Mar 24 23:59:00 GMT 2021" )); + + assert(activationDate1.equals("Sun Mar 14 00:00:00 GMT 2021" )); + assert(activationDate2.equals("Sun Mar 14 00:00:00 GMT 2021" )); + assert(activationDate3.equals("Sun Mar 14 00:00:00 GMT 2021" )); + + } + + private void decodedDateToStrings() { + + issuingDate1 = iTicketDecodedCase1.getIssuerDetails().getIssuingDate().toString(); + issuingDate2 = iTicketDecodedCase2.getIssuerDetails().getIssuingDate().toString(); + issuingDate3 = iTicketDecodedCase3.getIssuerDetails().getIssuingDate().toString(); + + validFrom1 = ((IOpenTicket) iTicketDecodedCase1.getDocumentData().iterator().next()).getValidFrom().toString(); + validFrom2 = ((IOpenTicket) iTicketDecodedCase2.getDocumentData().iterator().next()).getValidFrom().toString(); + validFrom3 = ((IOpenTicket) iTicketDecodedCase3.getDocumentData().iterator().next()).getValidFrom().toString(); + + validUntil1 = ((IOpenTicket) iTicketDecodedCase1.getDocumentData().iterator().next()).getValidUntil().toString(); + validUntil2 = ((IOpenTicket) iTicketDecodedCase2.getDocumentData().iterator().next()).getValidUntil().toString(); + validUntil3 = ((IOpenTicket) iTicketDecodedCase3.getDocumentData().iterator().next()).getValidUntil().toString(); + + activationDate1 = ((IOpenTicket) iTicketDecodedCase1.getDocumentData().iterator().next()).getActivatedDays().iterator().next().toString(); + activationDate2 = ((IOpenTicket) iTicketDecodedCase2.getDocumentData().iterator().next()).getActivatedDays().iterator().next().toString(); + activationDate3 = ((IOpenTicket) iTicketDecodedCase3.getDocumentData().iterator().next()).getActivatedDays().iterator().next().toString(); + + } + + + private void setActivatedDayTo(String dateString) throws ParseException { + DateFormat dateFormat = new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ); + Date date = dateFormat.parse(dateString); + ((IOpenTicket) iTicketDecodedFromAsn1Case1.getDocumentData().iterator().next()).getActivatedDays().clear(); + ((IOpenTicket) iTicketDecodedFromAsn1Case1.getDocumentData().iterator().next()).getActivatedDays().add(date); + + ((IOpenTicket) iTicketDecodedFromAsn1Case2.getDocumentData().iterator().next()).getActivatedDays().clear(); + ((IOpenTicket) iTicketDecodedFromAsn1Case2.getDocumentData().iterator().next()).getActivatedDays().add(date); + + ((IOpenTicket) iTicketDecodedFromAsn1Case3.getDocumentData().iterator().next()).getActivatedDays().clear(); + ((IOpenTicket) iTicketDecodedFromAsn1Case3.getDocumentData().iterator().next()).getActivatedDays().add(date); + + } + + private void setValidFromTo(String dateString) throws ParseException { + DateFormat dateFormat = new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ); + Date date = dateFormat.parse(dateString); + ((IOpenTicket) iTicketDecodedFromAsn1Case1.getDocumentData().iterator().next()).setValidFrom(date); + ((IOpenTicket) iTicketDecodedFromAsn1Case2.getDocumentData().iterator().next()).setValidFrom(date); + ((IOpenTicket) iTicketDecodedFromAsn1Case3.getDocumentData().iterator().next()).setValidFrom(date); + } + + private void setValidUntilTo(String dateString) throws ParseException { + DateFormat dateFormat = new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ); + Date date = dateFormat.parse(dateString); + ((IOpenTicket) iTicketDecodedFromAsn1Case1.getDocumentData().iterator().next()).setValidUntil(date); + ((IOpenTicket) iTicketDecodedFromAsn1Case2.getDocumentData().iterator().next()).setValidUntil(date); + ((IOpenTicket) iTicketDecodedFromAsn1Case3.getDocumentData().iterator().next()).setValidUntil(date); + } + + + /** + * Encode in Time Zone + */ + private void encode(String timeZone) { + TimeZone.setDefault(TimeZone.getTimeZone(timeZone)); + try { + encodedInTimeZone1 = encoder.encode(iTicketDecodedFromAsn1Case1); + encodedInTimeZone2 = encoder.encode(iTicketDecodedFromAsn1Case2); + encodedInTimeZone3 = encoder.encode(iTicketDecodedFromAsn1Case3); + } catch (EncodingFormatException e) { + e.printStackTrace(); + } + } + + /** + * Decode in Time Zone + */ + private void decode(String timeZone) { + TimeZone.setDefault(TimeZone.getTimeZone(timeZone)); + + try { + iTicketDecodedCase1 = decoder.decodeFromAsn(encodedInTimeZone1); + iTicketDecodedCase2 = decoder.decodeFromAsn(encodedInTimeZone2); + iTicketDecodedCase3 = decoder.decodeFromAsn(encodedInTimeZone3); + } catch (IOException e) { + e.printStackTrace(); + } + } + + + /** + * Encode decode. + */ + private void decodeTestTicketUTC() { + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); + try { + iTicketDecodedFromAsn1Case1 = decoder.decodeFromAsn(encoded1); + iTicketDecodedFromAsn1Case2 = decoder.decodeFromAsn(encoded2); + iTicketDecodedFromAsn1Case3 = decoder.decodeFromAsn(encoded3); + } catch (IOException e) { + e.printStackTrace(); + } + + } + +} diff --git a/src/test/java/org/uic/barcode/ticket/api/test/ParkingTimeZoneTestV1.java b/src/test/java/org/uic/barcode/ticket/api/test/ParkingTimeZoneTestV1.java new file mode 100644 index 0000000..0cda25e --- /dev/null +++ b/src/test/java/org/uic/barcode/ticket/api/test/ParkingTimeZoneTestV1.java @@ -0,0 +1,312 @@ +package org.uic.barcode.ticket.api.test;
+
+import java.io.IOException;
+import java.text.DateFormat;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.TimeZone;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.uic.barcode.asn1.uper.UperEncoder;
+import org.uic.barcode.ticket.EncodingFormatException;
+import org.uic.barcode.ticket.api.asn.omv1.UicRailTicketData;
+import org.uic.barcode.ticket.api.spec.IParkingGround;
+import org.uic.barcode.ticket.api.spec.IUicRailTicket;
+import org.uic.barcode.ticket.api.test.testtickets.ParkingTestTicketV1;
+import org.uic.barcode.ticket.api.utils.Api2OpenAsnEncoder;
+import org.uic.barcode.ticket.api.utils.OpenAsn2ApiDecoder;
+
+
+/**
+ * The Class ParkingTestV1.
+ *
+ *
+ *
+ */
+public class ParkingTimeZoneTestV1 {
+
+ /** The low level encoded test ticket test case 1 . */
+ private byte[] encoded1 = null;
+
+ /** The low level encoded test ticket test case 2 . */
+ private byte[] encoded2 = null;
+
+ /** The low level encoded test ticket test case 3 . */
+ private byte[] encoded3 = null;
+
+ /** The decoder. */
+ OpenAsn2ApiDecoder decoder = new OpenAsn2ApiDecoder();
+
+ /** The encoder. */
+ Api2OpenAsnEncoder encoder = new Api2OpenAsnEncoder();
+
+ /** The API ticket low level encoded for case 1. */
+ IUicRailTicket iTicketDecodedFromAsn1Case1 = null;
+
+ /** The API ticket low level encoded for case 2. */
+ IUicRailTicket iTicketDecodedFromAsn1Case2 = null;
+
+ /** The API ticket low level encoded for case 3. */
+ IUicRailTicket iTicketDecodedFromAsn1Case3 = null;
+
+
+ /** The ticket decoded 1. */
+ IUicRailTicket iTicketDecodedCase1 = null;
+
+ /** The i ticket decoded 2. */
+ IUicRailTicket iTicketDecodedCase2 = null;
+
+ /** The i ticket decoded 3. */
+ IUicRailTicket iTicketDecodedCase3 = null;
+
+ byte[] encodedInTimeZone1 = null;
+ byte[] encodedInTimeZone2 = null;
+ byte[] encodedInTimeZone3 = null;
+
+ String issuingDate1 = null;
+ String issuingDate2 = null;
+ String issuingDate3 = null;
+
+ String FromParkingDateDate1 = null;
+ String FromParkingDateDate2 = null;
+ String FromParkingDateDate3 = null;
+
+ String ToParkingDateDate1 = null;
+ String ToParkingDateDate2 = null;
+ String ToParkingDateDate3 = null;
+
+ TimeZone defaulttimeZone = null;
+
+ /**
+ * Prepare tickets.
+ */
+ @Before public void prepare() {
+
+ defaulttimeZone = TimeZone.getDefault();
+
+ UicRailTicketData ticket1 = ParkingTestTicketV1.getUicTestTicket();
+ UicRailTicketData ticket2 = ParkingTestTicketV1.getUicTestTicket();
+ UicRailTicketData ticket3 = ParkingTestTicketV1.getUicTestTicket();
+
+ //encode in UTC time zone
+ TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
+
+ encoded1 = UperEncoder.encode(ticket1);
+ encoded2 = UperEncoder.encode(ticket2);
+ encoded3 = UperEncoder.encode(ticket3);
+
+ }
+
+ /**
+ * clean up
+ */
+ @After public void resetTimeZone() {
+ TimeZone.setDefault(defaulttimeZone);
+ }
+
+
+ /**
+ * Test encode test tickets in UTC and decode in CET.
+ *
+ * @throws IllegalArgumentException the illegal argument exception
+ * @throws IllegalAccessException the illegal access exception
+ * @throws ParseException
+ */
+ @Test public void testCETCETencoding() throws IllegalArgumentException, IllegalAccessException, ParseException {
+
+ //get tickets
+ decodeTestTicketUTC();
+
+ //set the dates in the time zone CET
+ TimeZone.setDefault(TimeZone.getTimeZone("CET"));
+ iTicketDecodedFromAsn1Case1.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-12:30" ));
+ iTicketDecodedFromAsn1Case2.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-00:30" ));
+ iTicketDecodedFromAsn1Case3.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.03-23:30" ));
+
+ setFromParkingDateDateTo("2021.03.14-00:00");
+ setToParkingDateDateTo("2021.03.24-23:59");
+
+ encode("CET");
+ decode("CET");
+ decodedDateToStrings();
+
+ assert(issuingDate1.equals("Thu Mar 04 13:30:00 CET 2021" ));
+ assert(issuingDate2.equals("Thu Mar 04 01:30:00 CET 2021" ));
+ assert(issuingDate3.equals("Thu Mar 04 00:30:00 CET 2021" ));
+
+ assert(FromParkingDateDate1.equals("Sun Mar 14 00:00:00 CET 2021" ));
+ assert(FromParkingDateDate2.equals("Sun Mar 14 00:00:00 CET 2021" ));
+ assert(FromParkingDateDate3.equals("Sun Mar 14 00:00:00 CET 2021" ));
+
+ assert(ToParkingDateDate1.equals("Wed Mar 24 23:59:00 CET 2021" ));
+ assert(ToParkingDateDate2.equals("Wed Mar 24 23:59:00 CET 2021" ));
+ assert(ToParkingDateDate3.equals("Wed Mar 24 23:59:00 CET 2021" ));
+
+
+ }
+
+
+ /**
+ * Test encode test tickets in UTC and decode in CET.
+ *
+ * @throws IllegalArgumentException the illegal argument exception
+ * @throws IllegalAccessException the illegal access exception
+ * @throws ParseException
+ */
+ @Test public void testGMTCETencoding() throws IllegalArgumentException, IllegalAccessException, ParseException {
+
+ //get tickets
+ decodeTestTicketUTC();
+
+ //set the dates in the time zone GMT
+ TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
+ iTicketDecodedFromAsn1Case1.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-12:30" ));
+ iTicketDecodedFromAsn1Case2.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-00:30" ));
+ iTicketDecodedFromAsn1Case3.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.03-23:30" ));
+
+ setFromParkingDateDateTo("2021.03.14-00:00");
+ setToParkingDateDateTo("2021.03.24-23:59");
+
+ encode("GMT");
+ decode("CET");
+ decodedDateToStrings();
+
+ assert(issuingDate1.equals("Thu Mar 04 13:30:00 CET 2021" ));
+ assert(issuingDate2.equals("Thu Mar 04 01:30:00 CET 2021" ));
+ assert(issuingDate3.equals("Thu Mar 04 00:30:00 CET 2021" ));
+
+ assert(FromParkingDateDate1.equals("Sun Mar 14 00:00:00 CET 2021" ));
+ assert(FromParkingDateDate2.equals("Sun Mar 14 00:00:00 CET 2021" ));
+ assert(FromParkingDateDate3.equals("Sun Mar 14 00:00:00 CET 2021" ));
+
+ assert(ToParkingDateDate1.equals("Wed Mar 24 23:59:00 CET 2021" ));
+ assert(ToParkingDateDate2.equals("Wed Mar 24 23:59:00 CET 2021" ));
+ assert(ToParkingDateDate3.equals("Wed Mar 24 23:59:00 CET 2021" ));
+
+
+ }
+
+ /**
+ * Test encode test tickets in UTC and decode in CET.
+ *
+ * @throws IllegalArgumentException the illegal argument exception
+ * @throws IllegalAccessException the illegal access exception
+ * @throws ParseException
+ */
+ @Test public void testCETGMTencoding() throws IllegalArgumentException, IllegalAccessException, ParseException {
+
+ //get tickets
+ decodeTestTicketUTC();
+
+ //set the dates in the time zone GMT
+ TimeZone.setDefault(TimeZone.getTimeZone("CET"));
+ iTicketDecodedFromAsn1Case1.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-12:30" ));
+ iTicketDecodedFromAsn1Case2.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-00:30" ));
+ iTicketDecodedFromAsn1Case3.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.03-23:30" ));
+
+ setFromParkingDateDateTo("2021.03.14-00:00");
+ setToParkingDateDateTo("2021.03.24-23:59");
+
+ encode("CET");
+ decode("GMT");
+ decodedDateToStrings();
+
+ assert(issuingDate1.equals("Thu Mar 04 12:30:00 GMT 2021" ));
+ assert(issuingDate2.equals("Thu Mar 04 00:30:00 GMT 2021" ));
+ assert(issuingDate3.equals("Wed Mar 03 23:30:00 GMT 2021" ));
+
+ assert(FromParkingDateDate1.equals("Sun Mar 14 00:00:00 GMT 2021" ));
+ assert(FromParkingDateDate2.equals("Sun Mar 14 00:00:00 GMT 2021" ));
+ assert(FromParkingDateDate3.equals("Sun Mar 14 00:00:00 GMT 2021" ));
+
+ assert(ToParkingDateDate1.equals("Wed Mar 24 23:59:00 GMT 2021" ));
+ assert(ToParkingDateDate2.equals("Wed Mar 24 23:59:00 GMT 2021" ));
+ assert(ToParkingDateDate3.equals("Wed Mar 24 23:59:00 GMT 2021" ));
+
+
+ }
+
+ private void decodedDateToStrings() {
+
+ issuingDate1 = iTicketDecodedCase1.getIssuerDetails().getIssuingDate().toString();
+ issuingDate2 = iTicketDecodedCase2.getIssuerDetails().getIssuingDate().toString();
+ issuingDate3 = iTicketDecodedCase3.getIssuerDetails().getIssuingDate().toString();
+
+ FromParkingDateDate1 = ((IParkingGround) iTicketDecodedCase1.getDocumentData().iterator().next()).getFromParkingDate().toString();
+ FromParkingDateDate2 = ((IParkingGround) iTicketDecodedCase2.getDocumentData().iterator().next()).getFromParkingDate().toString();
+ FromParkingDateDate3 = ((IParkingGround) iTicketDecodedCase3.getDocumentData().iterator().next()).getFromParkingDate().toString();
+
+ ToParkingDateDate1 = ((IParkingGround) iTicketDecodedCase1.getDocumentData().iterator().next()).getToParkingDate().toString();
+ ToParkingDateDate2 = ((IParkingGround) iTicketDecodedCase2.getDocumentData().iterator().next()).getToParkingDate().toString();
+ ToParkingDateDate3 = ((IParkingGround) iTicketDecodedCase3.getDocumentData().iterator().next()).getToParkingDate().toString();
+
+
+ }
+
+
+ private void setFromParkingDateDateTo(String dateString) throws ParseException {
+ DateFormat dateFormat = new SimpleDateFormat( "yyyy.MM.dd-HH:mm" );
+ Date date = dateFormat.parse(dateString);
+ ((IParkingGround) iTicketDecodedFromAsn1Case1.getDocumentData().iterator().next()).setFromParkingDate(date);
+ ((IParkingGround) iTicketDecodedFromAsn1Case2.getDocumentData().iterator().next()).setFromParkingDate(date);
+ ((IParkingGround) iTicketDecodedFromAsn1Case3.getDocumentData().iterator().next()).setFromParkingDate(date);
+ }
+
+ private void setToParkingDateDateTo(String dateString) throws ParseException {
+ DateFormat dateFormat = new SimpleDateFormat( "yyyy.MM.dd-HH:mm" );
+ Date date = dateFormat.parse(dateString);
+ ((IParkingGround) iTicketDecodedFromAsn1Case1.getDocumentData().iterator().next()).setToParkingDate(date);
+ ((IParkingGround) iTicketDecodedFromAsn1Case2.getDocumentData().iterator().next()).setToParkingDate(date);
+ ((IParkingGround) iTicketDecodedFromAsn1Case3.getDocumentData().iterator().next()).setToParkingDate(date);
+ }
+
+
+ /**
+ * Encode in Time Zone
+ */
+ private void encode(String timeZone) {
+ TimeZone.setDefault(TimeZone.getTimeZone(timeZone));
+ try {
+ encodedInTimeZone1 = encoder.encode(iTicketDecodedFromAsn1Case1);
+ encodedInTimeZone2 = encoder.encode(iTicketDecodedFromAsn1Case2);
+ encodedInTimeZone3 = encoder.encode(iTicketDecodedFromAsn1Case3);
+ } catch (EncodingFormatException e) {
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * Decode in Time Zone
+ */
+ private void decode(String timeZone) {
+ TimeZone.setDefault(TimeZone.getTimeZone(timeZone));
+
+ try {
+ iTicketDecodedCase1 = decoder.decodeFromAsn(encodedInTimeZone1);
+ iTicketDecodedCase2 = decoder.decodeFromAsn(encodedInTimeZone2);
+ iTicketDecodedCase3 = decoder.decodeFromAsn(encodedInTimeZone3);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+
+ /**
+ * Encode decode.
+ */
+ private void decodeTestTicketUTC() {
+ TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
+ try {
+ iTicketDecodedFromAsn1Case1 = decoder.decodeFromAsn(encoded1);
+ iTicketDecodedFromAsn1Case2 = decoder.decodeFromAsn(encoded2);
+ iTicketDecodedFromAsn1Case3 = decoder.decodeFromAsn(encoded3);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ }
+
+}
diff --git a/src/test/java/org/uic/barcode/ticket/api/test/ParkingTimeZoneTestV2.java b/src/test/java/org/uic/barcode/ticket/api/test/ParkingTimeZoneTestV2.java new file mode 100644 index 0000000..3833034 --- /dev/null +++ b/src/test/java/org/uic/barcode/ticket/api/test/ParkingTimeZoneTestV2.java @@ -0,0 +1,312 @@ +package org.uic.barcode.ticket.api.test;
+
+import java.io.IOException;
+import java.text.DateFormat;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.TimeZone;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.uic.barcode.asn1.uper.UperEncoder;
+import org.uic.barcode.ticket.EncodingFormatException;
+import org.uic.barcode.ticket.api.asn.omv2.UicRailTicketData;
+import org.uic.barcode.ticket.api.spec.IParkingGround;
+import org.uic.barcode.ticket.api.spec.IUicRailTicket;
+import org.uic.barcode.ticket.api.test.testtickets.ParkingTestTicketV2;
+import org.uic.barcode.ticket.api.utils.Api2OpenAsnEncoderV2;
+import org.uic.barcode.ticket.api.utils.OpenAsn2ApiDecoderV2;
+
+
+/**
+ * The Class ParkingTestV2.
+ *
+ *
+ *
+ */
+public class ParkingTimeZoneTestV2 {
+
+ /** The low level encoded test ticket test case 1 . */
+ private byte[] encoded1 = null;
+
+ /** The low level encoded test ticket test case 2 . */
+ private byte[] encoded2 = null;
+
+ /** The low level encoded test ticket test case 3 . */
+ private byte[] encoded3 = null;
+
+ /** The decoder. */
+ OpenAsn2ApiDecoderV2 decoder = new OpenAsn2ApiDecoderV2();
+
+ /** The encoder. */
+ Api2OpenAsnEncoderV2 encoder = new Api2OpenAsnEncoderV2();
+
+ /** The API ticket low level encoded for case 1. */
+ IUicRailTicket iTicketDecodedFromAsn1Case1 = null;
+
+ /** The API ticket low level encoded for case 2. */
+ IUicRailTicket iTicketDecodedFromAsn1Case2 = null;
+
+ /** The API ticket low level encoded for case 3. */
+ IUicRailTicket iTicketDecodedFromAsn1Case3 = null;
+
+
+ /** The ticket decoded 1. */
+ IUicRailTicket iTicketDecodedCase1 = null;
+
+ /** The i ticket decoded 2. */
+ IUicRailTicket iTicketDecodedCase2 = null;
+
+ /** The i ticket decoded 3. */
+ IUicRailTicket iTicketDecodedCase3 = null;
+
+ byte[] encodedInTimeZone1 = null;
+ byte[] encodedInTimeZone2 = null;
+ byte[] encodedInTimeZone3 = null;
+
+ String issuingDate1 = null;
+ String issuingDate2 = null;
+ String issuingDate3 = null;
+
+ String FromParkingDateDate1 = null;
+ String FromParkingDateDate2 = null;
+ String FromParkingDateDate3 = null;
+
+ String ToParkingDateDate1 = null;
+ String ToParkingDateDate2 = null;
+ String ToParkingDateDate3 = null;
+
+ TimeZone defaulttimeZone = null;
+
+ /**
+ * Prepare tickets.
+ */
+ @Before public void prepare() {
+
+ defaulttimeZone = TimeZone.getDefault();
+
+ UicRailTicketData ticket1 = ParkingTestTicketV2.getUicTestTicket();
+ UicRailTicketData ticket2 = ParkingTestTicketV2.getUicTestTicket();
+ UicRailTicketData ticket3 = ParkingTestTicketV2.getUicTestTicket();
+
+ //encode in UTC time zone
+ TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
+
+ encoded1 = UperEncoder.encode(ticket1);
+ encoded2 = UperEncoder.encode(ticket2);
+ encoded3 = UperEncoder.encode(ticket3);
+
+ }
+
+ /**
+ * clean up
+ */
+ @After public void resetTimeZone() {
+ TimeZone.setDefault(defaulttimeZone);
+ }
+
+
+ /**
+ * Test encode test tickets in UTC and decode in CET.
+ *
+ * @throws IllegalArgumentException the illegal argument exception
+ * @throws IllegalAccessException the illegal access exception
+ * @throws ParseException
+ */
+ @Test public void testCETCETencoding() throws IllegalArgumentException, IllegalAccessException, ParseException {
+
+ //get tickets
+ decodeTestTicketUTC();
+
+ //set the dates in the time zone CET
+ TimeZone.setDefault(TimeZone.getTimeZone("CET"));
+ iTicketDecodedFromAsn1Case1.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-12:30" ));
+ iTicketDecodedFromAsn1Case2.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-00:30" ));
+ iTicketDecodedFromAsn1Case3.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.03-23:30" ));
+
+ setFromParkingDateDateTo("2021.03.14-00:00");
+ setToParkingDateDateTo("2021.03.24-23:59");
+
+ encode("CET");
+ decode("CET");
+ decodedDateToStrings();
+
+ assert(issuingDate1.equals("Thu Mar 04 13:30:00 CET 2021" ));
+ assert(issuingDate2.equals("Thu Mar 04 01:30:00 CET 2021" ));
+ assert(issuingDate3.equals("Thu Mar 04 00:30:00 CET 2021" ));
+
+ assert(FromParkingDateDate1.equals("Sun Mar 14 00:00:00 CET 2021" ));
+ assert(FromParkingDateDate2.equals("Sun Mar 14 00:00:00 CET 2021" ));
+ assert(FromParkingDateDate3.equals("Sun Mar 14 00:00:00 CET 2021" ));
+
+ assert(ToParkingDateDate1.equals("Wed Mar 24 23:59:00 CET 2021" ));
+ assert(ToParkingDateDate2.equals("Wed Mar 24 23:59:00 CET 2021" ));
+ assert(ToParkingDateDate3.equals("Wed Mar 24 23:59:00 CET 2021" ));
+
+
+ }
+
+
+ /**
+ * Test encode test tickets in UTC and decode in CET.
+ *
+ * @throws IllegalArgumentException the illegal argument exception
+ * @throws IllegalAccessException the illegal access exception
+ * @throws ParseException
+ */
+ @Test public void testGMTCETencoding() throws IllegalArgumentException, IllegalAccessException, ParseException {
+
+ //get tickets
+ decodeTestTicketUTC();
+
+ //set the dates in the time zone GMT
+ TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
+ iTicketDecodedFromAsn1Case1.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-12:30" ));
+ iTicketDecodedFromAsn1Case2.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-00:30" ));
+ iTicketDecodedFromAsn1Case3.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.03-23:30" ));
+
+ setFromParkingDateDateTo("2021.03.14-00:00");
+ setToParkingDateDateTo("2021.03.24-23:59");
+
+ encode("GMT");
+ decode("CET");
+ decodedDateToStrings();
+
+ assert(issuingDate1.equals("Thu Mar 04 13:30:00 CET 2021" ));
+ assert(issuingDate2.equals("Thu Mar 04 01:30:00 CET 2021" ));
+ assert(issuingDate3.equals("Thu Mar 04 00:30:00 CET 2021" ));
+
+ assert(FromParkingDateDate1.equals("Sun Mar 14 00:00:00 CET 2021" ));
+ assert(FromParkingDateDate2.equals("Sun Mar 14 00:00:00 CET 2021" ));
+ assert(FromParkingDateDate3.equals("Sun Mar 14 00:00:00 CET 2021" ));
+
+ assert(ToParkingDateDate1.equals("Wed Mar 24 23:59:00 CET 2021" ));
+ assert(ToParkingDateDate2.equals("Wed Mar 24 23:59:00 CET 2021" ));
+ assert(ToParkingDateDate3.equals("Wed Mar 24 23:59:00 CET 2021" ));
+
+
+ }
+
+ /**
+ * Test encode test tickets in UTC and decode in CET.
+ *
+ * @throws IllegalArgumentException the illegal argument exception
+ * @throws IllegalAccessException the illegal access exception
+ * @throws ParseException
+ */
+ @Test public void testCETGMTencoding() throws IllegalArgumentException, IllegalAccessException, ParseException {
+
+ //get tickets
+ decodeTestTicketUTC();
+
+ //set the dates in the time zone GMT
+ TimeZone.setDefault(TimeZone.getTimeZone("CET"));
+ iTicketDecodedFromAsn1Case1.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-12:30" ));
+ iTicketDecodedFromAsn1Case2.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-00:30" ));
+ iTicketDecodedFromAsn1Case3.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.03-23:30" ));
+
+ setFromParkingDateDateTo("2021.03.14-00:00");
+ setToParkingDateDateTo("2021.03.24-23:59");
+
+ encode("CET");
+ decode("GMT");
+ decodedDateToStrings();
+
+ assert(issuingDate1.equals("Thu Mar 04 12:30:00 GMT 2021" ));
+ assert(issuingDate2.equals("Thu Mar 04 00:30:00 GMT 2021" ));
+ assert(issuingDate3.equals("Wed Mar 03 23:30:00 GMT 2021" ));
+
+ assert(FromParkingDateDate1.equals("Sun Mar 14 00:00:00 GMT 2021" ));
+ assert(FromParkingDateDate2.equals("Sun Mar 14 00:00:00 GMT 2021" ));
+ assert(FromParkingDateDate3.equals("Sun Mar 14 00:00:00 GMT 2021" ));
+
+ assert(ToParkingDateDate1.equals("Wed Mar 24 23:59:00 GMT 2021" ));
+ assert(ToParkingDateDate2.equals("Wed Mar 24 23:59:00 GMT 2021" ));
+ assert(ToParkingDateDate3.equals("Wed Mar 24 23:59:00 GMT 2021" ));
+
+
+ }
+
+ private void decodedDateToStrings() {
+
+ issuingDate1 = iTicketDecodedCase1.getIssuerDetails().getIssuingDate().toString();
+ issuingDate2 = iTicketDecodedCase2.getIssuerDetails().getIssuingDate().toString();
+ issuingDate3 = iTicketDecodedCase3.getIssuerDetails().getIssuingDate().toString();
+
+ FromParkingDateDate1 = ((IParkingGround) iTicketDecodedCase1.getDocumentData().iterator().next()).getFromParkingDate().toString();
+ FromParkingDateDate2 = ((IParkingGround) iTicketDecodedCase2.getDocumentData().iterator().next()).getFromParkingDate().toString();
+ FromParkingDateDate3 = ((IParkingGround) iTicketDecodedCase3.getDocumentData().iterator().next()).getFromParkingDate().toString();
+
+ ToParkingDateDate1 = ((IParkingGround) iTicketDecodedCase1.getDocumentData().iterator().next()).getToParkingDate().toString();
+ ToParkingDateDate2 = ((IParkingGround) iTicketDecodedCase2.getDocumentData().iterator().next()).getToParkingDate().toString();
+ ToParkingDateDate3 = ((IParkingGround) iTicketDecodedCase3.getDocumentData().iterator().next()).getToParkingDate().toString();
+
+
+ }
+
+
+ private void setFromParkingDateDateTo(String dateString) throws ParseException {
+ DateFormat dateFormat = new SimpleDateFormat( "yyyy.MM.dd-HH:mm" );
+ Date date = dateFormat.parse(dateString);
+ ((IParkingGround) iTicketDecodedFromAsn1Case1.getDocumentData().iterator().next()).setFromParkingDate(date);
+ ((IParkingGround) iTicketDecodedFromAsn1Case2.getDocumentData().iterator().next()).setFromParkingDate(date);
+ ((IParkingGround) iTicketDecodedFromAsn1Case3.getDocumentData().iterator().next()).setFromParkingDate(date);
+ }
+
+ private void setToParkingDateDateTo(String dateString) throws ParseException {
+ DateFormat dateFormat = new SimpleDateFormat( "yyyy.MM.dd-HH:mm" );
+ Date date = dateFormat.parse(dateString);
+ ((IParkingGround) iTicketDecodedFromAsn1Case1.getDocumentData().iterator().next()).setToParkingDate(date);
+ ((IParkingGround) iTicketDecodedFromAsn1Case2.getDocumentData().iterator().next()).setToParkingDate(date);
+ ((IParkingGround) iTicketDecodedFromAsn1Case3.getDocumentData().iterator().next()).setToParkingDate(date);
+ }
+
+
+ /**
+ * Encode in Time Zone
+ */
+ private void encode(String timeZone) {
+ TimeZone.setDefault(TimeZone.getTimeZone(timeZone));
+ try {
+ encodedInTimeZone1 = encoder.encode(iTicketDecodedFromAsn1Case1);
+ encodedInTimeZone2 = encoder.encode(iTicketDecodedFromAsn1Case2);
+ encodedInTimeZone3 = encoder.encode(iTicketDecodedFromAsn1Case3);
+ } catch (EncodingFormatException e) {
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * Decode in Time Zone
+ */
+ private void decode(String timeZone) {
+ TimeZone.setDefault(TimeZone.getTimeZone(timeZone));
+
+ try {
+ iTicketDecodedCase1 = decoder.decodeFromAsn(encodedInTimeZone1);
+ iTicketDecodedCase2 = decoder.decodeFromAsn(encodedInTimeZone2);
+ iTicketDecodedCase3 = decoder.decodeFromAsn(encodedInTimeZone3);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+
+ /**
+ * Encode decode.
+ */
+ private void decodeTestTicketUTC() {
+ TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
+ try {
+ iTicketDecodedFromAsn1Case1 = decoder.decodeFromAsn(encoded1);
+ iTicketDecodedFromAsn1Case2 = decoder.decodeFromAsn(encoded2);
+ iTicketDecodedFromAsn1Case3 = decoder.decodeFromAsn(encoded3);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ }
+
+}
diff --git a/src/test/java/org/uic/barcode/ticket/api/test/PassTimeZoneTestV1.java b/src/test/java/org/uic/barcode/ticket/api/test/PassTimeZoneTestV1.java new file mode 100644 index 0000000..e7aeae8 --- /dev/null +++ b/src/test/java/org/uic/barcode/ticket/api/test/PassTimeZoneTestV1.java @@ -0,0 +1,455 @@ +package org.uic.barcode.ticket.api.test; + +import java.io.IOException; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.TimeZone; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.uic.barcode.asn1.uper.UperEncoder; +import org.uic.barcode.ticket.EncodingFormatException; +import org.uic.barcode.ticket.api.asn.omv1.UicRailTicketData; +import org.uic.barcode.ticket.api.spec.IPass; +import org.uic.barcode.ticket.api.spec.IUicRailTicket; +import org.uic.barcode.ticket.api.test.testtickets.PassTimeZoneTestTicketCase1V1; +import org.uic.barcode.ticket.api.test.testtickets.PassTimeZoneTestTicketCase2V1; +import org.uic.barcode.ticket.api.test.testtickets.PassTimeZoneTestTicketCase3V1; +import org.uic.barcode.ticket.api.utils.Api2OpenAsnEncoder; +import org.uic.barcode.ticket.api.utils.OpenAsn2ApiDecoder; + + +/** + * The Class PassTimeZoneTestV1. + * + * + * test validity dates and activated days in different time zones + * + Case 1: + issue date: 04-03-2021 12:30 UTC + issuingYear: 2021 + issuingDay: 63 + issuingTime: 750 + validFromDay: 10 + activatedDays: [0] + utcOffset: 0 + + expected: activated for 04-03-2021 + 10 + 0 00:00 UTC --> 14-03-2021 + + Case 2: + issue date: 04-03-2021 00:30 UTC + issuingYear: 2021 + issuingDay: 63 + issuingTime: 30 + validFromDay: 10 + activatedDays: [0] + utcOffset: 0 + + expected: activated for 04-03-2021 + 10 + 0 00:00 UTC --> 14-03-2021 + + Case 3: + issue date: 03-03-2021 23:30 UTC + issuingYear: 2021 + issuingDay: 62 + issuingTime: 1410 + validFromDay: 11 + activatedDays: [0] + + expected: activated for 03-03-2021 + 11 + 0 00:00 UTC --> 14-03-2021 + * + * - Test tickets defined on the level of the asn.1 object model + * - set the local time zone + * - decoded to get an API ticket object + * - encode again to + * + * + * + * + */ +public class PassTimeZoneTestV1 { + + /** The low level encoded test ticket test case 1 . */ + private byte[] encoded1 = null; + + /** The low level encoded test ticket test case 2 . */ + private byte[] encoded2 = null; + + /** The low level encoded test ticket test case 3 . */ + private byte[] encoded3 = null; + + /** The decoder. */ + OpenAsn2ApiDecoder decoder = new OpenAsn2ApiDecoder(); + + /** The encoder. */ + Api2OpenAsnEncoder encoder = new Api2OpenAsnEncoder(); + + /** The API ticket low level encoded for case 1. */ + IUicRailTicket iTicketDecodedFromAsn1Case1 = null; + + /** The API ticket low level encoded for case 2. */ + IUicRailTicket iTicketDecodedFromAsn1Case2 = null; + + /** The API ticket low level encoded for case 3. */ + IUicRailTicket iTicketDecodedFromAsn1Case3 = null; + + + /** The ticket decoded 1. */ + IUicRailTicket iTicketDecodedCase1 = null; + + /** The i ticket decoded 2. */ + IUicRailTicket iTicketDecodedCase2 = null; + + /** The i ticket decoded 3. */ + IUicRailTicket iTicketDecodedCase3 = null; + + byte[] encodedInTimeZone1 = null; + byte[] encodedInTimeZone2 = null; + byte[] encodedInTimeZone3 = null; + + String issuingDate1 = null; + String issuingDate2 = null; + String issuingDate3 = null; + + String validFrom1 = null; + String validFrom2 = null; + String validFrom3 = null; + + String validUntil1 = null; + String validUntil2 = null; + String validUntil3 = null; + + String activationDate1 = null; + String activationDate2 = null; + String activationDate3 = null; + + TimeZone defaulttimeZone = null; + + /** + * Prepare tickets. + */ + @Before public void prepare() { + + defaulttimeZone = TimeZone.getDefault(); + + UicRailTicketData ticket1 = PassTimeZoneTestTicketCase1V1.getUicTestTicket(); + UicRailTicketData ticket2 = PassTimeZoneTestTicketCase2V1.getUicTestTicket(); + UicRailTicketData ticket3 = PassTimeZoneTestTicketCase3V1.getUicTestTicket(); + + //encode in UTC time zone + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); + + encoded1 = UperEncoder.encode(ticket1); + encoded2 = UperEncoder.encode(ticket2); + encoded3 = UperEncoder.encode(ticket3); + + } + + /** + * clean up + */ + @After public void resetTimeZone() { + TimeZone.setDefault(defaulttimeZone); + } + + /** + * Test decode test tickets in CET. + * + * @throws IllegalArgumentException the illegal argument exception + * @throws IllegalAccessException the illegal access exception + */ + @Test public void testCETdecodeOnly() throws IllegalArgumentException, IllegalAccessException { + + encodedInTimeZone1 = encoded1; + encodedInTimeZone2 = encoded2; + encodedInTimeZone3 = encoded3; + + decode("CET"); + decodedDateToStrings(); + + assert(issuingDate1.equals("Thu Mar 04 13:30:00 CET 2021" )); + assert(issuingDate2.equals("Thu Mar 04 01:30:00 CET 2021" )); + assert(issuingDate3.equals("Thu Mar 04 00:30:00 CET 2021" )); + + assert(validFrom1.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(validFrom2.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(validFrom3.equals("Sun Mar 14 00:00:00 CET 2021" )); + + assert(validUntil1.equals("Wed Mar 24 23:59:00 CET 2021" )); + assert(validUntil2.equals("Wed Mar 24 23:59:00 CET 2021" )); + assert(validUntil3.equals("Wed Mar 24 23:59:00 CET 2021" )); + + assert(activationDate1.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(activationDate2.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(activationDate3.equals("Sun Mar 14 00:00:00 CET 2021" )); + + } + + /** + * Test decode test tickets in GMT. + * + * @throws IllegalArgumentException the illegal argument exception + * @throws IllegalAccessException the illegal access exception + */ + @Test public void testGMTdecodeOnly() throws IllegalArgumentException, IllegalAccessException { + + encodedInTimeZone1 = encoded1; + encodedInTimeZone2 = encoded2; + encodedInTimeZone3 = encoded3; + + //test decoded ticket + decode("GMT"); + decodedDateToStrings(); + + assert(issuingDate1.equals("Thu Mar 04 12:30:00 GMT 2021" )); + assert(issuingDate2.equals("Thu Mar 04 00:30:00 GMT 2021" )); + assert(issuingDate3.equals("Wed Mar 03 23:30:00 GMT 2021" )); + + assert(validFrom1.equals("Sun Mar 14 00:00:00 GMT 2021" )); + assert(validFrom2.equals("Sun Mar 14 00:00:00 GMT 2021" )); + assert(validFrom3.equals("Sun Mar 14 00:00:00 GMT 2021" )); + + assert(validUntil1.equals("Wed Mar 24 23:59:00 GMT 2021" )); + assert(validUntil2.equals("Wed Mar 24 23:59:00 GMT 2021" )); + assert(validUntil3.equals("Wed Mar 24 23:59:00 GMT 2021" )); + + assert(activationDate1.equals("Sun Mar 14 00:00:00 GMT 2021" )); + assert(activationDate2.equals("Sun Mar 14 00:00:00 GMT 2021" )); + assert(activationDate3.equals("Sun Mar 14 00:00:00 GMT 2021" )); + + } + + + /** + * Test encode test tickets in UTC and decode in CET. + * + * @throws IllegalArgumentException the illegal argument exception + * @throws IllegalAccessException the illegal access exception + * @throws ParseException + */ + @Test public void testCETCETencoding() throws IllegalArgumentException, IllegalAccessException, ParseException { + + //get tickets + decodeTestTicketUTC(); + + //set the dates in the time zone CET + TimeZone.setDefault(TimeZone.getTimeZone("CET")); + iTicketDecodedFromAsn1Case1.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-12:30" )); + iTicketDecodedFromAsn1Case2.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-00:30" )); + iTicketDecodedFromAsn1Case3.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.03-23:30" )); + + setValidFromTo("2021.03.14-00:00"); + setValidUntilTo("2021.03.24-23:59"); + setActivatedDayTo("2021.03.14-00:00"); + + encode("CET"); + decode("CET"); + decodedDateToStrings(); + + assert(issuingDate1.equals("Thu Mar 04 13:30:00 CET 2021" )); + assert(issuingDate2.equals("Thu Mar 04 01:30:00 CET 2021" )); + assert(issuingDate3.equals("Thu Mar 04 00:30:00 CET 2021" )); + + assert(validFrom1.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(validFrom2.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(validFrom3.equals("Sun Mar 14 00:00:00 CET 2021" )); + + assert(validUntil1.equals("Wed Mar 24 23:59:00 CET 2021" )); + assert(validUntil2.equals("Wed Mar 24 23:59:00 CET 2021" )); + assert(validUntil3.equals("Wed Mar 24 23:59:00 CET 2021" )); + + assert(activationDate1.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(activationDate2.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(activationDate3.equals("Sun Mar 14 00:00:00 CET 2021" )); + + } + + + /** + * Test encode test tickets in UTC and decode in CET. + * + * @throws IllegalArgumentException the illegal argument exception + * @throws IllegalAccessException the illegal access exception + * @throws ParseException + */ + @Test public void testGMTCETencoding() throws IllegalArgumentException, IllegalAccessException, ParseException { + + //get tickets + decodeTestTicketUTC(); + + //set the dates in the time zone GMT + TimeZone.setDefault(TimeZone.getTimeZone("GMT")); + iTicketDecodedFromAsn1Case1.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-12:30" )); + iTicketDecodedFromAsn1Case2.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-00:30" )); + iTicketDecodedFromAsn1Case3.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.03-23:30" )); + + setValidFromTo("2021.03.14-00:00"); + setValidUntilTo("2021.03.24-23:59"); + setActivatedDayTo("2021.03.14-00:00"); + + encode("GMT"); + decode("CET"); + decodedDateToStrings(); + + assert(issuingDate1.equals("Thu Mar 04 13:30:00 CET 2021" )); + assert(issuingDate2.equals("Thu Mar 04 01:30:00 CET 2021" )); + assert(issuingDate3.equals("Thu Mar 04 00:30:00 CET 2021" )); + + assert(validFrom1.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(validFrom2.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(validFrom3.equals("Sun Mar 14 00:00:00 CET 2021" )); + + assert(validUntil1.equals("Wed Mar 24 23:59:00 CET 2021" )); + assert(validUntil2.equals("Wed Mar 24 23:59:00 CET 2021" )); + assert(validUntil3.equals("Wed Mar 24 23:59:00 CET 2021" )); + + assert(activationDate1.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(activationDate2.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(activationDate3.equals("Sun Mar 14 00:00:00 CET 2021" )); + + } + + /** + * Test encode test tickets in UTC and decode in CET. + * + * @throws IllegalArgumentException the illegal argument exception + * @throws IllegalAccessException the illegal access exception + * @throws ParseException + */ + @Test public void testCETGMTencoding() throws IllegalArgumentException, IllegalAccessException, ParseException { + + //get tickets + decodeTestTicketUTC(); + + //set the dates in the time zone GMT + TimeZone.setDefault(TimeZone.getTimeZone("CET")); + iTicketDecodedFromAsn1Case1.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-12:30" )); + iTicketDecodedFromAsn1Case2.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-00:30" )); + iTicketDecodedFromAsn1Case3.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.03-23:30" )); + + setValidFromTo("2021.03.14-00:00"); + setValidUntilTo("2021.03.24-23:59"); + setActivatedDayTo("2021.03.14-00:00"); + + encode("CET"); + decode("GMT"); + decodedDateToStrings(); + + assert(issuingDate1.equals("Thu Mar 04 12:30:00 GMT 2021" )); + assert(issuingDate2.equals("Thu Mar 04 00:30:00 GMT 2021" )); + assert(issuingDate3.equals("Wed Mar 03 23:30:00 GMT 2021" )); + + assert(validFrom1.equals("Sun Mar 14 00:00:00 GMT 2021" )); + assert(validFrom2.equals("Sun Mar 14 00:00:00 GMT 2021" )); + assert(validFrom3.equals("Sun Mar 14 00:00:00 GMT 2021" )); + + assert(validUntil1.equals("Wed Mar 24 23:59:00 GMT 2021" )); + assert(validUntil2.equals("Wed Mar 24 23:59:00 GMT 2021" )); + assert(validUntil3.equals("Wed Mar 24 23:59:00 GMT 2021" )); + + assert(activationDate1.equals("Sun Mar 14 00:00:00 GMT 2021" )); + assert(activationDate2.equals("Sun Mar 14 00:00:00 GMT 2021" )); + assert(activationDate3.equals("Sun Mar 14 00:00:00 GMT 2021" )); + + } + + private void decodedDateToStrings() { + + issuingDate1 = iTicketDecodedCase1.getIssuerDetails().getIssuingDate().toString(); + issuingDate2 = iTicketDecodedCase2.getIssuerDetails().getIssuingDate().toString(); + issuingDate3 = iTicketDecodedCase3.getIssuerDetails().getIssuingDate().toString(); + + validFrom1 = ((IPass) iTicketDecodedCase1.getDocumentData().iterator().next()).getValidFrom().toString(); + validFrom2 = ((IPass) iTicketDecodedCase2.getDocumentData().iterator().next()).getValidFrom().toString(); + validFrom3 = ((IPass) iTicketDecodedCase3.getDocumentData().iterator().next()).getValidFrom().toString(); + + validUntil1 = ((IPass) iTicketDecodedCase1.getDocumentData().iterator().next()).getValidUntil().toString(); + validUntil2 = ((IPass) iTicketDecodedCase2.getDocumentData().iterator().next()).getValidUntil().toString(); + validUntil3 = ((IPass) iTicketDecodedCase3.getDocumentData().iterator().next()).getValidUntil().toString(); + + activationDate1 = ((IPass) iTicketDecodedCase1.getDocumentData().iterator().next()).getActivatedDays().iterator().next().toString(); + activationDate2 = ((IPass) iTicketDecodedCase2.getDocumentData().iterator().next()).getActivatedDays().iterator().next().toString(); + activationDate3 = ((IPass) iTicketDecodedCase3.getDocumentData().iterator().next()).getActivatedDays().iterator().next().toString(); + + } + + + private void setActivatedDayTo(String dateString) throws ParseException { + DateFormat dateFormat = new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ); + Date date = dateFormat.parse(dateString); + ((IPass) iTicketDecodedFromAsn1Case1.getDocumentData().iterator().next()).getActivatedDays().clear(); + ((IPass) iTicketDecodedFromAsn1Case1.getDocumentData().iterator().next()).getActivatedDays().add(date); + + ((IPass) iTicketDecodedFromAsn1Case2.getDocumentData().iterator().next()).getActivatedDays().clear(); + ((IPass) iTicketDecodedFromAsn1Case2.getDocumentData().iterator().next()).getActivatedDays().add(date); + + ((IPass) iTicketDecodedFromAsn1Case3.getDocumentData().iterator().next()).getActivatedDays().clear(); + ((IPass) iTicketDecodedFromAsn1Case3.getDocumentData().iterator().next()).getActivatedDays().add(date); + + } + + private void setValidFromTo(String dateString) throws ParseException { + DateFormat dateFormat = new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ); + Date date = dateFormat.parse(dateString); + ((IPass) iTicketDecodedFromAsn1Case1.getDocumentData().iterator().next()).setValidFrom(date); + ((IPass) iTicketDecodedFromAsn1Case2.getDocumentData().iterator().next()).setValidFrom(date); + ((IPass) iTicketDecodedFromAsn1Case3.getDocumentData().iterator().next()).setValidFrom(date); + } + + private void setValidUntilTo(String dateString) throws ParseException { + DateFormat dateFormat = new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ); + Date date = dateFormat.parse(dateString); + ((IPass) iTicketDecodedFromAsn1Case1.getDocumentData().iterator().next()).setValidUntil(date); + ((IPass) iTicketDecodedFromAsn1Case2.getDocumentData().iterator().next()).setValidUntil(date); + ((IPass) iTicketDecodedFromAsn1Case3.getDocumentData().iterator().next()).setValidUntil(date); + } + + + /** + * Encode in Time Zone + */ + private void encode(String timeZone) { + TimeZone.setDefault(TimeZone.getTimeZone(timeZone)); + try { + encodedInTimeZone1 = encoder.encode(iTicketDecodedFromAsn1Case1); + encodedInTimeZone2 = encoder.encode(iTicketDecodedFromAsn1Case2); + encodedInTimeZone3 = encoder.encode(iTicketDecodedFromAsn1Case3); + } catch (EncodingFormatException e) { + e.printStackTrace(); + } + } + + /** + * Decode in Time Zone + */ + private void decode(String timeZone) { + TimeZone.setDefault(TimeZone.getTimeZone(timeZone)); + + try { + iTicketDecodedCase1 = decoder.decodeFromAsn(encodedInTimeZone1); + iTicketDecodedCase2 = decoder.decodeFromAsn(encodedInTimeZone2); + iTicketDecodedCase3 = decoder.decodeFromAsn(encodedInTimeZone3); + } catch (IOException e) { + e.printStackTrace(); + } + } + + + /** + * Encode decode. + */ + private void decodeTestTicketUTC() { + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); + try { + iTicketDecodedFromAsn1Case1 = decoder.decodeFromAsn(encoded1); + iTicketDecodedFromAsn1Case2 = decoder.decodeFromAsn(encoded2); + iTicketDecodedFromAsn1Case3 = decoder.decodeFromAsn(encoded3); + } catch (IOException e) { + e.printStackTrace(); + } + + } + +} diff --git a/src/test/java/org/uic/barcode/ticket/api/test/PassTimeZoneTestV2.java b/src/test/java/org/uic/barcode/ticket/api/test/PassTimeZoneTestV2.java new file mode 100644 index 0000000..decc26b --- /dev/null +++ b/src/test/java/org/uic/barcode/ticket/api/test/PassTimeZoneTestV2.java @@ -0,0 +1,345 @@ +package org.uic.barcode.ticket.api.test; + +import java.io.IOException; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.TimeZone; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.uic.barcode.asn1.uper.UperEncoder; +import org.uic.barcode.ticket.EncodingFormatException; +import org.uic.barcode.ticket.api.asn.omv2.UicRailTicketData; +import org.uic.barcode.ticket.api.spec.IPass; +import org.uic.barcode.ticket.api.spec.IUicRailTicket; +import org.uic.barcode.ticket.api.test.testtickets.PassTimeZoneTestTicketV2; +import org.uic.barcode.ticket.api.utils.Api2OpenAsnEncoderV2; +import org.uic.barcode.ticket.api.utils.OpenAsn2ApiDecoderV2; + + +/** + * The Class FipTimeZoneTestV2. + * + * + * + */ +public class PassTimeZoneTestV2 { + + /** The low level encoded test ticket test case 1 . */ + private byte[] encoded1 = null; + + /** The low level encoded test ticket test case 2 . */ + private byte[] encoded2 = null; + + /** The low level encoded test ticket test case 3 . */ + private byte[] encoded3 = null; + + /** The decoder. */ + OpenAsn2ApiDecoderV2 decoder = new OpenAsn2ApiDecoderV2(); + + /** The encoder. */ + Api2OpenAsnEncoderV2 encoder = new Api2OpenAsnEncoderV2(); + + /** The API ticket low level encoded for case 1. */ + IUicRailTicket iTicketDecodedFromAsn1Case1 = null; + + /** The API ticket low level encoded for case 2. */ + IUicRailTicket iTicketDecodedFromAsn1Case2 = null; + + /** The API ticket low level encoded for case 3. */ + IUicRailTicket iTicketDecodedFromAsn1Case3 = null; + + + /** The ticket decoded 1. */ + IUicRailTicket iTicketDecodedCase1 = null; + + /** The i ticket decoded 2. */ + IUicRailTicket iTicketDecodedCase2 = null; + + /** The i ticket decoded 3. */ + IUicRailTicket iTicketDecodedCase3 = null; + + byte[] encodedInTimeZone1 = null; + byte[] encodedInTimeZone2 = null; + byte[] encodedInTimeZone3 = null; + + String issuingDate1 = null; + String issuingDate2 = null; + String issuingDate3 = null; + + String validFrom1 = null; + String validFrom2 = null; + String validFrom3 = null; + + String validUntil1 = null; + String validUntil2 = null; + String validUntil3 = null; + + String activationDate1 = null; + String activationDate2 = null; + String activationDate3 = null; + + TimeZone defaulttimeZone = null; + + /** + * Prepare tickets. + */ + @Before public void prepare() { + + defaulttimeZone = TimeZone.getDefault(); + + UicRailTicketData ticket1 = PassTimeZoneTestTicketV2.getUicTestTicket(); + UicRailTicketData ticket2 = PassTimeZoneTestTicketV2.getUicTestTicket(); + UicRailTicketData ticket3 = PassTimeZoneTestTicketV2.getUicTestTicket(); + + //encode in UTC time zone + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); + + encoded1 = UperEncoder.encode(ticket1); + encoded2 = UperEncoder.encode(ticket2); + encoded3 = UperEncoder.encode(ticket3); + + } + + /** + * clean up + */ + @After public void resetTimeZone() { + TimeZone.setDefault(defaulttimeZone); + } + + + /** + * Test encode test tickets in UTC and decode in CET. + * + * @throws IllegalArgumentException the illegal argument exception + * @throws IllegalAccessException the illegal access exception + * @throws ParseException + */ + @Test public void testCETCETencoding() throws IllegalArgumentException, IllegalAccessException, ParseException { + + //get tickets + decodeTestTicketUTC(); + + //set the dates in the time zone CET + TimeZone.setDefault(TimeZone.getTimeZone("CET")); + iTicketDecodedFromAsn1Case1.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-12:30" )); + iTicketDecodedFromAsn1Case2.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-00:30" )); + iTicketDecodedFromAsn1Case3.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.03-23:30" )); + + setValidFromTo("2021.03.14-00:00"); + setValidUntilTo("2021.03.24-23:59"); + setActivatedDayTo("2021.03.14-00:00"); + + encode("CET"); + decode("CET"); + decodedDateToStrings(); + + assert(issuingDate1.equals("Thu Mar 04 13:30:00 CET 2021" )); + assert(issuingDate2.equals("Thu Mar 04 01:30:00 CET 2021" )); + assert(issuingDate3.equals("Thu Mar 04 00:30:00 CET 2021" )); + + assert(validFrom1.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(validFrom2.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(validFrom3.equals("Sun Mar 14 00:00:00 CET 2021" )); + + assert(validUntil1.equals("Wed Mar 24 23:59:00 CET 2021" )); + assert(validUntil2.equals("Wed Mar 24 23:59:00 CET 2021" )); + assert(validUntil3.equals("Wed Mar 24 23:59:00 CET 2021" )); + + assert(activationDate1.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(activationDate2.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(activationDate3.equals("Sun Mar 14 00:00:00 CET 2021" )); + + } + + + /** + * Test encode test tickets in UTC and decode in CET. + * + * @throws IllegalArgumentException the illegal argument exception + * @throws IllegalAccessException the illegal access exception + * @throws ParseException + */ + @Test public void testGMTCETencoding() throws IllegalArgumentException, IllegalAccessException, ParseException { + + //get tickets + decodeTestTicketUTC(); + + //set the dates in the time zone GMT + TimeZone.setDefault(TimeZone.getTimeZone("GMT")); + iTicketDecodedFromAsn1Case1.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-12:30" )); + iTicketDecodedFromAsn1Case2.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-00:30" )); + iTicketDecodedFromAsn1Case3.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.03-23:30" )); + + setValidFromTo("2021.03.14-00:00"); + setValidUntilTo("2021.03.24-23:59"); + setActivatedDayTo("2021.03.14-00:00"); + + encode("GMT"); + decode("CET"); + decodedDateToStrings(); + + assert(issuingDate1.equals("Thu Mar 04 13:30:00 CET 2021" )); + assert(issuingDate2.equals("Thu Mar 04 01:30:00 CET 2021" )); + assert(issuingDate3.equals("Thu Mar 04 00:30:00 CET 2021" )); + + assert(validFrom1.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(validFrom2.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(validFrom3.equals("Sun Mar 14 00:00:00 CET 2021" )); + + assert(validUntil1.equals("Wed Mar 24 23:59:00 CET 2021" )); + assert(validUntil2.equals("Wed Mar 24 23:59:00 CET 2021" )); + assert(validUntil3.equals("Wed Mar 24 23:59:00 CET 2021" )); + + assert(activationDate1.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(activationDate2.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(activationDate3.equals("Sun Mar 14 00:00:00 CET 2021" )); + + } + + /** + * Test encode test tickets in UTC and decode in CET. + * + * @throws IllegalArgumentException the illegal argument exception + * @throws IllegalAccessException the illegal access exception + * @throws ParseException + */ + @Test public void testCETGMTencoding() throws IllegalArgumentException, IllegalAccessException, ParseException { + + //get tickets + decodeTestTicketUTC(); + + //set the dates in the time zone GMT + TimeZone.setDefault(TimeZone.getTimeZone("CET")); + iTicketDecodedFromAsn1Case1.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-12:30" )); + iTicketDecodedFromAsn1Case2.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-00:30" )); + iTicketDecodedFromAsn1Case3.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.03-23:30" )); + + setValidFromTo("2021.03.14-00:00"); + setValidUntilTo("2021.03.24-23:59"); + setActivatedDayTo("2021.03.14-00:00"); + + encode("CET"); + decode("GMT"); + decodedDateToStrings(); + + assert(issuingDate1.equals("Thu Mar 04 12:30:00 GMT 2021" )); + assert(issuingDate2.equals("Thu Mar 04 00:30:00 GMT 2021" )); + assert(issuingDate3.equals("Wed Mar 03 23:30:00 GMT 2021" )); + + assert(validFrom1.equals("Sun Mar 14 00:00:00 GMT 2021" )); + assert(validFrom2.equals("Sun Mar 14 00:00:00 GMT 2021" )); + assert(validFrom3.equals("Sun Mar 14 00:00:00 GMT 2021" )); + + assert(validUntil1.equals("Wed Mar 24 23:59:00 GMT 2021" )); + assert(validUntil2.equals("Wed Mar 24 23:59:00 GMT 2021" )); + assert(validUntil3.equals("Wed Mar 24 23:59:00 GMT 2021" )); + + assert(activationDate1.equals("Sun Mar 14 00:00:00 GMT 2021" )); + assert(activationDate2.equals("Sun Mar 14 00:00:00 GMT 2021" )); + assert(activationDate3.equals("Sun Mar 14 00:00:00 GMT 2021" )); + + } + + private void decodedDateToStrings() { + + issuingDate1 = iTicketDecodedCase1.getIssuerDetails().getIssuingDate().toString(); + issuingDate2 = iTicketDecodedCase2.getIssuerDetails().getIssuingDate().toString(); + issuingDate3 = iTicketDecodedCase3.getIssuerDetails().getIssuingDate().toString(); + + validFrom1 = ((IPass) iTicketDecodedCase1.getDocumentData().iterator().next()).getValidFrom().toString(); + validFrom2 = ((IPass) iTicketDecodedCase2.getDocumentData().iterator().next()).getValidFrom().toString(); + validFrom3 = ((IPass) iTicketDecodedCase3.getDocumentData().iterator().next()).getValidFrom().toString(); + + validUntil1 = ((IPass) iTicketDecodedCase1.getDocumentData().iterator().next()).getValidUntil().toString(); + validUntil2 = ((IPass) iTicketDecodedCase2.getDocumentData().iterator().next()).getValidUntil().toString(); + validUntil3 = ((IPass) iTicketDecodedCase3.getDocumentData().iterator().next()).getValidUntil().toString(); + + activationDate1 = ((IPass) iTicketDecodedCase1.getDocumentData().iterator().next()).getActivatedDays().iterator().next().toString(); + activationDate2 = ((IPass) iTicketDecodedCase2.getDocumentData().iterator().next()).getActivatedDays().iterator().next().toString(); + activationDate3 = ((IPass) iTicketDecodedCase3.getDocumentData().iterator().next()).getActivatedDays().iterator().next().toString(); + + } + + + private void setActivatedDayTo(String dateString) throws ParseException { + DateFormat dateFormat = new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ); + Date date = dateFormat.parse(dateString); + ((IPass) iTicketDecodedFromAsn1Case1.getDocumentData().iterator().next()).getActivatedDays().clear(); + ((IPass) iTicketDecodedFromAsn1Case1.getDocumentData().iterator().next()).getActivatedDays().add(date); + + ((IPass) iTicketDecodedFromAsn1Case2.getDocumentData().iterator().next()).getActivatedDays().clear(); + ((IPass) iTicketDecodedFromAsn1Case2.getDocumentData().iterator().next()).getActivatedDays().add(date); + + ((IPass) iTicketDecodedFromAsn1Case3.getDocumentData().iterator().next()).getActivatedDays().clear(); + ((IPass) iTicketDecodedFromAsn1Case3.getDocumentData().iterator().next()).getActivatedDays().add(date); + + } + + private void setValidFromTo(String dateString) throws ParseException { + DateFormat dateFormat = new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ); + Date date = dateFormat.parse(dateString); + ((IPass) iTicketDecodedFromAsn1Case1.getDocumentData().iterator().next()).setValidFrom(date); + ((IPass) iTicketDecodedFromAsn1Case2.getDocumentData().iterator().next()).setValidFrom(date); + ((IPass) iTicketDecodedFromAsn1Case3.getDocumentData().iterator().next()).setValidFrom(date); + } + + private void setValidUntilTo(String dateString) throws ParseException { + DateFormat dateFormat = new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ); + Date date = dateFormat.parse(dateString); + ((IPass) iTicketDecodedFromAsn1Case1.getDocumentData().iterator().next()).setValidUntil(date); + ((IPass) iTicketDecodedFromAsn1Case2.getDocumentData().iterator().next()).setValidUntil(date); + ((IPass) iTicketDecodedFromAsn1Case3.getDocumentData().iterator().next()).setValidUntil(date); + } + + + /** + * Encode in Time Zone + */ + private void encode(String timeZone) { + TimeZone.setDefault(TimeZone.getTimeZone(timeZone)); + try { + encodedInTimeZone1 = encoder.encode(iTicketDecodedFromAsn1Case1); + encodedInTimeZone2 = encoder.encode(iTicketDecodedFromAsn1Case2); + encodedInTimeZone3 = encoder.encode(iTicketDecodedFromAsn1Case3); + } catch (EncodingFormatException e) { + e.printStackTrace(); + } + } + + /** + * Decode in Time Zone + */ + private void decode(String timeZone) { + TimeZone.setDefault(TimeZone.getTimeZone(timeZone)); + + try { + iTicketDecodedCase1 = decoder.decodeFromAsn(encodedInTimeZone1); + iTicketDecodedCase2 = decoder.decodeFromAsn(encodedInTimeZone2); + iTicketDecodedCase3 = decoder.decodeFromAsn(encodedInTimeZone3); + } catch (IOException e) { + e.printStackTrace(); + } + } + + + /** + * Encode decode. + */ + private void decodeTestTicketUTC() { + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); + try { + iTicketDecodedFromAsn1Case1 = decoder.decodeFromAsn(encoded1); + iTicketDecodedFromAsn1Case2 = decoder.decodeFromAsn(encoded2); + iTicketDecodedFromAsn1Case3 = decoder.decodeFromAsn(encoded3); + } catch (IOException e) { + e.printStackTrace(); + } + + } + +} diff --git a/src/test/java/org/uic/barcode/ticket/api/test/ReservationTimeZoneTestV1.java b/src/test/java/org/uic/barcode/ticket/api/test/ReservationTimeZoneTestV1.java new file mode 100644 index 0000000..5bf78cb --- /dev/null +++ b/src/test/java/org/uic/barcode/ticket/api/test/ReservationTimeZoneTestV1.java @@ -0,0 +1,312 @@ +package org.uic.barcode.ticket.api.test; + +import java.io.IOException; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.TimeZone; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.uic.barcode.asn1.uper.UperEncoder; +import org.uic.barcode.ticket.EncodingFormatException; +import org.uic.barcode.ticket.api.asn.omv1.UicRailTicketData; +import org.uic.barcode.ticket.api.spec.IReservation; +import org.uic.barcode.ticket.api.spec.IUicRailTicket; +import org.uic.barcode.ticket.api.test.testtickets.ReservationTestTicketV1; +import org.uic.barcode.ticket.api.utils.Api2OpenAsnEncoder; +import org.uic.barcode.ticket.api.utils.OpenAsn2ApiDecoder; + + +/** + * The Class FipTimeZoneTestV1. + * + * + * + */ +public class ReservationTimeZoneTestV1 { + + /** The low level encoded test ticket test case 1 . */ + private byte[] encoded1 = null; + + /** The low level encoded test ticket test case 2 . */ + private byte[] encoded2 = null; + + /** The low level encoded test ticket test case 3 . */ + private byte[] encoded3 = null; + + /** The decoder. */ + OpenAsn2ApiDecoder decoder = new OpenAsn2ApiDecoder(); + + /** The encoder. */ + Api2OpenAsnEncoder encoder = new Api2OpenAsnEncoder(); + + /** The API ticket low level encoded for case 1. */ + IUicRailTicket iTicketDecodedFromAsn1Case1 = null; + + /** The API ticket low level encoded for case 2. */ + IUicRailTicket iTicketDecodedFromAsn1Case2 = null; + + /** The API ticket low level encoded for case 3. */ + IUicRailTicket iTicketDecodedFromAsn1Case3 = null; + + + /** The ticket decoded 1. */ + IUicRailTicket iTicketDecodedCase1 = null; + + /** The i ticket decoded 2. */ + IUicRailTicket iTicketDecodedCase2 = null; + + /** The i ticket decoded 3. */ + IUicRailTicket iTicketDecodedCase3 = null; + + byte[] encodedInTimeZone1 = null; + byte[] encodedInTimeZone2 = null; + byte[] encodedInTimeZone3 = null; + + String issuingDate1 = null; + String issuingDate2 = null; + String issuingDate3 = null; + + String departureDate1 = null; + String departureDate2 = null; + String departureDate3 = null; + + String arrivalDate1 = null; + String arrivalDate2 = null; + String arrivalDate3 = null; + + TimeZone defaulttimeZone = null; + + /** + * Prepare tickets. + */ + @Before public void prepare() { + + defaulttimeZone = TimeZone.getDefault(); + + UicRailTicketData ticket1 = ReservationTestTicketV1.getUicTestTicket(); + UicRailTicketData ticket2 = ReservationTestTicketV1.getUicTestTicket(); + UicRailTicketData ticket3 = ReservationTestTicketV1.getUicTestTicket(); + + //encode in UTC time zone + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); + + encoded1 = UperEncoder.encode(ticket1); + encoded2 = UperEncoder.encode(ticket2); + encoded3 = UperEncoder.encode(ticket3); + + } + + /** + * clean up + */ + @After public void resetTimeZone() { + TimeZone.setDefault(defaulttimeZone); + } + + + /** + * Test encode test tickets in UTC and decode in CET. + * + * @throws IllegalArgumentException the illegal argument exception + * @throws IllegalAccessException the illegal access exception + * @throws ParseException + */ + @Test public void testCETCETencoding() throws IllegalArgumentException, IllegalAccessException, ParseException { + + //get tickets + decodeTestTicketUTC(); + + //set the dates in the time zone CET + TimeZone.setDefault(TimeZone.getTimeZone("CET")); + iTicketDecodedFromAsn1Case1.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-12:30" )); + iTicketDecodedFromAsn1Case2.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-00:30" )); + iTicketDecodedFromAsn1Case3.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.03-23:30" )); + + setdepartureDateTo("2021.03.14-00:00"); + setarrivalDateTo("2021.03.24-23:59"); + + encode("CET"); + decode("CET"); + decodedDateToStrings(); + + assert(issuingDate1.equals("Thu Mar 04 13:30:00 CET 2021" )); + assert(issuingDate2.equals("Thu Mar 04 01:30:00 CET 2021" )); + assert(issuingDate3.equals("Thu Mar 04 00:30:00 CET 2021" )); + + assert(departureDate1.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(departureDate2.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(departureDate3.equals("Sun Mar 14 00:00:00 CET 2021" )); + + assert(arrivalDate1.equals("Wed Mar 24 23:59:00 CET 2021" )); + assert(arrivalDate2.equals("Wed Mar 24 23:59:00 CET 2021" )); + assert(arrivalDate3.equals("Wed Mar 24 23:59:00 CET 2021" )); + + + } + + + /** + * Test encode test tickets in UTC and decode in CET. + * + * @throws IllegalArgumentException the illegal argument exception + * @throws IllegalAccessException the illegal access exception + * @throws ParseException + */ + @Test public void testGMTCETencoding() throws IllegalArgumentException, IllegalAccessException, ParseException { + + //get tickets + decodeTestTicketUTC(); + + //set the dates in the time zone GMT + TimeZone.setDefault(TimeZone.getTimeZone("GMT")); + iTicketDecodedFromAsn1Case1.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-12:30" )); + iTicketDecodedFromAsn1Case2.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-00:30" )); + iTicketDecodedFromAsn1Case3.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.03-23:30" )); + + setdepartureDateTo("2021.03.14-00:00"); + setarrivalDateTo("2021.03.24-23:59"); + + encode("GMT"); + decode("CET"); + decodedDateToStrings(); + + assert(issuingDate1.equals("Thu Mar 04 13:30:00 CET 2021" )); + assert(issuingDate2.equals("Thu Mar 04 01:30:00 CET 2021" )); + assert(issuingDate3.equals("Thu Mar 04 00:30:00 CET 2021" )); + + assert(departureDate1.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(departureDate2.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(departureDate3.equals("Sun Mar 14 00:00:00 CET 2021" )); + + assert(arrivalDate1.equals("Wed Mar 24 23:59:00 CET 2021" )); + assert(arrivalDate2.equals("Wed Mar 24 23:59:00 CET 2021" )); + assert(arrivalDate3.equals("Wed Mar 24 23:59:00 CET 2021" )); + + + } + + /** + * Test encode test tickets in UTC and decode in CET. + * + * @throws IllegalArgumentException the illegal argument exception + * @throws IllegalAccessException the illegal access exception + * @throws ParseException + */ + @Test public void testCETGMTencoding() throws IllegalArgumentException, IllegalAccessException, ParseException { + + //get tickets + decodeTestTicketUTC(); + + //set the dates in the time zone GMT + TimeZone.setDefault(TimeZone.getTimeZone("CET")); + iTicketDecodedFromAsn1Case1.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-12:30" )); + iTicketDecodedFromAsn1Case2.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-00:30" )); + iTicketDecodedFromAsn1Case3.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.03-23:30" )); + + setdepartureDateTo("2021.03.14-00:00"); + setarrivalDateTo("2021.03.24-23:59"); + + encode("CET"); + decode("GMT"); + decodedDateToStrings(); + + assert(issuingDate1.equals("Thu Mar 04 12:30:00 GMT 2021" )); + assert(issuingDate2.equals("Thu Mar 04 00:30:00 GMT 2021" )); + assert(issuingDate3.equals("Wed Mar 03 23:30:00 GMT 2021" )); + + assert(departureDate1.equals("Sun Mar 14 00:00:00 GMT 2021" )); + assert(departureDate2.equals("Sun Mar 14 00:00:00 GMT 2021" )); + assert(departureDate3.equals("Sun Mar 14 00:00:00 GMT 2021" )); + + assert(arrivalDate1.equals("Wed Mar 24 23:59:00 GMT 2021" )); + assert(arrivalDate2.equals("Wed Mar 24 23:59:00 GMT 2021" )); + assert(arrivalDate3.equals("Wed Mar 24 23:59:00 GMT 2021" )); + + + } + + private void decodedDateToStrings() { + + issuingDate1 = iTicketDecodedCase1.getIssuerDetails().getIssuingDate().toString(); + issuingDate2 = iTicketDecodedCase2.getIssuerDetails().getIssuingDate().toString(); + issuingDate3 = iTicketDecodedCase3.getIssuerDetails().getIssuingDate().toString(); + + departureDate1 = ((IReservation) iTicketDecodedCase1.getDocumentData().iterator().next()).getDepartureDate().toString(); + departureDate2 = ((IReservation) iTicketDecodedCase2.getDocumentData().iterator().next()).getDepartureDate().toString(); + departureDate3 = ((IReservation) iTicketDecodedCase3.getDocumentData().iterator().next()).getDepartureDate().toString(); + + arrivalDate1 = ((IReservation) iTicketDecodedCase1.getDocumentData().iterator().next()).getArrivalDate().toString(); + arrivalDate2 = ((IReservation) iTicketDecodedCase2.getDocumentData().iterator().next()).getArrivalDate().toString(); + arrivalDate3 = ((IReservation) iTicketDecodedCase3.getDocumentData().iterator().next()).getArrivalDate().toString(); + + + } + + + private void setdepartureDateTo(String dateString) throws ParseException { + DateFormat dateFormat = new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ); + Date date = dateFormat.parse(dateString); + ((IReservation) iTicketDecodedFromAsn1Case1.getDocumentData().iterator().next()).setDepartureDate(date); + ((IReservation) iTicketDecodedFromAsn1Case2.getDocumentData().iterator().next()).setDepartureDate(date); + ((IReservation) iTicketDecodedFromAsn1Case3.getDocumentData().iterator().next()).setDepartureDate(date); + } + + private void setarrivalDateTo(String dateString) throws ParseException { + DateFormat dateFormat = new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ); + Date date = dateFormat.parse(dateString); + ((IReservation) iTicketDecodedFromAsn1Case1.getDocumentData().iterator().next()).setArrivalDate(date); + ((IReservation) iTicketDecodedFromAsn1Case2.getDocumentData().iterator().next()).setArrivalDate(date); + ((IReservation) iTicketDecodedFromAsn1Case3.getDocumentData().iterator().next()).setArrivalDate(date); + } + + + /** + * Encode in Time Zone + */ + private void encode(String timeZone) { + TimeZone.setDefault(TimeZone.getTimeZone(timeZone)); + try { + encodedInTimeZone1 = encoder.encode(iTicketDecodedFromAsn1Case1); + encodedInTimeZone2 = encoder.encode(iTicketDecodedFromAsn1Case2); + encodedInTimeZone3 = encoder.encode(iTicketDecodedFromAsn1Case3); + } catch (EncodingFormatException e) { + e.printStackTrace(); + } + } + + /** + * Decode in Time Zone + */ + private void decode(String timeZone) { + TimeZone.setDefault(TimeZone.getTimeZone(timeZone)); + + try { + iTicketDecodedCase1 = decoder.decodeFromAsn(encodedInTimeZone1); + iTicketDecodedCase2 = decoder.decodeFromAsn(encodedInTimeZone2); + iTicketDecodedCase3 = decoder.decodeFromAsn(encodedInTimeZone3); + } catch (IOException e) { + e.printStackTrace(); + } + } + + + /** + * Encode decode. + */ + private void decodeTestTicketUTC() { + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); + try { + iTicketDecodedFromAsn1Case1 = decoder.decodeFromAsn(encoded1); + iTicketDecodedFromAsn1Case2 = decoder.decodeFromAsn(encoded2); + iTicketDecodedFromAsn1Case3 = decoder.decodeFromAsn(encoded3); + } catch (IOException e) { + e.printStackTrace(); + } + + } + +} diff --git a/src/test/java/org/uic/barcode/ticket/api/test/ReservationTimeZoneTestV2.java b/src/test/java/org/uic/barcode/ticket/api/test/ReservationTimeZoneTestV2.java new file mode 100644 index 0000000..34d3bc2 --- /dev/null +++ b/src/test/java/org/uic/barcode/ticket/api/test/ReservationTimeZoneTestV2.java @@ -0,0 +1,312 @@ +package org.uic.barcode.ticket.api.test; + +import java.io.IOException; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.TimeZone; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.uic.barcode.asn1.uper.UperEncoder; +import org.uic.barcode.ticket.EncodingFormatException; +import org.uic.barcode.ticket.api.asn.omv2.UicRailTicketData; +import org.uic.barcode.ticket.api.spec.IReservation; +import org.uic.barcode.ticket.api.spec.IUicRailTicket; +import org.uic.barcode.ticket.api.test.testtickets.ReservationTestTicketV2; +import org.uic.barcode.ticket.api.utils.Api2OpenAsnEncoderV2; +import org.uic.barcode.ticket.api.utils.OpenAsn2ApiDecoderV2; + + +/** + * The Class FipTimeZoneTestV1. + * + * + * + */ +public class ReservationTimeZoneTestV2 { + + /** The low level encoded test ticket test case 1 . */ + private byte[] encoded1 = null; + + /** The low level encoded test ticket test case 2 . */ + private byte[] encoded2 = null; + + /** The low level encoded test ticket test case 3 . */ + private byte[] encoded3 = null; + + /** The decoder. */ + OpenAsn2ApiDecoderV2 decoder = new OpenAsn2ApiDecoderV2(); + + /** The encoder. */ + Api2OpenAsnEncoderV2 encoder = new Api2OpenAsnEncoderV2(); + + /** The API ticket low level encoded for case 1. */ + IUicRailTicket iTicketDecodedFromAsn1Case1 = null; + + /** The API ticket low level encoded for case 2. */ + IUicRailTicket iTicketDecodedFromAsn1Case2 = null; + + /** The API ticket low level encoded for case 3. */ + IUicRailTicket iTicketDecodedFromAsn1Case3 = null; + + + /** The ticket decoded 1. */ + IUicRailTicket iTicketDecodedCase1 = null; + + /** The i ticket decoded 2. */ + IUicRailTicket iTicketDecodedCase2 = null; + + /** The i ticket decoded 3. */ + IUicRailTicket iTicketDecodedCase3 = null; + + byte[] encodedInTimeZone1 = null; + byte[] encodedInTimeZone2 = null; + byte[] encodedInTimeZone3 = null; + + String issuingDate1 = null; + String issuingDate2 = null; + String issuingDate3 = null; + + String departureDate1 = null; + String departureDate2 = null; + String departureDate3 = null; + + String arrivalDate1 = null; + String arrivalDate2 = null; + String arrivalDate3 = null; + + TimeZone defaulttimeZone = null; + + /** + * Prepare tickets. + */ + @Before public void prepare() { + + defaulttimeZone = TimeZone.getDefault(); + + UicRailTicketData ticket1 = ReservationTestTicketV2.getUicTestTicket(); + UicRailTicketData ticket2 = ReservationTestTicketV2.getUicTestTicket(); + UicRailTicketData ticket3 = ReservationTestTicketV2.getUicTestTicket(); + + //encode in UTC time zone + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); + + encoded1 = UperEncoder.encode(ticket1); + encoded2 = UperEncoder.encode(ticket2); + encoded3 = UperEncoder.encode(ticket3); + + } + + /** + * clean up + */ + @After public void resetTimeZone() { + TimeZone.setDefault(defaulttimeZone); + } + + + /** + * Test encode test tickets in UTC and decode in CET. + * + * @throws IllegalArgumentException the illegal argument exception + * @throws IllegalAccessException the illegal access exception + * @throws ParseException + */ + @Test public void testCETCETencoding() throws IllegalArgumentException, IllegalAccessException, ParseException { + + //get tickets + decodeTestTicketUTC(); + + //set the dates in the time zone CET + TimeZone.setDefault(TimeZone.getTimeZone("CET")); + iTicketDecodedFromAsn1Case1.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-12:30" )); + iTicketDecodedFromAsn1Case2.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-00:30" )); + iTicketDecodedFromAsn1Case3.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.03-23:30" )); + + setdepartureDateTo("2021.03.14-00:00"); + setarrivalDateTo("2021.03.24-23:59"); + + encode("CET"); + decode("CET"); + decodedDateToStrings(); + + assert(issuingDate1.equals("Thu Mar 04 13:30:00 CET 2021" )); + assert(issuingDate2.equals("Thu Mar 04 01:30:00 CET 2021" )); + assert(issuingDate3.equals("Thu Mar 04 00:30:00 CET 2021" )); + + assert(departureDate1.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(departureDate2.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(departureDate3.equals("Sun Mar 14 00:00:00 CET 2021" )); + + assert(arrivalDate1.equals("Wed Mar 24 23:59:00 CET 2021" )); + assert(arrivalDate2.equals("Wed Mar 24 23:59:00 CET 2021" )); + assert(arrivalDate3.equals("Wed Mar 24 23:59:00 CET 2021" )); + + + } + + + /** + * Test encode test tickets in UTC and decode in CET. + * + * @throws IllegalArgumentException the illegal argument exception + * @throws IllegalAccessException the illegal access exception + * @throws ParseException + */ + @Test public void testGMTCETencoding() throws IllegalArgumentException, IllegalAccessException, ParseException { + + //get tickets + decodeTestTicketUTC(); + + //set the dates in the time zone GMT + TimeZone.setDefault(TimeZone.getTimeZone("GMT")); + iTicketDecodedFromAsn1Case1.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-12:30" )); + iTicketDecodedFromAsn1Case2.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-00:30" )); + iTicketDecodedFromAsn1Case3.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.03-23:30" )); + + setdepartureDateTo("2021.03.14-00:00"); + setarrivalDateTo("2021.03.24-23:59"); + + encode("GMT"); + decode("CET"); + decodedDateToStrings(); + + assert(issuingDate1.equals("Thu Mar 04 13:30:00 CET 2021" )); + assert(issuingDate2.equals("Thu Mar 04 01:30:00 CET 2021" )); + assert(issuingDate3.equals("Thu Mar 04 00:30:00 CET 2021" )); + + assert(departureDate1.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(departureDate2.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(departureDate3.equals("Sun Mar 14 00:00:00 CET 2021" )); + + assert(arrivalDate1.equals("Wed Mar 24 23:59:00 CET 2021" )); + assert(arrivalDate2.equals("Wed Mar 24 23:59:00 CET 2021" )); + assert(arrivalDate3.equals("Wed Mar 24 23:59:00 CET 2021" )); + + + } + + /** + * Test encode test tickets in UTC and decode in CET. + * + * @throws IllegalArgumentException the illegal argument exception + * @throws IllegalAccessException the illegal access exception + * @throws ParseException + */ + @Test public void testCETGMTencoding() throws IllegalArgumentException, IllegalAccessException, ParseException { + + //get tickets + decodeTestTicketUTC(); + + //set the dates in the time zone GMT + TimeZone.setDefault(TimeZone.getTimeZone("CET")); + iTicketDecodedFromAsn1Case1.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-12:30" )); + iTicketDecodedFromAsn1Case2.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-00:30" )); + iTicketDecodedFromAsn1Case3.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.03-23:30" )); + + setdepartureDateTo("2021.03.14-00:00"); + setarrivalDateTo("2021.03.24-23:59"); + + encode("CET"); + decode("GMT"); + decodedDateToStrings(); + + assert(issuingDate1.equals("Thu Mar 04 12:30:00 GMT 2021" )); + assert(issuingDate2.equals("Thu Mar 04 00:30:00 GMT 2021" )); + assert(issuingDate3.equals("Wed Mar 03 23:30:00 GMT 2021" )); + + assert(departureDate1.equals("Sun Mar 14 00:00:00 GMT 2021" )); + assert(departureDate2.equals("Sun Mar 14 00:00:00 GMT 2021" )); + assert(departureDate3.equals("Sun Mar 14 00:00:00 GMT 2021" )); + + assert(arrivalDate1.equals("Wed Mar 24 23:59:00 GMT 2021" )); + assert(arrivalDate2.equals("Wed Mar 24 23:59:00 GMT 2021" )); + assert(arrivalDate3.equals("Wed Mar 24 23:59:00 GMT 2021" )); + + + } + + private void decodedDateToStrings() { + + issuingDate1 = iTicketDecodedCase1.getIssuerDetails().getIssuingDate().toString(); + issuingDate2 = iTicketDecodedCase2.getIssuerDetails().getIssuingDate().toString(); + issuingDate3 = iTicketDecodedCase3.getIssuerDetails().getIssuingDate().toString(); + + departureDate1 = ((IReservation) iTicketDecodedCase1.getDocumentData().iterator().next()).getDepartureDate().toString(); + departureDate2 = ((IReservation) iTicketDecodedCase2.getDocumentData().iterator().next()).getDepartureDate().toString(); + departureDate3 = ((IReservation) iTicketDecodedCase3.getDocumentData().iterator().next()).getDepartureDate().toString(); + + arrivalDate1 = ((IReservation) iTicketDecodedCase1.getDocumentData().iterator().next()).getArrivalDate().toString(); + arrivalDate2 = ((IReservation) iTicketDecodedCase2.getDocumentData().iterator().next()).getArrivalDate().toString(); + arrivalDate3 = ((IReservation) iTicketDecodedCase3.getDocumentData().iterator().next()).getArrivalDate().toString(); + + + } + + + private void setdepartureDateTo(String dateString) throws ParseException { + DateFormat dateFormat = new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ); + Date date = dateFormat.parse(dateString); + ((IReservation) iTicketDecodedFromAsn1Case1.getDocumentData().iterator().next()).setDepartureDate(date); + ((IReservation) iTicketDecodedFromAsn1Case2.getDocumentData().iterator().next()).setDepartureDate(date); + ((IReservation) iTicketDecodedFromAsn1Case3.getDocumentData().iterator().next()).setDepartureDate(date); + } + + private void setarrivalDateTo(String dateString) throws ParseException { + DateFormat dateFormat = new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ); + Date date = dateFormat.parse(dateString); + ((IReservation) iTicketDecodedFromAsn1Case1.getDocumentData().iterator().next()).setArrivalDate(date); + ((IReservation) iTicketDecodedFromAsn1Case2.getDocumentData().iterator().next()).setArrivalDate(date); + ((IReservation) iTicketDecodedFromAsn1Case3.getDocumentData().iterator().next()).setArrivalDate(date); + } + + + /** + * Encode in Time Zone + */ + private void encode(String timeZone) { + TimeZone.setDefault(TimeZone.getTimeZone(timeZone)); + try { + encodedInTimeZone1 = encoder.encode(iTicketDecodedFromAsn1Case1); + encodedInTimeZone2 = encoder.encode(iTicketDecodedFromAsn1Case2); + encodedInTimeZone3 = encoder.encode(iTicketDecodedFromAsn1Case3); + } catch (EncodingFormatException e) { + e.printStackTrace(); + } + } + + /** + * Decode in Time Zone + */ + private void decode(String timeZone) { + TimeZone.setDefault(TimeZone.getTimeZone(timeZone)); + + try { + iTicketDecodedCase1 = decoder.decodeFromAsn(encodedInTimeZone1); + iTicketDecodedCase2 = decoder.decodeFromAsn(encodedInTimeZone2); + iTicketDecodedCase3 = decoder.decodeFromAsn(encodedInTimeZone3); + } catch (IOException e) { + e.printStackTrace(); + } + } + + + /** + * Encode decode. + */ + private void decodeTestTicketUTC() { + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); + try { + iTicketDecodedFromAsn1Case1 = decoder.decodeFromAsn(encoded1); + iTicketDecodedFromAsn1Case2 = decoder.decodeFromAsn(encoded2); + iTicketDecodedFromAsn1Case3 = decoder.decodeFromAsn(encoded3); + } catch (IOException e) { + e.printStackTrace(); + } + + } + +} diff --git a/src/test/java/org/uic/barcode/ticket/api/test/StationPassageTimeZoneTestV1.java b/src/test/java/org/uic/barcode/ticket/api/test/StationPassageTimeZoneTestV1.java new file mode 100644 index 0000000..aedf569 --- /dev/null +++ b/src/test/java/org/uic/barcode/ticket/api/test/StationPassageTimeZoneTestV1.java @@ -0,0 +1,312 @@ +package org.uic.barcode.ticket.api.test; + +import java.io.IOException; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.TimeZone; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.uic.barcode.asn1.uper.UperEncoder; +import org.uic.barcode.ticket.EncodingFormatException; +import org.uic.barcode.ticket.api.asn.omv1.UicRailTicketData; +import org.uic.barcode.ticket.api.spec.IStationPassage; +import org.uic.barcode.ticket.api.spec.IUicRailTicket; +import org.uic.barcode.ticket.api.test.testtickets.StationPassageTestTicketV1; +import org.uic.barcode.ticket.api.utils.Api2OpenAsnEncoder; +import org.uic.barcode.ticket.api.utils.OpenAsn2ApiDecoder; + + +/** + * The Class FipTimeZoneTestV1. + * + * + * + */ +public class StationPassageTimeZoneTestV1 { + + /** The low level encoded test ticket test case 1 . */ + private byte[] encoded1 = null; + + /** The low level encoded test ticket test case 2 . */ + private byte[] encoded2 = null; + + /** The low level encoded test ticket test case 3 . */ + private byte[] encoded3 = null; + + /** The decoder. */ + OpenAsn2ApiDecoder decoder = new OpenAsn2ApiDecoder(); + + /** The encoder. */ + Api2OpenAsnEncoder encoder = new Api2OpenAsnEncoder(); + + /** The API ticket low level encoded for case 1. */ + IUicRailTicket iTicketDecodedFromAsn1Case1 = null; + + /** The API ticket low level encoded for case 2. */ + IUicRailTicket iTicketDecodedFromAsn1Case2 = null; + + /** The API ticket low level encoded for case 3. */ + IUicRailTicket iTicketDecodedFromAsn1Case3 = null; + + + /** The ticket decoded 1. */ + IUicRailTicket iTicketDecodedCase1 = null; + + /** The i ticket decoded 2. */ + IUicRailTicket iTicketDecodedCase2 = null; + + /** The i ticket decoded 3. */ + IUicRailTicket iTicketDecodedCase3 = null; + + byte[] encodedInTimeZone1 = null; + byte[] encodedInTimeZone2 = null; + byte[] encodedInTimeZone3 = null; + + String issuingDate1 = null; + String issuingDate2 = null; + String issuingDate3 = null; + + String validFromDate1 = null; + String validFromDate2 = null; + String validFromDate3 = null; + + String validUntilDate1 = null; + String validUntilDate2 = null; + String validUntilDate3 = null; + + TimeZone defaulttimeZone = null; + + /** + * Prepare tickets. + */ + @Before public void prepare() { + + defaulttimeZone = TimeZone.getDefault(); + + UicRailTicketData ticket1 = StationPassageTestTicketV1.getUicTestTicket(); + UicRailTicketData ticket2 = StationPassageTestTicketV1.getUicTestTicket(); + UicRailTicketData ticket3 = StationPassageTestTicketV1.getUicTestTicket(); + + //encode in UTC time zone + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); + + encoded1 = UperEncoder.encode(ticket1); + encoded2 = UperEncoder.encode(ticket2); + encoded3 = UperEncoder.encode(ticket3); + + } + + /** + * clean up + */ + @After public void resetTimeZone() { + TimeZone.setDefault(defaulttimeZone); + } + + + /** + * Test encode test tickets in UTC and decode in CET. + * + * @throws IllegalArgumentException the illegal argument exception + * @throws IllegalAccessException the illegal access exception + * @throws ParseException + */ + @Test public void testCETCETencoding() throws IllegalArgumentException, IllegalAccessException, ParseException { + + //get tickets + decodeTestTicketUTC(); + + //set the dates in the time zone CET + TimeZone.setDefault(TimeZone.getTimeZone("CET")); + iTicketDecodedFromAsn1Case1.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-12:30" )); + iTicketDecodedFromAsn1Case2.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-00:30" )); + iTicketDecodedFromAsn1Case3.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.03-23:30" )); + + setValidFromDateTo("2021.03.14-00:00"); + setUntilDateTo("2021.03.24-23:59"); + + encode("CET"); + decode("CET"); + decodedDateToStrings(); + + assert(issuingDate1.equals("Thu Mar 04 13:30:00 CET 2021" )); + assert(issuingDate2.equals("Thu Mar 04 01:30:00 CET 2021" )); + assert(issuingDate3.equals("Thu Mar 04 00:30:00 CET 2021" )); + + assert(validFromDate1.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(validFromDate2.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(validFromDate3.equals("Sun Mar 14 00:00:00 CET 2021" )); + + assert(validUntilDate1.equals("Wed Mar 24 23:59:00 CET 2021" )); + assert(validUntilDate2.equals("Wed Mar 24 23:59:00 CET 2021" )); + assert(validUntilDate3.equals("Wed Mar 24 23:59:00 CET 2021" )); + + + } + + + /** + * Test encode test tickets in UTC and decode in CET. + * + * @throws IllegalArgumentException the illegal argument exception + * @throws IllegalAccessException the illegal access exception + * @throws ParseException + */ + @Test public void testGMTCETencoding() throws IllegalArgumentException, IllegalAccessException, ParseException { + + //get tickets + decodeTestTicketUTC(); + + //set the dates in the time zone GMT + TimeZone.setDefault(TimeZone.getTimeZone("GMT")); + iTicketDecodedFromAsn1Case1.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-12:30" )); + iTicketDecodedFromAsn1Case2.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-00:30" )); + iTicketDecodedFromAsn1Case3.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.03-23:30" )); + + setValidFromDateTo("2021.03.14-00:00"); + setUntilDateTo("2021.03.24-23:59"); + + encode("GMT"); + decode("CET"); + decodedDateToStrings(); + + assert(issuingDate1.equals("Thu Mar 04 13:30:00 CET 2021" )); + assert(issuingDate2.equals("Thu Mar 04 01:30:00 CET 2021" )); + assert(issuingDate3.equals("Thu Mar 04 00:30:00 CET 2021" )); + + assert(validFromDate1.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(validFromDate2.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(validFromDate3.equals("Sun Mar 14 00:00:00 CET 2021" )); + + assert(validUntilDate1.equals("Wed Mar 24 23:59:00 CET 2021" )); + assert(validUntilDate2.equals("Wed Mar 24 23:59:00 CET 2021" )); + assert(validUntilDate3.equals("Wed Mar 24 23:59:00 CET 2021" )); + + + } + + /** + * Test encode test tickets in UTC and decode in CET. + * + * @throws IllegalArgumentException the illegal argument exception + * @throws IllegalAccessException the illegal access exception + * @throws ParseException + */ + @Test public void testCETGMTencoding() throws IllegalArgumentException, IllegalAccessException, ParseException { + + //get tickets + decodeTestTicketUTC(); + + //set the dates in the time zone GMT + TimeZone.setDefault(TimeZone.getTimeZone("CET")); + iTicketDecodedFromAsn1Case1.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-12:30" )); + iTicketDecodedFromAsn1Case2.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-00:30" )); + iTicketDecodedFromAsn1Case3.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.03-23:30" )); + + setValidFromDateTo("2021.03.14-00:00"); + setUntilDateTo("2021.03.24-23:59"); + + encode("CET"); + decode("GMT"); + decodedDateToStrings(); + + assert(issuingDate1.equals("Thu Mar 04 12:30:00 GMT 2021" )); + assert(issuingDate2.equals("Thu Mar 04 00:30:00 GMT 2021" )); + assert(issuingDate3.equals("Wed Mar 03 23:30:00 GMT 2021" )); + + assert(validFromDate1.equals("Sun Mar 14 00:00:00 GMT 2021" )); + assert(validFromDate2.equals("Sun Mar 14 00:00:00 GMT 2021" )); + assert(validFromDate3.equals("Sun Mar 14 00:00:00 GMT 2021" )); + + assert(validUntilDate1.equals("Wed Mar 24 23:59:00 GMT 2021" )); + assert(validUntilDate2.equals("Wed Mar 24 23:59:00 GMT 2021" )); + assert(validUntilDate3.equals("Wed Mar 24 23:59:00 GMT 2021" )); + + + } + + private void decodedDateToStrings() { + + issuingDate1 = iTicketDecodedCase1.getIssuerDetails().getIssuingDate().toString(); + issuingDate2 = iTicketDecodedCase2.getIssuerDetails().getIssuingDate().toString(); + issuingDate3 = iTicketDecodedCase3.getIssuerDetails().getIssuingDate().toString(); + + validFromDate1 = ((IStationPassage) iTicketDecodedCase1.getDocumentData().iterator().next()).getValidFrom().toString(); + validFromDate2 = ((IStationPassage) iTicketDecodedCase2.getDocumentData().iterator().next()).getValidFrom().toString(); + validFromDate3 = ((IStationPassage) iTicketDecodedCase3.getDocumentData().iterator().next()).getValidFrom().toString(); + + validUntilDate1 = ((IStationPassage) iTicketDecodedCase1.getDocumentData().iterator().next()).getValidUntil().toString(); + validUntilDate2 = ((IStationPassage) iTicketDecodedCase2.getDocumentData().iterator().next()).getValidUntil().toString(); + validUntilDate3 = ((IStationPassage) iTicketDecodedCase3.getDocumentData().iterator().next()).getValidUntil().toString(); + + + } + + + private void setValidFromDateTo(String dateString) throws ParseException { + DateFormat dateFormat = new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ); + Date date = dateFormat.parse(dateString); + ((IStationPassage) iTicketDecodedFromAsn1Case1.getDocumentData().iterator().next()).setValidFrom(date); + ((IStationPassage) iTicketDecodedFromAsn1Case2.getDocumentData().iterator().next()).setValidFrom(date); + ((IStationPassage) iTicketDecodedFromAsn1Case3.getDocumentData().iterator().next()).setValidFrom(date); + } + + private void setUntilDateTo(String dateString) throws ParseException { + DateFormat dateFormat = new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ); + Date date = dateFormat.parse(dateString); + ((IStationPassage) iTicketDecodedFromAsn1Case1.getDocumentData().iterator().next()).setValidUntil(date); + ((IStationPassage) iTicketDecodedFromAsn1Case2.getDocumentData().iterator().next()).setValidUntil(date); + ((IStationPassage) iTicketDecodedFromAsn1Case3.getDocumentData().iterator().next()).setValidUntil(date); + } + + + /** + * Encode in Time Zone + */ + private void encode(String timeZone) { + TimeZone.setDefault(TimeZone.getTimeZone(timeZone)); + try { + encodedInTimeZone1 = encoder.encode(iTicketDecodedFromAsn1Case1); + encodedInTimeZone2 = encoder.encode(iTicketDecodedFromAsn1Case2); + encodedInTimeZone3 = encoder.encode(iTicketDecodedFromAsn1Case3); + } catch (EncodingFormatException e) { + e.printStackTrace(); + } + } + + /** + * Decode in Time Zone + */ + private void decode(String timeZone) { + TimeZone.setDefault(TimeZone.getTimeZone(timeZone)); + + try { + iTicketDecodedCase1 = decoder.decodeFromAsn(encodedInTimeZone1); + iTicketDecodedCase2 = decoder.decodeFromAsn(encodedInTimeZone2); + iTicketDecodedCase3 = decoder.decodeFromAsn(encodedInTimeZone3); + } catch (IOException e) { + e.printStackTrace(); + } + } + + + /** + * Encode decode. + */ + private void decodeTestTicketUTC() { + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); + try { + iTicketDecodedFromAsn1Case1 = decoder.decodeFromAsn(encoded1); + iTicketDecodedFromAsn1Case2 = decoder.decodeFromAsn(encoded2); + iTicketDecodedFromAsn1Case3 = decoder.decodeFromAsn(encoded3); + } catch (IOException e) { + e.printStackTrace(); + } + + } + +} diff --git a/src/test/java/org/uic/barcode/ticket/api/test/StationPassageTimeZoneTestV2.java b/src/test/java/org/uic/barcode/ticket/api/test/StationPassageTimeZoneTestV2.java new file mode 100644 index 0000000..e87e968 --- /dev/null +++ b/src/test/java/org/uic/barcode/ticket/api/test/StationPassageTimeZoneTestV2.java @@ -0,0 +1,312 @@ +package org.uic.barcode.ticket.api.test; + +import java.io.IOException; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.TimeZone; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.uic.barcode.asn1.uper.UperEncoder; +import org.uic.barcode.ticket.EncodingFormatException; +import org.uic.barcode.ticket.api.asn.omv2.UicRailTicketData; +import org.uic.barcode.ticket.api.spec.IStationPassage; +import org.uic.barcode.ticket.api.spec.IUicRailTicket; +import org.uic.barcode.ticket.api.test.testtickets.StationPassageTestTicketV2; +import org.uic.barcode.ticket.api.utils.Api2OpenAsnEncoderV2; +import org.uic.barcode.ticket.api.utils.OpenAsn2ApiDecoderV2; + + +/** + * The Class FipTimeZoneTestV2. + * + * + * + */ +public class StationPassageTimeZoneTestV2 { + + /** The low level encoded test ticket test case 1 . */ + private byte[] encoded1 = null; + + /** The low level encoded test ticket test case 2 . */ + private byte[] encoded2 = null; + + /** The low level encoded test ticket test case 3 . */ + private byte[] encoded3 = null; + + /** The decoder. */ + OpenAsn2ApiDecoderV2 decoder = new OpenAsn2ApiDecoderV2(); + + /** The encoder. */ + Api2OpenAsnEncoderV2 encoder = new Api2OpenAsnEncoderV2(); + + /** The API ticket low level encoded for case 1. */ + IUicRailTicket iTicketDecodedFromAsn1Case1 = null; + + /** The API ticket low level encoded for case 2. */ + IUicRailTicket iTicketDecodedFromAsn1Case2 = null; + + /** The API ticket low level encoded for case 3. */ + IUicRailTicket iTicketDecodedFromAsn1Case3 = null; + + + /** The ticket decoded 1. */ + IUicRailTicket iTicketDecodedCase1 = null; + + /** The i ticket decoded 2. */ + IUicRailTicket iTicketDecodedCase2 = null; + + /** The i ticket decoded 3. */ + IUicRailTicket iTicketDecodedCase3 = null; + + byte[] encodedInTimeZone1 = null; + byte[] encodedInTimeZone2 = null; + byte[] encodedInTimeZone3 = null; + + String issuingDate1 = null; + String issuingDate2 = null; + String issuingDate3 = null; + + String validFromDate1 = null; + String validFromDate2 = null; + String validFromDate3 = null; + + String validUntilDate1 = null; + String validUntilDate2 = null; + String validUntilDate3 = null; + + TimeZone defaulttimeZone = null; + + /** + * Prepare tickets. + */ + @Before public void prepare() { + + defaulttimeZone = TimeZone.getDefault(); + + UicRailTicketData ticket1 = StationPassageTestTicketV2.getUicTestTicket(); + UicRailTicketData ticket2 = StationPassageTestTicketV2.getUicTestTicket(); + UicRailTicketData ticket3 = StationPassageTestTicketV2.getUicTestTicket(); + + //encode in UTC time zone + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); + + encoded1 = UperEncoder.encode(ticket1); + encoded2 = UperEncoder.encode(ticket2); + encoded3 = UperEncoder.encode(ticket3); + + } + + /** + * clean up + */ + @After public void resetTimeZone() { + TimeZone.setDefault(defaulttimeZone); + } + + + /** + * Test encode test tickets in UTC and decode in CET. + * + * @throws IllegalArgumentException the illegal argument exception + * @throws IllegalAccessException the illegal access exception + * @throws ParseException + */ + @Test public void testCETCETencoding() throws IllegalArgumentException, IllegalAccessException, ParseException { + + //get tickets + decodeTestTicketUTC(); + + //set the dates in the time zone CET + TimeZone.setDefault(TimeZone.getTimeZone("CET")); + iTicketDecodedFromAsn1Case1.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-12:30" )); + iTicketDecodedFromAsn1Case2.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-00:30" )); + iTicketDecodedFromAsn1Case3.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.03-23:30" )); + + setValidFromDateTo("2021.03.14-00:00"); + setUntilDateTo("2021.03.24-23:59"); + + encode("CET"); + decode("CET"); + decodedDateToStrings(); + + assert(issuingDate1.equals("Thu Mar 04 13:30:00 CET 2021" )); + assert(issuingDate2.equals("Thu Mar 04 01:30:00 CET 2021" )); + assert(issuingDate3.equals("Thu Mar 04 00:30:00 CET 2021" )); + + assert(validFromDate1.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(validFromDate2.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(validFromDate3.equals("Sun Mar 14 00:00:00 CET 2021" )); + + assert(validUntilDate1.equals("Wed Mar 24 23:59:00 CET 2021" )); + assert(validUntilDate2.equals("Wed Mar 24 23:59:00 CET 2021" )); + assert(validUntilDate3.equals("Wed Mar 24 23:59:00 CET 2021" )); + + + } + + + /** + * Test encode test tickets in UTC and decode in CET. + * + * @throws IllegalArgumentException the illegal argument exception + * @throws IllegalAccessException the illegal access exception + * @throws ParseException + */ + @Test public void testGMTCETencoding() throws IllegalArgumentException, IllegalAccessException, ParseException { + + //get tickets + decodeTestTicketUTC(); + + //set the dates in the time zone GMT + TimeZone.setDefault(TimeZone.getTimeZone("GMT")); + iTicketDecodedFromAsn1Case1.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-12:30" )); + iTicketDecodedFromAsn1Case2.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-00:30" )); + iTicketDecodedFromAsn1Case3.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.03-23:30" )); + + setValidFromDateTo("2021.03.14-00:00"); + setUntilDateTo("2021.03.24-23:59"); + + encode("GMT"); + decode("CET"); + decodedDateToStrings(); + + assert(issuingDate1.equals("Thu Mar 04 13:30:00 CET 2021" )); + assert(issuingDate2.equals("Thu Mar 04 01:30:00 CET 2021" )); + assert(issuingDate3.equals("Thu Mar 04 00:30:00 CET 2021" )); + + assert(validFromDate1.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(validFromDate2.equals("Sun Mar 14 00:00:00 CET 2021" )); + assert(validFromDate3.equals("Sun Mar 14 00:00:00 CET 2021" )); + + assert(validUntilDate1.equals("Wed Mar 24 23:59:00 CET 2021" )); + assert(validUntilDate2.equals("Wed Mar 24 23:59:00 CET 2021" )); + assert(validUntilDate3.equals("Wed Mar 24 23:59:00 CET 2021" )); + + + } + + /** + * Test encode test tickets in UTC and decode in CET. + * + * @throws IllegalArgumentException the illegal argument exception + * @throws IllegalAccessException the illegal access exception + * @throws ParseException + */ + @Test public void testCETGMTencoding() throws IllegalArgumentException, IllegalAccessException, ParseException { + + //get tickets + decodeTestTicketUTC(); + + //set the dates in the time zone GMT + TimeZone.setDefault(TimeZone.getTimeZone("CET")); + iTicketDecodedFromAsn1Case1.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-12:30" )); + iTicketDecodedFromAsn1Case2.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-00:30" )); + iTicketDecodedFromAsn1Case3.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.03-23:30" )); + + setValidFromDateTo("2021.03.14-00:00"); + setUntilDateTo("2021.03.24-23:59"); + + encode("CET"); + decode("GMT"); + decodedDateToStrings(); + + assert(issuingDate1.equals("Thu Mar 04 12:30:00 GMT 2021" )); + assert(issuingDate2.equals("Thu Mar 04 00:30:00 GMT 2021" )); + assert(issuingDate3.equals("Wed Mar 03 23:30:00 GMT 2021" )); + + assert(validFromDate1.equals("Sun Mar 14 00:00:00 GMT 2021" )); + assert(validFromDate2.equals("Sun Mar 14 00:00:00 GMT 2021" )); + assert(validFromDate3.equals("Sun Mar 14 00:00:00 GMT 2021" )); + + assert(validUntilDate1.equals("Wed Mar 24 23:59:00 GMT 2021" )); + assert(validUntilDate2.equals("Wed Mar 24 23:59:00 GMT 2021" )); + assert(validUntilDate3.equals("Wed Mar 24 23:59:00 GMT 2021" )); + + + } + + private void decodedDateToStrings() { + + issuingDate1 = iTicketDecodedCase1.getIssuerDetails().getIssuingDate().toString(); + issuingDate2 = iTicketDecodedCase2.getIssuerDetails().getIssuingDate().toString(); + issuingDate3 = iTicketDecodedCase3.getIssuerDetails().getIssuingDate().toString(); + + validFromDate1 = ((IStationPassage) iTicketDecodedCase1.getDocumentData().iterator().next()).getValidFrom().toString(); + validFromDate2 = ((IStationPassage) iTicketDecodedCase2.getDocumentData().iterator().next()).getValidFrom().toString(); + validFromDate3 = ((IStationPassage) iTicketDecodedCase3.getDocumentData().iterator().next()).getValidFrom().toString(); + + validUntilDate1 = ((IStationPassage) iTicketDecodedCase1.getDocumentData().iterator().next()).getValidUntil().toString(); + validUntilDate2 = ((IStationPassage) iTicketDecodedCase2.getDocumentData().iterator().next()).getValidUntil().toString(); + validUntilDate3 = ((IStationPassage) iTicketDecodedCase3.getDocumentData().iterator().next()).getValidUntil().toString(); + + + } + + + private void setValidFromDateTo(String dateString) throws ParseException { + DateFormat dateFormat = new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ); + Date date = dateFormat.parse(dateString); + ((IStationPassage) iTicketDecodedFromAsn1Case1.getDocumentData().iterator().next()).setValidFrom(date); + ((IStationPassage) iTicketDecodedFromAsn1Case2.getDocumentData().iterator().next()).setValidFrom(date); + ((IStationPassage) iTicketDecodedFromAsn1Case3.getDocumentData().iterator().next()).setValidFrom(date); + } + + private void setUntilDateTo(String dateString) throws ParseException { + DateFormat dateFormat = new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ); + Date date = dateFormat.parse(dateString); + ((IStationPassage) iTicketDecodedFromAsn1Case1.getDocumentData().iterator().next()).setValidUntil(date); + ((IStationPassage) iTicketDecodedFromAsn1Case2.getDocumentData().iterator().next()).setValidUntil(date); + ((IStationPassage) iTicketDecodedFromAsn1Case3.getDocumentData().iterator().next()).setValidUntil(date); + } + + + /** + * Encode in Time Zone + */ + private void encode(String timeZone) { + TimeZone.setDefault(TimeZone.getTimeZone(timeZone)); + try { + encodedInTimeZone1 = encoder.encode(iTicketDecodedFromAsn1Case1); + encodedInTimeZone2 = encoder.encode(iTicketDecodedFromAsn1Case2); + encodedInTimeZone3 = encoder.encode(iTicketDecodedFromAsn1Case3); + } catch (EncodingFormatException e) { + e.printStackTrace(); + } + } + + /** + * Decode in Time Zone + */ + private void decode(String timeZone) { + TimeZone.setDefault(TimeZone.getTimeZone(timeZone)); + + try { + iTicketDecodedCase1 = decoder.decodeFromAsn(encodedInTimeZone1); + iTicketDecodedCase2 = decoder.decodeFromAsn(encodedInTimeZone2); + iTicketDecodedCase3 = decoder.decodeFromAsn(encodedInTimeZone3); + } catch (IOException e) { + e.printStackTrace(); + } + } + + + /** + * Encode decode. + */ + private void decodeTestTicketUTC() { + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); + try { + iTicketDecodedFromAsn1Case1 = decoder.decodeFromAsn(encoded1); + iTicketDecodedFromAsn1Case2 = decoder.decodeFromAsn(encoded2); + iTicketDecodedFromAsn1Case3 = decoder.decodeFromAsn(encoded3); + } catch (IOException e) { + e.printStackTrace(); + } + + } + +} diff --git a/src/test/java/org/uic/barcode/ticket/api/test/UperEncodeOpenTicketTestV1.java b/src/test/java/org/uic/barcode/ticket/api/test/UperEncodeOpenTicketTestV1.java new file mode 100644 index 0000000..cb20d79 --- /dev/null +++ b/src/test/java/org/uic/barcode/ticket/api/test/UperEncodeOpenTicketTestV1.java @@ -0,0 +1,54 @@ +package org.uic.barcode.ticket.api.test; + +import static org.junit.Assert.assertEquals; + +import java.io.IOException; +import java.util.logging.Level; + +import org.junit.Test; +import org.uic.barcode.asn1.uper.UperEncoder; +import org.uic.barcode.ticket.EncodingFormatException; +import org.uic.barcode.ticket.api.asn.omv1.UicRailTicketData; +import org.uic.barcode.ticket.api.spec.IUicRailTicket; +import org.uic.barcode.ticket.api.test.testtickets.OpenTestTicketV1; +import org.uic.barcode.ticket.api.utils.Api2OpenAsnEncoder; +import org.uic.barcode.ticket.api.utils.OpenAsn2ApiDecoder; + + +public class UperEncodeOpenTicketTestV1 { + + + + @Test public void testDecodeTicket() throws IllegalArgumentException, IllegalAccessException { + UicRailTicketData ticket = OpenTestTicketV1.getUicTestTicket(); + byte[] encoded = UperEncoder.encode(ticket); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + //String expectedHex = "78022020050DD3CF9F5CBCAA65E7D284EA40218A02D000822537B43701A237BB82B164CDA358088373CBBB4EFE40EDAF28EE4DEEAE004A03AD12B01416D08000000004039BDC195B951A58DAD95D125B999BC48088040EE0C2E6E6C2CECA021282DAE6E8CAE4C8C2DA0080BDA60100402C800131B20081013A65E7D00805881416D00B20283DA0"; + //assertEquals(hex,expectedHex); + UicRailTicketData decodedTicket = UperEncoder.decode(encoded, UicRailTicketData.class); + OpenAsn2ApiDecoder decoder = new OpenAsn2ApiDecoder(); + IUicRailTicket iTicket = null; + try { + iTicket = decoder.decodeFromAsn(encoded); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + Api2OpenAsnEncoder encoder = new Api2OpenAsnEncoder(); + try { + encoded = encoder.encode(iTicket); + } catch (EncodingFormatException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + assert(decodedTicket != null); + assertEquals(decodedTicket.controlDetail.ageCheckRequired , false ); + assertEquals(decodedTicket.controlDetail.getInfoText() , "cd" ); + assertEquals(decodedTicket.controlDetail.getIncludedTickets().get(0).getProductOwnerIA5() , "test" ); + assert(decodedTicket.controlDetail.getIdentificationByCardReference().get(0).getTrailingCardIdNum() == 100L ); + } + +} diff --git a/src/test/java/org/uic/barcode/ticket/api/test/UperEncodePassTestV1.java b/src/test/java/org/uic/barcode/ticket/api/test/UperEncodePassTestV1.java new file mode 100644 index 0000000..7436f5d --- /dev/null +++ b/src/test/java/org/uic/barcode/ticket/api/test/UperEncodePassTestV1.java @@ -0,0 +1,53 @@ +package org.uic.barcode.ticket.api.test; + +import static org.junit.Assert.assertEquals; + +import java.io.IOException; +import java.util.logging.Level; + +import org.junit.Test; +import org.uic.barcode.asn1.uper.UperEncoder; +import org.uic.barcode.ticket.EncodingFormatException; +import org.uic.barcode.ticket.api.asn.omv1.UicRailTicketData; +import org.uic.barcode.ticket.api.spec.IUicRailTicket; +import org.uic.barcode.ticket.api.test.testtickets.PassTestTicketV1; +import org.uic.barcode.ticket.api.utils.Api2OpenAsnEncoder; +import org.uic.barcode.ticket.api.utils.OpenAsn2ApiDecoder; + + +public class UperEncodePassTestV1 { + + + @Test public void testDecodeTicket() throws IllegalArgumentException, IllegalAccessException { + UicRailTicketData ticket = PassTestTicketV1.getUicTestTicket(); + byte[] encoded = UperEncoder.encode(ticket); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + + UicRailTicketData decodedTicket = UperEncoder.decode(encoded, UicRailTicketData.class); + + OpenAsn2ApiDecoder decoder = new OpenAsn2ApiDecoder(); + IUicRailTicket iTicket = null; + try { + iTicket = decoder.decodeFromAsn(encoded); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + Api2OpenAsnEncoder encoder = new Api2OpenAsnEncoder(); + try { + encoded = encoder.encode(iTicket); + } catch (EncodingFormatException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + assert(decodedTicket != null); + assertEquals(decodedTicket.controlDetail.ageCheckRequired , false ); + assertEquals(decodedTicket.controlDetail.getInfoText() , "cd" ); + assertEquals(decodedTicket.controlDetail.getIncludedTickets().get(0).getProductOwnerIA5() , "test" ); + assert(decodedTicket.controlDetail.getIdentificationByCardReference().get(0).getTrailingCardIdNum() == 100L ); + } + +} diff --git a/src/test/java/org/uic/barcode/ticket/api/test/UperEncodeTicketLinkTestV1.java b/src/test/java/org/uic/barcode/ticket/api/test/UperEncodeTicketLinkTestV1.java new file mode 100644 index 0000000..0a4b04c --- /dev/null +++ b/src/test/java/org/uic/barcode/ticket/api/test/UperEncodeTicketLinkTestV1.java @@ -0,0 +1,75 @@ +package org.uic.barcode.ticket.api.test; + +import static org.junit.Assert.assertEquals; + +import java.io.IOException; +import java.util.logging.Level; + +import org.junit.Test; +import org.uic.barcode.asn1.uper.UperEncoder; +import org.uic.barcode.ticket.api.asn.omv1.UicRailTicketData; +import org.uic.barcode.ticket.api.spec.ILinkMode; +import org.uic.barcode.ticket.api.spec.ITicketLink; +import org.uic.barcode.ticket.api.spec.ITicketType; +import org.uic.barcode.ticket.api.spec.IUicRailTicket; +import org.uic.barcode.ticket.api.test.testtickets.SimpleTicketLinkTestTicket; +import org.uic.barcode.ticket.api.utils.OpenAsn2ApiDecoder; + + +public class UperEncodeTicketLinkTestV1 { + + + + + + @Test public void testEncodeTicket() throws IllegalArgumentException, IllegalAccessException { + UicRailTicketData ticket = SimpleTicketLinkTestTicket.getUicTestTicket(); + byte[] encoded = UperEncoder.encode(ticket); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + + String expectedHex = SimpleTicketLinkTestTicket.getEncodingV1Hex(); + UperEncoder.logger.debug(String.format("first difference at index: %d",hex.compareTo(expectedHex))); + UperEncoder.logger.debug(String.format("data returned: %s",hex)); + UperEncoder.logger.debug(String.format("data expected: %s",expectedHex)); + assert(hex.equals(expectedHex)); + } + + @Test public void testDecodeTicket() throws IllegalArgumentException, IllegalAccessException { + UicRailTicketData ticket = SimpleTicketLinkTestTicket.getUicTestTicket(); + byte[] encoded = UperEncoder.encode(ticket); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + String expectedHex = SimpleTicketLinkTestTicket.getEncodingV1Hex(); + assertEquals(hex,expectedHex); + UicRailTicketData decodedTicket = UperEncoder.decode(encoded, UicRailTicketData.class); + assert(decodedTicket != null); + assertEquals(decodedTicket.controlDetail.ageCheckRequired , false ); + assertEquals(decodedTicket.controlDetail.getInfoText() , "cd" ); + assertEquals(decodedTicket.controlDetail.getIncludedTickets().get(0).getProductOwnerIA5() , "test" ); + assert(decodedTicket.controlDetail.getIdentificationByCardReference().get(0).getTrailingCardIdNum() == 100L ); + assert(decodedTicket.controlDetail.getIncludedTickets().size() == 1); + } + + @Test public void testDecodeTicket2Api() throws IllegalArgumentException, IllegalAccessException { + UicRailTicketData ticket = SimpleTicketLinkTestTicket.getUicTestTicket(); + byte[] encoded = UperEncoder.encode(ticket); + UicRailTicketData decodedTicket = UperEncoder.decode(encoded, UicRailTicketData.class); + + OpenAsn2ApiDecoder decoder = new OpenAsn2ApiDecoder(); + IUicRailTicket uicTicket = null; + try { + uicTicket = decoder.decodeFromAsn(decodedTicket); + } catch (IOException e) { + assert (false); + } + + assert(uicTicket.getControlDetails().getLinkedTickets().size() == 1); + for (ITicketLink link: uicTicket.getControlDetails().getLinkedTickets() ) { + assert(link.getLinkMode() == ILinkMode.onlyValidInCombination); + assert(link.getTicketType() == ITicketType.pass); + } + } + + +} diff --git a/src/test/java/org/uic/barcode/ticket/api/test/UperEncodeTicketTestV1.java b/src/test/java/org/uic/barcode/ticket/api/test/UperEncodeTicketTestV1.java new file mode 100644 index 0000000..cf8eec6 --- /dev/null +++ b/src/test/java/org/uic/barcode/ticket/api/test/UperEncodeTicketTestV1.java @@ -0,0 +1,118 @@ +package org.uic.barcode.ticket.api.test; + +import static org.junit.Assert.assertEquals; + +import java.util.logging.Level; + +import org.junit.Test; +import org.uic.barcode.asn1.uper.UperEncoder; +import org.uic.barcode.ticket.api.asn.omv1.UicRailTicketData; +import org.uic.barcode.ticket.api.test.testtickets.SimpleUicTestTicket; + + +public class UperEncodeTicketTestV1 { + + /** + * Example from the Standard on UPER. + <pre> + value UicRailTicketData ::= { + issuingDetail { + issuingYear 2018 + issuingDay 1 + specimen TRUE, + securePaperTicket FALSE, + activated TRUE, + issuerPNR "issuerTestPNR", + issuedOnLine 12 + } + ,travelerDetail{ + traveler { + { + firstName "John" + ,secondName "Dow" + ,idCard "12345" + ,ticketHolder TRUE + ,status {{customerStatusDescr "senior" }} + } + } + ,groupName "myGroup" + } + ,transportDocument { + { + token {tokenProviderIA5 "VDV", token '82DA'H } + ,ticket openTicket : { + returnIncluded FALSE + infoText "openTicketInfo" + } + } + ,{ + ticket stationPassage : { + productName "passage" + ,stationNameUTF8 { "Amsterdam" } + ,validFromDay 0 + ,numberOfDaysValid 123 + } + } + } + ,controlDetail { + identificationByCardReference { + { trailingCardIdNum 100 } + } + ,identificationByIdCard FALSE + ,identificationByPassportId FALSE + ,passportValidationRequired FALSE + ,onlineValidationRequired FALSE + ,ageCheckRequired FALSE + ,reductionCardCheckRequired FALSE + ,infoText "cd" + ,includedTickets { + { productOwnerIA5 "test" } + } + } + ,extension { + { extensionId "1", extensionData '82DA'H } + ,{ extensionId "2", extensionData '83DA'H } + } + } + + +78022020 050DD3CF 9F5CBCAA 65E7D284 EA40218A 02D00082 2537B437 01A237BB +82B164CD A3580883 73CBBB4E FE40EDAF 28EE4DEE AE004A03 AD12B014 16D08000 +00000403 9BDC195B 951A58DA D95D125B 999BC480 88040EE0 C2E6E6C2 CECA0212 +82DAE6E8 CAE4C8C2 DA0080BD A6010040 2C800131 B2008101 3A65E7D0 08058814 +16D00B20 283DA0 + + </pre> + */ + + + + @Test public void testEncodeTicket() throws IllegalArgumentException, IllegalAccessException { + UicRailTicketData ticket = SimpleUicTestTicket.getUicTestTicket(); + byte[] encoded = UperEncoder.encode(ticket); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + + String expectedHex = "78022020050DD3CF9F5CBCAA65E7D284EA40218A02D000822537B43701A237BB82B164CDA358088373CBBB4EFE40EDAF28EE4DEEAE004A03AD12B01416D08000000004039BDC195B951A58DAD95D125B999BC48088040EE0C2E6E6C2CECA021282DAE6E8CAE4C8C2DA0080BDA60100402C800131B20081013A65E7D00805881416D00B20283DA0"; + UperEncoder.logger.debug(String.format("first difference at index: %d",hex.compareTo(expectedHex))); + UperEncoder.logger.debug(String.format("data returned: %s",hex)); + UperEncoder.logger.debug(String.format("data expected: %s",expectedHex)); + assert(hex.equals(expectedHex)); + } + + @Test public void testDecodeTicket() throws IllegalArgumentException, IllegalAccessException { + UicRailTicketData ticket = SimpleUicTestTicket.getUicTestTicket(); + byte[] encoded = UperEncoder.encode(ticket); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + String expectedHex = "78022020050DD3CF9F5CBCAA65E7D284EA40218A02D000822537B43701A237BB82B164CDA358088373CBBB4EFE40EDAF28EE4DEEAE004A03AD12B01416D08000000004039BDC195B951A58DAD95D125B999BC48088040EE0C2E6E6C2CECA021282DAE6E8CAE4C8C2DA0080BDA60100402C800131B20081013A65E7D00805881416D00B20283DA0"; + assertEquals(hex,expectedHex); + UicRailTicketData decodedTicket = UperEncoder.decode(encoded, UicRailTicketData.class); + assert(decodedTicket != null); + assertEquals(decodedTicket.controlDetail.ageCheckRequired , false ); + assertEquals(decodedTicket.controlDetail.getInfoText() , "cd" ); + assertEquals(decodedTicket.controlDetail.getIncludedTickets().get(0).getProductOwnerIA5() , "test" ); + assert(decodedTicket.controlDetail.getIdentificationByCardReference().get(0).getTrailingCardIdNum() == 100L ); + } + +} diff --git a/src/test/java/org/uic/barcode/ticket/api/test/UperEncodeUICTicketExtensionTest.java b/src/test/java/org/uic/barcode/ticket/api/test/UperEncodeUICTicketExtensionTest.java new file mode 100644 index 0000000..da59f67 --- /dev/null +++ b/src/test/java/org/uic/barcode/ticket/api/test/UperEncodeUICTicketExtensionTest.java @@ -0,0 +1,126 @@ +package org.uic.barcode.ticket.api.test; + +import static org.junit.Assert.assertEquals; + +import java.util.logging.Level; + +import org.junit.Test; +import org.uic.barcode.asn1.datatypes.Asn1Optional; +import org.uic.barcode.asn1.datatypes.Sequence; +import org.uic.barcode.asn1.uper.UperEncoder; +import org.uic.barcode.ticket.api.asn.omv1.ExtensionData; +import org.uic.barcode.ticket.api.asn.omv1.SequenceOfExtensionData; + + +public class UperEncodeUICTicketExtensionTest { + + /** + * Example from the Standard on UPER. + <pre> + World-Schema DEFINITIONS AUTOMATIC TAGS ::= + BEGIN + TestRecord ::= [APPLICATION 0] IMPLICIT SEQUENCE { + extensionList SEQUENCE OF ExtensionData OPTIONAL + } + ExtensionData ::= SEQUENCE { + extensionId IA5String, + extensionData OCTET STRING + } + END + + + value TestRecord ::= { extensionList { + { extensionId "1", extensionData '82DA'H } + ,{ extensionId "2", extensionData '83DA'H } + } + } + +Encoding to the file 'data.uper' using PER UNALIGNED encoding rule... +TestRecord SEQUENCE [fieldcount (not encoded) = 1] + extensionList SEQUENCE OF [count = 2] + ExtensionData SEQUENCE [fieldcount (not encoded) = 2] + extensionId IA5String [length = 1.0] + "1" + extensionData OCTET STRING [length = 2.0] + 0x82da + ExtensionData SEQUENCE [fieldcount (not encoded) = 2] + extensionId IA5String [length = 1.0] + "2" + extensionData OCTET STRING [length = 2.0] + 0x83da +Total encoded length = 10.7 +Encoded successfully in 11 bytes: +8100B102 82DA0164 0507B4 + </pre> + */ + + + @Sequence + public static class TestRecord { + + @Asn1Optional public SequenceOfExtensionData extension; + + public TestRecord() {} + public void setExtensionList(SequenceOfExtensionData sd){ + this.extension = sd; + } + } + + + @Test public void testEncodeTicket() throws IllegalArgumentException, IllegalAccessException { + TestRecord ticket = new TestRecord(); + populateTicket(ticket); + byte[] encoded = UperEncoder.encode(ticket); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + String expectedHex = "8100B10282DA01640507B4"; + UperEncoder.logger.debug(String.format("first diference at index: %d",hex.compareTo(expectedHex))); + assertEquals(hex,expectedHex); + } + + @Test public void testDecodeTicket() throws IllegalArgumentException, IllegalAccessException { + TestRecord ticket = new TestRecord(); + populateTicket(ticket); + byte[] encoded = UperEncoder.encode(ticket); + String hex = UperEncoder.hexStringFromBytes(encoded); + UperEncoder.logger.log(Level.FINEST,String.format("data hex: %s", hex)); + String expectedHex = "8100B10282DA01640507B4"; + TestRecord decodedTicket = UperEncoder.decode(encoded, TestRecord.class); + assert(decodedTicket != null); + assertEquals(hex,expectedHex); + } + + private void populateTicket(TestRecord ticket) { + + SequenceOfExtensionData ed = new SequenceOfExtensionData(); + populateExtensionSequence(ed); + ticket.setExtensionList(ed); + + } + + + + /* + ,extension { + { extensionId "1", extensionData '82DA'H } + ,{ extensionId "2", extensionData '83DA'H } + } + */ + private void populateExtensionSequence(SequenceOfExtensionData ed) { + ExtensionData ed1 = new ExtensionData(); + ed1.setExtensionId("1"); + byte[] ba1 = { (byte) 0x82, (byte) 0xDA }; + ed1.setExtensionData(ba1); + ExtensionData ed2 = new ExtensionData(); + ed2.setExtensionId("2"); + byte[] ba2 = { (byte) 0x83, (byte) 0xDA }; + ed2.setExtensionData(ba2); + ed.add(ed1); + ed.add(ed2); + } + + + + + +} diff --git a/src/test/java/org/uic/barcode/ticket/api/test/VoucherTimeZoneTestV1.java b/src/test/java/org/uic/barcode/ticket/api/test/VoucherTimeZoneTestV1.java new file mode 100644 index 0000000..423908b --- /dev/null +++ b/src/test/java/org/uic/barcode/ticket/api/test/VoucherTimeZoneTestV1.java @@ -0,0 +1,312 @@ +package org.uic.barcode.ticket.api.test;
+
+import java.io.IOException;
+import java.text.DateFormat;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.TimeZone;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.uic.barcode.asn1.uper.UperEncoder;
+import org.uic.barcode.ticket.EncodingFormatException;
+import org.uic.barcode.ticket.api.asn.omv1.UicRailTicketData;
+import org.uic.barcode.ticket.api.spec.IVoucher;
+import org.uic.barcode.ticket.api.spec.IUicRailTicket;
+import org.uic.barcode.ticket.api.test.testtickets.VoucherTestTicketV1;
+import org.uic.barcode.ticket.api.utils.Api2OpenAsnEncoder;
+import org.uic.barcode.ticket.api.utils.OpenAsn2ApiDecoder;
+
+
+/**
+ * The Class VoucherTestV1.
+ *
+ *
+ *
+ */
+public class VoucherTimeZoneTestV1 {
+
+ /** The low level encoded test ticket test case 1 . */
+ private byte[] encoded1 = null;
+
+ /** The low level encoded test ticket test case 2 . */
+ private byte[] encoded2 = null;
+
+ /** The low level encoded test ticket test case 3 . */
+ private byte[] encoded3 = null;
+
+ /** The decoder. */
+ OpenAsn2ApiDecoder decoder = new OpenAsn2ApiDecoder();
+
+ /** The encoder. */
+ Api2OpenAsnEncoder encoder = new Api2OpenAsnEncoder();
+
+ /** The API ticket low level encoded for case 1. */
+ IUicRailTicket iTicketDecodedFromAsn1Case1 = null;
+
+ /** The API ticket low level encoded for case 2. */
+ IUicRailTicket iTicketDecodedFromAsn1Case2 = null;
+
+ /** The API ticket low level encoded for case 3. */
+ IUicRailTicket iTicketDecodedFromAsn1Case3 = null;
+
+
+ /** The ticket decoded 1. */
+ IUicRailTicket iTicketDecodedCase1 = null;
+
+ /** The i ticket decoded 2. */
+ IUicRailTicket iTicketDecodedCase2 = null;
+
+ /** The i ticket decoded 3. */
+ IUicRailTicket iTicketDecodedCase3 = null;
+
+ byte[] encodedInTimeZone1 = null;
+ byte[] encodedInTimeZone2 = null;
+ byte[] encodedInTimeZone3 = null;
+
+ String issuingDate1 = null;
+ String issuingDate2 = null;
+ String issuingDate3 = null;
+
+ String validFromDate1 = null;
+ String validFromDate2 = null;
+ String validFromDate3 = null;
+
+ String validUntilDate1 = null;
+ String validUntilDate2 = null;
+ String validUntilDate3 = null;
+
+ TimeZone defaulttimeZone = null;
+
+ /**
+ * Prepare tickets.
+ */
+ @Before public void prepare() {
+
+ defaulttimeZone = TimeZone.getDefault();
+
+ UicRailTicketData ticket1 = VoucherTestTicketV1.getUicTestTicket();
+ UicRailTicketData ticket2 = VoucherTestTicketV1.getUicTestTicket();
+ UicRailTicketData ticket3 = VoucherTestTicketV1.getUicTestTicket();
+
+ //encode in UTC time zone
+ TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
+
+ encoded1 = UperEncoder.encode(ticket1);
+ encoded2 = UperEncoder.encode(ticket2);
+ encoded3 = UperEncoder.encode(ticket3);
+
+ }
+
+ /**
+ * clean up
+ */
+ @After public void resetTimeZone() {
+ TimeZone.setDefault(defaulttimeZone);
+ }
+
+
+ /**
+ * Test encode test tickets in UTC and decode in CET.
+ *
+ * @throws IllegalArgumentException the illegal argument exception
+ * @throws IllegalAccessException the illegal access exception
+ * @throws ParseException
+ */
+ @Test public void testCETCETencoding() throws IllegalArgumentException, IllegalAccessException, ParseException {
+
+ //get tickets
+ decodeTestTicketUTC();
+
+ //set the dates in the time zone CET
+ TimeZone.setDefault(TimeZone.getTimeZone("CET"));
+ iTicketDecodedFromAsn1Case1.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-12:30" ));
+ iTicketDecodedFromAsn1Case2.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-00:30" ));
+ iTicketDecodedFromAsn1Case3.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.03-23:30" ));
+
+ setValidFromDateTo("2021.03.14-00:00");
+ setValidUntilDateTo("2022.03.24-23:59");
+
+ encode("CET");
+ decode("CET");
+ decodedDateToStrings();
+
+ assert(issuingDate1.equals("Thu Mar 04 13:30:00 CET 2021" ));
+ assert(issuingDate2.equals("Thu Mar 04 01:30:00 CET 2021" ));
+ assert(issuingDate3.equals("Thu Mar 04 00:30:00 CET 2021" ));
+
+ assert(validFromDate1.equals("Sun Mar 14 00:00:00 CET 2021" ));
+ assert(validFromDate2.equals("Sun Mar 14 00:00:00 CET 2021" ));
+ assert(validFromDate3.equals("Sun Mar 14 00:00:00 CET 2021" ));
+
+ assert(validUntilDate1.equals("Thu Mar 24 23:59:00 CET 2022" ));
+ assert(validUntilDate2.equals("Thu Mar 24 23:59:00 CET 2022" ));
+ assert(validUntilDate3.equals("Thu Mar 24 23:59:00 CET 2022" ));
+
+
+ }
+
+
+ /**
+ * Test encode test tickets in UTC and decode in CET.
+ *
+ * @throws IllegalArgumentException the illegal argument exception
+ * @throws IllegalAccessException the illegal access exception
+ * @throws ParseException
+ */
+ @Test public void testGMTCETencoding() throws IllegalArgumentException, IllegalAccessException, ParseException {
+
+ //get tickets
+ decodeTestTicketUTC();
+
+ //set the dates in the time zone GMT
+ TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
+ iTicketDecodedFromAsn1Case1.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-12:30" ));
+ iTicketDecodedFromAsn1Case2.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-00:30" ));
+ iTicketDecodedFromAsn1Case3.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.03-23:30" ));
+
+ setValidFromDateTo("2021.03.14-00:00");
+ setValidUntilDateTo("2021.03.24-23:59");
+
+ encode("GMT");
+ decode("CET");
+ decodedDateToStrings();
+
+ assert(issuingDate1.equals("Thu Mar 04 13:30:00 CET 2021" ));
+ assert(issuingDate2.equals("Thu Mar 04 01:30:00 CET 2021" ));
+ assert(issuingDate3.equals("Thu Mar 04 00:30:00 CET 2021" ));
+
+ assert(validFromDate1.equals("Sun Mar 14 00:00:00 CET 2021" ));
+ assert(validFromDate2.equals("Sun Mar 14 00:00:00 CET 2021" ));
+ assert(validFromDate3.equals("Sun Mar 14 00:00:00 CET 2021" ));
+
+ assert(validUntilDate1.equals("Wed Mar 24 23:59:00 CET 2021" ));
+ assert(validUntilDate2.equals("Wed Mar 24 23:59:00 CET 2021" ));
+ assert(validUntilDate3.equals("Wed Mar 24 23:59:00 CET 2021" ));
+
+
+ }
+
+ /**
+ * Test encode test tickets in UTC and decode in CET.
+ *
+ * @throws IllegalArgumentException the illegal argument exception
+ * @throws IllegalAccessException the illegal access exception
+ * @throws ParseException
+ */
+ @Test public void testCETGMTencoding() throws IllegalArgumentException, IllegalAccessException, ParseException {
+
+ //get tickets
+ decodeTestTicketUTC();
+
+ //set the dates in the time zone GMT
+ TimeZone.setDefault(TimeZone.getTimeZone("CET"));
+ iTicketDecodedFromAsn1Case1.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-12:30" ));
+ iTicketDecodedFromAsn1Case2.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-00:30" ));
+ iTicketDecodedFromAsn1Case3.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.03-23:30" ));
+
+ setValidFromDateTo("2021.03.14-00:00");
+ setValidUntilDateTo("2022.03.24-23:59");
+
+ encode("CET");
+ decode("GMT");
+ decodedDateToStrings();
+
+ assert(issuingDate1.equals("Thu Mar 04 12:30:00 GMT 2021" ));
+ assert(issuingDate2.equals("Thu Mar 04 00:30:00 GMT 2021" ));
+ assert(issuingDate3.equals("Wed Mar 03 23:30:00 GMT 2021" ));
+
+ assert(validFromDate1.equals("Sun Mar 14 00:00:00 GMT 2021" ));
+ assert(validFromDate2.equals("Sun Mar 14 00:00:00 GMT 2021" ));
+ assert(validFromDate3.equals("Sun Mar 14 00:00:00 GMT 2021" ));
+
+ assert(validUntilDate1.equals("Thu Mar 24 23:59:00 GMT 2022" ));
+ assert(validUntilDate2.equals("Thu Mar 24 23:59:00 GMT 2022" ));
+ assert(validUntilDate3.equals("Thu Mar 24 23:59:00 GMT 2022" ));
+
+
+ }
+
+ private void decodedDateToStrings() {
+
+ issuingDate1 = iTicketDecodedCase1.getIssuerDetails().getIssuingDate().toString();
+ issuingDate2 = iTicketDecodedCase2.getIssuerDetails().getIssuingDate().toString();
+ issuingDate3 = iTicketDecodedCase3.getIssuerDetails().getIssuingDate().toString();
+
+ validFromDate1 = ((IVoucher) iTicketDecodedCase1.getDocumentData().iterator().next()).getValidFrom().toString();
+ validFromDate2 = ((IVoucher) iTicketDecodedCase2.getDocumentData().iterator().next()).getValidFrom().toString();
+ validFromDate3 = ((IVoucher) iTicketDecodedCase3.getDocumentData().iterator().next()).getValidFrom().toString();
+
+ validUntilDate1 = ((IVoucher) iTicketDecodedCase1.getDocumentData().iterator().next()).getValidUntil().toString();
+ validUntilDate2 = ((IVoucher) iTicketDecodedCase2.getDocumentData().iterator().next()).getValidUntil().toString();
+ validUntilDate3 = ((IVoucher) iTicketDecodedCase3.getDocumentData().iterator().next()).getValidUntil().toString();
+
+
+ }
+
+
+ private void setValidFromDateTo(String dateString) throws ParseException {
+ DateFormat dateFormat = new SimpleDateFormat( "yyyy.MM.dd-HH:mm" );
+ Date date = dateFormat.parse(dateString);
+ ((IVoucher) iTicketDecodedFromAsn1Case1.getDocumentData().iterator().next()).setValidFrom(date);
+ ((IVoucher) iTicketDecodedFromAsn1Case2.getDocumentData().iterator().next()).setValidFrom(date);
+ ((IVoucher) iTicketDecodedFromAsn1Case3.getDocumentData().iterator().next()).setValidFrom(date);
+ }
+
+ private void setValidUntilDateTo(String dateString) throws ParseException {
+ DateFormat dateFormat = new SimpleDateFormat( "yyyy.MM.dd-HH:mm" );
+ Date date = dateFormat.parse(dateString);
+ ((IVoucher) iTicketDecodedFromAsn1Case1.getDocumentData().iterator().next()).setValidUntil(date);
+ ((IVoucher) iTicketDecodedFromAsn1Case2.getDocumentData().iterator().next()).setValidUntil(date);
+ ((IVoucher) iTicketDecodedFromAsn1Case3.getDocumentData().iterator().next()).setValidUntil(date);
+ }
+
+
+ /**
+ * Encode in Time Zone
+ */
+ private void encode(String timeZone) {
+ TimeZone.setDefault(TimeZone.getTimeZone(timeZone));
+ try {
+ encodedInTimeZone1 = encoder.encode(iTicketDecodedFromAsn1Case1);
+ encodedInTimeZone2 = encoder.encode(iTicketDecodedFromAsn1Case2);
+ encodedInTimeZone3 = encoder.encode(iTicketDecodedFromAsn1Case3);
+ } catch (EncodingFormatException e) {
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * Decode in Time Zone
+ */
+ private void decode(String timeZone) {
+ TimeZone.setDefault(TimeZone.getTimeZone(timeZone));
+
+ try {
+ iTicketDecodedCase1 = decoder.decodeFromAsn(encodedInTimeZone1);
+ iTicketDecodedCase2 = decoder.decodeFromAsn(encodedInTimeZone2);
+ iTicketDecodedCase3 = decoder.decodeFromAsn(encodedInTimeZone3);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+
+ /**
+ * Encode decode.
+ */
+ private void decodeTestTicketUTC() {
+ TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
+ try {
+ iTicketDecodedFromAsn1Case1 = decoder.decodeFromAsn(encoded1);
+ iTicketDecodedFromAsn1Case2 = decoder.decodeFromAsn(encoded2);
+ iTicketDecodedFromAsn1Case3 = decoder.decodeFromAsn(encoded3);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ }
+
+}
diff --git a/src/test/java/org/uic/barcode/ticket/api/test/VoucherTimeZoneTestV2.java b/src/test/java/org/uic/barcode/ticket/api/test/VoucherTimeZoneTestV2.java new file mode 100644 index 0000000..53dc248 --- /dev/null +++ b/src/test/java/org/uic/barcode/ticket/api/test/VoucherTimeZoneTestV2.java @@ -0,0 +1,312 @@ +package org.uic.barcode.ticket.api.test;
+
+import java.io.IOException;
+import java.text.DateFormat;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.TimeZone;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.uic.barcode.asn1.uper.UperEncoder;
+import org.uic.barcode.ticket.EncodingFormatException;
+import org.uic.barcode.ticket.api.asn.omv1.UicRailTicketData;
+import org.uic.barcode.ticket.api.spec.IVoucher;
+import org.uic.barcode.ticket.api.spec.IUicRailTicket;
+import org.uic.barcode.ticket.api.test.testtickets.VoucherTestTicketV1;
+import org.uic.barcode.ticket.api.utils.Api2OpenAsnEncoder;
+import org.uic.barcode.ticket.api.utils.OpenAsn2ApiDecoder;
+
+
+/**
+ * The Class VoucherTestV1.
+ *
+ *
+ *
+ */
+public class VoucherTimeZoneTestV2 {
+
+ /** The low level encoded test ticket test case 1 . */
+ private byte[] encoded1 = null;
+
+ /** The low level encoded test ticket test case 2 . */
+ private byte[] encoded2 = null;
+
+ /** The low level encoded test ticket test case 3 . */
+ private byte[] encoded3 = null;
+
+ /** The decoder. */
+ OpenAsn2ApiDecoder decoder = new OpenAsn2ApiDecoder();
+
+ /** The encoder. */
+ Api2OpenAsnEncoder encoder = new Api2OpenAsnEncoder();
+
+ /** The API ticket low level encoded for case 1. */
+ IUicRailTicket iTicketDecodedFromAsn1Case1 = null;
+
+ /** The API ticket low level encoded for case 2. */
+ IUicRailTicket iTicketDecodedFromAsn1Case2 = null;
+
+ /** The API ticket low level encoded for case 3. */
+ IUicRailTicket iTicketDecodedFromAsn1Case3 = null;
+
+
+ /** The ticket decoded 1. */
+ IUicRailTicket iTicketDecodedCase1 = null;
+
+ /** The i ticket decoded 2. */
+ IUicRailTicket iTicketDecodedCase2 = null;
+
+ /** The i ticket decoded 3. */
+ IUicRailTicket iTicketDecodedCase3 = null;
+
+ byte[] encodedInTimeZone1 = null;
+ byte[] encodedInTimeZone2 = null;
+ byte[] encodedInTimeZone3 = null;
+
+ String issuingDate1 = null;
+ String issuingDate2 = null;
+ String issuingDate3 = null;
+
+ String validFromDate1 = null;
+ String validFromDate2 = null;
+ String validFromDate3 = null;
+
+ String validUntilDate1 = null;
+ String validUntilDate2 = null;
+ String validUntilDate3 = null;
+
+ TimeZone defaulttimeZone = null;
+
+ /**
+ * Prepare tickets.
+ */
+ @Before public void prepare() {
+
+ defaulttimeZone = TimeZone.getDefault();
+
+ UicRailTicketData ticket1 = VoucherTestTicketV1.getUicTestTicket();
+ UicRailTicketData ticket2 = VoucherTestTicketV1.getUicTestTicket();
+ UicRailTicketData ticket3 = VoucherTestTicketV1.getUicTestTicket();
+
+ //encode in UTC time zone
+ TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
+
+ encoded1 = UperEncoder.encode(ticket1);
+ encoded2 = UperEncoder.encode(ticket2);
+ encoded3 = UperEncoder.encode(ticket3);
+
+ }
+
+ /**
+ * clean up
+ */
+ @After public void resetTimeZone() {
+ TimeZone.setDefault(defaulttimeZone);
+ }
+
+
+ /**
+ * Test encode test tickets in UTC and decode in CET.
+ *
+ * @throws IllegalArgumentException the illegal argument exception
+ * @throws IllegalAccessException the illegal access exception
+ * @throws ParseException
+ */
+ @Test public void testCETCETencoding() throws IllegalArgumentException, IllegalAccessException, ParseException {
+
+ //get tickets
+ decodeTestTicketUTC();
+
+ //set the dates in the time zone CET
+ TimeZone.setDefault(TimeZone.getTimeZone("CET"));
+ iTicketDecodedFromAsn1Case1.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-12:30" ));
+ iTicketDecodedFromAsn1Case2.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-00:30" ));
+ iTicketDecodedFromAsn1Case3.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.03-23:30" ));
+
+ setValidFromDateTo("2021.03.14-00:00");
+ setValidUntilDateTo("2022.03.24-23:59");
+
+ encode("CET");
+ decode("CET");
+ decodedDateToStrings();
+
+ assert(issuingDate1.equals("Thu Mar 04 13:30:00 CET 2021" ));
+ assert(issuingDate2.equals("Thu Mar 04 01:30:00 CET 2021" ));
+ assert(issuingDate3.equals("Thu Mar 04 00:30:00 CET 2021" ));
+
+ assert(validFromDate1.equals("Sun Mar 14 00:00:00 CET 2021" ));
+ assert(validFromDate2.equals("Sun Mar 14 00:00:00 CET 2021" ));
+ assert(validFromDate3.equals("Sun Mar 14 00:00:00 CET 2021" ));
+
+ assert(validUntilDate1.equals("Thu Mar 24 23:59:00 CET 2022" ));
+ assert(validUntilDate2.equals("Thu Mar 24 23:59:00 CET 2022" ));
+ assert(validUntilDate3.equals("Thu Mar 24 23:59:00 CET 2022" ));
+
+
+ }
+
+
+ /**
+ * Test encode test tickets in UTC and decode in CET.
+ *
+ * @throws IllegalArgumentException the illegal argument exception
+ * @throws IllegalAccessException the illegal access exception
+ * @throws ParseException
+ */
+ @Test public void testGMTCETencoding() throws IllegalArgumentException, IllegalAccessException, ParseException {
+
+ //get tickets
+ decodeTestTicketUTC();
+
+ //set the dates in the time zone GMT
+ TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
+ iTicketDecodedFromAsn1Case1.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-12:30" ));
+ iTicketDecodedFromAsn1Case2.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-00:30" ));
+ iTicketDecodedFromAsn1Case3.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.03-23:30" ));
+
+ setValidFromDateTo("2021.03.14-00:00");
+ setValidUntilDateTo("2021.03.24-23:59");
+
+ encode("GMT");
+ decode("CET");
+ decodedDateToStrings();
+
+ assert(issuingDate1.equals("Thu Mar 04 13:30:00 CET 2021" ));
+ assert(issuingDate2.equals("Thu Mar 04 01:30:00 CET 2021" ));
+ assert(issuingDate3.equals("Thu Mar 04 00:30:00 CET 2021" ));
+
+ assert(validFromDate1.equals("Sun Mar 14 00:00:00 CET 2021" ));
+ assert(validFromDate2.equals("Sun Mar 14 00:00:00 CET 2021" ));
+ assert(validFromDate3.equals("Sun Mar 14 00:00:00 CET 2021" ));
+
+ assert(validUntilDate1.equals("Wed Mar 24 23:59:00 CET 2021" ));
+ assert(validUntilDate2.equals("Wed Mar 24 23:59:00 CET 2021" ));
+ assert(validUntilDate3.equals("Wed Mar 24 23:59:00 CET 2021" ));
+
+
+ }
+
+ /**
+ * Test encode test tickets in UTC and decode in CET.
+ *
+ * @throws IllegalArgumentException the illegal argument exception
+ * @throws IllegalAccessException the illegal access exception
+ * @throws ParseException
+ */
+ @Test public void testCETGMTencoding() throws IllegalArgumentException, IllegalAccessException, ParseException {
+
+ //get tickets
+ decodeTestTicketUTC();
+
+ //set the dates in the time zone GMT
+ TimeZone.setDefault(TimeZone.getTimeZone("CET"));
+ iTicketDecodedFromAsn1Case1.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-12:30" ));
+ iTicketDecodedFromAsn1Case2.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.04-00:30" ));
+ iTicketDecodedFromAsn1Case3.getIssuerDetails().setIssuingDate(new SimpleDateFormat( "yyyy.MM.dd-HH:mm" ).parse( "2021.03.03-23:30" ));
+
+ setValidFromDateTo("2021.03.14-00:00");
+ setValidUntilDateTo("2022.03.24-23:59");
+
+ encode("CET");
+ decode("GMT");
+ decodedDateToStrings();
+
+ assert(issuingDate1.equals("Thu Mar 04 12:30:00 GMT 2021" ));
+ assert(issuingDate2.equals("Thu Mar 04 00:30:00 GMT 2021" ));
+ assert(issuingDate3.equals("Wed Mar 03 23:30:00 GMT 2021" ));
+
+ assert(validFromDate1.equals("Sun Mar 14 00:00:00 GMT 2021" ));
+ assert(validFromDate2.equals("Sun Mar 14 00:00:00 GMT 2021" ));
+ assert(validFromDate3.equals("Sun Mar 14 00:00:00 GMT 2021" ));
+
+ assert(validUntilDate1.equals("Thu Mar 24 23:59:00 GMT 2022" ));
+ assert(validUntilDate2.equals("Thu Mar 24 23:59:00 GMT 2022" ));
+ assert(validUntilDate3.equals("Thu Mar 24 23:59:00 GMT 2022" ));
+
+
+ }
+
+ private void decodedDateToStrings() {
+
+ issuingDate1 = iTicketDecodedCase1.getIssuerDetails().getIssuingDate().toString();
+ issuingDate2 = iTicketDecodedCase2.getIssuerDetails().getIssuingDate().toString();
+ issuingDate3 = iTicketDecodedCase3.getIssuerDetails().getIssuingDate().toString();
+
+ validFromDate1 = ((IVoucher) iTicketDecodedCase1.getDocumentData().iterator().next()).getValidFrom().toString();
+ validFromDate2 = ((IVoucher) iTicketDecodedCase2.getDocumentData().iterator().next()).getValidFrom().toString();
+ validFromDate3 = ((IVoucher) iTicketDecodedCase3.getDocumentData().iterator().next()).getValidFrom().toString();
+
+ validUntilDate1 = ((IVoucher) iTicketDecodedCase1.getDocumentData().iterator().next()).getValidUntil().toString();
+ validUntilDate2 = ((IVoucher) iTicketDecodedCase2.getDocumentData().iterator().next()).getValidUntil().toString();
+ validUntilDate3 = ((IVoucher) iTicketDecodedCase3.getDocumentData().iterator().next()).getValidUntil().toString();
+
+
+ }
+
+
+ private void setValidFromDateTo(String dateString) throws ParseException {
+ DateFormat dateFormat = new SimpleDateFormat( "yyyy.MM.dd-HH:mm" );
+ Date date = dateFormat.parse(dateString);
+ ((IVoucher) iTicketDecodedFromAsn1Case1.getDocumentData().iterator().next()).setValidFrom(date);
+ ((IVoucher) iTicketDecodedFromAsn1Case2.getDocumentData().iterator().next()).setValidFrom(date);
+ ((IVoucher) iTicketDecodedFromAsn1Case3.getDocumentData().iterator().next()).setValidFrom(date);
+ }
+
+ private void setValidUntilDateTo(String dateString) throws ParseException {
+ DateFormat dateFormat = new SimpleDateFormat( "yyyy.MM.dd-HH:mm" );
+ Date date = dateFormat.parse(dateString);
+ ((IVoucher) iTicketDecodedFromAsn1Case1.getDocumentData().iterator().next()).setValidUntil(date);
+ ((IVoucher) iTicketDecodedFromAsn1Case2.getDocumentData().iterator().next()).setValidUntil(date);
+ ((IVoucher) iTicketDecodedFromAsn1Case3.getDocumentData().iterator().next()).setValidUntil(date);
+ }
+
+
+ /**
+ * Encode in Time Zone
+ */
+ private void encode(String timeZone) {
+ TimeZone.setDefault(TimeZone.getTimeZone(timeZone));
+ try {
+ encodedInTimeZone1 = encoder.encode(iTicketDecodedFromAsn1Case1);
+ encodedInTimeZone2 = encoder.encode(iTicketDecodedFromAsn1Case2);
+ encodedInTimeZone3 = encoder.encode(iTicketDecodedFromAsn1Case3);
+ } catch (EncodingFormatException e) {
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * Decode in Time Zone
+ */
+ private void decode(String timeZone) {
+ TimeZone.setDefault(TimeZone.getTimeZone(timeZone));
+
+ try {
+ iTicketDecodedCase1 = decoder.decodeFromAsn(encodedInTimeZone1);
+ iTicketDecodedCase2 = decoder.decodeFromAsn(encodedInTimeZone2);
+ iTicketDecodedCase3 = decoder.decodeFromAsn(encodedInTimeZone3);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+
+ /**
+ * Encode decode.
+ */
+ private void decodeTestTicketUTC() {
+ TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
+ try {
+ iTicketDecodedFromAsn1Case1 = decoder.decodeFromAsn(encoded1);
+ iTicketDecodedFromAsn1Case2 = decoder.decodeFromAsn(encoded2);
+ iTicketDecodedFromAsn1Case3 = decoder.decodeFromAsn(encoded3);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ }
+
+}
|