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

Add getId method to CoreModelObject #25

Merged
merged 1 commit into from
Jan 29, 2025
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
9 changes: 9 additions & 0 deletions src/main/java/org/spdx/core/CoreModelObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -1031,4 +1031,13 @@ public String getSpecVersion() throws InvalidSPDXAnalysisException {
public void setIdPrefix(@Nullable String idPrefix) {
this.idPrefix = idPrefix;
}

/**
* @return if the objectUri is prefixed by the idPrefix, returns just the id portion of the objectUri, otherwise
* returns the objectUri
*/
public String getId() {
return Objects.nonNull(idPrefix) && objectUri.startsWith(idPrefix) ?
objectUri.substring(idPrefix.length()) : objectUri;
}
}
41 changes: 26 additions & 15 deletions src/test/java/org/spdx/core/TestCoreModelObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
*
*/
public class TestCoreModelObject {

static final String OBJECT_URI = "https://myspdx.docs/objecturi#part1";

static final String PREFIX = "https://myspdx.docs/objecturi#";
static final String ID = "part1";
static final String OBJECT_URI = PREFIX + ID;
static final String OBJECT_PROPERTY_NAME = "objectPropName";
static final String PROPERTY_NAMESPACE = "https://spdx-mock/namespace";
static final String STRING_PROPERTY_NAME = "stringPropName";
Expand Down Expand Up @@ -80,7 +82,7 @@ public void testCoreModelObjectIModelStoreStringIModelCopyManagerBooleanString()

/**
* Test method for {@link org.spdx.core.CoreModelObject#CoreModelObject(org.spdx.core.CoreModelObject.CoreModelObjectBuilder, java.lang.String)}.
* @throws InvalidSPDXAnalysisException
* @throws InvalidSPDXAnalysisException on unexpected errors
*/
@Test
public void testCoreModelObjectCoreModelObjectBuilderString() throws InvalidSPDXAnalysisException {
Expand Down Expand Up @@ -108,7 +110,7 @@ public void testVerifyString() {

/**
* Test method for {@link org.spdx.core.CoreModelObject#verifyCollection(java.util.Collection, java.lang.String, java.util.Set, java.lang.String)}.
* @throws InvalidSPDXAnalysisException
* @throws InvalidSPDXAnalysisException on unexpected errors
*/
@Test
public void testVerifyCollection() throws InvalidSPDXAnalysisException {
Expand All @@ -134,7 +136,7 @@ public void testSetStrict() {

/**
* Test method for {@link org.spdx.core.CoreModelObject#getPropertyValueDescriptors()}.
* @throws InvalidSPDXAnalysisException
* @throws InvalidSPDXAnalysisException on unexpected errors
*/
@Test
public void testGetPropertyValueDescriptors() throws InvalidSPDXAnalysisException {
Expand Down Expand Up @@ -163,7 +165,7 @@ public void testGetPropertyValueDescriptors() throws InvalidSPDXAnalysisExceptio

/**
* Test method for {@link org.spdx.core.CoreModelObject#getObjectPropertyValue(org.spdx.storage.PropertyDescriptor)}.
* @throws InvalidSPDXAnalysisException
* @throws InvalidSPDXAnalysisException on unexpected errors
*/
@Test
public void testGetObjectPropertyValue() throws InvalidSPDXAnalysisException {
Expand All @@ -179,7 +181,7 @@ public void testGetObjectPropertyValue() throws InvalidSPDXAnalysisException {

/**
* Test method for {@link org.spdx.core.CoreModelObject#setPropertyValue(org.spdx.storage.PropertyDescriptor, java.lang.Object)}.
* @throws InvalidSPDXAnalysisException
* @throws InvalidSPDXAnalysisException on unexpected errors
*/
@Test
public void testSetPropertyValue() throws InvalidSPDXAnalysisException {
Expand Down Expand Up @@ -223,7 +225,7 @@ public void testSetPropertyValue() throws InvalidSPDXAnalysisException {

/**
* Test method for {@link org.spdx.core.CoreModelObject#removeProperty(org.spdx.storage.PropertyDescriptor)}.
* @throws InvalidSPDXAnalysisException
* @throws InvalidSPDXAnalysisException on unexpected errors
*/
@Test
public void testRemoveProperty() throws InvalidSPDXAnalysisException {
Expand All @@ -239,7 +241,7 @@ public void testRemoveProperty() throws InvalidSPDXAnalysisException {

/**
* Test method for {@link org.spdx.core.CoreModelObject#clearValueCollection(org.spdx.storage.PropertyDescriptor)}.
* @throws InvalidSPDXAnalysisException
* @throws InvalidSPDXAnalysisException on unexpected errors
*/
@Test
public void testClearValueCollection() throws InvalidSPDXAnalysisException {
Expand All @@ -258,7 +260,7 @@ public void testClearValueCollection() throws InvalidSPDXAnalysisException {

/**
* Test method for {@link org.spdx.core.CoreModelObject#addPropertyValueToCollection(org.spdx.storage.PropertyDescriptor, java.lang.Object)}.
* @throws InvalidSPDXAnalysisException
* @throws InvalidSPDXAnalysisException on unexpected errors
*/
@Test
public void testAddPropertyValueToCollection() throws InvalidSPDXAnalysisException {
Expand All @@ -273,7 +275,7 @@ public void testAddPropertyValueToCollection() throws InvalidSPDXAnalysisExcepti

/**
* Test method for {@link org.spdx.core.CoreModelObject#removePropertyValueFromCollection(org.spdx.storage.PropertyDescriptor, java.lang.Object)}.
* @throws InvalidSPDXAnalysisException
* @throws InvalidSPDXAnalysisException on unexpected errors
*/
@Test
public void testRemovePropertyValueFromCollection() throws InvalidSPDXAnalysisException {
Expand All @@ -288,7 +290,7 @@ public void testRemovePropertyValueFromCollection() throws InvalidSPDXAnalysisEx

/**
* Test method for {@link org.spdx.core.CoreModelObject#getObjectPropertyValueSet(org.spdx.storage.PropertyDescriptor, java.lang.Class)}.
* @throws InvalidSPDXAnalysisException
* @throws InvalidSPDXAnalysisException on unexpected errors
*/
@Test
public void testGetObjectPropertyValueSet() throws InvalidSPDXAnalysisException {
Expand All @@ -305,7 +307,7 @@ public void testGetObjectPropertyValueSet() throws InvalidSPDXAnalysisException

/**
* Test method for {@link org.spdx.core.CoreModelObject#getObjectPropertyValueCollection(org.spdx.storage.PropertyDescriptor, java.lang.Class)}.
* @throws InvalidSPDXAnalysisException
* @throws InvalidSPDXAnalysisException on unexpected errors
*/
@Test
public void testGetObjectPropertyValueCollection() throws InvalidSPDXAnalysisException {
Expand All @@ -322,7 +324,7 @@ public void testGetObjectPropertyValueCollection() throws InvalidSPDXAnalysisExc

/**
* Test method for {@link org.spdx.core.CoreModelObject#equivalent(org.spdx.core.CoreModelObject, boolean)}.
* @throws InvalidSPDXAnalysisException
* @throws InvalidSPDXAnalysisException on unexpected errors
*/
@Test
public void testEquivalentCoreModelObjectBoolean() throws InvalidSPDXAnalysisException {
Expand Down Expand Up @@ -371,7 +373,7 @@ public void testEquivalentCoreModelObjectBoolean() throws InvalidSPDXAnalysisExc

/**
* Test method for {@link org.spdx.core.CoreModelObject#toTypedValue()}.
* @throws InvalidSPDXAnalysisException
* @throws InvalidSPDXAnalysisException on unexpected errors
*/
@Test
public void testToTypedValue() throws InvalidSPDXAnalysisException {
Expand All @@ -381,4 +383,13 @@ public void testToTypedValue() throws InvalidSPDXAnalysisException {
assertEquals("3.0.0", result.getSpecVersion());
}

@Test
public void testGetId() throws InvalidSPDXAnalysisException {
CoreModelObject prefix = new MockModelType(modelStore, OBJECT_URI, copyManager, true, "3.0.0");
prefix.setIdPrefix(PREFIX);
assertEquals(ID, prefix.getId());
CoreModelObject noPrefix = new MockModelType(modelStore, OBJECT_URI, copyManager, true, "3.0.0");
assertEquals(OBJECT_URI, noPrefix.getId());
}

}