Exemplo n.º 1
0
        public async Task BatchResponseDeserializationAsync()
        {
            using CosmosClient cosmosClient = MockCosmosUtil.CreateMockCosmosClient();
            ContainerInternal containerCore = (ContainerInlineCore)cosmosClient.GetDatabase("db").GetContainer("cont");
            List <TransactionalBatchOperationResult> results = new List <TransactionalBatchOperationResult>
            {
                new TransactionalBatchOperationResult(HttpStatusCode.Conflict),
                new TransactionalBatchOperationResult(HttpStatusCode.OK)
                {
                    ResourceStream = new MemoryStream(new byte[] { 0x41, 0x42 }, index: 0, count: 2, writable: false, publiclyVisible: true),
                    RequestCharge  = 2.5,
                    ETag           = "1234",
                    RetryAfter     = TimeSpan.FromMilliseconds(360)
                }
            };

            MemoryStream responseContent = await new BatchResponsePayloadWriter(results).GeneratePayloadAsync();

            CosmosSerializer serializer = new CosmosJsonDotNetSerializer();
            SinglePartitionKeyServerBatchRequest batchRequest = await SinglePartitionKeyServerBatchRequest.CreateAsync(
                partitionKey : Cosmos.PartitionKey.None,
                operations : new ArraySegment <ItemBatchOperation>(
                    new ItemBatchOperation[]
            {
                new ItemBatchOperation(OperationType.Read, operationIndex: 0, id: "someId", containerCore: containerCore),
                new ItemBatchOperation(OperationType.Read, operationIndex: 0, id: "someId", containerCore: containerCore)
            }),
                serializerCore : MockCosmosUtil.Serializer,
                trace : NoOpTrace.Singleton,
                cancellationToken : CancellationToken.None);

            ResponseMessage response = new ResponseMessage((HttpStatusCode)StatusCodes.MultiStatus)
            {
                Content = responseContent
            };

            response.Headers.Session    = Guid.NewGuid().ToString();
            response.Headers.ActivityId = Guid.NewGuid().ToString();
            TransactionalBatchResponse batchResponse = await TransactionalBatchResponse.FromResponseMessageAsync(
                response,
                batchRequest,
                MockCosmosUtil.Serializer,
                true,
                NoOpTrace.Singleton,
                CancellationToken.None);

            Assert.IsNotNull(batchRequest);
            Assert.AreEqual(HttpStatusCode.Conflict, batchResponse.StatusCode);
            Assert.AreEqual(2, batchResponse.Count);
            Assert.AreEqual(response.Headers.Session, batchResponse[0].SessionToken);
            Assert.AreEqual(response.Headers.Session, batchResponse[1].SessionToken);
            Assert.AreEqual(response.Headers.ActivityId, batchResponse[0].ActivityId);
            Assert.AreEqual(response.Headers.ActivityId, batchResponse[1].ActivityId);


            CosmosBatchOperationResultEqualityComparer comparer = new CosmosBatchOperationResultEqualityComparer();

            Assert.IsTrue(comparer.Equals(results[0], batchResponse[0]));
            Assert.IsTrue(comparer.Equals(results[1], batchResponse[1]));
        }
        private static void ValidateOperations <T>(PatchOperation patchOperation, PatchOperationType operationType, T value)
        {
            Assert.AreEqual(operationType, patchOperation.OperationType);
            Assert.AreEqual(path, patchOperation.Path);

            if (!operationType.Equals(PatchOperationType.Remove))
            {
                string           expected;
                CosmosSerializer cosmosSerializer = new CosmosJsonDotNetSerializer();
                using (Stream stream = cosmosSerializer.ToStream(value))
                {
                    using (StreamReader streamReader = new StreamReader(stream))
                    {
                        expected = streamReader.ReadToEnd();
                    }
                }

                Assert.IsTrue(patchOperation.TrySerializeValueParameter(new CustomSerializer(), out Stream valueParam));

                string actual;
                using (valueParam)
                {
                    using (StreamReader streamReader = new StreamReader(valueParam))
                    {
                        actual = streamReader.ReadToEnd();
                    }
                }

                Assert.AreEqual(expected, actual);
            }
        }
        public void ValidateCustomSerializerSettings()
        {
            JsonSerializerSettings settings = new JsonSerializerSettings()
            {
                NullValueHandling = NullValueHandling.Ignore
            };

            ToDoActivity toDoActivityNoDescription = new ToDoActivity()
            {
                id          = "c1d433c1-369d-430e-91e5-14e3ce588f71",
                taskNum     = 42,
                cost        = double.MaxValue,
                description = null,
                status      = "TBD"
            };

            string toDoActivityJson = @"{""id"":""c1d433c1-369d-430e-91e5-14e3ce588f71"",""taskNum"":42,""cost"":1.7976931348623157E+308,""status"":""TBD""}";
            CosmosJsonDotNetSerializer cosmosDefaultJsonSerializer = new CosmosJsonDotNetSerializer(settings);

            using (Stream stream = cosmosDefaultJsonSerializer.ToStream <ToDoActivity>(toDoActivityNoDescription))
            {
                Assert.IsNotNull(stream);
                using (StreamReader reader = new StreamReader(stream))
                {
                    string responseAsString = reader.ReadToEnd();
                    Assert.IsNotNull(responseAsString);
                    Assert.AreEqual(toDoActivityJson, responseAsString);
                }
            }
        }
        public async Task BatchSingleServerResponseAsync()
        {
            List <TransactionalBatchOperationResult> expectedResults = new List <TransactionalBatchOperationResult>();
            CosmosJsonDotNetSerializer jsonSerializer = new CosmosJsonDotNetSerializer();
            TestItem testItem = new TestItem("tst");

            Stream       itemStream     = jsonSerializer.ToStream <TestItem>(testItem);
            MemoryStream resourceStream = itemStream as MemoryStream;

            if (resourceStream == null)
            {
                await itemStream.CopyToAsync(resourceStream);

                resourceStream.Position = 0;
            }

            expectedResults.Add(
                new TransactionalBatchOperationResult(HttpStatusCode.OK)
            {
                ETag           = "theETag",
                SubStatusCode  = (SubStatusCodes)1100,
                ResourceStream = resourceStream
            });
            expectedResults.Add(new TransactionalBatchOperationResult(HttpStatusCode.Conflict));

            double requestCharge = 3.6;

            TestHandler testHandler = new TestHandler(async(request, cancellationToken) =>
            {
                ResponseMessage responseMessage = new ResponseMessage(HttpStatusCode.OK, requestMessage: null, errorMessage: null)
                {
                    Content = await new BatchResponsePayloadWriter(expectedResults).GeneratePayloadAsync()
                };

                responseMessage.Headers.RequestCharge = requestCharge;
                return(responseMessage);
            });

            Container container = BatchUnitTests.GetContainer(testHandler);

            TransactionalBatchResponse batchResponse = await new BatchCore((ContainerInternal)container, new Cosmos.PartitionKey(BatchUnitTests.PartitionKey1))
                                                       .ReadItem("id1")
                                                       .ReadItem("id2")
                                                       .ExecuteAsync();

            Assert.AreEqual(HttpStatusCode.OK, batchResponse.StatusCode);
            Assert.AreEqual(requestCharge, batchResponse.RequestCharge);

            TransactionalBatchOperationResult <TestItem> result0 = batchResponse.GetOperationResultAtIndex <TestItem>(0);

            Assert.AreEqual(expectedResults[0].StatusCode, result0.StatusCode);
            Assert.AreEqual(expectedResults[0].SubStatusCode, result0.SubStatusCode);
            Assert.AreEqual(expectedResults[0].ETag, result0.ETag);
            Assert.AreEqual(testItem, result0.Resource);

            Assert.AreEqual(expectedResults[1].StatusCode, batchResponse[1].StatusCode);
            Assert.AreEqual(SubStatusCodes.Unknown, batchResponse[1].SubStatusCode);
            Assert.IsNull(batchResponse[1].ETag);
            Assert.IsNull(batchResponse[1].ResourceStream);
        }
Exemplo n.º 5
0
        public async Task ContainerSettingsIndexTest()
        {
            string containerJsonString = "{\"indexingPolicy\":{\"automatic\":true,\"indexingMode\":\"Consistent\",\"includedPaths\":[{\"path\":\"/*\",\"indexes\":[{\"dataType\":\"Number\",\"precision\":-1,\"kind\":\"Range\"},{\"dataType\":\"String\",\"precision\":-1,\"kind\":\"Range\"}]}],\"excludedPaths\":[{\"path\":\"/\\\"_etag\\\"/?\"}],\"compositeIndexes\":[],\"spatialIndexes\":[]},\"id\":\"MigrationTest\",\"partitionKey\":{\"paths\":[\"/id\"],\"kind\":\"Hash\"}}";

            CosmosJsonDotNetSerializer serializerCore      = new CosmosJsonDotNetSerializer();
            ContainerProperties        containerProperties = null;

            using (MemoryStream memory = new MemoryStream(Encoding.UTF8.GetBytes(containerJsonString)))
            {
                containerProperties = serializerCore.FromStream <ContainerProperties>(memory);
            }

            Assert.IsNotNull(containerProperties);
            Assert.AreEqual("MigrationTest", containerProperties.Id);

            string containerJsonAfterConversion = null;

            using (Stream stream = serializerCore.ToStream <ContainerProperties>(containerProperties))
            {
                using (StreamReader sr = new StreamReader(stream))
                {
                    containerJsonAfterConversion = await sr.ReadToEndAsync();
                }
            }

            Assert.AreEqual(containerJsonString, containerJsonAfterConversion);
        }
        public async Task BatchResponseDeserializationAsync()
        {
            List <BatchOperationResult> results = new List <BatchOperationResult>();

            results.Add(new BatchOperationResult(HttpStatusCode.Conflict));

            results.Add(
                new BatchOperationResult(HttpStatusCode.OK)
            {
                ResourceStream = new MemoryStream(new byte[] { 0x41, 0x42 }, index: 0, count: 2, writable: false, publiclyVisible: true),
                ETag           = "1234"
            });

            results.Add(
                new BatchOperationResult((HttpStatusCode)StatusCodes.TooManyRequests)
            {
                RetryAfter = TimeSpan.FromMilliseconds(360)
            });

            MemoryStream responseContent = await new BatchResponsePayloadWriter(results).GeneratePayloadAsync();

            CosmosSerializer serializer = new CosmosJsonDotNetSerializer();
            SinglePartitionKeyServerBatchRequest batchResponse = await SinglePartitionKeyServerBatchRequest.CreateAsync(
                partitionKey : Cosmos.PartitionKey.None,
                operations : new ArraySegment <ItemBatchOperation>(
                    new ItemBatchOperation[]
            {
                new ItemBatchOperation(OperationType.Read, operationIndex: 0, id: "someId")
            }),
                maxBodyLength : 100,
                maxOperationCount : 1,
                serializer : serializer,
                cancellationToken : CancellationToken.None);

            BatchResponse batchresponse = await BatchResponse.PopulateFromContentAsync(
                new ResponseMessage(HttpStatusCode.OK) { Content = responseContent },
                batchResponse,
                serializer);

            Assert.IsNotNull(batchresponse);
            Assert.IsTrue(batchresponse.IsSuccessStatusCode);
            Assert.AreEqual(3, batchresponse.Count);

            CosmosBatchOperationResultEqualityComparer comparer = new CosmosBatchOperationResultEqualityComparer();

            Assert.IsTrue(comparer.Equals(results[0], batchresponse[0]));
            Assert.IsTrue(comparer.Equals(results[1], batchresponse[1]));
            Assert.IsTrue(comparer.Equals(results[2], batchresponse[2]));
        }
        public void ValidateJson()
        {
            CosmosJsonDotNetSerializer cosmosDefaultJsonSerializer = new CosmosJsonDotNetSerializer();

            using (Stream stream = cosmosDefaultJsonSerializer.ToStream <ToDoActivity>(this.toDoActivity))
            {
                Assert.IsNotNull(stream);
                using (StreamReader reader = new StreamReader(stream))
                {
                    string responseAsString = reader.ReadToEnd();
                    Assert.IsNotNull(responseAsString);
                    Assert.AreEqual(this.toDoActivityJson, responseAsString);
                }
            }
        }
        public void ValidateSerializer()
        {
            CosmosJsonDotNetSerializer cosmosDefaultJsonSerializer = new CosmosJsonDotNetSerializer();

            using (Stream stream = cosmosDefaultJsonSerializer.ToStream <ToDoActivity>(this.toDoActivity))
            {
                Assert.IsNotNull(stream);
                ToDoActivity result = cosmosDefaultJsonSerializer.FromStream <ToDoActivity>(stream);
                Assert.IsNotNull(result);
                Assert.AreEqual(this.toDoActivity.id, result.id);
                Assert.AreEqual(this.toDoActivity.taskNum, result.taskNum);
                Assert.AreEqual(this.toDoActivity.cost, result.cost);
                Assert.AreEqual(this.toDoActivity.description, result.description);
                Assert.AreEqual(this.toDoActivity.status, result.status);
            }
        }
        public async Task StreamIteratorTest()
        {
            string containerName    = Guid.NewGuid().ToString();
            string partitionKeyPath = "/users";

            ContainerResponse containerResponse = await this.cosmosDatabase.CreateContainerAsync(containerName, partitionKeyPath);

            Assert.AreEqual(HttpStatusCode.Created, containerResponse.StatusCode);
            Assert.AreEqual(containerName, containerResponse.Resource.Id);
            Assert.AreEqual(partitionKeyPath, containerResponse.Resource.PartitionKey.Paths.First());

            containerName     = Guid.NewGuid().ToString();
            containerResponse = await this.cosmosDatabase.CreateContainerAsync(containerName, partitionKeyPath);

            Assert.AreEqual(HttpStatusCode.Created, containerResponse.StatusCode);
            Assert.AreEqual(containerName, containerResponse.Resource.Id);
            Assert.AreEqual(partitionKeyPath, containerResponse.Resource.PartitionKey.Paths.First());

            HashSet <string> containerIds = new HashSet <string>();
            FeedIterator     resultSet    = this.cosmosDatabase.GetContainerQueryStreamIterator(
                requestOptions: new QueryRequestOptions()
            {
                MaxItemCount = 1
            });

            while (resultSet.HasMoreResults)
            {
                using (ResponseMessage message = await resultSet.ReadNextAsync())
                {
                    Assert.AreEqual(HttpStatusCode.OK, message.StatusCode);
                    CosmosJsonDotNetSerializer defaultJsonSerializer = new CosmosJsonDotNetSerializer();
                    dynamic containers = defaultJsonSerializer.FromStream <dynamic>(message.Content).DocumentCollections;
                    foreach (dynamic container in containers)
                    {
                        string id = container.id.ToString();
                        containerIds.Add(id);
                    }
                }
            }

            Assert.IsTrue(containerIds.Count > 0, "The iterator did not find any containers.");
            Assert.IsTrue(containerIds.Contains(containerName), "The iterator did not find the created container");

            containerResponse = await containerResponse.Container.DeleteContainerAsync();

            Assert.AreEqual(HttpStatusCode.NoContent, containerResponse.StatusCode);
        }
Exemplo n.º 10
0
        public async Task UpdatesLease()
        {
            string itemId = "1";

            Cosmos.PartitionKey      partitionKey  = new Cosmos.PartitionKey("1");
            DocumentServiceLeaseCore leaseToUpdate = new DocumentServiceLeaseCore();

            Stream leaseStream = new CosmosJsonDotNetSerializer().ToStream(leaseToUpdate);

            Mock <ContainerCore> mockedItems = new Mock <ContainerCore>();

            mockedItems.Setup(i => i.ReplaceItemStreamAsync(
                                  It.IsAny <Stream>(),
                                  It.Is <string>((id) => id == itemId),
                                  It.Is <Cosmos.PartitionKey>(pk => pk.Equals(partitionKey)),
                                  It.IsAny <ItemRequestOptions>(),
                                  It.IsAny <CancellationToken>()))
            .ReturnsAsync((Stream stream, string id, PartitionKey pk, ItemRequestOptions options, CancellationToken cancellationToken) =>
            {
                return(new ResponseMessage(HttpStatusCode.OK)
                {
                    Content = stream
                });
            });

            var updater      = new DocumentServiceLeaseUpdaterCosmos(DocumentServiceLeaseUpdaterCosmosTests.GetMockedContainer(mockedItems));
            var updatedLease = await updater.UpdateLeaseAsync(leaseToUpdate, itemId, partitionKey, serverLease =>
            {
                serverLease.Owner = "newHost";
                return(serverLease);
            });

            Assert.AreEqual("newHost", updatedLease.Owner);
            Mock.Get(mockedItems.Object)
            .Verify(items => items.ReplaceItemStreamAsync(
                        It.IsAny <Stream>(),
                        It.Is <string>((id) => id == itemId),
                        It.Is <Cosmos.PartitionKey>(pk => pk.Equals(partitionKey)),
                        It.IsAny <ItemRequestOptions>(),
                        It.IsAny <CancellationToken>()), Times.Once);
            Mock.Get(mockedItems.Object)
            .Verify(items => items.ReadItemStreamAsync(
                        It.Is <string>((id) => id == itemId),
                        It.Is <Cosmos.PartitionKey>((pk) => pk.Equals(partitionKey)),
                        It.IsAny <ItemRequestOptions>(),
                        It.IsAny <CancellationToken>()), Times.Never);
        }
        public void GetCosmosSerializerWithWrapperOrDefaultTest()
        {
            CosmosJsonDotNetSerializer serializer = new CosmosJsonDotNetSerializer();
            CosmosClientOptions        options    = new CosmosClientOptions()
            {
                Serializer = serializer
            };

            CosmosSerializer cosmosSerializer = options.GetCosmosSerializerWithWrapperOrDefault();

            Assert.AreNotEqual(cosmosSerializer, options.PropertiesSerializer, "Serializer should be custom not the default");
            Assert.AreNotEqual(cosmosSerializer, serializer, "Serializer should be in the CosmosJsonSerializerWrapper");

            CosmosJsonSerializerWrapper cosmosJsonSerializerWrapper = cosmosSerializer as CosmosJsonSerializerWrapper;

            Assert.IsNotNull(cosmosJsonSerializerWrapper);
            Assert.AreEqual(cosmosJsonSerializerWrapper.InternalJsonSerializer, serializer);
        }
Exemplo n.º 12
0
        public async Task BatchResponseDeserializationAsync()
        {
            List <TransactionalBatchOperationResult> results = new List <TransactionalBatchOperationResult>();

            results.Add(new TransactionalBatchOperationResult(HttpStatusCode.Conflict));

            results.Add(
                new TransactionalBatchOperationResult(HttpStatusCode.OK)
            {
                ResourceStream = new MemoryStream(new byte[] { 0x41, 0x42 }, index: 0, count: 2, writable: false, publiclyVisible: true),
                RequestCharge  = 2.5,
                ETag           = "1234",
                RetryAfter     = TimeSpan.FromMilliseconds(360)
            });

            MemoryStream responseContent = await new BatchResponsePayloadWriter(results).GeneratePayloadAsync();

            CosmosSerializer serializer = new CosmosJsonDotNetSerializer();
            SinglePartitionKeyServerBatchRequest batchRequest = await SinglePartitionKeyServerBatchRequest.CreateAsync(
                partitionKey : Cosmos.PartitionKey.None,
                operations : new ArraySegment <ItemBatchOperation>(
                    new ItemBatchOperation[]
            {
                new ItemBatchOperation(OperationType.Read, operationIndex: 0, id: "someId"),
                new ItemBatchOperation(OperationType.Read, operationIndex: 0, id: "someId")
            }),
                serializerCore : MockCosmosUtil.Serializer,
                cancellationToken : CancellationToken.None);

            TransactionalBatchResponse batchResponse = await TransactionalBatchResponse.FromResponseMessageAsync(
                new ResponseMessage((HttpStatusCode)StatusCodes.MultiStatus) { Content = responseContent },
                batchRequest,
                MockCosmosUtil.Serializer);

            Assert.IsNotNull(batchRequest);
            Assert.AreEqual(HttpStatusCode.Conflict, batchResponse.StatusCode);
            Assert.AreEqual(2, batchResponse.Count);

            CosmosBatchOperationResultEqualityComparer comparer = new CosmosBatchOperationResultEqualityComparer();

            Assert.IsTrue(comparer.Equals(results[0], batchResponse[0]));
            Assert.IsTrue(comparer.Equals(results[1], batchResponse[1]));
        }
        /// <summary>
        /// Add our Implementation of CosmosDB to the startup of an ASP.NET app.
        /// </summary>
        /// <remarks>
        /// Requires a config value for DocumentDBUrl and DocumentDBKey
        /// </remarks>
        /// <param name="services"></param>
        public static void UseCosmosDb(this IServiceCollection services, IConfiguration configuration)
        {
            RegisterServices(services, configuration);
            //Register the document client from the config
            services.AddSingleton(ctx =>
            {
                var serializerSettings = new JsonSerializerSettings()
                {
                    ContractResolver = new DefaultContractResolver(),
                    TypeNameHandling = TypeNameHandling.Objects,
                    Converters       = new List <JsonConverter>()
                    {
                        new StringEnumConverter()
                    }
                };

                //Serialize enums as string using default naming strategy (unchanged)
                serializerSettings.Converters.Add(new StringEnumConverter()
                {
                    NamingStrategy = new DefaultNamingStrategy()
                });

                var serializer = new CosmosJsonDotNetSerializer(serializerSettings);

                var config = ctx.GetService <IOptionsMonitor <CosmosDbConfiguration> >();

                if (string.IsNullOrEmpty(config.CurrentValue.ConnectionString))
                {
                    throw new InvalidOperationException("Missing required DocumentDB Configuration");
                }

                return(new CosmosClient(config.CurrentValue.ConnectionString, new CosmosClientOptions()
                {
                    Serializer = serializer
                }));
            });
        }
        public async Task ContainerContractTest()
        {
            ClientEncryptionIncludedPath clientEncryptionIncludedPath1 = new ClientEncryptionIncludedPath()
            {
                Path = "/path",
                ClientEncryptionKeyId = "dekId",
                EncryptionAlgorithm   = "AEAD_AES_256_CBC_HMAC_SHA256",
                EncryptionType        = "Randomized"
            };

            Collection <ClientEncryptionIncludedPath> paths = new Collection <ClientEncryptionIncludedPath>()
            {
                clientEncryptionIncludedPath1
            };

            ContainerProperties containerProperties = new ContainerProperties(Guid.NewGuid().ToString(), "/users")
            {
                IndexingPolicy = new IndexingPolicy()
                {
                    Automatic     = true,
                    IndexingMode  = IndexingMode.Consistent,
                    IncludedPaths = new Collection <IncludedPath>()
                    {
                        new IncludedPath()
                        {
                            Path = "/*"
                        }
                    },
                    ExcludedPaths = new Collection <ExcludedPath>()
                    {
                        new ExcludedPath()
                        {
                            Path = "/test/*"
                        }
                    },
                    CompositeIndexes = new Collection <Collection <CompositePath> >()
                    {
                        new Collection <CompositePath>()
                        {
                            new CompositePath()
                            {
                                Path  = "/address/city",
                                Order = CompositePathSortOrder.Ascending
                            },
                            new CompositePath()
                            {
                                Path  = "/address/zipcode",
                                Order = CompositePathSortOrder.Descending
                            }
                        }
                    },
                    SpatialIndexes = new Collection <SpatialPath>()
                    {
                        new SpatialPath()
                        {
                            Path         = "/address/spatial/*",
                            SpatialTypes = new Collection <SpatialType>()
                            {
                                SpatialType.LineString
                            }
                        }
                    }
                },
                ClientEncryptionPolicy = new ClientEncryptionPolicy(paths)
            };

            CosmosJsonDotNetSerializer serializer = new CosmosJsonDotNetSerializer();
            Stream stream = serializer.ToStream(containerProperties);
            ContainerProperties deserialziedTest = serializer.FromStream <ContainerProperties>(stream);

            ContainerResponse response = await this.database.CreateContainerAsync(containerProperties);

            Assert.IsNotNull(response);
            Assert.IsTrue(response.RequestCharge > 0);
            Assert.IsNotNull(response.Headers);
            Assert.IsNotNull(response.Headers.ActivityId);

            ContainerProperties responseProperties = response.Resource;

            Assert.IsNotNull(responseProperties.Id);
            Assert.IsNotNull(responseProperties.ResourceId);
            Assert.IsNotNull(responseProperties.ETag);
            Assert.IsTrue(responseProperties.LastModified.HasValue);

            Assert.IsTrue(responseProperties.LastModified.Value > new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc), responseProperties.LastModified.Value.ToString());

            Assert.AreEqual(1, responseProperties.IndexingPolicy.IncludedPaths.Count);
            IncludedPath includedPath = responseProperties.IndexingPolicy.IncludedPaths.First();

            Assert.AreEqual("/*", includedPath.Path);

            Assert.AreEqual("/test/*", responseProperties.IndexingPolicy.ExcludedPaths.First().Path);

            Assert.AreEqual(1, responseProperties.IndexingPolicy.CompositeIndexes.Count);
            Assert.AreEqual(2, responseProperties.IndexingPolicy.CompositeIndexes.First().Count);
            CompositePath compositePath = responseProperties.IndexingPolicy.CompositeIndexes.First().First();

            Assert.AreEqual("/address/city", compositePath.Path);
            Assert.AreEqual(CompositePathSortOrder.Ascending, compositePath.Order);

            Assert.AreEqual(1, responseProperties.IndexingPolicy.SpatialIndexes.Count);
            SpatialPath spatialPath = responseProperties.IndexingPolicy.SpatialIndexes.First();

            Assert.AreEqual("/address/spatial/*", spatialPath.Path);
            Assert.AreEqual(4, spatialPath.SpatialTypes.Count); // All SpatialTypes are returned

            Assert.AreEqual(1, responseProperties.ClientEncryptionPolicy.IncludedPaths.Count());
            Assert.AreEqual(1, responseProperties.ClientEncryptionPolicy.PolicyFormatVersion);
            ClientEncryptionIncludedPath clientEncryptionIncludedPath = responseProperties.ClientEncryptionPolicy.IncludedPaths.First();

            Assert.IsTrue(this.VerifyClientEncryptionIncludedPath(clientEncryptionIncludedPath1, clientEncryptionIncludedPath));
        }
        public void ValidateSqlQuerySpecSerializer()
        {
            List <SqlQuerySpec> sqlQuerySpecs = new List <SqlQuerySpec>();

            sqlQuerySpecs.Add(new SqlQuerySpec()
            {
                QueryText = "SELECT root._rid, [{\"item\": root[\"NumberField\"]}] AS orderByItems, root AS payload\nFROM root\nWHERE (true)\nORDER BY root[\"NumberField\"] DESC"
            });

            sqlQuerySpecs.Add(new SqlQuerySpec()
            {
                QueryText = "Select * from something"
            });

            sqlQuerySpecs.Add(new SqlQuerySpec()
            {
                QueryText  = "Select * from something",
                Parameters = new SqlParameterCollection()
            });

            SqlParameterCollection sqlParameters = new SqlParameterCollection();

            sqlParameters.Add(new SqlParameter("@id", "test1"));

            sqlQuerySpecs.Add(new SqlQuerySpec()
            {
                QueryText  = "Select * from something",
                Parameters = sqlParameters
            });

            sqlParameters = new SqlParameterCollection();
            sqlParameters.Add(new SqlParameter("@id", "test2"));
            sqlParameters.Add(new SqlParameter("@double", 42.42));
            sqlParameters.Add(new SqlParameter("@int", 9001));
            sqlParameters.Add(new SqlParameter("@null", null));
            sqlParameters.Add(new SqlParameter("@datetime", DateTime.UtcNow));

            sqlQuerySpecs.Add(new SqlQuerySpec()
            {
                QueryText  = "Select * from something",
                Parameters = sqlParameters
            });

            CosmosJsonDotNetSerializer userSerializer       = new CosmosJsonDotNetSerializer();
            CosmosJsonDotNetSerializer propertiesSerializer = new CosmosJsonDotNetSerializer();

            CosmosSerializer sqlQuerySpecSerializer = CosmosSqlQuerySpecJsonConverter.CreateSqlQuerySpecSerializer(
                userSerializer,
                propertiesSerializer);

            foreach (SqlQuerySpec sqlQuerySpec in sqlQuerySpecs)
            {
                Stream stream = propertiesSerializer.ToStream <SqlQuerySpec>(sqlQuerySpec);
                string result1;
                using (StreamReader sr = new StreamReader(stream))
                {
                    result1 = sr.ReadToEnd();
                    Assert.IsNotNull(result1);
                }

                stream = sqlQuerySpecSerializer.ToStream(sqlQuerySpec);
                string result2;
                using (StreamReader sr = new StreamReader(stream))
                {
                    result2 = sr.ReadToEnd();
                    Assert.IsNotNull(result2);
                }

                Assert.AreEqual(result1, result2);
            }
        }
Exemplo n.º 16
0
        public async Task ContainerContractTest()
        {
            ContainerProperties containerProperties = new ContainerProperties(Guid.NewGuid().ToString(), "/users")
            {
                IndexingPolicy = new IndexingPolicy()
                {
                    Automatic     = true,
                    IndexingMode  = IndexingMode.Consistent,
                    IncludedPaths = new Collection <IncludedPath>()
                    {
                        new IncludedPath()
                        {
                            Path = "/*"
                        }
                    },
                    ExcludedPaths = new Collection <ExcludedPath>()
                    {
                        new ExcludedPath()
                        {
                            Path = "/test/*"
                        }
                    },
                    CompositeIndexes = new Collection <Collection <CompositePath> >()
                    {
                        new Collection <CompositePath>()
                        {
                            new CompositePath()
                            {
                                Path  = "/address/city",
                                Order = CompositePathSortOrder.Ascending
                            },
                            new CompositePath()
                            {
                                Path  = "/address/zipcode",
                                Order = CompositePathSortOrder.Descending
                            }
                        }
                    },
                    SpatialIndexes = new Collection <SpatialPath>()
                    {
                        new SpatialPath()
                        {
                            Path         = "/address/spatial/*",
                            SpatialTypes = new Collection <SpatialType>()
                            {
                                SpatialType.LineString
                            }
                        }
                    }
                }
            };

            var    serializer = new CosmosJsonDotNetSerializer();
            Stream stream     = serializer.ToStream(containerProperties);
            ContainerProperties deserialziedTest = serializer.FromStream <ContainerProperties>(stream);

            ContainerResponse response = await this.database.CreateContainerAsync(containerProperties);

            Assert.IsNotNull(response);
            Assert.IsTrue(response.RequestCharge > 0);
            Assert.IsNotNull(response.Headers);
            Assert.IsNotNull(response.Headers.ActivityId);

            ContainerProperties responseProperties = response.Resource;

            Assert.IsNotNull(responseProperties.Id);
            Assert.IsNotNull(responseProperties.ResourceId);
            Assert.IsNotNull(responseProperties.ETag);
            Assert.IsTrue(responseProperties.LastModified.HasValue);

            Assert.IsTrue(responseProperties.LastModified.Value > new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc), responseProperties.LastModified.Value.ToString());

            Assert.AreEqual(1, responseProperties.IndexingPolicy.IncludedPaths.Count);
            IncludedPath includedPath = responseProperties.IndexingPolicy.IncludedPaths.First();

            Assert.AreEqual("/*", includedPath.Path);

            Assert.AreEqual("/test/*", responseProperties.IndexingPolicy.ExcludedPaths.First().Path);

            Assert.AreEqual(1, responseProperties.IndexingPolicy.CompositeIndexes.Count);
            Assert.AreEqual(2, responseProperties.IndexingPolicy.CompositeIndexes.First().Count);
            CompositePath compositePath = responseProperties.IndexingPolicy.CompositeIndexes.First().First();

            Assert.AreEqual("/address/city", compositePath.Path);
            Assert.AreEqual(CompositePathSortOrder.Ascending, compositePath.Order);

            Assert.AreEqual(1, responseProperties.IndexingPolicy.SpatialIndexes.Count);
            SpatialPath spatialPath = responseProperties.IndexingPolicy.SpatialIndexes.First();

            Assert.AreEqual("/address/spatial/*", spatialPath.Path);
            Assert.AreEqual(1, spatialPath.SpatialTypes.Count);
            Assert.AreEqual(SpatialType.LineString, spatialPath.SpatialTypes.First());
        }
Exemplo n.º 17
0
        public async Task ContainerV2CompatTest()
        {
            string             containerId        = "SerializeContainerTest";
            DocumentCollection documentCollection = new DocumentCollection()
            {
                Id           = containerId,
                PartitionKey = new PartitionKeyDefinition()
                {
                    Paths = new Collection <string>()
                    {
                        "/pkPath"
                    }
                },
                IndexingPolicy = new IndexingPolicy()
                {
                    IncludedPaths = new Collection <IncludedPath>()
                    {
                        new IncludedPath()
                        {
                            Path = "/*"
                        }
                    },
                    CompositeIndexes = new Collection <Collection <CompositePath> >()
                    {
                        new Collection <CompositePath>()
                        {
                            new CompositePath()
                            {
                                Path  = "/address/test/*",
                                Order = CompositePathSortOrder.Ascending
                            },
                            new CompositePath()
                            {
                                Path  = "/address/test2/*",
                                Order = CompositePathSortOrder.Ascending
                            }
                        }
                    },
                    SpatialIndexes = new Collection <SpatialSpec>()
                    {
                        new SpatialSpec()
                        {
                            Path         = "/name/first/*",
                            SpatialTypes = new Collection <SpatialType>()
                            {
                                SpatialType.LineString
                            }
                        }
                    }
                },
            };


            string documentJsonString = null;

            using (MemoryStream memoryStream = new MemoryStream())
            {
                documentCollection.SaveTo(memoryStream);
                memoryStream.Position = 0;
                using (StreamReader sr = new StreamReader(memoryStream))
                {
                    documentJsonString = await sr.ReadToEndAsync();
                }
            }

            Assert.IsNotNull(documentJsonString);

            string cosmosJsonString = null;

            using (MemoryStream memoryStream = new MemoryStream())
            {
                documentCollection.SaveTo(memoryStream);
                memoryStream.Position = 0;

                CosmosJsonDotNetSerializer serializerCore      = new CosmosJsonDotNetSerializer();
                ContainerProperties        containerProperties = serializerCore.FromStream <ContainerProperties>(memoryStream);

                Assert.IsNotNull(containerProperties);
                Assert.AreEqual(containerId, containerProperties.Id);

                using (Stream stream = serializerCore.ToStream <ContainerProperties>(containerProperties))
                {
                    using (StreamReader sr = new StreamReader(stream))
                    {
                        cosmosJsonString = await sr.ReadToEndAsync();
                    }
                }
            }

            JObject jObjectDocumentCollection = JObject.Parse(documentJsonString);
            JObject jObjectContainer          = JObject.Parse(cosmosJsonString);

            Assert.IsTrue(JToken.DeepEquals(jObjectDocumentCollection, jObjectContainer), $"v2:{documentJsonString}; v3:{cosmosJsonString}");
        }
Exemplo n.º 18
0
        public async Task BatchCrudRequestAsync()
        {
            Random random = new Random();

            TestItem createItem = new TestItem("create");

            byte[] createStreamContent = new byte[20];
            random.NextBytes(createStreamContent);
            byte[] createStreamBinaryId = new byte[20];
            random.NextBytes(createStreamBinaryId);
            int createTtl = 45;
            TransactionalBatchItemRequestOptions createRequestOptions = new TransactionalBatchItemRequestOptions()
            {
                Properties = new Dictionary <string, object>()
                {
                    { WFConstants.BackendHeaders.BinaryId, createStreamBinaryId },
                    { WFConstants.BackendHeaders.TimeToLiveInSeconds, createTtl.ToString() },
                },
                IndexingDirective = Microsoft.Azure.Cosmos.IndexingDirective.Exclude
            };

            string readId = Guid.NewGuid().ToString();

            byte[] readStreamBinaryId = new byte[20];
            random.NextBytes(readStreamBinaryId);
            TransactionalBatchItemRequestOptions readRequestOptions = new TransactionalBatchItemRequestOptions()
            {
                Properties = new Dictionary <string, object>()
                {
                    { WFConstants.BackendHeaders.BinaryId, readStreamBinaryId }
                },
                IfNoneMatchEtag = "readCondition"
            };

            TestItem replaceItem = new TestItem("repl");

            byte[] replaceStreamContent = new byte[20];
            random.NextBytes(replaceStreamContent);
            const string replaceStreamId = "replStream";

            byte[] replaceStreamBinaryId = new byte[20];
            random.NextBytes(replaceStreamBinaryId);
            TransactionalBatchItemRequestOptions replaceRequestOptions = new TransactionalBatchItemRequestOptions()
            {
                Properties = new Dictionary <string, object>()
                {
                    { WFConstants.BackendHeaders.BinaryId, replaceStreamBinaryId }
                },
                IfMatchEtag       = "replCondition",
                IndexingDirective = Microsoft.Azure.Cosmos.IndexingDirective.Exclude
            };

            TestItem upsertItem = new TestItem("upsert");

            byte[] upsertStreamContent = new byte[20];
            random.NextBytes(upsertStreamContent);
            byte[] upsertStreamBinaryId = new byte[20];
            random.NextBytes(upsertStreamBinaryId);
            TransactionalBatchItemRequestOptions upsertRequestOptions = new TransactionalBatchItemRequestOptions()
            {
                Properties = new Dictionary <string, object>()
                {
                    { WFConstants.BackendHeaders.BinaryId, upsertStreamBinaryId }
                },
                IfMatchEtag       = "upsertCondition",
                IndexingDirective = Microsoft.Azure.Cosmos.IndexingDirective.Exclude
            };

            string deleteId = Guid.NewGuid().ToString();

            byte[] deleteStreamBinaryId = new byte[20];
            random.NextBytes(deleteStreamBinaryId);
            TransactionalBatchItemRequestOptions deleteRequestOptions = new TransactionalBatchItemRequestOptions()
            {
                Properties = new Dictionary <string, object>()
                {
                    { WFConstants.BackendHeaders.BinaryId, deleteStreamBinaryId }
                },
                IfNoneMatchEtag = "delCondition"
            };

            CosmosJsonDotNetSerializer jsonSerializer = new CosmosJsonDotNetSerializer();
            BatchTestHandler           testHandler    = new BatchTestHandler((request, operations) =>
            {
                Assert.AreEqual(new Cosmos.PartitionKey(BatchUnitTests.PartitionKey1).ToString(), request.Headers.PartitionKey);
                Assert.AreEqual(bool.TrueString, request.Headers[HttpConstants.HttpHeaders.IsBatchAtomic]);
                Assert.AreEqual(bool.TrueString, request.Headers[HttpConstants.HttpHeaders.IsBatchOrdered]);
                Assert.IsFalse(request.Headers.TryGetValue(HttpConstants.HttpHeaders.ShouldBatchContinueOnError, out string unused));

                Assert.AreEqual(16, operations.Count);

                int operationIndex = 0;

                // run the loop twice, once for operations without item request options, and one for with item request options
                for (int loopCount = 0; loopCount < 2; loopCount++)
                {
                    bool hasItemRequestOptions = loopCount == 1;

                    ItemBatchOperation operation = operations[operationIndex++];
                    Assert.AreEqual(OperationType.Create, operation.OperationType);
                    Assert.IsNull(operation.Id);
                    Assert.AreEqual(createItem, BatchUnitTests.Deserialize(operation.ResourceBody, jsonSerializer));
                    BatchUnitTests.VerifyBatchItemRequestOptionsAreEqual(hasItemRequestOptions ? createRequestOptions : null, operation.RequestOptions);

                    operation = operations[operationIndex++];
                    Assert.AreEqual(OperationType.Read, operation.OperationType);
                    Assert.AreEqual(readId, operation.Id);
                    BatchUnitTests.VerifyBatchItemRequestOptionsAreEqual(hasItemRequestOptions ? readRequestOptions : null, operation.RequestOptions);

                    operation = operations[operationIndex++];
                    Assert.AreEqual(OperationType.Replace, operation.OperationType);
                    Assert.AreEqual(replaceItem.Id, operation.Id);
                    Assert.AreEqual(replaceItem, BatchUnitTests.Deserialize(operation.ResourceBody, jsonSerializer));
                    BatchUnitTests.VerifyBatchItemRequestOptionsAreEqual(hasItemRequestOptions ? replaceRequestOptions : null, operation.RequestOptions);

                    operation = operations[operationIndex++];
                    Assert.AreEqual(OperationType.Upsert, operation.OperationType);
                    Assert.IsNull(operation.Id);
                    Assert.AreEqual(upsertItem, BatchUnitTests.Deserialize(operation.ResourceBody, jsonSerializer));
                    BatchUnitTests.VerifyBatchItemRequestOptionsAreEqual(hasItemRequestOptions ? upsertRequestOptions : null, operation.RequestOptions);

                    operation = operations[operationIndex++];
                    Assert.AreEqual(OperationType.Delete, operation.OperationType);
                    Assert.AreEqual(deleteId, operation.Id);
                    BatchUnitTests.VerifyBatchItemRequestOptionsAreEqual(hasItemRequestOptions ? deleteRequestOptions : null, operation.RequestOptions);

                    operation = operations[operationIndex++];
                    Assert.AreEqual(OperationType.Create, operation.OperationType);
                    Assert.IsNull(operation.Id);
                    Assert.IsTrue(operation.ResourceBody.Span.SequenceEqual(createStreamContent));
                    BatchUnitTests.VerifyBatchItemRequestOptionsAreEqual(hasItemRequestOptions ? createRequestOptions : null, operation.RequestOptions);

                    operation = operations[operationIndex++];
                    Assert.AreEqual(OperationType.Replace, operation.OperationType);
                    Assert.AreEqual(replaceStreamId, operation.Id);
                    Assert.IsTrue(operation.ResourceBody.Span.SequenceEqual(replaceStreamContent));
                    BatchUnitTests.VerifyBatchItemRequestOptionsAreEqual(hasItemRequestOptions ? replaceRequestOptions : null, operation.RequestOptions);

                    operation = operations[operationIndex++];
                    Assert.AreEqual(OperationType.Upsert, operation.OperationType);
                    Assert.IsNull(operation.Id);
                    Assert.IsTrue(operation.ResourceBody.Span.SequenceEqual(upsertStreamContent));
                    BatchUnitTests.VerifyBatchItemRequestOptionsAreEqual(hasItemRequestOptions ? upsertRequestOptions : null, operation.RequestOptions);
                }

                return(Task.FromResult(new ResponseMessage(HttpStatusCode.OK)));
            });

            Container container = BatchUnitTests.GetContainer(testHandler);

            TransactionalBatchResponse batchResponse = await new BatchCore((ContainerInternal)container, new Cosmos.PartitionKey(BatchUnitTests.PartitionKey1))
                                                       .CreateItem(createItem)
                                                       .ReadItem(readId)
                                                       .ReplaceItem(replaceItem.Id, replaceItem)
                                                       .UpsertItem(upsertItem)
                                                       .DeleteItem(deleteId)

                                                       // stream
                                                       .CreateItemStream(new MemoryStream(createStreamContent))
                                                       .ReplaceItemStream(replaceStreamId, new MemoryStream(replaceStreamContent))
                                                       .UpsertItemStream(new MemoryStream(upsertStreamContent))

                                                       // regular with options
                                                       .CreateItem(createItem, createRequestOptions)
                                                       .ReadItem(readId, readRequestOptions)
                                                       .ReplaceItem(replaceItem.Id, replaceItem, replaceRequestOptions)
                                                       .UpsertItem(upsertItem, upsertRequestOptions)
                                                       .DeleteItem(deleteId, deleteRequestOptions)

                                                       // stream with options
                                                       .CreateItemStream(new MemoryStream(createStreamContent), createRequestOptions)
                                                       .ReplaceItemStream(replaceStreamId, new MemoryStream(replaceStreamContent), replaceRequestOptions)
                                                       .UpsertItemStream(new MemoryStream(upsertStreamContent), upsertRequestOptions)
                                                       .ExecuteAsync();
        }