public async Task ShouldLoadEntryIntoHtmlFile()
        {
            var projectInfo = this.TestContext.ShouldGetProjectDirectoryInfo(this.GetType());

            var blobContainerName = this.TestContext.Properties["blobContainerName"].ToString();
            var htmlFile          = this.TestContext.Properties["htmlFile"].ToString();

            htmlFile = Path.Combine(projectInfo.FullName, htmlFile);
            var slug = this.TestContext.Properties["slug"].ToString();

            var container = cloudStorageAccountClassic.CreateCloudBlobClient().GetContainerReference(blobContainerName);
            var keys      = new AzureBlobKeys();

            keys.Add <BlogEntry>(i => i.Slug);

            var repository = new BlogRepository(keys, container);
            var blogEntry  = await repository.LoadSingleAsync <BlogEntry>(slug);

            var h2          = new XElement("h2", blogEntry.Title);
            var contentHtml = string.Format("<content>{0}</content>", blogEntry.Content);

            contentHtml = HtmlUtility.ConvertToXml(contentHtml);
            var content = XElement.Parse(contentHtml);

            var html = File.ReadAllText(htmlFile);
            var xDoc = XDocument.Parse(html);
            var body = xDoc.Root.Element("body");

            body.Value = string.Empty;
            body.Add(h2);
            body.Add(content.Elements());

            File.WriteAllText(htmlFile, xDoc.ToString());
        }
        public async Task ShouldGenerateRepositoryIndex()
        {
            var projectInfo = this.TestContext.ShouldGetProjectDirectoryInfo(this.GetType());

            #region test properties:

            var blobContainerName = this.TestContext.Properties["blobContainerName"].ToString();

            var indexPath = this.TestContext.Properties["indexPath"].ToString();
            indexPath = projectInfo.ToCombinedPath(indexPath);
            this.TestContext.ShouldFindFile(indexPath);

            var topicsPath = this.TestContext.Properties["topicsPath"].ToString();
            topicsPath = projectInfo.ToCombinedPath(topicsPath);
            this.TestContext.ShouldFindFile(topicsPath);

            #endregion

            var container = cloudStorageAccountClassic.CreateCloudBlobClient().GetContainerReference(blobContainerName);
            var keys      = new AzureBlobKeys();
            keys.Add <BlogEntry>(i => i.Slug);

            var repository = new BlogRepository(keys, container);

            var json = await repository.ToRepositoryIndexAsync(topicsPath, useJavaScriptCase : true);

            await repository.SetIndex(json);

            File.WriteAllText(indexPath, json);
        }
        public async Task ShouldHaveEntity()
        {
            var blobContainerName = this.TestContext.Properties["blobContainerName"].ToString();
            var slug = this.TestContext.Properties["slug"].ToString();

            var container = cloudStorageAccountClassic.CreateCloudBlobClient().GetContainerReference(blobContainerName);
            var keys      = new AzureBlobKeys();

            keys.Add <BlogEntry>(i => i.Slug);

            var repository = new BlogRepository(keys, container);

            Assert.IsTrue(await repository.HasEntityAsync <BlogEntry>(slug), "The expected Blog Entry is not in the Repository.");
        }
예제 #4
0
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
            var meta = new ProgramMetadata();

            this.Configuration.Bind(nameof(ProgramMetadata), meta);

            var validation = meta.ToValidationResults();

            if (validation.Any())
            {
                throw new ApplicationException(validation.ToDisplayText());
            }

            #region functional members:

            BlogRepository serveRepository(IServiceProvider factory)
            {
                var set = meta
                          .CloudStorageSet
                          .TryGetValueWithKey("SonghayCloudStorage");

                var connectionString    = set.TryGetValueWithKey("classic");
                var cloudStorageAccount = CloudStorageAccount.Parse(connectionString);

                var repositoryKeys = new AzureBlobKeys();

                repositoryKeys.Add <BlogEntry>(e => e.Slug);

                var container = cloudStorageAccount.GetContainerReference(set.TryGetValueWithKey("classic-day-path-container"));

                return(new BlogRepository(repositoryKeys, container));
            }

            #endregion

            services
            .AddSingleton <IRepositoryAsync, BlogRepository>(serveRepository)
            .AddSingleton(factory => meta.ToAzureSearchRestApiMetadata())
            .AddSingleton(factory => this.Configuration.GetSection(nameof(AzureSearchPostTemplate)).Get <AzureSearchPostTemplate>())
            .AddSpaStaticFiles(configuration =>
            {
                configuration.RootPath = "ClientApp/dist";
            });
        }
        public async Task ShouldReplaceEntry()
        {
            var blobContainerName = this.TestContext.Properties["blobContainerName"].ToString();
            var slugNew           = this.TestContext.Properties["slugNew"].ToString();
            var slugOld           = this.TestContext.Properties["slugOld"].ToString();

            var container = cloudStorageAccountClassic.CreateCloudBlobClient().GetContainerReference(blobContainerName);
            var keys      = new AzureBlobKeys();

            keys.Add <BlogEntry>(i => i.Slug);

            var repository   = new BlogRepository(keys, container);
            var blogEntryNew = await repository.LoadSingleAsync <BlogEntry>(slugNew);

            var blogEntryOld = await repository.LoadSingleAsync <BlogEntry>(slugOld);

            blogEntryNew.InceptDate       = blogEntryOld.InceptDate;
            blogEntryNew.ModificationDate = DateTime.Now;

            await repository.SaveEntityAsync(blogEntryNew);

            await repository.DeleteEntityAsync <BlogEntry>(blogEntryOld.Slug);
        }
        public async Task ShouldGetIndex()
        {
            var blobContainerName = this.TestContext.Properties["blobContainerName"].ToString();

            var container = cloudStorageAccountClassic.CreateCloudBlobClient().GetContainerReference(blobContainerName);
            var keys      = new AzureBlobKeys();

            keys.Add <BlogEntry>(i => i.Slug);

            var repository = new BlogRepository(keys, container);

            var sampleSize = 10;
            var index      = await repository.GetIndexAsync();

            index = index
                    .OrderByDescending(i => i.InceptDate)
                    .Take(sampleSize);
            Assert.IsTrue(index.Any(), "The expected repository index is not here.");

            index.ForEachInEnumerable(i => this.TestContext.WriteLine(i.ToString()));

            Assert.AreEqual(sampleSize, index.Count(), "The expected repository index count is not here.");
        }
        public async Task ShouldGenerateBlogRss()
        {
            var projectInfo = this.TestContext.ShouldGetProjectDirectoryInfo(this.GetType());

            #region test properties:

            var blobContainerName        = this.TestContext.Properties["blobContainerName"].ToString();
            var blobContainerNameClassic = this.TestContext.Properties["blobContainerNameClassic"].ToString();

            var rssPath = this.TestContext.Properties["rssPath"].ToString();
            rssPath = projectInfo.ToCombinedPath(rssPath);
            this.TestContext.ShouldFindFile(rssPath);

            #endregion

            var containerClassic = cloudStorageAccountClassic.CreateCloudBlobClient().GetContainerReference(blobContainerNameClassic);
            var keys             = new AzureBlobKeys();
            keys.Add <BlogEntry>(i => i.Slug);

            var repository = new BlogRepository(keys, containerClassic);
            var data       = await(repository as IBlogEntryIndex).GetIndexAsync();
            Assert.IsTrue(data.Any(), "The expected data are not here.");

            var feed = data
                       .OrderByDescending(i => i.InceptDate)
                       .Take(10);

            var builder  = new StringBuilder();
            var settings = new XmlWriterSettings
            {
                Async              = true,
                CloseOutput        = true,
                Encoding           = Encoding.UTF8,
                Indent             = true,
                OmitXmlDeclaration = true
            };
            var person = new SyndicationPerson("Bryan Wilhite", "*****@*****.**");

            using (var writer = XmlWriter.Create(builder, settings))
            {
                var feedWriter = new RssFeedWriter(writer);

                await feedWriter.WritePubDate(DateTime.Now);

                await feedWriter.WriteTitle($">DayPath_");

                await feedWriter.WriteDescription($"The technical journey of @BryanWilhite.");

                await feedWriter.WriteCopyright($"Bryan Wilhite, Songhay System {DateTime.Now.Year}");

                await feedWriter.Write(new SyndicationLink(new Uri("http://songhayblog.azurewebsites.net", UriKind.Absolute)));

                var tasks = feed.Select(async entry =>
                {
                    var item = new SyndicationItem
                    {
                        Description = entry.Content,
                        Id          = entry.Slug,
                        LastUpdated = entry.ModificationDate,
                        Published   = entry.InceptDate,
                        Title       = entry.Title
                    };

                    item.AddContributor(person);
                    item.AddLink(new SyndicationLink(new Uri($"http://songhayblog.azurewebsites.net/blog/entry/{entry.Slug}", UriKind.Absolute)));

                    await feedWriter.Write(item);
                });

                await Task.WhenAll(tasks);

                await writer.FlushAsync();
            }

            File.WriteAllText(rssPath, builder.ToString());

            var container = cloudStorageAccount.CreateCloudBlobClient().GetContainerReference(blobContainerName);
            await container.UploadBlobAsync(rssPath, string.Empty);
        }
        public async Task ShouldGenerateBlogEntry()
        {
            var projectInfo    = this.TestContext.ShouldGetProjectDirectoryInfo(this.GetType());
            var webProjectInfo = this.TestContext.ShouldGetConventionalProjectDirectoryInfo(this.GetType());

            #region test properties:

            var blobContainerName  = this.TestContext.Properties["blobContainerName"].ToString();
            var entryHeaderElement = this.TestContext.Properties["entryHeaderElement"].ToString();

            var entryPath = this.TestContext.Properties["entryPath"].ToString();
            entryPath = projectInfo.ToCombinedPath(entryPath);
            this.TestContext.ShouldFindFile(entryPath);

            var entryOutputPath = this.TestContext.Properties["entryOutputPath"].ToString();
            entryOutputPath = projectInfo.ToCombinedPath(entryOutputPath);
            this.TestContext.ShouldFindFile(entryOutputPath);

            #endregion

            var container = cloudStorageAccountClassic.CreateCloudBlobClient().GetContainerReference(blobContainerName);
            var keys      = new AzureBlobKeys();
            keys.Add <BlogEntry>(i => i.Slug);

            var repository = new BlogRepository(keys, container);

            var entry = File.ReadAllText(entryPath);
            entry = HtmlUtility.ConvertToXml(entry);
            var xdEntry = XDocument.Parse(entry);
            var body    = xdEntry.Root.Element("body");
            Assert.IsNotNull(body, "The expected body is not here.");

            var title   = body.Element(entryHeaderElement).GetInnerXml();
            var content = new XElement("body",
                                       body
                                       .Elements()
                                       .Where(i => !i.Name.LocalName.Equals(entryHeaderElement))
                                       ).GetInnerXml();

            var blogEntry = (new BlogEntry
            {
                Content = content,
                InceptDate = DateTime.Now,
                IsPublished = true,
                Title = title
            }).WithDefaultSlug();

            if (await repository.HasEntityAsync <BlogEntry>(blogEntry.Slug))
            {
                var previousEntry = await repository.LoadSingleAsync <BlogEntry>(blogEntry.Slug);

                Assert.IsNotNull(previousEntry, "The expected previous entry is not here.");
                blogEntry.InceptDate = previousEntry.InceptDate;
            }

            await repository.SaveEntityAsync(blogEntry);

            Assert.IsTrue(await repository.HasEntityAsync <BlogEntry>(blogEntry.Slug), "The expected Blog Entry is not in the Repository.");
            var json = blogEntry.ToJson(useJavaScriptCase: true);
            File.WriteAllText(entryOutputPath, json);
        }
예제 #9
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BlogRepository"/> class.
 /// </summary>
 /// <param name="keys">The keys.</param>
 /// <param name="container">The container.</param>
 public BlogRepository(AzureBlobKeys keys, CloudBlobContainer container)
     : base(keys, container)
 {
 }