|
| 1 | +using System.Net; |
| 2 | +using FluentAssertions; |
| 3 | +using JsonApiDotNetCore.Middleware; |
| 4 | +using Microsoft.Net.Http.Headers; |
| 5 | +using OpenApiClientTests.SchemaProperties.NullableReferenceTypesDisabled.GeneratedCode; |
| 6 | +using TestBuildingBlocks; |
| 7 | +using Xunit; |
| 8 | + |
| 9 | +namespace OpenApiClientTests.SchemaProperties.NullableReferenceTypesDisabled; |
| 10 | + |
| 11 | +/// <summary> |
| 12 | +/// Should consider if the shape of the two tests here is more favourable over the test with the same name in the RequestTests suite. The drawback of the |
| 13 | +/// form here is that the expected json string is less easy to read. However the win is that this form allows us to run the tests in a [Theory]. This is |
| 14 | +/// relevant because each of these properties represent unique test cases. In the other test form, it is not clear which properties are tested without. |
| 15 | +/// For instance: here in Can_exclude_optional_relationships it is immediately clear that the properties we omit are those in the inline data. |
| 16 | +/// </summary> |
| 17 | +public sealed class AlternativeFormRequestTests |
| 18 | +{ |
| 19 | + private const string HenHouseUrl = "http://localhost/henHouses"; |
| 20 | + |
| 21 | + private readonly Dictionary<string, string> _partials = new() |
| 22 | + { |
| 23 | + { |
| 24 | + nameof(HenHouseRelationshipsInPostRequest.OldestChicken), @"""oldestChicken"": { |
| 25 | + ""data"": { |
| 26 | + ""type"": ""chickens"", |
| 27 | + ""id"": ""1"" |
| 28 | + } |
| 29 | + }" |
| 30 | + }, |
| 31 | + { |
| 32 | + nameof(HenHouseRelationshipsInPostRequest.FirstChicken), @"""firstChicken"": { |
| 33 | + ""data"": { |
| 34 | + ""type"": ""chickens"", |
| 35 | + ""id"": ""1"" |
| 36 | + } |
| 37 | + }" |
| 38 | + }, |
| 39 | + { |
| 40 | + nameof(HenHouseRelationshipsInPostRequest.AllChickens), @"""allChickens"": { |
| 41 | + ""data"": [ |
| 42 | + { |
| 43 | + ""type"": ""chickens"", |
| 44 | + ""id"": ""1"" |
| 45 | + } |
| 46 | + ] |
| 47 | + }" |
| 48 | + }, |
| 49 | + |
| 50 | + { |
| 51 | + nameof(HenHouseRelationshipsInPostRequest.ChickensReadyForLaying), @"""chickensReadyForLaying"": { |
| 52 | + ""data"": [ |
| 53 | + { |
| 54 | + ""type"": ""chickens"", |
| 55 | + ""id"": ""1"" |
| 56 | + } |
| 57 | + ] |
| 58 | + }" |
| 59 | + } |
| 60 | + }; |
| 61 | + |
| 62 | + [Theory] |
| 63 | + [InlineData(nameof(HenHouseRelationshipsInPostRequest.OldestChicken))] |
| 64 | + [InlineData(nameof(HenHouseRelationshipsInPostRequest.AllChickens))] |
| 65 | + public async Task Can_exclude_optional_relationships(string propertyName) |
| 66 | + { |
| 67 | + // Arrange |
| 68 | + using var wrapper = FakeHttpClientWrapper.Create(HttpStatusCode.NoContent, null); |
| 69 | + var apiClient = new NullableReferenceTypesDisabledClient(wrapper.HttpClient); |
| 70 | + |
| 71 | + HenHouseRelationshipsInPostRequest relationshipsObject = new() |
| 72 | + { |
| 73 | + OldestChicken = new NullableToOneChickenInRequest |
| 74 | + { |
| 75 | + Data = new ChickenIdentifier |
| 76 | + { |
| 77 | + Id = "1", |
| 78 | + Type = ChickenResourceType.Chickens |
| 79 | + } |
| 80 | + }, |
| 81 | + FirstChicken = new ToOneChickenInRequest |
| 82 | + { |
| 83 | + Data = new ChickenIdentifier |
| 84 | + { |
| 85 | + Id = "1", |
| 86 | + Type = ChickenResourceType.Chickens |
| 87 | + } |
| 88 | + }, |
| 89 | + AllChickens = new ToManyChickenInRequest |
| 90 | + { |
| 91 | + Data = new List<ChickenIdentifier> |
| 92 | + { |
| 93 | + new() |
| 94 | + { |
| 95 | + Id = "1", |
| 96 | + Type = ChickenResourceType.Chickens |
| 97 | + } |
| 98 | + } |
| 99 | + }, |
| 100 | + ChickensReadyForLaying = new ToManyChickenInRequest |
| 101 | + { |
| 102 | + Data = new List<ChickenIdentifier> |
| 103 | + { |
| 104 | + new() |
| 105 | + { |
| 106 | + Id = "1", |
| 107 | + Type = ChickenResourceType.Chickens |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + }; |
| 112 | + |
| 113 | + relationshipsObject.SetPropertyToDefaultValue(propertyName); |
| 114 | + |
| 115 | + var requestDocument = new HenHousePostRequestDocument |
| 116 | + { |
| 117 | + Data = new HenHouseDataInPostRequest |
| 118 | + { |
| 119 | + Relationships = relationshipsObject |
| 120 | + } |
| 121 | + }; |
| 122 | + |
| 123 | + await ApiResponse.TranslateAsync(async () => await apiClient.PostHenHouseAsync(requestDocument)); |
| 124 | + |
| 125 | + // Assert |
| 126 | + wrapper.Request.ShouldNotBeNull(); |
| 127 | + wrapper.Request.Headers.GetValue(HeaderNames.Accept).Should().Be(HeaderConstants.MediaType); |
| 128 | + wrapper.Request.Method.Should().Be(HttpMethod.Post); |
| 129 | + wrapper.Request.RequestUri.Should().Be(HenHouseUrl); |
| 130 | + wrapper.Request.Content.Should().NotBeNull(); |
| 131 | + wrapper.Request.Content!.Headers.ContentType.Should().NotBeNull(); |
| 132 | + wrapper.Request.Content!.Headers.ContentType!.ToString().Should().Be(HeaderConstants.MediaType); |
| 133 | + |
| 134 | + string body = GetRelationshipsObjectWithSinglePropertyOmitted(propertyName); |
| 135 | + |
| 136 | + wrapper.RequestBody.Should().BeJson(@"{ |
| 137 | + ""data"": { |
| 138 | + ""type"": ""henHouses"", |
| 139 | + ""relationships"": " + body + @" |
| 140 | + } |
| 141 | +}"); |
| 142 | + } |
| 143 | + |
| 144 | + [Theory] |
| 145 | + [InlineData(nameof(HenHouseRelationshipsInPostRequest.FirstChicken))] |
| 146 | + [InlineData(nameof(HenHouseRelationshipsInPostRequest.ChickensReadyForLaying))] |
| 147 | + public async Task Can_exclude_relationships_that_are_required_for_POST_when_performing_PATCH(string propertyName) |
| 148 | + { |
| 149 | + // Arrange |
| 150 | + using var wrapper = FakeHttpClientWrapper.Create(HttpStatusCode.NoContent, null); |
| 151 | + var apiClient = new NullableReferenceTypesDisabledClient(wrapper.HttpClient); |
| 152 | + |
| 153 | + var relationshipsObject = new HenHouseRelationshipsInPatchRequest |
| 154 | + { |
| 155 | + OldestChicken = new NullableToOneChickenInRequest |
| 156 | + { |
| 157 | + Data = new ChickenIdentifier |
| 158 | + { |
| 159 | + Id = "1", |
| 160 | + Type = ChickenResourceType.Chickens |
| 161 | + } |
| 162 | + }, |
| 163 | + FirstChicken = new ToOneChickenInRequest |
| 164 | + { |
| 165 | + Data = new ChickenIdentifier |
| 166 | + { |
| 167 | + Id = "1", |
| 168 | + Type = ChickenResourceType.Chickens |
| 169 | + } |
| 170 | + }, |
| 171 | + AllChickens = new ToManyChickenInRequest |
| 172 | + { |
| 173 | + Data = new List<ChickenIdentifier> |
| 174 | + { |
| 175 | + new() |
| 176 | + { |
| 177 | + Id = "1", |
| 178 | + Type = ChickenResourceType.Chickens |
| 179 | + } |
| 180 | + } |
| 181 | + }, |
| 182 | + ChickensReadyForLaying = new ToManyChickenInRequest |
| 183 | + { |
| 184 | + Data = new List<ChickenIdentifier> |
| 185 | + { |
| 186 | + new() |
| 187 | + { |
| 188 | + Id = "1", |
| 189 | + Type = ChickenResourceType.Chickens |
| 190 | + } |
| 191 | + } |
| 192 | + } |
| 193 | + }; |
| 194 | + |
| 195 | + relationshipsObject.SetPropertyToDefaultValue(propertyName); |
| 196 | + |
| 197 | + var requestDocument = new HenHousePatchRequestDocument |
| 198 | + { |
| 199 | + Data = new HenHouseDataInPatchRequest |
| 200 | + { |
| 201 | + Id = "1", |
| 202 | + Type = HenHouseResourceType.HenHouses, |
| 203 | + Relationships = relationshipsObject |
| 204 | + } |
| 205 | + }; |
| 206 | + |
| 207 | + await ApiResponse.TranslateAsync(async () => await apiClient.PatchHenHouseAsync(1, requestDocument)); |
| 208 | + |
| 209 | + // Assert |
| 210 | + wrapper.Request.ShouldNotBeNull(); |
| 211 | + wrapper.Request.Headers.GetValue(HeaderNames.Accept).Should().Be(HeaderConstants.MediaType); |
| 212 | + wrapper.Request.Method.Should().Be(HttpMethod.Patch); |
| 213 | + wrapper.Request.RequestUri.Should().Be(HenHouseUrl + "/1"); |
| 214 | + wrapper.Request.Content.Should().NotBeNull(); |
| 215 | + wrapper.Request.Content!.Headers.ContentType.Should().NotBeNull(); |
| 216 | + wrapper.Request.Content!.Headers.ContentType!.ToString().Should().Be(HeaderConstants.MediaType); |
| 217 | + |
| 218 | + string serializedRelationshipsObject = GetRelationshipsObjectWithSinglePropertyOmitted(propertyName); |
| 219 | + |
| 220 | + wrapper.RequestBody.Should().BeJson(@"{ |
| 221 | + ""data"": { |
| 222 | + ""type"": ""henHouses"", |
| 223 | + ""id"": ""1"", |
| 224 | + ""relationships"": " + serializedRelationshipsObject + @" |
| 225 | + } |
| 226 | +}"); |
| 227 | + } |
| 228 | + |
| 229 | + private string GetRelationshipsObjectWithSinglePropertyOmitted(string excludeProperty) |
| 230 | + { |
| 231 | + string partial = ""; |
| 232 | + |
| 233 | + foreach ((string key, string relationshipJsonPartial) in _partials) |
| 234 | + { |
| 235 | + if (excludeProperty == key) |
| 236 | + { |
| 237 | + continue; |
| 238 | + } |
| 239 | + |
| 240 | + if (partial.Length > 0) |
| 241 | + { |
| 242 | + partial += ",\n "; |
| 243 | + } |
| 244 | + |
| 245 | + partial += relationshipJsonPartial; |
| 246 | + } |
| 247 | + |
| 248 | + return @"{ |
| 249 | + " + partial + @" |
| 250 | + }"; |
| 251 | + } |
| 252 | +} |
0 commit comments