|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using FluentAssertions; |
| 6 | +using LinkDotNet.Blog.Domain; |
| 7 | +using LinkDotNet.Blog.Infrastructure.Persistence; |
| 8 | +using LinkDotNet.Blog.Web.Shared.Services; |
| 9 | +using LinkDotNet.Blog.Web.Shared.Services.Sitemap; |
| 10 | +using Moq; |
| 11 | +using Xunit; |
| 12 | + |
| 13 | +namespace LinkDotNet.Blog.IntegrationTests.Web.Shared.Services |
| 14 | +{ |
| 15 | + public sealed class SitemapServiceTests : IDisposable |
| 16 | + { |
| 17 | + private const string OutputDirectory = "wwwroot"; |
| 18 | + private const string OutputFilename = $"{OutputDirectory}/sitemap.xml"; |
| 19 | + private readonly SitemapService sut; |
| 20 | + |
| 21 | + public SitemapServiceTests() |
| 22 | + { |
| 23 | + var repositoryMock = new Mock<IRepository<BlogPost>>(); |
| 24 | + sut = new SitemapService(repositoryMock.Object, null, new XmlFileWriter()); |
| 25 | + Directory.CreateDirectory("wwwroot"); |
| 26 | + } |
| 27 | + |
| 28 | + [Fact] |
| 29 | + public async Task ShouldSaveSitemapUrlInCorrectFormat() |
| 30 | + { |
| 31 | + var urlSet = new SitemapUrlSet() |
| 32 | + { |
| 33 | + Urls = new List<SitemapUrl> |
| 34 | + { |
| 35 | + new SitemapUrl |
| 36 | + { |
| 37 | + Location = "here", |
| 38 | + }, |
| 39 | + }, |
| 40 | + }; |
| 41 | + await sut.SaveSitemapToFileAsync(urlSet); |
| 42 | + |
| 43 | + var lines = await File.ReadAllTextAsync(OutputFilename); |
| 44 | + lines.Should().Be( |
| 45 | +@"<?xml version=""1.0"" encoding=""utf-8""?> |
| 46 | +<urlset xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns=""http://www.sitemaps.org/schemas/sitemap/0.9""> |
| 47 | + <url> |
| 48 | + <loc>here</loc> |
| 49 | + </url> |
| 50 | +</urlset>"); |
| 51 | + } |
| 52 | + |
| 53 | + public void Dispose() |
| 54 | + { |
| 55 | + if (File.Exists(OutputFilename)) |
| 56 | + { |
| 57 | + File.Delete(OutputFilename); |
| 58 | + } |
| 59 | + |
| 60 | + if (Directory.Exists(OutputDirectory)) |
| 61 | + { |
| 62 | + Directory.Delete(OutputDirectory, true); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | +} |
0 commit comments