-
Notifications
You must be signed in to change notification settings - Fork 29
/
ProductsContentFinderTests.cs
180 lines (162 loc) · 7.94 KB
/
ProductsContentFinderTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using Moq;
using NUnit.Framework;
using System.Collections.Generic;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Core.PublishedCache;
using Umbraco.Cms.Core.Routing;
using Umbraco.Cms.Core.Web;
using UmbracoNineDemoSite.Core;
using UmbracoNineDemoSite.Core.Features.Products;
using UmbracoNineDemoSite.Integrations.Products.Entities;
using UmbracoNineDemoSite.Integrations.Products.Services;
namespace UmbracoNineDemoSite.Tests.Unit.Features.Products
{
public class ProductsContentFinderTests
{
#region properties
private delegate void ServiceTryGetPublishedSnapshot(out IPublishedSnapshot snapshot);
private delegate void ServiceTryGetUmbracoContext(out IUmbracoContext context);
private delegate void ServiceSetPublishedContent(IPublishedContent content);
private readonly string _productsContainerAlias = "productsContainer";
private readonly int _productId = 1001;
#endregion
[Test]
[TestCase("products", "1001", "any")]
[TestCase("products", "1001", "something")]
public void Given_RequestContainsExistingProductIdInSecondSegment_When_TryFindContent_Then_ExpectTrue(
string rootPath, string productId, string productName)
{
IPublishedContent dummyContent;
bool result;
Mock<ProductsContainer> productsContainer;
SetupAndCallTRyFindContent(rootPath, productId, productName, out dummyContent, out result, out productsContainer);
#region check result of method call
Assert.True(result);
Assert.IsNotNull(dummyContent);
Assert.AreEqual(dummyContent.Name, productsContainer.Object.Name);
Assert.AreEqual(dummyContent.Id, productsContainer.Object.Id);
Assert.AreEqual(dummyContent.ContentType.Alias, productsContainer.Object.ContentType.Alias);
#endregion
}
[Test]
[TestCase("codegarden/products", "1001", "any")]
public void Given_RequestContainsExistingProductIdInWrongSegment_When_TryFindContent_Then_ExpectFalse(
string rootPath, string productId, string productName)
{
IPublishedContent dummyContent;
bool result;
Mock<ProductsContainer> productsContainer;
SetupAndCallTRyFindContent(rootPath, productId, productName, out dummyContent, out result, out productsContainer);
#region check result of method call
Assert.False(result);
Assert.IsNull(dummyContent);
#endregion
}
[Test]
[TestCase("products", "1002", "any")]
public void Given_RequestDoesNotContainExistingProductId_When_TryFindContent_Then_ExpectFalse(
string rootPath, string productId, string productName)
{
IPublishedContent dummyContent;
bool result;
Mock<ProductsContainer> productsContainer;
SetupAndCallTRyFindContent(rootPath, productId, productName, out dummyContent, out result, out productsContainer);
#region check result of method call
Assert.False(result);
Assert.IsNull(dummyContent);
#endregion
}
[Test]
[TestCase("xxx", "1001", "any")]
public void Given_RequestContainsWrongRootSegment_When_TryFindContent_Then_ExpectFalse(
string rootPath, string productId, string productName)
{
IPublishedContent dummyContent;
bool result;
Mock<ProductsContainer> productsContainer;
SetupAndCallTRyFindContent(rootPath, productId, productName, out dummyContent, out result, out productsContainer);
#region check result of method call
Assert.False(result);
Assert.IsNull(dummyContent);
#endregion
}
private void SetupAndCallTRyFindContent(string rootPath, string productId, string productName, out IPublishedContent foundContent, out bool result, out Mock<ProductsContainer> productsContainerOut)
{
#region setup request Mock
var absolutePathDecoded = $"/{rootPath}/{productId}/{productName}";
var request = new Mock<IPublishedRequestBuilder>();
request.Setup(s => s.AbsolutePathDecoded).Returns(absolutePathDecoded);
IPublishedContent callbackContent = null;
request.Setup(s => s.SetPublishedContent(It.IsAny<IPublishedContent>()))
.Callback(new ServiceSetPublishedContent((IPublishedContent content) =>
{
callbackContent = content;
}));
#endregion
#region setup IProductService Mock
var product = new Mock<IProduct>();
product.Setup(s => s.Id).Returns(_productId);
product.Setup(s => s.Name).Returns(productName);
var productService = new Mock<IProductService>();
productService.Setup(x => x.Get(_productId)).Returns(product.Object);
#endregion
#region setup IUmbracoContextAccessor Mock
var contentType = Mock.Of<IPublishedContentType>();
var productsContainerContent = Mock.Of<IPublishedContent>();
var productsContainerFallback = Mock.Of<IPublishedValueFallback>();
var productsContainer = new Mock<ProductsContainer>(
productsContainerContent, productsContainerFallback);
productsContainer.Setup(s => s.ContentType)
.Returns(contentType);
productsContainer.Setup(s => s.Id)
.Returns(1000);
productsContainer.Setup(s => s.Name)
.Returns("Products");
productsContainer.Setup(s => s.UrlSegment)
.Returns("products");
var publishedContentCollection = new List<IPublishedContent>
{
productsContainer.Object
};
var contentCache = new Mock<IPublishedContentCache>();
contentCache.Setup(s => s.GetByContentType(contentType))
.Returns(publishedContentCollection);
contentCache.Setup(s => s.GetContentType(1))
.Returns(contentType);
contentCache.Setup(s => s.GetContentType(_productsContainerAlias))
.Returns(contentType);
var umbracoContext = new Mock<IUmbracoContext>();
umbracoContext.Setup(s => s.Content)
.Returns(contentCache.Object);
IUmbracoContext ctx;
var umbracoContextAccessor = new Mock<IUmbracoContextAccessor>();
umbracoContextAccessor
.Setup(x => x.TryGetUmbracoContext(out ctx))
.Callback(new ServiceTryGetUmbracoContext((out IUmbracoContext uContext) =>
{
uContext = umbracoContext.Object;
}));
#endregion
#region setup IPublishedSnapshotAccessor Mock
var publishedSnapshot = new Mock<IPublishedSnapshot>();
publishedSnapshot.Setup(s => s.Content)
.Returns(contentCache.Object);
IPublishedSnapshot snapShot;
var publishedSnapshotAccessor = new Mock<IPublishedSnapshotAccessor>();
publishedSnapshotAccessor
.Setup(s => s.TryGetPublishedSnapshot(out snapShot))
.Callback(new ServiceTryGetPublishedSnapshot((out IPublishedSnapshot snapshot) =>
{
snapshot = publishedSnapshot.Object;
}))
.Returns(true);
#endregion
#region call TryFindContent of ProductsContentFinder
var productsContentFinder = new ProductsContentFinder(productService.Object, umbracoContextAccessor.Object, publishedSnapshotAccessor.Object);
result = productsContentFinder.TryFindContent(request.Object);
#endregion
foundContent = callbackContent;
productsContainerOut = productsContainer;
}
}
}