Skip to content

Commit

Permalink
Merge pull request #70 from spdx/purposedash
Browse files Browse the repository at this point in the history
Retain the underscore for the OPERATING_SYSTEM enum value
  • Loading branch information
goneall authored Jan 18, 2024
2 parents 4a3f39a + 5f11bd6 commit eb9ad6a
Show file tree
Hide file tree
Showing 4 changed files with 820 additions and 1 deletion.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ This store supports serializing and deserializing files in JSON, YAML and XML fo
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.15.0</version>
</dependency>
<dependency>
<groupId>com.github.java-json-tools</groupId>
<artifactId>json-schema-validator</artifactId>
<version>2.2.14</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/spdx/jacksonstore/JacksonSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.spdx.library.model.SpdxInvalidTypeException;
import org.spdx.library.model.SpdxModelFactory;
import org.spdx.library.model.TypedValue;
import org.spdx.library.model.enumerations.Purpose;
import org.spdx.library.model.enumerations.RelationshipType;
import org.spdx.library.model.enumerations.SpdxEnumFactory;
import org.spdx.library.model.license.AnyLicenseInfo;
Expand Down Expand Up @@ -602,7 +603,7 @@ private Object toSerializable(String documentUri, Object value, ArrayNode relati
private String individualUriToString(String documentUri, String uri) throws InvalidSPDXAnalysisException {
Object enumval = SpdxEnumFactory.uriToEnum.get(uri);
if (Objects.nonNull(enumval)) {
if (enumval instanceof RelationshipType) {
if (enumval instanceof RelationshipType || enumval instanceof Purpose) {
return enumval.toString();
} else {
return enumval.toString().replaceAll("_", "-");
Expand Down
62 changes: 62 additions & 0 deletions src/test/java/org/spdx/jacksonstore/MultiFormatStoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.github.fge.jackson.JsonLoader;
import com.github.fge.jsonschema.core.exceptions.ProcessingException;
import com.github.fge.jsonschema.core.report.ProcessingReport;
import com.github.fge.jsonschema.main.JsonSchema;
import com.github.fge.jsonschema.main.JsonSchemaFactory;

import junit.framework.TestCase;

Expand All @@ -81,6 +86,8 @@ public class MultiFormatStoreTest extends TestCase {
static final String JSON_WITH_DUPLICATES_FILE_PATH = "testResources" + File.separator + "duplicated.json";
static final String JSON_NO_HAS_FILES_FILE_PATH = "testResources" + File.separator + "noHasFilesDescribes.json";
static final String XML_1REL_FILE_PATH = "testResources" + File.separator + "SPDXXML-SingleRel-v2.3.spdx.xml";
static final String JSON_SCHEMA_V2_3 = "testResources" + File.separator + "spdx-schema.json";

/* (non-Javadoc)
* @see junit.framework.TestCase#setUp()
*/
Expand Down Expand Up @@ -626,4 +633,59 @@ public void testDeSerializeXml_singleRelationship() throws InvalidSPDXAnalysisEx
List<String> verify = inputDocument.verify();
assertEquals(0, verify.size());
}

/**
* Test serialized json validates
* @throws IOException
* @throws InvalidSPDXAnalysisException
* @throws SpdxCompareException
* @throws ProcessingException
*/
public void testSerializeJson() throws InvalidSPDXAnalysisException, IOException, SpdxCompareException, ProcessingException {
File jsonFile = new File(JSON_FILE_PATH);
MultiFormatStore inputStore = new MultiFormatStore(new InMemSpdxStore(), Format.JSON_PRETTY);
try (InputStream input = new FileInputStream(jsonFile)) {
inputStore.deSerialize(input, false);
}
String documentUri = inputStore.getDocumentUris().get(0);
SpdxDocument inputDocument = new SpdxDocument(inputStore, documentUri, null, false);
// Add a purpose of operating system to make sure the underscore is preserved
SpdxPackage pkg = inputDocument.createPackage(SpdxConstants.SPDX_ELEMENT_REF_PRENUM + "-purpose",
"Package with a Purpose", new SpdxNoAssertionLicense(), "NoAssertion",
new SpdxNoAssertionLicense())
.setPrimaryPurpose(Purpose.OPERATING_SYSTEM)
.setDownloadLocation("NOASSERTION")
.setFilesAnalyzed(false)
.build();
inputDocument.addRelationship(inputDocument.createRelationship(pkg, RelationshipType.DESCRIBES, "Describe another package"));
List<String> verify = inputDocument.verify();
assertEquals(0, verify.size());
// test that it deserializes correctly
Path tempDirPath = Files.createTempDirectory("mfsTest2");
File serFile = tempDirPath.resolve("testspdx.json").toFile();
assertTrue(serFile.createNewFile());
try {
try (OutputStream stream = new FileOutputStream(serFile)) {
inputStore.serialize(documentUri, stream);
}
ISerializableModelStore resultStore = new MultiFormatStore(new InMemSpdxStore(), MultiFormatStore.Format.JSON);
try (InputStream inStream = new FileInputStream(serFile)) {
assertEquals(documentUri, resultStore.deSerialize(inStream, false));
}
SpdxDocument resultDoc = SpdxModelFactory.createSpdxDocument(resultStore, documentUri, new ModelCopyManager());
verify = resultDoc.verify();
assertEquals(0, verify.size());
// validate schema file
JsonNode spdxJsonSchema = JsonLoader.fromFile(new File(JSON_SCHEMA_V2_3));
final JsonSchema schema = JsonSchemaFactory.byDefault().getJsonSchema(spdxJsonSchema);
JsonNode spdxDocJson = JsonLoader.fromFile(serFile);
ProcessingReport report = schema.validateUnchecked(spdxDocJson, true);
assertTrue(report.isSuccess());
} finally {
if (serFile.exists()) {
serFile.delete();
}
tempDirPath.toFile().delete();
}
}
}
Loading

0 comments on commit eb9ad6a

Please sign in to comment.