Initial commit.
This commit is contained in:
33
.gitignore
vendored
Normal file
33
.gitignore
vendored
Normal file
@@ -0,0 +1,33 @@
|
||||
HELP.md
|
||||
target/
|
||||
!.mvn/wrapper/maven-wrapper.jar
|
||||
!**/src/main/**/target/
|
||||
!**/src/test/**/target/
|
||||
|
||||
### STS ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
build/
|
||||
!**/src/main/**/build/
|
||||
!**/src/test/**/build/
|
||||
|
||||
### VS Code ###
|
||||
.vscode/
|
||||
41
pom.xml
Normal file
41
pom.xml
Normal file
@@ -0,0 +1,41 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.5.3</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<groupId>org.takiguchi.workflow-validator</groupId>
|
||||
<artifactId>workflow-validator</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>workflow-validator</name>
|
||||
<description>Demo project for Spring Boot</description>
|
||||
<properties>
|
||||
<java.version>11</java.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,13 @@
|
||||
package org.takiguchi.workflowvalidator.workflowvalidator;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class WorkflowValidatorApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(WorkflowValidatorApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
package org.takiguchi.workflowvalidator.workflowvalidator.model;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.takiguchi.workflowvalidator.workflowvalidator.model.StepType.FINAL;
|
||||
import static org.takiguchi.workflowvalidator.workflowvalidator.model.StepType.INITIAL;
|
||||
|
||||
public class Step {
|
||||
private String name;
|
||||
private StepType type;
|
||||
private List<Step> previousSteps;
|
||||
private List<Step> nextSteps;
|
||||
|
||||
public Step(String name, StepType type, List<Step> previousSteps, List<Step> nextSteps) {
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
this.previousSteps = previousSteps;
|
||||
this.nextSteps = nextSteps;
|
||||
}
|
||||
|
||||
public static Builder oneStep() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public StepType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(StepType type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public List<Step> getPreviousSteps() {
|
||||
return previousSteps;
|
||||
}
|
||||
|
||||
public void setPreviousSteps(List<Step> previousSteps) {
|
||||
this.previousSteps = previousSteps;
|
||||
}
|
||||
|
||||
public List<Step> getNextSteps() {
|
||||
return nextSteps;
|
||||
}
|
||||
|
||||
public void setNextSteps(List<Step> nextSteps) {
|
||||
this.nextSteps = nextSteps;
|
||||
}
|
||||
|
||||
public boolean hasAtLeastOneNextStep() {
|
||||
return !Optional.ofNullable(nextSteps)
|
||||
.map(List::isEmpty)
|
||||
.orElse(false);
|
||||
}
|
||||
|
||||
public boolean hasAtLeastOnePreviousStep() {
|
||||
return !Optional.ofNullable(previousSteps)
|
||||
.map(List::isEmpty)
|
||||
.orElse(false);
|
||||
}
|
||||
|
||||
public boolean isInitialStep() {
|
||||
return type == INITIAL;
|
||||
}
|
||||
|
||||
public boolean isFinalStep() {
|
||||
return type == FINAL;
|
||||
}
|
||||
|
||||
public void addPreviousStep(Step step) {
|
||||
previousSteps.add(step);
|
||||
}
|
||||
|
||||
public void addNextStep(Step step) {
|
||||
nextSteps.add(step);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Step{" + name + '}';
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
private String name;
|
||||
private StepType type;
|
||||
private List<Step> previousSteps = new ArrayList<>();
|
||||
private List<Step> nextSteps = new ArrayList<>();
|
||||
|
||||
public Builder name(String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder type(StepType type) {
|
||||
this.type = type;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder previousSteps(List<Step> previousSteps) {
|
||||
this.previousSteps = previousSteps;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder nextSteps(List<Step> nextSteps) {
|
||||
this.nextSteps = nextSteps;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Step build() {
|
||||
return new Step(name, type, previousSteps, nextSteps);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package org.takiguchi.workflowvalidator.workflowvalidator.model;
|
||||
|
||||
public enum StepType {
|
||||
INITIAL, MIDDLE, FINAL
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package org.takiguchi.workflowvalidator.workflowvalidator.model;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Workflow {
|
||||
private final List<Step> steps;
|
||||
|
||||
public Workflow(List<Step> steps) {
|
||||
this.steps = steps;
|
||||
}
|
||||
|
||||
public List<Step> getSteps() {
|
||||
return steps;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package org.takiguchi.workflowvalidator.workflowvalidator.service;
|
||||
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.takiguchi.workflowvalidator.workflowvalidator.model.Step;
|
||||
import org.takiguchi.workflowvalidator.workflowvalidator.model.Workflow;
|
||||
|
||||
public class WorkflowValidator {
|
||||
public void validate(Workflow workflow) {
|
||||
if (CollectionUtils.isEmpty(workflow.getSteps())) {
|
||||
throw new IllegalStateException("Workflow should have at least one step.");
|
||||
}
|
||||
workflow.getSteps().forEach(this::validateStep);
|
||||
}
|
||||
|
||||
private void validateStep(Step step) {
|
||||
if (step.hasAtLeastOneNextStep()) {
|
||||
if (step.isFinalStep()) {
|
||||
throw new IllegalStateException(String.format("The step %s is final, but it has at least one next step, but it shouldn't.", step));
|
||||
}
|
||||
|
||||
// Non final step, that has at least one next step.
|
||||
validatePreviousStepsOf(step);
|
||||
} else {
|
||||
if (!step.isFinalStep()) {
|
||||
throw new IllegalStateException(String.format("The step %s should have at least one next step.", step));
|
||||
}
|
||||
|
||||
// Final Step, that hasn't any next step.
|
||||
validatePreviousStepsOf(step);
|
||||
}
|
||||
}
|
||||
|
||||
private void validatePreviousStepsOf(Step step) {
|
||||
if (step.hasAtLeastOnePreviousStep()) {
|
||||
if (step.isInitialStep()) {
|
||||
throw new IllegalStateException(String.format("The step %s is initial, but is has at least one previous step, but it shouldn't.", step));
|
||||
}
|
||||
|
||||
// Non initial step, that has at least one previous step.
|
||||
|
||||
if (canPreviousStepsReachAnInitialStep(step)) {
|
||||
// Ok
|
||||
} else {
|
||||
throw new IllegalStateException(String.format("The step %s hasn't any previous step that can reach an initial step.", step));
|
||||
}
|
||||
} else {
|
||||
if (!step.isInitialStep()) {
|
||||
throw new IllegalStateException(String.format("The step %s han't any previous step while it is not an initial step.", step));
|
||||
}
|
||||
|
||||
// Initial step, that hasn't any previous step.
|
||||
}
|
||||
}
|
||||
|
||||
private boolean canPreviousStepsReachAnInitialStep(Step step) {
|
||||
return step.getPreviousSteps().stream()
|
||||
.anyMatch(this::canStepReachAnInitialStep);
|
||||
}
|
||||
|
||||
private boolean canStepReachAnInitialStep(Step step) {
|
||||
boolean result;
|
||||
|
||||
if (step.hasAtLeastOnePreviousStep()) {
|
||||
result = canPreviousStepsReachAnInitialStep(step);
|
||||
} else {
|
||||
result = step.isInitialStep();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
1
src/main/resources/application.properties
Normal file
1
src/main/resources/application.properties
Normal file
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1,147 @@
|
||||
package org.takiguchi.workflowvalidator.workflowvalidator.service;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
import org.takiguchi.workflowvalidator.workflowvalidator.model.Step;
|
||||
import org.takiguchi.workflowvalidator.workflowvalidator.model.StepType;
|
||||
import org.takiguchi.workflowvalidator.workflowvalidator.model.Workflow;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.catchThrowableOfType;
|
||||
import static org.takiguchi.workflowvalidator.workflowvalidator.model.Step.oneStep;
|
||||
import static org.takiguchi.workflowvalidator.workflowvalidator.model.StepType.*;
|
||||
|
||||
class WorkflowValidatorTest {
|
||||
/** Tested validator. */
|
||||
private WorkflowValidator validator;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
validator = new WorkflowValidator();
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("should_validate_data")
|
||||
void should_validate(Workflow workflow) {
|
||||
// when
|
||||
validator.validate(workflow);
|
||||
}
|
||||
|
||||
private static Stream<Arguments> should_validate_data() {
|
||||
return Stream.of(
|
||||
Arguments.of(workflowGood1()),
|
||||
Arguments.of(workflowGood2()),
|
||||
Arguments.of(workflowGood3())
|
||||
);
|
||||
}
|
||||
|
||||
private static Step buildOneStep(String name, StepType type) {
|
||||
return oneStep()
|
||||
.name(name)
|
||||
.type(type)
|
||||
.build();
|
||||
}
|
||||
|
||||
private static Workflow workflowGood1() {
|
||||
Step stepInitial = buildOneStep("A", INITIAL);
|
||||
|
||||
Step stepFinal = buildOneStep("B", FINAL);
|
||||
|
||||
stepInitial.addNextStep(stepFinal);
|
||||
stepFinal.addPreviousStep(stepInitial);
|
||||
|
||||
return new Workflow(List.of(stepInitial, stepFinal));
|
||||
}
|
||||
|
||||
private static Workflow workflowGood2() {
|
||||
Step stepInitial = buildOneStep("A", INITIAL);
|
||||
Step stepB = buildOneStep("B", MIDDLE);
|
||||
Step stepFinal = buildOneStep("B", FINAL);
|
||||
|
||||
stepInitial.addNextStep(stepB);
|
||||
|
||||
stepB.addPreviousStep(stepInitial);
|
||||
stepB.addNextStep(stepFinal);
|
||||
|
||||
stepFinal.addPreviousStep(stepB);
|
||||
|
||||
return new Workflow(List.of(stepInitial, stepB, stepFinal));
|
||||
}
|
||||
|
||||
private static Object workflowGood3() {
|
||||
Step stepInitial = buildOneStep("A", INITIAL);
|
||||
Step stepB = buildOneStep("B", MIDDLE);
|
||||
Step stepC = buildOneStep("C", MIDDLE);
|
||||
Step stepFinal = buildOneStep("D", FINAL);
|
||||
|
||||
stepInitial.addNextStep(stepB);
|
||||
stepInitial.addNextStep(stepC);
|
||||
|
||||
stepB.addPreviousStep(stepInitial);
|
||||
stepB.addNextStep(stepFinal);
|
||||
|
||||
stepC.addPreviousStep(stepInitial);
|
||||
stepC.addNextStep(stepFinal);
|
||||
|
||||
stepFinal.addPreviousStep(stepB);
|
||||
stepFinal.addPreviousStep(stepC);
|
||||
|
||||
return new Workflow(List.of(stepInitial, stepB, stepC, stepFinal));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("should_throw_an_exception_data")
|
||||
void should_throw_an_exception(Workflow workflow) {
|
||||
// when
|
||||
IllegalStateException exception = catchThrowableOfType(() -> validator.validate(workflow), IllegalStateException.class);
|
||||
|
||||
assertThat(exception).isNotNull();
|
||||
}
|
||||
|
||||
private static Stream<Arguments> should_throw_an_exception_data() {
|
||||
return Stream.of(
|
||||
Arguments.of(workflowBad1()),
|
||||
Arguments.of(workflowBad2()),
|
||||
Arguments.of(workflowBad3()),
|
||||
Arguments.of(workflowBad4())
|
||||
);
|
||||
}
|
||||
|
||||
private static Workflow workflowBad1() {
|
||||
Step stepInitial = buildOneStep("A", INITIAL);
|
||||
|
||||
return new Workflow(List.of(stepInitial));
|
||||
}
|
||||
|
||||
private static Workflow workflowBad2() {
|
||||
Step stepFinal = buildOneStep("B", FINAL);
|
||||
|
||||
return new Workflow(List.of(stepFinal));
|
||||
}
|
||||
|
||||
private static Workflow workflowBad3() {
|
||||
Step stepInitial = buildOneStep("A", INITIAL);
|
||||
Step stepFinal = buildOneStep("B", FINAL);
|
||||
|
||||
stepInitial.addNextStep(stepFinal);
|
||||
|
||||
return new Workflow(List.of(stepInitial, stepFinal));
|
||||
}
|
||||
|
||||
private static Workflow workflowBad4() {
|
||||
Step stepInitial = buildOneStep("A", INITIAL);
|
||||
Step stepFinal = buildOneStep("B", FINAL);
|
||||
|
||||
stepFinal.addPreviousStep(stepInitial);
|
||||
|
||||
return new Workflow(List.of(stepInitial, stepFinal));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user