Skip to content

Commit 5fba1fc

Browse files
committed
Completed conversion SPDX V2 to SPDX V3
Signed-off-by: Gary O'Neall <gary@sourceauditor.com>
1 parent 6e585be commit 5fba1fc

4 files changed

Lines changed: 1265 additions & 70 deletions

File tree

src/main/java/org/spdx/library/conversion/ExternalMapInfo.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ public ExternalMap addExternalMap(String externalUri, IModelStore modelStore) th
8080
retval.getVerifiedUsings().add(externalDocumentHash.get());
8181
}
8282
docImports.add(retval);
83+
existingExternalMap.put(externalUri, retval);
8384
}
8485
return retval;
8586
}

src/main/java/org/spdx/library/conversion/Spdx2to3Converter.java

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,8 @@ public Relationship convertAndStore(org.spdx.library.model.v2.Relationship fromR
504504
}
505505
toRelationship.setCompleteness(completeness);
506506
if (relatedSpdxElement.isPresent() &&
507-
!(relatedSpdxElement.get() instanceof org.spdx.library.model.v2.SpdxNoneElement)) {
507+
!((relatedSpdxElement.get() instanceof org.spdx.library.model.v2.SpdxNoneElement) ||
508+
(relatedSpdxElement.get() instanceof org.spdx.library.model.v2.SpdxNoAssertionElement))) {
508509
if (SWAP_TO_FROM_REL_TYPES.contains(fromRelationshipType)) {
509510
toRelationship.setFrom(convertAndStore(relatedSpdxElement.get()));
510511
} else {
@@ -541,17 +542,12 @@ public Annotation convertAndStore(org.spdx.library.model.v2.Annotation fromAnnot
541542
toAnnotation.setAnnotationType(ANNOTATION_TYPE_MAP.get(fromAnnotation.getAnnotationType()));
542543
toAnnotation.setStatement(fromAnnotation.getComment());
543544
toAnnotation.setSubject(toElement);
544-
String fromAnnotationDate = fromAnnotation.getAnnotationDate();
545-
CreationInfo creationInfo = defaultCreationInfo;
546-
if (Objects.nonNull(fromAnnotationDate) &&
547-
!Objects.equals(fromAnnotationDate, defaultCreationInfo.getCreated())) {
548-
// Create a new creation info with the annotation date
549-
creationInfo = toAnnotation.createCreationInfo(toModelStore.getNextId(IdType.Anonymous))
550-
.setCreated(fromAnnotationDate)
551-
.addAllCreatedBy(defaultCreationInfo.getCreatedBys())
552-
.addAllCreatedUsing(defaultCreationInfo.getCreatedUsings())
553-
.build();
554-
}
545+
CreationInfo creationInfo = new CreationInfo.CreationInfoBuilder(toModelStore, toModelStore.getNextId(IdType.Anonymous), null)
546+
.setCreated(fromAnnotation.getAnnotationDate())
547+
.setSpecVersion(SpdxConstantsV3.MODEL_SPEC_VERSION)
548+
.addAllCreatedUsing(defaultCreationInfo.getCreatedUsings())
549+
.build();
550+
creationInfo.getCreatedBys().add(stringToAgent(fromAnnotation.getAnnotator(), creationInfo));
555551
toAnnotation.setCreationInfo(creationInfo);
556552
return toAnnotation;
557553
}
@@ -780,6 +776,8 @@ public ListedLicense convertAndStore(org.spdx.library.model.v2.license.SpdxListe
780776
toListedLicense.setStandardLicenseHeader(fromSpdxListedLicense.getStandardLicenseHeader());
781777
// fromSpdxListedLicense.getStandardLicenseHeaderTemplate(); - no equivalent in SPDX version 3.X
782778
toListedLicense.setStandardLicenseTemplate(fromSpdxListedLicense.getStandardLicenseTemplate());
779+
toListedLicense.setIsDeprecatedLicenseId(fromSpdxListedLicense.isDeprecated());
780+
toListedLicense.setIsOsiApproved(fromSpdxListedLicense.isOsiApproved());
783781
return toListedLicense;
784782
}
785783

@@ -881,7 +879,8 @@ public ListedLicenseException convertAndStore(org.spdx.library.model.v2.license.
881879
ListedLicenseException toListedException = (ListedLicenseException)SpdxModelClassFactoryV3.getModelObject(toModelStore,
882880
fromException.getObjectUri(), SpdxConstantsV3.EXPANDED_LICENSING_LISTED_LICENSE_EXCEPTION, copyManager, true, defaultUriPrefix);
883881
convertLicenseAdditionProperties(fromException, toListedException);
884-
toListedException.setDeprecatedVersion(fromException.getDeprecatedVersion());
882+
toListedException.setDeprecatedVersion(fromException.getDeprecatedVersion());
883+
toListedException.setIsDeprecatedAdditionId(fromException.isDeprecated());
885884
// fromException.getExample(); - no SPDX spec version 3 equivalent
886885
// fromException.getExceptionTextHtml(); - no SPDX spec version 3 equivalent
887886
return toListedException;
@@ -1163,13 +1162,6 @@ public SpdxPackage convertAndStore(org.spdx.library.model.v2.SpdxPackage spdxPac
11631162
toPackage.setPrimaryPurpose(PURPOSE_MAP.get(primaryPurpose.get()));
11641163
}
11651164
toPackage.setReleaseTime(spdxPackage.getReleaseDate().orElse(null));
1166-
String sha1 = spdxPackage.getSha1();
1167-
if (Objects.nonNull(sha1)) {
1168-
toPackage.getVerifiedUsings().add(toPackage.createHash(toModelStore.getNextId(IdType.Anonymous))
1169-
.setAlgorithm(HashAlgorithm.SHA1)
1170-
.setHashValue(sha1)
1171-
.build());
1172-
}
11731165
toPackage.setSourceInfo(spdxPackage.getSourceInfo().orElse(null));
11741166
toPackage.setSummary(spdxPackage.getSummary().orElse(null));
11751167
Optional<String> supplier = spdxPackage.getSupplier();
@@ -1316,6 +1308,7 @@ public Snippet convertAndStore(org.spdx.library.model.v2.SpdxSnippet fromSnippet
13161308
.setEndIntegerRange(((org.spdx.library.model.v2.pointer.LineCharPointer)fromLineRange.get().getEndPointer()).getLineNumber())
13171309
.build());
13181310
}
1311+
toSnippet.setSnippetFromFile(convertAndStore(fromSnippet.getSnippetFromFile()));
13191312
return toSnippet;
13201313
}
13211314
}

src/test/java/org/spdx/library/conversion/ExternalMapInfoTest.java

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,38 @@
1919

2020
import static org.junit.Assert.*;
2121

22+
import java.util.ArrayList;
23+
import java.util.List;
24+
import java.util.Optional;
25+
2226
import org.junit.After;
2327
import org.junit.Before;
2428
import org.junit.Test;
29+
import org.spdx.core.InvalidSPDXAnalysisException;
30+
import org.spdx.library.SpdxModelFactory;
31+
import org.spdx.library.model.v2.SpdxConstantsCompatV2;
32+
import org.spdx.library.model.v3_0_0.core.ExternalMap;
33+
import org.spdx.library.model.v3_0_0.core.Hash;
34+
import org.spdx.library.model.v3_0_0.core.IntegrityMethod;
35+
import org.spdx.storage.IModelStore;
36+
import org.spdx.storage.IModelStore.IdType;
37+
import org.spdx.storage.simple.InMemSpdxStore;
2538

2639
/**
2740
* @author gary
2841
*
2942
*/
3043
public class ExternalMapInfoTest {
44+
45+
IModelStore modelStore;
3146

3247
/**
3348
* @throws java.lang.Exception
3449
*/
3550
@Before
3651
public void setUp() throws Exception {
52+
SpdxModelFactory.init();
53+
modelStore = new InMemSpdxStore();
3754
}
3855

3956
/**
@@ -45,10 +62,29 @@ public void tearDown() throws Exception {
4562

4663
/**
4764
* Test method for {@link org.spdx.library.conversion.ExternalMapInfo#addExternalMap(java.lang.String, org.spdx.storage.IModelStore)}.
65+
* @throws InvalidSPDXAnalysisException
4866
*/
4967
@Test
50-
public void testAddExternalMap() {
51-
fail("Not yet implemented");
68+
public void testAddExternalMap() throws InvalidSPDXAnalysisException {
69+
String docRefId = SpdxConstantsCompatV2.EXTERNAL_DOC_REF_PRENUM + "test";
70+
String externalDocumentUri = "https://external/doc/uri";
71+
Hash hash = new Hash(modelStore, modelStore.getNextId(IdType.Anonymous), null, true, null);
72+
Optional<Hash> externalDocumentHash = Optional.of(hash);
73+
List<ExternalMap> docImports = new ArrayList<>();
74+
ExternalMapInfo emi = new ExternalMapInfo(docRefId, externalDocumentUri, externalDocumentHash, docImports);
75+
String externalUri = externalDocumentUri + "#" + SpdxConstantsCompatV2.SPDX_ELEMENT_REF_PRENUM + "test";
76+
assertTrue(docImports.isEmpty());
77+
emi.addExternalMap(externalUri, modelStore);
78+
assertEquals(1, docImports.size());
79+
assertEquals(externalUri, docImports.get(0).getExternalSpdxId());
80+
assertEquals(externalDocumentUri, docImports.get(0).getLocationHint().get());
81+
IntegrityMethod[] hashes = docImports.get(0).getVerifiedUsings().toArray(new IntegrityMethod[docImports.get(0).getVerifiedUsings().size()]);
82+
assertEquals(1, hashes.length);
83+
assertTrue(hashes[0] instanceof Hash);
84+
assertTrue(hashes[0].equivalent(hash));
85+
emi.addExternalMap(externalUri, modelStore);
86+
// already added
87+
assertEquals(1, docImports.size());
5288
}
5389

5490
}

0 commit comments

Comments
 (0)