From b7e8a06f680a04cf7a328faa61a7e655e13c44e6 Mon Sep 17 00:00:00 2001 From: Debdatta Kunda Date: Thu, 5 Sep 2024 12:40:40 -0700 Subject: [PATCH] Adding more tests --- .../Resource/Container/ContainerCore.Items.cs | 2 +- .../src/Serializer/CosmosSerializationUtil.cs | 12 ++ .../CosmosSerializationUtilTests.cs | 143 ++++++++++++++++++ 3 files changed, 156 insertions(+), 1 deletion(-) create mode 100644 Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/CosmosSerializationUtilTests.cs diff --git a/Microsoft.Azure.Cosmos/src/Resource/Container/ContainerCore.Items.cs b/Microsoft.Azure.Cosmos/src/Resource/Container/ContainerCore.Items.cs index 174de24292..82b4adbf8d 100644 --- a/Microsoft.Azure.Cosmos/src/Resource/Container/ContainerCore.Items.cs +++ b/Microsoft.Azure.Cosmos/src/Resource/Container/ContainerCore.Items.cs @@ -924,7 +924,7 @@ private async Task ProcessItemStreamAsync( ContainerInternal.ValidatePartitionKey(partitionKey, requestOptions); string resourceUri = this.GetResourceUri(requestOptions, operationType, itemId); - // Convert Text to Binary Stream TrySerializeStreamToTargetFormat(). + // Convert Text to Binary Stream. if (CosmosSerializationUtil.TrySerializeStreamToTargetFormat( expectedSerializationFormat: JsonSerializationFormat.Text, targetSerializationFormat: targetRequestSerializationFormat, diff --git a/Microsoft.Azure.Cosmos/src/Serializer/CosmosSerializationUtil.cs b/Microsoft.Azure.Cosmos/src/Serializer/CosmosSerializationUtil.cs index 4d605fd157..0b9abe8010 100644 --- a/Microsoft.Azure.Cosmos/src/Serializer/CosmosSerializationUtil.cs +++ b/Microsoft.Azure.Cosmos/src/Serializer/CosmosSerializationUtil.cs @@ -204,6 +204,12 @@ internal static Stream ConvertInputToTextStream( return streamPayload; } + /// + /// Determines if the first byte of a stream matches the binary JSON serialization format. + /// + /// The first byte of the stream to check. + /// The desired JSON serialization format. + /// Returns true if the first byte matches the binary format, otherwise false. internal static bool IsBinaryFormat( int firstByte, JsonSerializationFormat desiredFormat) @@ -211,6 +217,12 @@ internal static bool IsBinaryFormat( return desiredFormat == JsonSerializationFormat.Binary && firstByte == (int)JsonSerializationFormat.Binary; } + /// + /// Determines if the first byte of a stream matches the text JSON serialization format. + /// + /// The first byte of the stream to check. + /// The desired JSON serialization format. + /// Returns true if the first byte matches the text format, otherwise false. internal static bool IsTextFormat( int firstByte, JsonSerializationFormat desiredFormat) diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/CosmosSerializationUtilTests.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/CosmosSerializationUtilTests.cs new file mode 100644 index 0000000000..4d70443b99 --- /dev/null +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/CosmosSerializationUtilTests.cs @@ -0,0 +1,143 @@ +//------------------------------------------------------------ +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------ + +namespace Microsoft.Azure.Cosmos.Tests +{ + using System.IO; + using Microsoft.Azure.Cosmos.Json; + using Microsoft.VisualStudio.TestTools.UnitTesting; + + /// + /// Test class for + /// + [TestClass] + public class CosmosSerializationUtilTests + { + [TestMethod] + public void GetStringWithPropertyNamingPolicy_CamelCase() + { + // Arrange + CosmosLinqSerializerOptions options = new() { PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase }; + string propertyName = "TestProperty"; + + // Act + string result = CosmosSerializationUtil.GetStringWithPropertyNamingPolicy(options, propertyName); + + // Assert + Assert.AreEqual("testProperty", result); + } + + [TestMethod] + public void GetStringWithPropertyNamingPolicy_Default() + { + // Arrange + CosmosLinqSerializerOptions options = new() { PropertyNamingPolicy = CosmosPropertyNamingPolicy.Default }; + string propertyName = "TestProperty"; + + // Act + string result = CosmosSerializationUtil.GetStringWithPropertyNamingPolicy(options, propertyName); + + // Assert + Assert.AreEqual("TestProperty", result); + } + + [TestMethod] + public void IsBinaryFormat_True() + { + // Arrange + int firstByte = (int)JsonSerializationFormat.Binary; + JsonSerializationFormat format = JsonSerializationFormat.Binary; + + // Act + bool result = CosmosSerializationUtil.IsBinaryFormat(firstByte, format); + + // Assert + Assert.IsTrue(result); + } + + [TestMethod] + public void IsBinaryFormat_False() + { + // Arrange + int firstByte = (int)JsonSerializationFormat.Text; + JsonSerializationFormat format = JsonSerializationFormat.Binary; + + // Act + bool result = CosmosSerializationUtil.IsBinaryFormat(firstByte, format); + + // Assert + Assert.IsFalse(result); + } + + [TestMethod] + public void IsTextFormat_True() + { + // Arrange + int firstByte = (int)JsonSerializationFormat.Text; + JsonSerializationFormat format = JsonSerializationFormat.Text; + + // Act + bool result = CosmosSerializationUtil.IsTextFormat(firstByte, format); + + // Assert + Assert.IsTrue(result); + } + + [TestMethod] + public void IsTextFormat_False() + { + // Arrange + int firstByte = (int)JsonSerializationFormat.Binary; + JsonSerializationFormat format = JsonSerializationFormat.Text; + + // Act + bool result = CosmosSerializationUtil.IsTextFormat(firstByte, format); + + // Assert + Assert.IsFalse(result); + } + + [TestMethod] + [DataRow("text", "binary", DisplayName = "Validate Text to Binary Conversation.")] + [DataRow("binary", "text", DisplayName = "Validate Binary to Text Conversation.")] + public void TrySerializeStreamToTargetFormat_Success(string expected, string target) + { + // Arrange + JsonSerializationFormat expectedFormat = expected.Equals("text") ? JsonSerializationFormat.Text : JsonSerializationFormat.Binary; + JsonSerializationFormat targetFormat = target.Equals("text") ? JsonSerializationFormat.Text : JsonSerializationFormat.Binary; + string json = "{\"name\":\"test\"}"; + + Stream inputStream = JsonSerializationFormat.Text.Equals(expectedFormat) + ? CosmosSerializationUtil.ConvertInputToTextStream(json, Newtonsoft.Json.JsonSerializer.Create()) + : CosmosSerializationUtil.ConvertInputToBinaryStream(json, Newtonsoft.Json.JsonSerializer.Create()); + + // Act + bool result = CosmosSerializationUtil.TrySerializeStreamToTargetFormat(expectedFormat, targetFormat, inputStream, out Stream outputStream); + + // Assert + Assert.IsTrue(result); + Assert.IsNotNull(outputStream); + Assert.IsTrue(CosmosSerializationUtil.CheckFirstBufferByte(outputStream, targetFormat, out byte[] binBytes)); + Assert.IsNotNull(binBytes); + Assert.IsTrue(binBytes.Length > 0); + } + + [TestMethod] + public void TrySerializeStreamToTargetFormat_Failure() + { + // Arrange + string json = "{\"name\":\"test\"}"; + Stream inputStream = CosmosSerializationUtil.ConvertInputToTextStream(json, Newtonsoft.Json.JsonSerializer.Create()); + JsonSerializationFormat expectedFormat = JsonSerializationFormat.Text; + JsonSerializationFormat targetFormat = JsonSerializationFormat.Text; + + // Act + bool result = CosmosSerializationUtil.TrySerializeStreamToTargetFormat(expectedFormat, targetFormat, inputStream, out Stream outputStream); + + // Assert + Assert.IsFalse(result); + Assert.IsNull(outputStream); + } + } +} \ No newline at end of file