From d93b0c58855859907ebd2f13038810cea8b05564 Mon Sep 17 00:00:00 2001 From: Melvin Johner Date: Tue, 20 Aug 2024 11:53:25 +0200 Subject: [PATCH] fix(#281): cascade delete on TestResult to ScenarioExecution --- .../simulator/model/TestResult.java | 5 ++-- .../web/rest/TestResultResourceIT.java | 23 +++++++++++++++---- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/simulator-spring-boot/src/main/java/org/citrusframework/simulator/model/TestResult.java b/simulator-spring-boot/src/main/java/org/citrusframework/simulator/model/TestResult.java index 18a78eb37..9472cc65b 100644 --- a/simulator-spring-boot/src/main/java/org/citrusframework/simulator/model/TestResult.java +++ b/simulator-spring-boot/src/main/java/org/citrusframework/simulator/model/TestResult.java @@ -40,6 +40,7 @@ import java.util.Set; import static jakarta.persistence.CascadeType.ALL; +import static jakarta.persistence.CascadeType.REMOVE; import static java.util.Objects.nonNull; import static lombok.AccessLevel.NONE; import static org.apache.commons.lang3.exception.ExceptionUtils.getRootCause; @@ -107,7 +108,7 @@ public class TestResult extends AbstractAuditingEntity impleme /** * Optional test parameters */ - @OneToMany(mappedBy = "testResult", cascade = ALL) + @OneToMany(mappedBy = "testResult", cascade = {REMOVE}) @JsonIgnoreProperties(value = {"testResult"}, allowSetters = true) private final Set testParameters = new HashSet<>(); @@ -132,7 +133,7 @@ public class TestResult extends AbstractAuditingEntity impleme private String failureType; @ToString.Exclude - @OneToOne(mappedBy = "testResult") + @OneToOne(mappedBy = "testResult", cascade = ALL) private ScenarioExecution scenarioExecution; /** diff --git a/simulator-spring-boot/src/test/java/org/citrusframework/simulator/web/rest/TestResultResourceIT.java b/simulator-spring-boot/src/test/java/org/citrusframework/simulator/web/rest/TestResultResourceIT.java index 99f622ff2..a91199a49 100644 --- a/simulator-spring-boot/src/test/java/org/citrusframework/simulator/web/rest/TestResultResourceIT.java +++ b/simulator-spring-boot/src/test/java/org/citrusframework/simulator/web/rest/TestResultResourceIT.java @@ -98,7 +98,12 @@ public class TestResultResourceIT { * if they test an entity which requires the current entity. */ public static TestResult createEntity(EntityManager entityManager) { - return TestResult.builder() + return createEntity(entityManager, false); + + } + + public static TestResult createEntity(EntityManager entityManager, boolean includeScenarioExecution) { + var testResult = TestResult.builder() .status(DEFAULT_STATUS) .testName(DEFAULT_TEST_NAME) .className(DEFAULT_CLASS_NAME) @@ -108,6 +113,12 @@ public static TestResult createEntity(EntityManager entityManager) { .createdDate(DEFAULT_CREATED_DATE) .lastModifiedDate(DEFAULT_LAST_MODIFIED_DATE) .build(); + + if (includeScenarioExecution) { + testResult.setScenarioExecution(ScenarioExecutionResourceIT.createEntity(entityManager)); + } + + return testResult; } /** @@ -758,16 +769,18 @@ void getAllTestResultsByTestParameterIsEqualToSomething() throws Exception { @Test @Transactional void deleteAllTestResults() throws Exception { - TestParameter testParameter = getOrCreateTestParameterWithTestResult(); + testResult = createEntity(entityManager, true); + getOrCreateTestParameterWithTestResult(); - int databaseSizeBeforeDelete = testResultRepository.findAll().size(); + assertThat(testResultRepository.findAll()) + .isNotEmpty(); mockMvc .perform(delete(ENTITY_API_URL).accept(MediaType.APPLICATION_JSON)) .andExpect(status().isNoContent()); - List eventList = testResultRepository.findAll(); - assertThat(eventList).hasSize(databaseSizeBeforeDelete - 1); + assertThat(testResultRepository.findAll()) + .isEmpty(); } private TestParameter getOrCreateTestParameterWithTestResult() {