Example #1
0
        public void Should_Convert_To_PascalCase_Json_With_Altered_Serialization_Settings()
        {
            var transcoder = new LegacyTranscoder(
                new DefaultSerializer(
                    new JsonSerializerSettings(),
                    new JsonSerializerSettings
            {
                ContractResolver = new DefaultContractResolver()
            }));

            var data = new Pascal
            {
                SomeProperty    = "SOME",
                SomeIntProperty = 12345,
                HasPascalCase   = true
            };
            var expectedJsonBytes = Encoding.UTF8.GetBytes("{\"SomeProperty\":\"SOME\",\"SomeIntProperty\":12345,\"HasPascalCase\":true}");

            using (var jsonBytes = new MemoryStream())
                using (var jsonEncoded = new MemoryStream())
                {
                    transcoder.SerializeAsJson(jsonBytes, data);
                    transcoder.Encode(jsonEncoded, data, TypeCode.Object, OpCode.Get);

                    Assert.Equal(expectedJsonBytes, jsonBytes.ToArray());
                    Assert.Equal(expectedJsonBytes, jsonEncoded.ToArray());
                }
        }
Example #2
0
        public void Should_Hydrate_Poco_In_PascalCase_Whatever_The_Case_In_Json()
        {
            byte[] jsonData   = Encoding.UTF8.GetBytes("{ \"SomeProperty\": \"SOME\", \"someIntProperty\": 12345, \"haspAscalCASE\": true }");
            var    transcoder = new LegacyTranscoder();
            var    hydrated   = transcoder.DeserializeAsJson <Pascal>(jsonData.AsMemory());

            Assert.Equal("SOME", hydrated.SomeProperty);
            Assert.Equal(12345, hydrated.SomeIntProperty);
            Assert.True(hydrated.HasPascalCase);
        }
Example #3
0
        public void ScramShaMechanism_ClientNonce_IsPopulated()
        {
            var transcoder = new LegacyTranscoder();
            var password   = "******";
            var username   = "******";

            var mech = new ScramShaMechanism(transcoder, MechanismType.ScramSha256, username, password,
                                             new Mock <ILogger <ScramShaMechanism> >().Object, NullRequestTracer.Instance);

            Assert.True(!string.IsNullOrEmpty(mech.ClientNonce));
        }
Example #4
0
        public void ScramShaMechanism_WhenUsernameIsInvalidinCtor_ThrowArgumentNullException(string username)
        {
            var    transcoder = new LegacyTranscoder();
            string password   = null;

            // ReSharper disable once ExpressionIsAlwaysNull
            // ReSharper disable once ObjectCreationAsStatement
            // ReSharper disable once ExpressionIsAlwaysNull
            Assert.Throws <ArgumentNullException>(() =>
                                                  new ScramShaMechanism(transcoder, MechanismType.ScramSha256, username, password,
                                                                        new Mock <ILogger <ScramShaMechanism> >().Object, NullRequestTracer.Instance));
        }
Example #5
0
        public void Test_Json_Deserialize_Int()
        {
            var transcoder = new LegacyTranscoder();
            int value      = 42;

            using (var stream = new MemoryStream())
            {
                transcoder.SerializeAsJson(stream, value);
                var actual = transcoder.DeserializeAsJson <int>(stream.ToArray());

                Assert.Equal(value, actual);
            }
        }
Example #6
0
        public void DecodeString_Returns_String_When_Buffer_Is_Empty_And_Type_Is_String()
        {
            var transcoder = new LegacyTranscoder(new DefaultSerializer());

            var bytes  = new byte[0];
            var result = transcoder.Decode <string>(bytes.AsMemory(),
                                                    new Flags {
                DataFormat = DataFormat.String
            },
                                                    OpCode.NoOp);

            Assert.Null(result);
        }
        public void When_ByteArray_Is_Stored_With_Legacy_Flags_It_Is_Decoded_As_A_ByteArray()
        {
            var legacyByteArray = new byte[]
            {
                129, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 8, 0,
                0, 0, 5, 19, 185, 8, 248, 3, 104, 208, 188, 0,
                0, 250, 82, 116, 101, 115, 116
            };

            var flags = Flags.Read(legacyByteArray.AsSpan(24));

            var transcoder = new LegacyTranscoder();
            var result     = transcoder.Decode <byte[]>(legacyByteArray.AsMemory(28, 4), flags, OpCode.Get);

            Assert.Equal("test", Encoding.UTF8.GetString(result));
        }
Example #8
0
        public void Test_Deserialize_Char()
        {
            var transcoder = new LegacyTranscoder();
            var value      = 'o';

            var flags = new Flags
            {
                Compression = Compression.None,
                DataFormat  = DataFormat.Reserved,
                TypeCode    = Convert.GetTypeCode(value)
            };

            var bytes  = Encoding.UTF8.GetBytes(value.ToString());
            var actual = transcoder.Decode <char>(bytes.AsMemory(), flags, OpCode.Get);

            Assert.Equal(value, actual);
        }
Example #9
0
        public void Test_Deserialize_String()
        {
            var transcoder = new LegacyTranscoder();
            var value      = "astring";

            var flags = new Flags
            {
                Compression = Compression.None,
                DataFormat  = DataFormat.Reserved,
                TypeCode    = Convert.GetTypeCode(value)
            };

            using (var stream = new MemoryStream())
            {
                transcoder.Encode(stream, value, flags, OpCode.Get);
                var actual = transcoder.Decode <string>(stream.ToArray(), flags, OpCode.Get);
                Assert.Equal(value, actual);
            }
        }
        public void Test_Deserialize_Int()
        {
            var transcoder = new LegacyTranscoder();
            var five       = 5;

            var flags = new Flags
            {
                Compression = Couchbase.Core.IO.Operations.Compression.None,
                DataFormat  = DataFormat.Reserved,
                TypeCode    = Convert.GetTypeCode(five)
            };

            using (var stream = new MemoryStream())
            {
                transcoder.Encode(stream, five, flags, OpCode.Get);
                var actual = transcoder.Decode <int>(stream.ToArray(), flags, OpCode.Get);
                Assert.Equal(five, actual);
            }
        }
Example #11
0
        public void Test_Null()
        {
            var transcoder = new LegacyTranscoder();

            var flags = new Flags
            {
                Compression = Compression.None,
                DataFormat  = DataFormat.Reserved,
                TypeCode    = TypeCode.Empty
            };

            var expected = new byte[0];

            using (var stream = new MemoryStream())
            {
                transcoder.Encode <string>(stream, null, flags, OpCode.Get);

                Assert.Equal(expected, stream.ToArray());
            }
        }
Example #12
0
        public void Test_Deserialize_Null()
        {
            var    transcoder = new LegacyTranscoder();
            object value      = null;

            var flags = new Flags
            {
                Compression = Compression.None,
                DataFormat  = DataFormat.Reserved,
                TypeCode    = TypeCode.Empty
            };

            // ReSharper disable once ExpressionIsAlwaysNull
            using (var stream = new MemoryStream())
            {
                transcoder.SerializeAsJson(stream, value);
                var actual = transcoder.Decode <object>(stream.ToArray(), flags, OpCode.Get);
                Assert.Equal(value, actual);
            }
        }
Example #13
0
        public void Test_Byte_Array()
        {
            var transcoder = new LegacyTranscoder();
            var value      = new byte[] { 0x00, 0x00, 0x01 };

            var flags = new Flags
            {
                Compression = Compression.None,
                DataFormat  = DataFormat.Binary,
                TypeCode    = Type.GetTypeCode(typeof(byte[]))
            };

            using (var stream = new MemoryStream())
            {
                transcoder.Encode(stream, value, flags, OpCode.Get);
                Assert.Equal(value, stream.ToArray());

                var actual = transcoder.Decode <byte[]>(stream.ToArray(), flags, OpCode.Get);
                Assert.Equal(value, actual);
            }
        }
Example #14
0
        public void Test_Char()
        {
            var transcoder = new LegacyTranscoder();
            var value      = 'o';

            var flags = new Flags
            {
                Compression = Compression.None,
                DataFormat  = DataFormat.Reserved,
                TypeCode    = Convert.GetTypeCode(value)
            };

            var expected = new byte[] { 0x6f };

            using (var stream = new MemoryStream())
            {
                transcoder.Encode(stream, value, flags, OpCode.Get);

                Assert.Equal(expected, stream.ToArray());
            }
        }
Example #15
0
        public void Test_Serialize_String()
        {
            var    transcoder = new LegacyTranscoder();
            string data       = "Hello";

            var flags = new Flags
            {
                Compression = Compression.None,
                DataFormat  = DataFormat.Reserved,
                TypeCode    = Convert.GetTypeCode(data)
            };

            var expected = new byte[] { 0x48, 0x65, 0x6c, 0x6c, 0x6f };

            using (var stream = new MemoryStream())
            {
                transcoder.Encode(stream, data, flags, OpCode.Get);

                Assert.Equal(expected, stream.ToArray());
            }
        }
Example #16
0
        public void Test_Serialize_UInt16()
        {
            var    transcoder = new LegacyTranscoder(new DefaultSerializer());
            UInt16 data       = 5;

            var flags = new Flags
            {
                Compression = Compression.None,
                DataFormat  = DataFormat.Reserved,
                TypeCode    = Convert.GetTypeCode(data)
            };

            var expected = new byte[] { 0x05, 0x00 };

            using (var stream = new MemoryStream())
            {
                transcoder.Encode(stream, data, flags, OpCode.Get);

                Assert.Equal(expected, stream.ToArray());
            }
        }
        public void Test_Serialize_UInt64()
        {
            var    transcoder = new LegacyTranscoder();
            UInt64 data       = 9;

            var flags = new Flags
            {
                Compression = Couchbase.Core.IO.Operations.Compression.None,
                DataFormat  = DataFormat.Reserved,
                TypeCode    = Convert.GetTypeCode(data)
            };

            var expected = new byte[] { 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

            using (var stream = new MemoryStream())
            {
                transcoder.Encode(stream, data, flags, OpCode.Get);

                Assert.Equal(expected, stream.ToArray());
            }
        }
Example #18
0
        public void Test_Poco()
        {
            var transcoder = new LegacyTranscoder();
            var value      = new Person {
                Name = "jeff"
            };

            var flags = new Flags
            {
                Compression = Compression.None,
                DataFormat  = DataFormat.Reserved,
                TypeCode    = Type.GetTypeCode(typeof(Person))
            };

            using (var stream = new MemoryStream())
            {
                transcoder.Encode(stream, value, flags, OpCode.Get);
                var actual = transcoder.Decode <Person>(stream.ToArray(), flags, OpCode.Get);

                Assert.Equal(value.Name, actual.Name);
            }
        }
Example #19
0
        public void Should_Convert_To_CamelCase_Json_With_Default_Serialization_Settings()
        {
            var transcoder = new LegacyTranscoder();
            var data       = new Pascal
            {
                SomeProperty    = "SOME",
                SomeIntProperty = 12345,
                HasPascalCase   = true
            };
            var expectedJsonBytes = Encoding.UTF8.GetBytes("{\"someProperty\":\"SOME\",\"someIntProperty\":12345,\"hasPascalCase\":true}");

            using (var jsonBytes = new MemoryStream())
                using (var jsonEncoded = new MemoryStream())
                {
                    transcoder.SerializeAsJson(jsonBytes, data);
                    transcoder.Encode(jsonEncoded, data, new Flags {
                        DataFormat = DataFormat.Json
                    }, OpCode.Get);
                    Assert.Equal(expectedJsonBytes, jsonBytes.ToArray());
                    Assert.Equal(expectedJsonBytes, jsonEncoded.ToArray());
                }
        }
Example #20
0
        public void When_ByteArray_Is_Stored_With_Legacy_Flags_It_Is_Decoded_As_A_ByteArray()
        {
            var legacyByteArray = new byte[]
            {
                129, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 8, 0,
                0, 0, 5, 19, 185, 8, 248, 3, 104, 208, 188, 0,
                0, 250, 82, 116, 101, 115, 116
            };

            var format = new byte();

            var temp = legacyByteArray[24];

            BitUtils.SetBit(ref format, 0, BitUtils.GetBit(temp, 0));
            BitUtils.SetBit(ref format, 1, BitUtils.GetBit(temp, 1));
            BitUtils.SetBit(ref format, 2, BitUtils.GetBit(temp, 2));
            BitUtils.SetBit(ref format, 3, BitUtils.GetBit(temp, 3));

            var compression = new byte();

            BitUtils.SetBit(ref compression, 4, BitUtils.GetBit(temp, 4));
            BitUtils.SetBit(ref compression, 5, BitUtils.GetBit(temp, 5));
            BitUtils.SetBit(ref compression, 6, BitUtils.GetBit(temp, 6));

            var flags = new Flags
            {
                DataFormat  = (DataFormat)format,
                Compression = (Compression)compression,
                TypeCode    = (TypeCode)(ByteConverter.ToUInt16(legacyByteArray.AsSpan(26)) & 0xff),
            };

            var transcoder = new LegacyTranscoder();
            var result     = transcoder.Decode <byte[]>(legacyByteArray.AsMemory(28, 4), flags, OpCode.Get);

            Assert.Equal("test", Encoding.UTF8.GetString(result));
        }
Example #21
0
        public async Task GetEmptyDoc_LegacyTranscoder(string content)
        {
            const string id         = nameof(GetEmptyDoc_LegacyTranscoder);
            var          collection = await _fixture.GetDefaultCollection().ConfigureAwait(false);

            var transcoder = new LegacyTranscoder(new DefaultSerializer());

            try
            {
                await collection.InsertAsync(id, content, insertOptions => insertOptions.Transcoder(transcoder))
                .ConfigureAwait(false);

                var getResult = await collection.GetAsync(id, getOptions => getOptions.Transcoder(transcoder))
                                .ConfigureAwait(false);

                var value = getResult.ContentAs <string>();

                Assert.Null(value);
            }
            finally
            {
                await collection.RemoveAsync(id).ConfigureAwait(false);
            }
        }