-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from pixeltree/master
Validate Lights_xxx.xml file has valid format
- Loading branch information
Showing
13 changed files
with
735 additions
and
2 deletions.
There are no files selected for viewing
226 changes: 226 additions & 0 deletions
226
src/main/java/org/opengis/cite/cdb10/CDBStructure/LightsXxxXmlStructureTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,226 @@ | ||
package org.opengis.cite.cdb10.CDBStructure; | ||
|
||
import org.opengis.cite.cdb10.CommonFixture; | ||
import org.opengis.cite.cdb10.util.SchemaValidatorErrorHandler; | ||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
import org.w3c.dom.Node; | ||
import org.w3c.dom.NodeList; | ||
import org.xml.sax.SAXException; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.*; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
/** | ||
* Created by martin on 2016-09-12. | ||
*/ | ||
public class LightsXxxXmlStructureTests extends CommonFixture { | ||
|
||
private static final List<String> DIRECTIONALITY_VALUES = Arrays.asList("Omnidirectional", "Directional", "Bidirectional"); | ||
|
||
@Test | ||
public void verifyLights_XxxXmlFileExists() { | ||
for (File xmlFile : getCustomLightsXmlFiles()) { | ||
Assert.assertTrue(Files.exists(Paths.get(path, "Metadata", "Schema", xmlFile.getName())), "Optional file."); | ||
} | ||
} | ||
|
||
@Test | ||
public void verifyLightsXmlFileNameIsValid() { | ||
ArrayList<String> invalidFileNames = new ArrayList<>(); | ||
|
||
for (File xmlFile : getCustomLightsXmlFiles()) { | ||
if (!xmlFile.getName().matches("^Lights_[a-zA-Z0-9_-]{0,25}.xml$")) { | ||
invalidFileNames.add(xmlFile.getName()); | ||
} | ||
} | ||
|
||
if (invalidFileNames.size() > 0) { | ||
Assert.fail(String.format("%s are not a valid file name(s) the file name must start with 'Lights_', " + | ||
"can only be a maximum of 32 characters and contain letters, numbers, " + | ||
"underscores and dashes.", invalidFileNames.toString())); | ||
} | ||
} | ||
|
||
@Test | ||
public void verifyLightsTuningXsdFFileExists() { | ||
for (File xmlFile : getCustomLightsXmlFiles()) { | ||
Assert.assertTrue(Files.exists(Paths.get(path, "Metadata", "Schema", "Lights_Tuning.xsd")), | ||
"If a custom Lights_xxx.xml exists there should be Lights_Tuning.xsd in the Schema folder."); | ||
} | ||
} | ||
|
||
@Test | ||
public void verifyLightsXxxXmlAgainstSchema() throws IOException, SAXException { | ||
for (File xmlFile : getCustomLightsXmlFiles()) { | ||
File xsdFile = Paths.get(path, "Metadata", "Schema", "Lights_Tuning.xsd").toFile(); | ||
|
||
SchemaValidatorErrorHandler errorHandler = XmlUtilities.validateXmlFileIsValid(xmlFile, xsdFile); | ||
|
||
if (!errorHandler.noErrors()) { | ||
Assert.fail(xmlFile.getName() + " does not contain valid XML. Errors: " + errorHandler.getMessages()); | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
public void verifyLightsXxxXmlDirectionalityValueIsValid() { | ||
for (File xmlFile : getCustomLightsXmlFiles()) { | ||
ArrayList<String> directionalityValues = getDirectionalityValues(xmlFile); | ||
|
||
for (String value : directionalityValues) { | ||
Assert.assertTrue(DIRECTIONALITY_VALUES.contains(value), | ||
String.format("'%s' element Directionality should have a value of 'Omnidirectional', " + | ||
"'Directional' or 'Bidirectional'. Value '%s' is not valid.", xmlFile.getName(), value)); | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
public void verifyLightsXxxXmlElementIntensityIsInRange() { | ||
for (File xmlFile : getCustomLightsXmlFiles()) { | ||
ArrayList<String> invalidIntensityValues = getInvalidPercentageValues(xmlFile, "//Intensity"); | ||
|
||
Assert.assertEquals(invalidIntensityValues.size(), 0, | ||
String.format("'%s' Intensity elements value can range from 0.0 to 1.0. Values %s are not valid.", | ||
xmlFile.getName(), invalidIntensityValues.toString())); | ||
} | ||
} | ||
|
||
@Test | ||
public void verifyLightsXxxXmlElementResidualIntensityIsInRange() { | ||
for (File xmlFile : getCustomLightsXmlFiles()) { | ||
ArrayList<String> invalidResidualIntensityValues = getInvalidPercentageValues(xmlFile, "//Residual_Intensity"); | ||
|
||
Assert.assertEquals(invalidResidualIntensityValues.size(), 0, | ||
String.format("'%s' Residual_Intensity elements value can range from 0.0 to 1.0. Values %s are not valid.", | ||
xmlFile.getName(), invalidResidualIntensityValues.toString())); | ||
} | ||
} | ||
|
||
@Test | ||
public void verifyLightsXxxXmlElementDuty_CycleIsInRange() { | ||
for (File xmlFile : getCustomLightsXmlFiles()) { | ||
ArrayList<String> invalidResidualIntensityValues = getInvalidPercentageValues(xmlFile, "//Duty_Cycle"); | ||
|
||
Assert.assertEquals(invalidResidualIntensityValues.size(), 0, | ||
String.format("'%s' Duty_Cycle elements value can range from 0.0 to 1.0. Values %s are not valid.", | ||
xmlFile.getName(), invalidResidualIntensityValues.toString())); | ||
} | ||
} | ||
|
||
@Test | ||
public void verifyLightsXxxXmlFrequencyValueIsValid() { | ||
for (File xmlFile : getCustomLightsXmlFiles()) { | ||
ArrayList<String> invalidFrequencyValues = getInvalidFrequencyValues(xmlFile); | ||
|
||
Assert.assertEquals(invalidFrequencyValues.size(), 0, | ||
String.format("'%s' Duty_Cycle elements value can range from 0.0 to 1.0. Values %s are not valid.", | ||
xmlFile.getName(), invalidFrequencyValues.toString())); | ||
} | ||
} | ||
|
||
@Test | ||
public void verifyLightsXxxXmlColorIsInRange() { | ||
for (File xmlFile : getCustomLightsXmlFiles()) { | ||
ArrayList<String> invalidColorValues = getInvalidColorValues(xmlFile); | ||
|
||
Assert.assertEquals(invalidColorValues.size(), 0, | ||
String.format("'%s' Duty_Cycle elements value can range from 0.0 to 1.0. Values %s are not valid.", | ||
xmlFile.getName(), invalidColorValues.toString())); | ||
} | ||
} | ||
|
||
private List<File> getCustomLightsXmlFiles() { | ||
String glob = "glob:Lights_*.xml"; | ||
|
||
final PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher(glob); | ||
|
||
List<File> lightsXmlFiles = new ArrayList<>(); | ||
|
||
try { | ||
DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get(path, "Metadata")); | ||
for (Path entry : stream) { | ||
if (pathMatcher.matches(entry.getFileName())) { | ||
lightsXmlFiles.add(entry.toFile()); | ||
} | ||
} | ||
stream.close(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
return lightsXmlFiles; | ||
} | ||
|
||
private boolean valueIsOutOfRange(Float value) { | ||
return value < 0.0 || value > 1.0; | ||
} | ||
|
||
private ArrayList<String> getDirectionalityValues(File xmlFile) { | ||
NodeList nodeList = XmlUtilities.getNodeList("//Directionality", Paths.get(path, "Metadata", xmlFile.getName())); | ||
|
||
ArrayList<String> directionalityValues = new ArrayList<>(); | ||
|
||
for (int i = 0; i < nodeList.getLength(); i++) { | ||
Node currentItem = nodeList.item(i); | ||
directionalityValues.add(currentItem.getTextContent()); | ||
} | ||
return directionalityValues; | ||
} | ||
|
||
private ArrayList<String> getInvalidColorValues(File xmlFile) { | ||
NodeList frequencyNodes = XmlUtilities.getNodeList("//Color", Paths.get(path, "Metadata", xmlFile.getName())); | ||
|
||
ArrayList<String> invalidColorValues = new ArrayList<>(); | ||
|
||
for (int i = 0; i < frequencyNodes.getLength(); i++) { | ||
Node currentItem = frequencyNodes.item(i); | ||
String[] values = currentItem.getTextContent().split("\\s+"); | ||
|
||
for (String value : values) { | ||
Float floatValue = Float.parseFloat(value); | ||
if (valueIsOutOfRange(floatValue)) { | ||
invalidColorValues.add(floatValue.toString()); | ||
} | ||
} | ||
} | ||
return invalidColorValues; | ||
} | ||
|
||
private ArrayList<String> getInvalidPercentageValues(File xmlFile, String nodeToSearchFor) { | ||
NodeList nodes = XmlUtilities.getNodeList(nodeToSearchFor, Paths.get(path, "Metadata", xmlFile.getName())); | ||
|
||
ArrayList<String> invalidValues = new ArrayList<>(); | ||
|
||
for (int i = 0; i < nodes.getLength(); i++) { | ||
Node currentItem = nodes.item(i); | ||
Float value = Float.parseFloat(currentItem.getTextContent()); | ||
|
||
if (valueIsOutOfRange(value)) { | ||
invalidValues.add(currentItem.getTextContent()); | ||
} | ||
} | ||
return invalidValues; | ||
} | ||
|
||
private ArrayList<String> getInvalidFrequencyValues(File xmlFile) { | ||
NodeList frequencyNodes = XmlUtilities.getNodeList("//Frequency", Paths.get(path, "Metadata", xmlFile.getName())); | ||
|
||
ArrayList<String> invalidFrequencyValues = new ArrayList<>(); | ||
|
||
for (int i = 0; i < frequencyNodes.getLength(); i++) { | ||
Node currentItem = frequencyNodes.item(i); | ||
Float value = Float.parseFloat(currentItem.getTextContent()); | ||
|
||
if (value < 0.0) { | ||
invalidFrequencyValues.add(currentItem.getTextContent()); | ||
} | ||
} | ||
return invalidFrequencyValues; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/test/java/org/opengis/cite/cdb10/fixtures/invalid/Lights_ClientInvalid.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?xml version="1.0"?> | ||
<Lights_Tuning xmlns="CDB" xmlns:CDB="CDB" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="CDB Schema/Lights_Tuning.xsd"> | ||
<Light type="\Light\Platform\Air\Aircraft_Helos\Anti-collision"> | ||
<Description>Tuned for MH-47 CMS</Description> | ||
<Intensity>0.75</Intensity> | ||
<Color>1.0 0.0 0.0</Color> | ||
<Directionality>Omnidirectional</Directionality> | ||
<Frequency>0.5</Frequency> | ||
<Duty_Cycle>0.2</Duty_Cycle> | ||
</Light> | ||
<Light type="\Light\Platform\Air\Aircraft_Helos\Landing"> | ||
<Description>...</Description> | ||
<Intensity>1.07</Intensity> | ||
<Color>1.0 0.9 0.9</Color> | ||
<Directionality>Invalid</Directionality> | ||
<Residual_Intensity>0.05</Residual_Intensity> | ||
<Lobe_Width> | ||
<Horizontal>1.1</Horizontal> | ||
<Vertical>1.5</Vertical> | ||
</Lobe_Width> | ||
</Light> | ||
</Lights_Tuning> |
12 changes: 12 additions & 0 deletions
12
...va/org/opengis/cite/cdb10/fixtures/invalid/Lights_ClientInvalidColorValueIsOutOfRange.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?xml version="1.0"?> | ||
<Lights_Tuning xmlns="CDB" xmlns:CDB="CDB" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="CDB Schema/Lights_Tuning.xsd"> | ||
<Light type="\Light\Platform\Air\Aircraft_Helos\Anti-collision"> | ||
<Description>Tuned for MH-47 CMS</Description> | ||
<Intensity>0.75</Intensity> | ||
<Color>1.1 0.0 -0.1</Color> | ||
<Directionality>Omnidirectional</Directionality> | ||
<Frequency>0.5</Frequency> | ||
<Duty_Cycle>0.2</Duty_Cycle> | ||
</Light> | ||
</Lights_Tuning> |
12 changes: 12 additions & 0 deletions
12
.../java/org/opengis/cite/cdb10/fixtures/invalid/Lights_ClientInvalidDirectionalityValue.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?xml version="1.0"?> | ||
<Lights_Tuning xmlns="CDB" xmlns:CDB="CDB" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="CDB Schema/Lights_Tuning.xsd"> | ||
<Light type="\Light\Platform\Air\Aircraft_Helos\Anti-collision"> | ||
<Description>Tuned for MH-47 CMS</Description> | ||
<Intensity>0.75</Intensity> | ||
<Color>1.0 0.0 0.0</Color> | ||
<Directionality>INVALID_VALUE</Directionality> | ||
<Frequency>0.5</Frequency> | ||
<Duty_Cycle>0.2</Duty_Cycle> | ||
</Light> | ||
</Lights_Tuning> |
24 changes: 24 additions & 0 deletions
24
...java/org/opengis/cite/cdb10/fixtures/invalid/Lights_ClientInvalidDuty_CycleOutOfRange.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?xml version="1.0"?> | ||
<Lights_Tuning xmlns="CDB" xmlns:CDB="CDB" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="CDB Schema/Lights_Tuning.xsd"> | ||
<Light type="\Light\Platform\Air\Aircraft_Helos\Anti-collision"> | ||
<Description>Tuned for MH-47 CMS</Description> | ||
<Intensity>0.75</Intensity> | ||
<Color>1.0 0.0 0.0</Color> | ||
<Directionality>Omnidirectional</Directionality> | ||
<Frequency>0.5</Frequency> | ||
<Duty_Cycle>-0.1</Duty_Cycle> | ||
</Light> | ||
<Light type="\Light\Platform\Air\Aircraft_Helos\Landing"> | ||
<Description>...</Description> | ||
<Intensity>1.0</Intensity> | ||
<Color>1.0 0.9 0.9</Color> | ||
<Directionality>Directional</Directionality> | ||
<Residual_Intensity>0.05</Residual_Intensity> | ||
<Duty_Cycle>1.01</Duty_Cycle> | ||
<Lobe_Width> | ||
<Horizontal>1.1</Horizontal> | ||
<Vertical>1.5</Vertical> | ||
</Lobe_Width> | ||
</Light> | ||
</Lights_Tuning> |
12 changes: 12 additions & 0 deletions
12
src/test/java/org/opengis/cite/cdb10/fixtures/invalid/Lights_ClientInvalidFrequencyValue.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?xml version="1.0"?> | ||
<Lights_Tuning xmlns="CDB" xmlns:CDB="CDB" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="CDB Schema/Lights_Tuning.xsd"> | ||
<Light type="\Light\Platform\Air\Aircraft_Helos\Anti-collision"> | ||
<Description>Tuned for MH-47 CMS</Description> | ||
<Intensity>0.75</Intensity> | ||
<Color>1.0 0.0 0.0</Color> | ||
<Directionality>Omnidirectional</Directionality> | ||
<Frequency>-0.1</Frequency> | ||
<Duty_Cycle>0.2</Duty_Cycle> | ||
</Light> | ||
</Lights_Tuning> |
23 changes: 23 additions & 0 deletions
23
.../java/org/opengis/cite/cdb10/fixtures/invalid/Lights_ClientInvalidIntensityOutOfRange.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?xml version="1.0"?> | ||
<Lights_Tuning xmlns="CDB" xmlns:CDB="CDB" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="CDB Schema/Lights_Tuning.xsd"> | ||
<Light type="\Light\Platform\Air\Aircraft_Helos\Anti-collision"> | ||
<Description>Tuned for MH-47 CMS</Description> | ||
<Intensity>-0.1</Intensity> | ||
<Color>1.0 0.0 0.0</Color> | ||
<Directionality>Omnidirectional</Directionality> | ||
<Frequency>0.5</Frequency> | ||
<Duty_Cycle>0.2</Duty_Cycle> | ||
</Light> | ||
<Light type="\Light\Platform\Air\Aircraft_Helos\Landing"> | ||
<Description>...</Description> | ||
<Intensity>1.01</Intensity> | ||
<Color>1.0 0.9 0.9</Color> | ||
<Directionality>Directional</Directionality> | ||
<Residual_Intensity>0.05</Residual_Intensity> | ||
<Lobe_Width> | ||
<Horizontal>1.1</Horizontal> | ||
<Vertical>1.5</Vertical> | ||
</Lobe_Width> | ||
</Light> | ||
</Lights_Tuning> |
24 changes: 24 additions & 0 deletions
24
.../opengis/cite/cdb10/fixtures/invalid/Lights_ClientInvalidResidual_IntensityOutOfRange.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?xml version="1.0"?> | ||
<Lights_Tuning xmlns="CDB" xmlns:CDB="CDB" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="CDB Schema/Lights_Tuning.xsd"> | ||
<Light type="\Light\Platform\Air\Aircraft_Helos\Anti-collision"> | ||
<Description>Tuned for MH-47 CMS</Description> | ||
<Intensity>0.75</Intensity> | ||
<Color>1.0 0.0 0.0</Color> | ||
<Directionality>Omnidirectional</Directionality> | ||
<Residual_Intensity>-0.1</Residual_Intensity> | ||
<Frequency>0.5</Frequency> | ||
<Duty_Cycle>0.2</Duty_Cycle> | ||
</Light> | ||
<Light type="\Light\Platform\Air\Aircraft_Helos\Landing"> | ||
<Description>...</Description> | ||
<Intensity>1.0</Intensity> | ||
<Color>1.0 0.9 0.9</Color> | ||
<Directionality>Directional</Directionality> | ||
<Residual_Intensity>1.01</Residual_Intensity> | ||
<Lobe_Width> | ||
<Horizontal>1.1</Horizontal> | ||
<Vertical>1.5</Vertical> | ||
</Lobe_Width> | ||
</Light> | ||
</Lights_Tuning> |
Oops, something went wrong.