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); }
public FeedProcessorFactoryCore( ContainerInternal container, ChangeFeedProcessorOptions changeFeedProcessorOptions, DocumentServiceLeaseCheckpointer leaseCheckpointer, CosmosSerializerCore serializerCore) { if (container == null) { throw new ArgumentNullException(nameof(container)); } if (changeFeedProcessorOptions == null) { throw new ArgumentNullException(nameof(changeFeedProcessorOptions)); } if (leaseCheckpointer == null) { throw new ArgumentNullException(nameof(leaseCheckpointer)); } if (serializerCore == null) { throw new ArgumentNullException(nameof(serializerCore)); } this.container = container; this.changeFeedProcessorOptions = changeFeedProcessorOptions; this.leaseCheckpointer = leaseCheckpointer; this.serializerCore = serializerCore; }
public BatchAsyncBatcherThatOverflows( int maxBatchOperationCount, int maxBatchByteSize, CosmosSerializerCore serializerCore, BatchAsyncBatcherExecuteDelegate executor, BatchAsyncBatcherRetryDelegate retrier) : base(maxBatchOperationCount, maxBatchByteSize, serializerCore, executor, retrier, BatchAsyncBatcherTests.MockClientContext()) { }
/// <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); }
internal static T[] GetResourcesHelper <T>( IReadOnlyList <CosmosElement> cosmosElements, CosmosSerializerCore serializerCore, CosmosSerializationFormatOptions cosmosSerializationOptions = null) { using (MemoryStream memoryStream = ElementsToMemoryStream( cosmosElements, cosmosSerializationOptions)) { return(serializerCore.FromFeedStream <T>(memoryStream)); } }
public FeedProcessorCore( ChangeFeedObserver <T> observer, FeedIterator resultSetIterator, ProcessorOptions options, PartitionCheckpointer checkpointer, CosmosSerializerCore serializerCore) { this.observer = observer; this.options = options; this.checkpointer = checkpointer; this.resultSetIterator = resultSetIterator; this.serializerCore = serializerCore; }
private static IEnumerable <T> GetResourcesHelper <T>( IReadOnlyList <CosmosElement> cosmosArray, CosmosSerializerCore serializerCore) { List <T> result = new List <T>(); foreach (CosmosElement element in cosmosArray) { MemoryStream memory = CosmosElementSerializer.ElementToMemoryStream(element, null); result.Add(serializerCore.FromStream <T>(memory)); } return(result); }
public ObserverExceptionWrappingChangeFeedObserverDecoratorTests() { this.observer = new Mock <ChangeFeedObserver>(); this.changeFeedObserverContext = new ChangeFeedObserverContextCore(Guid.NewGuid().ToString(), feedResponse: null, Mock.Of <PartitionCheckpointer>());; this.observerWrapper = new ObserverExceptionWrappingChangeFeedObserverDecorator(this.observer.Object); this.serializerCore = new CosmosSerializerCore(); MyDocument document = new MyDocument(); this.documents = new List <MyDocument> { document }; }
internal static IReadOnlyCollection <T> FromFeedResponseStream <T>( CosmosSerializerCore serializerCore, Stream streamWithServiceEnvelope) { if (streamWithServiceEnvelope == null) { return(new List <T>()); } using (streamWithServiceEnvelope) using (MemoryStream stream = GetStreamWithoutServiceEnvelope( streamWithServiceEnvelope)) { return(serializerCore.FromFeedStream <T>(stream)); } }
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); } } }
internal static IEnumerable <T> GetResources <T>( IReadOnlyList <CosmosElement> cosmosArray, CosmosSerializerCore serializerCore) { if (cosmosArray == null) { return(new List <T>()); } if (typeof(CosmosElement).IsAssignableFrom(typeof(T))) { return(cosmosArray.Cast <T>()); } return(CosmosElementSerializer.GetResourcesHelper <T>( cosmosArray, serializerCore)); }
/// <summary> /// Converts a list of CosmosElements into a list of objects. /// </summary> /// <param name="containerRid">Container Rid</param> /// <param name="cosmosElements">The cosmos elements</param> /// <param name="resourceType">The resource type</param> /// <param name="jsonSerializer">The JSON </param> /// <param name="cosmosSerializationOptions">The custom serialization options. This allows custom serialization types like BSON, JSON, or other formats</param> /// <returns>Returns a list of deserialized objects</returns> internal static IEnumerable <T> Deserialize <T>( string containerRid, IEnumerable <CosmosElement> cosmosElements, ResourceType resourceType, CosmosSerializerCore jsonSerializer, CosmosSerializationFormatOptions cosmosSerializationOptions = null) { if (!cosmosElements.Any()) { return(Enumerable.Empty <T>()); } Stream stream = CosmosElementSerializer.ToStream( containerRid, cosmosElements, resourceType, cosmosSerializationOptions); IEnumerable <T> typedResults = jsonSerializer.FromFeedResponseStream <T>( stream, resourceType); return(typedResults); }
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); }
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); } } }
public void ValidateCustomSerializerNotUsedForInternalTypes() { CosmosSerializerHelper serializerHelper = new CosmosSerializerHelper( null, (item) => throw new ArgumentException("Should be using internal serializer"), (item) => throw new ArgumentException("Should be using internal serializer")); CosmosSerializerCore serializerCore = new CosmosSerializerCore(serializerHelper); string id = "testId"; this.TestProperty <AccountProperties>( serializerCore, id, $@"{{""id"":""{id}"",""writableLocations"":[],""readableLocations"":[],""userConsistencyPolicy"":null,""addresses"":null,""userReplicationPolicy"":null,""systemReplicationPolicy"":null,""readPolicy"":null,""queryEngineConfiguration"":null,""enableMultipleWriteLocations"":false}}"); this.TestProperty <DatabaseProperties>( serializerCore, id, $@"{{""id"":""{id}""}}"); this.TestProperty <ContainerProperties>( serializerCore, id, $@"{{""id"":""{id}"",""partitionKey"":{{""paths"":[],""kind"":""Hash""}}}}"); this.TestProperty <StoredProcedureProperties>( serializerCore, id, $@"{{""body"":""bodyCantBeNull"",""id"":""{id}""}}"); this.TestProperty <TriggerProperties>( serializerCore, id, $@"{{""body"":null,""triggerType"":""Pre"",""triggerOperation"":""All"",""id"":""{id}""}}"); this.TestProperty <UserDefinedFunctionProperties>( serializerCore, id, $@"{{""body"":null,""id"":""{id}""}}"); this.TestProperty <UserProperties>( serializerCore, id, $@"{{""id"":""{id}"",""_permissions"":null}}"); this.TestProperty <PermissionProperties>( serializerCore, id, $@"{{""id"":""{id}"",""resource"":null,""permissionMode"":0}}"); this.TestProperty <ConflictProperties>( serializerCore, id, $@"{{""id"":""{id}"",""operationType"":""Invalid"",""resourceType"":null,""resourceId"":null,""content"":null,""conflict_lsn"":0}}"); // Throughput doesn't have an id. string defaultThroughputJson = @"{}"; ThroughputProperties property = JsonConvert.DeserializeObject <ThroughputProperties>(defaultThroughputJson); Assert.IsNull(property.Throughput); string propertyJson = JsonConvert.SerializeObject(property); Assert.AreEqual(defaultThroughputJson, propertyJson); }
public async Task ValidateResponseFactoryJsonSerializer() { ResponseMessage databaseResponse = this.CreateResponse(); ResponseMessage containerResponse = this.CreateResponse(); ResponseMessage storedProcedureExecuteResponse = this.CreateResponse(); ResponseMessage storedProcedureResponse = this.CreateResponse(); ResponseMessage triggerResponse = this.CreateResponse(); ResponseMessage udfResponse = this.CreateResponse(); ResponseMessage itemResponse = this.CreateResponse(); Mock <CosmosSerializer> mockUserJsonSerializer = new Mock <CosmosSerializer>(); CosmosSerializerCore serializerCore = new CosmosSerializerCore(mockUserJsonSerializer.Object); CosmosResponseFactory cosmosResponseFactory = new CosmosResponseFactory( serializerCore); // Test the user specified response mockUserJsonSerializer.Setup(x => x.FromStream <ToDoActivity>(itemResponse.Content)).Callback <Stream>(input => input.Dispose()).Returns(new ToDoActivity()); mockUserJsonSerializer.Setup(x => x.FromStream <ToDoActivity>(storedProcedureExecuteResponse.Content)).Callback <Stream>(input => input.Dispose()).Returns(new ToDoActivity()); // Verify all the user types use the user specified version await cosmosResponseFactory.CreateItemResponseAsync <ToDoActivity>(Task.FromResult(itemResponse)); await cosmosResponseFactory.CreateStoredProcedureExecuteResponseAsync <ToDoActivity>(Task.FromResult(storedProcedureExecuteResponse)); // Throw if the setups were not called mockUserJsonSerializer.VerifyAll(); // Test the system specified response ContainerProperties containerSettings = new ContainerProperties("mockId", "/pk"); DatabaseProperties databaseSettings = new DatabaseProperties() { Id = "mock" }; StoredProcedureProperties cosmosStoredProcedureSettings = new StoredProcedureProperties() { Id = "mock" }; TriggerProperties cosmosTriggerSettings = new TriggerProperties() { Id = "mock" }; UserDefinedFunctionProperties cosmosUserDefinedFunctionSettings = new UserDefinedFunctionProperties() { Id = "mock" }; Mock <Container> mockContainer = new Mock <Container>(); Mock <Database> mockDatabase = new Mock <Database>(); // Verify all the system types that should always use default await cosmosResponseFactory.CreateContainerResponseAsync(mockContainer.Object, Task.FromResult(containerResponse)); await cosmosResponseFactory.CreateDatabaseResponseAsync(mockDatabase.Object, Task.FromResult(databaseResponse)); await cosmosResponseFactory.CreateStoredProcedureResponseAsync(Task.FromResult(storedProcedureResponse)); await cosmosResponseFactory.CreateTriggerResponseAsync(Task.FromResult(triggerResponse)); await cosmosResponseFactory.CreateUserDefinedFunctionResponseAsync(Task.FromResult(udfResponse)); }
public void ValidateResponseFactoryJsonSerializer() { ResponseMessage databaseResponse = this.CreateResponse(); ResponseMessage containerResponse = this.CreateResponse(); ResponseMessage storedProcedureExecuteResponse = this.CreateResponse(); ResponseMessage storedProcedureResponse = this.CreateResponse(); ResponseMessage triggerResponse = this.CreateResponse(); ResponseMessage udfResponse = this.CreateResponse(); ResponseMessage itemResponse = this.CreateResponse(); Mock <CosmosSerializer> mockUserJsonSerializer = new Mock <CosmosSerializer>(); CosmosSerializerCore serializerCore = new CosmosSerializerCore(mockUserJsonSerializer.Object); CosmosResponseFactoryInternal cosmosResponseFactory = new CosmosResponseFactoryCore( serializerCore); // Test the user specified response mockUserJsonSerializer.Setup(x => x.FromStream <ToDoActivity>(itemResponse.Content)).Callback <Stream>(input => input.Dispose()).Returns(new ToDoActivity()); mockUserJsonSerializer.Setup(x => x.FromStream <ToDoActivity>(storedProcedureExecuteResponse.Content)).Callback <Stream>(input => input.Dispose()).Returns(new ToDoActivity()); // Verify all the user types use the user specified version cosmosResponseFactory.CreateItemResponse <ToDoActivity>(itemResponse); cosmosResponseFactory.CreateStoredProcedureExecuteResponse <ToDoActivity>(storedProcedureExecuteResponse); // Throw if the setups were not called mockUserJsonSerializer.VerifyAll(); // Test read feed scenario ResponseMessage readFeedResponse = this.CreateReadFeedResponse(); mockUserJsonSerializer.Setup(x => x.FromStream <ToDoActivity[]>(It.IsAny <Stream>())) .Callback <Stream>(input => input.Dispose()) .Returns(new ToDoActivity[] { new ToDoActivity() }); FeedResponse <ToDoActivity> feedResponse = cosmosResponseFactory.CreateItemFeedResponse <ToDoActivity>(readFeedResponse); foreach (ToDoActivity toDoActivity in feedResponse) { Assert.IsNotNull(toDoActivity); } mockUserJsonSerializer.VerifyAll(); ResponseMessage changeFeedResponseMessage = this.CreateChangeFeedNotModifiedResponse(); FeedResponse <ToDoActivity> changeFeedResponse = cosmosResponseFactory.CreateItemFeedResponse <ToDoActivity>(changeFeedResponseMessage); Assert.AreEqual(HttpStatusCode.NotModified, changeFeedResponse.StatusCode); Assert.IsFalse(changeFeedResponse.Resource.Any()); ResponseMessage queryResponse = this.CreateReadFeedResponse(); mockUserJsonSerializer.Setup(x => x.FromStream <ToDoActivity[]>(It.IsAny <Stream>())).Callback <Stream>(input => input.Dispose()).Returns(new ToDoActivity[] { new ToDoActivity() }); FeedResponse <ToDoActivity> queryFeedResponse = cosmosResponseFactory.CreateItemFeedResponse <ToDoActivity>(queryResponse); foreach (ToDoActivity toDoActivity in queryFeedResponse) { Assert.IsNotNull(toDoActivity); } mockUserJsonSerializer.VerifyAll(); // Test the system specified response ContainerProperties containerSettings = new ContainerProperties("mockId", "/pk"); DatabaseProperties databaseSettings = new DatabaseProperties() { Id = "mock" }; StoredProcedureProperties cosmosStoredProcedureSettings = new StoredProcedureProperties() { Id = "mock" }; TriggerProperties cosmosTriggerSettings = new TriggerProperties() { Id = "mock" }; UserDefinedFunctionProperties cosmosUserDefinedFunctionSettings = new UserDefinedFunctionProperties() { Id = "mock" }; Mock <Container> mockContainer = new Mock <Container>(); Mock <Database> mockDatabase = new Mock <Database>(); // Verify all the system types that should always use default cosmosResponseFactory.CreateContainerResponse(mockContainer.Object, containerResponse); cosmosResponseFactory.CreateDatabaseResponse(mockDatabase.Object, databaseResponse); cosmosResponseFactory.CreateStoredProcedureResponse(storedProcedureResponse); cosmosResponseFactory.CreateTriggerResponse(triggerResponse); cosmosResponseFactory.CreateUserDefinedFunctionResponse(udfResponse); }