Skip to content

Commit

Permalink
feat: scenario action api
Browse files Browse the repository at this point in the history
  • Loading branch information
bbortt committed Oct 29, 2023
1 parent 4b1fe34 commit a32c47f
Show file tree
Hide file tree
Showing 42 changed files with 2,309 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ public Collection<MessageHeader> getHeaders() {
return headers;
}

public ScenarioExecution getScenarioExecution() {
return scenarioExecution;
}

public void setScenarioExecution(ScenarioExecution scenarioExecution) {
this.scenarioExecution = scenarioExecution;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne;
import jakarta.validation.constraints.NotEmpty;
import org.citrusframework.simulator.service.ScenarioActionQueryService;

import java.io.Serial;
import java.io.Serializable;
import java.time.Instant;
Expand Down Expand Up @@ -55,6 +57,10 @@ public class ScenarioAction implements Serializable {
@JsonIgnoreProperties(value = { "scenarioParameters", "scenarioActions", "scenarioMessages" }, allowSetters = true)
private ScenarioExecution scenarioExecution;

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

public Long getActionId() {
return actionId;
}
Expand Down Expand Up @@ -100,4 +106,28 @@ public String toString() {
", endDate='" + getEndDate() + "'" +
"}";
}

public static class ScenarioActionBuilder {

private final ScenarioAction scenarioAction = new ScenarioAction();

public ScenarioActionBuilder name(String name) {
scenarioAction.name = name;
return this;
}

public ScenarioActionBuilder startDate(Instant startDate) {
scenarioAction.startDate = startDate;
return this;
}

public ScenarioActionBuilder endDate(Instant endDate) {
scenarioAction.endDate = endDate;
return this;
}

public ScenarioAction build(){
return scenarioAction;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public class ScenarioExecution implements Serializable {
* Actual status as a numerical representation of {@link Status}
*/
@Column(nullable = false)
private Integer status;
private Integer status = Status.UNKNOWN.getId();

@Size(max = 1000)
@Column(length = 1000)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.citrusframework.simulator.repository;

import org.citrusframework.simulator.model.ScenarioAction;
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 ScenarioAction entity.
*/
@Repository
public interface ScenarioActionRepository extends JpaRepository<ScenarioAction, Long>, JpaSpecificationExecutor<ScenarioAction> {
default Optional<ScenarioAction> findOneWithEagerRelationships(Long id) {
return this.findOneWithToOneRelationships(id);
}

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

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

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

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

@Query(
"select scenarioAction from ScenarioAction scenarioAction left join fetch scenarioAction.scenarioExecution where scenarioAction.actionId =:actionId"
)
Optional<ScenarioAction> findOneWithToOneRelationships(@Param("actionId") Long actionId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package org.citrusframework.simulator.service;

import jakarta.persistence.criteria.JoinType;
import org.citrusframework.simulator.model.ScenarioAction;
import org.citrusframework.simulator.model.ScenarioAction_;
import org.citrusframework.simulator.model.ScenarioExecution_;
import org.citrusframework.simulator.repository.ScenarioActionRepository;
import org.citrusframework.simulator.service.criteria.ScenarioActionCriteria;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* Service for executing complex queries for {@link ScenarioAction} entities in the database.
* The main input is a {@link ScenarioActionCriteria} which gets converted to {@link Specification},
* in a way that all the filters must apply.
* It returns a {@link List} of {@link ScenarioAction} or a {@link Page} of {@link ScenarioAction} which fulfills the criteria.
*/
@Service
@Transactional(readOnly = true)
public class ScenarioActionQueryService extends QueryService<ScenarioAction> {

private final Logger log = LoggerFactory.getLogger(ScenarioActionQueryService.class);

private final ScenarioActionRepository scenarioActionRepository;

public ScenarioActionQueryService(ScenarioActionRepository scenarioActionRepository) {
this.scenarioActionRepository = scenarioActionRepository;
}

/**
* Return a {@link List} of {@link ScenarioAction} which matches the criteria from the database.
* @param criteria The object which holds all the filters, which the entities should match.
* @return the matching entities.
*/
@Transactional(readOnly = true)
public List<ScenarioAction> findByCriteria(ScenarioActionCriteria criteria) {
log.debug("find by criteria : {}", criteria);
final Specification<ScenarioAction> specification = createSpecification(criteria);
return scenarioActionRepository.findAll(specification);
}

/**
* Return a {@link Page} of {@link ScenarioAction} which matches the criteria from the database.
* @param criteria The object which holds all the filters, which the entities should match.
* @param page The page, which should be returned.
* @return the matching entities.
*/
@Transactional(readOnly = true)
public Page<ScenarioAction> findByCriteria(ScenarioActionCriteria criteria, Pageable page) {
log.debug("find by criteria : {}, page: {}", criteria, page);
final Specification<ScenarioAction> specification = createSpecification(criteria);
return scenarioActionRepository.findAll(specification, page);
}

/**
* Return the number of matching entities in the database.
* @param criteria The object which holds all the filters, which the entities should match.
* @return the number of matching entities.
*/
@Transactional(readOnly = true)
public long countByCriteria(ScenarioActionCriteria criteria) {
log.debug("count by criteria : {}", criteria);
final Specification<ScenarioAction> specification = createSpecification(criteria);
return scenarioActionRepository.count(specification);
}

/**
* Function to convert {@link ScenarioActionCriteria} to a {@link Specification}
* @param criteria The object which holds all the filters, which the entities should match.
* @return the matching {@link Specification} of the entity.
*/
protected Specification<ScenarioAction> createSpecification(ScenarioActionCriteria criteria) {
Specification<ScenarioAction> specification = Specification.where(null);
if (criteria != null) {
// This has to be called first, because the distinct method returns null
if (criteria.getDistinct() != null) {
specification = specification.and(distinct(criteria.getDistinct()));
}
if (criteria.getActionId() != null) {
specification = specification.and(buildRangeSpecification(criteria.getActionId(), ScenarioAction_.actionId));
}
if (criteria.getName() != null) {
specification = specification.and(buildStringSpecification(criteria.getName(), ScenarioAction_.name));
}
if (criteria.getStartDate() != null) {
specification = specification.and(buildRangeSpecification(criteria.getStartDate(), ScenarioAction_.startDate));
}
if (criteria.getEndDate() != null) {
specification = specification.and(buildRangeSpecification(criteria.getEndDate(), ScenarioAction_.endDate));
}
if (criteria.getScenarioExecutionId() != null) {
specification =
specification.and(
buildSpecification(
criteria.getScenarioExecutionId(),
root -> root.join(ScenarioAction_.scenarioExecution, JoinType.LEFT).get(ScenarioExecution_.executionId)
)
);
}
}
return specification;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.citrusframework.simulator.service;

import org.citrusframework.simulator.model.ScenarioAction;
import org.citrusframework.simulator.model.ScenarioExecution;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.Objects;
import java.util.Optional;

/**
* Service Interface for managing {@link ScenarioAction}.
*/
public interface ScenarioActionService {
/**
* Save a scenarioAction.
*
* @param scenarioAction the entity to save.
* @return the persisted entity.
*/
ScenarioAction save(ScenarioAction scenarioAction);

/**
* Get all the scenarioActions.
*
* @param pageable the pagination information.
* @return the list of entities.
*/
Page<ScenarioAction> findAll(Pageable pageable);

/**
* Get the "id" scenarioAction.
*
* @param id the id of the entity.
* @return the entity.
*/
Optional<ScenarioAction> findOne(Long id);

/**
* Function that converts the {@link ScenarioAction} to its "DTO-form": It may only contain the {@code scenarioName}
* of the related {@link ScenarioExecution}, no further attributes. That is especially true for relationships,
* because of a possible {@link org.hibernate.LazyInitializationException}).
*
* @param scenarioAction The entity, which should be returned
* @return the entity with prepared {@link ScenarioExecution}
*/
static ScenarioAction restrictToDtoProperties(ScenarioAction scenarioAction) {
ScenarioExecution scenarioExecution = scenarioAction.getScenarioExecution();
if (!Objects.isNull(scenarioExecution)) {
scenarioAction.setScenarioExecution(ScenarioExecution.builder().scenarioName(scenarioExecution.getScenarioName()).build());
}
return scenarioAction;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import jakarta.persistence.criteria.JoinType;
import org.citrusframework.simulator.model.Message_;
import org.citrusframework.simulator.model.ScenarioAction;
import org.citrusframework.simulator.model.ScenarioAction_;
import org.citrusframework.simulator.model.ScenarioExecution;
import org.citrusframework.simulator.model.ScenarioExecution_;
import org.citrusframework.simulator.repository.ScenarioExecutionRepository;
Expand Down Expand Up @@ -105,6 +107,15 @@ protected Specification<ScenarioExecution> createSpecification(ScenarioExecution
if (criteria.getErrorMessage() != null) {
specification = specification.and(buildStringSpecification(criteria.getErrorMessage(), ScenarioExecution_.errorMessage));
}
if (criteria.getScenarioActionsId() != null) {
specification =
specification.and(
buildSpecification(
criteria.getScenarioActionsId(),
root -> root.join(ScenarioExecution_.scenarioActions, JoinType.LEFT).get(ScenarioAction_.actionId)
)
);
}
if (criteria.getScenarioMessagesId() != null) {
specification =
specification.and(
Expand Down
Loading

0 comments on commit a32c47f

Please sign in to comment.