|
| 1 | +using System; |
| 2 | +using System.Net.Http; |
| 3 | +using System.Text.Json; |
| 4 | +using System.Threading; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using FluentAssertions; |
| 7 | +using TestBuildingBlocks; |
| 8 | +using Xunit; |
| 9 | + |
| 10 | +namespace OpenApiTests.NamingConvention.KebabCase |
| 11 | +{ |
| 12 | + public sealed class KebabCaseTests |
| 13 | + : IClassFixture<IntegrationTestContext<KebabCaseNamingConventionStartup<NamingConventionDbContext>, NamingConventionDbContext>> |
| 14 | + { |
| 15 | + private static Lazy<Task<JsonDocument>> _lazyOpenApiDocument; |
| 16 | + private readonly IntegrationTestContext<KebabCaseNamingConventionStartup<NamingConventionDbContext>, NamingConventionDbContext> _testContext; |
| 17 | + |
| 18 | + public KebabCaseTests(IntegrationTestContext<KebabCaseNamingConventionStartup<NamingConventionDbContext>, NamingConventionDbContext> testContext) |
| 19 | + { |
| 20 | + _testContext = testContext; |
| 21 | + |
| 22 | + _lazyOpenApiDocument ??= new Lazy<Task<JsonDocument>>(async () => |
| 23 | + { |
| 24 | + testContext.UseController<SupermarketsController>(); |
| 25 | + const string requestUrl = "swagger/v1/swagger.json"; |
| 26 | + string content = await GetAsync(requestUrl); |
| 27 | + |
| 28 | + return JsonDocument.Parse(content); |
| 29 | + }, LazyThreadSafetyMode.ExecutionAndPublication); |
| 30 | + } |
| 31 | + |
| 32 | + private async Task<string> GetAsync(string requestUrl) |
| 33 | + { |
| 34 | + var request = new HttpRequestMessage(HttpMethod.Get, requestUrl); |
| 35 | + |
| 36 | + using HttpClient client = _testContext.Factory.CreateClient(); |
| 37 | + HttpResponseMessage responseMessage = await client.SendAsync(request); |
| 38 | + |
| 39 | + return await responseMessage.Content.ReadAsStringAsync(); |
| 40 | + } |
| 41 | + |
| 42 | + [Fact] |
| 43 | + public async Task Kebab_naming_policy_is_applied_to_get_collection_endpoint() |
| 44 | + { |
| 45 | + // Arrange |
| 46 | + const string expectedReferenceIdForResponseDocument = "supermarket-collection-response-document"; |
| 47 | + const string expectedReferenceIdForToOneResourceResponseData = "to-one-staff-member-response-data"; |
| 48 | + const string expectedReferenceIdForToManyResourceResponseData = "to-many-staff-member-response-data"; |
| 49 | + const string expectedReferenceIdForResourceDataInResponse = "supermarket-data-in-response"; |
| 50 | + const string expectedReferenceIdForResourceIdentifier = "staff-member-identifier"; |
| 51 | + const string expectedReferenceIdResourceAttributesInResponse = "supermarket-attributes-in-response"; |
| 52 | + const string expectedReferenceIdResourceRelationshipsInResponse = "supermarket-relationships-in-response"; |
| 53 | + const string expectedReferenceIdForLinksInRelationshipObject = "links-in-relationship-object"; |
| 54 | + const string expectedReferenceIdForLinksResourceObject = "links-in-resource-object"; |
| 55 | + const string expectedReferenceIdForTopLevelLinks = "links-in-resource-collection-document"; |
| 56 | + const string expectedReferenceIdForJsonapiObject = "jsonapi-object"; |
| 57 | + const string expectedReferenceIdForNullValue = "null-value"; |
| 58 | + const string expectedReferenceIdForEnum = "supermarket-type"; |
| 59 | + |
| 60 | + const string expectedOperationId = "get-supermarket-collection"; |
| 61 | + const string expectedPrimaryResourcePublicName = "supermarkets"; |
| 62 | + string expectedPrimaryResourceType = $"{expectedPrimaryResourcePublicName}-resource-type"; |
| 63 | + string expectedPath = $"/{expectedPrimaryResourcePublicName}"; |
| 64 | + const string expectedPropertyNameForStringAttribute = "name-of-city"; |
| 65 | + const string expectedPropertyNameForEnumAttribute = "kind"; |
| 66 | + const string expectedPropertyNameForToOneRelationship = "store-manager"; |
| 67 | + const string expectedPropertyNameForToManyRelationship = "cashiers"; |
| 68 | + const string expectedRelatedResourceType = "staff-members-resource-type"; |
| 69 | + const string expectedRelatedResourcePublicName = "staff-members"; |
| 70 | + |
| 71 | + // Act |
| 72 | + JsonDocument document = await _lazyOpenApiDocument.Value; |
| 73 | + |
| 74 | + // Assert |
| 75 | + document.SelectTokenOrError("paths").Should().HaveProperty(expectedPath); |
| 76 | + document.SelectTokenOrError($"paths.{expectedPath}.get.operationId").GetString().Should().Be(expectedOperationId); |
| 77 | + |
| 78 | + string responseRefId = document.SelectTokenOrError($"paths.{expectedPath}.get.responses.200.content['application/vnd.api+json'].schema.$ref") |
| 79 | + .GetReferenceSchemaId(); |
| 80 | + |
| 81 | + responseRefId.Should().Be(expectedReferenceIdForResponseDocument); |
| 82 | + |
| 83 | + string topLevelLinksRefId = document.SelectTokenOrError($"components.schemas.{responseRefId}.properties.links.$ref").GetReferenceSchemaId(); |
| 84 | + topLevelLinksRefId.Should().Be(expectedReferenceIdForTopLevelLinks); |
| 85 | + |
| 86 | + string jsonapiRefId = document.SelectTokenOrError($"components.schemas.{responseRefId}.properties.jsonapi.$ref").GetReferenceSchemaId(); |
| 87 | + jsonapiRefId.Should().Be(expectedReferenceIdForJsonapiObject); |
| 88 | + |
| 89 | + string dataRefId = document.SelectTokenOrError($"components.schemas.{responseRefId}.properties.data.items.$ref").GetReferenceSchemaId(); |
| 90 | + dataRefId.Should().Be(expectedReferenceIdForResourceDataInResponse); |
| 91 | + |
| 92 | + string resourceLinksRefId = document.SelectTokenOrError($"components.schemas.{dataRefId}.properties.links.$ref").GetReferenceSchemaId(); |
| 93 | + resourceLinksRefId.Should().Be(expectedReferenceIdForLinksResourceObject); |
| 94 | + |
| 95 | + string primaryResourceTypeRefId = document.SelectTokenOrError($"components.schemas.{dataRefId}.properties.type.$ref").GetReferenceSchemaId(); |
| 96 | + primaryResourceTypeRefId.Should().Be(expectedPrimaryResourceType); |
| 97 | + string primaryResourceTypeValue = document.SelectTokenOrError($"components.schemas.{primaryResourceTypeRefId}.enum[0]").GetString(); |
| 98 | + primaryResourceTypeValue.Should().Be(expectedPrimaryResourcePublicName); |
| 99 | + |
| 100 | + string attributesRefId = document.SelectTokenOrError($"components.schemas.{dataRefId}.properties.attributes.$ref").GetReferenceSchemaId(); |
| 101 | + attributesRefId.Should().Be(expectedReferenceIdResourceAttributesInResponse); |
| 102 | + JsonElement attributes = document.SelectTokenOrError($"components.schemas.{attributesRefId}.properties"); |
| 103 | + |
| 104 | + attributes.Should().HaveProperty(expectedPropertyNameForStringAttribute); |
| 105 | + attributes.Should().HaveProperty(expectedPropertyNameForEnumAttribute); |
| 106 | + string enumRefId = document.SelectTokenOrError($"components.schemas.{attributesRefId}.properties.kind.$ref").GetReferenceSchemaId(); |
| 107 | + enumRefId.Should().Be(expectedReferenceIdForEnum); |
| 108 | + |
| 109 | + string relationshipsRefId = document.SelectTokenOrError($"components.schemas.{dataRefId}.properties.relationships.$ref").GetReferenceSchemaId(); |
| 110 | + relationshipsRefId.Should().Be(expectedReferenceIdResourceRelationshipsInResponse); |
| 111 | + |
| 112 | + JsonElement relationships = document.SelectTokenOrError($"components.schemas.{relationshipsRefId}.properties"); |
| 113 | + relationships.Should().HaveProperty(expectedPropertyNameForToOneRelationship); |
| 114 | + |
| 115 | + string toOneResourceRefId = document.SelectTokenOrError($"components.schemas.{relationshipsRefId}.properties.store-manager.$ref") |
| 116 | + .GetReferenceSchemaId(); |
| 117 | + |
| 118 | + toOneResourceRefId.Should().Be(expectedReferenceIdForToOneResourceResponseData); |
| 119 | + relationships.Should().HaveProperty(expectedPropertyNameForToManyRelationship); |
| 120 | + string toManyResourceRefId = document.SelectTokenOrError($"components.schemas.{relationshipsRefId}.properties.cashiers.$ref").GetReferenceSchemaId(); |
| 121 | + toManyResourceRefId.Should().Be(expectedReferenceIdForToManyResourceResponseData); |
| 122 | + |
| 123 | + string relationshipLinksRefId = document.SelectTokenOrError($"components.schemas.{toOneResourceRefId}.properties.links.$ref").GetReferenceSchemaId(); |
| 124 | + relationshipLinksRefId.Should().Be(expectedReferenceIdForLinksInRelationshipObject); |
| 125 | + |
| 126 | + string resourceIdentifierRefId = document.SelectTokenOrError($"components.schemas.{toOneResourceRefId}.properties.data.oneOf[0].$ref") |
| 127 | + .GetReferenceSchemaId(); |
| 128 | + |
| 129 | + resourceIdentifierRefId.Should().Be(expectedReferenceIdForResourceIdentifier); |
| 130 | + string nullValueRefId = document.SelectTokenOrError($"components.schemas.{toOneResourceRefId}.properties.data.oneOf[1].$ref").GetReferenceSchemaId(); |
| 131 | + nullValueRefId.Should().Be(expectedReferenceIdForNullValue); |
| 132 | + |
| 133 | + string relatedResourceTypeReferenceSchema = |
| 134 | + document.SelectTokenOrError($"components.schemas.{resourceIdentifierRefId}.properties.type.$ref").GetReferenceSchemaId(); |
| 135 | + |
| 136 | + relatedResourceTypeReferenceSchema.Should().Be(expectedRelatedResourceType); |
| 137 | + |
| 138 | + string relatedResourceTypeValue = |
| 139 | + document.SelectTokenOrError($"components.schemas.{relatedResourceTypeReferenceSchema}.enum[0]").GetReferenceSchemaId(); |
| 140 | + |
| 141 | + relatedResourceTypeValue.Should().Be(expectedRelatedResourcePublicName); |
| 142 | + } |
| 143 | + |
| 144 | + [Fact] |
| 145 | + public async Task Kebab_naming_policy_is_applied_to_get_single_endpoint() |
| 146 | + { |
| 147 | + // Arrange |
| 148 | + const string expectedReferenceIdForResponseDocument = "supermarket-primary-response-document"; |
| 149 | + const string expectedReferenceIdForTopLevelLinks = "links-in-resource-document"; |
| 150 | + |
| 151 | + const string expectedOperationId = "get-supermarket"; |
| 152 | + const string expectedPrimaryResourcePublicName = "supermarkets"; |
| 153 | + string expectedPath = $"/{expectedPrimaryResourcePublicName}/{{id}}"; |
| 154 | + |
| 155 | + // Act |
| 156 | + JsonDocument document = await _lazyOpenApiDocument.Value; |
| 157 | + |
| 158 | + // Assert |
| 159 | + document.SelectTokenOrError("paths").Should().HaveProperty(expectedPath); |
| 160 | + document.SelectTokenOrError($"paths.{expectedPath}.get.tags[0]").GetString().Should().Be(expectedPrimaryResourcePublicName); |
| 161 | + document.SelectTokenOrError($"paths.{expectedPath}.get.operationId").GetString().Should().Be(expectedOperationId); |
| 162 | + |
| 163 | + string responseRefId = document.SelectTokenOrError($"paths.{expectedPath}.get.responses.200.content['application/vnd.api+json'].schema.$ref") |
| 164 | + .GetReferenceSchemaId(); |
| 165 | + |
| 166 | + responseRefId.Should().Be(expectedReferenceIdForResponseDocument); |
| 167 | + |
| 168 | + string topLevelLinksRefId = document.SelectTokenOrError($"components.schemas.{responseRefId}.properties.links.$ref").GetReferenceSchemaId(); |
| 169 | + topLevelLinksRefId.Should().Be(expectedReferenceIdForTopLevelLinks); |
| 170 | + } |
| 171 | + } |
| 172 | +} |
0 commit comments