forked from Sitecore/ASP.NET-Core-SDK
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSitecoreLayoutClientBuilderExtensionsFixture.cs
More file actions
205 lines (176 loc) · 8.34 KB
/
SitecoreLayoutClientBuilderExtensionsFixture.cs
File metadata and controls
205 lines (176 loc) · 8.34 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
using AutoFixture;
using AwesomeAssertions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Sitecore.AspNetCore.SDK.AutoFixture.Attributes;
using Sitecore.AspNetCore.SDK.LayoutService.Client.Configuration;
using Sitecore.AspNetCore.SDK.LayoutService.Client.Extensions;
using Sitecore.AspNetCore.SDK.LayoutService.Client.Interfaces;
using Sitecore.AspNetCore.SDK.LayoutService.Client.Request;
using Sitecore.AspNetCore.SDK.LayoutService.Client.Request.Handlers;
using Sitecore.AspNetCore.SDK.LayoutService.Client.Request.Handlers.GraphQL;
using Xunit;
namespace Sitecore.AspNetCore.SDK.LayoutService.Client.Tests.Extensions;
public class SitecoreLayoutClientBuilderExtensionsFixture
{
// ReSharper disable once UnusedMember.Global - Used by testing framework
public static Action<IFixture> AutoSetup => f =>
{
SitecoreLayoutClientBuilder builder = new(new ServiceCollection());
f.Inject(builder);
};
[Fact]
public void AddHandler_NullBuilder_Throws()
{
// Arrange
Action action = () => SitecoreLayoutClientBuilderExtensions.AddHandler<ILayoutRequestHandler>(null!, "string");
// Act / Assert
action.Should().Throw<ArgumentNullException>()
.And.ParamName.Should().Be("builder");
}
[Theory]
[MemberAutoNSubstituteData(nameof(EmptyStrings))]
public void AddHandler_InvalidName_Throws(string value, SitecoreLayoutClientBuilder builder)
{
// Arrange
Action action = () => builder.AddHandler<ILayoutRequestHandler>(value);
// Act / Assert
action.Should().Throw<ArgumentException>()
.And.ParamName.Should().Be("name");
}
[Theory]
[AutoNSubstituteData]
public void AddHandler_Throws_IfTServiceIsAnInterface(SitecoreLayoutClientBuilder builder, string handlerName)
{
// Arrange
Action action = () => builder.AddHandler<ILayoutRequestHandler>(handlerName);
// Act / Assert
action.Should().Throw<ArgumentException>()
.WithMessage($"Can only register implementations of {typeof(ILayoutRequestHandler)} as layout services.");
}
[Theory]
[AutoNSubstituteData]
public void AddHandler_Throws_IfTServiceIsAnAbstractClass(SitecoreLayoutClientBuilder builder, string handlerName)
{
// Arrange
Action action = () => builder.AddHandler<TestAbstractSitecoreLayoutRequestHandler>(handlerName);
// Act / Assert
action.Should().Throw<ArgumentException>()
.WithMessage("Abstract registrations must provide a factory to resolve a layout service.");
}
[Theory]
[AutoNSubstituteData]
public void AddHandler_CreatesFactory_IfFactoryIsNull(SitecoreLayoutClientBuilder builder, string handlerName)
{
// Arrange / Act
ILayoutRequestHandlerBuilder<HttpLayoutRequestHandler> result = builder.AddHandler<HttpLayoutRequestHandler>(handlerName);
// Assert
ServiceProvider provider = result.Services.BuildServiceProvider();
SitecoreLayoutClientOptions options = provider.GetRequiredService<IOptions<SitecoreLayoutClientOptions>>().Value;
options.HandlerRegistry[handlerName].Should().NotBeNull();
}
[Theory]
[AutoNSubstituteData]
public void AddHandler_UsesTheProvidedFactory_IfFactoryIsNotNull(
SitecoreLayoutClientBuilder builder,
string handlerName,
HttpLayoutRequestHandler service)
{
// Arrange / Act
ILayoutRequestHandlerBuilder<HttpLayoutRequestHandler> result = builder.AddHandler(handlerName, _ => service);
// Assert
ServiceProvider provider = result.Services.BuildServiceProvider();
SitecoreLayoutClientOptions options = provider.GetRequiredService<IOptions<SitecoreLayoutClientOptions>>().Value;
ILayoutRequestHandler instance = options.HandlerRegistry[handlerName].Invoke(provider);
instance.Should().BeSameAs(service);
}
[Theory]
[AutoNSubstituteData]
public void AddHandler_WithValidValues_ReturnsNewBuilderWithCorrectValues(
SitecoreLayoutClientBuilder builder,
string handlerName)
{
// Arrange / Act
ILayoutRequestHandlerBuilder<HttpLayoutRequestHandler> result = builder.AddHandler<HttpLayoutRequestHandler>(handlerName);
// Assert
result.Should().BeOfType<SitecoreLayoutRequestHandlerBuilder<HttpLayoutRequestHandler>>();
result.Services.Should().BeSameAs(builder.Services);
result.HandlerName.Should().Be(handlerName);
}
[Fact]
public void WithDefaultRequestOptions_BuilderIsNUll_ThrowsArgumentNullException()
{
// Arrange
Func<ISitecoreLayoutClientBuilder> act =
() => SitecoreLayoutClientBuilderExtensions.WithDefaultRequestOptions(null!, null!);
// Act & Assert
act.Should().Throw<ArgumentNullException>().WithMessage("Value cannot be null. (Parameter 'builder')");
}
[Theory]
[AutoNSubstituteData]
public void WithDefaultRequestOptions_ConfigureActionIsNUll_ThrowsArgumentNullException(SitecoreLayoutClientBuilder builder)
{
// Arrange
Func<ISitecoreLayoutClientBuilder> act =
() => builder.WithDefaultRequestOptions(null!);
// Act & Assert
act.Should().Throw<ArgumentNullException>().WithMessage("Value cannot be null. (Parameter 'configureRequest')");
}
[Theory]
[AutoNSubstituteData]
public void WithDefaultRequestOptions_RequestDefaultsIsNotNull_ServiceProviderReturnsOptionsWithRequestDefaults(SitecoreLayoutClientBuilder builder)
{
// Act
ISitecoreLayoutClientBuilder result = builder.WithDefaultRequestOptions(_ => { });
// Assert
ServiceProvider provider = result.Services.BuildServiceProvider();
SitecoreLayoutRequestOptions options = provider.GetRequiredService<IOptions<SitecoreLayoutRequestOptions>>().Value;
options.RequestDefaults.Should().NotBeNull();
}
[Theory]
[AutoNSubstituteData]
public void WithDefaultRequestOptions_RequestDefaultsHasApiKeySpecified_ServiceProviderReturnsOptionsWithRequestDefaultsContainingApiKey(SitecoreLayoutClientBuilder builder)
{
// Act
ISitecoreLayoutClientBuilder result = builder.WithDefaultRequestOptions(o => { o.ApiKey("test_api_key"); });
// Assert
ServiceProvider provider = result.Services.BuildServiceProvider();
SitecoreLayoutRequestOptions options = provider.GetRequiredService<IOptions<SitecoreLayoutRequestOptions>>().Value;
options.RequestDefaults.Should().NotBeNull();
options.RequestDefaults.Should().BeOfType<SitecoreLayoutRequest>();
options.RequestDefaults.Should().ContainKey(RequestKeys.ApiKey);
options.RequestDefaults.ApiKey().Should().Be("test_api_key");
}
[Theory]
[AutoNSubstituteData]
public void AddGraphQLHandler_Minimal_IsValid(SitecoreLayoutClientBuilder builder, string name, string siteName, string apiKey, Uri uri)
{
// Act
ILayoutRequestHandlerBuilder<GraphQLLayoutServiceHandler> result = builder.AddGraphQLHandler(name, siteName, apiKey, uri);
// Assert
ServiceProvider provider = result.Services.BuildServiceProvider();
SitecoreLayoutRequestOptions options = provider.GetRequiredService<IOptions<SitecoreLayoutRequestOptions>>().Value;
options.RequestDefaults.ApiKey().Should().Be(apiKey);
options.RequestDefaults.SiteName().Should().Be(siteName);
options.RequestDefaults.Language().Should().Be("en");
}
[Theory]
[AutoNSubstituteData]
public void AddGraphQLWithContextHandler_Minimal_IsValid(SitecoreLayoutClientBuilder builder, string contextId)
{
// Act
ILayoutRequestHandlerBuilder<GraphQLLayoutServiceHandler> result = builder.AddGraphQLWithContextHandler("Test", contextId);
// Assert
ServiceProvider provider = result.Services.BuildServiceProvider();
SitecoreLayoutRequestOptions options = provider.GetRequiredService<IOptions<SitecoreLayoutRequestOptions>>().Value;
options.RequestDefaults.ContextId().Should().Be(contextId);
options.RequestDefaults.SiteName().Should().BeNull();
options.RequestDefaults.Language().Should().Be("en");
}
private static IEnumerable<object[]> EmptyStrings()
{
yield return [null!];
yield return [string.Empty];
yield return ["\t\t "];
}
}