Skip to content

Commit

Permalink
Add more test cases
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Alzate <[email protected]>
  • Loading branch information
mr-zepol committed May 26, 2024
1 parent b9d55fc commit 573b046
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 19 deletions.
41 changes: 32 additions & 9 deletions src/test/java/org/cyclonedx/BomJsonGeneratorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,31 +181,46 @@ public void schema14JBomLinkGenerationTest() throws Exception {
assertEquals("urn:cdx:f08a6ccd-4dce-4759-bd84-c626675d60a7/1", bom2.getComponents().get(0).getExternalReferences().get(0).getUrl());
}

@Test
public void testIssue408Regression_1_5() throws Exception {
Version version = Version.VERSION_15;
Bom bom = createCommonJsonBom("/regression/issue408-1.5.json");
assertLicenseInformation(bom, version);

BomXmlGenerator generator = BomGeneratorFactory.createXml(version, bom);
File loadedFile = writeToFile(generator.toXmlString());

XmlParser parser = new XmlParser();
assertTrue(parser.isValid(loadedFile, version));
}

@Test
public void testIssue408Regression() throws Exception {
Version version = Version.VERSION_16;
Bom bom = createCommonJsonBom("/regression/issue408.json");
assertLicenseInformation(bom);
assertLicenseInformation(bom, version);

BomJsonGenerator generator = BomGeneratorFactory.createJson(Version.VERSION_16, bom);
BomJsonGenerator generator = BomGeneratorFactory.createJson(version, bom);
File loadedFile = writeToFile(generator.toJsonString());

JsonParser parser = new JsonParser();
assertTrue(parser.isValid(loadedFile, Version.VERSION_16));
assertTrue(parser.isValid(loadedFile, version));
}

@Test
public void testIssue408Regression_xmlToJson() throws Exception {
Version version = Version.VERSION_16;
Bom bom = createCommonXmlBom("/regression/issue408.xml");
assertLicenseInformation(bom);
assertLicenseInformation(bom, version);

BomJsonGenerator generator = BomGeneratorFactory.createJson(Version.VERSION_16, bom);
BomJsonGenerator generator = BomGeneratorFactory.createJson(version, bom);
File loadedFile = writeToFile(generator.toJsonString());

JsonParser parser = new JsonParser();
assertTrue(parser.isValid(loadedFile, Version.VERSION_16));
assertTrue(parser.isValid(loadedFile, version));
}

private void assertLicenseInformation(Bom bom) {
private void assertLicenseInformation(Bom bom, Version version) {

//First Component
Component component = bom.getComponents().get(0);
Expand All @@ -220,7 +235,11 @@ private void assertLicenseInformation(Bom bom) {
assertNotNull(license1);
assertNotNull(license1.getId());
assertNull(license1.getName());
assertNotNull(license1.getAcknowledgement());
if(version.getVersion() >= Version.VERSION_16.getVersion()) {
assertNotNull(license1.getAcknowledgement());
} else {
assertNull(license1.getAcknowledgement());
}
assertNotNull(license1.getBomRef());

License license2 = component.getLicenses().getLicenses().get(1);
Expand All @@ -240,7 +259,11 @@ private void assertLicenseInformation(Bom bom) {

Expression expression = component2.getLicenses().getExpression();
assertNotNull(expression.getValue());
assertNotNull(expression.getAcknowledgement());
if(version.getVersion() >= Version.VERSION_16.getVersion()) {
assertNotNull(expression.getAcknowledgement());
} else {
assertNull(expression.getAcknowledgement());
}
assertNotNull(expression.getBomRef());

//Third Component Evidence
Expand Down
42 changes: 33 additions & 9 deletions src/test/java/org/cyclonedx/BomXmlGeneratorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -305,31 +305,46 @@ public void schema14JBomLinkGenerationTest() throws Exception {
assertEquals("urn:cdx:f08a6ccd-4dce-4759-bd84-c626675d60a7/1", bom2.getComponents().get(0).getExternalReferences().get(0).getUrl());
}

@Test
public void testIssue408Regression_1_5() throws Exception {
Version version = Version.VERSION_15;
Bom bom = createCommonBomXml("/regression/issue408-1.5.xml");
assertLicenseInformation(bom, version);

BomXmlGenerator generator = BomGeneratorFactory.createXml(version, bom);
File loadedFile = writeToFile(generator.toXmlString());

XmlParser parser = new XmlParser();
assertTrue(parser.isValid(loadedFile, version));
}

@Test
public void testIssue408Regression() throws Exception {
Version version = Version.VERSION_16;
Bom bom = createCommonBomXml("/regression/issue408.xml");
assertLicenseInformation(bom);
assertLicenseInformation(bom, version);

BomXmlGenerator generator = BomGeneratorFactory.createXml(Version.VERSION_16, bom);
BomXmlGenerator generator = BomGeneratorFactory.createXml(version, bom);
File loadedFile = writeToFile(generator.toXmlString());

XmlParser parser = new XmlParser();
assertTrue(parser.isValid(loadedFile, Version.VERSION_16));
assertTrue(parser.isValid(loadedFile, version));
}

@Test
public void testIssue408Regression_jsonToXml() throws Exception {
Version version = Version.VERSION_16;
Bom bom = createCommonJsonBom("/regression/issue408.json");
assertLicenseInformation(bom);
assertLicenseInformation(bom, version);

BomXmlGenerator generator = BomGeneratorFactory.createXml(Version.VERSION_16, bom);
BomXmlGenerator generator = BomGeneratorFactory.createXml(version, bom);
File loadedFile = writeToFile(generator.toXmlString());

XmlParser parser = new XmlParser();
assertTrue(parser.isValid(loadedFile, Version.VERSION_16));
assertTrue(parser.isValid(loadedFile, version));
}

private void assertLicenseInformation(Bom bom) {
private void assertLicenseInformation(Bom bom, Version version) {

//First Component
Component component = bom.getComponents().get(0);
Expand All @@ -344,7 +359,12 @@ private void assertLicenseInformation(Bom bom) {
assertNotNull(license1);
assertNotNull(license1.getId());
assertNull(license1.getName());
assertNotNull(license1.getAcknowledgement());

if(version.getVersion() >= Version.VERSION_16.getVersion()) {
assertNotNull(license1.getAcknowledgement());
} else {
assertNull(license1.getAcknowledgement());
}
assertNotNull(license1.getBomRef());

License license2 = component.getLicenses().getLicenses().get(1);
Expand All @@ -364,7 +384,11 @@ private void assertLicenseInformation(Bom bom) {

Expression expression = component2.getLicenses().getExpression();
assertNotNull(expression.getValue());
assertNotNull(expression.getAcknowledgement());
if(version.getVersion() >= Version.VERSION_16.getVersion()) {
assertNotNull(expression.getAcknowledgement());
} else {
assertNull(expression.getAcknowledgement());
}
assertNotNull(expression.getBomRef());

//Third Component Evidence
Expand Down
70 changes: 70 additions & 0 deletions src/test/resources/regression/issue408-1.5.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"bomFormat": "CycloneDX",
"specVersion": "1.5",
"serialNumber": "urn:uuid:1624fa6f-aebe-4dba-8ead-f2c876c9b832",
"version": 1,
"metadata": {
"licenses": [
{
"id": "MIT"
},
{
"name": "MIT"
}
]
},
"components": [
{
"type": "library",
"name": "test",
"licenses": [
{
"id": "MIT",
"bom-ref": "test-1"
},
{
"name": "MIT WITH Libtool-exception"
}
]
},
{
"type": "library",
"name": "test",
"licenses": [
{
"expression": "CC0-1.0",
"bom-ref": "test"
}
]
},
{
"type": "library",
"name": "test-1",
"evidence": {
"licenses": [
{
"id": "Apache-2.0",
"url": "http://www.apache.org/licenses/LICENSE-2.0"
},
{
"id": "LGPL-2.1-only",
"url": "https://opensource.org/licenses/LGPL-2.1"
}
]
}
}
],
"services": [
{
"name": "Stock ticker service",
"licenses": [
{
"name": "Partner license"
},
{
"name": "test"
}
]
}
]
}
60 changes: 60 additions & 0 deletions src/test/resources/regression/issue408-1.5.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?xml version="1.0" encoding="UTF-8"?>
<bom serialNumber="urn:uuid:7fec5e81-b204-4eec-a0d6-383dc39c4867" version="1" xmlns="http://cyclonedx.org/schema/bom/1.5">
<metadata>
<licenses>
<license>
<id>MIT</id>
</license>
<license>
<name>MIT</name>
</license>
</licenses>
</metadata>
<components>
<component type="library">
<name>test</name>
<licenses>
<license bom-ref="test-1">
<id>MIT</id>
</license>
<license>
<name>MIT WITH Libtool-exception</name>
</license>
</licenses>
</component>
<component type="library">
<name>test</name>
<licenses>
<expression bom-ref="test">CC0-1.0</expression>
</licenses>
</component>
<component type="library">
<name>test-1</name>
<evidence>
<licenses>
<license>
<id>Apache-2.0</id>
<url>http://www.apache.org/licenses/LICENSE-2.0</url>
</license>
<license>
<id>LGPL-2.1-only</id>
<url>https://opensource.org/licenses/LGPL-2.1</url>
</license>
</licenses>
</evidence>
</component>
</components>
<services>
<service bom-ref="b2a46a4b-8367-4bae-9820-95557cfe03a8">
<name>Stock ticker service</name>
<licenses>
<license>
<name>Partner license</name>
</license>
<license>
<name>test</name>
</license>
</licenses>
</service>
</services>
</bom>
2 changes: 1 addition & 1 deletion src/test/resources/regression/issue408.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"bomFormat": "CycloneDX",
"specVersion": "1.5",
"specVersion": "1.6",
"serialNumber": "urn:uuid:1624fa6f-aebe-4dba-8ead-f2c876c9b832",
"version": 1,
"metadata": {
Expand Down

0 comments on commit 573b046

Please sign in to comment.