Exemplo n.º 1
0
        public void GivenTestData_WhenBuildPackage_ShouldBeAbleToReadManifest()
        {
            string specFile    = new TestResources().WriteTestData_1();
            string buildFolder = Path.Combine(Path.GetTempPath(), nameof(nBlog), "build", Guid.NewGuid().ToString());

            string packageFile = new ArticlePackageBuilder()
                                 .SetSpecFile(specFile)
                                 .SetBuildFolder(buildFolder)
                                 .SetObjFolder(Path.Combine(buildFolder, "obj"))
                                 .Build();

            byte[]         fileBytes      = File.ReadAllBytes(packageFile);
            ArticlePayload articlePayload = fileBytes.ToArticlePayload((ArticleId)"id");

            ArticleManifest articleManifest = articlePayload.ReadManifest();

            articleManifest.Should().NotBeNull();
            articleManifest.Should().NotBeNull();
            articleManifest.ArticleId.Should().Be("article/contact");
            articleManifest.Title.Should().Be("Contact");
            articleManifest.Author.Should().Be("Ghost Writer");
            articleManifest.Date.Should().Be(new DateTime(2020, 1, 2));

            Enumerable.SequenceEqual(articleManifest.Tags, new[] { "Tag1", "Tag2" }).Should().BeTrue();
        }
        private IReadOnlyList <WordCount> IndexDoc(ArticlePayload articlePayload, string documentPath)
        {
            byte[] documentBytes = articlePayload.GetPackageItem(documentPath);
            string documentRaw   = Encoding.UTF8.GetString(documentBytes.RemoveBOM());

            return(new ParseWord().Parse(documentRaw));
        }
Exemplo n.º 3
0
        public async Task Set(ArticlePayload articlePayload, CancellationToken token = default)
        {
            articlePayload.VerifyNotNull(nameof(articlePayload));

            _logger.LogTrace($"{nameof(Set)}: Writing {articlePayload.Id}");
            await _dataLakeStore.Write(ToFullFileName(articlePayload.Id), articlePayload.ToBytes(), true, token);
        }
Exemplo n.º 4
0
        public ArticleCache(ArticlePayload articlePayload)
        {
            articlePayload.VerifyNotNull(nameof(articlePayload));
            articlePayload.Verify();

            ArticlePayload = articlePayload;
        }
Exemplo n.º 5
0
        public async Task GivenFakePackage_WhenFullLifeCycle_ShouldPass()
        {
            TestWebsiteHost host = TestApplication.GetHost();

            const string payload = "This is a test";
            string       id      = "fake1";

            byte[] bytes = Encoding.UTF8.GetBytes(payload);

            ArticlePayload articlePayload = bytes.ToArticlePayload((ArticleId)id);

            await host.ArticleClient.Set(articlePayload);

            ArticlePayload?readPayload = await host.ArticleClient.Get((ArticleId)id);

            readPayload.Should().NotBeNull();

            (articlePayload == readPayload).Should().BeTrue();

            string payloadText = Encoding.UTF8.GetString(readPayload !.ToBytes());

            payloadText.Should().Be(payload);

            BatchSet <string> searchList = await host.ArticleClient.List(QueryParameters.Default).ReadNext();

            searchList.Should().NotBeNull();
            searchList.Records.Any(x => x.StartsWith(id)).Should().BeTrue();

            (await host.ArticleClient.Delete((ArticleId)id)).Should().BeTrue();

            searchList = await host.ArticleClient.List(QueryParameters.Default).ReadNext();

            searchList.Should().NotBeNull();
            searchList.Records.Any(x => x.StartsWith(id)).Should().BeFalse();
        }
Exemplo n.º 6
0
        public async Task Set(ArticlePayload articlePayload, CancellationToken token = default)
        {
            articlePayload.VerifyNotNull(nameof(articlePayload));

            _logger.LogTrace($"{nameof(Set)}: Id={articlePayload.Id}");

            await _httpClient.PostAsJsonAsync("api/article", articlePayload, token);
        }
Exemplo n.º 7
0
        private async Task UploadPackage(string articlePackageFile, CancellationToken token)
        {
            _logger.LogInformation($"Uploading article package {articlePackageFile}");

            ArticlePayload articlePayload = File.ReadAllBytes(articlePackageFile).ToArticlePayload();

            await _blogClient.Set(articlePayload, token);
        }
Exemplo n.º 8
0
        public async Task Set(ArticlePayload articlePayload, CancellationToken token)
        {
            articlePayload.VerifyNotNull(nameof(articlePayload))
            .VerifyAssert(x => articlePayload.Id.ToLower() == base.ActorKey.Value, $"Id mismatch - id={articlePayload.Id.ToLower()}, actorKey={base.ActorKey}");

            _logger.LogTrace($"{nameof(Set)}: Writing {articlePayload.Id}");
            await _acticleStore.Set(articlePayload, token);

            _cache.Set(articlePayload);
        }
        public IReadOnlyList <WordCount> Build(string packageFile)
        {
            packageFile.VerifyNotEmpty(nameof(packageFile));
            packageFile.VerifyAssert(x => File.Exists(x), $"{packageFile} does not exist");

            byte[]         bytes          = File.ReadAllBytes(packageFile);
            ArticlePayload articlePayload = bytes.ToArticlePayload((ArticleId)"id");

            return(Build(articlePayload));
        }
Exemplo n.º 10
0
        public async Task Set(ArticlePayload record, CancellationToken token = default)
        {
            record.VerifyNotNull(nameof(record));

            var actorKey = new ActorKey(new ArticleId(record.Id).ToString());

            _logger.LogTrace($"{nameof(Set)}: actorKey={actorKey}, id={record.Id}");

            IArticlePackageActor actor = _actorHost !.GetActor <IArticlePackageActor>(actorKey);
            await actor.Set(record, token);
        }
Exemplo n.º 11
0
        public async Task <IActionResult> Post([FromBody] ArticlePayload record)
        {
            if (!record.IsValid())
            {
                return(BadRequest());
            }

            await _acticleStoreService.Set(record);

            return(Ok());
        }
 public static bool IsValid(this ArticlePayload subject)
 {
     try
     {
         subject.Verify();
         return(true);
     }
     catch (ArgumentException)
     {
         return(false);
     }
 }
        public static void Verify(this ArticlePayload subject)
        {
            subject.VerifyNotNull(nameof(subject));

            subject.Id.VerifyNotEmpty(nameof(subject.Id));
            subject.PackagePayload.VerifyAssert(x => x?.Length > 0, $"{nameof(subject.PackagePayload)} is required");
            subject.Hash.VerifyNotEmpty($"{nameof(subject.Hash)} is required");

            byte[] packagePayload = Convert.FromBase64String(subject.PackagePayload);
            byte[] hash           = MD5.Create().ComputeHash(packagePayload);

            Convert.ToBase64String(hash).VerifyAssert(x => x == subject.Hash, "Hash verification failed");
        }
Exemplo n.º 14
0
        public async Task GivenRealPackage_WhenFullLifeCycleInFolder_ShouldPass()
        {
            TestWebsiteHost host = TestApplication.GetHost();

            string specFile    = new TestResources().WriteTestData_1();
            string buildFolder = Path.Combine(Path.GetTempPath(), nameof(nBlog), "build", Guid.NewGuid().ToString());

            string packageFile = new ArticlePackageBuilder()
                                 .SetSpecFile(specFile)
                                 .SetBuildFolder(buildFolder)
                                 .SetObjFolder(Path.Combine(buildFolder, "obj"))
                                 .Build();

            byte[]          packageBytes    = File.ReadAllBytes(packageFile);
            ArticlePayload  articlePayload  = packageBytes.ToArticlePayload();
            ArticleManifest articleManifest = packageBytes.ReadManifest();

            await host.ArticleClient.Set(articlePayload);

            ArticlePayload?readPayload = await host.ArticleClient.Get((ArticleId)articlePayload.Id);

            readPayload.Should().NotBeNull();

            ArticleManifest readArticleManifest = articlePayload.ReadManifest();

            articleManifest.ArticleId.Should().Be(readArticleManifest.ArticleId);
            articleManifest.PackageVersion.Should().Be(readArticleManifest.PackageVersion);
            articleManifest.Title.Should().Be(readArticleManifest.Title);
            articleManifest.Summary.Should().Be(readArticleManifest.Summary);
            articleManifest.Author.Should().Be(readArticleManifest.Author);
            articleManifest.ImageFile.Should().Be(readArticleManifest.ImageFile);
            articleManifest.Date.Should().Be(readArticleManifest.Date);
            Enumerable.SequenceEqual(articleManifest.Tags !, readArticleManifest.Tags !).Should().BeTrue();
            Enumerable.SequenceEqual(articleManifest.Categories !, readArticleManifest.Categories !).Should().BeTrue();

            (articlePayload == readPayload).Should().BeTrue();

            BatchSet <string> searchList = await host.ArticleClient.List(QueryParameters.Default).ReadNext();

            searchList.Should().NotBeNull();

            searchList.Records.Any(x => x.StartsWith(articlePayload.Id)).Should().BeTrue();

            (await host.ArticleClient.Delete((ArticleId)articlePayload.Id)).Should().BeTrue();

            searchList = await host.ArticleClient.List(QueryParameters.Default).ReadNext();

            searchList.Should().NotBeNull();
            searchList.Records.Any(x => x.StartsWith(articlePayload.Id)).Should().BeFalse();
        }
        public static ArticlePayload ToArticlePayload(this byte[] subject, ArticleId articleId)
        {
            subject.VerifyAssert(x => x?.Length > 0, $"{nameof(subject)} is empty");
            articleId.VerifyNotNull(nameof(articleId));

            var payload = new ArticlePayload
            {
                Id             = (string)articleId,
                PackagePayload = Convert.ToBase64String(subject),
                Hash           = Convert.ToBase64String(MD5.Create().ComputeHash(subject)),
            };

            payload.Verify();
            return(payload);
        }
Exemplo n.º 16
0
        private void BuildIndex(Context context, string packageFile, CancellationToken token)
        {
            _logger.LogInformation($"{nameof(BuildIndex)}: packageFile={packageFile}");

            byte[]         bytes          = File.ReadAllBytes(packageFile);
            ArticlePayload articlePayload = bytes.ToArticlePayload((ArticleId)"id");

            IReadOnlyList <WordCount> wordCounts = new ArticlePackageIndexBuilder().Build(articlePayload);

            ArticleManifest articleManifest = articlePayload.ReadManifest();

            var articleIndex = new ArticleIndex
            {
                ArticleId   = articleManifest.ArticleId,
                WordIndexes = wordCounts,
            };

            context.Queue.Enqueue(articleIndex);
        }
        public IReadOnlyList <WordCount> Build(ArticlePayload articlePayload)
        {
            IReadOnlyList <string> docsToIndex = articlePayload.GetPackageEntries()
                                                 .Where(x =>
            {
                var extension = Path.GetExtension(x).Replace(".", string.Empty);
                return(_documentExtensionsToIndex.Any(x => string.Equals(x, extension, StringComparison.OrdinalIgnoreCase)));
            }).ToList();

            var list = docsToIndex
                       .SelectMany(x => IndexDoc(articlePayload, x))
                       .ToList();

            return(list
                   .GroupBy(x => x.Word, StringComparer.OrdinalIgnoreCase)
                   .Select(x => new WordCount {
                Word = x.Key, Count = x.Sum(x => x.Count)
            })
                   .OrderBy(x => x.Word)
                   .ToList());
        }
 public static IReadOnlyList <string> GetPackageEntries(this ArticlePayload subject) => subject.ToBytes().GetPackageEntries();
        public static byte[] ToBytes(this ArticlePayload subject)
        {
            subject.Verify();

            return(Convert.FromBase64String(subject.PackagePayload));
        }
 public static ArticleManifest ReadManifest(this ArticlePayload subject) => subject.ToBytes().ReadManifest();
 public static byte[] GetPackageItem(this ArticlePayload subject, string path) => subject.ToBytes().GetPackageItem(path);