Skip to content

Commit

Permalink
#184 Adding tests for the Workflow normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasLacroix committed Nov 9, 2021
1 parent 969c9c0 commit 01cd51a
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 72 deletions.
43 changes: 0 additions & 43 deletions ml2wf/src/test/java/com/ml2wf/testutils/XMLTestBase.java

This file was deleted.

17 changes: 17 additions & 0 deletions ml2wf/src/test/java/com/ml2wf/v2/testutils/TreeTestBase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.ml2wf.v2.testutils;

import com.ml2wf.v2.tree.wf.factory.IWorkflowFactory;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public abstract class TreeTestBase {

/**
* {@code ClassLoader}'s instance used to get resources.
*
* @see ClassLoader
*/
protected static ClassLoader classLoader = TreeTestBase.class.getClassLoader();

protected final IWorkflowFactory workflowFactory;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.ml2wf.v2.testutils;

import com.ml2wf.util.FileHandler;
import com.ml2wf.util.Pair;
import com.ml2wf.v2.tree.wf.Workflow;
import com.ml2wf.v2.xml.XMLWorkflowFactory;
import io.vavr.control.Try;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.net.URISyntaxException;
import java.util.HashSet;
import java.util.Objects;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertTrue;

public class XMLWorkflowTestBase extends TreeTestBase {

/**
* Meta workflows' directory.
*/
protected static final String META_DIRECTORY = "./wf_meta/";

public XMLWorkflowTestBase() {
super(new XMLWorkflowFactory());
}

/**
* Returns a {@code Stream} containing all meta-workflows located under the
* {@link #META_DIRECTORY} directory.
*
* @return a {@code Stream} containing all meta-workflows located under the
* {@link #META_DIRECTORY} directory
*
* @throws URISyntaxException
*
* @see FileUtils
*/
protected static Stream<File> metaFiles() throws URISyntaxException {
File instanceDir = new File(
Objects.requireNonNull(classLoader.getResource(META_DIRECTORY)).toURI());
return new HashSet<>(FileUtils.listFiles(instanceDir, FileHandler.getWfExtensions(), true)).stream();
}

protected Pair<Workflow, Workflow> getReferenceInstanceWorkflows(File file) {
Try<Workflow> tryReferenceWorkflow = workflowFactory.workflowFromFile(file);
assertTrue(tryReferenceWorkflow.isSuccess());
// the workflow to instantiate that will be compared to the reference one
Try<Workflow> tryWorkflowToInstantiate = workflowFactory.workflowFromFile(file);
assertTrue(tryWorkflowToInstantiate.isSuccess());
Workflow workflowToInstantiate = tryWorkflowToInstantiate.get();
// instantiating
workflowToInstantiate.instantiate();
return new Pair<>(tryReferenceWorkflow.get(), workflowToInstantiate);
}

protected Pair<Workflow, Workflow> getReferenceNormalizedWorkflows(File file) {
Try<Workflow> tryReferenceWorkflow = workflowFactory.workflowFromFile(file);
assertTrue(tryReferenceWorkflow.isSuccess());
// the workflow to instantiate that will be compared to the reference one
Try<Workflow> tryWorkflowToInstantiate = workflowFactory.workflowFromFile(file);
assertTrue(tryWorkflowToInstantiate.isSuccess());
Workflow workflowToInstantiate = tryWorkflowToInstantiate.get();
// instantiating
workflowToInstantiate.normalize();
return new Pair<>(tryReferenceWorkflow.get(), workflowToInstantiate);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ml2wf.testutils.assertions;
package com.ml2wf.v2.testutils.assertions;

import com.ml2wf.util.Pair;
import com.ml2wf.v2.tree.ITreeIterator;
Expand All @@ -24,15 +24,15 @@ public void verify() {
ITreeIterator<Process> workflow1Iterator = workflow1.iterator();
ITreeIterator<Process> workflow2Iterator = workflow2.iterator();
while (workflow1Iterator.hasNext() && workflow2Iterator.hasNext()) {
ITreeIterator<WorkflowTask> referenceTasksIterator = workflow1Iterator.next().iterator();
ITreeIterator<WorkflowTask> instantiatedTasksIterator = workflow2Iterator.next().iterator();
while (referenceTasksIterator.hasNext() && instantiatedTasksIterator.hasNext()) {
WorkflowTask referenceTask = referenceTasksIterator.next();
WorkflowTask instantiatedTask = instantiatedTasksIterator.next();
Optional.ofNullable(forEachTask).ifPresent(c -> c.accept(referenceTask, instantiatedTask));
ITreeIterator<WorkflowTask> workflow1TasksIterator = workflow1Iterator.next().iterator();
ITreeIterator<WorkflowTask> workflow2TasksIterator = workflow2Iterator.next().iterator();
while (workflow1TasksIterator.hasNext() && workflow2TasksIterator.hasNext()) {
WorkflowTask workflow1Task = workflow1TasksIterator.next();
WorkflowTask workflow2Task = workflow2TasksIterator.next();
Optional.ofNullable(forEachTask).ifPresent(c -> c.accept(workflow1Task, workflow2Task));
}
Optional.ofNullable(afterTasksIteration)
.ifPresent(c -> c.accept(referenceTasksIterator, instantiatedTasksIterator));
.ifPresent(c -> c.accept(workflow1TasksIterator, workflow2TasksIterator));
}
Optional.ofNullable(afterProcessesIteration).ifPresent(c -> c.accept(workflow1Iterator, workflow2Iterator));
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package com.ml2wf.v2.tree.wf;

import com.ml2wf.testutils.assertions.ForEachTaskAssertion;
import com.ml2wf.testutils.XMLTestBase;
import com.ml2wf.util.Pair;
import com.ml2wf.v2.tree.wf.factory.IWorkflowFactory;
import com.ml2wf.v2.xml.XMLWorkflowFactory;
import io.vavr.control.Try;
import com.ml2wf.v2.testutils.XMLWorkflowTestBase;
import com.ml2wf.v2.testutils.assertions.ForEachTaskAssertion;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
Expand All @@ -14,21 +10,7 @@

import static org.junit.jupiter.api.Assertions.*;

class TestWorkflowInstantiation extends XMLTestBase {

private final IWorkflowFactory workflowFactory = new XMLWorkflowFactory();

private Pair<Workflow, Workflow> getReferenceInstanceWorkflows(File file) {
Try<Workflow> tryReferenceWorkflow = workflowFactory.workflowFromFile(file);
assertTrue(tryReferenceWorkflow.isSuccess());
// the workflow to instantiate that will be compared to the reference one
Try<Workflow> tryWorkflowToInstantiate = workflowFactory.workflowFromFile(file);
assertTrue(tryWorkflowToInstantiate.isSuccess());
Workflow workflowToInstantiate = tryWorkflowToInstantiate.get();
// instantiating
workflowToInstantiate.instantiate();
return new Pair<>(tryReferenceWorkflow.get(), workflowToInstantiate);
}
class TestWorkflowInstantiation extends XMLWorkflowTestBase {

@ParameterizedTest
@MethodSource("metaFiles")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.ml2wf.v2.tree.wf;

import com.ml2wf.v2.testutils.XMLWorkflowTestBase;
import com.ml2wf.v2.testutils.assertions.ForEachTaskAssertion;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import java.io.File;

import static org.junit.jupiter.api.Assertions.*;

class TestWorkflowNormalization extends XMLWorkflowTestBase {

@ParameterizedTest
@MethodSource("metaFiles")
@DisplayName("Testing that the normalization updates the workflow's tasks names as expected")
void testNormalizationUpdatesNames(File file) {
ForEachTaskAssertion.builder()
.workflowPair(getReferenceNormalizedWorkflows(file))
.forEachTask((referenceTask, instantiatedTask) -> {
assertEquals(referenceTask.getId(), instantiatedTask.getId());
assertFalse(instantiatedTask.getName().contains(" "));
assertEquals(referenceTask.getName().replace(" ", "_"),
instantiatedTask.getName());
})
.afterTasksIteration((referenceProcessIterator, instanceProcessIterator) ->
assertFalse(referenceProcessIterator.hasNext() || instanceProcessIterator.hasNext())
)
.afterProcessesIteration((referenceInstanceProcess, referenceInstanceWorkflows) ->
assertFalse(referenceInstanceProcess.hasNext() || referenceInstanceWorkflows.hasNext())
)
.build()
.verify();
}

@ParameterizedTest
@MethodSource("metaFiles")
@DisplayName("Testing that the normalization does not impact the iteration")
void testNormalizationDoesNotImpactIteration(File file) {
ForEachTaskAssertion.builder()
.workflowPair(getReferenceNormalizedWorkflows(file))
.forEachTask((referenceTask, instantiatedTask) -> {
assertEquals(referenceTask.getId(), instantiatedTask.getId());
// note that we don't test the tasks' names as it is not the purpose of this test
})
.afterTasksIteration((referenceProcessIterator, instanceProcessIterator) ->
assertFalse(referenceProcessIterator.hasNext() || instanceProcessIterator.hasNext())
)
.afterProcessesIteration((referenceInstanceProcess, referenceInstanceWorkflows) ->
assertFalse(referenceInstanceProcess.hasNext() || referenceInstanceWorkflows.hasNext())
)
.build()
.verify();
}

@ParameterizedTest
@MethodSource("metaFiles")
@DisplayName("Testing that the normalization does not impact the documentation")
void testNormalizationDoesNotImpactTheDocumentation(File file) {
ForEachTaskAssertion.builder()
.workflowPair(getReferenceNormalizedWorkflows(file))
.forEachTask((referenceTask, instantiatedTask) -> {
assertEquals(referenceTask.getId(), instantiatedTask.getId());
if (referenceTask.getDocumentation() != null) {
assertEquals(referenceTask.getDocumentation().getId(),
instantiatedTask.getDocumentation().getId());
assertTrue(instantiatedTask.getDocumentation().getContent()
.contains(referenceTask.getDocumentation().getContent()));
}
})
.afterTasksIteration((referenceProcessIterator, instanceProcessIterator) ->
assertFalse(referenceProcessIterator.hasNext() || instanceProcessIterator.hasNext())
)
.afterProcessesIteration((referenceInstanceProcess, referenceInstanceWorkflows) ->
assertFalse(referenceInstanceProcess.hasNext() || referenceInstanceWorkflows.hasNext())
)
.build()
.verify();
}
}

0 comments on commit 01cd51a

Please sign in to comment.