Skip to content

Commit

Permalink
add unit test first
Browse files Browse the repository at this point in the history
  • Loading branch information
Al Niessner authored and Al Niessner committed May 30, 2024
1 parent c8a5d55 commit a23224b
Show file tree
Hide file tree
Showing 14 changed files with 3,787 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import gov.nasa.pds.tools.util.LabelUtil;

public class CrossLabelFileAreaReferenceChecker {
final private static HashMap<String,Boolean> isObservational = new HashMap<String,Boolean>();
final private static HashMap<String,List<String>> knownRefs = new HashMap<String,List<String>>();
private static String resolve (String name, ValidationTarget target) throws URISyntaxException {
if (!name.startsWith ("/")) {
Expand All @@ -33,20 +34,24 @@ private static String resolve (String name, ValidationTarget target) throws URIS
* @throws SAXException
* @throws URISyntaxException
*/
public static boolean add (String name, ValidationTarget target) throws IOException, ParserConfigurationException, SAXException, URISyntaxException {
public static boolean add (String name, ValidationTarget target, boolean isObs)
throws IOException, ParserConfigurationException, SAXException, URISyntaxException {
boolean success = false;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
Document xml = dbf.newDocumentBuilder().parse(target.getUrl().openStream());
DOMSource domSource = new DOMSource(xml);
DOMSource domSource = new DOMSource(xml);
String full_name = resolve(name, target);
isObservational.put(full_name,
(isObservational.containsKey(full_name) ? isObservational.get(full_name) : false) | isObs);
for (String lid : LabelUtil.getLogicalIdentifiers (domSource, target.getUrl())) {
if (lid.contains("::")) {
lid = lid.substring (0, lid.indexOf("::"));
}
if (!knownRefs.keySet().contains (resolve(name, target))) {
knownRefs.put(resolve(name, target), (List<String>)Arrays.asList(lid, target.getUrl().getPath()));
if (!knownRefs.keySet().contains (full_name)) {
knownRefs.put(full_name, (List<String>)Arrays.asList(lid, target.getUrl().getPath()));
success = true;
} else {
success = false;
success = !isObservational.get(full_name);
}
}
return success;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -73,7 +75,8 @@ public class FileReferenceValidationRule extends AbstractValidationRule {
*/
private final String FILE_AREA_OBJECTS_XPATH =
"//*[starts-with(name(), 'File_Area')] | //Document_File";

private final HashSet<String> OBS_DATA_TAGS = new HashSet<String>(Arrays.asList(
"Product_Observational", "Observation_Area", "File_Area_Observational", "File_Area_Observational_Supplemental"));
private Map<URL, String> checksumManifest;
private PDFUtil pdfUtil = null; // Define pdfUtil so we can reuse it for every call to
// validateFileReferences()
Expand Down Expand Up @@ -323,8 +326,14 @@ private boolean validate(NodeInfo xml) {
} // for (TinyNodeImpl child : children)

if (!name.isBlank()) {
boolean isObservational = OBS_DATA_TAGS.contains(fileAreaObject.getLocalPart());
NodeInfo ancestor = fileAreaObject;
String fullName = directory.isBlank() ? name : String.join(File.pathSeparator, directory, name);
if (!CrossLabelFileAreaReferenceChecker.add (fullName, target)) {
while (!isObservational && !fileAreaObject.getRoot().isSameNodeInfo(ancestor)) {
ancestor = ancestor.getParent();
isObservational |= OBS_DATA_TAGS.contains(ancestor.getLocalPart());
}
if (!CrossLabelFileAreaReferenceChecker.add (fullName, target, isObservational)) {
this.getListener().addProblem(
new ValidationProblem(
new ProblemDefinition(ExceptionType.ERROR, ProblemType.DUPLICATED_FILE_AREA_REFERENCE,
Expand Down
3 changes: 3 additions & 0 deletions src/test/resources/features/developer.feature
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ Scenario Outline: Execute validate command for tests below.
Examples:
| testName | testDir | messageCount | messageText | problemEnum | resourceDir | reportDir | commandArgs | refOutputValue |

# Validate#905
|"NASA-PDS/validate#905 Success no duplicates in non-observational;" | "github905" | 0 | "0 errors expected" | "totalErrors" | "src/test/resources" | "target/test" | "-r {reportDir}/report_github905.json -s json --skip-context-validation -R pds4.bundle -t {resourceDir}/github905/dsn_0159-science.2008-02-29.xml {resourceDir}/github905/dsn_0159-science.2009-05-18.xml" | "report_github905.json" |

# Validate#873
|"NASA-PDS/validate#873 Success files same name different paths" | "github873" | 0 | "0 errors expected" | "totalErrors" | "src/test/resources" | "target/test" | "-r {reportDir}/report_github873.json -s json --skip-context-validation -R pds4.bundle -t {resourceDir}/github873" | "report_github873.json" |

Expand Down
Loading

0 comments on commit a23224b

Please sign in to comment.