Skip to content

Commit

Permalink
feat: scenario execution read-only rest api
Browse files Browse the repository at this point in the history
  • Loading branch information
bbortt committed Oct 21, 2023
1 parent 8a3e80a commit 1b9fde0
Show file tree
Hide file tree
Showing 63 changed files with 2,919 additions and 248 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import lombok.Data;
import lombok.NoArgsConstructor;
import org.citrusframework.simulator.model.ScenarioParameter;
import org.citrusframework.simulator.service.ScenarioExecutionService;
import org.citrusframework.simulator.service.ScenarioExecutorService;
import org.citrusframework.simulator.service.ScenarioLookupService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
Expand All @@ -37,12 +37,12 @@
@RequestMapping("api/scenario")
public class ScenarioController {

private final ScenarioExecutionService scenarioExecutionService;
private final ScenarioExecutorService scenarioExecutorService;
private final ScenarioLookupService scenarioLookupService;
private final List<Scenario> scenarios;

public ScenarioController(ScenarioExecutionService scenarioExecutionService, ScenarioLookupService scenarioLookupService) {
this.scenarioExecutionService = scenarioExecutionService;
public ScenarioController(ScenarioExecutorService scenarioExecutorService, ScenarioLookupService scenarioLookupService) {
this.scenarioExecutorService = scenarioExecutorService;
this.scenarioLookupService = scenarioLookupService;
this.scenarios = getScenarioList(scenarioLookupService);
}
Expand Down Expand Up @@ -93,7 +93,7 @@ public Collection<ScenarioParameter> getScenarioParameters(@PathVariable("name")
public Long launchScenario(
@PathVariable("name") String name,
@RequestBody(required = false) List<ScenarioParameter> scenarioParameters) {
return scenarioExecutionService.run(name, scenarioParameters);
return scenarioExecutorService.run(name, scenarioParameters);
}

public record Scenario(String name, ScenarioController.Scenario.ScenarioType type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import org.citrusframework.simulator.correlation.CorrelationHandler;
import org.citrusframework.simulator.correlation.CorrelationHandlerRegistry;
import org.citrusframework.simulator.exception.SimulatorException;
import org.citrusframework.simulator.service.ScenarioExecutorService;
import org.citrusframework.simulator.scenario.SimulatorScenario;
import org.citrusframework.simulator.service.ScenarioExecutionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -51,7 +51,7 @@ public class SimulatorEndpointAdapter extends RequestDispatchingEndpointAdapter
private SimulatorConfigurationProperties configuration;

@Autowired
private ScenarioExecutionService scenarioExecutionService;
private ScenarioExecutorService scenarioExecutorService;

/**
* Spring application context
Expand Down Expand Up @@ -106,7 +106,7 @@ public Message dispatchMessage(Message request, String mappingName) {

scenario.getScenarioEndpoint().setName(scenarioName);
scenario.getScenarioEndpoint().add(request, responseFuture);
scenarioExecutionService.run(scenario, scenarioName, Collections.emptyList());
scenarioExecutorService.run(scenario, scenarioName, Collections.emptyList());

try {
if (handleResponse) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
Expand All @@ -28,6 +29,7 @@
import jakarta.persistence.OneToMany;
import jakarta.persistence.OrderBy;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import org.apache.commons.lang3.StringUtils;

import java.io.Serial;
Expand Down Expand Up @@ -67,10 +69,11 @@ public class Message extends AbstractAuditingEntity<Message, Long> implements Se
private String citrusMessageId;

@OrderBy("name ASC")
@OneToMany(mappedBy = "message", cascade = CascadeType.ALL, orphanRemoval = true)
@JsonIgnoreProperties(value = { "message" }, allowSetters = true)
@OneToMany(mappedBy = "message", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
private Collection<MessageHeader> headers = new ArrayList<>();

@ManyToOne
@ManyToOne(fetch = FetchType.EAGER)
@JsonIgnoreProperties(value = {"scenarioParameters", "scenarioActions", "scenarioMessages"}, allowSetters = true)
private ScenarioExecution scenarioExecution;

Expand Down Expand Up @@ -196,6 +199,10 @@ public static class MessageBuilder extends AuditingEntityBuilder<MessageBuilder,

private final Message message = new Message();

private MessageBuilder() {
// Static access through entity
}

@Override
protected Message getEntity() {
return message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
Expand Down Expand Up @@ -135,6 +136,10 @@ public static class MessageHeaderBuilder extends AuditingEntityBuilder<MessageHe

private final MessageHeader messageHeader = new MessageHeader();

private MessageHeaderBuilder() {
// Static access through entity
}

public MessageHeaderBuilder name(String name) {
messageHeader.setName(name);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
Expand All @@ -34,6 +35,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import org.springframework.util.StringUtils;

/**
Expand Down Expand Up @@ -81,10 +83,14 @@ public class ScenarioExecution implements Serializable {
private List<ScenarioAction> scenarioActions = new ArrayList<>();

@OrderBy("messageId ASC")
@OneToMany(mappedBy = "scenarioExecution", cascade = CascadeType.ALL, orphanRemoval = true)
@OneToMany(mappedBy = "scenarioExecution", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
@JsonIgnoreProperties(value = { "headers", "scenarioExecution" }, allowSetters = true)
private List<Message> scenarioMessages = new ArrayList<>();

public static ScenarioExecutionBuilder builder() {
return new ScenarioExecutionBuilder();
}

public Long getExecutionId() {
return executionId;
}
Expand Down Expand Up @@ -222,4 +228,41 @@ public ErrorMessageTruncationException(String errorMessage, Exception exception)
super(errorMessage, exception);
}
}

public static class ScenarioExecutionBuilder {

private final ScenarioExecution scenarioExecution = new ScenarioExecution();

private ScenarioExecutionBuilder(){
// Static access through entity
}

public ScenarioExecution build(){
return scenarioExecution;
}

public ScenarioExecutionBuilder startDate(Instant startDate) {
scenarioExecution.setStartDate(startDate);
return this;
}
public ScenarioExecutionBuilder endDate(Instant endDate) {
scenarioExecution.setEndDate(endDate);
return this;
}

public ScenarioExecutionBuilder scenarioName(String scenarioName) {
scenarioExecution.setScenarioName(scenarioName);
return this;
}

public ScenarioExecutionBuilder status(Status status) {
scenarioExecution.setStatus(status);
return this;
}

public ScenarioExecutionBuilder errorMessage(String errorMessage) throws ErrorMessageTruncationException {
scenarioExecution.setErrorMessage(errorMessage);
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ public static class TestParameterBuilder extends AuditingEntityBuilder<TestParam

private final TestParameter testParameter = new TestParameter();

private TestParameterBuilder(){
// Static access through entity
}

public TestParameterBuilder key(String key) {
if (Objects.isNull(testParameter.testParameterId)) {
testParameter.testParameterId = new TestParameterId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.Size;

import java.io.Serial;
import java.io.Serializable;
Expand Down Expand Up @@ -85,7 +86,8 @@ public class TestResult extends AbstractAuditingEntity<TestResult, Long> impleme
/**
* Error message
*/
@Column(updatable = false)
@Size(max = 1000)
@Column(length = 1000, updatable = false)
private String errorMessage;

/**
Expand Down Expand Up @@ -233,6 +235,10 @@ public static class TestResultBuilder extends AuditingEntityBuilder<TestResultBu

private final TestResult testResult = new TestResult();

private TestResultBuilder(){
// Static access through entity
}

public TestResultBuilder id(Long id) {
testResult.id = id;
return this;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,44 @@
package org.citrusframework.simulator.repository;

import org.citrusframework.simulator.model.Message;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

/**
* Spring Data JPA repository for the {@link Message} entity.
*/
@Repository
public interface MessageRepository extends JpaRepository<Message, Long>, JpaSpecificationExecutor<Message> {}
public interface MessageRepository extends JpaRepository<Message, Long>, JpaSpecificationExecutor<Message> {

default Optional<Message> findOneWithEagerRelationships(Long id) {
return this.findOneWithToOneRelationships(id);
}

default List<Message> findAllWithEagerRelationships() {
return this.findAllWithToOneRelationships();
}

default Page<Message> findAllWithEagerRelationships(Pageable pageable) {
return this.findAllWithToOneRelationships(pageable);
}

@Query(
value = "select message from Message message left join fetch message.scenarioExecution",
countQuery = "select count(message) from Message message"
)
Page<Message> findAllWithToOneRelationships(Pageable pageable);

@Query("select message from Message message left join fetch message.scenarioExecution")
List<Message> findAllWithToOneRelationships();

@Query("select message from Message message left join fetch message.scenarioExecution where message.id =:id")
Optional<Message> findOneWithToOneRelationships(@Param("id") Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.springframework.stereotype.Repository;

/**
* Spring Data JPA repository for the {@link ScenarioExecution} entity.
* Spring Data JPA repository for the ScenarioExecution entity.
*/
@Repository
public interface ScenarioExecutionRepository extends JpaRepository<ScenarioExecution, Long>, JpaSpecificationExecutor<ScenarioExecution> {}
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,6 @@ public interface MessageHeaderService {
*/
MessageHeader save(MessageHeader messageHeader);

/**
* Updates a messageHeader.
*
* @param messageHeader the entity to update.
* @return the persisted entity.
*/
MessageHeader update(MessageHeader messageHeader);

/**
* Get all the messageHeaders.
*
Expand All @@ -45,15 +37,8 @@ public interface MessageHeaderService {
/**
* Get the "id" messageHeader.
*
* @param id the id of the entity.
* @param headerId the id of the entity.
* @return the entity.
*/
Optional<MessageHeader> findOne(Long id);

/**
* Delete the "id" messageHeader.
*
* @param id the id of the entity.
*/
void delete(Long id);
Optional<MessageHeader> findOne(Long headerId);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package org.citrusframework.simulator.service;

import jakarta.persistence.criteria.JoinType;
import org.citrusframework.simulator.model.Message;
import org.citrusframework.simulator.model.MessageHeader_;
import org.citrusframework.simulator.model.Message_;
import org.citrusframework.simulator.model.ScenarioExecution_;
import org.citrusframework.simulator.repository.MessageRepository;
import org.citrusframework.simulator.service.criteria.MessageCriteria;
import org.slf4j.Logger;
Expand Down Expand Up @@ -87,6 +90,24 @@ protected Specification<Message> createSpecification(MessageCriteria criteria) {
if (criteria.getLastModifiedDate() != null) {
specification = specification.and(buildRangeSpecification(criteria.getLastModifiedDate(), Message_.lastModifiedDate));
}
if (criteria.getHeadersId() != null) {
specification =
specification.and(
buildSpecification(
criteria.getHeadersId(),
root -> root.join(Message_.headers, JoinType.LEFT).get(MessageHeader_.headerId)
)
);
}
if (criteria.getScenarioExecutionId() != null) {
specification =
specification.and(
buildSpecification(
criteria.getScenarioExecutionId(),
root -> root.join(Message_.scenarioExecution, JoinType.LEFT).get(ScenarioExecution_.executionId)
)
);
}
}
return specification;
}
Expand Down
Loading

0 comments on commit 1b9fde0

Please sign in to comment.