1
+ using System ;
2
+ using System . Linq . Expressions ;
3
+ using System . Threading . Tasks ;
4
+ using Bunit ;
5
+ using Bunit . TestDoubles ;
6
+ using FluentAssertions ;
7
+ using LinkDotNet . Blog . Domain ;
8
+ using LinkDotNet . Blog . Infrastructure . Persistence ;
9
+ using LinkDotNet . Blog . TestUtilities ;
10
+ using LinkDotNet . Blog . Web . Shared . Services ;
11
+ using LinkDotNet . Blog . Web . Shared . Services . Sitemap ;
12
+ using Moq ;
13
+ using X . PagedList ;
14
+ using Xunit ;
15
+
16
+ namespace LinkDotNet . Blog . UnitTests . Web . Shared . Services
17
+ {
18
+ public class SitemapServiceTests : TestContext
19
+ {
20
+ private readonly Mock < IRepository < BlogPost > > repositoryMock ;
21
+ private readonly Mock < IXmlFileWriter > xmlFileWriterMock ;
22
+ private readonly SitemapService sut ;
23
+ private readonly FakeNavigationManager fakeNavigationManager ;
24
+
25
+ public SitemapServiceTests ( )
26
+ {
27
+ repositoryMock = new Mock < IRepository < BlogPost > > ( ) ;
28
+ fakeNavigationManager = new FakeNavigationManager ( Renderer ) ;
29
+
30
+ xmlFileWriterMock = new Mock < IXmlFileWriter > ( ) ;
31
+ sut = new SitemapService (
32
+ repositoryMock . Object ,
33
+ fakeNavigationManager ,
34
+ xmlFileWriterMock . Object ) ;
35
+ }
36
+
37
+ [ Fact ]
38
+ public async Task ShouldCreateSitemap ( )
39
+ {
40
+ var bp1 = new BlogPostBuilder ( )
41
+ . WithUpdatedDate ( new DateTime ( 2020 , 1 , 1 ) )
42
+ . WithTags ( "tag1" , "tag2" )
43
+ . Build ( ) ;
44
+ bp1 . Id = "id1" ;
45
+ var bp2 = new BlogPostBuilder ( )
46
+ . WithUpdatedDate ( new DateTime ( 2019 , 1 , 1 ) )
47
+ . WithTags ( "tag2" )
48
+ . Build ( ) ;
49
+ bp2 . Id = "id2" ;
50
+ var blogPosts = new [ ] { bp1 , bp2 } ;
51
+ repositoryMock . Setup ( r => r . GetAllAsync (
52
+ It . IsAny < Expression < Func < BlogPost , bool > > > ( ) ,
53
+ p => p . UpdatedDate ,
54
+ true ,
55
+ It . IsAny < int > ( ) ,
56
+ It . IsAny < int > ( ) ) )
57
+ . ReturnsAsync ( new PagedList < BlogPost > ( blogPosts , 1 , 10 ) ) ;
58
+
59
+ var sitemap = await sut . CreateSitemapAsync ( ) ;
60
+
61
+ sitemap . Namespace . Should ( ) . Be ( "http://www.sitemaps.org/schemas/sitemap/0.9" ) ;
62
+ sitemap . Urls . Should ( ) . HaveCount ( 5 ) ;
63
+ sitemap . Urls [ 0 ] . Location . Should ( ) . Be ( $ "{ fakeNavigationManager . BaseUri } ") ;
64
+ sitemap . Urls [ 1 ] . Location . Should ( ) . Be ( $ "{ fakeNavigationManager . BaseUri } blogPost/id1") ;
65
+ sitemap . Urls [ 2 ] . Location . Should ( ) . Be ( $ "{ fakeNavigationManager . BaseUri } blogPost/id2") ;
66
+ sitemap . Urls [ 3 ] . Location . Should ( ) . Be ( $ "{ fakeNavigationManager . BaseUri } searchByTag/tag1") ;
67
+ sitemap . Urls [ 4 ] . Location . Should ( ) . Be ( $ "{ fakeNavigationManager . BaseUri } searchByTag/tag2") ;
68
+ }
69
+
70
+ [ Fact ]
71
+ public async Task ShouldSaveSitemapToFile ( )
72
+ {
73
+ var sitemap = new SitemapUrlSet ( ) ;
74
+
75
+ await sut . SaveSitemapToFileAsync ( sitemap ) ;
76
+
77
+ xmlFileWriterMock . Verify ( x => x . WriteObjectToXmlFileAsync ( sitemap , "sitemap.xml" ) ) ;
78
+ }
79
+ }
80
+ }
0 commit comments