|
| 1 | +using System.Text.Json; |
| 2 | +using System.Text.Json.Nodes; |
| 3 | +using Confix.Entities.Schema; |
| 4 | +using Confix.Utilities.Json; |
| 5 | + |
| 6 | +namespace Confix.Tool.Tests.Entities.Schema; |
| 7 | + |
| 8 | +public class MetadataKeywordTests |
| 9 | +{ |
| 10 | + [Fact] |
| 11 | + public void Constructor_WithValidArray_SetsValue() |
| 12 | + { |
| 13 | + // arrange |
| 14 | + var array = new JsonArray("item1", "item2"); |
| 15 | + |
| 16 | + // act |
| 17 | + var keyword = new MetadataKeyword(array); |
| 18 | + |
| 19 | + // assert |
| 20 | + Assert.NotNull(keyword.Value); |
| 21 | + Assert.Equal(2, keyword.Value.Count); |
| 22 | + } |
| 23 | + |
| 24 | + [Fact] |
| 25 | + public void Constructor_WithEmptyArray_SetsEmptyValue() |
| 26 | + { |
| 27 | + // arrange |
| 28 | + var array = new JsonArray(); |
| 29 | + |
| 30 | + // act |
| 31 | + var keyword = new MetadataKeyword(array); |
| 32 | + |
| 33 | + // assert |
| 34 | + Assert.NotNull(keyword.Value); |
| 35 | + Assert.Empty(keyword.Value); |
| 36 | + } |
| 37 | + |
| 38 | + [Fact] |
| 39 | + public void Equals_SameValues_ReturnsTrue() |
| 40 | + { |
| 41 | + // arrange |
| 42 | + var keyword1 = new MetadataKeyword(new JsonArray("item1", "item2")); |
| 43 | + var keyword2 = new MetadataKeyword(new JsonArray("item1", "item2")); |
| 44 | + |
| 45 | + // act & assert |
| 46 | + Assert.True(keyword1.Equals(keyword2)); |
| 47 | + } |
| 48 | + |
| 49 | + [Fact] |
| 50 | + public void Equals_DifferentValues_ReturnsFalse() |
| 51 | + { |
| 52 | + // arrange |
| 53 | + var keyword1 = new MetadataKeyword(new JsonArray("item1")); |
| 54 | + var keyword2 = new MetadataKeyword(new JsonArray("item2")); |
| 55 | + |
| 56 | + // act & assert |
| 57 | + Assert.False(keyword1.Equals(keyword2)); |
| 58 | + } |
| 59 | + |
| 60 | + [Fact] |
| 61 | + public void Equals_Null_ReturnsFalse() |
| 62 | + { |
| 63 | + // arrange |
| 64 | + var keyword = new MetadataKeyword(new JsonArray("item1")); |
| 65 | + |
| 66 | + // act & assert |
| 67 | + Assert.False(keyword.Equals(null)); |
| 68 | + } |
| 69 | + |
| 70 | + [Fact] |
| 71 | + public void Equals_SameReference_ReturnsTrue() |
| 72 | + { |
| 73 | + // arrange |
| 74 | + var keyword = new MetadataKeyword(new JsonArray("item1")); |
| 75 | + |
| 76 | + // act & assert |
| 77 | + Assert.True(keyword.Equals(keyword)); |
| 78 | + } |
| 79 | + |
| 80 | + [Fact] |
| 81 | + public void GetHashCode_SameValues_ReturnsSameHash() |
| 82 | + { |
| 83 | + // arrange |
| 84 | + var keyword1 = new MetadataKeyword(new JsonArray("item1", "item2")); |
| 85 | + var keyword2 = new MetadataKeyword(new JsonArray("item1", "item2")); |
| 86 | + |
| 87 | + // act & assert |
| 88 | + Assert.Equal(keyword1.GetHashCode(), keyword2.GetHashCode()); |
| 89 | + } |
| 90 | + |
| 91 | + [Fact] |
| 92 | + public void JsonConverter_DeserializeValidArray_ReturnsKeyword() |
| 93 | + { |
| 94 | + // arrange |
| 95 | + var json = """["item1", "item2", "item3"]"""; |
| 96 | + |
| 97 | + // act |
| 98 | + var keyword = JsonSerializer.Deserialize<MetadataKeyword>(json); |
| 99 | + |
| 100 | + // assert |
| 101 | + Assert.NotNull(keyword); |
| 102 | + Assert.Equal(3, keyword.Value.Count); |
| 103 | + } |
| 104 | + |
| 105 | + [Fact] |
| 106 | + public void JsonConverter_DeserializeEmptyArray_ReturnsKeywordWithEmptyArray() |
| 107 | + { |
| 108 | + // arrange |
| 109 | + var json = """[]"""; |
| 110 | + |
| 111 | + // act |
| 112 | + var keyword = JsonSerializer.Deserialize<MetadataKeyword>(json); |
| 113 | + |
| 114 | + // assert |
| 115 | + Assert.NotNull(keyword); |
| 116 | + Assert.Empty(keyword.Value); |
| 117 | + } |
| 118 | + |
| 119 | + [Fact] |
| 120 | + public void JsonConverter_DeserializeNull_ReturnsNull() |
| 121 | + { |
| 122 | + // arrange - when JSON is literal "null", the serializer returns null |
| 123 | + // without calling the converter (standard System.Text.Json behavior) |
| 124 | + var json = """null"""; |
| 125 | + |
| 126 | + // act |
| 127 | + var keyword = JsonSerializer.Deserialize<MetadataKeyword>(json); |
| 128 | + |
| 129 | + // assert |
| 130 | + Assert.Null(keyword); |
| 131 | + } |
| 132 | + |
| 133 | + [Fact] |
| 134 | + public void JsonConverter_DeserializeComplexArray_ReturnsKeyword() |
| 135 | + { |
| 136 | + // arrange |
| 137 | + var json = """[{"key": "value"}, null, "string", 42]"""; |
| 138 | + |
| 139 | + // act |
| 140 | + var keyword = JsonSerializer.Deserialize<MetadataKeyword>(json); |
| 141 | + |
| 142 | + // assert |
| 143 | + Assert.NotNull(keyword); |
| 144 | + Assert.Equal(4, keyword.Value.Count); |
| 145 | + } |
| 146 | + |
| 147 | + [Fact] |
| 148 | + public void JsonConverter_SerializeKeyword_ReturnsValidJson() |
| 149 | + { |
| 150 | + // arrange |
| 151 | + var keyword = new MetadataKeyword(new JsonArray("item1", "item2")); |
| 152 | + var options = new JsonSerializerOptions { WriteIndented = false }; |
| 153 | + |
| 154 | + // act |
| 155 | + var json = JsonSerializer.Serialize(keyword, options); |
| 156 | + |
| 157 | + // assert |
| 158 | + // Note: The custom converter writes property name, so we wrap it |
| 159 | + Assert.Contains("metadata", json); |
| 160 | + } |
| 161 | + |
| 162 | + [Fact] |
| 163 | + public void JsonConverter_RoundTrip_PreservesValue() |
| 164 | + { |
| 165 | + // arrange |
| 166 | + var originalArray = new JsonArray("item1", 42, true); |
| 167 | + var keyword = new MetadataKeyword(originalArray); |
| 168 | + |
| 169 | + // act - serialize then deserialize the array directly |
| 170 | + var json = keyword.Value.ToJsonString(); |
| 171 | + var deserializedKeyword = JsonSerializer.Deserialize<MetadataKeyword>(json); |
| 172 | + |
| 173 | + // assert |
| 174 | + Assert.NotNull(deserializedKeyword); |
| 175 | + Assert.True(keyword.Value.IsEquivalentTo(deserializedKeyword.Value)); |
| 176 | + } |
| 177 | +} |
0 commit comments