forked from Azure/autorest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSwaggerModelerValidationTests.cs
764 lines (661 loc) · 37.8 KB
/
SwaggerModelerValidationTests.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
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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using System;
using System.IO;
using System.Linq;
using Xunit;
using System.Collections.Generic;
using AutoRest.Swagger.Validation.Core;
using AutoRest.Core.Logging;
using AutoRest.Core;
using AutoRest.Swagger.Validation;
using static AutoRest.Core.Utilities.DependencyInjection;
namespace AutoRest.Swagger.Tests
{
internal static class AssertExtensions
{
internal static void AssertOnlyValidationWarning(this IEnumerable<ValidationMessage> messages, Type validationType)
{
AssertOnlyValidationMessage(messages.Where(m => m.Severity == Category.Warning), validationType);
}
internal static void AssertOnlyValidationWarning(this IEnumerable<ValidationMessage> messages, Type validationType, int count)
{
AssertOnlyValidationMessage(messages.Where(m => m.Severity == Category.Warning), validationType, count);
}
internal static void AssertOnlyValidationMessage(this IEnumerable<ValidationMessage> messages, Type validationType)
{
// checks that the collection has one item, and that it is the correct message type.
AssertOnlyValidationMessage(messages, validationType, 1);
}
internal static void AssertOnlyValidationMessage(this IEnumerable<ValidationMessage> messages, Type validationType, int count)
{
// checks that the collection has the right number of items and each is the correct type.
Assert.Equal(count, messages.Count(message => message.Rule.GetType() == validationType));
}
}
[Collection("Validation Tests")]
public partial class SwaggerModelerValidationTests
{
private IEnumerable<ValidationMessage> ValidateSwagger(string input)
{
using (NewContext)
{
new Settings
{
CodeGenerator = "None",
Namespace = "Test",
Input = input
};
var modeler = new SwaggerModeler();
var messages = new List<LogMessage>();
Logger.Instance.AddListener(new SignalingLogListener(Category.Info, messages.Add));
modeler.Build();
return messages.OfType<ValidationMessage>();
}
}
[Fact]
public void MissingDescriptionValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "definition-missing-description.json"));
messages.AssertOnlyValidationMessage(typeof(DescriptionMissing), 2);
}
[Fact]
public void AvoidMsdnReferencesValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "definition-contains-msdn-reference.json"));
messages.AssertOnlyValidationMessage(typeof(AvoidMsdnReferences), 4);
}
[Fact]
public void BooleanPropertiesValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "boolean-properties.json"));
messages.AssertOnlyValidationMessage(typeof(BooleanPropertyNotRecommended), 4);
}
[Fact]
public void DefaultValueInEnumValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "default-value-not-in-enum.json"));
messages.AssertOnlyValidationMessage(typeof(DefaultMustBeInEnum));
}
[Fact]
public void EmptyClientNameValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "empty-client-name-extension.json"));
messages.AssertOnlyValidationMessage(typeof(NonEmptyClientName));
}
[Fact]
public void UniqueResourcePathsValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "network-interfaces-api.json"));
messages.AssertOnlyValidationMessage(typeof(UniqueResourcePaths));
}
[Fact]
public void AnonymousSchemasDiscouragedValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "anonymous-response-type.json"));
messages.AssertOnlyValidationMessage(typeof(AvoidAnonymousTypes));
}
[Fact]
public void AnonymousParameterSchemaValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "anonymous-parameter-type.json"));
messages.AssertOnlyValidationMessage(typeof(AnonymousBodyParameter));
}
[Fact]
public void OperationParametersValidation()
{
// ignore ServiceDefinitionParameters validation rule since it overlaps with this
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "operations-invalid-parameters.json"))
.Where(msg => msg.Rule.GetType().Name != "ServiceDefinitionParameters");
messages.AssertOnlyValidationMessage(typeof(OperationParametersValidation), 1);
}
[Fact]
public void ServiceDefinitionParametersValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "service-def-invalid-parameters.json"));
messages.AssertOnlyValidationMessage(typeof(ServiceDefinitionParameters), 2);
}
[Fact]
public void OperationGroupSingleUnderscoreValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "operation-group-underscores.json"));
messages.AssertOnlyValidationMessage(typeof(OneUnderscoreInOperationId));
}
[Fact]
public void NonAppJsonTypeOperationForConsumes()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "non-app-json-operation-consumes.json"));
messages.AssertOnlyValidationWarning(typeof(NonAppJsonTypeWarning));
}
[Fact]
public void ProvidersPathValidate()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "providers-path.json"));
messages.AssertOnlyValidationWarning(typeof(ProvidersPathValidation), 2);
}
[Fact]
public void NonAppJsonTypeOperationForProduces()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "non-app-json-operation-produces.json"));
messages.AssertOnlyValidationWarning(typeof(NonAppJsonTypeWarning));
}
[Fact]
public void NonAppJsonTypeServiceDefinitionForProduces()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "non-app-json-service-def-produces.json"));
messages.AssertOnlyValidationWarning(typeof(NonAppJsonTypeWarning));
}
[Fact]
public void NonAppJsonTypeServiceDefinitionForConsumes()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "non-app-json-service-def-consumes.json"));
messages.AssertOnlyValidationWarning(typeof(NonAppJsonTypeWarning));
}
[Fact]
public void NonHttpsServiceDefinitionForScheme()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "non-https-service-def-scheme.json"));
messages.AssertOnlyValidationWarning(typeof(SupportedSchemesWarning));
}
[Fact]
public void NonHttpsOperationsForScheme()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "non-https-operations-scheme.json"));
messages.AssertOnlyValidationWarning(typeof(SupportedSchemesWarning));
}
[Fact]
public void XmsPathNotInPathsValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "xms-path-not-in-paths.json"));
messages.AssertOnlyValidationMessage(typeof(XmsPathsMustOverloadPaths));
}
[Fact]
public void InvalidFormatValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "invalid-format.json"));
messages.AssertOnlyValidationMessage(typeof(ValidFormats));
}
[Fact]
public void ListOperationsNamingValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "list-operations-naming.json"));
messages.AssertOnlyValidationMessage(typeof(ListOperationNamingWarning), 2);
}
[Fact]
public void ArmResourcePropertiesBagValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "arm-resource-properties-bag.json"));
messages.AssertOnlyValidationMessage(typeof(ArmResourcePropertiesBag), 1);
}
[Fact]
public void CollectionObjectsPropertiesNamingValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "collection-objects-naming.json"));
messages.AssertOnlyValidationMessage(typeof(CollectionObjectPropertiesNamingValidation), 2);
}
[Fact]
public void BodyTopLevelPropertiesValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "body-top-level-properties.json"));
messages.AssertOnlyValidationMessage(typeof(BodyTopLevelProperties), 2);
}
[Fact]
public void PropertyNameCasingValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "property-names-casing.json"));
messages.AssertOnlyValidationMessage(typeof(BodyPropertiesNamesCamelCase), 1);
messages.AssertOnlyValidationMessage(typeof(DefinitionsPropertiesNamesCamelCase), 1);
}
[Fact]
public void NestedPropertiesValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "nested-properties.json"));
messages.AssertOnlyValidationMessage(typeof(AvoidNestedProperties));
}
[Fact]
public void OperationDescriptionValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "operation-missing-description.json"));
messages.AssertOnlyValidationMessage(typeof(OperationDescriptionRequired));
}
[Fact]
public void ParameterDescriptionValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "parameter-missing-description.json"));
messages.AssertOnlyValidationMessage(typeof(ParameterDescriptionRequired), 2);
}
[Fact]
public void PageableNextLinkNotModeledValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "pageable-nextlink-not-modeled.json"));
messages.AssertOnlyValidationMessage(typeof(NextLinkPropertyMustExist));
}
[Fact]
public void Pageable200ResponseNotModeledValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "pageable-no-200-response.json"));
messages.Any(m => m.Rule.GetType() == typeof(PageableRequires200Response));
}
[Fact]
public void OperationNameValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "operation-name-not-valid.json"));
messages.AssertOnlyValidationMessage(typeof(GetOperationNameValidation), 1);
messages.AssertOnlyValidationMessage(typeof(PutOperationNameValidation), 1);
messages.AssertOnlyValidationMessage(typeof(DeleteOperationNameValidation), 1);
}
[Fact]
public void LongRunningResponseForPutValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource","Swagger", "Validation", "long-running-invalid-response-put.json"));
messages.AssertOnlyValidationMessage(typeof(LongRunningResponseValidationRule));
}
[Fact]
public void LongRunningResponseForPostValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "long-running-invalid-response-post.json"));
messages.AssertOnlyValidationMessage(typeof(LongRunningResponseValidationRule));
}
[Fact]
public void LongRunningResponseForDeleteValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "long-running-invalid-response-delete.json"));
messages.AssertOnlyValidationMessage(typeof(LongRunningResponseValidationRule));
}
[Fact]
public void MutabilityNotModeledWithReadOnlyValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "mutability-invalid-values-for-readonly.json"));
messages.AssertOnlyValidationMessage(typeof(MutabilityWithReadOnlyRule), 2);
}
[Fact]
public void VersionFormatValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "swagger-version-validation.json"));
messages.AssertOnlyValidationMessage(typeof(APIVersionPattern), 1);
}
[Fact]
public void GuidUsageValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "swagger-guid-validation.json"));
messages.AssertOnlyValidationMessage(typeof(GuidValidation), 1);
}
[Fact]
public void DeleteRequestBodyValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "swagger-delete-request-body-validation.json"));
messages.AssertOnlyValidationMessage(typeof(DeleteMustNotHaveRequestBody), 1);
}
[Fact]
public void ResourceExtensionValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "swagger-ext-msresource-validation.json"));
messages.AssertOnlyValidationMessage(typeof(ResourceIsMsResourceValidation), 1);
}
[Fact]
public void MsClientNameExtensionValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "swagger-ext-msclientname-validation.json"));
messages.AssertOnlyValidationMessage(typeof(XmsClientNamePropertyValidation), 1);
messages.AssertOnlyValidationMessage(typeof(XmsClientNameParameterValidation), 1);
}
[Fact]
public void OperationsApiValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "swagger-operations-api-validation.json"));
messages.AssertOnlyValidationMessage(typeof(OperationsAPIImplementationValidation), 1);
}
[Fact]
public void ResourceModelValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "swagger-ext-resource-validation.json"));
messages.AssertOnlyValidationMessage(typeof(ResourceModelValidation), 1);
}
[Fact]
public void SkuModelValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "swagger-skumodel-validation.json"));
messages.AssertOnlyValidationMessage(typeof(SkuModelValidation), 1);
}
[Fact]
public void TrackedResourceGetOperationValidation2()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource","Swagger", "Validation", "swagger-tracked-resource-1-validation.json"));
messages.AssertOnlyValidationMessage(typeof(TrackedResourceGetOperationValidation), 1);
}
[Fact]
public void TrackedResourceListByResourceGroupValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource","Swagger", "Validation", "swagger-tracked-resource-2-validation.json"));
messages.AssertOnlyValidationMessage(typeof(TrackedResourceListByResourceGroup), 1);
}
[Fact]
public void TrackedResourcePatchOperationValidationValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "tracked-resource-patch-operation.json"));
messages.AssertOnlyValidationMessage(typeof(TrackedResourcePatchOperationValidation), 1);
}
[Fact]
public void TrackedResourceGetOperationValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "tracked-resource-get-operation.json"));
messages.AssertOnlyValidationMessage(typeof(TrackedResourceGetOperationValidation), 1);
}
[Fact]
public void TrackedResourceListBySubscriptionValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource","Swagger", "Validation", "swagger-tracked-resource-3-validation.json"));
messages.AssertOnlyValidationMessage(typeof(TrackedResourceListBySubscription), 1);
}
[Fact]
public void TrackedResourceListByImmediateParentValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "swagger-list-by-immediate-parent.json"));
messages.AssertOnlyValidationMessage(typeof(TrackedResourceListByImmediateParent), 1);
}
[Fact]
public void TrackedResourceListByImmediateParentWithOperationValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "swagger-list-by-immediate-parent-2.json"));
messages.AssertOnlyValidationMessage(typeof(TrackedResourceListByImmediateParent), 1);
}
[Fact]
public void PutGetPatchResponseValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "swagger-putgetpatch-response-validation.json"));
messages.AssertOnlyValidationMessage(typeof(PutGetPatchResponseValidation), 1);
}
[Fact]
public void SecurityDefinitionStructurePresenceValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "security-definitions-validations-1.json"));
messages.AssertOnlyValidationMessage(typeof(SecurityDefinitionsStructureValidation), 1);
}
[Fact]
public void SecurityDefinitionStructureEmptyValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "security-definitions-validations-2.json"));
messages.AssertOnlyValidationMessage(typeof(SecurityDefinitionsStructureValidation), 1);
}
[Fact]
public void SecurityDefinitionStructureMultipleEntriesValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "security-definitions-validations-3.json"));
messages.AssertOnlyValidationMessage(typeof(SecurityDefinitionsStructureValidation), 1);
}
[Fact]
public void SecurityDefinitionStructureIncorrectKeyValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "security-definitions-validations-4.json"));
messages.AssertOnlyValidationMessage(typeof(SecurityDefinitionsStructureValidation), 1);
}
[Fact]
public void SecurityDefinitionStructureMissingDescriptionValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "security-definitions-validations-5.json"));
messages.AssertOnlyValidationMessage(typeof(SecurityDefinitionsStructureValidation), 1);
}
[Fact]
public void SecurityDefinitionStructureEmptyDescriptionValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "security-definitions-validations-6.json"));
messages.AssertOnlyValidationMessage(typeof(SecurityDefinitionsStructureValidation), 1);
}
[Fact]
public void SecurityDefinitionStructureIncorrectDefValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "security-definitions-validations-7.json"));
messages.AssertOnlyValidationMessage(typeof(SecurityDefinitionsStructureValidation), 1);
}
[Fact]
public void SecurityDefinitionStructureMissingScopesValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "security-definitions-validations-8.json"));
messages.AssertOnlyValidationMessage(typeof(SecurityDefinitionsStructureValidation), 1);
}
[Fact]
public void SecurityDefinitionStructureEmptyScopesValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "security-definitions-validations-9.json"));
messages.AssertOnlyValidationMessage(typeof(SecurityDefinitionsStructureValidation), 1);
}
[Fact]
public void SecurityDefinitionStructureMultipleScopesValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "security-definitions-validations-10.json"));
messages.AssertOnlyValidationMessage(typeof(SecurityDefinitionsStructureValidation), 1);
}
[Fact]
public void SecurityDefinitionStructureMissingScopesDescriptionValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "security-definitions-validations-11.json"));
messages.AssertOnlyValidationMessage(typeof(SecurityDefinitionsStructureValidation), 1);
}
public void RequiredReadOnlyPropertiesValidationInDefinitions()
{
// This test validates if a definition has required properties which are marked as readonly true
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "required-readonly-properties.json"));
messages.AssertOnlyValidationMessage(typeof(RequiredReadOnlyPropertiesValidation), 1);
}
[Fact]
public void RequiredReadOnlyPropertiesValidationInResponses()
{
// This test validates if a definition has required properties which are marked as readonly true
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "required-readonly-properties-2.json"));
messages.AssertOnlyValidationMessage(typeof(RequiredReadOnlyPropertiesValidation), 1);
}
[Fact]
public void RequiredReadOnlyPropertiesValidationInParameters()
{
// This test validates if a definition has required properties which are marked as readonly true
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "required-readonly-properties-3.json"));
messages.AssertOnlyValidationMessage(typeof(RequiredReadOnlyPropertiesValidation), 1);
}
[Fact]
public void RequiredReadOnlyPropertiesValidationInNestedSchema()
{
// This test validates if a definition has required properties which are marked as readonly true
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "required-readonly-properties-4.json"));
messages.AssertOnlyValidationMessage(typeof(RequiredReadOnlyPropertiesValidation), 1);
}
[Fact]
public void RequiredReadOnlyPropertiesValidationInItems()
{
// This test validates if a definition has required properties which are marked as readonly true
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "required-readonly-properties-5.json"));
messages.AssertOnlyValidationMessage(typeof(RequiredReadOnlyPropertiesValidation), 1);
}
[Fact]
public void DefaultValuedInPropertiesInPatchRequestValidation()
{
// This test validates if a definition has required properties which are marked as readonly true
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "default-valued-properties-in-patch-request.json"));
messages.AssertOnlyValidationMessage(typeof(PatchBodyParametersSchemaValidation), 1);
}
[Fact]
public void RequiredPropertiesInPatchRequestValidation()
{
// This test validates if a definition has required properties which are marked as readonly true
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "req-properties-in-patch-request.json"));
messages.AssertOnlyValidationMessage(typeof(PatchBodyParametersSchemaValidation), 1);
}
[Fact]
public void BodyParametersInlineValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "body-parameters-validation-1.json"));
messages.AssertOnlyValidationMessage(typeof(BodyParametersValidation), 1);
}
[Fact]
public void BodyParametersReferencedValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "body-parameters-validation-2.json"));
messages.AssertOnlyValidationMessage(typeof(BodyParametersValidation), 1);
}
[Fact]
public void XmsExamplesProvidedValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "xms-examples-absent.json"));
messages.AssertOnlyValidationMessage(typeof(XmsExamplesProvidedValidation), 2);
}
[Fact]
public void PutResponseResourceValidationTest()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "put-response-resource-validation.json"));
messages.AssertOnlyValidationMessage(typeof(PutResponseResourceValidation), 1);
}
[Fact]
public void EmptyParameterNameValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "empty-parameter-name.json"));
messages.AssertOnlyValidationMessage(typeof(ParameterNameValidation), 2);
}
}
#region Positive tests
public partial class SwaggerModelerValidationTests
{
/// <summary>
/// Verifies that a clean Swagger file does not result in any validation errors
/// </summary>
[Fact]
public void CleanFileValidation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "positive", "clean-complex-spec.json"));
Assert.Empty(messages.Where(m => m.Severity >= Category.Warning));
}
/// <summary>
///
/// </summary>
[Fact]
public void ValidCollectionObjectsPropertiesName()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "positive", "collection-objects-naming-valid.json"));
Assert.Empty(messages.Where(m => m.Severity >= Category.Warning));
}
/// <summary>
/// Verifies that a clean Swagger file does not result in any validation errors
/// </summary>
[Fact]
public void RequiredPropertyDefinedAllOf()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "positive", "required-property-defined-allof.json"));
Assert.Empty(messages.Where(m => m.Severity >= Category.Warning));
}
/// <summary>
/// Verifies that a clean Swagger file does not result in any validation errors
/// </summary>
[Fact]
public void PageableNextLinkDefinedAllOf()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "positive", "pageable-nextlink-defined-allof.json"));
Assert.Empty(messages.Where(m => m.Severity >= Category.Warning));
}
/// <summary>
/// Verifies that a x-ms-long-running extension response modeled correctly
/// </summary>
[Fact]
public void LongRunningResponseDefined()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource","Swagger", "Validation", "positive", "long-running-valid-response.json"));
messages.AssertOnlyValidationMessage(typeof(LongRunningResponseValidationRule), 0);
}
/// <summary>
/// Verifies that tracked resource has a patch operation
/// </summary>
[Fact]
public void ValidTrackedResourcePatchOperation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "positive", "tracked-resource-patch-valid-operation.json"));
messages.AssertOnlyValidationMessage(typeof(TrackedResourcePatchOperationValidation), 0);
}
/// <summary>
/// Verifies that tracked resource has a get operation
/// </summary>
[Fact]
public void ValidTrackedResourceGetOperation()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "positive", "tracked-resource-get-valid-operation.json"));
messages.AssertOnlyValidationMessage(typeof(TrackedResourceGetOperationValidation), 0);
}
/// <summary>
/// Verifies that listing operations (any operation that returns an array or is of xmspageable type)
/// are correctly named
/// </summary>
[Fact]
public void ListOperationsCorrectlyNamed()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "positive", "list-operations-valid-naming.json"));
messages.AssertOnlyValidationMessage(typeof(ListOperationNamingWarning), 0);
}
/// Verifies that a providers path is of proper format
/// </summary>
[Fact]
public void ProvidersPathValidJson()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "positive", "providers-path-valid.json"));
messages.AssertOnlyValidationWarning(typeof(ProvidersPathValidation), 0);
}
/// <summary>
/// Verifies that property names follow camelCase style
/// </summary>
[Fact]
public void ValidPropertyNameCasing()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "positive", "property-names-casing-valid.json"));
messages.AssertOnlyValidationMessage(typeof(BodyPropertiesNamesCamelCase), 0);
messages.AssertOnlyValidationMessage(typeof(DefinitionsPropertiesNamesCamelCase), 0);
}
[Fact]
public void ValidServiceDefinitionParameters()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "positive", "service-def-valid-parameters.json"));
messages.AssertOnlyValidationMessage(typeof(ServiceDefinitionParameters), 0);
}
[Fact]
public void ValidOperationParameters()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "positive", "operations-valid-parameters.json"));
messages.AssertOnlyValidationMessage(typeof(OperationParametersValidation), 0);
}
[Fact]
public void ValidArmResourcePropertiesBag()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "positive", "arm-resource-properties-valid.json"));
messages.AssertOnlyValidationMessage(typeof(ArmResourcePropertiesBag), 0);
}
/// <summary>
/// Verifies resource models are correctly identified
/// </summary>
[Fact]
public void ValidResourceModels()
{
var filePath = Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "positive", "valid-resource-model-definitions.json");
var fileText = System.IO.File.ReadAllText(filePath);
var servDef = SwaggerParser.Parse(filePath, fileText);
Uri uriPath = null;
Uri.TryCreate(filePath, UriKind.RelativeOrAbsolute, out uriPath);
var context = new RuleContext(servDef, uriPath);
Assert.Equal(4, context.ResourceModels.Count());
Assert.Equal(1, context.TrackedResourceModels.Count());
Assert.Equal(3, context.ProxyResourceModels.Count());
}
/// <summary>
/// Verifies that sku object
/// </summary>
[Fact]
public void ValidSkuObjectStructure()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "positive", "swagger-skumodel-validation-valid.json"));
messages.AssertOnlyValidationMessage(typeof(SkuModelValidation), 0);
}
/// <summary>
/// Verifies resource model readonly properties
/// </summary>
[Fact]
public void ValidResourceModelReadOnlyProperties()
{
var messages = ValidateSwagger(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "Swagger", "Validation", "positive", "valid-resource-model-readonly-props.json"));
messages.AssertOnlyValidationMessage(typeof(ResourceModelValidation), 0);
}
}
#endregion
}