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

fix relationshipTest, add it to the solver, correct path in conversionTests #27

Merged
merged 1 commit into from
Oct 11, 2022
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 @@ -5,6 +5,7 @@
import org.spdx.library.InvalidSPDXAnalysisException;
import org.spdx.library.model.SpdxDocument;
import org.spdx.toolsJavaSolver.generationTestCases.GenerationMinimalTestCase;
import org.spdx.toolsJavaSolver.generationTestCases.GenerationRelationshipTestCase;

import java.io.FileOutputStream;
import java.io.IOException;
Expand All @@ -30,14 +31,18 @@ public static void main(String[] args) throws InvalidSPDXAnalysisException, IOEx
String outputPath = cmd.getOptionValue("f");

SpdxDocument outputDoc;
var testCaseName = TestCaseName.fromString(testCase);

switch (testCase) {
case "generationMinimalTest":
switch (testCaseName) {
case GENERATION_MINIMAL:
outputDoc = GenerationMinimalTestCase.buildDocument();
break;
case GENERATION_RELATIONSHIP:
outputDoc = GenerationRelationshipTestCase.buildDocument();
break;
default:
//TODO: add info about supported test cases
System.err.print("Error: " + testCase + " is an unrecognized test case. Here is a list of possible test cases: (work in progress)\n");
System.err.print("Error: " + testCase + " is an unrecognized or unimplemented test case. Here is a list of possible test cases: (work in progress)\n");
System.exit(1);
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.spdx.toolsJavaSolver;

public enum TestCaseName {

GENERATION_MINIMAL("generationMinimalTest"),
GENERATION_BASELINE_SBOM("generationBaselineSbomTest"),
GENERATION_DOCUMENT("generationDocumentTest"),
GENERATION_PACKAGE("generationPackageTest"),
GENERATION_FILE("generationFileTest"),
GENERATION_SNIPPET("generationSnippetTest"),
GENERATION_LICENSE("generationLicenseTest"),
GENERATION_RELATIONSHIP("generationRelationshipTest"),
GENERATION_EXTRACTED_LICENSE_INFO("generationExtractedLicenseInfoTest");

private final String fullName;

TestCaseName(String name) {
this.fullName = name;
}

public String getFullName() {
return this.fullName;
}

public static TestCaseName fromString(String str) {
for (var testCaseName : TestCaseName.values()){
if (testCaseName.getFullName().equals(str)){
return testCaseName;
}
}
throw new IllegalArgumentException("Unknown test case name: " + str);
}
}
Original file line number Diff line number Diff line change
@@ -1,36 +1,17 @@
package org.spdx.toolsJavaSolver.generationTestCases;

import org.spdx.jacksonstore.MultiFormatStore;
import org.spdx.library.InvalidSPDXAnalysisException;
import org.spdx.library.ModelCopyManager;
import org.spdx.library.Version;
import org.spdx.library.model.Checksum;
import org.spdx.library.model.SpdxDocument;
import org.spdx.library.model.SpdxModelFactory;
import org.spdx.library.model.enumerations.ChecksumAlgorithm;
import org.spdx.library.model.license.LicenseInfoFactory;
import org.spdx.storage.IModelStore;
import org.spdx.storage.simple.InMemSpdxStore;

import java.util.List;

public class GenerationMinimalTestCase {

public static SpdxDocument buildDocument() throws InvalidSPDXAnalysisException {
var modelStore = new MultiFormatStore(new InMemSpdxStore(), MultiFormatStore.Format.XML, MultiFormatStore.Verbose.COMPACT);
var documentUri = "https://some.namespace";
var copyManager = new ModelCopyManager();
var document = GenerationUtil.createSpdxDocumentWithBasicInfo("Minimal test document");

var document = SpdxModelFactory.createSpdxDocument(modelStore, documentUri, copyManager);

var creationInfo = document.createCreationInfo(
List.of("Tool: test-tool"), "2022-01-01T00:00:00Z");

document.setCreationInfo(creationInfo);
document.setSpecVersion(Version.TWO_POINT_THREE_VERSION);
document.setName("Minimal test document");

var sha1Checksum = createSha1Checksum(modelStore, documentUri);
var sha1Checksum = GenerationUtil.createSha1Checksum(document.getModelStore(), document.getDocumentUri());
var concludedLicense = LicenseInfoFactory.parseSPDXLicenseString("LGPL-3.0-only");
var file = document.createSpdxFile("SPDXRef-somefile", "./foo.txt", concludedLicense, List.of(), "Copyright 2022 some guy", sha1Checksum).build();

Expand All @@ -39,7 +20,4 @@ public static SpdxDocument buildDocument() throws InvalidSPDXAnalysisException {
return document;
}

static Checksum createSha1Checksum(IModelStore modelStore, String documentUri) throws InvalidSPDXAnalysisException {
return Checksum.create(modelStore, documentUri, ChecksumAlgorithm.SHA1, "d6a770ba38583ed4bb4525bd96e50461655d2758");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.spdx.toolsJavaSolver.generationTestCases;

import org.spdx.library.InvalidSPDXAnalysisException;
import org.spdx.library.model.SpdxDocument;
import org.spdx.library.model.enumerations.RelationshipType;
import org.spdx.library.model.license.LicenseInfoFactory;

import java.util.List;

public class GenerationRelationshipTestCase {

public static SpdxDocument buildDocument() throws InvalidSPDXAnalysisException {
var document = GenerationUtil.createSpdxDocumentWithBasicInfo("Relationship test document");

var modelStore = document.getModelStore();
var documentUri = document.getDocumentUri();

var sha1Checksum = GenerationUtil.createSha1Checksum(modelStore, documentUri);

var concludedLicense = LicenseInfoFactory.parseSPDXLicenseString("LGPL-2.0-only");

var fileA = document.createSpdxFile("SPDXRef-fileA", "./fileA.c", concludedLicense,
List.of(), "Copyright 2022 some person", sha1Checksum)
.build();

var fileB = document.createSpdxFile("SPDXRef-fileB", "./fileB.c", concludedLicense,
List.of(), "Copyright 2022 some person", sha1Checksum)
.build();

document.getDocumentDescribes().add(fileA);
document.getDocumentDescribes().add(fileB);

for (var relationshipType : RelationshipType.values()) {
if (relationshipType == RelationshipType.MISSING) {
continue;
}
fileB.addRelationship(
document.createRelationship(
fileA, relationshipType, String.format("comment on %s", relationshipType.name())));

}
Comment on lines +33 to +41
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a side remark (no need to change this): This could be done quite elegantly with a stream, using one filter to ignore the MISSING type and then a forEach


return document;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.spdx.toolsJavaSolver.generationTestCases;

import org.spdx.jacksonstore.MultiFormatStore;
import org.spdx.library.InvalidSPDXAnalysisException;
import org.spdx.library.ModelCopyManager;
import org.spdx.library.Version;
import org.spdx.library.model.Checksum;
import org.spdx.library.model.SpdxDocument;
import org.spdx.library.model.SpdxModelFactory;
import org.spdx.library.model.enumerations.ChecksumAlgorithm;
import org.spdx.storage.IModelStore;
import org.spdx.storage.simple.InMemSpdxStore;

import java.util.List;

public class GenerationUtil {

static Checksum createSha1Checksum(IModelStore modelStore, String documentUri) throws InvalidSPDXAnalysisException {
return Checksum.create(modelStore, documentUri, ChecksumAlgorithm.SHA1, "d6a770ba38583ed4bb4525bd96e50461655d2758");
}
Comment on lines +18 to +20
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find it funny that we are always generating a hardcoded checksum value 😂 . But it's fine for the time being, as long as the actual value is irrelevant


static SpdxDocument createSpdxDocumentWithBasicInfo(String documentName) throws InvalidSPDXAnalysisException {
var modelStore = new MultiFormatStore(new InMemSpdxStore(), MultiFormatStore.Format.XML, MultiFormatStore.Verbose.COMPACT);
var documentUri = "https://some.namespace";
var copyManager = new ModelCopyManager();

var document = SpdxModelFactory.createSpdxDocument(modelStore, documentUri, copyManager);

var creationInfo = document.createCreationInfo(
List.of("Tool: test-tool"), "2022-01-01T00:00:00Z");

document.setCreationInfo(creationInfo);
document.setSpecVersion(Version.TWO_POINT_THREE_VERSION);
document.setName(documentName);

return document;
}
}
9 changes: 4 additions & 5 deletions testbed/src/main/java/org/spdx/testbed/TestResult.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package org.spdx.testbed;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import lombok.Builder;
import org.spdx.testbed.util.Comparisons;

import java.util.Collections;
import java.util.Map;

@Builder
public class TestResult {
Boolean success;

@Builder.Default
Map<String, Comparisons.Tuple<?>> differences = Collections.emptyMap();
ArrayNode differences = (new ObjectMapper()).createArrayNode();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't create a new object mapper every time we need one. I created #28 to cover this, may require a little bit of thought

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public abstract class GenerationTestCase implements TestCase {
public TestResult test(String[] args) throws IOException, InvalidFileNameException, InvalidSPDXAnalysisException {
var inputDoc = parseArgsAndGetInputDoc(args);
var referenceDoc = buildReferenceDocument();
var differences = Comparisons.findDifferences(referenceDoc, inputDoc, false);
var differences = Comparisons.findDifferencesAsJsonPatch(referenceDoc, inputDoc);

if (differences.isEmpty()) {
System.out.print(this.getClass().getSimpleName() + " succeeded!\n");
Expand Down
28 changes: 14 additions & 14 deletions testbed/src/test/java/conversion/TestConversion.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,23 @@ public void convertDocument(String inputFilePath, String outputFilePath) throws
}

private static Stream<Arguments> provideFileNames() {
var xmlInputFile = "SPDXDocumentExamples/spdx-spec-v2.3/SPDXXMLExample-v2.3.spdx.xml";
var rdfInputFile = "SPDXDocumentExamples/spdx-spec-v2.3/SPDXRdfExample-v2.3.spdx.rdf.xml";
var jsonInputFile = "SPDXDocumentExamples/spdx-spec-v2.3/SPDXJSONExample-v2.3.spdx.json";
var xlsInputFile = "SPDXDocumentExamples/spdx-spec-v2.3/SPDXSpreadsheetExample-v2.3.xls";
var xlsxInputFile = "SPDXDocumentExamples/spdx-spec-v2.3/SPDXSpreadsheetExample-v2.3.xlsx";
var tagInputFile = "SPDXDocumentExamples/spdx-spec-v2.3/SPDXTagExample-v2.3.spdx";
var yamlInputFile = "SPDXDocumentExamples/spdx-spec-v2.3/SPDXYAMLExample-2.3.spdx.yaml";
var xmlInputFile = "../SPDXDocumentExamples/spdx-spec-v2.3/SPDXXMLExample-v2.3.spdx.xml";
var rdfInputFile = "../SPDXDocumentExamples/spdx-spec-v2.3/SPDXRdfExample-v2.3.spdx.rdf.xml";
var jsonInputFile = "../SPDXDocumentExamples/spdx-spec-v2.3/SPDXJSONExample-v2.3.spdx.json";
var xlsInputFile = "../SPDXDocumentExamples/spdx-spec-v2.3/SPDXSpreadsheetExample-v2.3.xls";
var xlsxInputFile = "../SPDXDocumentExamples/spdx-spec-v2.3/SPDXSpreadsheetExample-v2.3.xlsx";
var tagInputFile = "../SPDXDocumentExamples/spdx-spec-v2.3/SPDXTagExample-v2.3.spdx";
var yamlInputFile = "../SPDXDocumentExamples/spdx-spec-v2.3/SPDXYAMLExample-2.3.spdx.yaml";
var inputFiles = Set.of(xmlInputFile, rdfInputFile, jsonInputFile, xlsInputFile,
xlsxInputFile, tagInputFile, yamlInputFile);

var xmlOutputFile = "SPDXDocumentExamples/temp/convertedFile.spdx.xml";
var rdfOutputFile = "SPDXDocumentExamples/temp/convertedFile.spdx.rdf.xml";
var jsonOutputFile = "SPDXDocumentExamples/temp/convertedFile.spdx.json";
var xlsOutputFile = "SPDXDocumentExamples/temp/convertedFile.xls";
var xlsxOutputFile = "SPDXDocumentExamples/temp/convertedFile.xlsx";
var tagOutputFile = "SPDXDocumentExamples/temp/convertedFile.spdx";
var yamlOutputFile = "SPDXDocumentExamples/temp/convertedFile.spdx.yaml";
var xmlOutputFile = "../SPDXDocumentExamples/temp/convertedFile.spdx.xml";
var rdfOutputFile = "../SPDXDocumentExamples/temp/convertedFile.spdx.rdf.xml";
var jsonOutputFile = "../SPDXDocumentExamples/temp/convertedFile.spdx.json";
var xlsOutputFile = "../SPDXDocumentExamples/temp/convertedFile.xls";
var xlsxOutputFile = "../SPDXDocumentExamples/temp/convertedFile.xlsx";
var tagOutputFile = "../SPDXDocumentExamples/temp/convertedFile.spdx";
var yamlOutputFile = "../SPDXDocumentExamples/temp/convertedFile.spdx.yaml";
var outputFiles = Set.of(xmlOutputFile, rdfOutputFile, jsonOutputFile, xlsOutputFile,
xlsxOutputFile, tagOutputFile, yamlOutputFile);

Expand Down
Loading