From d1bb5e0e408c2ef6e72db604c1640aea25f939c3 Mon Sep 17 00:00:00 2001 From: Phil Schneider Date: Thu, 25 Apr 2024 18:15:16 +0200 Subject: [PATCH] feat(seeding): add delete seeder to remove changed entries Refs: #25 --- .../Repositories/PolicyRepository.cs | 18 ++- .../Entities/IActiveEntity.cs | 6 + .../PolicyHub.Entities/Entities/Policy.cs | 2 +- .../Entities/PolicyAttribute.cs | 9 +- .../PolicyAttributeAssignedUseCases.cs | 6 +- .../PolicyHub.Entities/Entities/PolicyType.cs | 2 +- .../PolicyHub.Entities/Entities/UseCase.cs | 6 +- .../PolicyHub.Entities/PolicyHubContext.cs | 10 +- ...40425103233_25-policy-seeding.Designer.cs} | 45 +++---- ...cs => 20240425103233_25-policy-seeding.cs} | 51 ++++---- .../PolicyHubContextModelSnapshot.cs | 41 +++---- .../Seeder/BatchDeleteSeeder.cs | 97 +++++++++++++++ .../Seeder/BatchInsertSeeder.cs | 4 +- .../Seeder/BatchUpdateSeeder.cs | 10 +- .../policy_attribute_assigned_use_cases.json | 112 +++++++++--------- .../Seeder/Data/policy_attributes.json | 6 +- .../Data/policy_kind_configurations.json | 1 - .../BusinessLogic/PolicyHubBusinessLogic.cs | 9 +- .../PolicyRepositoryTests.cs | 15 ++- .../Controllers/PolicyHubControllerTests.cs | 84 +------------ 20 files changed, 303 insertions(+), 231 deletions(-) create mode 100644 src/database/PolicyHub.Entities/Entities/IActiveEntity.cs rename src/database/PolicyHub.Migrations/Migrations/{20240424045703_feat-25-policy-seeding.Designer.cs => 20240425103233_25-policy-seeding.Designer.cs} (97%) rename src/database/PolicyHub.Migrations/Migrations/{20240424045703_feat-25-policy-seeding.cs => 20240425103233_25-policy-seeding.cs} (81%) create mode 100644 src/database/PolicyHub.Migrations/Seeder/BatchDeleteSeeder.cs diff --git a/src/database/PolicyHub.DbAccess/Repositories/PolicyRepository.cs b/src/database/PolicyHub.DbAccess/Repositories/PolicyRepository.cs index a32c05e..195a1c3 100644 --- a/src/database/PolicyHub.DbAccess/Repositories/PolicyRepository.cs +++ b/src/database/PolicyHub.DbAccess/Repositories/PolicyRepository.cs @@ -35,8 +35,9 @@ public IAsyncEnumerable GetAttributeKeys() => public IAsyncEnumerable GetPolicyTypes(PolicyTypeId? type, UseCaseId? useCase) => dbContext.Policies .Where(p => - (type == null || p.Types.Any(x => x.Id == type)) && - (useCase == null || p.UseCases.Any(x => x.Id == useCase))) + p.IsActive && + (type == null || p.Types.Any(x => x.Id == type && x.IsActive)) && + (useCase == null || p.UseCases.Any(x => x.Id == useCase && x.IsActive))) .Select(p => new PolicyTypeResponse( p.TechnicalKey, p.Types.Where(t => t.IsActive).Select(t => t.Id), @@ -50,13 +51,21 @@ public IAsyncEnumerable GetPolicyTypes(PolicyTypeId? type, U public Task<(bool Exists, string LeftOperand, (AttributeKeyId? Key, IEnumerable Values) Attributes, string? RightOperandValue)> GetPolicyContentAsync(UseCaseId? useCase, PolicyTypeId type, string credential) => dbContext.Policies .Where(p => + p.IsActive && p.Types.Any(t => t.IsActive && t.Id == type) && - (useCase == null || p.UseCases.Any(x => x.Id == useCase)) && + (useCase == null || p.UseCases.Any(x => x.Id == useCase && x.IsActive)) && p.TechnicalKey == credential) .Select(p => new ValueTuple>, string?>( true, p.LeftOperandValue ?? p.TechnicalKey, - new ValueTuple>(p.AttributeKeyId, p.AttributeKey!.PolicyAttributes.Where(pa => pa.IsActive && pa.PolicyId == p.Id && (useCase == null || pa.PolicyAttributeAssignedUseCases.Any(x => x.UseCaseId == useCase))).Select(a => a.AttributeValue)), + new ValueTuple>( + p.AttributeKeyId, + p.AttributeKey!.PolicyAttributes.Where(pa => + pa.IsActive && + pa.PolicyId == p.Id && + pa.IsActive && + (useCase == null || pa.PolicyAttributeAssignedUseCases.Any(x => x.UseCaseId == useCase && x.IsActive)) + ).Select(a => a.AttributeValue)), p.PolicyKind!.Configuration!.RightOperandValue )) .FirstOrDefaultAsync(); @@ -64,6 +73,7 @@ public IAsyncEnumerable GetPolicyTypes(PolicyTypeId? type, U public IAsyncEnumerable<(string TechnicalKey, string LeftOperand, (AttributeKeyId? Key, IEnumerable Values) Attributes, string? RightOperandValue)> GetPolicyForOperandContent(PolicyTypeId type, IEnumerable technicalKeys) => dbContext.Policies .Where(p => + p.IsActive && p.Types.Any(t => t.IsActive && t.Id == type) && technicalKeys.Contains(p.TechnicalKey)) .Select(p => new ValueTuple>, string?>( diff --git a/src/database/PolicyHub.Entities/Entities/IActiveEntity.cs b/src/database/PolicyHub.Entities/Entities/IActiveEntity.cs new file mode 100644 index 0000000..f95f029 --- /dev/null +++ b/src/database/PolicyHub.Entities/Entities/IActiveEntity.cs @@ -0,0 +1,6 @@ +namespace Org.Eclipse.TractusX.PolicyHub.Entities.Entities; + +public interface IActiveEntity +{ + bool IsActive { get; set; } +} diff --git a/src/database/PolicyHub.Entities/Entities/Policy.cs b/src/database/PolicyHub.Entities/Entities/Policy.cs index 98f410d..356a325 100644 --- a/src/database/PolicyHub.Entities/Entities/Policy.cs +++ b/src/database/PolicyHub.Entities/Entities/Policy.cs @@ -21,7 +21,7 @@ namespace Org.Eclipse.TractusX.PolicyHub.Entities.Entities; -public class Policy +public class Policy : IActiveEntity { private Policy() { diff --git a/src/database/PolicyHub.Entities/Entities/PolicyAttribute.cs b/src/database/PolicyHub.Entities/Entities/PolicyAttribute.cs index 3b2693e..07830c7 100644 --- a/src/database/PolicyHub.Entities/Entities/PolicyAttribute.cs +++ b/src/database/PolicyHub.Entities/Entities/PolicyAttribute.cs @@ -21,11 +21,12 @@ namespace Org.Eclipse.TractusX.PolicyHub.Entities.Entities; -public class PolicyAttribute +public class PolicyAttribute : IActiveEntity { private PolicyAttribute() { AttributeValue = null!; + PolicyAttributeAssignedUseCases = new HashSet(); } public PolicyAttribute(Guid id, Guid policyId, AttributeKeyId key, string attributeValue) @@ -38,11 +39,11 @@ public PolicyAttribute(Guid id, Guid policyId, AttributeKeyId key, string attrib } public Guid Id { get; set; } - public Guid PolicyId { get; private set; } + public Guid PolicyId { get; set; } - public AttributeKeyId Key { get; private set; } + public AttributeKeyId Key { get; set; } - public string AttributeValue { get; private set; } + public string AttributeValue { get; set; } public bool IsActive { get; set; } diff --git a/src/database/PolicyHub.Entities/Entities/PolicyAttributeAssignedUseCases.cs b/src/database/PolicyHub.Entities/Entities/PolicyAttributeAssignedUseCases.cs index 77c7f17..d32d1f2 100644 --- a/src/database/PolicyHub.Entities/Entities/PolicyAttributeAssignedUseCases.cs +++ b/src/database/PolicyHub.Entities/Entities/PolicyAttributeAssignedUseCases.cs @@ -29,14 +29,14 @@ private PolicyAttributeAssignedUseCases() UseCase = null!; } - public PolicyAttributeAssignedUseCases(Guid id, UseCaseId useCaseId) + public PolicyAttributeAssignedUseCases(Guid attributeId, UseCaseId useCaseId) : this() { - Id = id; + AttributeId = attributeId; UseCaseId = useCaseId; } - public Guid Id { get; set; } + public Guid AttributeId { get; set; } public UseCaseId UseCaseId { get; set; } diff --git a/src/database/PolicyHub.Entities/Entities/PolicyType.cs b/src/database/PolicyHub.Entities/Entities/PolicyType.cs index c33f9eb..171011d 100644 --- a/src/database/PolicyHub.Entities/Entities/PolicyType.cs +++ b/src/database/PolicyHub.Entities/Entities/PolicyType.cs @@ -21,7 +21,7 @@ namespace Org.Eclipse.TractusX.PolicyHub.Entities.Entities; -public class PolicyType +public class PolicyType : IActiveEntity { private PolicyType() { diff --git a/src/database/PolicyHub.Entities/Entities/UseCase.cs b/src/database/PolicyHub.Entities/Entities/UseCase.cs index be93c89..f7a6880 100644 --- a/src/database/PolicyHub.Entities/Entities/UseCase.cs +++ b/src/database/PolicyHub.Entities/Entities/UseCase.cs @@ -21,15 +21,17 @@ namespace Org.Eclipse.TractusX.PolicyHub.Entities.Entities; -public class UseCase +public class UseCase : IActiveEntity { private UseCase() { Label = null!; Policies = new HashSet(); + PolicyAttributeAssignedUseCases = new HashSet(); } - public UseCase(UseCaseId useCaseId, bool isActive) : this() + public UseCase(UseCaseId useCaseId, bool isActive) + : this() { Id = useCaseId; Label = useCaseId.ToString(); diff --git a/src/database/PolicyHub.Entities/PolicyHubContext.cs b/src/database/PolicyHub.Entities/PolicyHubContext.cs index 3915cb1..540fe1a 100644 --- a/src/database/PolicyHub.Entities/PolicyHubContext.cs +++ b/src/database/PolicyHub.Entities/PolicyHubContext.cs @@ -111,13 +111,10 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) modelBuilder.Entity(entity => { - entity.Property(x => x.IsActive).HasDefaultValue(true); - entity .HasKey(x => new { x.Id }); - entity - .HasKey(x => new { x.PolicyId, x.Key, x.AttributeValue }); + entity.Property(x => x.IsActive).HasDefaultValue(true); entity .HasOne(pa => pa.AttributeKey) @@ -135,13 +132,12 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) entity.Property(x => x.IsActive).HasDefaultValue(true); entity - .HasKey(x => new { x.Id, x.UseCaseId }); + .HasKey(x => new { x.AttributeId, x.UseCaseId }); entity .HasOne(pa => pa.PolicyAttribute) .WithMany(p => p.PolicyAttributeAssignedUseCases) - .HasForeignKey(x => x.Id) - .HasPrincipalKey(x => x.Id); + .HasForeignKey(x => x.AttributeId); entity .HasOne(pa => pa.UseCase) diff --git a/src/database/PolicyHub.Migrations/Migrations/20240424045703_feat-25-policy-seeding.Designer.cs b/src/database/PolicyHub.Migrations/Migrations/20240425103233_25-policy-seeding.Designer.cs similarity index 97% rename from src/database/PolicyHub.Migrations/Migrations/20240424045703_feat-25-policy-seeding.Designer.cs rename to src/database/PolicyHub.Migrations/Migrations/20240425103233_25-policy-seeding.Designer.cs index aac7f24..9236f95 100644 --- a/src/database/PolicyHub.Migrations/Migrations/20240424045703_feat-25-policy-seeding.Designer.cs +++ b/src/database/PolicyHub.Migrations/Migrations/20240425103233_25-policy-seeding.Designer.cs @@ -31,8 +31,8 @@ namespace Org.Eclipse.TractusX.PolicyHub.Migrations.Migrations { [DbContext(typeof(PolicyHubContext))] - [Migration("20240424045703_feat-25-policy-seeding")] - partial class feat25policyseeding + [Migration("20240425103233_25-policy-seeding")] + partial class _25policyseeding { /// protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -177,45 +177,47 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) modelBuilder.Entity("Org.Eclipse.TractusX.PolicyHub.Entities.Entities.PolicyAttribute", b => { - b.Property("PolicyId") + b.Property("Id") + .ValueGeneratedOnAdd() .HasColumnType("uuid") - .HasColumnName("policy_id"); - - b.Property("Key") - .HasColumnType("integer") - .HasColumnName("key"); + .HasColumnName("id"); b.Property("AttributeValue") + .IsRequired() .HasColumnType("text") .HasColumnName("attribute_value"); - b.Property("Id") - .HasColumnType("uuid") - .HasColumnName("id"); - b.Property("IsActive") .ValueGeneratedOnAdd() .HasColumnType("boolean") .HasDefaultValue(true) .HasColumnName("is_active"); - b.HasKey("PolicyId", "Key", "AttributeValue") - .HasName("pk_policy_attributes"); + b.Property("Key") + .HasColumnType("integer") + .HasColumnName("key"); - b.HasAlternateKey("Id") - .HasName("ak_policy_attributes_id"); + b.Property("PolicyId") + .HasColumnType("uuid") + .HasColumnName("policy_id"); + + b.HasKey("Id") + .HasName("pk_policy_attributes"); b.HasIndex("Key") .HasDatabaseName("ix_policy_attributes_key"); + b.HasIndex("PolicyId") + .HasDatabaseName("ix_policy_attributes_policy_id"); + b.ToTable("policy_attributes", "policy-hub"); }); modelBuilder.Entity("Org.Eclipse.TractusX.PolicyHub.Entities.Entities.PolicyAttributeAssignedUseCases", b => { - b.Property("Id") + b.Property("AttributeId") .HasColumnType("uuid") - .HasColumnName("id"); + .HasColumnName("attribute_id"); b.Property("UseCaseId") .HasColumnType("integer") @@ -227,7 +229,7 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) .HasDefaultValue(true) .HasColumnName("is_active"); - b.HasKey("Id", "UseCaseId") + b.HasKey("AttributeId", "UseCaseId") .HasName("pk_policy_attribute_assigned_use_cases"); b.HasIndex("UseCaseId") @@ -479,11 +481,10 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) { b.HasOne("Org.Eclipse.TractusX.PolicyHub.Entities.Entities.PolicyAttribute", "PolicyAttribute") .WithMany("PolicyAttributeAssignedUseCases") - .HasForeignKey("Id") - .HasPrincipalKey("Id") + .HasForeignKey("AttributeId") .OnDelete(DeleteBehavior.Cascade) .IsRequired() - .HasConstraintName("fk_policy_attribute_assigned_use_cases_policy_attributes_id"); + .HasConstraintName("fk_policy_attribute_assigned_use_cases_policy_attributes_attri"); b.HasOne("Org.Eclipse.TractusX.PolicyHub.Entities.Entities.UseCase", "UseCase") .WithMany("PolicyAttributeAssignedUseCases") diff --git a/src/database/PolicyHub.Migrations/Migrations/20240424045703_feat-25-policy-seeding.cs b/src/database/PolicyHub.Migrations/Migrations/20240425103233_25-policy-seeding.cs similarity index 81% rename from src/database/PolicyHub.Migrations/Migrations/20240424045703_feat-25-policy-seeding.cs rename to src/database/PolicyHub.Migrations/Migrations/20240425103233_25-policy-seeding.cs index 4aa6632..a0b45cf 100644 --- a/src/database/PolicyHub.Migrations/Migrations/20240424045703_feat-25-policy-seeding.cs +++ b/src/database/PolicyHub.Migrations/Migrations/20240425103233_25-policy-seeding.cs @@ -1,4 +1,4 @@ -/******************************************************************************** +/******************************************************************************** * Copyright (c) 2023 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional @@ -17,16 +17,15 @@ * SPDX-License-Identifier: Apache-2.0 ********************************************************************************/ +using System; using Microsoft.EntityFrameworkCore.Migrations; #nullable disable -#pragma warning disable CA1814 // Prefer jagged arrays over multidimensional - namespace Org.Eclipse.TractusX.PolicyHub.Migrations.Migrations { /// - public partial class feat25policyseeding : Migration + public partial class _25policyseeding : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) @@ -41,11 +40,10 @@ protected override void Up(MigrationBuilder migrationBuilder) schema: "policy-hub", table: "policy_attributes"); - migrationBuilder.DeleteData( + migrationBuilder.DropPrimaryKey( + name: "pk_policy_attributes", schema: "policy-hub", - table: "policy_types", - keyColumn: "id", - keyValue: 3); + table: "policy_attributes"); migrationBuilder.AddColumn( name: "id", @@ -53,10 +51,10 @@ protected override void Up(MigrationBuilder migrationBuilder) table: "policy_attributes", type: "uuid", nullable: false, - defaultValue: new Guid("00000000-0000-0000-0000-000000000000")); + defaultValueSql: "gen_random_uuid()"); - migrationBuilder.AddUniqueConstraint( - name: "ak_policy_attributes_id", + migrationBuilder.AddPrimaryKey( + name: "pk_policy_attributes", schema: "policy-hub", table: "policy_attributes", column: "id"); @@ -66,16 +64,16 @@ protected override void Up(MigrationBuilder migrationBuilder) schema: "policy-hub", columns: table => new { - id = table.Column(type: "uuid", nullable: false), + attribute_id = table.Column(type: "uuid", nullable: false), use_case_id = table.Column(type: "integer", nullable: false), is_active = table.Column(type: "boolean", nullable: false, defaultValue: true) }, constraints: table => { - table.PrimaryKey("pk_policy_attribute_assigned_use_cases", x => new { x.id, x.use_case_id }); + table.PrimaryKey("pk_policy_attribute_assigned_use_cases", x => new { x.attribute_id, x.use_case_id }); table.ForeignKey( - name: "fk_policy_attribute_assigned_use_cases_policy_attributes_id", - column: x => x.id, + name: "fk_policy_attribute_assigned_use_cases_policy_attributes_attri", + column: x => x.attribute_id, principalSchema: "policy-hub", principalTable: "policy_attributes", principalColumn: "id", @@ -89,6 +87,12 @@ protected override void Up(MigrationBuilder migrationBuilder) onDelete: ReferentialAction.Cascade); }); + migrationBuilder.CreateIndex( + name: "ix_policy_attributes_policy_id", + schema: "policy-hub", + table: "policy_attributes", + column: "policy_id"); + migrationBuilder.CreateIndex( name: "ix_policy_attribute_assigned_use_cases_use_case_id", schema: "policy-hub", @@ -132,8 +136,13 @@ protected override void Down(MigrationBuilder migrationBuilder) name: "policy_attribute_assigned_use_cases", schema: "policy-hub"); - migrationBuilder.DropUniqueConstraint( - name: "ak_policy_attributes_id", + migrationBuilder.DropPrimaryKey( + name: "pk_policy_attributes", + schema: "policy-hub", + table: "policy_attributes"); + + migrationBuilder.DropIndex( + name: "ix_policy_attributes_policy_id", schema: "policy-hub", table: "policy_attributes"); @@ -142,11 +151,11 @@ protected override void Down(MigrationBuilder migrationBuilder) schema: "policy-hub", table: "policy_attributes"); - migrationBuilder.InsertData( + migrationBuilder.AddPrimaryKey( + name: "pk_policy_attributes", schema: "policy-hub", - table: "policy_types", - columns: new[] { "id", "is_active", "label" }, - values: new object[] { 3, true, "Purpose" }); + table: "policy_attributes", + columns: new[] { "policy_id", "key", "attribute_value" }); migrationBuilder.AddForeignKey( name: "fk_policies_policy_kinds_policy_kind_id", diff --git a/src/database/PolicyHub.Migrations/Migrations/PolicyHubContextModelSnapshot.cs b/src/database/PolicyHub.Migrations/Migrations/PolicyHubContextModelSnapshot.cs index 86fff95..5ea70e7 100644 --- a/src/database/PolicyHub.Migrations/Migrations/PolicyHubContextModelSnapshot.cs +++ b/src/database/PolicyHub.Migrations/Migrations/PolicyHubContextModelSnapshot.cs @@ -170,45 +170,47 @@ protected override void BuildModel(ModelBuilder modelBuilder) modelBuilder.Entity("Org.Eclipse.TractusX.PolicyHub.Entities.Entities.PolicyAttribute", b => { - b.Property("PolicyId") + b.Property("Id") + .ValueGeneratedOnAdd() .HasColumnType("uuid") - .HasColumnName("policy_id"); - - b.Property("Key") - .HasColumnType("integer") - .HasColumnName("key"); + .HasColumnName("id"); b.Property("AttributeValue") + .IsRequired() .HasColumnType("text") .HasColumnName("attribute_value"); - b.Property("Id") - .HasColumnType("uuid") - .HasColumnName("id"); - b.Property("IsActive") .ValueGeneratedOnAdd() .HasColumnType("boolean") .HasDefaultValue(true) .HasColumnName("is_active"); - b.HasKey("PolicyId", "Key", "AttributeValue") - .HasName("pk_policy_attributes"); + b.Property("Key") + .HasColumnType("integer") + .HasColumnName("key"); - b.HasAlternateKey("Id") - .HasName("ak_policy_attributes_id"); + b.Property("PolicyId") + .HasColumnType("uuid") + .HasColumnName("policy_id"); + + b.HasKey("Id") + .HasName("pk_policy_attributes"); b.HasIndex("Key") .HasDatabaseName("ix_policy_attributes_key"); + b.HasIndex("PolicyId") + .HasDatabaseName("ix_policy_attributes_policy_id"); + b.ToTable("policy_attributes", "policy-hub"); }); modelBuilder.Entity("Org.Eclipse.TractusX.PolicyHub.Entities.Entities.PolicyAttributeAssignedUseCases", b => { - b.Property("Id") + b.Property("AttributeId") .HasColumnType("uuid") - .HasColumnName("id"); + .HasColumnName("attribute_id"); b.Property("UseCaseId") .HasColumnType("integer") @@ -220,7 +222,7 @@ protected override void BuildModel(ModelBuilder modelBuilder) .HasDefaultValue(true) .HasColumnName("is_active"); - b.HasKey("Id", "UseCaseId") + b.HasKey("AttributeId", "UseCaseId") .HasName("pk_policy_attribute_assigned_use_cases"); b.HasIndex("UseCaseId") @@ -472,11 +474,10 @@ protected override void BuildModel(ModelBuilder modelBuilder) { b.HasOne("Org.Eclipse.TractusX.PolicyHub.Entities.Entities.PolicyAttribute", "PolicyAttribute") .WithMany("PolicyAttributeAssignedUseCases") - .HasForeignKey("Id") - .HasPrincipalKey("Id") + .HasForeignKey("AttributeId") .OnDelete(DeleteBehavior.Cascade) .IsRequired() - .HasConstraintName("fk_policy_attribute_assigned_use_cases_policy_attributes_id"); + .HasConstraintName("fk_policy_attribute_assigned_use_cases_policy_attributes_attri"); b.HasOne("Org.Eclipse.TractusX.PolicyHub.Entities.Entities.UseCase", "UseCase") .WithMany("PolicyAttributeAssignedUseCases") diff --git a/src/database/PolicyHub.Migrations/Seeder/BatchDeleteSeeder.cs b/src/database/PolicyHub.Migrations/Seeder/BatchDeleteSeeder.cs new file mode 100644 index 0000000..acc9ff2 --- /dev/null +++ b/src/database/PolicyHub.Migrations/Seeder/BatchDeleteSeeder.cs @@ -0,0 +1,97 @@ +/******************************************************************************** + * Copyright (c) 2023 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.PolicyHub.Entities; +using Org.Eclipse.TractusX.PolicyHub.Entities.Entities; +using Org.Eclipse.TractusX.Portal.Backend.Framework.Seeding; + +namespace Org.Eclipse.TractusX.PolicyHub.Migrations.Seeder; + +/// +/// Seeder to seed the all configured entities +/// +public class BatchDeleteSeeder : ICustomSeeder +{ + private readonly PolicyHubContext _context; + private readonly ILogger _logger; + private readonly SeederSettings _settings; + + /// + /// Constructor + /// + /// The database context + /// The logger + /// The options + public BatchDeleteSeeder(PolicyHubContext context, ILogger logger, IOptions options) + { + _context = context; + _logger = logger; + _settings = options.Value; + } + + /// + public int Order => 3; + + /// + 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("policies", x => x.Id, cancellationToken).ConfigureAwait(false); + await SeedTable("policy_attributes", x => x.Id, cancellationToken).ConfigureAwait(false); + await SeedTable("policy_kind_configurations", x => x.PolicyKindId, cancellationToken).ConfigureAwait(false); + await SeedTable("policy_assigned_types", x => new { x.PolicyId, x.PolicyTypeId }, cancellationToken).ConfigureAwait(false); + await SeedTable("policy_assigned_use_cases", x => new { x.PolicyId, x.UseCaseId }, cancellationToken).ConfigureAwait(false); + await SeedTable("policy_attribute_assigned_use_cases", x => new { x.AttributeId, x.UseCaseId }, cancellationToken).ConfigureAwait(false); + + await _context.SaveChangesAsync(cancellationToken).ConfigureAwait(false); + } + + 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); + var isActiveEntity = typeof(T).IsAssignableTo(typeof(IActiveEntity)); + + // 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(); + if (isActiveEntity) + { + foreach (var entity in entitiesToRemove) + { + _context.Set().Attach(entity); + (entity as IActiveEntity)!.IsActive = false; + } + } + else + { + _context.Set().RemoveRange(entitiesToRemove); + } + } +} diff --git a/src/database/PolicyHub.Migrations/Seeder/BatchInsertSeeder.cs b/src/database/PolicyHub.Migrations/Seeder/BatchInsertSeeder.cs index ba79b35..a8b714d 100644 --- a/src/database/PolicyHub.Migrations/Seeder/BatchInsertSeeder.cs +++ b/src/database/PolicyHub.Migrations/Seeder/BatchInsertSeeder.cs @@ -60,11 +60,11 @@ public async Task ExecuteAsync(CancellationToken cancellationToken) } await SeedTable("policies", x => x.Id, cancellationToken).ConfigureAwait(false); - await SeedTable("policy_attributes", x => new { x.PolicyId, x.Key, x.AttributeValue }, cancellationToken).ConfigureAwait(false); + await SeedTable("policy_attributes", x => x.Id, cancellationToken).ConfigureAwait(false); await SeedTable("policy_kind_configurations", x => x.PolicyKindId, cancellationToken).ConfigureAwait(false); await SeedTable("policy_assigned_types", x => new { x.PolicyId, x.PolicyTypeId }, cancellationToken).ConfigureAwait(false); await SeedTable("policy_assigned_use_cases", x => new { x.PolicyId, x.UseCaseId }, cancellationToken).ConfigureAwait(false); - await SeedTable("policy_attribute_assigned_use_cases", x => new { x.Id, x.UseCaseId }, cancellationToken).ConfigureAwait(false); + await SeedTable("policy_attribute_assigned_use_cases", x => new { x.AttributeId, x.UseCaseId }, cancellationToken).ConfigureAwait(false); await _context.SaveChangesAsync(cancellationToken).ConfigureAwait(false); } diff --git a/src/database/PolicyHub.Migrations/Seeder/BatchUpdateSeeder.cs b/src/database/PolicyHub.Migrations/Seeder/BatchUpdateSeeder.cs index 01e958d..4279cda 100644 --- a/src/database/PolicyHub.Migrations/Seeder/BatchUpdateSeeder.cs +++ b/src/database/PolicyHub.Migrations/Seeder/BatchUpdateSeeder.cs @@ -44,19 +44,23 @@ public async Task ExecuteAsync(CancellationToken cancellationToken) await SeedTable( "policies", x => new { x.Id }, - x => x.dbEntity.IsActive != x.dataEntity.IsActive, + x => x.dbEntity.IsActive != x.dataEntity.IsActive || x.dbEntity.TechnicalKey != x.dataEntity.TechnicalKey, (dbEntity, entity) => { dbEntity.IsActive = entity.IsActive; + dbEntity.TechnicalKey = entity.TechnicalKey; }, cancellationToken).ConfigureAwait(false); await SeedTable( "policy_attributes", - x => new { x.Id, x.PolicyId, x.Key, x.AttributeValue }, - x => x.dbEntity.IsActive != x.dataEntity.IsActive, + x => new { x.Id }, + x => x.dbEntity.IsActive != x.dataEntity.IsActive || x.dbEntity.AttributeValue != x.dataEntity.AttributeValue || x.dbEntity.Key != x.dataEntity.Key || x.dbEntity.PolicyId != x.dataEntity.PolicyId, (dbEntry, entry) => { dbEntry.IsActive = entry.IsActive; + dbEntry.AttributeValue = entry.AttributeValue; + dbEntry.Key = entry.Key; + dbEntry.PolicyId = entry.PolicyId; }, cancellationToken).ConfigureAwait(false); await _context.SaveChangesAsync(cancellationToken).ConfigureAwait(false); diff --git a/src/database/PolicyHub.Migrations/Seeder/Data/policy_attribute_assigned_use_cases.json b/src/database/PolicyHub.Migrations/Seeder/Data/policy_attribute_assigned_use_cases.json index 09b6b12..08d7a90 100644 --- a/src/database/PolicyHub.Migrations/Seeder/Data/policy_attribute_assigned_use_cases.json +++ b/src/database/PolicyHub.Migrations/Seeder/Data/policy_attribute_assigned_use_cases.json @@ -1,58 +1,58 @@ [ - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300d1", "use_case_id": 1, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300d1", "use_case_id": 2, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300d1", "use_case_id": 3, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300d1", "use_case_id": 4, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300d1", "use_case_id": 5, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300d2", "use_case_id": 1, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300d2", "use_case_id": 2, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300d2", "use_case_id": 3, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300d2", "use_case_id": 4, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300d2", "use_case_id": 5, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300d6", "use_case_id": 1, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300d6", "use_case_id": 2, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300d6", "use_case_id": 3, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300d6", "use_case_id": 4, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300d6", "use_case_id": 5, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300d7", "use_case_id": 1, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300d7", "use_case_id": 2, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300d7", "use_case_id": 3, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300d7", "use_case_id": 4, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300d7", "use_case_id": 5, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300d8", "use_case_id": 1, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300d8", "use_case_id": 2, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300d8", "use_case_id": 3, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300d8", "use_case_id": 4, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300d8", "use_case_id": 5, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300d9", "use_case_id": 1, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300e1", "use_case_id": 1, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300e2", "use_case_id": 1, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300e3", "use_case_id": 2, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300e3", "use_case_id": 3, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300e3", "use_case_id": 4, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300e3", "use_case_id": 5, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300e4", "use_case_id": 2, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300e4", "use_case_id": 3, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300e4", "use_case_id": 4, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300e4", "use_case_id": 5, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300e6", "use_case_id": 2, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300e6", "use_case_id": 3, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300e6", "use_case_id": 4, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300e6", "use_case_id": 5, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300e7", "use_case_id": 2, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300e7", "use_case_id": 3, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300e7", "use_case_id": 4, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300e7", "use_case_id": 5, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300e8", "use_case_id": 2, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300e8", "use_case_id": 3, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300e8", "use_case_id": 4, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300e8", "use_case_id": 5, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300e9", "use_case_id": 2, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300e9", "use_case_id": 3, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300e9", "use_case_id": 4, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300e9", "use_case_id": 5, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300f1", "use_case_id": 2, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300f1", "use_case_id": 3, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300f1", "use_case_id": 4, "is_active": true }, - { "id": "01a0fba3-9b6e-435a-b045-e0e890c300f1", "use_case_id": 5, "is_active": true } + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300d1", "use_case_id": 1, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300d1", "use_case_id": 2, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300d1", "use_case_id": 3, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300d1", "use_case_id": 4, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300d1", "use_case_id": 5, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300d2", "use_case_id": 1, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300d2", "use_case_id": 2, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300d2", "use_case_id": 3, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300d2", "use_case_id": 4, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300d2", "use_case_id": 5, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300d6", "use_case_id": 1, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300d6", "use_case_id": 2, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300d6", "use_case_id": 3, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300d6", "use_case_id": 4, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300d6", "use_case_id": 5, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300d7", "use_case_id": 1, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300d7", "use_case_id": 2, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300d7", "use_case_id": 3, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300d7", "use_case_id": 4, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300d7", "use_case_id": 5, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300d8", "use_case_id": 1, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300d8", "use_case_id": 2, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300d8", "use_case_id": 3, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300d8", "use_case_id": 4, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300d8", "use_case_id": 5, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300d9", "use_case_id": 1, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300e1", "use_case_id": 1, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300e2", "use_case_id": 1, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300e3", "use_case_id": 2, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300e3", "use_case_id": 3, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300e3", "use_case_id": 4, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300e3", "use_case_id": 5, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300e4", "use_case_id": 2, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300e4", "use_case_id": 3, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300e4", "use_case_id": 4, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300e4", "use_case_id": 5, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300e6", "use_case_id": 2, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300e6", "use_case_id": 3, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300e6", "use_case_id": 4, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300e6", "use_case_id": 5, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300e7", "use_case_id": 2, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300e7", "use_case_id": 3, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300e7", "use_case_id": 4, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300e7", "use_case_id": 5, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300e8", "use_case_id": 2, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300e8", "use_case_id": 3, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300e8", "use_case_id": 4, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300e8", "use_case_id": 5, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300e9", "use_case_id": 2, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300e9", "use_case_id": 3, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300e9", "use_case_id": 4, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300e9", "use_case_id": 5, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300f1", "use_case_id": 2, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300f1", "use_case_id": 3, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300f1", "use_case_id": 4, "is_active": true }, + { "attribute_id": "01a0fba3-9b6e-435a-b045-e0e890c300f1", "use_case_id": 5, "is_active": true } ] \ No newline at end of file diff --git a/src/database/PolicyHub.Migrations/Seeder/Data/policy_attributes.json b/src/database/PolicyHub.Migrations/Seeder/Data/policy_attributes.json index c02ac53..167221f 100644 --- a/src/database/PolicyHub.Migrations/Seeder/Data/policy_attributes.json +++ b/src/database/PolicyHub.Migrations/Seeder/Data/policy_attributes.json @@ -59,21 +59,21 @@ "id": "01a0fba3-9b6e-435a-b045-e0e890c300d9", "policy_id": "01a0fba3-9b6e-435a-b045-e0e890c300c9", "key": 5, - "attribute_value": "1.0", + "attribute_value": "Traceability:1.0", "is_active": true }, { "id": "01a0fba3-9b6e-435a-b045-e0e890c300e1", "policy_id": "01a0fba3-9b6e-435a-b045-e0e890c300c9", "key": 5, - "attribute_value": "1.1", + "attribute_value": "Traceability:1.1", "is_active": true }, { "id": "01a0fba3-9b6e-435a-b045-e0e890c300e2", "policy_id": "01a0fba3-9b6e-435a-b045-e0e890c300c9", "key": 5, - "attribute_value": "1.2", + "attribute_value": "Traceability:1.2", "is_active": true }, { diff --git a/src/database/PolicyHub.Migrations/Seeder/Data/policy_kind_configurations.json b/src/database/PolicyHub.Migrations/Seeder/Data/policy_kind_configurations.json index f47fb46..0d4f101 100644 --- a/src/database/PolicyHub.Migrations/Seeder/Data/policy_kind_configurations.json +++ b/src/database/PolicyHub.Migrations/Seeder/Data/policy_kind_configurations.json @@ -1,3 +1,2 @@ [ - { "policy_kind_id": 3, "right_operand_value": "active:{0}" } ] diff --git a/src/hub/PolicyHub.Service/BusinessLogic/PolicyHubBusinessLogic.cs b/src/hub/PolicyHub.Service/BusinessLogic/PolicyHubBusinessLogic.cs index 77f4001..ab18a26 100644 --- a/src/hub/PolicyHub.Service/BusinessLogic/PolicyHubBusinessLogic.cs +++ b/src/hub/PolicyHub.Service/BusinessLogic/PolicyHubBusinessLogic.cs @@ -65,7 +65,14 @@ private static (object rightOperand, AdditionalAttributes? additionalAttribute) AttributeKeyId.DynamicValue => (value ?? "{dynamicValue}", null), AttributeKeyId.Regex => (GetRegexValue(attributes, value), null), _ => operatorId == OperatorId.Equals - ? rightOperands.Count() > 1 ? ($"@{leftOperand}{(useCase != null ? useCase.ToString().Insert(0, ".") : string.Empty)}-{attributes.Key}", new AdditionalAttributes($"@{leftOperand}{(useCase != null ? useCase.ToString().Insert(0, ".") : string.Empty)}-{attributes.Key}", rightOperands)) : (rightOperands.Single(), null) + ? rightOperands.Count() > 1 ? + ($"@{leftOperand}{(useCase != null ? + useCase.ToString().Insert(0, ".") : + string.Empty)}-{attributes.Key}", + new AdditionalAttributes($"@{leftOperand}{(useCase != null ? + useCase.ToString().Insert(0, ".") : + string.Empty)}-{attributes.Key}", rightOperands)) : + (rightOperands.Single(), null) : (rightOperands, null) }; diff --git a/tests/database/PolicyHub.DbAccess.Tests/PolicyRepositoryTests.cs b/tests/database/PolicyHub.DbAccess.Tests/PolicyRepositoryTests.cs index 0112256..c55554a 100644 --- a/tests/database/PolicyHub.DbAccess.Tests/PolicyRepositoryTests.cs +++ b/tests/database/PolicyHub.DbAccess.Tests/PolicyRepositoryTests.cs @@ -150,9 +150,20 @@ public async Task GetPolicyContentAsync_WithRightOperand_ReturnsExpectedResult() // Assert result.Exists.Should().BeTrue(); result.Attributes.Key.Should().Be(AttributeKeyId.Version); - result.Attributes.Values.Should().Contain("1.1"); + result.Attributes.Values.Should().Satisfy( + x => x == "Traceability:1.0", + x => x == "Traceability:1.1", + x => x == "Traceability:1.2", + x => x == "Quality:1.0", + x => x == "PCF:1.0", + x => x == "Behavioraltwin:1.0", + x => x == "Circulareconomy:1.0", + x => x == "Demandcapacity:1.0", + x => x == "Puris:1.0", + x => x == "Businesspartner:1.0" + ); result.LeftOperand.Should().Be("FrameworkAgreement"); - result.RightOperandValue.Should().Be("active:{0}"); + result.RightOperandValue.Should().BeNull(); } #endregion diff --git a/tests/hub/PolicyHub.Service.Tests/Controllers/PolicyHubControllerTests.cs b/tests/hub/PolicyHub.Service.Tests/Controllers/PolicyHubControllerTests.cs index 2174327..21f47a7 100644 --- a/tests/hub/PolicyHub.Service.Tests/Controllers/PolicyHubControllerTests.cs +++ b/tests/hub/PolicyHub.Service.Tests/Controllers/PolicyHubControllerTests.cs @@ -176,7 +176,7 @@ public async Task GetPolicyContent_UsageFrameworkEquals_ReturnsExpected() response.StatusCode.Should().Be(HttpStatusCode.OK); (await response.Content.ReadAsStringAsync()) .Should() - .Be("{\"content\":{\"@context\":[\"https://www.w3.org/ns/odrl.jsonld\",{\"cx\":\"https://w3id.org/catenax/v0.0.1/ns/\"}],\"@type\":\"Offer\",\"@id\":\"....\",\"permission\":{\"action\":\"use\",\"constraint\":{\"leftOperand\":\"cx-policy:FrameworkAgreement\",\"operator\":\"eq\",\"rightOperand\":\"@FrameworkAgreement.Traceability-Version\"}}},\"attributes\":[{\"key\":\"@FrameworkAgreement.Traceability-Version\",\"possibleValues\":[\"active:1.0\",\"active:1.1\",\"active:1.2\"]}]}"); + .Be("{\"content\":{\"@context\":[\"https://www.w3.org/ns/odrl.jsonld\",{\"cx\":\"https://w3id.org/catenax/v0.0.1/ns/\"}],\"@type\":\"Offer\",\"@id\":\"....\",\"permission\":{\"action\":\"use\",\"constraint\":{\"leftOperand\":\"cx-policy:FrameworkAgreement\",\"operator\":\"eq\",\"rightOperand\":\"@FrameworkAgreement.Traceability-Version\"}}},\"attributes\":[{\"key\":\"@FrameworkAgreement.Traceability-Version\",\"possibleValues\":[\"Traceability:1.0\",\"Traceability:1.1\",\"Traceability:1.2\"]}]}"); } [Fact] @@ -190,7 +190,7 @@ public async Task GetPolicyContent_UsageDismantlerIn_ReturnsExpected() response.StatusCode.Should().Be(HttpStatusCode.OK); (await response.Content.ReadAsStringAsync()) .Should() - .Be("{\"content\":{\"@context\":[\"https://www.w3.org/ns/odrl.jsonld\",{\"cx\":\"https://w3id.org/catenax/v0.0.1/ns/\"}],\"@type\":\"Offer\",\"@id\":\"....\",\"permission\":{\"action\":\"use\",\"constraint\":{\"leftOperand\":\"cx-policy:Dismantler.activityType\",\"operator\":\"in\",\"rightOperand\":[\"Audi\",\"BMW\",\"VW\"]}}}}"); + .Be("{\"content\":{\"@context\":[\"https://www.w3.org/ns/odrl.jsonld\",{\"cx\":\"https://w3id.org/catenax/v0.0.1/ns/\"}],\"@type\":\"Offer\",\"@id\":\"....\",\"permission\":{\"action\":\"use\",\"constraint\":{\"leftOperand\":\"cx-policy:Dismantler.activityType\",\"operator\":\"in\",\"rightOperand\":[\"BMW\",\"Audi\",\"VW\"]}}}}"); } [Fact] @@ -220,7 +220,7 @@ public async Task GetPolicyContentWithFiltersAsync_TwoEqualsConstraintsAndOperan ConstraintOperandId.And, new[] { - new Constraints("FrameworkAgreement", OperatorId.Equals, "1.0"), + new Constraints("FrameworkAgreement", OperatorId.Equals, "Traceability:1.0"), new Constraints("companyRole.dismantler", OperatorId.In, "Audi") }); @@ -232,7 +232,7 @@ public async Task GetPolicyContentWithFiltersAsync_TwoEqualsConstraintsAndOperan response.StatusCode.Should().Be(HttpStatusCode.OK); (await response.Content.ReadAsStringAsync()) .Should() - .Be("{\"content\":{\"@context\":[\"https://www.w3.org/ns/odrl.jsonld\",{\"cx\":\"https://w3id.org/catenax/v0.0.1/ns/\"}],\"@type\":\"Offer\",\"@id\":\"....\",\"permission\":{\"action\":\"use\",\"constraint\":{\"odrl:and\":[{\"leftOperand\":\"cx-policy:Dismantler.activityType\",\"operator\":\"in\",\"rightOperand\":[\"Audi\",\"BMW\",\"VW\"]},{\"leftOperand\":\"cx-policy:FrameworkAgreement\",\"operator\":\"eq\",\"rightOperand\":\"@FrameworkAgreement-Version\"}]}}},\"attributes\":[{\"key\":\"@FrameworkAgreement-Version\",\"possibleValues\":[\"active:1.0\",\"active:1.1\",\"active:1.2\",\"active:Behavioraltwin:1.0\",\"active:Businesspartner:1.0\",\"active:Circulareconomy:1.0\",\"active:Demandcapacity:1.0\",\"active:PCF:1.0\",\"active:Puris:1.0\",\"active:Quality:1.0\"]}]}"); + .Be("{\"content\":{\"@context\":[\"https://www.w3.org/ns/odrl.jsonld\",{\"cx\":\"https://w3id.org/catenax/v0.0.1/ns/\"}],\"@type\":\"Offer\",\"@id\":\"....\",\"permission\":{\"action\":\"use\",\"constraint\":{\"odrl:and\":[{\"leftOperand\":\"cx-policy:Dismantler.activityType\",\"operator\":\"in\",\"rightOperand\":[\"BMW\",\"Audi\",\"VW\"]},{\"leftOperand\":\"cx-policy:FrameworkAgreement\",\"operator\":\"eq\",\"rightOperand\":\"@FrameworkAgreement-Version\"}]}}},\"attributes\":[{\"key\":\"@FrameworkAgreement-Version\",\"possibleValues\":[\"Traceability:1.0\",\"Traceability:1.1\",\"Traceability:1.2\",\"Quality:1.0\",\"PCF:1.0\",\"Behavioraltwin:1.0\",\"Circulareconomy:1.0\",\"Demandcapacity:1.0\",\"Puris:1.0\",\"Businesspartner:1.0\"]}]}"); } [Fact] @@ -265,7 +265,7 @@ public async Task GetPolicyContentWithFiltersAsync_MultipleConstraintsEqualsAndO ConstraintOperandId.And, new[] { - new Constraints("FrameworkAgreement", OperatorId.Equals, "1.0"), + new Constraints("FrameworkAgreement", OperatorId.Equals, "Traceability:1.0"), new Constraints("companyRole.dismantler", OperatorId.In, "Audi"), new Constraints("BusinessPartnerNumber", OperatorId.Equals, "BPNL00000003CRHK") }); @@ -278,81 +278,9 @@ public async Task GetPolicyContentWithFiltersAsync_MultipleConstraintsEqualsAndO response.StatusCode.Should().Be(HttpStatusCode.OK); (await response.Content.ReadAsStringAsync()) .Should() - .Be("{\"content\":{\"@context\":[\"https://www.w3.org/ns/odrl.jsonld\",{\"cx\":\"https://w3id.org/catenax/v0.0.1/ns/\"}],\"@type\":\"Offer\",\"@id\":\"....\",\"permission\":{\"action\":\"use\",\"constraint\":{\"odrl:and\":[{\"leftOperand\":\"cx-policy:BusinessPartnerNumber\",\"operator\":\"eq\",\"rightOperand\":\"BPNL00000003CRHK\"},{\"leftOperand\":\"cx-policy:Dismantler.activityType\",\"operator\":\"in\",\"rightOperand\":[\"Audi\",\"BMW\",\"VW\"]},{\"leftOperand\":\"cx-policy:FrameworkAgreement\",\"operator\":\"eq\",\"rightOperand\":\"@FrameworkAgreement-Version\"}]}}},\"attributes\":[{\"key\":\"@FrameworkAgreement-Version\",\"possibleValues\":[\"active:1.0\",\"active:1.1\",\"active:1.2\",\"active:Behavioraltwin:1.0\",\"active:Businesspartner:1.0\",\"active:Circulareconomy:1.0\",\"active:Demandcapacity:1.0\",\"active:PCF:1.0\",\"active:Puris:1.0\",\"active:Quality:1.0\"]}]}"); + .Be("{\"content\":{\"@context\":[\"https://www.w3.org/ns/odrl.jsonld\",{\"cx\":\"https://w3id.org/catenax/v0.0.1/ns/\"}],\"@type\":\"Offer\",\"@id\":\"....\",\"permission\":{\"action\":\"use\",\"constraint\":{\"odrl:and\":[{\"leftOperand\":\"cx-policy:BusinessPartnerNumber\",\"operator\":\"eq\",\"rightOperand\":\"BPNL00000003CRHK\"},{\"leftOperand\":\"cx-policy:Dismantler.activityType\",\"operator\":\"in\",\"rightOperand\":[\"BMW\",\"Audi\",\"VW\"]},{\"leftOperand\":\"cx-policy:FrameworkAgreement\",\"operator\":\"eq\",\"rightOperand\":\"@FrameworkAgreement-Version\"}]}}},\"attributes\":[{\"key\":\"@FrameworkAgreement-Version\",\"possibleValues\":[\"Traceability:1.0\",\"Traceability:1.1\",\"Traceability:1.2\",\"Quality:1.0\",\"PCF:1.0\",\"Behavioraltwin:1.0\",\"Circulareconomy:1.0\",\"Demandcapacity:1.0\",\"Puris:1.0\",\"Businesspartner:1.0\"]}]}"); } - // [Fact] - // public async Task GetPolicyContentWithFiltersAsync_MultipleConstraintsEqualsOrOperand_ReturnsExpected() - // { - // // Arrange - // var data = new PolicyContentRequest( - // PolicyTypeId.Usage, - // ConstraintOperandId.Or, - // new[] - // { - // new Constraints("FrameworkAgreement.traceability", OperatorId.Equals, null), - // new Constraints("companyRole.dismantler", OperatorId.In, null), - // }); - // [Fact] - // public async Task GetPolicyContentWithFiltersAsync_MultipleConstraintsEqualsOrOperand_ReturnsExpected() - // { - // // Arrange - // var data = new PolicyContentRequest( - // PolicyTypeId.Usage, - // ConstraintOperandId.Or, - // new[] - // { - // new Constraints("FrameworkAgreement.traceability", OperatorId.Equals, null), - // new Constraints("companyRole.dismantler", OperatorId.In, null), - // }); - - // // Act - // var response = await _client.PostAsJsonAsync($"{BaseUrl}/policy-content", data, JsonOptions); - - // // Assert - // response.Should().NotBeNull(); - // response.StatusCode.Should().Be(HttpStatusCode.OK); - // (await response.Content.ReadAsStringAsync()) - // .Should() - // .Be("{\"content\":{\"@context\":[\"https://www.w3.org/ns/odrl.jsonld\",{\"cx\":\"https://w3id.org/catenax/v0.0.1/ns/\"}],\"@type\":\"Offer\",\"@id\":\"....\",\"permission\":{\"action\":\"use\",\"constraint\":{\"odrl:or\":[{\"leftOperand\":\"FrameworkAgreement.traceability\",\"operator\":\"eq\",\"rightOperand\":\"@FrameworkAgreement.traceability-Version\"},{\"leftOperand\":\"Dismantler.activityType\",\"operator\":\"in\",\"rightOperand\":[\"Audi\",\"BMW\",\"VW\"]}]}}},\"attributes\":[{\"key\":\"@FrameworkAgreement.traceability-Version\",\"possibleValues\":[\"active:1.0\",\"active:1.1\",\"active:1.2\"]}]}"); - // } - - // [Fact] - // public async Task GetPolicyContentWithFiltersAsync_WithSameConstraintKeys_ReturnsError() - // { - // // Arrange - // var data = new PolicyContentRequest( - // PolicyTypeId.Usage, - // ConstraintOperandId.Or, - // new[] - // { - // new Constraints("FrameworkAgreement.traceability", OperatorId.Equals, null), - // new Constraints("FrameworkAgreement.traceability", OperatorId.Equals, null), - // }); - // [Fact] - // public async Task GetPolicyContentWithFiltersAsync_WithSameConstraintKeys_ReturnsError() - // { - // // Arrange - // var data = new PolicyContentRequest( - // PolicyTypeId.Usage, - // ConstraintOperandId.Or, - // new[] - // { - // new Constraints("FrameworkAgreement.traceability", OperatorId.Equals, null), - // new Constraints("FrameworkAgreement.traceability", OperatorId.Equals, null), - // }); - - // // Act - // var response = await _client.PostAsJsonAsync($"{BaseUrl}/policy-content", data, JsonOptions); - - // // Assert - // response.Should().NotBeNull(); - // response.StatusCode.Should().Be(HttpStatusCode.BadRequest); - // var error = await response.Content.ReadFromJsonAsync(JsonOptions); - // error!.Errors.Should().ContainSingle().And.Satisfy( - // x => x.Value.Single() == "Keys FrameworkAgreement.traceability have been defined multiple times"); - // } - #endregion #region Swagger