|
| 1 | +using FluentAssertions; |
| 2 | +using JsonApiDotNetCore.Resources; |
| 3 | +using JsonApiDotNetCore.Resources.Annotations; |
| 4 | +using TestBuildingBlocks; |
| 5 | +using Xunit; |
| 6 | + |
| 7 | +namespace JsonApiDotNetCoreTests.UnitTests.ResourceGraph; |
| 8 | + |
| 9 | +public sealed class HasManyAttributeTests |
| 10 | +{ |
| 11 | + [Fact] |
| 12 | + public void Cannot_set_value_to_null() |
| 13 | + { |
| 14 | + // Arrange |
| 15 | + var attribute = new HasManyAttribute |
| 16 | + { |
| 17 | + Property = typeof(TestResource).GetProperty(nameof(TestResource.Children))! |
| 18 | + }; |
| 19 | + |
| 20 | + var resource = new TestResource(); |
| 21 | + |
| 22 | + // Act |
| 23 | + Action action = () => attribute.SetValue(resource, null); |
| 24 | + |
| 25 | + // Assert |
| 26 | + action.Should().ThrowExactly<ArgumentNullException>(); |
| 27 | + } |
| 28 | + |
| 29 | + [Fact] |
| 30 | + public void Cannot_set_value_to_primitive_type() |
| 31 | + { |
| 32 | + // Arrange |
| 33 | + var attribute = new HasManyAttribute |
| 34 | + { |
| 35 | + Property = typeof(TestResource).GetProperty(nameof(TestResource.Children))! |
| 36 | + }; |
| 37 | + |
| 38 | + var resource = new TestResource(); |
| 39 | + |
| 40 | + // Act |
| 41 | + Action action = () => attribute.SetValue(resource, 1); |
| 42 | + |
| 43 | + // Assert |
| 44 | + action.Should().ThrowExactly<InvalidOperationException>().WithMessage("Resource of type 'System.Int32' must be a collection."); |
| 45 | + } |
| 46 | + |
| 47 | + [Fact] |
| 48 | + public void Cannot_set_value_to_single_resource() |
| 49 | + { |
| 50 | + // Arrange |
| 51 | + var attribute = new HasManyAttribute |
| 52 | + { |
| 53 | + Property = typeof(TestResource).GetProperty(nameof(TestResource.Children))! |
| 54 | + }; |
| 55 | + |
| 56 | + var resource = new TestResource(); |
| 57 | + |
| 58 | + // Act |
| 59 | + Action action = () => attribute.SetValue(resource, resource); |
| 60 | + |
| 61 | + // Assert |
| 62 | + action.Should().ThrowExactly<InvalidOperationException>().WithMessage($"Resource of type '{typeof(TestResource).FullName}' must be a collection."); |
| 63 | + } |
| 64 | + |
| 65 | + [Fact] |
| 66 | + public void Can_set_value_to_collection_with_single_resource() |
| 67 | + { |
| 68 | + // Arrange |
| 69 | + var attribute = new HasManyAttribute |
| 70 | + { |
| 71 | + Property = typeof(TestResource).GetProperty(nameof(TestResource.Children))! |
| 72 | + }; |
| 73 | + |
| 74 | + var resource = new TestResource(); |
| 75 | + |
| 76 | + var children = new List<TestResource> |
| 77 | + { |
| 78 | + resource |
| 79 | + }; |
| 80 | + |
| 81 | + // Act |
| 82 | + attribute.SetValue(resource, children); |
| 83 | + |
| 84 | + // Assert |
| 85 | + attribute.GetValue(resource).Should().BeOfType<List<TestResource>>().Subject.ShouldHaveCount(1); |
| 86 | + } |
| 87 | + |
| 88 | + [Fact] |
| 89 | + public void Cannot_set_value_to_collection_with_null_element() |
| 90 | + { |
| 91 | + // Arrange |
| 92 | + var attribute = new HasManyAttribute |
| 93 | + { |
| 94 | + Property = typeof(TestResource).GetProperty(nameof(TestResource.Children))! |
| 95 | + }; |
| 96 | + |
| 97 | + var resource = new TestResource(); |
| 98 | + |
| 99 | + var children = new List<TestResource> |
| 100 | + { |
| 101 | + resource, |
| 102 | + null! |
| 103 | + }; |
| 104 | + |
| 105 | + // Act |
| 106 | + Action action = () => attribute.SetValue(resource, children); |
| 107 | + |
| 108 | + // Assert |
| 109 | + action.Should().ThrowExactly<InvalidOperationException>().WithMessage("Resource collection must not contain null values."); |
| 110 | + } |
| 111 | + |
| 112 | + [Fact] |
| 113 | + public void Cannot_set_value_to_collection_with_primitive_element() |
| 114 | + { |
| 115 | + // Arrange |
| 116 | + var attribute = new HasManyAttribute |
| 117 | + { |
| 118 | + Property = typeof(TestResource).GetProperty(nameof(TestResource.Children))! |
| 119 | + }; |
| 120 | + |
| 121 | + var resource = new TestResource(); |
| 122 | + |
| 123 | + var children = new List<object> |
| 124 | + { |
| 125 | + resource, |
| 126 | + 1 |
| 127 | + }; |
| 128 | + |
| 129 | + // Act |
| 130 | + Action action = () => attribute.SetValue(resource, children); |
| 131 | + |
| 132 | + // Assert |
| 133 | + action.Should().ThrowExactly<InvalidOperationException>().WithMessage("Resource of type 'System.Int32' does not implement IIdentifiable."); |
| 134 | + } |
| 135 | + |
| 136 | + private sealed class TestResource : Identifiable<long> |
| 137 | + { |
| 138 | + [HasMany] |
| 139 | + public IEnumerable<TestResource> Children { get; set; } = new HashSet<TestResource>(); |
| 140 | + } |
| 141 | +} |
0 commit comments