public void ValidatePatchOperationSerialization()
        {
            int toCount   = 0;
            int fromCount = 0;

            CosmosSerializerHelper serializerHelper = new CosmosSerializerHelper(
                null,
                (input) => fromCount++,
                (input) => toCount++);

            CosmosSerializerCore  serializerCore = new CosmosSerializerCore(serializerHelper);
            List <PatchOperation> patch          = new List <PatchOperation>()
            {
                PatchOperation.Remove("/removePath")
            };

            Assert.AreEqual(0, toCount);

            // custom serializer is not used since operation type is Remove, which doesnt have "value" param to serialize
            using (Stream stream = serializerCore.ToStream(patch)) { }
            Assert.AreEqual(0, toCount);

            patch.Add(PatchOperation.Add("/addPath", "addValue"));
            // custom serializer is used since there is Add operation type also
            using (Stream stream = serializerCore.ToStream(patch)) { }
            Assert.AreEqual(1, toCount);
        }
        /// <summary>
        /// Materializes the operation's resource into a Memory{byte} wrapping a byte array.
        /// </summary>
        /// <param name="serializerCore">Serializer to serialize user provided objects to JSON.</param>
        /// <param name="cancellationToken"><see cref="CancellationToken"/> for cancellation.</param>
        internal override Task MaterializeResourceAsync(CosmosSerializerCore serializerCore, CancellationToken cancellationToken)
        {
            if (this.body.IsEmpty && this.Resource != null)
            {
                this.ResourceStream = serializerCore.ToStream(this.Resource);
                return(base.MaterializeResourceAsync(serializerCore, cancellationToken));
            }

            return(Task.CompletedTask);
        }
        public void ValidatePatchOperationSerialization()
        {
            int toCount   = 0;
            int fromCount = 0;

            CosmosSerializerHelper serializerHelper = new CosmosSerializerHelper(
                null,
                (input) => fromCount++,
                (input) => toCount++);

            CosmosSerializerCore  serializerCore = new CosmosSerializerCore(serializerHelper);
            List <PatchOperation> patch          = new List <PatchOperation>()
            {
                PatchOperation.Remove("/removePath")
            };

            Assert.AreEqual(0, toCount);

            PatchItemRequestOptions patchRequestOptions = new PatchItemRequestOptions();

            // custom serializer is not used since operation type is Remove, which doesnt have "value" param to serialize
            using (Stream stream = serializerCore.ToStream(new PatchSpec(patch, patchRequestOptions))) { }
            Assert.AreEqual(0, toCount);

            patch.Add(PatchOperation.Add("/addPath", "addValue"));
            // custom serializer is used since there is Add operation type also
            using (Stream stream = serializerCore.ToStream(new PatchSpec(patch, patchRequestOptions))) { }
            Assert.AreEqual(1, toCount);

            patch.Clear();
            toCount = 0;
            patch.Add(PatchOperation.Add("/addPath", new CosmosJsonDotNetSerializer().ToStream("addValue")));
            // custom serializer is not used since the input value is of type stream
            using (Stream stream = serializerCore.ToStream(new PatchSpec(patch, patchRequestOptions))) { }
            Assert.AreEqual(0, toCount);
        }
        private void TestProperty <T>(
            CosmosSerializerCore serializerCore,
            string id,
            string defaultJson)
        {
            dynamic property = serializerCore.FromStream <T>(new MemoryStream(Encoding.UTF8.GetBytes(defaultJson)));

            Assert.AreEqual(id, property.Id);
            using (Stream stream = serializerCore.ToStream <T>(property))
            {
                using (StreamReader sr = new StreamReader(stream))
                {
                    string propertyJson = sr.ReadToEnd();
                    Assert.AreEqual(defaultJson, propertyJson);
                }
            }
        }
        public void ValidateSqlQuerySpecSerialization()
        {
            int toCount   = 0;
            int fromCount = 0;

            CosmosSerializerHelper serializerHelper = new CosmosSerializerHelper(
                null,
                (input) => fromCount++,
                (input) => toCount++);

            CosmosSerializerCore serializerCore = new CosmosSerializerCore(serializerHelper);
            SqlQuerySpec         querySpec      = new SqlQuerySpec("select * from T where T.id = @id");

            querySpec.Parameters = new SqlParameterCollection()
            {
                new SqlParameter("@id", "testValue")
            };

            try
            {
                serializerCore.ToStream <SqlQuerySpec>(querySpec);
                Assert.Fail("ToStream should throw exception");
            }
            catch (ArgumentException e)
            {
                Assert.IsNotNull(e);
            }

            try
            {
                serializerCore.FromStream <SqlQuerySpec>(new MemoryStream());
                Assert.Fail("FromStream should throw exception");
            }
            catch (ArgumentException e)
            {
                Assert.IsNotNull(e);
            }

            Assert.AreEqual(0, toCount);
            Assert.AreEqual(0, fromCount);

            using (Stream stream = serializerCore.ToStreamSqlQuerySpec(querySpec, ResourceType.Offer)) { }

            Assert.AreEqual(0, toCount);
            Assert.AreEqual(0, fromCount);

            List <ResourceType> publicQuerySupportedTypes = new List <ResourceType>()
            {
                ResourceType.Database,
                ResourceType.Collection,
                ResourceType.Document,
                ResourceType.Trigger,
                ResourceType.UserDefinedFunction,
                ResourceType.StoredProcedure,
                ResourceType.Permission,
                ResourceType.User,
                ResourceType.Conflict
            };

            foreach (ResourceType resourceType in publicQuerySupportedTypes)
            {
                toCount = 0;

                using (Stream stream = serializerCore.ToStreamSqlQuerySpec(querySpec, resourceType))
                {
                    Assert.AreEqual(1, toCount);
                    Assert.AreEqual(0, fromCount);
                }
            }
        }