Skip to content

Commit

Permalink
fix: scenario execution rest api
Browse files Browse the repository at this point in the history
  • Loading branch information
bbortt committed Oct 29, 2023
1 parent ff1f252 commit 31149dd
Show file tree
Hide file tree
Showing 8 changed files with 295 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -72,7 +74,7 @@ public class Message extends AbstractAuditingEntity<Message, Long> implements Se
@OrderBy("name ASC")
@JsonIgnoreProperties(value = { "message" }, allowSetters = true)
@OneToMany(fetch = FetchType.LAZY, mappedBy = "message", cascade = CascadeType.ALL, orphanRemoval = true)
private Collection<MessageHeader> headers = new ArrayList<>();
private final Set<MessageHeader> headers = new HashSet<>();

@ManyToOne(fetch = FetchType.LAZY)
@JsonIgnoreProperties(value = {"scenarioParameters", "scenarioActions", "scenarioMessages"}, allowSetters = true)
Expand Down Expand Up @@ -102,6 +104,10 @@ public String getCitrusMessageId() {
return citrusMessageId;
}

public Set<MessageHeader> getHeaders() {
return headers;
}

public void addHeader(MessageHeader messageHeader) {
headers.add(messageHeader);
messageHeader.setMessage(this);
Expand All @@ -112,10 +118,6 @@ public void removeHeader(MessageHeader messageHeader) {
messageHeader.setMessage(null);
}

public Collection<MessageHeader> getHeaders() {
return headers;
}

public ScenarioExecution getScenarioExecution() {
return scenarioExecution;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<ScenarioParameter> scenarioParameters = new ArrayList<>();
private final Set<ScenarioParameter> scenarioParameters = new HashSet<>();

@OrderBy("actionId ASC")
@OneToMany(mappedBy = "scenarioExecution", cascade = CascadeType.ALL, orphanRemoval = true)
private List<ScenarioAction> scenarioActions = new ArrayList<>();
@OneToMany(fetch = FetchType.LAZY, mappedBy = "scenarioExecution", cascade = CascadeType.ALL, orphanRemoval = true)
private final Set<ScenarioAction> 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<Message> scenarioMessages = new ArrayList<>();
private final Set<Message> scenarioMessages = new HashSet<>();

public static ScenarioExecutionBuilder builder() {
return new ScenarioExecutionBuilder();
Expand Down Expand Up @@ -149,7 +151,7 @@ public void setErrorMessage(String errorMessage) throws ErrorMessageTruncationEx
}
}

public Collection<ScenarioParameter> getScenarioParameters() {
public Set<ScenarioParameter> getScenarioParameters() {
return scenarioParameters;
}

Expand All @@ -163,7 +165,7 @@ public void removeScenarioParameter(ScenarioParameter scenarioParameter) {
scenarioParameter.setScenarioExecution(null);
}

public Collection<ScenarioAction> getScenarioActions() {
public Set<ScenarioAction> getScenarioActions() {
return scenarioActions;
}

Expand All @@ -177,7 +179,7 @@ public void removeScenarioAction(ScenarioAction scenarioAction) {
scenarioAction.setScenarioExecution(null);
}

public Collection<Message> getScenarioMessages() {
public Set<Message> getScenarioMessages() {
return scenarioMessages;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ScenarioExecution, Long>, JpaSpecificationExecutor<ScenarioExecution> {}
public interface ScenarioExecutionRepository extends JpaRepository<ScenarioExecution, Long>, JpaSpecificationExecutor<ScenarioExecution> {

@Override
@EntityGraph(attributePaths = {"scenarioParameters", "scenarioActions", "scenarioMessages"})
Page<ScenarioExecution> findAll(Pageable pageable);

@Override
@EntityGraph(attributePaths = {"scenarioParameters", "scenarioActions", "scenarioMessages"})
Page<ScenarioExecution> findAll(Specification<ScenarioExecution> spec, Pageable pageable);

@Override
@EntityGraph(attributePaths = {"scenarioParameters", "scenarioActions", "scenarioMessages"})
Optional<ScenarioExecution> findById(Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public void setScenarioMessagesId(LongFilter scenarioMessagesId) {
this.scenarioMessagesId = scenarioMessagesId;
}

public LongFilter getScenarioParametersId() {
public LongFilter getScenarioParametersId() {
return scenarioParametersId;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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<ScenarioExecution> page = new PageImpl<>(List.of(sampleScenarioExecution));

when(scenarioExecutionRepositoryMock.findAll(pageable)).thenReturn(page);

Page<ScenarioExecution> result = fixture.findAll(pageable);

assertEquals(page, result);
}

@Test
void testFindOne() {
Long scenarioExecutionId = 1L;

when(scenarioExecutionRepositoryMock.findById(scenarioExecutionId)).thenReturn(Optional.of(sampleScenarioExecution));

Optional<ScenarioExecution> maybeScenarioExecution = fixture.findOne(scenarioExecutionId);

assertTrue(maybeScenarioExecution.isPresent());
assertEquals(sampleScenarioExecution, maybeScenarioExecution.get());
}
}
Loading

0 comments on commit 31149dd

Please sign in to comment.