Skip to content

Commit

Permalink
[GEOS-8724] Intermittent build failure in NamespacesWfsTest (#2870)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuno Oliveira committed May 9, 2018
1 parent bd45692 commit 75c7639
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 147 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ protected void addStationFeatureType(String namespacePrefix, String gmlPrefix,
String featureType, String mappingsName, String mappingsPath, String featureType, String mappingsName, String mappingsPath,
Map<String, String> parameters) { Map<String, String> parameters) {
// create root directory // create root directory
File gmlDirectory = new File(TEST_ROOT_DIRECTORY, gmlPrefix); File gmlDirectory = getDirectoryForGmlPrefix(gmlPrefix);
gmlDirectory.mkdirs(); gmlDirectory.mkdirs();
// add the necessary files // add the necessary files
File stationsMappings = new File(gmlDirectory, File stationsMappings = new File(gmlDirectory,
Expand Down Expand Up @@ -144,7 +144,7 @@ protected void addStationFeatureType(String namespacePrefix, String gmlPrefix,
String stationsMappingsPath, String measurementsMappingsName, String stationsMappingsPath, String measurementsMappingsName,
String measurementsMappingsPath, Map<String, String> parameters) { String measurementsMappingsPath, Map<String, String> parameters) {
// create root directory // create root directory
File gmlDirectory = new File(TEST_ROOT_DIRECTORY, gmlPrefix); File gmlDirectory = getDirectoryForGmlPrefix(gmlPrefix);
gmlDirectory.mkdirs(); gmlDirectory.mkdirs();
// add the necessary files // add the necessary files
File stationsMappings = new File(gmlDirectory, File stationsMappings = new File(gmlDirectory,
Expand Down Expand Up @@ -174,7 +174,7 @@ protected void addStationFeatureType(String namespacePrefix, String gmlPrefix,
protected void addMeasurementFeatureType(String namespacePrefix, String gmlPrefix, protected void addMeasurementFeatureType(String namespacePrefix, String gmlPrefix,
String mappingsName, String mappingsPath, Map<String, String> parameters) { String mappingsName, String mappingsPath, Map<String, String> parameters) {
// create root directory // create root directory
File gmlDirectory = new File(TEST_ROOT_DIRECTORY, gmlPrefix); File gmlDirectory = getDirectoryForGmlPrefix(gmlPrefix);
gmlDirectory.mkdirs(); gmlDirectory.mkdirs();
// add the necessary files // add the necessary files
File measurementsMappings = new File(gmlDirectory, File measurementsMappings = new File(gmlDirectory,
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -8,24 +8,19 @@
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;


import java.io.File;
import java.io.InputStream;
import java.io.StringReader; import java.io.StringReader;
import java.nio.file.Files;
import java.util.AbstractMap.SimpleEntry; import java.util.AbstractMap.SimpleEntry;
import java.util.Collections; import java.util.Collections;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;


import org.custommonkey.xmlunit.XpathEngine; import org.custommonkey.xmlunit.XpathEngine;
import org.geoserver.util.IOUtils;
import org.geoserver.wfs.StoredQuery; import org.geoserver.wfs.StoredQuery;
import org.geoserver.wfs.StoredQueryProvider; import org.geoserver.wfs.StoredQueryProvider;
import org.geotools.wfs.v2_0.WFS; import org.geotools.wfs.v2_0.WFS;
import org.geotools.wfs.v2_0.WFSConfiguration; import org.geotools.wfs.v2_0.WFSConfiguration;
import org.geotools.xml.Parser; import org.geotools.xml.Parser;
import org.junit.AfterClass;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.w3c.dom.Document; import org.w3c.dom.Document;
Expand All @@ -38,8 +33,6 @@
*/ */
public final class NamespacesWfsTest extends AbstractAppSchemaTestSupport { public final class NamespacesWfsTest extends AbstractAppSchemaTestSupport {


private static final File TEST_ROOT_DIRECTORY;

private static final String TEST_STORED_QUERY_ID = "NamespacesTestStoredQuery"; private static final String TEST_STORED_QUERY_ID = "NamespacesTestStoredQuery";


/* Should return the same result as a GetFeature request against the Station feature type */ /* Should return the same result as a GetFeature request against the Station feature type */
Expand Down Expand Up @@ -78,133 +71,6 @@ public final class NamespacesWfsTest extends AbstractAppSchemaTestSupport {
new SimpleEntry<>("GML_LOCATION", "http://schemas.opengis.net/gml/3.2.1/gml.xsd")) new SimpleEntry<>("GML_LOCATION", "http://schemas.opengis.net/gml/3.2.1/gml.xsd"))
.collect(Collectors.toMap((e) -> e.getKey(), (e) -> e.getValue()))); .collect(Collectors.toMap((e) -> e.getKey(), (e) -> e.getValue())));


static {
try {
// create the tests root directory
TEST_ROOT_DIRECTORY = IOUtils.createTempDirectory("app-schema-stations");
} catch (Exception exception) {
throw new RuntimeException("Error creating temporary directory.", exception);
}
}

@AfterClass
public static void afterTests() {
try {
// remove tests root directory
IOUtils.delete(TEST_ROOT_DIRECTORY);
} catch (Exception exception) {
throw new RuntimeException(String.format(
"Error removing tests root directory '%s'.", TEST_ROOT_DIRECTORY.getAbsolutePath()));
}
}

/**
* Helper class that will setup custom complex feature types using the stations data set.
* Parameterization will be used to setup complex features types for GML31 and GML32 based
* on the same mappings files and schemas.
*/
private static final class MockData extends AbstractAppSchemaMockData {

// stations GML 3.1 namespaces
private static final String STATIONS_PREFIX_GML31 = "st_gml31";
private static final String STATIONS_URI_GML31 = "http://www.stations_gml31.org/1.0";
private static final String MEASUREMENTS_PREFIX_GML31 = "ms_gml31";
private static final String MEASUREMENTS_URI_GML31 = "http://www.measurements_gml31.org/1.0";

// stations GML 3.2 namespaces
private static final String STATIONS_PREFIX_GML32 = "st_gml32";
private static final String STATIONS_URI_GML32 = "http://www.stations_gml32.org/1.0";
private static final String MEASUREMENTS_PREFIX_GML32 = "ms_gml32";
private static final String MEASUREMENTS_URI_GML32 = "http://www.measurements_gml32.org/1.0";

@Override
public void addContent() {
// add GML 3.1 namespaces
putNamespace(STATIONS_PREFIX_GML31, STATIONS_URI_GML31);
putNamespace(MEASUREMENTS_PREFIX_GML31, MEASUREMENTS_URI_GML31);
// add GML 3.2 namespaces
putNamespace(STATIONS_PREFIX_GML32, STATIONS_URI_GML32);
putNamespace(MEASUREMENTS_PREFIX_GML32, MEASUREMENTS_URI_GML32);
// add GML 3.1 feature type
addFeatureType(STATIONS_PREFIX_GML31, "gml31", GML31_PARAMETERS);
// add GML 3.2 feature type
addFeatureType(STATIONS_PREFIX_GML32, "gml32", GML32_PARAMETERS);
}

/**
* Helper method that will add the stations feature type customizing
* it for the desired GML version.
*/
private void addFeatureType(String namespacePrefix, String gmlPrefix, Map<String, String> parameters) {
// create root directory
File gmlDirectory = new File(TEST_ROOT_DIRECTORY, gmlPrefix);
gmlDirectory.mkdirs();
// add the necessary files
File stationsMappings = new File(gmlDirectory, String.format("stations_%s.xml", gmlPrefix));
File stationsProperties = new File(gmlDirectory, String.format("stations_%s.properties", gmlPrefix));
File stationsSchema = new File(gmlDirectory, String.format("stations_%s.xsd", gmlPrefix));
File measurementsMappings = new File(gmlDirectory, String.format("measurements_%s.xml", gmlPrefix));
File measurementsProperties = new File(gmlDirectory, String.format("measurements_%s.properties", gmlPrefix));
File measurementsSchema = new File(gmlDirectory, String.format("measurements_%s.xsd", gmlPrefix));
// perform the parameterization
substituteParameters("/test-data/stations/mappings/stations.xml", parameters, stationsMappings);
substituteParameters("/test-data/stations/data/stations.properties", parameters, stationsProperties);
substituteParameters("/test-data/stations/schemas/stations.xsd", parameters, stationsSchema);
substituteParameters("/test-data/stations/mappings/measurements.xml", parameters, measurementsMappings);
substituteParameters("/test-data/stations/data/measurements.properties", parameters, measurementsProperties);
substituteParameters("/test-data/stations/schemas/measurements.xsd", parameters, measurementsSchema);
// create the feature type
addFeatureType(namespacePrefix, String.format("Station_%s", gmlPrefix),
stationsMappings.getAbsolutePath(),
stationsProperties.getAbsolutePath(),
stationsSchema.getAbsolutePath(),
measurementsMappings.getAbsolutePath(),
measurementsProperties.getAbsolutePath(),
measurementsSchema.getAbsolutePath());
}

/**
* Helper method that reads a resource to a string performs the
* parameterization and writes the result to the provided new file.
*/
private static void substituteParameters(String resourceName, Map<String, String> parameters, File newFile) {
// read the resource content
String resourceContent = resourceToString(resourceName);
resourceContent = substituteParametersInTemplateString(resourceContent, parameters);
try {
// write the final resource content to the provided location
Files.write(newFile.toPath(), resourceContent.getBytes());
} catch (Exception exception) {
throw new RuntimeException(String.format(
"Error writing content to file '%s'.", newFile.getAbsolutePath()), exception);
}
}

private static String substituteParametersInTemplateString(String templateString,
Map<String, String> parameters) {
String processedString = templateString;

for (Map.Entry<String, String> parameter : parameters.entrySet()) {
processedString = processedString
.replace(String.format("${%s}", parameter.getKey()), parameter.getValue());
}

return processedString;
}

/**
* Helper method the reads a resource content to a string.
*/
private static String resourceToString(String resourceName) {
try (InputStream input = NamespacesWfsTest.class.getResourceAsStream(resourceName)) {
return IOUtils.toString(input);
} catch (Exception exception) {
throw new RuntimeException(String.format(
"Error reading resource '%s' content.", resourceName), exception);
}
}
}

// xpath engines used to check WFS responses // xpath engines used to check WFS responses
private XpathEngine WFS11_XPATH_ENGINE; private XpathEngine WFS11_XPATH_ENGINE;
private XpathEngine WFS20_XPATH_ENGINE; private XpathEngine WFS20_XPATH_ENGINE;
Expand Down Expand Up @@ -349,8 +215,7 @@ private StoredQueryDescriptionType createTestStoredQueryDefinition(
Parser p = new Parser(new WFSConfiguration()); Parser p = new Parser(new WFSConfiguration());
p.setRootElementType(WFS.StoredQueryDescriptionType); p.setRootElementType(WFS.StoredQueryDescriptionType);


String queryDefinition = MockData String queryDefinition = substitutePlaceHolders(TEST_STORED_QUERY_DEFINITION, parameters);
.substituteParametersInTemplateString(TEST_STORED_QUERY_DEFINITION, parameters);
StringReader reader = new StringReader(queryDefinition); StringReader reader = new StringReader(queryDefinition);
try { try {
return (StoredQueryDescriptionType) p.parse(reader); return (StoredQueryDescriptionType) p.parse(reader);
Expand All @@ -359,6 +224,19 @@ private StoredQueryDescriptionType createTestStoredQueryDefinition(
} }
} }


/**
* Helper method that just substitutes the provided place holder values in the provided string.
* Place holders are identified with syntax ${PLACE_HOLDER_NAME}.
*/
private static String substitutePlaceHolders(String string, Map<String, String> placeHolderValues) {
String processedString = string;
for (Map.Entry<String, String> placeHolder : placeHolderValues.entrySet()) {
processedString = processedString
.replace(String.format("${%s}", placeHolder.getKey()), placeHolder.getValue());
}
return processedString;
}

/** /**
* Check the result of a WFS 1.1 (GML 3.1) get feature request targeting stations data set. * Check the result of a WFS 1.1 (GML 3.1) get feature request targeting stations data set.
*/ */
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ public class StationsMockData extends AbstractAppSchemaMockData {
protected static final String MEASUREMENTS_URI_GML32 = "http://www.measurements_gml32.org/1.0"; protected static final String MEASUREMENTS_URI_GML32 = "http://www.measurements_gml32.org/1.0";


// directory that should contain all the new files created during the setup of this data set // directory that should contain all the new files created during the setup of this data set
protected final File TEST_ROOT_DIRECTORY = createTestRootDirectory(); private File testRootDirectory;



/** /**
* Helper method that just quietly creates a temporary directory, * Helper method that just quietly creates a temporary directory,
Expand Down Expand Up @@ -159,7 +158,7 @@ protected static String resourceToString(String resourceName) {
protected void addMeasurementFeatureType(String namespacePrefix, String gmlPrefix, String mappingsName, protected void addMeasurementFeatureType(String namespacePrefix, String gmlPrefix, String mappingsName,
String mappingsPath, Map<String, String> parameters) { String mappingsPath, Map<String, String> parameters) {
// create root directory // create root directory
File gmlDirectory = new File(TEST_ROOT_DIRECTORY, gmlPrefix); File gmlDirectory = getDirectoryForGmlPrefix(gmlPrefix);
gmlDirectory.mkdirs(); gmlDirectory.mkdirs();
// add the necessary files // add the necessary files
File measurementsMappings = new File(gmlDirectory, String.format("%s_%s.xml", mappingsName, gmlPrefix)); File measurementsMappings = new File(gmlDirectory, String.format("%s_%s.xml", mappingsName, gmlPrefix));
Expand All @@ -182,7 +181,7 @@ protected void addMeasurementFeatureType(String namespacePrefix, String gmlPrefi
protected void addStationFeatureType(String namespacePrefix, String gmlPrefix, String mappingsName, protected void addStationFeatureType(String namespacePrefix, String gmlPrefix, String mappingsName,
String mappingsPath, Map<String, String> parameters) { String mappingsPath, Map<String, String> parameters) {
// create root directory // create root directory
File gmlDirectory = new File(TEST_ROOT_DIRECTORY, gmlPrefix); File gmlDirectory = getDirectoryForGmlPrefix(gmlPrefix);
gmlDirectory.mkdirs(); gmlDirectory.mkdirs();
// add the necessary files // add the necessary files
File stationsMappings = new File(gmlDirectory, String.format("%s_%s.xml", mappingsName, gmlPrefix)); File stationsMappings = new File(gmlDirectory, String.format("%s_%s.xml", mappingsName, gmlPrefix));
Expand All @@ -209,7 +208,7 @@ protected void addStationFeatureType(String namespacePrefix, String gmlPrefix, S
String stationsMappingsPath, String measurementsMappingsName, String stationsMappingsPath, String measurementsMappingsName,
String measurementsMappingsPath, Map<String, String> parameters) { String measurementsMappingsPath, Map<String, String> parameters) {
// create root directory // create root directory
File gmlDirectory = new File(TEST_ROOT_DIRECTORY, gmlPrefix); File gmlDirectory = getDirectoryForGmlPrefix(gmlPrefix);
gmlDirectory.mkdirs(); gmlDirectory.mkdirs();
// add the necessary files // add the necessary files
File stationsMappings = new File(gmlDirectory, String.format("%s_%s.xml", stationsMappingsName, gmlPrefix)); File stationsMappings = new File(gmlDirectory, String.format("%s_%s.xml", stationsMappingsName, gmlPrefix));
Expand All @@ -233,17 +232,29 @@ protected void addStationFeatureType(String namespacePrefix, String gmlPrefix, S
measurementsMappings.getAbsolutePath(), measurementsMappings.getAbsolutePath(),
measurementsProperties.getAbsolutePath()); measurementsProperties.getAbsolutePath());
} }


/**
* Helper method that returns the directory, relative to tests root directory, that will
* contain the mappings, schemas, properties, etc ... of the target GML version.
*/
protected synchronized File getDirectoryForGmlPrefix(String gmlPrefix) {
if (testRootDirectory == null) {
// init the test directory
testRootDirectory = createTestRootDirectory();
}
return new File(testRootDirectory, gmlPrefix);
}

@Override @Override
public void tearDown() { public void tearDown() {
super.tearDown(); super.tearDown();
try { try {
// remove tests root directory // remove tests root directory
IOUtils.delete(TEST_ROOT_DIRECTORY); IOUtils.delete(testRootDirectory);
} catch (Exception exception) { } catch (Exception exception) {
// something bad happen, just log the exception and move on // something bad happen, just log the exception and move on
LOGGER.log(Level.WARNING, String.format( LOGGER.log(Level.WARNING, String.format(
"Error removing tests root directory '%s'.", TEST_ROOT_DIRECTORY.getAbsolutePath()), exception); "Error removing tests root directory '%s'.", testRootDirectory.getAbsolutePath()), exception);
} }
} }
} }

0 comments on commit 75c7639

Please sign in to comment.