|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// Licensed to the Ed-Fi Alliance under one or more agreements. |
| 3 | +// The Ed-Fi Alliance licenses this file to you under the Apache License, Version 2.0. |
| 4 | +// See the LICENSE and NOTICES files in the project root for more information. |
| 5 | + |
| 6 | +using System.Text.Json.Nodes; |
| 7 | +using EdFi.DataManagementService.Core.ApiSchema; |
| 8 | +using FluentAssertions; |
| 9 | +using Microsoft.Extensions.Logging.Abstractions; |
| 10 | +using NUnit.Framework; |
| 11 | + |
| 12 | +namespace EdFi.DataManagementService.Core.Tests.Unit.ApiSchema; |
| 13 | + |
| 14 | +public class DependencyCalculatorTests |
| 15 | +{ |
| 16 | + private DependencyCalculator? _dependencyCalculator; |
| 17 | + |
| 18 | + [TestFixture] |
| 19 | + public class Given_A_Sample_ApiSchema() : DependencyCalculatorTests |
| 20 | + { |
| 21 | + private readonly string _sampleSchema = |
| 22 | + """ |
| 23 | + { |
| 24 | + "projectNameMapping": { |
| 25 | + "Ed-Fi": "ed-fi" |
| 26 | + }, |
| 27 | + "projectSchemas": { |
| 28 | + "ed-fi": { |
| 29 | + "resourceSchemas": { |
| 30 | + "absenceEventCategoryDescriptors": { |
| 31 | + "documentPathsMapping": { |
| 32 | + }, |
| 33 | + "resourceName": "AbsenceEventCategoryDescriptor" |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + """; |
| 40 | + |
| 41 | + private readonly string _expectedDescriptor = |
| 42 | + """ |
| 43 | + [ |
| 44 | + { |
| 45 | + "resource": "/ed-fi/absenceEventCategoryDescriptor", |
| 46 | + "order": 1, |
| 47 | + "operations": [ |
| 48 | + "Create", |
| 49 | + "Update" |
| 50 | + ] |
| 51 | + } |
| 52 | + ] |
| 53 | + """; |
| 54 | + |
| 55 | + [SetUp] |
| 56 | + public void Setup() |
| 57 | + { |
| 58 | + var logger = NullLogger<ApiSchemaSchemaProvider>.Instance; |
| 59 | + _dependencyCalculator = new DependencyCalculator(JsonNode.Parse(_sampleSchema)!, logger); |
| 60 | + } |
| 61 | + |
| 62 | + [Test] |
| 63 | + public void It_should_calculate_dependencies() |
| 64 | + { |
| 65 | + var dependencies = _dependencyCalculator!.GetDependencies(); |
| 66 | + dependencies.Should().NotBeEmpty(); |
| 67 | + dependencies.Count.Should().Be(1); |
| 68 | + |
| 69 | + var expectedDependencies = JsonNode.Parse(_expectedDescriptor)!.AsArray(); |
| 70 | + dependencies!.Should().BeEquivalentTo(expectedDependencies!, options => options |
| 71 | + .WithoutStrictOrdering() |
| 72 | + .IgnoringCyclicReferences()); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + [TestFixture] |
| 77 | + public class Given_A_Sample_ApiSchema_With_Subclass_Resources() : DependencyCalculatorTests |
| 78 | + { |
| 79 | + private readonly string _sampleSchema = |
| 80 | + """ |
| 81 | + { |
| 82 | + "projectNameMapping": { |
| 83 | + "Ed-Fi": "ed-fi" |
| 84 | + }, |
| 85 | + "projectSchemas": { |
| 86 | + "ed-fi": { |
| 87 | + "resourceSchemas": { |
| 88 | + "educationOrganizationCategoryDescriptors": { |
| 89 | + "documentPathsMapping": { |
| 90 | + }, |
| 91 | + "isDescriptor": true, |
| 92 | + "isSubclass": false, |
| 93 | + "resourceName": "EducationOrganizationCategoryDescriptor" |
| 94 | + }, |
| 95 | + "localEducationAgencies": { |
| 96 | + "documentPathsMapping": { |
| 97 | + "EducationOrganizationCategoryDescriptor": { |
| 98 | + "isDescriptor": true, |
| 99 | + "isReference": true, |
| 100 | + "projectName": "Ed-Fi", |
| 101 | + "resourceName": "EducationOrganizationCategoryDescriptor" |
| 102 | + }, |
| 103 | + "ParentLocalEducationAgency": { |
| 104 | + "isReference": true, |
| 105 | + "projectName": "Ed-Fi", |
| 106 | + "resourceName": "LocalEducationAgency" |
| 107 | + } |
| 108 | + }, |
| 109 | + "isSubclass": true, |
| 110 | + "resourceName": "LocalEducationAgency", |
| 111 | + "subclassType": "domainEntity", |
| 112 | + "superclassProjectName": "Ed-Fi", |
| 113 | + "superclassResourceName": "EducationOrganization" |
| 114 | + }, |
| 115 | + "schools": { |
| 116 | + "documentPathsMapping": { |
| 117 | + "EducationOrganizationCategoryDescriptor": { |
| 118 | + "isDescriptor": true, |
| 119 | + "isReference": true, |
| 120 | + "projectName": "Ed-Fi", |
| 121 | + "resourceName": "EducationOrganizationCategoryDescriptor" |
| 122 | + }, |
| 123 | + "LocalEducationAgency": { |
| 124 | + "isReference": true, |
| 125 | + "projectName": "Ed-Fi", |
| 126 | + "resourceName": "LocalEducationAgency" |
| 127 | + }, |
| 128 | + "WebSite": { |
| 129 | + "isReference": false, |
| 130 | + "path": "$.webSite" |
| 131 | + } |
| 132 | + }, |
| 133 | + "isSubclass": true, |
| 134 | + "resourceName": "School", |
| 135 | + "subclassType": "domainEntity", |
| 136 | + "superclassProjectName": "Ed-Fi", |
| 137 | + "superclassResourceName": "EducationOrganization" |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + """; |
| 144 | + |
| 145 | + private readonly string _expectedDescriptor = |
| 146 | + """ |
| 147 | + [ |
| 148 | + { |
| 149 | + "resource": "/ed-fi/educationOrganizationCategoryDescriptor", |
| 150 | + "order": 1, |
| 151 | + "operations": [ |
| 152 | + "Create", |
| 153 | + "Update" |
| 154 | + ] |
| 155 | + }, |
| 156 | + { |
| 157 | + "resource": "/ed-fi/localEducationAgency", |
| 158 | + "order": 2, |
| 159 | + "operations": [ |
| 160 | + "Create", |
| 161 | + "Update" |
| 162 | + ] |
| 163 | + }, |
| 164 | + { |
| 165 | + "resource": "/ed-fi/school", |
| 166 | + "order": 3, |
| 167 | + "operations": [ |
| 168 | + "Create", |
| 169 | + "Update" |
| 170 | + ] |
| 171 | + } |
| 172 | + ] |
| 173 | + """; |
| 174 | + |
| 175 | + [SetUp] |
| 176 | + public void Setup() |
| 177 | + { |
| 178 | + var logger = NullLogger<ApiSchemaSchemaProvider>.Instance; |
| 179 | + _dependencyCalculator = new DependencyCalculator(JsonNode.Parse(_sampleSchema)!, logger); |
| 180 | + } |
| 181 | + |
| 182 | + [Test] |
| 183 | + public void It_should_calculate_dependencies() |
| 184 | + { |
| 185 | + var dependencies = _dependencyCalculator!.GetDependencies(); |
| 186 | + dependencies.Should().NotBeEmpty(); |
| 187 | + |
| 188 | + var expectedDependencies = JsonNode.Parse(_expectedDescriptor)!.AsArray(); |
| 189 | + dependencies!.Should().BeEquivalentTo(expectedDependencies!, options => options |
| 190 | + .WithoutStrictOrdering() |
| 191 | + .IgnoringCyclicReferences()); |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + [TestFixture] |
| 196 | + public class Given_A_Sample_ApiSchema_Missing_ProjectSchemas() : DependencyCalculatorTests |
| 197 | + { |
| 198 | + private readonly string _sampleSchema = |
| 199 | + """ |
| 200 | + { |
| 201 | + "projectNameMapping": { |
| 202 | + "Ed-Fi": "ed-fi" |
| 203 | + } |
| 204 | + } |
| 205 | + """; |
| 206 | + |
| 207 | + [SetUp] |
| 208 | + public void Setup() |
| 209 | + { |
| 210 | + var logger = NullLogger<ApiSchemaSchemaProvider>.Instance; |
| 211 | + _dependencyCalculator = new DependencyCalculator(JsonNode.Parse(_sampleSchema)!, logger); |
| 212 | + } |
| 213 | + |
| 214 | + [Test] |
| 215 | + public void It_should_throw_invalid_operation() |
| 216 | + { |
| 217 | + Action act = () => _dependencyCalculator!.GetDependencies(); |
| 218 | + act.Should().Throw<InvalidOperationException>(); |
| 219 | + } |
| 220 | + } |
| 221 | +} |
| 222 | + |
0 commit comments