Example #1
0
        public async Task ItemQueryStreamSerializationSetting()
        {
            IList <ToDoActivity> deleteList = await this.CreateRandomItems(101, randomPartitionKey : true);

            CosmosSqlQueryDefinition   sql     = new CosmosSqlQueryDefinition("SELECT * FROM toDoActivity t ORDER BY t.taskNum");
            CosmosSerializationOptions options = new CosmosSerializationOptions(
                ContentSerializationFormat.CosmosBinary.ToString(),
                (content) => JsonNavigator.Create(content),
                () => JsonWriter.Create(JsonSerializationFormat.Binary));

            CosmosQueryRequestOptions requestOptions = new CosmosQueryRequestOptions()
            {
                CosmosSerializationOptions = options
            };

            List <ToDoActivity> resultList      = new List <ToDoActivity>();
            double totalRequstCharge            = 0;
            CosmosResultSetIterator setIterator =
                this.Container.Items.CreateItemQueryAsStream(sql, maxConcurrency: 5, maxItemCount: 5, requestOptions: requestOptions);

            while (setIterator.HasMoreResults)
            {
                using (CosmosQueryResponse iter = await setIterator.FetchNextSetAsync())
                {
                    Assert.IsTrue(iter.IsSuccess);
                    Assert.IsNull(iter.ErrorMessage);
                    Assert.IsTrue(iter.Count <= 5);
                    totalRequstCharge += iter.RequestCharge;
                    IJsonReader reader     = JsonReader.Create(iter.Content);
                    IJsonWriter textWriter = JsonWriter.Create(JsonSerializationFormat.Text);
                    textWriter.WriteAll(reader);
                    string json = Encoding.UTF8.GetString(textWriter.GetResult());
                    Assert.IsNotNull(json);
                    ToDoActivity[] responseActivities = JsonConvert.DeserializeObject <ToDoActivity[]>(json);
                    resultList.AddRange(responseActivities);
                }
            }

            Assert.AreEqual(deleteList.Count, resultList.Count);
            Assert.IsTrue(totalRequstCharge > 0);

            List <ToDoActivity> verifiedOrderBy = deleteList.OrderBy(x => x.taskNum).ToList();

            for (int i = 0; i < verifiedOrderBy.Count(); i++)
            {
                Assert.AreEqual(verifiedOrderBy[i].taskNum, resultList[i].taskNum);
                Assert.AreEqual(verifiedOrderBy[i].id, resultList[i].id);
            }
        }
Example #2
0
        public async Task DecryptQueryBinaryResponse()
        {
            TestDoc testDoc = await EncryptionTests.CreateItemAsync(EncryptionTests.containerCore, EncryptionTests.dekId, TestDoc.PathsToEncrypt);

            CosmosSerializationFormatOptions options = new CosmosSerializationFormatOptions(
                Documents.ContentSerializationFormat.CosmosBinary.ToString(),
                (content) => JsonNavigator.Create(content),
                () => JsonWriter.Create(JsonSerializationFormat.Binary));

            QueryRequestOptions requestOptions = new QueryRequestOptions()
            {
                CosmosSerializationFormatOptions = options
            };

            TestDoc expectedDoc = new TestDoc(testDoc);

            string query = "SELECT * FROM c";

            FeedIterator feedIterator = EncryptionTests.containerCore.GetItemQueryStreamIterator(
                query,
                requestOptions: requestOptions);

            while (feedIterator.HasMoreResults)
            {
                ResponseMessage response = await feedIterator.ReadNextAsync();

                Assert.IsTrue(response.IsSuccessStatusCode);
                Assert.IsNull(response.ErrorMessage);

                // Copy the stream and check that the first byte is the correct value
                MemoryStream memoryStream = new MemoryStream();
                response.Content.CopyTo(memoryStream);
                byte[] content = memoryStream.ToArray();
                response.Content.Position = 0;

                // Examine the first buffer byte to determine the serialization format
                byte firstByte = content[0];
                Assert.AreEqual(128, firstByte);
                Assert.AreEqual(JsonSerializationFormat.Binary, (JsonSerializationFormat)firstByte);

                IJsonReader reader     = JsonReader.Create(content);
                IJsonWriter textWriter = JsonWriter.Create(JsonSerializationFormat.Text);
                textWriter.WriteAll(reader);
                string json = Encoding.UTF8.GetString(textWriter.GetResult().ToArray());
                Assert.IsNotNull(json);
                Assert.IsTrue(json.Contains(testDoc.Sensitive));
            }
        }