Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw ERROR when file areas point at same file #769

Merged
merged 6 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,9 @@ public class FileReferencedMapList {
// Class to hold a map of every labels that referenced a physical file.
// If a file is referenced by more than one label, an error should be thrown.
class FileReferencedMap {
private URL url = null;
private List<String> labelNames = new ArrayList<>();

public FileReferencedMap(URL url) {
this.url = url;
public FileReferencedMap() {
}

public void setLabels(String labelName) {
Expand Down Expand Up @@ -86,7 +84,7 @@ public FileReferencedMap setLabels(URL url, String labelName) {
} else {
// If fileReferencedMaps does not contain an existing, url, create a new
// FileReferencedMap and add labelName to the new empty list.
fileReferencedMap = new FileReferencedMap(url);
fileReferencedMap = new FileReferencedMap();
fileReferencedMap.setLabels(labelName);
fileReferencedMaps.put(url, fileReferencedMap);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package gov.nasa.pds.tools.validate;

import java.io.IOException;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.dom.DOMSource;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import gov.nasa.pds.tools.util.LabelUtil;

public class CrossLabelFileAreaReferenceChecker {
final private static HashMap<String,List<String>> knownRefs = new HashMap<String,List<String>>();
private static String resolve (String name, ValidationTarget target) {
if (!name.startsWith ("/")) {
name = Path.of(target.getUrl().getPath()).getParent().resolve(name).toString();
}
return name;
}
/**
* @param name - the file being referenced by the file area
* @param target - the label being validated
* @return true if name and target are unique and only known references and false otherwise
* All of these exceptions should not happen because they are getting the LID from the target.
* Would not have made it this far if that could not have happened already, So, just pass them
* back to the called and let them handle it with their own generic exception handler/message.
* @throws IOException
* @throws ParserConfigurationException
* @throws SAXException
*/
public static boolean add (String name, ValidationTarget target) throws IOException, ParserConfigurationException, SAXException {
boolean success = false;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
Document xml = dbf.newDocumentBuilder().parse(target.getUrl().openStream());
DOMSource domSource = new DOMSource(xml);
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()));
success = true;
} else {
success = false;
}
}
return success;
}
public static String getOtherId (String name, ValidationTarget target) {
return knownRefs.get(resolve(name, target)).get(0);
}
public static String getOtherFilename (String name, ValidationTarget target) {
return knownRefs.get(resolve(name, target)).get(1);
}
public static void reset() {
knownRefs.clear();
}
}
2 changes: 2 additions & 0 deletions src/main/java/gov/nasa/pds/tools/validate/ProblemType.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public enum ProblemType {

MISSING_REFERENCED_FILE("error.label.missing_file"),

DUPLICATED_FILE_AREA_REFERENCE("error.label.file_areas_duplicated_reference"),

INVALID_LABEL("error.validation.invalid_label"),

EMPTY_FOLDER("error.validation.empty_folder", ProblemCategory.EXECUTION),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import gov.nasa.pds.tools.util.PDFUtil;
import gov.nasa.pds.tools.util.Utility;
import gov.nasa.pds.tools.util.XMLExtractor;
import gov.nasa.pds.tools.validate.CrossLabelFileAreaReferenceChecker;
import gov.nasa.pds.tools.validate.ProblemDefinition;
import gov.nasa.pds.tools.validate.ProblemType;
import gov.nasa.pds.tools.validate.ValidationProblem;
Expand Down Expand Up @@ -280,6 +281,15 @@ private boolean validate(NodeInfo xml) {
name = child.getStringValue();
this.fileMapping.put(name, "");
LOG.debug("FileReferenceValidationRule:validate:name {}", name);
if (!CrossLabelFileAreaReferenceChecker.add (name, target)) {
this.getListener().addProblem(
new ValidationProblem(
new ProblemDefinition(ExceptionType.ERROR, ProblemType.DUPLICATED_FILE_AREA_REFERENCE,
"This file area references " + name + " that is already used by label "
+ CrossLabelFileAreaReferenceChecker.getOtherId(name, target)
+ " in file " + CrossLabelFileAreaReferenceChecker.getOtherFilename(name, target)),
target, fileObject.getLineNumber(), -1));
};
} else if ("md5_checksum".equals(child.getLocalPart())) {
checksum = child.getStringValue();
LOG.debug("FileReferenceValidationRule:validate:checksum {}", checksum);
Expand Down
3 changes: 3 additions & 0 deletions src/test/java/cucumber/StepDefs.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.slf4j.LoggerFactory;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import gov.nasa.pds.tools.validate.CrossLabelFileAreaReferenceChecker;
import gov.nasa.pds.tools.validate.ProblemType;
import gov.nasa.pds.validate.ValidateLauncher;
import gov.nasa.pds.validate.constants.TestConstants;
Expand Down Expand Up @@ -78,6 +79,8 @@ void tearDown() throws Exception {
this.launcher.flushValidators();
// It seems the launcher does not completely flush any references to schematron
// which causes problem for subsequent tests.
// get rid of the cross references
CrossLabelFileAreaReferenceChecker.reset();
}

private void createManifestFileDo(String testPath) {
Expand Down
6 changes: 5 additions & 1 deletion src/test/resources/features/developer.feature
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ Scenario Outline: Execute validate command for tests below.
Examples:
| testName | testDir | messageCount | messageText | problemEnum | resourceDir | reportDir | commandArgs | refOutputValue |

# Validate#755
|"NASA-PDS/validate#755 Detect and report file areas referencing same file" | "github755" | 2 | "2 errors expected" | "DUPLICATED_FILE_AREA_REFERENCE" | "src/test/resources" | "target/test" | "-r {reportDir}/report_github755.json -s json -skip-content-validation -t {resourceDir}/github755/m221011.0013.xml {resourceDir}/github755/m221011.0014.xml {resourceDir}/github755/m221011.0015.xml {resourceDir}/github755/m221011.0030.xml" | "report_github755.json" |
|"NASA-PDS/validate#755 No common file referenced" | "github755" | 0 | "0 errors expected" | "totalErrors" | "src/test/resources" | "target/test" | "-r {reportDir}/report_github755.1.json -s json -skip-content-validation -t {resourceDir}/github755/m221011.0013.xml {resourceDir}/github755/m221011.0015.xml" | "report_github755.1.json" |

# Validate#754
|"NASA-PDS/validate#754 Success differentiating file area offsets" | "github754" | 0 | "1 warnings expected" | "DATA_OBJECTS_OUT_OF_ORDER" | "src/test/resources" | "target/test" | "-r {reportDir}/report_github754.json -s json --skip-context-validation -t {resourceDir}/github754/Cassini_ISS_CB2_Jupiter_global_map_2.xml" | "report_github754.json" |

Expand Down Expand Up @@ -263,7 +267,7 @@ Scenario Outline: Execute validate command for tests below.
# https://github.com/NASA-PDS/validate/issues/188 As a user, I want to validate a bundle that uses multiple versions of the Information Model / Discipline LDDs
|"NASA-PDS/validate#188 VALID" | "github188" | 0 | "0 errors message expected" | "totalErrors" | "src/test/resources" | "target/test" | "--skip-content-validation -r {reportDir}/report_github188_label_valid_both.json -s json -t {resourceDir}/github188/bundle_cassini-huygens-coradar.xml {resourceDir}/github188/BILQH07S314_D065_T008S02_V02_without_Missing_Area_tag.xml" | "report_github188_label_valid_both.json" |

|"NASA-PDS/validate#217 Success with Errors: Tables Types" | "github217" | 15 | "15 error message expected" | "totalErrors" | "src/test/resources" | "target/test" | "-r {reportDir}/report_github217.json -s json -t {resourceDir}/github217" | "report_github217.json" |
|"NASA-PDS/validate#217 Success with Errors: Tables Types" | "github217" | 65 | "65 error message expected" | "totalErrors" | "src/test/resources" | "target/test" | "-r {reportDir}/report_github217.json -s json -t {resourceDir}/github217" | "report_github217.json" |

# https://github.com/NASA-PDS/validate/issues/210 As a user, I want validate to raise a WARNING when differing versions of IM are used within a bundle
|"NASA-PDS/validate#210 WITH_WARNING" | "github210" | 1 | "1 warning message expected" | "totalWarnings" | "src/test/resources" | "target/test" | "--skip-content-validation -r {reportDir}/report_github210_label_valid_both_with_warning.json -s json -t {resourceDir}/github210/bundle_cassini-huygens-coradar.xml {resourceDir}/github210/BILQH07S314_D065_T008S02_V02_without_Missing_Area_tag.xml" | "report_github210_label_valid_both_with_warning.json" |
Expand Down
Binary file added src/test/resources/github755/comps_221011.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
203 changes: 203 additions & 0 deletions src/test/resources/github755/m221011.0013.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml-model
href="https://pds.nasa.gov/pds4/pds/v1/PDS4_PDS_1E00.sch"
schematypens="http://purl.oclc.org/dsdl/schematron"?>
<?xml-model
href="https://pds.nasa.gov/pds4/disp/v1/PDS4_DISP_1B00.sch"
schematypens="http://purl.oclc.org/dsdl/schematron"?>
<?xml-model
href="https://pds.nasa.gov/pds4/img/v1/PDS4_IMG_1E00_1800.sch"
schematypens="http://purl.oclc.org/dsdl/schematron"?>
<?xml-model
href="https://pds.nasa.gov/pds4/geom/v1/PDS4_GEOM_1E00_1810.sch"
schematypens="http://purl.oclc.org/dsdl/schematron"?>
<Product_Observational
xmlns="http://pds.nasa.gov/pds4/pds/v1"
xmlns:disp="http://pds.nasa.gov/pds4/disp/v1"
xmlns:img="http://pds.nasa.gov/pds4/img/v1"
xmlns:geom="http://pds.nasa.gov/pds4/geom/v1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pds.nasa.gov/pds4/pds/v1
https://pds.nasa.gov/pds4/pds/v1/PDS4_PDS_1E00.xsd
http://pds.nasa.gov/pds4/disp/v1
https://pds.nasa.gov/pds4/disp/v1/PDS4_DISP_1B00.xsd
http://pds.nasa.gov/pds4/img/v1
https://pds.nasa.gov/pds4/img/v1/PDS4_IMG_1E00_1800.xsd
http://pds.nasa.gov/pds4/geom/v1
https://pds.nasa.gov/pds4/geom/v1/PDS4_GEOM_1E00_1810.xsd">
<Identification_Area>
<logical_identifier>urn:nasa:pds:dart_teleobs:data_mroraw:m221011.0013</logical_identifier>
<version_id>1.0</version_id>
<title>Magdalena Ridge Observatory 2.4-meter Telescope Raw Image: m221011.0013</title>
<information_model_version>1.14.0.0</information_model_version>
<product_class>Product_Observational</product_class>
<Modification_History>
<Modification_Detail>
<modification_date>2023-08-01</modification_date>
<version_id>1.0</version_id>
<description>Initial PDS4 Delivery</description>
</Modification_Detail>
</Modification_History>
</Identification_Area>
<Observation_Area>
<Time_Coordinates>
<start_date_time>2022-10-11T08:49:36.32Z</start_date_time>
<stop_date_time>2022-10-11T08:49:51.32Z</stop_date_time>
</Time_Coordinates>
<Investigation_Area>
<name>Double Asteroid Redirection Test</name>
<type>Mission</type>
<Internal_Reference>
<lid_reference>urn:nasa:pds:context:investigation:mission.double_asteroid_redirection_test</lid_reference>
<reference_type>data_to_investigation</reference_type>
</Internal_Reference>
</Investigation_Area>
<Observing_System>
<Observing_System_Component>
<name>Magdalena Ridge Observatory (MRO)</name>
<type>Host</type>
<Internal_Reference>
<lid_reference>urn:nasa:pds:context:facility:observatory.magdalena_ridge</lid_reference>
<reference_type>is_facility</reference_type>
</Internal_Reference>
</Observing_System_Component>
<Observing_System_Component>
<name>MRO 2.4-meter Telescope</name>
<type>Telescope</type>
<description>
The MRO 2.4-meter Telescope is a Ritchey-Chretien telescope with a 2.4-meter mirror located in the
Cibola National Forest in New Mexico. It is owned and operated by New Mexico Institute of Mining and
Technology.
</description>
<Internal_Reference>
<lid_reference>urn:nasa:pds:context:telescope:magdalena_ridge.mro2m4</lid_reference>
<reference_type>is_telescope</reference_type>
</Internal_Reference>
</Observing_System_Component>
<Observing_System_Component>
<name>MRO2K</name>
<type>Instrument</type>
<description>
MRO2K, the visible-wavelength direct-imaging CCD for the MRO 2.4-meter Telescope, is an Andor iKon-L 936.
It is a 2048 x 2048 array delivering a 27.6 x 27.6 mm active image area. The CCD images are a 4.5 arc
minute square field of view with 13.5-micron pixels.
</description>
<Internal_Reference>
<lid_reference>urn:nasa:pds:context:instrument:magdalena_ridge.mro2m4.andor_2kx2k_ccd</lid_reference>
<reference_type>is_instrument</reference_type>
</Internal_Reference>
</Observing_System_Component>
</Observing_System>
<Target_Identification>
<name>Sky</name>
<type>Calibration Field</type>
</Target_Identification>
<Discipline_Area>
<disp:Display_Settings>
<Local_Internal_Reference>
<local_identifier_reference>m221011.0013</local_identifier_reference>
<local_reference_type>display_settings_to_array</local_reference_type>
</Local_Internal_Reference>
<disp:Display_Direction>
<disp:horizontal_display_axis>Sample</disp:horizontal_display_axis>
<disp:horizontal_display_direction>Left to Right</disp:horizontal_display_direction>
<disp:vertical_display_axis>Line</disp:vertical_display_axis>
<disp:vertical_display_direction>Bottom to Top</disp:vertical_display_direction>
</disp:Display_Direction>
</disp:Display_Settings>
<img:Exposure>
<img:exposure_duration unit="s">15.0</img:exposure_duration>
</img:Exposure>
<img:Optical_Filter>
<img:filter_name>R</img:filter_name>
<img:bandwidth unit="nm">158</img:bandwidth>
<img:center_filter_wavelength unit="nm">640</img:center_filter_wavelength>
</img:Optical_Filter>
<geom:Geometry>
<geom:Image_Display_Geometry>
<Local_Internal_Reference>
<local_identifier_reference>m221011.0013</local_identifier_reference>
<local_reference_type>display_to_data_object</local_reference_type>
</Local_Internal_Reference>
<geom:Display_Direction>
<geom:horizontal_display_axis>Sample</geom:horizontal_display_axis>
<geom:horizontal_display_direction>Left to Right</geom:horizontal_display_direction>
<geom:vertical_display_axis>Line</geom:vertical_display_axis>
<geom:vertical_display_direction>Bottom to Top</geom:vertical_display_direction>
</geom:Display_Direction>
<geom:Object_Orientation_RA_Dec>
<geom:right_ascension_angle unit="deg">89.60996</geom:right_ascension_angle>
<geom:declination_angle unit="deg">-16.43276</geom:declination_angle>
<geom:celestial_north_clock_angle unit="deg">0</geom:celestial_north_clock_angle>
<geom:Reference_Frame_Identification>
<geom:name>J2000</geom:name>
<geom:comment>equinox of RA and DEC</geom:comment>
</geom:Reference_Frame_Identification>
</geom:Object_Orientation_RA_Dec>
</geom:Image_Display_Geometry>
</geom:Geometry>
</Discipline_Area>
</Observation_Area>
<Reference_List>
<Internal_Reference>
<lid_reference>urn:nasa:pds:dart_teleobs:document_mro:mro_2p4m_dart_pds_sis</lid_reference>
<reference_type>data_to_document</reference_type>
<comment>
MRO DART Uncalibrated/Calibrated Data Product Software Interface Specification document which describes the data products in this collection.
</comment>
</Internal_Reference>
<Internal_Reference>
<lid_reference>urn:nasa:pds:dart_teleobs:document_mro:mro_description</lid_reference>
<reference_type>data_to_document</reference_type>
<comment>Overview of the MRO 2.4-meter Telescope.</comment>
</Internal_Reference>
<Internal_Reference>
<lid_reference>urn:nasa:pds:dart_teleobs:document_mro:mro2k_description</lid_reference>
<reference_type>data_to_document</reference_type>
<comment>Short description of the MRO2K instrument.</comment>
</Internal_Reference>
</Reference_List>
<File_Area_Observational>
<File>
<file_name>m221011.0013.fits</file_name>
<comment>Raw MRO image file</comment>
</File>
<Header>
<offset unit="byte">0</offset>
<object_length unit="byte">5760</object_length>
<parsing_standard_id>FITS 3.0</parsing_standard_id>
<description>Fits header.</description>
</Header>
<Array_2D_Image>
<local_identifier>m221011.0013</local_identifier>
<offset unit="byte">5760</offset>
<axes>2</axes>
<axis_index_order>Last Index Fastest</axis_index_order>
<Element_Array>
<data_type>SignedMSB2</data_type>
<unit>DN</unit>
<value_offset>32768.0</value_offset>
</Element_Array>
<Axis_Array>
<axis_name>Line</axis_name>
<elements>512</elements>
<sequence_number>1</sequence_number>
</Axis_Array>
<Axis_Array>
<axis_name>Sample</axis_name>
<elements>512</elements>
<sequence_number>2</sequence_number>
</Axis_Array>
</Array_2D_Image>
</File_Area_Observational>
<File_Area_Observational_Supplemental>
<File>
<file_name>comps_221011.png</file_name>
<comment>Identifies the reference stars used in the photometric reduction.</comment>
</File>
<Encoded_Image>
<offset unit="byte">0</offset>
<encoding_standard_id>PNG</encoding_standard_id>
</Encoded_Image>
</File_Area_Observational_Supplemental>
</Product_Observational>
Binary file not shown.
Loading