Skip to content

feat(api): specify delete entity-template behavior and enforce relation-target validation - #91

Closed
walterspieler-dkt wants to merge 17 commits into
mainfrom
CPDE-2895-idp-core-specify-and-validate-behavior-of-delete-entity-template
Closed

feat(api): specify delete entity-template behavior and enforce relation-target validation#91
walterspieler-dkt wants to merge 17 commits into
mainfrom
CPDE-2895-idp-core-specify-and-validate-behavior-of-delete-entity-template

Conversation

@walterspieler-dkt

Copy link
Copy Markdown
Collaborator

What this PR Provides

  • Adds a deletion guard for entity templates that are referenced as
    targetTemplateIdentifier by another template relation.
  • Introduces a dedicated domain exception
    EntityTemplateIsRelationTargetException with a clear validation message.
  • Implements cascade delete of all entities for a template before deleting the
    template itself, in the same transactional flow.
  • Extends repository ports/adapters and JPA repositories to support:
    • relation-target usage check before deletion.
    • bulk deletion of entities by template identifier.
  • Updates API behavior for DELETE /api/v1/entity-templates/{identifier}:
    • success response is now HTTP 200.
    • returns HTTP 400 when the template is still used as a relation target.
    • still returns HTTP 404 when template does not exist.
  • Adds/updates tests to validate:
    • service-level cascade deletion call path.
    • controller-level HTTP 200 success behavior.
    • controller-level HTTP 400 when deletion is blocked by relation targeting.

ADR link:

  • N/A

Fixes

  • N/A

Review

The reviewer must double-check these points:

  • The reviewer has tested the feature
  • The reviewer has reviewed the implementation of the feature
  • The documentation has been updated
  • The feature implementation respects the Technical Doc / ADR previously produced
  • The Pull Request title has a ! after the type/scope to identify the breaking
    change in the release note and ensure we will release a major version.

How to test

Please refer (copy/paste) the test section from the User Story. This should include

  • The initial state: what should be the status of the system before testing
    • A running idp-core API with a seeded database containing:
      • an existing template not referenced as a relation target
        (example: monitoring-service).
      • a template referenced as relation target by at least one other template
        (example from current test data: microservice).
      • a non-existing template identifier for negative tests.
  • What and how to test: steps to perform to test the feature
    • Call DELETE /api/v1/entity-templates/monitoring-service.
    • Verify the endpoint returns HTTP 200.
    • Verify entities belonging to monitoring-service are removed (for example,
      by checking related entity retrieval endpoints or DB content).
    • Call DELETE /api/v1/entity-templates/microservice.
    • Verify the endpoint returns HTTP 400 with an explanatory error message about
      relation-target usage.
    • Call DELETE /api/v1/entity-templates/<non-existent-id>.
    • Verify the endpoint returns HTTP 404.
  • Expected results: what should be observed for success or failure
    • Successful deletion returns HTTP 200 and removes both template and owned
      entities.
    • Deletion is rejected with HTTP 400 when the template is still used as a
      relation target in another template.
    • Unknown template identifiers return HTTP 404.

Breaking changes (if any)

  • API JSON schema modification (existing resource / behavior):
    • DELETE /api/v1/entity-templates/{identifier} success status changed from
      HTTP 204 to HTTP 200.
  • Behavior modification of a component:
    • template deletion now explicitly fails with HTTP 400 when the template is a
      relation target for another template.
  • Others:
    • cascade delete now explicitly removes all entities for the template before
      deleting the template.

Context of the Breaking Change

The delete-template endpoint behavior was made explicit and aligned with domain
integrity rules:

  • prevent deletion when it would leave dangling relation definitions.
  • ensure template-owned entities are purged in the same transactional flow.
  • expose clear API outcomes for consumers (200/400/404).

Result of the Breaking Change

  • API consumers that expected HTTP 204 on successful template deletion must
    adapt to HTTP 200.
  • API consumers now receive HTTP 400 for relation-target constraint violations
    and should resolve referencing relations before retrying deletion.

@walterspieler-dkt walterspieler-dkt self-assigned this Jul 10, 2026
@CLAassistant

CLAassistant commented Jul 10, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@walterspieler-dkt

Copy link
Copy Markdown
Collaborator Author

@copilot resolve the merge conflicts in this pull request

Co-authored-by: walterspieler-dkt <167087950+walterspieler-dkt@users.noreply.github.com>

Copilot AI commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

@copilot resolve the merge conflicts in this pull request

Merge conflicts resolved in commit chore: resolve merge conflicts with origin/main.

5 files had conflicts — all resolved by keeping additions from both sides:

  • EntityTemplateRepositoryPort + PostgresEntityTemplateAdapter: both existsRelationTargetingTemplate (ours) and existsById (main) are now present.
  • EntityTemplateValidationService: validateForDeletion now runs both the relation-target guard and the dynamic-mapping usage guard in sequence.
  • ApiExceptionHandler: switched to wildcard imports (entity_dynamic_mapping.*, entity_template.*) covering all exception types from both branches.
  • JpaEntityTemplateRepository: kept both the RevisionRepository extension and the countRelationTargetingTemplate JPQL query.

All 91 unit tests pass, build compiles cleanly, and CodeQL finds no issues.

@github-code-quality

github-code-quality Bot commented Aug 26, 2026

Copy link
Copy Markdown

Code Coverage Overview

Languages: Java

Java / code-coverage/jacoco

The overall line coverage in commit a910924 in the CPDE-2895-idp-core-s... branch remains at 91%, unchanged from commit f2964c4 in the main branch.

Show a line coverage summary of the most impacted files.
File main f2964c4 CPDE-2895-idp-core-s... a910924 +/-
com/decathlon/i...ionHandler.java 83% 83% 0%
com/decathlon/i...ateAdapter.java 91% 91% 0%
com/decathlon/i...ateService.java 94% 94% 0%
com/decathlon/i...tyTemplate.java 100% 100% 0%
com/decathlon/i...ionService.java 100% 100% 0%
com/decathlon/i...ityAdapter.java 81% 82% +1%
com/decathlon/i...ionService.java 90% 100% +10%
com/decathlon/i...tException.java 0% 75% +75%
com/decathlon/i...eException.java 0% 100% +100%
com/decathlon/i...gException.java 0% 100% +100%

Updated August 27, 2026 09:22 UTC

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR makes entity-template deletion behavior explicit across Domain, Persistence, and API layers by preventing deletion when a template is still referenced as a relation target, and by cascading deletion of template-owned entities in the same transactional flow.

Changes:

  • Added a relation-target deletion guard via a new domain exception and a persistence-side existence check.
  • Implemented cascade deletion of all entities for a template before deleting the template itself (single transaction).
  • Updated API contract and tests for DELETE /api/v1/entity-templates/{identifier} to return 200 on success and 400 on relation-target constraint violations.

Reviewed changes

Copilot reviewed 15 out of 15 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
src/test/java/com/decathlon/idp_core/infrastructure/adapters/api/controller/EntityTemplateControllerTest.java Updates DELETE endpoint test to expect 200 and adds a 400 relation-target scenario.
src/test/java/com/decathlon/idp_core/domain/service/entity_template/EntityTemplateServiceTest.java Adds service-level tests for cascade delete flow and validation failure behavior.
src/main/java/com/decathlon/idp_core/infrastructure/adapters/persistence/repository/JpaEntityTemplateRepository.java Adds JPQL count query to detect relation-target usage before deletion.
src/main/java/com/decathlon/idp_core/infrastructure/adapters/persistence/repository/JpaEntityRepository.java Adds derived delete for bulk removal of entities by template identifier (and documents intent).
src/main/java/com/decathlon/idp_core/infrastructure/adapters/persistence/PostgresEntityTemplateAdapter.java Implements new relation-target existence check via JPA repository.
src/main/java/com/decathlon/idp_core/infrastructure/adapters/persistence/PostgresEntityAdapter.java Implements new port method for deleting entities by template identifier.
src/main/java/com/decathlon/idp_core/infrastructure/adapters/api/handler/ApiExceptionHandler.java Maps the new domain exception to HTTP 400 Bad Request.
src/main/java/com/decathlon/idp_core/infrastructure/adapters/api/controller/EntityTemplateController.java Updates DELETE endpoint contract/annotations to return 200 and document 400/404.
src/main/java/com/decathlon/idp_core/domain/service/entity_template/EntityTemplateValidationService.java Adds relation-target validation to the deletion preconditions.
src/main/java/com/decathlon/idp_core/domain/service/entity_template/EntityTemplateService.java Adds cascade purge call before template deletion inside a transaction.
src/main/java/com/decathlon/idp_core/domain/port/EntityTemplateRepositoryPort.java Extends port contract with relation-target existence check.
src/main/java/com/decathlon/idp_core/domain/port/EntityRepositoryPort.java Extends port contract with bulk entity deletion by template identifier.
src/main/java/com/decathlon/idp_core/domain/exception/entity_template/EntityTemplateIsRelationTargetException.java Introduces a domain-specific exception for relation-target deletion constraint.
src/main/java/com/decathlon/idp_core/domain/constant/ValidationMessages.java Adds a new validation message for the relation-target deletion constraint.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@brandPittCode brandPittCode changed the title feat(api): specify delete entity-template behavior and enforce relation-target validation feat(api)!: specify delete entity-template behavior and enforce relation-target validation Aug 26, 2026
@brandPittCode brandPittCode changed the title feat(api)!: specify delete entity-template behavior and enforce relation-target validation feat(api): specify delete entity-template behavior and enforce relation-target validation Aug 26, 2026
brandPittCode
brandPittCode previously approved these changes Aug 26, 2026
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Andrés Brand <andresbrand@gmail.com>
@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants