diff --git a/src/database/SsiAuthoritySchemaRegistry.Migrations/Seeder/BatchDeleteSeeder.cs b/src/database/SsiAuthoritySchemaRegistry.Migrations/Seeder/BatchDeleteSeeder.cs new file mode 100644 index 0000000..de0b83c --- /dev/null +++ b/src/database/SsiAuthoritySchemaRegistry.Migrations/Seeder/BatchDeleteSeeder.cs @@ -0,0 +1,72 @@ +/******************************************************************************** + * Copyright (c) 2024 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ + +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using Org.Eclipse.TractusX.Portal.Backend.Framework.Seeding; +using Org.Eclipse.TractusX.SsiAuthoritySchemaRegistry.Entities; +using Org.Eclipse.TractusX.SsiAuthoritySchemaRegistry.Entities.Entities; + +namespace Org.Eclipse.TractusX.SsiAuthoritySchemaRegistry.Migrations.Seeder; + +/// +/// Seeder to seed the all configured entities +/// +public class BatchDeleteSeeder(RegistryContext context, ILogger logger, IOptions options) + : ICustomSeeder +{ + private readonly SeederSettings _settings = options.Value; + + /// + public int Order => 2; + + /// + public async Task ExecuteAsync(CancellationToken cancellationToken) + { + if (!_settings.DataPaths.Any()) + { + logger.LogInformation("There a no data paths configured, therefore the {SeederName} will be skipped", nameof(BatchInsertSeeder)); + return; + } + + await SeedTable("authorities", x => x.Bpn, cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None); + await SeedTable("credentials", x => x.Id, cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None); + await SeedTable("credential_authorities", x => new { x.CredentialId, x.Bpn }, cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None); + + await context.SaveChangesAsync(cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None); + } + + private async Task SeedTable(string fileName, Func keySelector, CancellationToken cancellationToken) + where T : class + { + logger.LogDebug("Start seeding {Filename}", fileName); + var additionalEnvironments = _settings.TestDataEnvironments ?? Enumerable.Empty(); + var data = await SeederHelper + .GetSeedData(logger, fileName, _settings.DataPaths, cancellationToken, additionalEnvironments.ToArray()) + .ConfigureAwait(false); + logger.LogDebug("Found {ElementCount} data", data.Count); + + // Identify entities in the database that are not present in the JSON data + var existingEntities = await context.Set().ToListAsync(cancellationToken).ConfigureAwait(false); + var entitiesToRemove = existingEntities + .Where(dbEntity => data.All(jsonEntity => !keySelector(dbEntity).Equals(keySelector(jsonEntity)))).ToList(); + context.Set().RemoveRange(entitiesToRemove); + } +} diff --git a/src/database/SsiAuthoritySchemaRegistry.Migrations/Seeder/Data/credential_authorities.json b/src/database/SsiAuthoritySchemaRegistry.Migrations/Seeder/Data/credential_authorities.json index e342e82..c1f7990 100644 --- a/src/database/SsiAuthoritySchemaRegistry.Migrations/Seeder/Data/credential_authorities.json +++ b/src/database/SsiAuthoritySchemaRegistry.Migrations/Seeder/Data/credential_authorities.json @@ -7,38 +7,6 @@ "credential_id": "255e01fc-65f6-43cd-8dfa-95e95fa95f61", "bpn": "BPNL00000003CRHK" }, - { - "credential_id": "255e01fc-65f6-43cd-8dfa-95e95fa95f62", - "bpn": "BPNL00000003CRHK" - }, - { - "credential_id": "255e01fc-65f6-43cd-8dfa-95e95fa95f63", - "bpn": "BPNL00000003CRHK" - }, - { - "credential_id": "255e01fc-65f6-43cd-8dfa-95e95fa95f65", - "bpn": "BPNL00000003CRHK" - }, - { - "credential_id": "255e01fc-65f6-43cd-8dfa-95e95fa95f66", - "bpn": "BPNL00000003CRHK" - }, - { - "credential_id": "255e01fc-65f6-43cd-8dfa-95e95fa95f67", - "bpn": "BPNL00000003CRHK" - }, - { - "credential_id": "255e01fc-65f6-43cd-8dfa-95e95fa95f68", - "bpn": "BPNL00000003CRHK" - }, - { - "credential_id": "255e01fc-65f6-43cd-8dfa-95e95fa95f69", - "bpn": "BPNL00000003CRHK" - }, - { - "credential_id": "255e01fc-65f6-43cd-8dfa-95e95fa95f6a", - "bpn": "BPNL00000003CRHK" - }, { "credential_id": "255e01fc-65f6-43cd-8dfa-95e95fa95f6b", "bpn": "BPNL00000003CRHK" @@ -46,5 +14,9 @@ { "credential_id": "255e01fc-65f6-43cd-8dfa-95e95fa95f6b", "bpn": "BPNL00000003CRHL" + }, + { + "credential_id": "10df6ecd-faa3-45ed-8da5-96261f6542dd", + "bpn": "BPNL00000003CRHK" } ] \ No newline at end of file diff --git a/src/database/SsiAuthoritySchemaRegistry.Migrations/Seeder/Data/credentials.json b/src/database/SsiAuthoritySchemaRegistry.Migrations/Seeder/Data/credentials.json index 6e636ec..02baff7 100644 --- a/src/database/SsiAuthoritySchemaRegistry.Migrations/Seeder/Data/credentials.json +++ b/src/database/SsiAuthoritySchemaRegistry.Migrations/Seeder/Data/credentials.json @@ -9,49 +9,14 @@ "type_id": 2, "name": "MembershipCredential" }, - { - "id": "255e01fc-65f6-43cd-8dfa-95e95fa95f62", - "type_id": 3, - "name": "TraceabilityCredential" - }, - { - "id": "255e01fc-65f6-43cd-8dfa-95e95fa95f63", - "type_id": 3, - "name": "QualityCredential" - }, - { - "id": "255e01fc-65f6-43cd-8dfa-95e95fa95f65", - "type_id": 3, - "name": "CircularEconomyCredential" - }, - { - "id": "255e01fc-65f6-43cd-8dfa-95e95fa95f66", - "type_id": 3, - "name": "PcfCredential" - }, - { - "id": "255e01fc-65f6-43cd-8dfa-95e95fa95f67", - "type_id": 3, - "name": "DemandCapacityCredential" - }, - { - "id": "255e01fc-65f6-43cd-8dfa-95e95fa95f68", - "type_id": 3, - "name": "PurisCredential" - }, - { - "id": "255e01fc-65f6-43cd-8dfa-95e95fa95f69", - "type_id": 3, - "name": "BusinessPartnerCredential" - }, - { - "id": "255e01fc-65f6-43cd-8dfa-95e95fa95f6a", - "type_id": 3, - "name": "BehavioralTwinCredential" - }, { "id": "255e01fc-65f6-43cd-8dfa-95e95fa95f6b", "type_id": 4, "name": "DismantlerCredential" + }, + { + "id": "10df6ecd-faa3-45ed-8da5-96261f6542dd", + "type_id": 3, + "name": "DataExchangeGovernanceCredential" } ] \ No newline at end of file diff --git a/tests/database/SsiAuthoritySchemaRegistry.DbAccess.Tests/CredentialRepositoryTests.cs b/tests/database/SsiAuthoritySchemaRegistry.DbAccess.Tests/CredentialRepositoryTests.cs index 333db9e..d9e8a3d 100644 --- a/tests/database/SsiAuthoritySchemaRegistry.DbAccess.Tests/CredentialRepositoryTests.cs +++ b/tests/database/SsiAuthoritySchemaRegistry.DbAccess.Tests/CredentialRepositoryTests.cs @@ -55,18 +55,11 @@ public async Task GetCredentials_WithValidData_ReturnsExpected() var result = await sut.GetCredentials(null, null).ToListAsync(); // Assert - result.Should().HaveCount(11).And.Satisfy( + result.Should().HaveCount(4).And.Satisfy( x => x.CredentialName == "BusinessPartnerNumber" && x.Credential == "BusinessPartnerCredential", x => x.CredentialName == "Membership" && x.Credential == "MembershipCredential", - x => x.CredentialName == "Framework" && x.Credential == "TraceabilityCredential", - x => x.CredentialName == "Framework" && x.Credential == "QualityCredential", - x => x.CredentialName == "Framework" && x.Credential == "CircularEconomyCredential", - x => x.CredentialName == "Framework" && x.Credential == "PcfCredential", - x => x.CredentialName == "Framework" && x.Credential == "DemandCapacityCredential", - x => x.CredentialName == "Framework" && x.Credential == "PurisCredential", - x => x.CredentialName == "Framework" && x.Credential == "BusinessPartnerCredential", - x => x.CredentialName == "Framework" && x.Credential == "BehavioralTwinCredential", - x => x.CredentialName == "CompanyRole" && x.Credential == "DismantlerCredential"); + x => x.CredentialName == "CompanyRole" && x.Credential == "DismantlerCredential", + x => x.CredentialName == "Framework" && x.Credential == "DataExchangeGovernanceCredential"); } [Fact] @@ -93,15 +86,8 @@ public async Task GetCredentials_WithTypeFilter_ReturnsExpected() var result = await sut.GetCredentials(null, CredentialTypeId.Framework).ToListAsync(); // Assert - result.Should().HaveCount(8).And.Satisfy( - x => x.CredentialName == "Framework" && x.Credential == "TraceabilityCredential", - x => x.CredentialName == "Framework" && x.Credential == "QualityCredential", - x => x.CredentialName == "Framework" && x.Credential == "CircularEconomyCredential", - x => x.CredentialName == "Framework" && x.Credential == "PcfCredential", - x => x.CredentialName == "Framework" && x.Credential == "DemandCapacityCredential", - x => x.CredentialName == "Framework" && x.Credential == "PurisCredential", - x => x.CredentialName == "Framework" && x.Credential == "BusinessPartnerCredential", - x => x.CredentialName == "Framework" && x.Credential == "BehavioralTwinCredential"); + result.Should().ContainSingle().And.Satisfy( + x => x.CredentialName == "Framework" && x.Credential == "DataExchangeGovernanceCredential"); } #endregion diff --git a/tests/registry/SsiAuthoritySchemaRegistry.Service.Tests/Controllers/RegistryControllerTests.cs b/tests/registry/SsiAuthoritySchemaRegistry.Service.Tests/Controllers/RegistryControllerTests.cs index ba4230e..915cfea 100644 --- a/tests/registry/SsiAuthoritySchemaRegistry.Service.Tests/Controllers/RegistryControllerTests.cs +++ b/tests/registry/SsiAuthoritySchemaRegistry.Service.Tests/Controllers/RegistryControllerTests.cs @@ -52,18 +52,11 @@ public async Task GetCredentials_WithoutFilters_ReturnsExpected() var data = await _client.GetFromJsonAsync>($"{BaseUrl}/credentials", JsonOptions); // Assert - data.Should().NotBeNull().And.HaveCount(11).And.Satisfy( - x => x.CredentialName == "BusinessPartnerNumber" && x.Credential == "BusinessPartnerCredential", - x => x.CredentialName == "Membership" && x.Credential == "MembershipCredential", - x => x.CredentialName == "Framework" && x.Credential == "TraceabilityCredential", - x => x.CredentialName == "Framework" && x.Credential == "QualityCredential", - x => x.CredentialName == "Framework" && x.Credential == "CircularEconomyCredential", - x => x.CredentialName == "Framework" && x.Credential == "PcfCredential", - x => x.CredentialName == "Framework" && x.Credential == "DemandCapacityCredential", - x => x.CredentialName == "Framework" && x.Credential == "PurisCredential", - x => x.CredentialName == "Framework" && x.Credential == "BusinessPartnerCredential", - x => x.CredentialName == "Framework" && x.Credential == "BehavioralTwinCredential", - x => x.CredentialName == "CompanyRole" && x.Credential == "DismantlerCredential" + data.Should().NotBeNull().And.HaveCount(4).And.Satisfy( + x => x.CredentialName == "BusinessPartnerNumber" && x.Credential == "BusinessPartnerCredential" && x.Authorities.Count() == 1, + x => x.CredentialName == "Membership" && x.Credential == "MembershipCredential" && x.Authorities.Count() == 1, + x => x.CredentialName == "CompanyRole" && x.Credential == "DismantlerCredential" && x.Authorities.Count() == 2, + x => x.CredentialName == "Framework" && x.Credential == "DataExchangeGovernanceCredential" && x.Authorities.Count() == 1 ); } @@ -85,15 +78,8 @@ public async Task GetCredentials_WithCredentialTypeFilters_ReturnsExpected() var data = await _client.GetFromJsonAsync>($"{BaseUrl}/credentials?credentialTypeId={CredentialTypeId.Framework}", JsonOptions); // Assert - data.Should().HaveCount(8).And.Satisfy( - x => x.CredentialName == "Framework" && x.Credential == "TraceabilityCredential", - x => x.CredentialName == "Framework" && x.Credential == "QualityCredential", - x => x.CredentialName == "Framework" && x.Credential == "CircularEconomyCredential", - x => x.CredentialName == "Framework" && x.Credential == "PcfCredential", - x => x.CredentialName == "Framework" && x.Credential == "DemandCapacityCredential", - x => x.CredentialName == "Framework" && x.Credential == "PurisCredential", - x => x.CredentialName == "Framework" && x.Credential == "BusinessPartnerCredential", - x => x.CredentialName == "Framework" && x.Credential == "BehavioralTwinCredential"); + data.Should().HaveCount(1).And.Satisfy( + x => x.CredentialName == "Framework" && x.Credential == "DataExchangeGovernanceCredential"); } #endregion