Exemple #1
0
        public void Test_Deserialize_DoesNotThrow()
        {
            // Arrange
            Mock.Get(internalSerializerMock)
            .SetReturnsDefault(Task.FromResult(string.Empty));

            // Act & Assert
            Assert.DoesNotThrowAsync(
                () => serializer.DeserializeAsync <dynamic>(new MemoryStream())
                );
        }
Exemple #2
0
        /// <summary>Receives a document from its persistence.</summary>
        /// <typeparam name="TDocument">A type of a document.</typeparam>
        /// <param name="id">A value that represents an ID of a document.</param>
        /// <param name="partitionId">A value that represents a partition ID of a document.</param>
        /// <param name="cancellationToken">A value that propagates notification that operations should be canceled.</param>
        /// <returns>An object that represents an async operation.</returns>
        public async Task <TDocument> FirstOrDefaultAsync <TDocument>(
            Guid id, string partitionId, CancellationToken cancellationToken)
            where TDocument : DocumentBase
        {
            using (var responseMessage = await _container.ReadItemStreamAsync(id.ToString(), new PartitionKey(partitionId), null, cancellationToken))
            {
                TDocument document = null;

                if (responseMessage.StatusCode != HttpStatusCode.NotFound)
                {
                    responseMessage.EnsureSuccessStatusCode();

                    document = await _serializer.DeserializeAsync <TDocument>(
                        responseMessage.Content, cancellationToken);
                }

                return(document);
            }
        }
Exemple #3
0
        public async Task Test_Roundtrip_DataEquals()
        {
            // Arrange
            var testData = "ABC";
            var stream   = new MemoryStream();

            // Act
            await serializer.SerializeAsync(stream, testData);

            stream.Position = 0;
            var roundtrippedData = await serializer.DeserializeAsync <string>(stream);

            // Assert
            Assert.AreEqual(testData, roundtrippedData);
        }
        async Task <T> IDocumentSerializer.DeserializeAsync <T>(Stream stream) where T : class
        {
            if (stream is null)
            {
                throw new System.ArgumentNullException(nameof(stream));
            }

            using var rijndael = Rijndael.Create();
            var encryptor = rijndael.CreateDecryptor(options.Key.ToArray(), options.IV.ToArray());

            using var cryptoStream = new CryptoStream(stream, encryptor, CryptoStreamMode.Read);

            try
            {
                return(await serializer.DeserializeAsync <T>(cryptoStream).ConfigureAwait(false));
            }
            catch (CryptographicException ex)
            {
                throw new SerializationException("Decryption failed. Make sure to use correct key.", ex);
            }
        }