-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNewControllerTest.cs
More file actions
581 lines (523 loc) · 21.5 KB
/
NewControllerTest.cs
File metadata and controls
581 lines (523 loc) · 21.5 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
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
using System.IO;
using System.IO.Compression;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Moq;
using Steeltoe.Common.Utils.Diagnostics;
using Steeltoe.NetCoreToolService.Controllers;
using Steeltoe.NetCoreToolService.Models;
using Xunit;
namespace Steeltoe.NetCoreToolService.Test.Controllers
{
public class NewControllerTest
{
/* ----------------------------------------------------------------- *
* positive tests *
* ----------------------------------------------------------------- */
[Fact]
public async Task GetTemplates_Should_Return_AllTemplates()
{
// Arrange
var executor = new Mock<ICommandExecutor>();
executor.Setup(expression: c => c.ExecuteAsync($"{NetCoreTool.Command} new list", null, -1))
.ReturnsAsync(new CommandResult
{
ExitCode = 0,
Output = @"
------------------- -------- --------- ---------
My Template myt lang tags
My Other Template myot otherlang othertags
",
}
);
var controller = new NewController(executor.Object);
// Act
var result = await controller.GetTemplates();
// Assert
var okResult = Assert.IsType<OkObjectResult>(result);
var templates = Assert.IsType<TemplateDictionary>(okResult.Value);
templates.Count.Should().Be(2);
templates.Keys.Should().Contain("myt");
templates["myt"].Name.Should().Be("My Template");
templates["myt"].Languages.Should().Be("lang");
templates["myt"].Tags.Should().Be("tags");
templates.Keys.Should().Contain("myot");
templates["myot"].Name.Should().Be("My Other Template");
templates["myot"].Languages.Should().Be("otherlang");
templates["myot"].Tags.Should().Be("othertags");
}
[Fact]
public async Task InstallTemplates_Should_Return_InstalledTemplates()
{
// Arrange
var executor = new Mock<ICommandExecutor>();
executor.SetupSequence(c => c.ExecuteAsync($"{NetCoreTool.Command} new list", null, -1))
.ReturnsAsync(new CommandResult
{
ExitCode = 0,
Output = @"
------------------- -------- --------- ---------
My Template myt lang tags
My Other Template myot otherlang othertags
",
}
)
.ReturnsAsync(new CommandResult
{
ExitCode = 0,
Output = @"
------------------- -------- --------- ----------
A New Template ant smalltalk newstuff
My Template myt lang tags
My Other Template myot otherlang othertags
Other New Template ont bigtalk otherstuff
",
}
);
executor.Setup(c => c.ExecuteAsync($"{NetCoreTool.Command} new install My.Templates", null, -1))
.ReturnsAsync(new CommandResult
{
ExitCode = 0,
Output = "",
}
);
var controller = new NewController(executor.Object);
// Act
var result = await controller.InstallTemplates("My.Templates");
// Assert
var createdResult = Assert.IsType<CreatedAtActionResult>(result);
var templates = Assert.IsType<TemplateDictionary>(createdResult.Value);
templates.Count.Should().Be(2);
templates["ant"].Name.Should().Be("A New Template");
templates["ant"].Languages.Should().Be("smalltalk");
templates["ant"].Tags.Should().Be("newstuff");
templates["ont"].Name.Should().Be("Other New Template");
templates["ont"].Languages.Should().Be("bigtalk");
templates["ont"].Tags.Should().Be("otherstuff");
}
[Fact]
public async Task UninstallTemplates_Should_Return_UninstalledTemplates()
{
// Arrange
var executor = new Mock<ICommandExecutor>();
executor.SetupSequence(c => c.ExecuteAsync($"{NetCoreTool.Command} new list", null, -1))
.ReturnsAsync(new CommandResult
{
ExitCode = 0,
Output = @"
------------------- -------- --------- ----------
A New Template ant smalltalk newstuff
My Template myt lang tags
My Other Template myot otherlang othertags
Other New Template ont bigtalk otherstuff
",
}
)
.ReturnsAsync(new CommandResult
{
ExitCode = 0,
Output = @"
------------------- -------- --------- ---------
My Template myt lang tags
My Other Template myot otherlang othertags
",
}
);
executor.Setup(c => c.ExecuteAsync($"{NetCoreTool.Command} new uninstall My.Templates", null, -1))
.ReturnsAsync(new CommandResult
{
ExitCode = 0,
Output = "",
}
);
var controller = new NewController(executor.Object);
// Act
var result = await controller.UninstallTemplates("My.Templates");
// Assert
var ok = Assert.IsType<OkObjectResult>(result);
var templates = Assert.IsType<TemplateDictionary>(ok.Value);
templates.Count.Should().Be(2);
templates["ant"].Name.Should().Be("A New Template");
templates["ant"].Languages.Should().Be("smalltalk");
templates["ant"].Tags.Should().Be("newstuff");
templates["ont"].Name.Should().Be("Other New Template");
templates["ont"].Languages.Should().Be("bigtalk");
templates["ont"].Tags.Should().Be("otherstuff");
}
[Fact]
public async Task GetTemplateHelp_Should_Return_Help()
{
// Arrange
var executor = new Mock<ICommandExecutor>();
executor.Setup(c => c.ExecuteAsync($"{NetCoreTool.Command} new mytemplate --help", null, -1))
.ReturnsAsync(new CommandResult
{
ExitCode = 0,
Output = @"
Some helpful tips for you
",
}
);
var controller = new NewController(executor.Object);
// Act
var result = await controller.GetTemplateHelp("mytemplate");
// Assert
var ok = Assert.IsType<OkObjectResult>(result);
var help = Assert.IsType<string>(ok.Value);
help.Should().Be("Some helpful tips for you");
}
[Fact]
public async Task GetTemplateProject_Should_Return_ProjectPackage()
{
// Arrange
var executor = new Mock<ICommandExecutor>();
executor.Setup(c => c.ExecuteAsync($"{NetCoreTool.Command} new mytemplate --output=Sample",
It.IsAny<string>(), -1))
.ReturnsAsync(new CommandResult
{
ExitCode = 0,
Output = @"
The template ""mytemplate"" was created successfully.
",
Error = "",
}
);
var controller = new NewController(executor.Object);
// Act
var result = await controller.GetTemplateProject("mytemplate");
// Assert
Assert.IsType<FileContentResult>(result);
}
[Fact]
public async Task GetTemplateProject_Should_Use_Defaults()
{
// Arrange
var executor = new Mock<ICommandExecutor>();
executor.Setup(c => c.ExecuteAsync($"{NetCoreTool.Command} new mytemplate --output=Sample",
It.IsAny<string>(), -1))
.ReturnsAsync(new CommandResult
{
ExitCode = 0,
Output = @"
The template ""mytemplate"" was created successfully.
",
Error = "",
}
);
var controller = new NewController(executor.Object);
// Act
var result = await controller.GetTemplateProject("mytemplate");
// Assert
var file = Assert.IsType<FileContentResult>(result);
file.ContentType.Should().Be("application/zip");
file.FileDownloadName.Should().Be("Sample.zip");
}
[Fact]
public async Task GetTemplateProject_Can_Specify_Output()
{
// Arrange
var executor = new Mock<ICommandExecutor>();
executor.Setup(c => c.ExecuteAsync($"{NetCoreTool.Command} new mytemplate --output=Joe",
It.IsAny<string>(), -1))
.ReturnsAsync(new CommandResult
{
ExitCode = 0,
Output = @"
The template ""mytemplate"" was created successfully.
",
Error = "",
}
);
var controller = new NewController(executor.Object);
// Act
var result = await controller.GetTemplateProject("mytemplate", "output=Joe");
// Assert
var file = Assert.IsType<FileContentResult>(result);
file.FileDownloadName.Should().StartWith("Joe.");
}
[Fact]
public async Task GetTemplateProject_Can_Specify_ZipPackaging()
{
// Arrange
var executor = new Mock<ICommandExecutor>();
executor.Setup(c => c.ExecuteAsync($"{NetCoreTool.Command} new mytemplate --output=Sample",
It.IsAny<string>(), -1))
.ReturnsAsync(new CommandResult
{
ExitCode = 0,
Output = @"
The template ""mytemplate"" was created successfully.
",
Error = "",
}
);
var controller = new NewController(executor.Object);
// Act
var result = await controller.GetTemplateProject("mytemplate", packaging: "zip");
// Assert
var file = Assert.IsType<FileContentResult>(result);
var _ = new ZipArchive(new MemoryStream(file.FileContents));
}
/* ----------------------------------------------------------------- *
* negative tests *
* ----------------------------------------------------------------- */
[Fact]
public async Task InstallTemplates_UnknownNuGet_Should_Return_BadRequest()
{
// Arrange
var executor = new Mock<ICommandExecutor>();
executor.Setup(c => c.ExecuteAsync($"{NetCoreTool.Command} new list", null, -1))
.ReturnsAsync(new CommandResult
{
ExitCode = 0,
Output = @"
------------------- -------- --------- ---------
My Template myt lang tags
My Other Template myot otherlang othertags
",
}
);
executor.Setup(c =>
c.ExecuteAsync($"{NetCoreTool.Command} new install No.Such.Template", null, -1))
.ReturnsAsync(new CommandResult
{
ExitCode = 2,
Output = @"
... error NU1101: Unable to find package No.Such.Template. No packages exist with this id in source(s): myget.org, nuget.org
Failed to restore ...
",
});
var controller = new NewController(executor.Object);
// Act
var result = await controller.InstallTemplates("No.Such.Template");
// Assert
var badRequest = Assert.IsType<BadRequestObjectResult>(result);
badRequest.Value.Should()
.Be(
"Unable to find package No.Such.Template. No packages exist with this id in source(s): myget.org, nuget.org");
}
[Fact]
public async Task UninstallTemplates_UnknownNuGet_Should_Return_NotFound()
{
// Arrange
var executor = new Mock<ICommandExecutor>();
executor.Setup(c => c.ExecuteAsync($"{NetCoreTool.Command} new list", null, -1))
.ReturnsAsync(new CommandResult
{
ExitCode = 0,
Output = @"
------------------- -------- --------- ---------
",
}
);
executor.Setup(c => c.ExecuteAsync($"{NetCoreTool.Command} new uninstall My.Templates", null, -1))
.ReturnsAsync(new CommandResult
{
ExitCode = 0,
Output = @"
Could not find something to uninstall called 'My.Templates'.
",
}
);
var controller = new NewController(executor.Object);
// Act
var result = await controller.UninstallTemplates("My.Templates");
// Assert
var notFound = Assert.IsType<NotFoundObjectResult>(result);
notFound.Value.Should().Be("No templates with NuGet ID 'My.Templates' installed.");
}
[Fact]
public async Task GetTemplateHelp_UnknownTemplate_Should_Return_NotFound()
{
// Arrange
var executor = new Mock<ICommandExecutor>();
executor.Setup(c => c.ExecuteAsync($"{NetCoreTool.Command} new nosuchtemplate --help", null, -1))
.ReturnsAsync(new CommandResult
{
ExitCode = 6,
Error = @"
No templates found matching: 'nosuchtemplate'.
",
}
);
var controller = new NewController(executor.Object);
// Act
var result = await controller.GetTemplateHelp("nosuchtemplate");
// Assert
var notFound = Assert.IsType<NotFoundObjectResult>(result);
notFound.Value.Should().Be("No templates found matching: 'nosuchtemplate'.");
}
[Fact]
public async Task GetTemplateProject_UnknownTemplate_Should_Return_NotFound()
{
// Arrange
var executor = new Mock<ICommandExecutor>();
executor.Setup(c =>
c.ExecuteAsync($"{NetCoreTool.Command} new nosuchtemplate --output=Sample", It.IsAny<string>(), -1))
.ReturnsAsync(new CommandResult
{
ExitCode = 14,
Error = @"
No templates found matching: 'nosuchtemplate'.
",
}
);
var controller = new NewController(executor.Object);
// Act
var result = await controller.GetTemplateProject("nosuchtemplate");
// Assert
var notFound = Assert.IsType<NotFoundObjectResult>(result);
notFound.Value.Should().Be("Template 'nosuchtemplate' not found.");
}
[Fact]
public async Task GetTemplateProject_UnknownSwitch1_Should_Return_NotFound()
{
// Arrange
var executor = new Mock<ICommandExecutor>();
executor.Setup(c =>
c.ExecuteAsync($"{NetCoreTool.Command} new mytemplate --output=Sample --unknown-switch",
It.IsAny<string>(), -1))
.ReturnsAsync(new CommandResult
{
ExitCode = 5,
Error = @"
Invalid input switch:
--unknown-switch
For a list of valid options, run 'dotnet new webapi --help'.
",
}
);
var controller = new NewController(executor.Object);
// Act
var result = await controller.GetTemplateProject("mytemplate", "unknown-switch");
// Assert
var notFound = Assert.IsType<NotFoundObjectResult>(result);
notFound.Value.Should().Be("Switch 'unknown-switch' not found.");
}
[Fact]
public async Task GetTemplateProject_UnknownSwitch2_Should_Return_NotFound()
{
// Arrange
var executor = new Mock<ICommandExecutor>();
executor.Setup(c =>
c.ExecuteAsync($"{NetCoreTool.Command} new mytemplate --output=Sample --unknown-switch",
It.IsAny<string>(), -1))
.ReturnsAsync(new CommandResult
{
ExitCode = 5,
Error = @"
Error: Invalid option(s):
--unknown-switch
'--unknown-switch' is not a valid option
",
}
);
var controller = new NewController(executor.Object);
// Act
var result = await controller.GetTemplateProject("mytemplate", "unknown-switch");
// Assert
var notFound = Assert.IsType<NotFoundObjectResult>(result);
notFound.Value.Should().Be("Switch 'unknown-switch' not found.");
}
[Fact]
public async Task GetTemplateProject_UnknownParameter_Should_Return_NotFound()
{
// Arrange
var executor = new Mock<ICommandExecutor>();
executor.Setup(c =>
c.ExecuteAsync($"{NetCoreTool.Command} new mytemplate --output=Sample --myoption=unknown",
It.IsAny<string>(), -1))
.ReturnsAsync(new CommandResult
{
ExitCode = 0,
Output = "",
Error = @"
Error: Invalid parameter(s):
--myoption unknown
'unknown' is not a valid value for --myoption
",
}
);
var controller = new NewController(executor.Object);
// Act
var result = await controller.GetTemplateProject("mytemplate", "myoption=unknown");
// Assert
var notFound = Assert.IsType<NotFoundObjectResult>(result);
notFound.Value.Should().Be("Option 'myoption' parameter 'unknown' not found.");
}
[Fact]
public async Task GetTemplateProject_UnknownPackaging_Should_Return_BadRequest()
{
// Arrange
var executor = new Mock<ICommandExecutor>();
executor.Setup(c =>
c.ExecuteAsync($"{NetCoreTool.Command} new mytemplate --output=Sample --myoption=unknown",
It.IsAny<string>(), -1))
.ReturnsAsync(new CommandResult
{
ExitCode = 0,
Output = "",
Error = @"
Error: Invalid parameter(s):
--myoption unknown
'unknown' is not a valid value for --myoption
",
}
);
var controller = new NewController(executor.Object);
// Act
var result = await controller.GetTemplateProject("mytemplate", packaging: "acme-packaging");
// Assert
var badRequest = Assert.IsType<BadRequestObjectResult>(result);
badRequest.Value.Should().Be("Unknown or unsupported packaging 'acme-packaging'.");
}
[Fact]
public async Task GetTemplateProject_UnknownError_Should_Return_InternalServerError()
{
// Arrange
var executor = new Mock<ICommandExecutor>();
executor.Setup(c => c.ExecuteAsync(It.IsAny<string>(), It.IsAny<string>(), -1))
.ReturnsAsync(new CommandResult
{
ExitCode = 1,
Output = "",
Error = @"
Something bad happened.
",
}
);
var controller = new NewController(executor.Object);
// Act
var result = await controller.GetTemplateProject("mytemplate");
// Assert
var internalServerError = Assert.IsType<ObjectResult>(result);
internalServerError.StatusCode.Should().Be(StatusCodes.Status500InternalServerError);
internalServerError.Value.Should().Be("Something bad happened.");
}
[Fact]
public async Task GetTemplateProject_UnknownOutput_Should_Return_InternalServerError()
{
// Arrange
var executor = new Mock<ICommandExecutor>();
executor.Setup(c => c.ExecuteAsync(It.IsAny<string>(), It.IsAny<string>(), -1))
.ReturnsAsync(new CommandResult
{
ExitCode = 0,
Output = @"
Unexpected output.
",
Error = "",
}
);
var controller = new NewController(executor.Object);
// Act
var result = await controller.GetTemplateProject("mytemplate");
// Assert
var internalServerError = Assert.IsType<ObjectResult>(result);
internalServerError.StatusCode.Should().Be(StatusCodes.Status500InternalServerError);
internalServerError.Value.Should().Be("Unexpected output.");
}
}
}