|
| 1 | +using ContractModelsAttributeCheck; |
| 2 | +using FluentAssertions; |
| 3 | +using Microsoft.AspNetCore.Mvc.ApiExplorer; |
| 4 | +using Microsoft.AspNetCore.Mvc.Testing; |
| 5 | +using Microsoft.Extensions.DependencyInjection; |
| 6 | +using SampleWebApp.Contract.V1; |
| 7 | +using SampleWebApp.Contract.V2; |
| 8 | +using System; |
| 9 | +using System.Linq; |
| 10 | +using System.Text.Json.Serialization; |
| 11 | +using System.Threading.Tasks; |
| 12 | +using Xunit; |
| 13 | + |
| 14 | +namespace SampleWebApp.Tests |
| 15 | +{ |
| 16 | + public class ValidateContractModelsAttributesTest: IClassFixture<WebApplicationFactory<SampleWebApp.Startup>> |
| 17 | + { |
| 18 | + private readonly WebApplicationFactory<SampleWebApp.Startup> _factory; |
| 19 | + private readonly Type[] _attributes = new[] { typeof(JsonPropertyNameAttribute), typeof(JsonIgnoreAttribute) }; |
| 20 | + |
| 21 | + public ValidateContractModelsAttributesTest(WebApplicationFactory<SampleWebApp.Startup> factory) |
| 22 | + { |
| 23 | + _factory = factory; |
| 24 | + } |
| 25 | + |
| 26 | + [Theory(DisplayName ="Don't include models that come FromQuery")] |
| 27 | + [InlineData("v1", typeof(PagingParameters))] |
| 28 | + [InlineData("v2", typeof(PagingParametersV2))] |
| 29 | + public void Query_V1_ContractModels(string apiVersion, Type typeNotInList) |
| 30 | + { |
| 31 | + // Arrange |
| 32 | + var apiProvider = _factory.Services.GetService<IApiDescriptionGroupCollectionProvider>(); |
| 33 | + var apiInfoForVersion = apiProvider.ApiDescriptionGroups.Items.FirstOrDefault(w => w.GroupName == apiVersion); |
| 34 | + var modelFinder = new ApiContractModelsFinder(); |
| 35 | + // Act |
| 36 | + var contractTypes = modelFinder.GetUsedContractTypes(apiInfoForVersion, "application/json"); |
| 37 | + |
| 38 | + // Assert |
| 39 | + contractTypes.GetAllTypes().Should().NotContain(typeNotInList); |
| 40 | + } |
| 41 | + |
| 42 | + [Fact] |
| 43 | + public void V1Models_Dont_Have_Attributes() |
| 44 | + { |
| 45 | + // Arrange |
| 46 | + var apiProvider = _factory.Services.GetService<IApiDescriptionGroupCollectionProvider>(); |
| 47 | + var apiInfoForVersion = apiProvider.ApiDescriptionGroups.Items.FirstOrDefault(w => w.GroupName == "v1"); |
| 48 | + var modelFinder = new ApiContractModelsAttributeChecker(); |
| 49 | + // Act |
| 50 | + var validationResults = modelFinder.CheckAttributesOfApiContractTypes(apiInfoForVersion, _attributes, "application/json"); |
| 51 | + |
| 52 | + // Assert |
| 53 | + var typesWithMissingAttributes = validationResults.Where(w => w.HasRequiredAttribute); |
| 54 | + typesWithMissingAttributes.Should().BeEmpty(); |
| 55 | + } |
| 56 | + |
| 57 | + [Fact] |
| 58 | + public void V2Models_Have_Attributes() |
| 59 | + { |
| 60 | + // Arrange |
| 61 | + var apiProvider = _factory.Services.GetService<IApiDescriptionGroupCollectionProvider>(); |
| 62 | + var apiInfoForVersion = apiProvider.ApiDescriptionGroups.Items.FirstOrDefault(w => w.GroupName == "v2"); |
| 63 | + var modelFinder = new ApiContractModelsAttributeChecker(); |
| 64 | + // Act |
| 65 | + var validationResults = modelFinder.CheckAttributesOfApiContractTypes(apiInfoForVersion, _attributes, "application/json"); |
| 66 | + |
| 67 | + // Assert |
| 68 | + var typesWithMissingAttributes = validationResults.Where(w => !w.HasRequiredAttribute); |
| 69 | + typesWithMissingAttributes.Should().BeEmpty(); |
| 70 | + } |
| 71 | + |
| 72 | + [Fact] |
| 73 | + public void Validate_All_Models() |
| 74 | + { |
| 75 | + // Arrange |
| 76 | + var apiProvider = _factory.Services.GetService<IApiDescriptionGroupCollectionProvider>(); |
| 77 | + var modelFinder = new ApiContractModelsAttributeChecker(); |
| 78 | + // Act |
| 79 | + var validationResults = modelFinder.CheckAttributesOfApiContractTypes(apiProvider, _attributes, "application/json"); |
| 80 | + |
| 81 | + // Assert |
| 82 | + var typesWithMissingAttributes = validationResults.Where(w => !w.HasRequiredAttribute); |
| 83 | + typesWithMissingAttributes.Should().NotBeEmpty(); |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments