Commit 9a44a670 authored by Stefan Schuhbaeck's avatar Stefan Schuhbaeck

refactor migration test.

parent 9ad30ee2
This diff is collapsed.
This diff is collapsed.
package org.vadere.simulator.projects.migration.jolttranformation;
import com.fasterxml.jackson.databind.JsonNode;
import org.vadere.simulator.entrypoints.Version;
import org.vadere.simulator.projects.migration.MigrationException;
public class JoltTransformV4toV5 extends JoltTransformation {
public JoltTransformV4toV5(String transformation, String identity, Version version) throws MigrationException {
super(transformation, identity, version);
}
@Override
protected void initPostHooks() throws MigrationException {
postTransformHooks.add(this::cleanupPedestrianOverlapProcessorAttribute);
postTransformHooks.add(this::addOverlapProcessors);
postTransformHooks.add(JoltTransformV1toV2::sort);
}
private JsonNode cleanupPedestrianOverlapProcessorAttribute(JsonNode node) throws MigrationException{
return node;
}
private JsonNode addOverlapProcessors(JsonNode node) throws MigrationException{
return node;
}
}
......@@ -26,7 +26,7 @@ public class TestUtils {
}
}
public static void copyDirTo(String dest, String backup) {
copyDirTo(Paths.get(dest), Paths.get(backup));
public static void copyDirTo(String source, String dest) {
copyDirTo(Paths.get(source), Paths.get(dest));
}
}
......@@ -144,7 +144,7 @@ public class JoltIdentityTransformationTest {
return get(new JsonObj(node.getMap().get(next)), path2);
}
if (node.type == ARRAY) {
throw new RuntimeException("only last element of path can be an array");
throw new RuntimeException("only last element of pathMustExist can be an array");
}
return new JsonObj(node);
}
......
......@@ -8,15 +8,9 @@ import org.vadere.simulator.entrypoints.Version;
import org.vadere.simulator.projects.io.TestUtils;
import org.vadere.state.util.StateJsonConverter;
import org.vadere.util.io.IOUtils;
import org.vadere.util.io.RecursiveCopy;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Comparator;
import joptsimple.internal.Strings;
......@@ -29,7 +23,7 @@ public class JoltMigrationAssistantTest {
public void resetTestStructure() throws URISyntaxException {
String dest = getClass().getResource("/migration/testProject_v0.1").toURI().getPath();
String backup = getClass().getResource("/migration/testProject_v0.1.bak").toURI().getPath();
TestUtils.resetTestStructure(dest, backup);
TestUtils.copyDirTo(dest, backup);
}
// Test transformation of single scenario file
......
......@@ -5,13 +5,14 @@ import com.fasterxml.jackson.databind.JsonNode;
import org.junit.Test;
import org.vadere.simulator.entrypoints.Version;
import org.vadere.simulator.projects.migration.MigrationException;
import org.vadere.state.util.StateJsonConverter;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Path;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
......@@ -20,6 +21,12 @@ public class JoltTransformV0toV1Test extends JoltTransformationTest{
private final String TRANSFORM = "/transform_vNOT-A-RELEASE_to_v0.1.json";
private final String IDENTITY = "/identity_v0.1.json";
@Override
protected Path getTestDir() {
return null;
}
// Source postHook should not bee used here
@Test
public void TestPostHooks1() throws IOException, MigrationException, URISyntaxException {
......@@ -27,19 +34,18 @@ public class JoltTransformV0toV1Test extends JoltTransformationTest{
String TEST1 = "/migration/vNOT-A-RELEASE_to_v0.1_Test1.scenario";
JsonNode in = getJson(TEST1);
JsonNode out = transformation.applyTransformation(in);
JsonNode sources = out.path("vadere").path("topography").path("sources");
assertFalse("There must be a source node", sources.isMissingNode());
// will test that sources exists.
JsonNode sources = pathMustExist(out, "vadere/topography/sources");
assertEquals("Therer must be one source",1, sources.size());
assertTrue("The source should not have the attribute distributionParameters", sources.elements().next().path("distributionParameters").isMissingNode());
assertEquals(out.path("vadere").path("mainModel").asText(),"org.vadere.simulator.models.osm.OptimalStepsModel");
assertEquals(out.path("vadere").path("attributesModel")
.path("org.vadere.state.attributes.models.AttributesOSM").path("pedestrianPotentialModel").asText(),
"org.vadere.simulator.models.potential.PotentialFieldPedestrianCompact");
assertEquals(out.path("vadere").path("attributesModel")
.path("org.vadere.state.attributes.models.AttributesOSM").path("obstaclePotentialModel").asText(),
"org.vadere.simulator.models.potential.PotentialFieldObstacleCompact");
assertThat(pathMustExist(out, "vadere/mainModel"),
nodeHasText("org.vadere.simulator.models.osm.OptimalStepsModel"));
assertThat(pathMustExist(out, "vadere/attributesModel/org.vadere.state.attributes.models.AttributesOSM/pedestrianPotentialModel"),
nodeHasText("org.vadere.simulator.models.potential.PotentialFieldPedestrianCompact"));
assertThat(pathMustExist(out, "vadere/attributesModel/org.vadere.state.attributes.models.AttributesOSM/obstaclePotentialModel"),
nodeHasText("org.vadere.simulator.models.potential.PotentialFieldObstacleCompact"));
}
......@@ -51,19 +57,16 @@ public class JoltTransformV0toV1Test extends JoltTransformationTest{
JsonNode in = getJson(TEST2);
JsonNode out = transformation.applyTransformation(in);
JsonNode sources = out.path("vadere").path("topography").path("sources");
assertFalse("There must be a source node", sources.isMissingNode());
JsonNode sources = pathMustExist(out, "vadere/topography/sources");
assertEquals("Therer must be one source",1, sources.size());
assertFalse("The source must have the attribute distributionParameters", sources.elements().next().path("distributionParameters").isMissingNode());
System.out.println(StateJsonConverter.writeValueAsString(out));
assertEquals(out.path("vadere").path("mainModel").asText(),"org.vadere.simulator.models.osm.OptimalStepsModel");
assertEquals(out.path("vadere").path("attributesModel")
.path("org.vadere.state.attributes.models.AttributesOSM").path("pedestrianPotentialModel").asText(),
"org.vadere.simulator.models.potential.PotentialFieldPedestrianCompact");
assertEquals(out.path("vadere").path("attributesModel")
.path("org.vadere.state.attributes.models.AttributesOSM").path("obstaclePotentialModel").asText(),
"org.vadere.simulator.models.potential.PotentialFieldObstacleCompact");
assertThat(pathMustExist(out, "vadere/mainModel"),
nodeHasText("org.vadere.simulator.models.osm.OptimalStepsModel"));
assertThat(pathMustExist(out, "vadere/attributesModel/org.vadere.state.attributes.models.AttributesOSM/pedestrianPotentialModel"),
nodeHasText("org.vadere.simulator.models.potential.PotentialFieldPedestrianCompact"));
assertThat(pathMustExist(out, "vadere/attributesModel/org.vadere.state.attributes.models.AttributesOSM/obstaclePotentialModel"),
nodeHasText("org.vadere.simulator.models.potential.PotentialFieldObstacleCompact"));
}
......@@ -78,4 +81,5 @@ public class JoltTransformV0toV1Test extends JoltTransformationTest{
fail("should not be reached! The Transformation should fail with MigrationException");
}
}
\ No newline at end of file
......@@ -5,10 +5,10 @@ import com.fasterxml.jackson.databind.JsonNode;
import org.junit.Test;
import org.vadere.simulator.entrypoints.Version;
import org.vadere.simulator.projects.migration.MigrationException;
import org.vadere.state.util.StateJsonConverter;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Path;
import static org.junit.Assert.*;
......@@ -17,17 +17,18 @@ public class JoltTransformV1toV2Test extends JoltTransformationTest{
private final String TRANSFORM = "/transform_v0.1_to_v0.2.json";
private final String IDENTITY = "/identity_v0.2.json";
@Override
protected Path getTestDir() {
return null;
}
@Test
public void Test1() throws MigrationException, IOException, URISyntaxException {
JoltTransformation t = JoltTransformation.get(Version.V0_1);
JsonNode node = t.applyTransformation(getJson("/migration/v0.1_to_v0.2_Test1.scenario"));
JsonNode apc = node.path("scenario").path("attributesModel").path("org.vadere.state.attributes.models.AttributesPotentialCompact");
assertFalse(apc.isMissingNode());
JsonNode osm = node.path("scenario").path("attributesModel").path("org.vadere.state.attributes.models.AttributesOSM");
assertFalse(osm.isMissingNode());
JsonNode aff = node.path("scenario").path("attributesModel").path("org.vadere.state.attributes.models.AttributesFloorField");
assertFalse(aff.isMissingNode());
pathMustExist(node, "scenario/attributesModel/org.vadere.state.attributes.models.AttributesPotentialCompact");
pathMustExist(node, "scenario/attributesModel/org.vadere.state.attributes.models.AttributesOSM");
pathMustExist(node, "scenario/attributesModel/org.vadere.state.attributes.models.AttributesFloorField");
}
......@@ -35,12 +36,9 @@ public class JoltTransformV1toV2Test extends JoltTransformationTest{
public void Test2() throws MigrationException, IOException, URISyntaxException {
JoltTransformation t = JoltTransformation.get(Version.V0_1);
JsonNode node = t.applyTransformation(getJson("/migration/v0.1_to_v0.2_Test2.scenario"));
JsonNode apc = node.path("scenario").path("attributesModel").path("org.vadere.state.attributes.models.AttributesPotentialCompact");
assertFalse(apc.isMissingNode());
JsonNode osm = node.path("scenario").path("attributesModel").path("org.vadere.state.attributes.models.AttributesOSM");
assertFalse(osm.isMissingNode());
JsonNode aff = node.path("scenario").path("attributesModel").path("org.vadere.state.attributes.models.AttributesFloorField");
assertFalse(aff.isMissingNode());
pathMustExist(node,"scenario/attributesModel/org.vadere.state.attributes.models.AttributesPotentialCompact");
pathMustExist(node, "scenario/attributesModel/org.vadere.state.attributes.models.AttributesOSM");
pathMustExist(node, "scenario/attributesModel/org.vadere.state.attributes.models.AttributesFloorField");
}
......@@ -49,10 +47,9 @@ public class JoltTransformV1toV2Test extends JoltTransformationTest{
public void Test3() throws MigrationException, IOException, URISyntaxException {
JoltTransformation t = JoltTransformation.get(Version.V0_1);
JsonNode node = t.applyTransformation(getJson("/migration/v0.1_to_v0.2_Test3.scenario"));
assertTrue("should be there and empty",!node.path("scenario").path("attributesModel").isMissingNode());
assertEquals("should be empty", 0, node.path("scenario").path("attributesModel").size());
assertEquals("Name schould be set", "XXXX", node.path("name").asText());
pathMustExist(node, "scenario/attributesModel");
assertEquals("should be empty", 0, pathMustExist(node, "scenario/attributesModel").size());
assertThat(pathMustExist(node, "name"), nodeHasText("XXXX"));
}
}
\ No newline at end of file
package org.vadere.simulator.projects.migration.jolttranformation;
import java.nio.file.Path;
public class JoltTransformV2toV3Test extends JoltTransformationTest{
private final String TRANSFORM = "/transform_v0.2_to_v0.3.json";
private final String IDENTITY = "/identity_v0.3.json";
@Override
protected Path getTestDir() {
return null;
}
}
package org.vadere.simulator.projects.migration.jolttranformation;
import org.hamcrest.core.IsNot;
import org.hamcrest.core.StringContains;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.vadere.simulator.entrypoints.Version;
import org.vadere.simulator.projects.migration.MigrationAssistant;
import org.vadere.simulator.projects.migration.MigrationException;
import org.vadere.simulator.projects.migration.MigrationOptions;
import org.vadere.util.io.IOUtils;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import org.vadere.simulator.entrypoints.Version;
import static org.hamcrest.core.IsNot.not;
import static org.junit.Assert.*;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
public class JoltTransformV3toV4Test {
public class JoltTransformV3toV4Test extends JoltTransformationTest{
ArrayList<Path> scenarios;
ArrayList<Path> bakScenarios;
private StringContains v04 = new StringContains("\"release\" : \"0.4\"");
private StringContains v04 = versionMatcher(Version.V0_4);
private StringContains useFixedSeed = new StringContains("useFixedSeed");
private StringContains fixedSeed = new StringContains("fixedSeed");
private StringContains simulationSeed = new StringContains("simulationSeed");
private StringContains useRandomSeed = new StringContains("useRandomSeed");
private StringContains randomSeed = new StringContains("randomSeed");
@Override
protected Path getTestDir() {
return getDirFromResources("/migration/v03_to_v04");
}
@Before
public void init() throws URISyntaxException {
public void init() {
super.init();
scenarios = new ArrayList<>();
bakScenarios = new ArrayList<>();
scenarios.add(Paths.get(getClass()
.getResource("/migration/v03_to_v04/basic_1_chicken_osm1.scenario").toURI()));
scenarios.add(Paths.get(getClass()
.getResource("/migration/v03_to_v04/group_OSM_1Source1Place.scenario").toURI()));
scenarios.forEach(f -> {
try {
bakScenarios.add(IOUtils.makeBackup(f, ".bak", true));
} catch (IOException e) {
e.printStackTrace();
}
});
scenarios.add(getTestFile("basic_1_chicken_osm1.scenario"));
scenarios.add(getTestFile("group_OSM_1Source1Place.scenario"));
bakScenarios.add(getTestFileBackup("basic_1_chicken_osm1.scenario"));
bakScenarios.add(getTestFileBackup("group_OSM_1Source1Place.scenario"));
}
@After
public void cleaUp() throws IOException {
for (int i=0; i < scenarios.size(); i++){
if (scenarios.get(i) != null && bakScenarios.get(i) != null){
Files.move(bakScenarios.get(i), scenarios.get(i), StandardCopyOption.REPLACE_EXISTING);
}
}
public void cleaUp() {
super.cleaUp();
}
@Test
......
package org.vadere.simulator.projects.migration.jolttranformation;
import java.nio.file.Path;
import static org.junit.Assert.*;
public class JoltTransformV4toV5Test extends JoltTransformationTest {
@Override
protected Path getTestDir() {
return getDirFromResources("/migration/v04_to_v05");
}
}
\ No newline at end of file
......@@ -2,23 +2,123 @@ package org.vadere.simulator.projects.migration.jolttranformation;
import com.fasterxml.jackson.databind.JsonNode;
import org.junit.Test;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.vadere.simulator.entrypoints.Version;
import org.hamcrest.core.StringContains;
import org.junit.After;
import org.junit.Before;
import org.vadere.simulator.projects.io.TestUtils;
import org.vadere.state.util.StateJsonConverter;
import org.vadere.util.io.IOUtils;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import static org.junit.Assert.*;
import static org.junit.Assert.assertThat;
abstract class JoltTransformationTest {
abstract protected Path getTestDir();
protected Path getTestDirBackup(){
Path testDir = getTestDir();
return testDir.getParent().resolve(testDir.getFileName() + ".bak");
}
protected Path getDirFromResources(String resource){
URL resource1 = getClass().getResource(resource);
File f = new File(resource1.getFile());
return Paths.get(f.toString());
}
protected Path getTestFile(String fileName){
return getTestDir().resolve(fileName);
}
protected Path getTestFileBackup(String fileName){
return getTestDirBackup().resolve(fileName);
}
@Before
public void init(){
Path testDir = getTestDir();
if (testDir != null) {
Path testDirBackup = getTestDirBackup();
TestUtils.copyDirTo(testDir, testDirBackup);
}
}
class JoltTransformationTest {
@After
public void cleaUp(){
Path testDir = getTestDir();
if (testDir != null) {
Path testDirBackup = getTestDirBackup();
TestUtils.copyDirTo(testDirBackup, testDir);
}
}
/**
* Get path from JsonNode and assert that it exists.
*/
protected JsonNode pathMustExist(JsonNode root, String path){
String[] pathElements = path.split("/");
JsonNode ret = root;
for (String item : pathElements) {
ret = ret.path(item);
}
assertThat(ret, notMissing(path));
return ret;
}
JsonNode getJson(String resources) throws IOException, URISyntaxException {
URL url = getClass().getResource(resources);
String json = IOUtils.readTextFile(Paths.get(url.toURI()));
return StateJsonConverter.deserializeToNode(json);
}
StringContains versionMatcher(Version v){
String pattern = "\"release\" : \"" + v.label('_') +"\"";
return new StringContains(pattern);
}
public Matcher<JsonNode> nodeHasText(String text){
return new BaseMatcher<JsonNode>() {
@Override
public boolean matches(Object o) {
final JsonNode n = (JsonNode)o;
return n.asText().equals(text);
}
@Override
public void describeTo(Description description) {
description.appendText("<\"" + text + "\">");
}
};
}
public Matcher<JsonNode> notMissing(String path){
return new BaseMatcher<JsonNode>() {
@Override
public boolean matches(Object o) {
final JsonNode n = (JsonNode)o;
return !n.isMissingNode();
}
@Override
public void describeTo(Description description) {
description.appendText("The nodes should not be empty. It should be reachable with Path: <" + path + ">" );
}
};
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment