|
| 1 | +using System.Net; |
| 2 | +using FluentAssertions; |
| 3 | +using FluentAssertions.Specialized; |
| 4 | +using JsonApiDotNetCore.Middleware; |
| 5 | +using Microsoft.Net.Http.Headers; |
| 6 | +using Newtonsoft.Json; |
| 7 | +using OpenApiClientTests.SchemaProperties.NullableReferenceTypesDisabled.GeneratedCode; |
| 8 | +using TestBuildingBlocks; |
| 9 | +using Xunit; |
| 10 | +using NullableReferenceTypesDisabledClient = OpenApiClientTests.SchemaProperties.NullableReferenceTypesDisabled.GeneratedCode.NullableReferenceTypesDisabledClient; |
| 11 | + |
| 12 | +namespace OpenApiClientTests.SchemaProperties.NullableReferenceTypesDisabled; |
| 13 | + |
| 14 | +public sealed class RequiredAttributesTests |
| 15 | +{ |
| 16 | + private const string HostPrefix = "http://localhost/"; |
| 17 | + |
| 18 | + [Fact] |
| 19 | + public async Task Partial_posting_resource_with_explicitly_omitting_required_fields_produces_expected_request() |
| 20 | + { |
| 21 | + // Arrange |
| 22 | + using var wrapper = FakeHttpClientWrapper.Create(HttpStatusCode.NoContent, null); |
| 23 | + var apiClient = new NullableReferenceTypesDisabledClient(wrapper.HttpClient); |
| 24 | + |
| 25 | + var requestDocument = new ChickenPostRequestDocument |
| 26 | + { |
| 27 | + Data = new ChickenDataInPostRequest |
| 28 | + { |
| 29 | + Attributes = new ChickenAttributesInPostRequest |
| 30 | + { |
| 31 | + HasProducedEggs = true |
| 32 | + } |
| 33 | + } |
| 34 | + }; |
| 35 | + |
| 36 | + using (apiClient.RegisterAttributesForRequestDocument<ChickenPostRequestDocument, ChickenAttributesInPostRequest>(requestDocument, |
| 37 | + chicken => chicken.HasProducedEggs)) |
| 38 | + { |
| 39 | + // Act |
| 40 | + await ApiResponse.TranslateAsync(async () => await apiClient.PostChickenAsync(requestDocument)); |
| 41 | + } |
| 42 | + |
| 43 | + // Assert |
| 44 | + wrapper.Request.ShouldNotBeNull(); |
| 45 | + wrapper.Request.Headers.GetValue(HeaderNames.Accept).Should().Be(HeaderConstants.MediaType); |
| 46 | + wrapper.Request.Method.Should().Be(HttpMethod.Post); |
| 47 | + wrapper.Request.RequestUri.Should().Be(HostPrefix + "chickens"); |
| 48 | + wrapper.Request.Content.Should().NotBeNull(); |
| 49 | + wrapper.Request.Content!.Headers.ContentType.Should().NotBeNull(); |
| 50 | + wrapper.Request.Content!.Headers.ContentType!.ToString().Should().Be(HeaderConstants.MediaType); |
| 51 | + |
| 52 | + wrapper.RequestBody.Should().BeJson(@"{ |
| 53 | + ""data"": { |
| 54 | + ""type"": ""chickens"", |
| 55 | + ""attributes"": { |
| 56 | + ""hasProducedEggs"": true |
| 57 | + } |
| 58 | + } |
| 59 | +}"); |
| 60 | + } |
| 61 | + |
| 62 | + [Fact] |
| 63 | + public async Task Partial_posting_resource_without_explicitly_omitting_required_fields_fails() |
| 64 | + { |
| 65 | + // Arrange |
| 66 | + using var wrapper = FakeHttpClientWrapper.Create(HttpStatusCode.NoContent, null); |
| 67 | + var apiClient = new NullableReferenceTypesDisabledClient(wrapper.HttpClient); |
| 68 | + |
| 69 | + var requestDocument = new ChickenPostRequestDocument |
| 70 | + { |
| 71 | + Data = new ChickenDataInPostRequest |
| 72 | + { |
| 73 | + Attributes = new ChickenAttributesInPostRequest |
| 74 | + { |
| 75 | + Weight = 3 |
| 76 | + } |
| 77 | + } |
| 78 | + }; |
| 79 | + |
| 80 | + // Act |
| 81 | + Func<Task<ChickenPrimaryResponseDocument?>> action = async () => |
| 82 | + await ApiResponse.TranslateAsync(async () => await apiClient.PostChickenAsync(requestDocument)); |
| 83 | + |
| 84 | + // Assert |
| 85 | + ExceptionAssertions<JsonSerializationException> assertion = await action.Should().ThrowExactlyAsync<JsonSerializationException>(); |
| 86 | + JsonSerializationException exception = assertion.Subject.Single(); |
| 87 | + |
| 88 | + exception.Message.Should().Be("Cannot write a null value for property 'nameOfCurrentFarm'. Property requires a value. Path 'data.attributes'."); |
| 89 | + } |
| 90 | + |
| 91 | + [Fact] |
| 92 | + public async Task Patching_resource_with_missing_id_fails() |
| 93 | + { |
| 94 | + // Arrange |
| 95 | + using var wrapper = FakeHttpClientWrapper.Create(HttpStatusCode.NoContent, null); |
| 96 | + var apiClient = new NullableReferenceTypesDisabledClient(wrapper.HttpClient); |
| 97 | + |
| 98 | + var requestDocument = new ChickenPatchRequestDocument |
| 99 | + { |
| 100 | + Data = new ChickenDataInPatchRequest |
| 101 | + { |
| 102 | + Attributes = new ChickenAttributesInPatchRequest |
| 103 | + { |
| 104 | + Age = 1 |
| 105 | + } |
| 106 | + } |
| 107 | + }; |
| 108 | + |
| 109 | + Func<Task> action = async () => await ApiResponse.TranslateAsync(async () => await apiClient.PatchChickenAsync(1, requestDocument)); |
| 110 | + |
| 111 | + // Assert |
| 112 | + await action.Should().ThrowAsync<JsonSerializationException>(); |
| 113 | + } |
| 114 | +} |
0 commit comments