|
| 1 | +using JsonApiToolkit.Mapping; |
| 2 | +using JsonApiToolkit.Models.Resources; |
| 3 | + |
| 4 | +namespace JsonApiToolkit.Tests.Mapping; |
| 5 | + |
| 6 | +public class JsonColumnMappingTests |
| 7 | +{ |
| 8 | + public class EntityWithJsonColumns |
| 9 | + { |
| 10 | + public int Id { get; set; } |
| 11 | + public string Name { get; set; } = string.Empty; |
| 12 | + |
| 13 | + // These collections have no ID properties, simulating EF Core owned entities stored as JSON |
| 14 | + public List<JsonData> JsonDataList { get; set; } = new(); |
| 15 | + public ICollection<ExploitationReport> ExploitationReports { get; set; } = |
| 16 | + new List<ExploitationReport>(); |
| 17 | + |
| 18 | + // This has an ID property, so should be a relationship |
| 19 | + public List<RelatedEntity> RelatedEntities { get; set; } = new(); |
| 20 | + } |
| 21 | + |
| 22 | + public class JsonData |
| 23 | + { |
| 24 | + public string Type { get; set; } = string.Empty; |
| 25 | + public string Value { get; set; } = string.Empty; |
| 26 | + public DateTime Timestamp { get; set; } |
| 27 | + // No Id property - this is owned entity stored as JSON |
| 28 | + } |
| 29 | + |
| 30 | + public class ExploitationReport |
| 31 | + { |
| 32 | + public string Description { get; set; } = string.Empty; |
| 33 | + public string Severity { get; set; } = string.Empty; |
| 34 | + // No Id property - this is owned entity stored as JSON |
| 35 | + } |
| 36 | + |
| 37 | + public class RelatedEntity |
| 38 | + { |
| 39 | + public int Id { get; set; } |
| 40 | + public string Name { get; set; } = string.Empty; |
| 41 | + } |
| 42 | + |
| 43 | + [Fact] |
| 44 | + public void GetAttributeProperties_IncludesJsonColumns() |
| 45 | + { |
| 46 | + // Arrange |
| 47 | + Type entityType = typeof(EntityWithJsonColumns); |
| 48 | + |
| 49 | + // Act |
| 50 | + var attributeProperties = EntityMapper.GetAttributeProperties(entityType); |
| 51 | + var attributeNames = attributeProperties.Select(p => p.Name).ToList(); |
| 52 | + |
| 53 | + // Assert |
| 54 | + Assert.Contains("Name", attributeNames); |
| 55 | + Assert.Contains("JsonDataList", attributeNames); // Should be included as attribute (no IDs) |
| 56 | + Assert.Contains("ExploitationReports", attributeNames); // Should be included as attribute (no IDs) |
| 57 | + Assert.DoesNotContain("RelatedEntities", attributeNames); // Should NOT be attribute (has IDs) |
| 58 | + } |
| 59 | + |
| 60 | + [Fact] |
| 61 | + public void GetRelationshipProperties_ExcludesJsonColumns() |
| 62 | + { |
| 63 | + // Arrange |
| 64 | + Type entityType = typeof(EntityWithJsonColumns); |
| 65 | + |
| 66 | + // Act |
| 67 | + var relationshipProperties = EntityMapper.GetRelationshipProperties(entityType); |
| 68 | + var relationshipNames = relationshipProperties.Select(p => p.Name).ToList(); |
| 69 | + |
| 70 | + // Assert |
| 71 | + Assert.DoesNotContain("JsonDataList", relationshipNames); // Should NOT be relationship (no IDs) |
| 72 | + Assert.DoesNotContain("ExploitationReports", relationshipNames); // Should NOT be relationship (no IDs) |
| 73 | + Assert.Contains("RelatedEntities", relationshipNames); // Should be relationship (has IDs) |
| 74 | + } |
| 75 | + |
| 76 | + [Fact] |
| 77 | + public void ToResourceObject_MapsJsonColumnsAsAttributes() |
| 78 | + { |
| 79 | + // Arrange |
| 80 | + var entity = new EntityWithJsonColumns |
| 81 | + { |
| 82 | + Id = 1, |
| 83 | + Name = "Test Entity", |
| 84 | + JsonDataList = new List<JsonData> |
| 85 | + { |
| 86 | + new() |
| 87 | + { |
| 88 | + Type = "warning", |
| 89 | + Value = "test warning", |
| 90 | + Timestamp = DateTime.Now, |
| 91 | + }, |
| 92 | + new() |
| 93 | + { |
| 94 | + Type = "error", |
| 95 | + Value = "test error", |
| 96 | + Timestamp = DateTime.Now, |
| 97 | + }, |
| 98 | + }, |
| 99 | + ExploitationReports = new List<ExploitationReport> |
| 100 | + { |
| 101 | + new() { Description = "CVE-2024-1234", Severity = "High" }, |
| 102 | + }, |
| 103 | + RelatedEntities = new List<RelatedEntity> |
| 104 | + { |
| 105 | + new() { Id = 10, Name = "Related 1" }, |
| 106 | + }, |
| 107 | + }; |
| 108 | + |
| 109 | + // Act |
| 110 | + ResourceObject resource = JsonApiMapper.ToResourceObject(entity, "entityWithJsonColumns"); |
| 111 | + |
| 112 | + // Assert |
| 113 | + Assert.NotNull(resource.Attributes); |
| 114 | + Assert.Equal("Test Entity", resource.Attributes["name"]); |
| 115 | + |
| 116 | + // JSON columns should be in attributes |
| 117 | + Assert.True(resource.Attributes.ContainsKey("jsonDataList")); |
| 118 | + Assert.True(resource.Attributes.ContainsKey("exploitationReports")); |
| 119 | + |
| 120 | + // Verify the JSON data is preserved |
| 121 | + var jsonDataList = resource.Attributes["jsonDataList"] as List<JsonData>; |
| 122 | + Assert.NotNull(jsonDataList); |
| 123 | + Assert.Equal(2, jsonDataList.Count); |
| 124 | + |
| 125 | + var exploitationReports = |
| 126 | + resource.Attributes["exploitationReports"] as ICollection<ExploitationReport>; |
| 127 | + Assert.NotNull(exploitationReports); |
| 128 | + Assert.Single(exploitationReports); |
| 129 | + |
| 130 | + // RelatedEntities should NOT be in attributes (it's a relationship) |
| 131 | + Assert.False(resource.Attributes.ContainsKey("relatedEntities")); |
| 132 | + } |
| 133 | +} |
0 commit comments