From 31149dd9dee370af515a8c4e2b8b3d094b09d2d3 Mon Sep 17 00:00:00 2001 From: Timon Borter Date: Sun, 29 Oct 2023 21:44:40 +0100 Subject: [PATCH] fix: scenario execution rest api --- .../simulator/model/Message.java | 12 +- .../simulator/model/ScenarioExecution.java | 18 ++- .../simulator/model/TestResult.java | 5 + .../ScenarioExecutionRepository.java | 22 ++- .../criteria/ScenarioExecutionCriteria.java | 2 +- .../ScenarioExecutionCriteriaTest.java | 147 ++++++++++++++++++ .../ScenarioExecutionServiceImplTest.java | 69 ++++++++ .../web/rest/ScenarioExecutionResourceIT.java | 47 ++++-- 8 files changed, 295 insertions(+), 27 deletions(-) create mode 100644 simulator-starter/src/test/java/org/citrusframework/simulator/service/criteria/ScenarioExecutionCriteriaTest.java create mode 100644 simulator-starter/src/test/java/org/citrusframework/simulator/service/impl/ScenarioExecutionServiceImplTest.java diff --git a/simulator-starter/src/main/java/org/citrusframework/simulator/model/Message.java b/simulator-starter/src/main/java/org/citrusframework/simulator/model/Message.java index 5f4156b97..c642b9c33 100644 --- a/simulator-starter/src/main/java/org/citrusframework/simulator/model/Message.java +++ b/simulator-starter/src/main/java/org/citrusframework/simulator/model/Message.java @@ -37,8 +37,10 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.HashSet; import java.util.Map; import java.util.Objects; +import java.util.Set; /** * JPA entity for representing inbound and outbound messages @@ -72,7 +74,7 @@ public class Message extends AbstractAuditingEntity implements Se @OrderBy("name ASC") @JsonIgnoreProperties(value = { "message" }, allowSetters = true) @OneToMany(fetch = FetchType.LAZY, mappedBy = "message", cascade = CascadeType.ALL, orphanRemoval = true) - private Collection headers = new ArrayList<>(); + private final Set headers = new HashSet<>(); @ManyToOne(fetch = FetchType.LAZY) @JsonIgnoreProperties(value = {"scenarioParameters", "scenarioActions", "scenarioMessages"}, allowSetters = true) @@ -102,6 +104,10 @@ public String getCitrusMessageId() { return citrusMessageId; } + public Set getHeaders() { + return headers; + } + public void addHeader(MessageHeader messageHeader) { headers.add(messageHeader); messageHeader.setMessage(this); @@ -112,10 +118,6 @@ public void removeHeader(MessageHeader messageHeader) { messageHeader.setMessage(null); } - public Collection getHeaders() { - return headers; - } - public ScenarioExecution getScenarioExecution() { return scenarioExecution; } diff --git a/simulator-starter/src/main/java/org/citrusframework/simulator/model/ScenarioExecution.java b/simulator-starter/src/main/java/org/citrusframework/simulator/model/ScenarioExecution.java index 3c7a76376..6e5d62e02 100644 --- a/simulator-starter/src/main/java/org/citrusframework/simulator/model/ScenarioExecution.java +++ b/simulator-starter/src/main/java/org/citrusframework/simulator/model/ScenarioExecution.java @@ -37,7 +37,9 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.HashSet; import java.util.List; +import java.util.Set; /** * JPA entity for representing a scenario execution @@ -76,18 +78,18 @@ public class ScenarioExecution implements Serializable { private String errorMessage; @OrderBy("name ASC") - @OneToMany(mappedBy = "scenarioExecution", cascade = CascadeType.ALL, orphanRemoval = true) + @OneToMany(fetch = FetchType.LAZY, mappedBy = "scenarioExecution", cascade = CascadeType.ALL, orphanRemoval = true) @JsonIgnoreProperties(value = {"scenarioExecution"}, allowSetters = true) - private List scenarioParameters = new ArrayList<>(); + private final Set scenarioParameters = new HashSet<>(); @OrderBy("actionId ASC") - @OneToMany(mappedBy = "scenarioExecution", cascade = CascadeType.ALL, orphanRemoval = true) - private List scenarioActions = new ArrayList<>(); + @OneToMany(fetch = FetchType.LAZY, mappedBy = "scenarioExecution", cascade = CascadeType.ALL, orphanRemoval = true) + private final Set scenarioActions = new HashSet<>(); @OrderBy("messageId ASC") @OneToMany(fetch = FetchType.LAZY, mappedBy = "scenarioExecution", cascade = CascadeType.ALL, orphanRemoval = true) @JsonIgnoreProperties(value = {"headers", "scenarioExecution"}, allowSetters = true) - private List scenarioMessages = new ArrayList<>(); + private final Set scenarioMessages = new HashSet<>(); public static ScenarioExecutionBuilder builder() { return new ScenarioExecutionBuilder(); @@ -149,7 +151,7 @@ public void setErrorMessage(String errorMessage) throws ErrorMessageTruncationEx } } - public Collection getScenarioParameters() { + public Set getScenarioParameters() { return scenarioParameters; } @@ -163,7 +165,7 @@ public void removeScenarioParameter(ScenarioParameter scenarioParameter) { scenarioParameter.setScenarioExecution(null); } - public Collection getScenarioActions() { + public Set getScenarioActions() { return scenarioActions; } @@ -177,7 +179,7 @@ public void removeScenarioAction(ScenarioAction scenarioAction) { scenarioAction.setScenarioExecution(null); } - public Collection getScenarioMessages() { + public Set getScenarioMessages() { return scenarioMessages; } diff --git a/simulator-starter/src/main/java/org/citrusframework/simulator/model/TestResult.java b/simulator-starter/src/main/java/org/citrusframework/simulator/model/TestResult.java index 96ad871ac..7b86e329c 100644 --- a/simulator-starter/src/main/java/org/citrusframework/simulator/model/TestResult.java +++ b/simulator-starter/src/main/java/org/citrusframework/simulator/model/TestResult.java @@ -160,6 +160,11 @@ public void addTestParameter(TestParameter testParameter) { testParameters.add(testParameter); } + public void removeTestParameter(TestParameter testParameter) { + testParameters.remove(testParameter); + testParameter.setTestResult(null); + } + public String getErrorMessage() { return errorMessage; } diff --git a/simulator-starter/src/main/java/org/citrusframework/simulator/repository/ScenarioExecutionRepository.java b/simulator-starter/src/main/java/org/citrusframework/simulator/repository/ScenarioExecutionRepository.java index e072da415..fe1cafe14 100644 --- a/simulator-starter/src/main/java/org/citrusframework/simulator/repository/ScenarioExecutionRepository.java +++ b/simulator-starter/src/main/java/org/citrusframework/simulator/repository/ScenarioExecutionRepository.java @@ -1,12 +1,32 @@ package org.citrusframework.simulator.repository; import org.citrusframework.simulator.model.ScenarioExecution; +import org.citrusframework.simulator.model.TestResult; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.jpa.domain.Specification; +import org.springframework.data.jpa.repository.EntityGraph; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.stereotype.Repository; +import java.util.Optional; + /** * Spring Data JPA repository for the {@link ScenarioExecution} entity. */ @Repository -public interface ScenarioExecutionRepository extends JpaRepository, JpaSpecificationExecutor {} +public interface ScenarioExecutionRepository extends JpaRepository, JpaSpecificationExecutor { + + @Override + @EntityGraph(attributePaths = {"scenarioParameters", "scenarioActions", "scenarioMessages"}) + Page findAll(Pageable pageable); + + @Override + @EntityGraph(attributePaths = {"scenarioParameters", "scenarioActions", "scenarioMessages"}) + Page findAll(Specification spec, Pageable pageable); + + @Override + @EntityGraph(attributePaths = {"scenarioParameters", "scenarioActions", "scenarioMessages"}) + Optional findById(Long id); +} diff --git a/simulator-starter/src/main/java/org/citrusframework/simulator/service/criteria/ScenarioExecutionCriteria.java b/simulator-starter/src/main/java/org/citrusframework/simulator/service/criteria/ScenarioExecutionCriteria.java index 09729c347..cde8ad49a 100644 --- a/simulator-starter/src/main/java/org/citrusframework/simulator/service/criteria/ScenarioExecutionCriteria.java +++ b/simulator-starter/src/main/java/org/citrusframework/simulator/service/criteria/ScenarioExecutionCriteria.java @@ -188,7 +188,7 @@ public void setScenarioMessagesId(LongFilter scenarioMessagesId) { this.scenarioMessagesId = scenarioMessagesId; } - public LongFilter getScenarioParametersId() { + public LongFilter getScenarioParametersId() { return scenarioParametersId; } diff --git a/simulator-starter/src/test/java/org/citrusframework/simulator/service/criteria/ScenarioExecutionCriteriaTest.java b/simulator-starter/src/test/java/org/citrusframework/simulator/service/criteria/ScenarioExecutionCriteriaTest.java new file mode 100644 index 000000000..5b21559a6 --- /dev/null +++ b/simulator-starter/src/test/java/org/citrusframework/simulator/service/criteria/ScenarioExecutionCriteriaTest.java @@ -0,0 +1,147 @@ +package org.citrusframework.simulator.service.criteria; + +import org.citrusframework.simulator.service.filter.InstantFilter; +import org.citrusframework.simulator.service.filter.IntegerFilter; +import org.citrusframework.simulator.service.filter.LongFilter; +import org.citrusframework.simulator.service.filter.StringFilter; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; + +class ScenarioExecutionCriteriaTest { + + private ScenarioExecutionCriteria fixture; + + @BeforeEach + void beforeEachSetup() { + fixture = new ScenarioExecutionCriteria(); + } + + @Test + void testExecutionId() { + assertNull(fixture.getExecutionId()); + + LongFilter executionIdFilter = fixture.id(); + assertNotNull(executionIdFilter); + assertSame(executionIdFilter, fixture.getExecutionId()); + + LongFilter mockExecutionIdFilter = mock(LongFilter.class); + fixture.setExecutionId(mockExecutionIdFilter); + assertSame(mockExecutionIdFilter, fixture.id()); + } + + @Test + void testStartDate() { + assertNull(fixture.getStartDate()); + + InstantFilter startDateFilter = fixture.startDate(); + assertNotNull(startDateFilter); + assertSame(startDateFilter, fixture.getStartDate()); + + InstantFilter mockStartDateFilter = mock(InstantFilter.class); + fixture.setStartDate(mockStartDateFilter); + assertSame(mockStartDateFilter, fixture.startDate()); + } + + // Add more test methods following the same pattern for all the other attributes... + + @Test + void testScenarioName() { + assertNull(fixture.getScenarioName()); + + StringFilter scenarioNameFilter = fixture.scenarioName(); + assertNotNull(scenarioNameFilter); + assertSame(scenarioNameFilter, fixture.getScenarioName()); + + StringFilter mockScenarioNameFilter = mock(StringFilter.class); + fixture.setScenarioName(mockScenarioNameFilter); + assertSame(mockScenarioNameFilter, fixture.scenarioName()); + } + + @Test + void testStatus() { + assertNull(fixture.getStatus()); + + IntegerFilter statusFilter = fixture.status(); + assertNotNull(statusFilter); + assertSame(statusFilter, fixture.getStatus()); + + IntegerFilter mockStatusFilter = mock(IntegerFilter.class); + fixture.setStatus(mockStatusFilter); + assertSame(mockStatusFilter, fixture.status()); + } + + @Test + void testErrorMessage() { + assertNull(fixture.getErrorMessage()); + + StringFilter errorMessageFilter = fixture.errorMessage(); + assertNotNull(errorMessageFilter); + assertSame(errorMessageFilter, fixture.getErrorMessage()); + + StringFilter mockErrorMessageFilter = mock(StringFilter.class); + fixture.setErrorMessage(mockErrorMessageFilter); + assertSame(mockErrorMessageFilter, fixture.errorMessage()); + } + + @Test + void testScenarioActionsId() { + assertNull(fixture.getScenarioActionsId()); + + LongFilter scenarioActionsIdFilter = fixture.scenarioActionsId(); + assertNotNull(scenarioActionsIdFilter); + assertSame(scenarioActionsIdFilter, fixture.getScenarioActionsId()); + + LongFilter mockScenarioActionsIdFilter = mock(LongFilter.class); + fixture.setScenarioActionsId(mockScenarioActionsIdFilter); + assertSame(mockScenarioActionsIdFilter, fixture.scenarioActionsId()); + } + + @Test + void testScenarioMessagesId() { + assertNull(fixture.getScenarioMessagesId()); + + LongFilter scenarioMessagesIdFilter = fixture.scenarioMessagesId(); + assertNotNull(scenarioMessagesIdFilter); + assertSame(scenarioMessagesIdFilter, fixture.getScenarioMessagesId()); + + LongFilter mockScenarioMessagesIdFilter = mock(LongFilter.class); + fixture.setScenarioMessagesId(mockScenarioMessagesIdFilter); + assertSame(mockScenarioMessagesIdFilter, fixture.scenarioMessagesId()); + } + + @Test + void testScenarioParametersId() { + assertNull(fixture.getScenarioParametersId()); + + LongFilter scenarioParametersIdFilter = fixture.scenarioParametersId(); + assertNotNull(scenarioParametersIdFilter); + assertSame(scenarioParametersIdFilter, fixture.getScenarioParametersId()); + + LongFilter mockScenarioParametersIdFilter = mock(LongFilter.class); + fixture.setScenarioParametersId(mockScenarioParametersIdFilter); + assertSame(mockScenarioParametersIdFilter, fixture.scenarioParametersId()); + } + + @Test + void testDistinct() { + assertNull(fixture.getDistinct()); + + fixture.setDistinct(true); + assertTrue(fixture.getDistinct()); + } + + @Test + void testCopy() { + ScenarioExecutionCriteria copiedCriteria = fixture.copy(); + assertNotSame(copiedCriteria, fixture); + assertEquals(copiedCriteria, fixture); + } +} diff --git a/simulator-starter/src/test/java/org/citrusframework/simulator/service/impl/ScenarioExecutionServiceImplTest.java b/simulator-starter/src/test/java/org/citrusframework/simulator/service/impl/ScenarioExecutionServiceImplTest.java new file mode 100644 index 000000000..efaacfb53 --- /dev/null +++ b/simulator-starter/src/test/java/org/citrusframework/simulator/service/impl/ScenarioExecutionServiceImplTest.java @@ -0,0 +1,69 @@ +package org.citrusframework.simulator.service.impl; + +import org.citrusframework.simulator.model.ScenarioExecution; +import org.citrusframework.simulator.repository.ScenarioExecutionRepository; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.Pageable; + +import java.util.List; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +class ScenarioExecutionServiceImplTest { + + @Mock + private ScenarioExecutionRepository scenarioExecutionRepositoryMock; + + private ScenarioExecution sampleScenarioExecution; + + private ScenarioExecutionServiceImpl fixture; + + @BeforeEach + void beforeEachSetup() { + sampleScenarioExecution = new ScenarioExecution(); + + fixture = new ScenarioExecutionServiceImpl(scenarioExecutionRepositoryMock); + } + + @Test + void testSave() { + when(scenarioExecutionRepositoryMock.save(sampleScenarioExecution)).thenReturn(sampleScenarioExecution); + + ScenarioExecution savedScenarioExecution = fixture.save(sampleScenarioExecution); + assertEquals(sampleScenarioExecution, savedScenarioExecution); + } + + @Test + void testFindAll() { + Pageable pageable = Pageable.unpaged(); + Page page = new PageImpl<>(List.of(sampleScenarioExecution)); + + when(scenarioExecutionRepositoryMock.findAll(pageable)).thenReturn(page); + + Page result = fixture.findAll(pageable); + + assertEquals(page, result); + } + + @Test + void testFindOne() { + Long scenarioExecutionId = 1L; + + when(scenarioExecutionRepositoryMock.findById(scenarioExecutionId)).thenReturn(Optional.of(sampleScenarioExecution)); + + Optional maybeScenarioExecution = fixture.findOne(scenarioExecutionId); + + assertTrue(maybeScenarioExecution.isPresent()); + assertEquals(sampleScenarioExecution, maybeScenarioExecution.get()); + } +} diff --git a/simulator-starter/src/test/java/org/citrusframework/simulator/web/rest/ScenarioExecutionResourceIT.java b/simulator-starter/src/test/java/org/citrusframework/simulator/web/rest/ScenarioExecutionResourceIT.java index 182e57201..595d3f5f9 100644 --- a/simulator-starter/src/test/java/org/citrusframework/simulator/web/rest/ScenarioExecutionResourceIT.java +++ b/simulator-starter/src/test/java/org/citrusframework/simulator/web/rest/ScenarioExecutionResourceIT.java @@ -5,6 +5,7 @@ import org.citrusframework.simulator.model.Message; import org.citrusframework.simulator.model.ScenarioAction; import org.citrusframework.simulator.model.ScenarioExecution; +import org.citrusframework.simulator.model.ScenarioParameter; import org.citrusframework.simulator.repository.ScenarioExecutionRepository; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -455,6 +456,28 @@ void getAllScenarioExecutionsByErrorMessageNotContainsSomething() throws Excepti defaultScenarioExecutionShouldBeFound("errorMessage.doesNotContain=" + UPDATED_ERROR_MESSAGE); } + @Test + @Transactional + void getAllScenarioExecutionsByScenarioActionIsEqualToSomething() throws Exception { + ScenarioAction scenarioAction; + if (TestUtil.findAll(entityManager, ScenarioAction.class).isEmpty()) { + scenarioExecutionRepository.saveAndFlush(scenarioExecution); + scenarioAction = ScenarioActionResourceIT.createEntity(entityManager); + } else { + scenarioAction = TestUtil.findAll(entityManager, ScenarioAction.class).get(0); + } + entityManager.persist(scenarioAction); + entityManager.flush(); + scenarioExecution.addScenarioAction(scenarioAction); + scenarioExecutionRepository.saveAndFlush(scenarioExecution); + Long scenarioActionId = scenarioAction.getActionId(); + // Get all the scenarioExecutionList where scenarioAction equals to scenarioActionId + defaultScenarioExecutionShouldBeFound("scenarioActionsId.equals=" + scenarioActionId); + + // Get all the scenarioExecutionList where scenarioAction equals to (scenarioActionId + 1) + defaultScenarioExecutionShouldNotBeFound("scenarioActionsId.equals=" + (scenarioActionId + 1)); + } + @Test @Transactional void getAllScenarioExecutionsByScenarioMessagesIsEqualToSomething() throws Exception { @@ -479,24 +502,24 @@ void getAllScenarioExecutionsByScenarioMessagesIsEqualToSomething() throws Excep @Test @Transactional - void getAllScenarioExecutionsByScenarioActionIsEqualToSomething() throws Exception { - ScenarioAction scenarioAction; - if (TestUtil.findAll(entityManager, ScenarioAction.class).isEmpty()) { + void getAllScenarioExecutionsByScenarioParametersIsEqualToSomething() throws Exception { + ScenarioParameter scenarioParameters; + if (TestUtil.findAll(entityManager, ScenarioParameter.class).isEmpty()) { scenarioExecutionRepository.saveAndFlush(scenarioExecution); - scenarioAction = ScenarioActionResourceIT.createEntity(entityManager); + scenarioParameters = ScenarioParameterResourceIT.createEntity(entityManager); } else { - scenarioAction = TestUtil.findAll(entityManager, ScenarioAction.class).get(0); + scenarioParameters = TestUtil.findAll(entityManager, ScenarioParameter.class).get(0); } - entityManager.persist(scenarioAction); + entityManager.persist(scenarioParameters); entityManager.flush(); - scenarioExecution.addScenarioAction(scenarioAction); + scenarioExecution.addScenarioParameter(scenarioParameters); scenarioExecutionRepository.saveAndFlush(scenarioExecution); - Long scenarioActionId = scenarioAction.getActionId(); - // Get all the scenarioExecutionList where scenarioAction equals to scenarioActionId - defaultScenarioExecutionShouldBeFound("scenarioActionsId.equals=" + scenarioActionId); + Long scenarioParametersId = scenarioParameters.getParameterId(); + // Get all the scenarioExecutionList where scenarioParameters equals to scenarioParametersId + defaultScenarioExecutionShouldBeFound("scenarioParametersId.equals=" + scenarioParametersId); - // Get all the scenarioExecutionList where scenarioAction equals to (scenarioActionId + 1) - defaultScenarioExecutionShouldNotBeFound("scenarioActionsId.equals=" + (scenarioActionId + 1)); + // Get all the scenarioExecutionList where scenarioParameters equals to (scenarioParametersId + 1) + defaultScenarioExecutionShouldNotBeFound("scenarioParametersId.equals=" + (scenarioParametersId + 1)); } /**