-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathEndpointTestImplementationFactoryExtension.cs
311 lines (268 loc) · 17.3 KB
/
EndpointTestImplementationFactoryExtension.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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Xml.Serialization;
using Intent.Engine;
using Intent.Metadata.Models;
using Intent.Modelers.Domain.Api;
using Intent.Modelers.Services.Api;
using Intent.Modules.AspNetCore.IntegrationTesting;
using Intent.Modules.AspNetCore.IntegrationTesting.Templates;
using Intent.Modules.AspNetCore.IntegrationTests.CRUD.FactoryExtensions.TestImplementations;
using Intent.Modules.AspNetCore.IntegrationTests.CRUD.Templates;
using Intent.Modules.AspNetCore.IntegrationTests.CRUD.Templates.TestDataFactory;
using Intent.Modules.Common;
using Intent.Modules.Common.CSharp.Builder;
using Intent.Modules.Common.CSharp.Mapping;
using Intent.Modules.Common.CSharp.Templates;
using Intent.Modules.Common.Plugins;
using Intent.Modules.Common.Templates;
using Intent.Modules.Integration.HttpClients.Shared.Templates;
using Intent.Modules.Metadata.WebApi.Models;
using Intent.Plugins.FactoryExtensions;
using Intent.RoslynWeaver.Attributes;
using Intent.Templates;
using static Intent.Modules.AspNetCore.IntegrationTests.CRUD.FactoryExtensions.EndpointTestImplementationFactoryExtension;
[assembly: DefaultIntentManaged(Mode.Fully)]
[assembly: IntentTemplate("Intent.ModuleBuilder.Templates.FactoryExtension", Version = "1.0")]
namespace Intent.Modules.AspNetCore.IntegrationTests.CRUD.FactoryExtensions
{
[IntentManaged(Mode.Fully, Body = Mode.Merge)]
public partial class EndpointTestImplementationFactoryExtension : FactoryExtensionBase
{
private const string CommandSpecializationType = "ccf14eb6-3a55-4d81-b5b9-d27311c70cb9";
private const string DtoSpecializationType = "fee0edca-4aa0-4f77-a524-6bbd84e78734";
public override string Id => "Intent.AspNetCore.IntegrationTests.CRUD.EndpointTestImplementationFactoryExtension";
private readonly IMetadataManager _metadataManager;
public EndpointTestImplementationFactoryExtension(IMetadataManager metadataManager)
{
_metadataManager = metadataManager;
}
[IntentManaged(Mode.Ignore)]
public override int Order => 0;
protected override void OnAfterTemplateRegistrations(IApplication application)
{
var testDataTemplate = application.FindTemplateInstance<ICSharpFileBuilderTemplate>(TestDataFactoryTemplate.TemplateId);
var crudMaps = CrudMapHelper.LoadCrudMaps(testDataTemplate, _metadataManager, application);
PopulateTestDataFactory(testDataTemplate, crudMaps);
GenerateCRUDTests(application, crudMaps);
}
private void GenerateCRUDTests(IApplication application, List<CrudMap> crudMaps)
{
foreach (var crudTest in crudMaps)
{
var template = application.FindTemplateInstance<ICSharpFileBuilderTemplate>("Intent.AspNetCore.IntegrationTesting.ServiceEndpointTest", crudTest.Create.Id);
template.AddNugetDependency(NugetPackages.AutoFixture);
DoCreateTest(template, crudTest);
template = application.FindTemplateInstance<ICSharpFileBuilderTemplate>("Intent.AspNetCore.IntegrationTesting.ServiceEndpointTest", crudTest.GetById.Id);
DoGetByIdTest(template, crudTest);
if (crudTest.Update != null)
{
template = application.FindTemplateInstance<ICSharpFileBuilderTemplate>("Intent.AspNetCore.IntegrationTesting.ServiceEndpointTest", crudTest.Update.Id);
DoUpdateTest(template, crudTest);
}
if (crudTest.Delete != null)
{
template = application.FindTemplateInstance<ICSharpFileBuilderTemplate>("Intent.AspNetCore.IntegrationTesting.ServiceEndpointTest", crudTest.Delete.Id);
DoDeleteTest(template, crudTest);
}
if (crudTest.GetAll != null)
{
template = application.FindTemplateInstance<ICSharpFileBuilderTemplate>("Intent.AspNetCore.IntegrationTesting.ServiceEndpointTest", crudTest.GetAll.Id);
DoGetAllTest(template, crudTest);
}
}
}
private void DoCreateTest(ICSharpFileBuilderTemplate template, CrudMap crudTest)
{
template.CSharpFile.OnBuild(file =>
{
var @class = template.CSharpFile.Classes.First();
var operation = crudTest.Create;
@class.AddMethod("Task", $"{operation.Name}_Should{operation.Name}", method =>
{
template.AddUsing("AutoFixture");
var sutId = $"{crudTest.Entity.Name.ToParameterName()}Id";
var dtoModel = crudTest.Create.Inputs.First();
var owningAgggregateId = crudTest.OwningAggregate is null ? null : $"{crudTest.OwningAggregate.Name.ToParameterName()}Id";
var getByIdParams = crudTest.OwningAggregate is null ? sutId : $"{owningAgggregateId}, {sutId}";
method
.Async()
.AddAttribute("Fact")
.AddStatement("//Arrange")
.AddStatement($"var client = new {template.GetTypeName("Intent.AspNetCore.IntegrationTesting.HttpClient", crudTest.Proxy.Id)}(CreateClient());")
.AddStatement($"var dataFactory = new TestDataFactory(WebAppFactory);", s => s.SeparatedFromPrevious());
if (crudTest.Dependencies.Any())
{
if (crudTest.OwningAggregate is null)
{
method.AddStatement($"await dataFactory.Create{crudTest.Entity.Name}Dependencies();");
}
else
{
method.AddStatement($"var {owningAgggregateId} = await dataFactory.Create{crudTest.Entity.Name}Dependencies();");
}
}
method
.AddStatement($"var command = dataFactory.CreateCommand<{template.GetTypeName(dtoModel.TypeReference)}>();", s => s.SeparatedFromPrevious())
.AddStatement("//Act", s => s.SeparatedFromPrevious());
if (crudTest.ResponseDtoIdField is null)
{
method.AddStatement($"var {sutId} = await client.{crudTest.Create.Name}Async(command);");
}
else
{
method.AddStatement($"var createdDto = await client.{crudTest.Create.Name}Async(command);");
method.AddStatement($"var {sutId} = createdDto.{crudTest.ResponseDtoIdField};");
}
method
.AddStatement("//Assert", s => s.SeparatedFromPrevious())
.AddStatement($"var {crudTest.Entity.Name.ToParameterName()} = await client.{crudTest.GetById.Name}Async({getByIdParams});")
.AddStatement($"Assert.NotNull({crudTest.Entity.Name.ToParameterName()});");
});
});
}
private void DoGetByIdTest(ICSharpFileBuilderTemplate template, CrudMap crudTest)
{
template.CSharpFile.OnBuild(file =>
{
var @class = template.CSharpFile.Classes.First();
var operation = crudTest.GetById;
@class.AddMethod("Task", $"{operation.Name}_Should{operation.Name}", method =>
{
template.AddUsing("AutoFixture");
var sutId = crudTest.OwningAggregate is null ? $"{crudTest.Entity.Name.ToParameterName()}Id" : $"ids.{crudTest.Entity.Name.ToPascalCase()}Id";
var owningAggregateId = crudTest.OwningAggregate is null ? null : $"ids.{crudTest.OwningAggregate.Name.ToPascalCase()}Id";
var createVarName = crudTest.OwningAggregate is null ? $"{crudTest.Entity.Name.ToParameterName()}Id" : "ids";
var getByIdParams = crudTest.OwningAggregate is null ? sutId : $"{owningAggregateId}, {sutId}";
method
.Async()
.AddAttribute("Fact")
.AddStatement("//Arrange")
.AddStatement($"var client = new {template.GetTypeName("Intent.AspNetCore.IntegrationTesting.HttpClient", crudTest.Proxy.Id)}(CreateClient());")
.AddStatement($"var dataFactory = new TestDataFactory(WebAppFactory);", s => s.SeparatedFromPrevious())
.AddStatement($"var {createVarName} = await dataFactory.Create{crudTest.Entity.Name}();")
.AddStatement("//Act", s => s.SeparatedFromPrevious())
.AddStatement($"var {crudTest.Entity.Name.ToParameterName()} = await client.{crudTest.GetById.Name}Async({getByIdParams});")
.AddStatement("//Assert", s => s.SeparatedFromPrevious())
.AddStatement($"Assert.NotNull({crudTest.Entity.Name.ToParameterName()});")
;
});
});
}
private void DoGetAllTest(ICSharpFileBuilderTemplate template, CrudMap crudTest)
{
template.CSharpFile.OnBuild(file =>
{
var @class = template.CSharpFile.Classes.First();
var operation = crudTest.GetAll!;
@class.AddMethod("Task", $"{operation.Name}_Should{operation.Name}", method =>
{
template.AddUsing("AutoFixture");
var dtoModel = crudTest.Create.Inputs.First();
var owningAggregateId = crudTest.OwningAggregate is null ? null : $"ids.{crudTest.OwningAggregate.Name.ToPascalCase()}Id";
method
.Async()
.AddAttribute("Fact")
.AddStatement("//Arrange")
.AddStatement($"var client = new {template.GetTypeName("Intent.AspNetCore.IntegrationTesting.HttpClient", crudTest.Proxy.Id)}(CreateClient());")
.AddStatement($"var dataFactory = new TestDataFactory(WebAppFactory);", s => s.SeparatedFromPrevious())
.AddStatement($"{(crudTest.OwningAggregate is not null ? "var ids = ":"")}await dataFactory.Create{crudTest.Entity.Name}();")
.AddStatement("//Act", s => s.SeparatedFromPrevious())
.AddStatement($"var {crudTest.Entity.Name.ToParameterName().Pluralize()} = await client.{crudTest.GetAll!.Name}Async({(crudTest.OwningAggregate is not null ? $"{owningAggregateId}" : "")});")
.AddStatement("//Assert", s => s.SeparatedFromPrevious())
.AddStatement($"Assert.True({crudTest.Entity.Name.ToParameterName().Pluralize()}.Count > 0);")
;
});
});
}
private void DoDeleteTest(ICSharpFileBuilderTemplate template, CrudMap crudTest)
{
template.CSharpFile.OnBuild(file =>
{
template.AddUsing("System.Net");
template.AddUsing("AutoFixture");
var @class = template.CSharpFile.Classes.First();
var operation = crudTest.Delete!;
@class.AddMethod("Task", $"{operation.Name}_Should{operation.Name}", method =>
{
var sutId = crudTest.OwningAggregate is null ? $"{crudTest.Entity.Name.ToParameterName()}Id" : $"ids.{crudTest.Entity.Name.ToPascalCase()}Id";
var dtoModel = crudTest.Create.Inputs.First();
var owningAggregateId = crudTest.OwningAggregate is null ? null : $"ids.{crudTest.OwningAggregate.Name.ToPascalCase()}Id";
var createVarName = crudTest.OwningAggregate is null ? $"{crudTest.Entity.Name.ToParameterName()}Id" : "ids";
var deleteParams = crudTest.OwningAggregate is null ? sutId : $"{owningAggregateId}, {sutId}";
var getByIdParams = crudTest.OwningAggregate is null ? sutId : $"{owningAggregateId}, {sutId}";
method
.Async()
.AddAttribute("Fact")
.AddStatement("//Arrange")
.AddStatement($"var client = new {template.GetTypeName("Intent.AspNetCore.IntegrationTesting.HttpClient", crudTest.Proxy.Id)}(CreateClient());")
.AddStatement($"var dataFactory = new TestDataFactory(WebAppFactory);", s => s.SeparatedFromPrevious())
.AddStatement($"var {createVarName} = await dataFactory.Create{crudTest.Entity.Name}();")
.AddStatement("//Act", s => s.SeparatedFromPrevious())
.AddStatement($"await client.{crudTest.Delete!.Name}Async({deleteParams});")
.AddStatement("//Assert", s => s.SeparatedFromPrevious())
.AddStatement($"var exception = await Assert.ThrowsAsync<HttpClientRequestException>(() => client.{crudTest.GetById.Name}Async({getByIdParams}));")
.AddStatement($"Assert.Equal(HttpStatusCode.NotFound, exception.StatusCode);")
;
});
});
}
private void DoUpdateTest(ICSharpFileBuilderTemplate template, CrudMap crudTest)
{
template.AddUsing("AutoFixture");
template.CSharpFile.OnBuild(file =>
{
var @class = template.CSharpFile.Classes.First();
var operation = crudTest.Update!;
@class.AddMethod("Task", $"{operation.Name}_Should{operation.Name}", method =>
{
var sutId = crudTest.OwningAggregate is null ? $"{crudTest.Entity.Name.ToParameterName()}Id" : $"ids.{crudTest.Entity.Name.ToPascalCase()}Id";
var updateDtoModel = crudTest.Update!.Inputs.First(x => x.TypeReference?.Element.SpecializationTypeId == DtoSpecializationType || x.TypeReference?.Element.SpecializationTypeId == CommandSpecializationType);
var getDtoModel = crudTest.GetById.ReturnType!;
var owningAggregateId = crudTest.OwningAggregate is null ? null : $"ids.{crudTest.OwningAggregate.Name.ToPascalCase()}Id";
var createVarName = crudTest.OwningAggregate is null ? $"{crudTest.Entity.Name.ToParameterName()}Id" : "ids";
var getByIdParams = crudTest.OwningAggregate is null ? sutId : $"{owningAggregateId}, {sutId}";
method
.Async()
.AddAttribute("Fact")
.AddStatement("//Arrange")
.AddStatement($"var client = new {template.GetTypeName("Intent.AspNetCore.IntegrationTesting.HttpClient", crudTest.Proxy.Id)}(CreateClient());")
.AddStatement($"var dataFactory = new TestDataFactory(WebAppFactory);", s => s.SeparatedFromPrevious())
.AddStatement($"var {$"{createVarName}"} = await dataFactory.Create{crudTest.Entity.Name}();")
.AddStatement($"var command = dataFactory.CreateCommand<{template.GetTypeName(updateDtoModel.TypeReference)}>();", s => s.SeparatedFromPrevious())
.AddStatement($"command.Id = {sutId};")
.AddStatement("//Act", s => s.SeparatedFromPrevious())
.AddStatement($"await client.{crudTest.Update!.Name}Async({sutId}, command);")
.AddStatement("//Assert", s => s.SeparatedFromPrevious())
.AddStatement($"var {crudTest.Entity.Name.ToParameterName()} = await client.{crudTest.GetById.Name}Async({getByIdParams});")
.AddStatement($"Assert.NotNull({crudTest.Entity.Name.ToParameterName()});")
;
//Checking that at least 1 field changed, ideally a string field
var matchingFields = ((IElement)getDtoModel.Element).ChildElements.Select(c => c.Name)
.Intersect(
((IElement)updateDtoModel.TypeReference.Element).ChildElements.Select(c => c.Name)).Where(x => !x.EndsWith("Id")).ToList();
if (matchingFields.Any())
{
var stringField = (((IElement)getDtoModel.Element).ChildElements).FirstOrDefault(x => matchingFields.Contains(x.Name) && x.TypeReference.HasStringType());
if (stringField != null)
{
method.AddStatement($"Assert.Equal(command.{stringField.Name}, {crudTest.Entity.Name.ToParameterName()}.{stringField.Name});");
}
else
{
method.AddStatement($"Assert.Equal(command.{matchingFields.First()}, {crudTest.Entity.Name.ToParameterName()}.{matchingFields.First()});");
}
}
});
});
}
private void PopulateTestDataFactory(ICSharpFileBuilderTemplate template, List<CrudMap> crudTests)
{
TestDataFactoryHelper.PopulateTestDataFactory(template, crudTests);
}
}
}