Ejemplo n.º 1
0
        public void TestIonWriterContractWriteValues()
        {
            IIonValue ionHashTests = loader.Load(file);

            byte[] expected = ExerciseWriter(IonReaderBuilder.Build(ionHashTests), false, (r, w) => { w.WriteValues(r); });
            byte[] actual   = ExerciseWriter(IonReaderBuilder.Build(ionHashTests), true, (r, w) => { w.WriteValues(r); });
            Assert.IsTrue(expected.Length > 1000);
            Assert.IsTrue(actual.Length > 1000);
            TestUtil.AssertEquals(expected, actual, "Byte arrays mismatch.");
        }
Ejemplo n.º 2
0
        public void TestSymbol()
        {
            var symbol = factory.NewSymbol("symbol");

            value.SetField("value", symbol);
            var reader = IonReaderBuilder.Build(value);

            jsonWriter.WriteValues(reader);
            Assert.AreEqual("{\"value\":\"symbol\"}", this.sw.ToString());
        }
Ejemplo n.º 3
0
        public void TestBigDecimal()
        {
            var bigDecimal = BigDecimal.Parse("123.456d101");

            value.SetField("value", factory.NewDecimal(bigDecimal));
            var reader = IonReaderBuilder.Build(value);

            jsonWriter.WriteValues(reader);
            Assert.AreEqual("{\"value\":123456e98}", this.sw.ToString());
        }
Ejemplo n.º 4
0
        public void TestTimestamp()
        {
            DateTime  time = new DateTime(2010, 6, 15, 3, 30, 45);
            Timestamp ts   = new Timestamp(time);

            value.SetField("value", factory.NewTimestamp(ts));
            var reader = IonReaderBuilder.Build(value);

            jsonWriter.WriteValues(reader);
            Assert.AreEqual("{\"value\":\"2010-06-15T03:30:45.0000000-00:00\"}", this.sw.ToString());
        }
Ejemplo n.º 5
0
        public void TestAnnotation()
        {
            var ionInt = factory.NewInt(123);

            ionInt.AddTypeAnnotation("annotation");
            value.SetField("value", ionInt);
            var reader = IonReaderBuilder.Build(value);

            jsonWriter.WriteValues(reader);
            // They should be suppressed
            Assert.AreEqual("{\"value\":123}", this.sw.ToString());
        }
Ejemplo n.º 6
0
        public void TestList()
        {
            IIonValue list = factory.NewEmptyList();

            list.Add(factory.NewString("Ion"));
            list.Add(factory.NewInt(123));
            value.SetField("value", list);
            var reader = IonReaderBuilder.Build(value);

            jsonWriter.WriteValues(reader);
            Assert.AreEqual("{\"value\":[\"Ion\",123]}", this.sw.ToString());
        }
Ejemplo n.º 7
0
        public void TestStruct()
        {
            IIonValue nestedStruct = factory.NewEmptyStruct();

            nestedStruct.SetField("nestedString", factory.NewString("Ion"));
            nestedStruct.SetField("nestedInt", factory.NewInt(123));
            value.SetField("value", nestedStruct);
            var reader = IonReaderBuilder.Build(value);

            jsonWriter.WriteValues(reader);
            Assert.AreEqual("{\"value\":{\"nestedString\":\"Ion\",\"nestedInt\":123}}", this.sw.ToString());
        }
Ejemplo n.º 8
0
        public void TestEmptyString()
        {
            IIonHashReader ihr = IonHashReaderBuilder
                                 .Standard()
                                 .WithHasherProvider(new IdentityIonHasherProvider())
                                 .WithReader(IonReaderBuilder.Build(""))
                                 .Build();

            Assert.AreEqual(IonType.None, ihr.MoveNext());
            TestUtil.AssertEquals(new byte[] { }, ihr.Digest(), "Digests don't match.");
            Assert.AreEqual(IonType.None, ihr.MoveNext());
            TestUtil.AssertEquals(new byte[] { }, ihr.Digest(), "Digests don't match.");
        }
        public void Execute_InsertDocumentWithMultipleFields_DocumentIsInserted()
        {
            // Given.
            // Create Ion struct to insert.
            IIonValue ionStruct = ValueFactory.NewEmptyStruct();

            ionStruct.SetField(Constants.ColumnName, ValueFactory.NewString(Constants.SingleDocumentValue));
            ionStruct.SetField(Constants.SecondColumnName, ValueFactory.NewString(Constants.SingleDocumentValue));

            // When.
            var query = $"INSERT  INTO {Constants.TableName} ?";
            var count = qldbDriver.Execute(txn =>
            {
                var result = txn.Execute(query, ionStruct);

                var count = 0;
                foreach (var row in result)
                {
                    count++;
                }
                return(count);
            });

            Assert.AreEqual(1, count);

            // Then.
            var       searchQuery = $@"SELECT {Constants.ColumnName}, {Constants.SecondColumnName} FROM {Constants.TableName} 
                               WHERE {Constants.ColumnName} = '{Constants.SingleDocumentValue}' AND  {Constants.SecondColumnName} = '{Constants.SingleDocumentValue}'";
            IIonValue value       = qldbDriver.Execute(txn =>
            {
                var result = txn.Execute(searchQuery);

                IIonValue value = null;
                foreach (var row in result)
                {
                    value = row;
                }
                return(value);
            });

            var ionReader = IonReaderBuilder.Build(value);

            ionReader.MoveNext();
            ionReader.StepIn();
            ionReader.MoveNext();
            Assert.AreEqual(Constants.ColumnName, ionReader.CurrentFieldName);
            Assert.AreEqual(Constants.SingleDocumentValue, ionReader.StringValue());
            ionReader.MoveNext();
            Assert.AreEqual(Constants.SecondColumnName, ionReader.CurrentFieldName);
            Assert.AreEqual(Constants.SingleDocumentValue, ionReader.StringValue());
        }
Ejemplo n.º 10
0
        /// <summary>
        /// The QldbHash of an IonValue is just the IonHash of that value.
        /// </summary>
        ///
        /// <param name="value">Ion Value to be hashed.</param>
        ///
        /// <returns>Hashed result.</returns>
        internal static QldbHash ToQldbHash(IIonValue value)
        {
            IIonReader     reader     = IonReaderBuilder.Build(value);
            IIonHashReader hashReader = IonHashReaderBuilder.Standard()
                                        .WithHasherProvider(HasherProvider)
                                        .WithReader(reader)
                                        .Build();

            while (hashReader.MoveNext() != IonType.None)
            {
            }

            return(new QldbHash(hashReader.Digest()));
        }
Ejemplo n.º 11
0
        public void DeserializeMultipleAnnotatedIonToClassNameWithSecondAnnotated()
        {
            // Multiple annotations are ignored, it will only pick the first one.
            IIonValue ionTruck = valueFactory.NewEmptyStruct();

            ionTruck.AddTypeAnnotation("FirstAnnotation");
            ionTruck.AddTypeAnnotation("Truck");

            IIonReader reader = IonReaderBuilder.Build(ionTruck);

            Vehicle truck = defaultSerializer.Deserialize <Vehicle>(reader);

            Assert.AreNotEqual(TestObjects.nativeTruck.ToString(), truck.ToString());
        }
Ejemplo n.º 12
0
        public void DeserializeMultipleAnnotatedIonToClassNameWithFirstAnnotated()
        {
            // Multiple annotations are ignored, it will only pick the first one.
            IIonValue ionTruck = valueFactory.NewEmptyStruct();

            ionTruck.AddTypeAnnotation("Amazon.IonObjectMapper.Test.Truck");
            ionTruck.AddTypeAnnotation("SecondAnnotation");

            IIonReader reader = IonReaderBuilder.Build(ionTruck);

            Vehicle truck = defaultSerializer.Deserialize <Vehicle>(reader);

            AssertIsTruck(truck);
        }
Ejemplo n.º 13
0
        public void TestIonReaderContract()
        {
            FileInfo  file         = DirStructure.IonHashDotnetTestFile("ion_hash_tests.ion");
            IIonValue ionHashTests = loader.Load(file);

            IIonReader ir = IonReaderBuilder.Build(ionHashTests);

            IIonHashReader ihr = IonHashReaderBuilder
                                 .Standard()
                                 .WithHasherProvider(new IdentityIonHasherProvider())
                                 .WithReader(IonReaderBuilder.Build(ionHashTests))
                                 .Build();

            ReaderCompare.Compare(ir, ihr);
        }
Ejemplo n.º 14
0
        public void TestUnresolvedSid()
        {
            // unresolved SIDs (such as SID 10 here) should result in an exception
            IIonValue ionContainer = loader.Load("(0xd3 0x8a 0x21 0x01)");

            byte[] ionBinary = IonHashTest.ContainerToBytes(ionContainer.GetElementAt(0));

            IIonHashReader ihr = IonHashReaderBuilder
                                 .Standard()
                                 .WithHasherProvider(new IdentityIonHasherProvider())
                                 .WithReader(IonReaderBuilder.Build(ionBinary))
                                 .Build();

            ihr.MoveNext();
            Assert.ThrowsException <UnknownSymbolException>(() => ihr.MoveNext());
        }
Ejemplo n.º 15
0
        public void Test(TestValue tv, string s)
        {
            IIonHashWriter hashWriter = null;

            try
            {
                hashWriter = IonHashWriterBuilder.Standard()
                             .WithWriter(IonTextWriterBuilder.Build(new StringWriter()))
                             .WithHasherProvider(HasherProvider)
                             .Build();
                hashWriter.WriteValues(IonReaderBuilder.Build(s));
            }
            catch (IonException e)
            {
                if (tv.IsValidIon())
                {
                    throw e;
                }
            }

            IIonHashReader hashReader = null;

            try
            {
                hashReader = IonHashReaderBuilder.Standard()
                             .WithReader(IonReaderBuilder.Build(s))
                             .WithHasherProvider(HasherProvider)
                             .Build();
                hashReader.MoveNext();
                hashReader.MoveNext();
            }
            catch (IonException e)
            {
                if (tv.IsValidIon())
                {
                    throw e;
                }
            }

            if (tv.validIon == null || tv.validIon.Value == true)
            {
                TestUtil.AssertEquals(
                    hashWriter.Digest(),
                    hashReader.Digest(),
                    "Reader/writer hashes for line |" + tv.AsIon() + "| as |" + s + "| don't match");
            }
        }
Ejemplo n.º 16
0
        public void Blob_PartialRead(int size, int step)
        {
            var blob = new byte[size];
            for (var i = 0; i < size; i++)
            {
                blob[i] = (byte)i;
            }

            var memStream = new MemoryStream();
            using (var writer = IonBinaryWriterBuilder.Build(memStream))
            {
                writer.WriteBlob(blob);
                writer.Finish();
            }
            var output = memStream.ToArray();
            var reader = IonReaderBuilder.Build(new MemoryStream(output));
            ReaderTestCommon.Blob_PartialRead(size, step, reader);
        }
Ejemplo n.º 17
0
        private void AssertNoFieldnameInCurrentHash(string value, byte[] expectedBytes)
        {
            var reader = IonReaderBuilder.Build(value);

            reader.MoveNext();

            MemoryStream memoryStream = new MemoryStream();
            var          writer       = IonBinaryWriterBuilder.Build(memoryStream);

            writer.StepIn(IonType.Struct);

            IIonHashWriter ihw = IonHashWriterBuilder
                                 .Standard()
                                 .WithHasherProvider(new IdentityIonHasherProvider())
                                 .WithWriter(writer)
                                 .Build();

            ihw.SetFieldName("field_name");
            ihw.WriteValue(reader);
            byte[] actual = ihw.Digest();
            TestUtil.AssertEquals(expectedBytes, actual, "Digest arrays mismatch");
            writer.StepOut();

            ihw.Finish();
            writer.Finish();
            memoryStream.Flush();
            byte[] bytes = memoryStream.ToArray();
            memoryStream.Close();

            reader = IonReaderBuilder.Build(bytes);
            reader.MoveNext();
            reader.StepIn();
            IIonHashReader ihr = IonHashReaderBuilder
                                 .Standard()
                                 .WithHasherProvider(new IdentityIonHasherProvider())
                                 .WithReader(reader)
                                 .Build();

            ihr.MoveNext();   //List
            ihr.MoveNext();   //none
            actual = ihr.Digest();
            TestUtil.AssertEquals(expectedBytes, actual, "Digest arrays mismatch");
        }
        private List <List <KeyValuePair <string, string> > > BuildData(IResult result)
        {
            List <List <KeyValuePair <string, string> > > keyValuePairListList = new List <List <KeyValuePair <string, string> > >();
            IEnumerator <IIonValue> enumerator = result.GetEnumerator();

            while (enumerator.MoveNext())
            {
                List <KeyValuePair <string, string> > document = new List <KeyValuePair <string, string> >();
                using (IIonReader reader = IonReaderBuilder.Build(enumerator.Current, new ReaderOptions()))
                {
                    IonType type;
                    while ((type = reader.MoveNext()) != IonType.None)
                    {
                        ConnectionService.buildData1(document, reader, type);
                    }
                }
                keyValuePairListList.Add(document);
            }
            return(keyValuePairListList);
        }
Ejemplo n.º 19
0
        private void LargeIntTest(string path, string expectedStr)
        {
            var expected = BigInteger.Parse(expectedStr);
            var bytes    = DirStructure.IonTestFileAsBytes(path);
            var reader   = IonReaderBuilder.Build(bytes);

            try
            {
                reader.MoveNext();
                reader.StepIn();
                reader.MoveNext();
                Assert.AreEqual(expected, reader.BigIntegerValue());
                reader.MoveNext();
                Assert.AreEqual(expected, reader.BigIntegerValue());
                reader.StepOut();
            }
            finally
            {
                reader.Dispose();
            }
        }
Ejemplo n.º 20
0
        public void FieldNameAsymmetry()
        {
            var memoryStream = new MemoryStream();
            var writer       = IonBinaryWriterBuilder.Build(memoryStream);

            IIonHashWriter ihw = IonHashWriterBuilder
                                 .Standard()
                                 .WithHasherProvider(new IdentityIonHasherProvider())
                                 .WithWriter(writer)
                                 .Build();

            // A nested struct: {a:{b:1}}
            writer.StepIn(IonType.Struct);
            writer.SetFieldName("a");
            ihw.StepIn(IonType.Struct);
            ihw.SetFieldName("b");
            ihw.WriteInt(1);
            ihw.StepOut();
            byte[] writeHash = ihw.Digest();
            ihw.Flush();
            writer.StepOut();
            writer.Flush();

            var loader   = IonLoader.Default;
            var ionValue = loader.Load(memoryStream.ToArray())  //Datagram
                           .GetElementAt(0)                     //first struct
                           .GetField("a");                      //inner struct

            IIonReader     reader = IonReaderBuilder.Build(ionValue);
            IIonHashReader ihr    = IonHashReaderBuilder
                                    .Standard()
                                    .WithReader(reader)
                                    .WithHasherProvider(new IdentityIonHasherProvider())
                                    .Build();

            ihr.MoveNext();  // struct
            ihr.MoveNext();  // none

            TestUtil.AssertEquals(writeHash, ihr.Digest(), "Digest arrays mismatch");
        }
Ejemplo n.º 21
0
        /// <summary>
        /// Apply the same writing logic to binary and text writers and assert the accuracy
        /// </summary>
        protected void AssertReaderWriter(Action <IIonReader> assertReader, Action <IIonWriter> writerFunc)
        {
            //bin
            using (var s = new MemoryStream())
            {
                var binWriter = IonBinaryWriterBuilder.Build(s);
                writerFunc(binWriter);
                s.Seek(0, SeekOrigin.Begin);
                var binReader = IonReaderBuilder.Build(s);
                assertReader(binReader);
            }

            //text
            var sw         = new StringWriter();
            var textWriter = IonTextWriterBuilder.Build(sw);

            writerFunc(textWriter);
            var str        = sw.ToString();
            var textReader = IonReaderBuilder.Build(str);

            assertReader(textReader);
        }
Ejemplo n.º 22
0
        public void TestTopLevelValues()
        {
            IIonHashReader ihr = IonHashReaderBuilder
                                 .Standard()
                                 .WithHasherProvider(new IdentityIonHasherProvider())
                                 .WithReader(IonReaderBuilder.Build("1 2 3"))
                                 .Build();

            Assert.AreEqual(IonType.Int, ihr.MoveNext());
            TestUtil.AssertEquals(new byte[] { }, ihr.Digest(), "Digests don't match.");

            Assert.AreEqual(IonType.Int, ihr.MoveNext());
            TestUtil.AssertEquals(new byte[] { 0x0b, 0x20, 0x01, 0x0e }, ihr.Digest(), "Digests don't match.");

            Assert.AreEqual(IonType.Int, ihr.MoveNext());
            TestUtil.AssertEquals(new byte[] { 0x0b, 0x20, 0x02, 0x0e }, ihr.Digest(), "Digests don't match.");

            Assert.AreEqual(IonType.None, ihr.MoveNext());
            TestUtil.AssertEquals(new byte[] { 0x0b, 0x20, 0x03, 0x0e }, ihr.Digest(), "Digests don't match.");

            Assert.AreEqual(IonType.None, ihr.MoveNext());
            TestUtil.AssertEquals(new byte[] { }, ihr.Digest(), "Digests don't match.");
        }
Ejemplo n.º 23
0
        private void Consume(Action <IIonHashReader> lambda)
        {
            IIonHashReader ihr = IonHashReaderBuilder
                                 .Standard()
                                 .WithHasherProvider(new IdentityIonHasherProvider())
                                 .WithReader(IonReaderBuilder.Build("[1,2,{a:3,b:4},5]"))
                                 .Build();

            TestUtil.AssertEquals(new byte[] { }, ihr.Digest(), "Digests don't match.");
            lambda(ihr);
            TestUtil.AssertEquals(new byte[] { 0x0b, 0xb0,
                                               0x0b, 0x20, 0x01, 0x0e,
                                               0x0b, 0x20, 0x02, 0x0e,
                                               0x0b, 0xd0,
                                               0x0c, 0x0b, 0x70, 0x61, 0x0c, 0x0e, 0x0c, 0x0b, 0x20, 0x03, 0x0c, 0x0e,
                                               0x0c, 0x0b, 0x70, 0x62, 0x0c, 0x0e, 0x0c, 0x0b, 0x20, 0x04, 0x0c, 0x0e,
                                               0x0e,
                                               0x0b, 0x20, 0x05, 0x0e,
                                               0x0e }, ihr.Digest(), "Digests don't match.");

            Assert.AreEqual(IonType.None, ihr.MoveNext());
            TestUtil.AssertEquals(new byte[] { }, ihr.Digest(), "Digests don't match.");
        }
Ejemplo n.º 24
0
        public void MultipleClobs()
        {
            IIonReader binReader;

            using (var ms = new MemoryStream())
            {
                ReadOnlySpan <byte> firstClob  = new ReadOnlySpan <byte>(new byte[] { 97, 98, 99, 32, 100, 101 });
                ReadOnlySpan <byte> secondClob = new ReadOnlySpan <byte>(new byte[] { 65, 32, 66, 32, 67 });
                ReadOnlySpan <byte> thirdClob  = new ReadOnlySpan <byte>(new byte[] { 50, 51, 32, 54, 55 });

                var binWriter = IonBinaryWriterBuilder.Build(ms);
                binWriter.WriteClob(firstClob);  // {{ "abc de" }}
                binWriter.WriteClob(secondClob); // {{ "A B\tC" }}
                binWriter.WriteClob(thirdClob);  // {{ "23 56" }}
                binWriter.Finish();

                ms.Seek(0, SeekOrigin.Begin);
                binReader = IonReaderBuilder.Build(ms);


                var type   = binReader.MoveNext();
                var buffer = binReader.NewByteArray();
                Assert.AreEqual(IonType.Clob, type);
                Assert.AreEqual("abc de", Encoding.ASCII.GetString(buffer));

                type   = binReader.MoveNext();
                buffer = binReader.NewByteArray();
                Assert.AreEqual(IonType.Clob, type);
                Assert.AreEqual("A B C", Encoding.ASCII.GetString(buffer));

                type   = binReader.MoveNext();
                buffer = binReader.NewByteArray();
                Assert.AreEqual(IonType.Clob, type);
                Assert.AreEqual("23 67", Encoding.ASCII.GetString(buffer));
            }
        }
        public void Scratch()
        {
            string ionText = "{iRoNiCcAsE: 3}";

            IonSerializer   ionSerializer;
            IIonReader      reader;
            NormalCaseClass result;

            ionSerializer = new IonSerializer(new IonSerializationOptions {
                PropertyNameCaseInsensitive = false
            });
            reader = IonReaderBuilder.Build(ionText);
            result = ionSerializer.Deserialize <NormalCaseClass>(reader);
            Console.WriteLine(result.ironicCase == null); // could not deserialize differently-cased text
            // True

            ionSerializer = new IonSerializer(new IonSerializationOptions {
                PropertyNameCaseInsensitive = true
            });
            reader = IonReaderBuilder.Build(ionText);
            result = ionSerializer.Deserialize <NormalCaseClass>(reader);
            Console.WriteLine(result.ironicCase); // successfully deserializes differently-cased text
            // 3
        }
Ejemplo n.º 26
0
        public void MultipleBlobs()
        {
            IIonReader binReader;

            using (var ms = new MemoryStream())
            {
                ReadOnlySpan <byte> firstBlob  = new ReadOnlySpan <byte>(new byte[] { 68, 65, 72, 72, 75 });
                ReadOnlySpan <byte> secondBlob = new ReadOnlySpan <byte>(new byte[] { 68, 69 });
                ReadOnlySpan <byte> thirdBlob  = new ReadOnlySpan <byte>(new byte[] { 68, 65, 85 });

                var binWriter = IonBinaryWriterBuilder.Build(ms);
                binWriter.WriteBlob(firstBlob);
                binWriter.WriteBlob(secondBlob);
                binWriter.WriteBlob(thirdBlob);
                binWriter.Finish();

                ms.Seek(0, SeekOrigin.Begin);
                binReader = IonReaderBuilder.Build(ms);


                var type   = binReader.MoveNext();
                var buffer = binReader.NewByteArray();
                Assert.AreEqual(IonType.Blob, type);
                Assert.AreEqual("68 65 72 72 75 ", BytesToHex(buffer));

                type   = binReader.MoveNext();
                buffer = binReader.NewByteArray();
                Assert.AreEqual(IonType.Blob, type);
                Assert.AreEqual("68 69 ", BytesToHex(buffer));

                type   = binReader.MoveNext();
                buffer = binReader.NewByteArray();
                Assert.AreEqual(IonType.Blob, type);
                Assert.AreEqual("68 65 85 ", BytesToHex(buffer));
            }
        }
Ejemplo n.º 27
0
 internal virtual IIonReader GetIonReader(byte[] ionBinary)
 {
     return(IonReaderBuilder.Build(ionBinary));
 }
Ejemplo n.º 28
0
 internal virtual IIonReader GetIonReader(IIonValue ionValue)
 {
     return(IonReaderBuilder.Build(ionValue));
 }
Ejemplo n.º 29
0
 internal override IIonReader GetIonReader(IIonValue ionValue)
 {
     return(IonReaderBuilder.Build(ionValue.ToPrettyString()));
 }
Ejemplo n.º 30
0
        public void SerializesAndDeserializePropertiesWhenConflictingFieldNamesArePresent()
        {
            var stream = defaultSerializer.Serialize(new PropertyNameConflictsWithIonPropertyField
            {
                firstString          = "ignored",
                SomeOtherFirstString = "expected"
            });

            var serialized = StreamToIonValue(stream);

            Assert.IsFalse(serialized.ContainsField("SomeOtherFirstString"));
            Assert.IsTrue(serialized.ContainsField("firstString"));
            Assert.AreEqual(serialized.GetField("firstString").StringValue, "expected");

            var deserialized = defaultSerializer.Deserialize <PropertyNameConflictsWithIonPropertyField>(IonReaderBuilder.Build(serialized));

            Assert.AreEqual(deserialized.SomeOtherFirstString, "expected");
        }