Skip to content

Commit 4631813

Browse files
Update JSON and XML tests to 1.6. Update cyclonedx.core.java to 9.0.2. Starting prototyping the XML Catalog tests
Signed-off-by: Nicolas-Peiffer <[email protected]>
2 parents 5ba4e65 + 02a1a06 commit 4631813

File tree

6 files changed

+172
-24
lines changed

6 files changed

+172
-24
lines changed

schema/xmlcatalog.xml

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,41 @@
11
<?xml version="1.0"?>
2+
<!--
3+
CycloneDX Software Bill-of-Material (SBoM) Specification
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<!--
18+
This XML catalog provides mappings for CycloneDX schemas.
19+
The catalog maps schema URLs to local XSD files to facilitate schema
20+
validation without needing internet access.
21+
Namespace: urn:oasis:names:tc:entity:xmlns:xml:catalog
22+
-->
223
<!-- to prevent unintendedn notwork access, we do not set a DTD/XSD in this XML -->
324
<catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog">
4-
<uri name="http://cyclonedx.org/schema/spdx" uri="spdx.xsd"/>
25+
26+
<!-- SPDX BOM Schema -->
27+
<uri name="http://cyclonedx.org/schema/spdx" uri="spdx.xsd"/>
28+
29+
<!-- CycloneDX BOM Schemas -->
530
<uri name="http://cyclonedx.org/schema/bom/1.0" uri="bom-1.0.xsd"/>
631
<uri name="http://cyclonedx.org/schema/bom/1.1" uri="bom-1.1.xsd"/>
732
<uri name="http://cyclonedx.org/schema/bom/1.2" uri="bom-1.2.xsd"/>
833
<uri name="http://cyclonedx.org/schema/bom/1.3" uri="bom-1.3.xsd"/>
934
<uri name="http://cyclonedx.org/schema/bom/1.4" uri="bom-1.4.xsd"/>
1035
<uri name="http://cyclonedx.org/schema/bom/1.5" uri="bom-1.5.xsd"/>
1136
<uri name="http://cyclonedx.org/schema/bom/1.6" uri="bom-1.6.xsd"/>
37+
38+
<!-- Placeholder for future schemas, where 1.x is the next CycloneDX Spec Version -->
39+
<!-- <uri name="http://cyclonedx.org/schema/bom/1.x" uri="bom-1.x.xsd"/> -->
40+
1241
</catalog>

tools/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
<lib.commons.lang3.version>3.6</lib.commons.lang3.version>
5656
<lib.commons.text.version>1.12.0</lib.commons.text.version>
5757
<lib.unirest.version>1.4.9</lib.unirest.version>
58-
<lib.cyclonedx.core.java.version>8.0.3</lib.cyclonedx.core.java.version>
58+
<lib.cyclonedx.core.java.version>9.0.2</lib.cyclonedx.core.java.version>
5959
</properties>
6060

6161
<scm>

tools/src/test/java/org/cyclonedx/schema/BaseSchemaVerificationTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ List<String> getAllResources() throws Exception {
2929
files.addAll(getResources("1.3/"));
3030
files.addAll(getResources("1.4/"));
3131
files.addAll(getResources("1.5/"));
32+
files.addAll(getResources("1.6/"));
3233
return files;
3334
}
3435

tools/src/test/java/org/cyclonedx/schema/JsonSchemaVerificationTest.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@
1313
*/
1414
package org.cyclonedx.schema;
1515

16-
import org.cyclonedx.CycloneDxSchema;
17-
import org.cyclonedx.parsers.JsonParser;
18-
import org.junit.jupiter.api.DynamicTest;
19-
import org.junit.jupiter.api.TestFactory;
2016
import java.io.File;
2117
import java.util.ArrayList;
2218
import java.util.Collection;
2319
import java.util.List;
2420

21+
import org.cyclonedx.parsers.JsonParser;
22+
import org.cyclonedx.Version;
23+
import org.junit.jupiter.api.DynamicTest;
24+
import org.junit.jupiter.api.TestFactory;
25+
2526
import static org.junit.jupiter.api.Assertions.assertTrue;
2627
import static org.junit.jupiter.api.Assertions.assertFalse;
2728

@@ -33,15 +34,17 @@ Collection<DynamicTest> dynamicTestsWithCollection() throws Exception {
3334
final List<DynamicTest> dynamicTests = new ArrayList<>();
3435
for (final String file: files) {
3536
if (file.endsWith(".json")) {
36-
final CycloneDxSchema.Version schemaVersion;
37+
final Version schemaVersion;
3738
if (file.endsWith("-1.2.json")) {
38-
schemaVersion = CycloneDxSchema.Version.VERSION_12;
39+
schemaVersion = Version.VERSION_12;
3940
} else if (file.endsWith("-1.3.json")) {
40-
schemaVersion = CycloneDxSchema.Version.VERSION_13;
41+
schemaVersion = Version.VERSION_13;
4142
} else if (file.endsWith("-1.4.json")) {
42-
schemaVersion = CycloneDxSchema.Version.VERSION_14;
43+
schemaVersion = Version.VERSION_14;
4344
} else if (file.endsWith("-1.5.json")) {
44-
schemaVersion = CycloneDxSchema.Version.VERSION_15;
45+
schemaVersion = Version.VERSION_15;
46+
} else if (file.endsWith("-1.6.json")) {
47+
schemaVersion = Version.VERSION_16;
4548
} else {
4649
schemaVersion = null;
4750
}
@@ -57,7 +60,7 @@ Collection<DynamicTest> dynamicTestsWithCollection() throws Exception {
5760
return dynamicTests;
5861
}
5962

60-
private boolean isValidJson(CycloneDxSchema.Version version, String resource) throws Exception {
63+
private boolean isValidJson(Version version, String resource) throws Exception {
6164
final File file = new File(this.getClass().getResource(resource).getFile());
6265
final JsonParser parser = new JsonParser();
6366
return parser.isValid(file, version);
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package org.cyclonedx.schema;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.DynamicTest;
5+
import org.junit.jupiter.api.TestFactory;
6+
7+
import org.w3c.dom.Document;
8+
import org.w3c.dom.Node;
9+
import org.w3c.dom.NodeList;
10+
import org.xml.sax.InputSource;
11+
import org.xml.sax.SAXException;
12+
13+
import javax.xml.parsers.DocumentBuilder;
14+
import javax.xml.parsers.DocumentBuilderFactory;
15+
import javax.xml.parsers.ParserConfigurationException;
16+
17+
import java.io.InputStream;
18+
import java.io.IOException;
19+
import java.io.StringReader;
20+
import java.nio.charset.StandardCharsets;
21+
import java.util.ArrayList;
22+
import java.util.List;
23+
24+
import static org.junit.jupiter.api.Assertions.assertTrue;
25+
import static org.junit.jupiter.api.DynamicTest.dynamicTest;
26+
27+
public class XmlCatalogVerificationTest {
28+
29+
/**
30+
* Tests the XML catalog by parsing the xmlcatalog.xml file and checking if the namespaces
31+
* in the XSD schema files match the namespaces defined in the xmlcatalog.xml file.
32+
*
33+
* @return a list of dynamic tests for each URI in the xmlcatalog.xml file
34+
* @throws IOException if an I/O error occurs while reading the XML catalog file
35+
* @throws ParserConfigurationException if a parser configuration error occurs
36+
* @throws SAXException if a SAX error occurs while parsing the XML catalog file
37+
*/
38+
@TestFactory
39+
public List<DynamicTest> testXmlCatalog() throws IOException, ParserConfigurationException, SAXException {
40+
// Define the path to the XML catalog file
41+
String xmlCatalogFilename = "xmlcatalog.xml";
42+
43+
// Load the XML catalog file from the classpath
44+
ClassLoader classLoader = getClass().getClassLoader();
45+
InputStream xmlCatalogStream = classLoader.getResourceAsStream(xmlCatalogFilename);
46+
47+
Assertions.assertNotNull(xmlCatalogStream, "XML catalog file not found");
48+
49+
// Parse the xmlcatalog.xml file
50+
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
51+
DocumentBuilder builder = factory.newDocumentBuilder();
52+
Document xmlCatalogDocument = builder.parse(new InputSource(xmlCatalogStream));
53+
54+
// Get the XML catalog elements
55+
NodeList xmlCatalogElements = xmlCatalogDocument.getDocumentElement().getChildNodes();
56+
57+
// List to hold dynamic tests
58+
List<DynamicTest> dynamicTests = new ArrayList<>();
59+
60+
// Iterate through the XML catalog elements
61+
for (int i = 0; i < xmlCatalogElements.getLength(); i++) {
62+
Node xmlCatalogElement = xmlCatalogElements.item(i);
63+
if (xmlCatalogElement.getNodeName().equals("uri")) {
64+
String uriName = xmlCatalogElement.getAttributes().getNamedItem("name").getTextContent();
65+
String xsdLocalFilename = xmlCatalogElement.getAttributes().getNamedItem("uri").getTextContent();
66+
67+
// Create a dynamic test for each URI
68+
dynamicTests.add(dynamicTest("Testing URI: " + uriName, () -> {
69+
// Load the XSD schema file from the classpath
70+
InputStream xsdSchemaFileStream = classLoader.getResourceAsStream(xsdLocalFilename);
71+
Assertions.assertNotNull(xsdSchemaFileStream, "The following file is missing: " + xsdLocalFilename);
72+
73+
// Read the XSD local file content
74+
String xsdContent = new String(xsdSchemaFileStream.readAllBytes(), StandardCharsets.UTF_8);
75+
76+
// Parse the XSD file content to a Document object
77+
Document xsdDocument = builder.parse(new InputSource(new StringReader(xsdContent)));
78+
79+
// Check if the XSD document contains the expected namespace
80+
NodeList schemaNodes = xsdDocument.getElementsByTagNameNS("*", "schema");
81+
boolean namespaceFound = false;
82+
for (int j = 0; j < schemaNodes.getLength(); j++) {
83+
Node schemaNode = schemaNodes.item(j);
84+
String targetNamespace = schemaNode.getAttributes().getNamedItem("targetNamespace").getTextContent();
85+
System.out.println("uriName.equals(targetNamespace)" + uriName.equals(targetNamespace));
86+
if (uriName.equals(targetNamespace)) {
87+
namespaceFound = true;
88+
break;
89+
}
90+
}
91+
assertTrue(namespaceFound, "The namespace " + uriName + " is not present in file " + xsdLocalFilename);
92+
}));
93+
}
94+
}
95+
96+
return dynamicTests;
97+
}
98+
}

tools/src/test/java/org/cyclonedx/schema/XmlSchemaVerificationTest.java

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,39 +13,48 @@
1313
*/
1414
package org.cyclonedx.schema;
1515

16-
import org.cyclonedx.CycloneDxSchema;
17-
import org.cyclonedx.parsers.XmlParser;
18-
import org.junit.jupiter.api.DynamicTest;
19-
import org.junit.jupiter.api.TestFactory;
2016
import java.io.File;
2117
import java.util.ArrayList;
2218
import java.util.Collection;
2319
import java.util.List;
2420

21+
import org.cyclonedx.parsers.XmlParser;
22+
import org.cyclonedx.Version;
23+
import org.junit.jupiter.api.DynamicTest;
24+
import org.junit.jupiter.api.TestFactory;
25+
2526
import static org.junit.jupiter.api.Assertions.assertTrue;
2627
import static org.junit.jupiter.api.Assertions.assertFalse;
2728

2829
public class XmlSchemaVerificationTest extends BaseSchemaVerificationTest {
2930

3031
@TestFactory
32+
/**
33+
* Generates a collection of dynamic tests based on the available XML files.
34+
*
35+
* @return Collection<DynamicTest> a collection of dynamic tests
36+
* @throws Exception if an error occurs during the generation of the dynamic tests
37+
*/
3138
Collection<DynamicTest> dynamicTestsWithCollection() throws Exception {
3239
final List<String> files = getAllResources();
3340
final List<DynamicTest> dynamicTests = new ArrayList<>();
3441
for (final String file: files) {
3542
if (file.endsWith(".xml")) {
36-
final CycloneDxSchema.Version schemaVersion;
43+
final Version schemaVersion;
3744
if (file.endsWith("-1.0.xml")) {
38-
schemaVersion = CycloneDxSchema.Version.VERSION_10;
45+
schemaVersion = Version.VERSION_10;
3946
} else if (file.endsWith("-1.1.xml")) {
40-
schemaVersion = CycloneDxSchema.Version.VERSION_11;
47+
schemaVersion = Version.VERSION_11;
4148
} else if (file.endsWith("-1.2.xml")) {
42-
schemaVersion = CycloneDxSchema.Version.VERSION_12;
49+
schemaVersion = Version.VERSION_12;
4350
} else if (file.endsWith("-1.3.xml")) {
44-
schemaVersion = CycloneDxSchema.Version.VERSION_13;
51+
schemaVersion = Version.VERSION_13;
4552
} else if (file.endsWith("-1.4.xml")) {
46-
schemaVersion = CycloneDxSchema.Version.VERSION_14;
53+
schemaVersion = Version.VERSION_14;
4754
} else if (file.endsWith("-1.5.xml")) {
48-
schemaVersion = CycloneDxSchema.Version.VERSION_15;
55+
schemaVersion = Version.VERSION_15;
56+
} else if (file.endsWith("-1.6.xml")) {
57+
schemaVersion = Version.VERSION_16;
4958
} else {
5059
schemaVersion = null;
5160
}
@@ -61,7 +70,15 @@ Collection<DynamicTest> dynamicTestsWithCollection() throws Exception {
6170
return dynamicTests;
6271
}
6372

64-
private boolean isValid(CycloneDxSchema.Version version, String resource) throws Exception {
73+
/**
74+
* Validates the given XML file against the specified CycloneDX schema version.
75+
*
76+
* @param version the CycloneDX schema version to validate against
77+
* @param resource the path to the XML file to be validated
78+
* @return boolean true if the XML file is valid according to the specified schema version, false otherwise
79+
* @throws Exception if an error occurs during the validation process
80+
*/
81+
private boolean isValid(Version version, String resource) throws Exception {
6582
final File file = new File(this.getClass().getResource(resource).getFile());
6683
final XmlParser parser = new XmlParser();
6784
return parser.isValid(file, version);

0 commit comments

Comments
 (0)