Skip to content

Commit

Permalink
Merge pull request #2068 from NCEAS/bug-1708-null-format-id
Browse files Browse the repository at this point in the history
Check if the format id is null in create/update
  • Loading branch information
taojing2002 authored Feb 12, 2025
2 parents b5869c5 + 94cc682 commit 8a5e620
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/edu/ucsb/nceas/metacat/dataone/D1NodeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,14 @@ public Identifier create(
"1180", "The checksum object from the system metadata shouldn't be null.");
}

if (sysmeta.getFormatId() == null || sysmeta.getFormatId().getValue().isBlank()) {
logMetacat.error(
"D1NodeService.create - the format id from the system metadata shouldn't be"
+ " null for the object " + pid.getValue());
throw new InvalidSystemMetadata(
"1180", "The the format id from the system metadata shouldn't be null.");
}

try {
String docType;
if (isScienceMetadata(sysmeta)) {
Expand Down
6 changes: 6 additions & 0 deletions src/edu/ucsb/nceas/metacat/dataone/MNodeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,12 @@ public Identifier update(Session session, Identifier pid, InputStream object, Id
"1202",
"The new identifier " + newPid.getValue() + " cannot " + "update itself.");
}

if (sysmeta.getFormatId() == null || sysmeta.getFormatId().getValue().isBlank()) {
throw new InvalidSystemMetadata(
"1300", "The the format id from the system metadata shouldn't be null.");
}

// lock existing pid
SystemMetadataManager.lock(pid);
// make sure that the newPid doesn't exists
Expand Down
36 changes: 36 additions & 0 deletions test/edu/ucsb/nceas/metacat/dataone/CNodeServiceIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.dataone.service.exceptions.BaseException;
import org.dataone.service.exceptions.IdentifierNotUnique;
import org.dataone.service.exceptions.InvalidRequest;
import org.dataone.service.exceptions.InvalidSystemMetadata;
import org.dataone.service.exceptions.NotAuthorized;
import org.dataone.service.exceptions.NotFound;
import org.dataone.service.exceptions.NotImplemented;
Expand Down Expand Up @@ -244,6 +245,41 @@ public void testCreate() {
}
}

/**
* Test object creation without object format id in the system metadata
*/
@Test
public void testCreateWithoutObjectFormatId() throws Exception {
D1NodeServiceTest.printTestHeader("testCreateWithoutObjectFormatId");
Identifier guid = new Identifier();
guid.setValue("testCreate." + System.currentTimeMillis());
Session session =d1NodeServiceTest.getCNSession();
try {
InputStream object =
new ByteArrayInputStream("test".getBytes(StandardCharsets.UTF_8));
SystemMetadata sysmeta =
D1NodeServiceTest.createSystemMetadata(guid, session.getSubject(), object);
sysmeta.setFormatId(null);
Identifier pid = d1NodeServiceTest.cnCreate(session, guid, object, sysmeta);
fail("It should fail since the format id is null.");
} catch (Exception e) {
assertTrue(e instanceof InvalidSystemMetadata);
}
try {
InputStream object =
new ByteArrayInputStream("test".getBytes(StandardCharsets.UTF_8));
SystemMetadata sysmeta =
D1NodeServiceTest.createSystemMetadata(guid, session.getSubject(), object);
ObjectFormatIdentifier formatIdentifier = new ObjectFormatIdentifier();
formatIdentifier.setValue("");
sysmeta.setFormatId(formatIdentifier);
Identifier pid = d1NodeServiceTest.cnCreate(session, guid, object, sysmeta);
fail("It should fail since the format id is blank.");
} catch (Exception e) {
assertTrue(e instanceof InvalidSystemMetadata);
}
}

/**
* Test the scenario that the pid doesn't match the one in the system metadata
* in the create request
Expand Down
85 changes: 85 additions & 0 deletions test/edu/ucsb/nceas/metacat/dataone/MNodeServiceIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,42 @@ public void testCreate() {
}
}

/**
* Test object creation without object format id in the system metadata
*/
@Test
public void testCreateWithoutObjectFormatId() throws Exception {
D1NodeServiceTest.printTestHeader("testCreateWithoutObjectFormatId");
Identifier guid = new Identifier();
guid.setValue("testCreate." + System.currentTimeMillis());
Session session = null;
session = d1NodeTest.getTestSession();
try {
InputStream object =
new ByteArrayInputStream("test".getBytes(StandardCharsets.UTF_8));
SystemMetadata sysmeta =
D1NodeServiceTest.createSystemMetadata(guid, session.getSubject(), object);
sysmeta.setFormatId(null);
Identifier pid = d1NodeTest.mnCreate(session, guid, object, sysmeta);
fail("It should fail since the format id is null.");
} catch (Exception e) {
assertTrue(e instanceof InvalidSystemMetadata);
}
try {
InputStream object =
new ByteArrayInputStream("test".getBytes(StandardCharsets.UTF_8));
SystemMetadata sysmeta =
D1NodeServiceTest.createSystemMetadata(guid, session.getSubject(), object);
ObjectFormatIdentifier formatIdentifier = new ObjectFormatIdentifier();
formatIdentifier.setValue("");
sysmeta.setFormatId(formatIdentifier);
Identifier pid = d1NodeTest.mnCreate(session, guid, object, sysmeta);
fail("It should fail since the format id is blank.");
} catch (Exception e) {
assertTrue(e instanceof InvalidSystemMetadata);
}
}

/**
* Test object creation
*/
Expand Down Expand Up @@ -786,6 +822,55 @@ public void testUpdate() {
}
}

/**
* Test to update objects with the system metadata without the format id
*/
@Test
public void testUpdateWithoutFormatId() {
D1NodeServiceTest.printTestHeader("testUpdateWiouthFormatId");
try {
Session session = d1NodeTest.getTestSession();
Identifier guid = new Identifier();
guid.setValue("testUpdate." + System.currentTimeMillis());
InputStream object =
new ByteArrayInputStream("test".getBytes(StandardCharsets.UTF_8));
SystemMetadata sysmeta =
D1NodeServiceTest.createSystemMetadata(guid, session.getSubject(), object);
// original
Identifier pid =
d1NodeTest.mnCreate(session, guid, object, sysmeta);

//Test the case that format id is null
Identifier newPid = new Identifier();
newPid.setValue(
"testUpdate." + (System.currentTimeMillis() + 1));
object = new ByteArrayInputStream("test".getBytes(StandardCharsets.UTF_8));
SystemMetadata newMeta =
D1NodeServiceTest.createSystemMetadata(newPid, session.getSubject(), object);
newMeta.setFormatId(null);
object = new ByteArrayInputStream("test".getBytes(StandardCharsets.UTF_8));
try {
d1NodeTest.mnUpdate(session, pid, object, newPid, newMeta);
fail("we shouldn't get here since the format id is null");
} catch (Exception e) {
assertTrue(e instanceof InvalidSystemMetadata);
}

object = new ByteArrayInputStream("test".getBytes(StandardCharsets.UTF_8));
ObjectFormatIdentifier formatIdentifier = new ObjectFormatIdentifier();
formatIdentifier.setValue("");
newMeta.setFormatId(formatIdentifier);
try {
d1NodeTest.mnUpdate(session, pid, object, newPid, newMeta);
fail("we shouldn't get here since the format id is blank");
} catch (Exception e) {
assertTrue(e instanceof InvalidSystemMetadata);
}
} catch (Exception e) {
fail("Unexpected error: " + e.getMessage());
}
}

/**
* Test object updating
*/
Expand Down

0 comments on commit 8a5e620

Please sign in to comment.