|
3 | 3 | using Dataverse.ConfigurationMigrationTool.Console.Features.Import.ValueConverters;
|
4 | 4 | using Dataverse.ConfigurationMigrationTool.Console.Features.Shared;
|
5 | 5 | using Dataverse.ConfigurationMigrationTool.Console.Services.Dataverse;
|
| 6 | +using Dataverse.ConfigurationMigrationTool.Console.Tests.Extensions; |
6 | 7 | using Dataverse.ConfigurationMigrationTool.Console.Tests.FakeBuilders;
|
7 | 8 | using Microsoft.Extensions.Logging;
|
8 | 9 | using Microsoft.Xrm.Sdk;
|
@@ -175,4 +176,116 @@ public async Task GivenARelationshipImportTaskWhereRelationshipNotFound_WhenExec
|
175 | 176 | // Assert
|
176 | 177 | result.ShouldBe(TaskResult.Failed);
|
177 | 178 | }
|
| 179 | + [Fact] |
| 180 | + public async Task GivenASelfHiearchyEntityTaskImport_WhenExecuted_ThenItShouldProcessInCorrectOrderAndReturnCompleted() |
| 181 | + { |
| 182 | + // Arrange |
| 183 | + var task = new ImportDataTask |
| 184 | + { |
| 185 | + EntitySchema = FakeSchemas.SelfHiearchyAccount, |
| 186 | + }; |
| 187 | + var dataImport = new Entities |
| 188 | + { |
| 189 | + Entity = new List<EntityImport> |
| 190 | + { |
| 191 | + FakeDatasets.SelfHiearchyAccountSets |
| 192 | + } |
| 193 | + }; |
| 194 | + _dataverseValueConverter.Convert( |
| 195 | + Arg.Is<LookupAttributeMetadata>(md => md.LogicalName == "parentaccountid"), |
| 196 | + Arg.Is<Field>(f => f.Name == "parentaccountid")) |
| 197 | + .Returns(x => new EntityReference { LogicalName = "account", Id = Guid.Parse(x.Arg<Field>().Value) }); |
| 198 | + _dataverseValueConverter.Convert( |
| 199 | + Arg.Is<StringAttributeMetadata>(md => md.LogicalName == "name"), |
| 200 | + Arg.Is<Field>(f => f.Name == "name")).Returns(x => x.Arg<Field>().Value); |
| 201 | + metadataService.GetEntity(FakeSchemas.SelfHiearchyAccount.Name).Returns(FakeMetadata.Account); |
| 202 | + bulkOrganizationService.Upsert(Arg.Is<UpsertRequest>(r => r.Target.GetAttributeValue<EntityReference>("parentaccountid") != null)) |
| 203 | + .Returns(x => new(new UpsertResponse() { ["Target"] = x.Arg<UpsertRequest>().Target.ToEntityReference() })); |
| 204 | + // Act |
| 205 | + var result = await importService.Execute(task, dataImport); |
| 206 | + // Assert |
| 207 | + Received.InOrder(() => |
| 208 | + { |
| 209 | + bulkOrganizationService.UpsertBulk(Arg.Is<IEnumerable<UpsertRequest>>(r => r.Count() == 1)); |
| 210 | + bulkOrganizationService.Upsert(Arg.Is<UpsertRequest>(r => r.Target.Id == FakeDatasets.AccountIds[2])); |
| 211 | + bulkOrganizationService.Upsert(Arg.Is<UpsertRequest>(r => r.Target.Id == FakeDatasets.AccountIds[1])); |
| 212 | + bulkOrganizationService.Upsert(Arg.Is<UpsertRequest>(r => r.Target.Id == FakeDatasets.AccountIds[0])); |
| 213 | + }); |
| 214 | + result.ShouldBe(TaskResult.Completed); |
| 215 | + } |
| 216 | + [Fact] |
| 217 | + public async Task GivenASelfHiearchyEntityTaskImportWothIssues_WhenExecuted_ThenItShouldProcessInCorrectOrderAndReturnFailed() |
| 218 | + { |
| 219 | + // Arrange |
| 220 | + var task = new ImportDataTask |
| 221 | + { |
| 222 | + EntitySchema = FakeSchemas.SelfHiearchyAccount, |
| 223 | + }; |
| 224 | + var dataImport = new Entities |
| 225 | + { |
| 226 | + Entity = new List<EntityImport> |
| 227 | + { |
| 228 | + FakeDatasets.SelfHiearchyAccountSets |
| 229 | + } |
| 230 | + }; |
| 231 | + var fault = new OrganizationServiceFault { Message = "Fault message" }; |
| 232 | + _dataverseValueConverter.Convert( |
| 233 | + Arg.Is<LookupAttributeMetadata>(md => md.LogicalName == "parentaccountid"), |
| 234 | + Arg.Is<Field>(f => f.Name == "parentaccountid")) |
| 235 | + .Returns(x => new EntityReference { LogicalName = "account", Id = Guid.Parse(x.Arg<Field>().Value) }); |
| 236 | + _dataverseValueConverter.Convert( |
| 237 | + Arg.Is<StringAttributeMetadata>(md => md.LogicalName == "name"), |
| 238 | + Arg.Is<Field>(f => f.Name == "name")).Returns(x => x.Arg<Field>().Value); |
| 239 | + metadataService.GetEntity(FakeSchemas.SelfHiearchyAccount.Name).Returns(FakeMetadata.Account); |
| 240 | + bulkOrganizationService.Upsert(Arg.Is<UpsertRequest>(r => r.Target.GetAttributeValue<EntityReference>("parentaccountid") != null)) |
| 241 | + .Returns(x => new(new OrganizationResponseFaultedResult() { Fault = fault, OriginalRequest = x.Arg<UpsertRequest>() })); |
| 242 | + |
| 243 | + bulkOrganizationService.UpsertBulk(Arg.Is<IEnumerable<UpsertRequest>>(r => r.Count() == 1)) |
| 244 | + .Returns(x => [new() { Fault = fault, OriginalRequest = x.Arg<IEnumerable<UpsertRequest>>().First() }]); |
| 245 | + // Act |
| 246 | + var result = await importService.Execute(task, dataImport); |
| 247 | + // Assert |
| 248 | + Received.InOrder(() => |
| 249 | + { |
| 250 | + bulkOrganizationService.UpsertBulk(Arg.Is<IEnumerable<UpsertRequest>>(r => r.Count() == 1)); |
| 251 | + bulkOrganizationService.Upsert(Arg.Is<UpsertRequest>(r => r.Target.Id == FakeDatasets.AccountIds[2])); |
| 252 | + bulkOrganizationService.Upsert(Arg.Is<UpsertRequest>(r => r.Target.Id == FakeDatasets.AccountIds[1])); |
| 253 | + bulkOrganizationService.Upsert(Arg.Is<UpsertRequest>(r => r.Target.Id == FakeDatasets.AccountIds[0])); |
| 254 | + }); |
| 255 | + result.ShouldBe(TaskResult.Failed); |
| 256 | + } |
| 257 | + [Fact] |
| 258 | + public async Task GivenACircularSelfHiearchyEntityTaskImport_WhenExecuted_ThenItShouldSkipThoseAndReturnCompleted() |
| 259 | + { |
| 260 | + // Arrange |
| 261 | + var task = new ImportDataTask |
| 262 | + { |
| 263 | + EntitySchema = FakeSchemas.SelfHiearchyAccount, |
| 264 | + }; |
| 265 | + var dataImport = new Entities |
| 266 | + { |
| 267 | + Entity = new List<EntityImport> |
| 268 | + { |
| 269 | + FakeDatasets.CIrcularSelfHiearchyAccountSets |
| 270 | + } |
| 271 | + }; |
| 272 | + _dataverseValueConverter.Convert( |
| 273 | + Arg.Is<LookupAttributeMetadata>(md => md.LogicalName == "parentaccountid"), |
| 274 | + Arg.Is<Field>(f => f.Name == "parentaccountid")) |
| 275 | + .Returns(x => new EntityReference { LogicalName = "account", Id = Guid.Parse(x.Arg<Field>().Value) }); |
| 276 | + _dataverseValueConverter.Convert( |
| 277 | + Arg.Is<StringAttributeMetadata>(md => md.LogicalName == "name"), |
| 278 | + Arg.Is<Field>(f => f.Name == "name")).Returns(x => x.Arg<Field>().Value); |
| 279 | + metadataService.GetEntity(FakeSchemas.SelfHiearchyAccount.Name).Returns(FakeMetadata.Account); |
| 280 | + bulkOrganizationService.Upsert(Arg.Is<UpsertRequest>(r => r.Target.GetAttributeValue<EntityReference>("parentaccountid") != null)) |
| 281 | + .Returns(x => new(new UpsertResponse() { ["Target"] = x.Arg<UpsertRequest>().Target.ToEntityReference() })); |
| 282 | + // Act |
| 283 | + var result = await importService.Execute(task, dataImport); |
| 284 | + // Assert |
| 285 | + await bulkOrganizationService.Received().UpsertBulk(Arg.Is<IEnumerable<UpsertRequest>>(r => r.Count() == 0)); |
| 286 | + await bulkOrganizationService.DidNotReceive().Upsert(Arg.Any<UpsertRequest>()); |
| 287 | + logger.ShouldHaveLogged(LogLevel.Warning, $"account({FakeDatasets.AccountIds[0]}) was skipped because his parent was not proccessed.", count: 1); |
| 288 | + logger.ShouldHaveLogged(LogLevel.Warning, $"account({FakeDatasets.AccountIds[1]}) was skipped because his parent was not proccessed.", count: 1); |
| 289 | + result.ShouldBe(TaskResult.Completed); |
| 290 | + } |
178 | 291 | }
|
0 commit comments