Exemplo n.º 1
0
        private static async Task TestRepository(IChasmRepository repository)
        {
            var g  = Guid.NewGuid();
            var cx = new ChasmRequestContext {
                CorrelationId = Guid.NewGuid().ToString("D")
            };

            byte[] buffer = g.ToByteArray();
            Sha1   sha    = s_hasher.HashData(buffer);

            // Unknown SHA
            Sha1 usha1 = s_hasher.HashData(Guid.NewGuid().ToByteArray());
            Sha1 usha2 = s_hasher.HashData(Guid.NewGuid().ToByteArray());

            // Blob
            var  metadata = new ChasmMetadata("application/json", "file123.txt");
            Sha1 sha2     = await repository.WriteObjectAsync(new ReadOnlyMemory <byte>(buffer), metadata, false, cx, default);

            Assert.Equal(sha, sha2);
            IChasmBlob rdata = await repository.ReadObjectAsync(sha, default);

            Assert.True(rdata != null);
            Assert.Equal(16, rdata.Content.Length);
            Assert.Equal(g, new Guid(rdata.Content.ToArray()));
            Assert.Equal("application/json", rdata.Metadata.ContentType);
            Assert.Equal("file123.txt", rdata.Metadata.Filename);

            IChasmBlob urdata = await repository.ReadObjectAsync(usha1, default);

            Assert.False(urdata != null);

            //
            metadata = new ChasmMetadata("application/text", null);
            sha2     = await repository.WriteObjectAsync(buffer, metadata, true, cx, default);

            Assert.Equal(sha, sha2);

            IChasmBlob cnt2 = await repository.ReadObjectAsync(sha2, default);

            Assert.Equal(buffer, cnt2.Content.ToArray());
            Assert.Equal("application/text", cnt2.Metadata.ContentType);
            Assert.Null(cnt2.Metadata.Filename);

            using (IChasmStream stream2 = await repository.ReadStreamAsync(sha2, cx, default))
                using (var ms = new MemoryStream())
                {
                    stream2.Content.CopyTo(ms);
                    Assert.Equal(buffer, ms.ToArray());
                }

            using (Stream stream2 = new MemoryStream(buffer))
            {
                sha2 = await repository.WriteObjectAsync(stream2, null, true, cx, default);
            }
            Assert.Equal(sha, sha2);

            using (IChasmStream stream2 = await repository.ReadStreamAsync(sha2, cx, default))
                using (var ms = new MemoryStream())
                {
                    stream2.Content.CopyTo(ms);
                    Assert.Equal(buffer, ms.ToArray());
                }

            // Tree
            var tree = new TreeNodeMap(
                new TreeNode("firstItem", NodeKind.Blob, sha),
                new TreeNode("secondItem", NodeKind.Blob, sha)
                );
            TreeId treeId = await repository.WriteTreeAsync(tree, cx, default);

            TreeNodeMap?rtree = await repository.ReadTreeAsync(treeId, cx, default);

            Assert.True(rtree.HasValue);
            Assert.Equal(tree, rtree.Value);

            TreeNodeMap?urtree = await repository.ReadTreeAsync(new TreeId(usha1), cx, default);

            Assert.False(urtree.HasValue);
        }