Skip to content

Commit 8a5e620

Browse files
authored
Merge pull request #2068 from NCEAS/bug-1708-null-format-id
Check if the format id is null in create/update
2 parents b5869c5 + 94cc682 commit 8a5e620

File tree

4 files changed

+135
-0
lines changed

4 files changed

+135
-0
lines changed

src/edu/ucsb/nceas/metacat/dataone/D1NodeService.java

+8
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,14 @@ public Identifier create(
396396
"1180", "The checksum object from the system metadata shouldn't be null.");
397397
}
398398

399+
if (sysmeta.getFormatId() == null || sysmeta.getFormatId().getValue().isBlank()) {
400+
logMetacat.error(
401+
"D1NodeService.create - the format id from the system metadata shouldn't be"
402+
+ " null for the object " + pid.getValue());
403+
throw new InvalidSystemMetadata(
404+
"1180", "The the format id from the system metadata shouldn't be null.");
405+
}
406+
399407
try {
400408
String docType;
401409
if (isScienceMetadata(sysmeta)) {

src/edu/ucsb/nceas/metacat/dataone/MNodeService.java

+6
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,12 @@ public Identifier update(Session session, Identifier pid, InputStream object, Id
411411
"1202",
412412
"The new identifier " + newPid.getValue() + " cannot " + "update itself.");
413413
}
414+
415+
if (sysmeta.getFormatId() == null || sysmeta.getFormatId().getValue().isBlank()) {
416+
throw new InvalidSystemMetadata(
417+
"1300", "The the format id from the system metadata shouldn't be null.");
418+
}
419+
414420
// lock existing pid
415421
SystemMetadataManager.lock(pid);
416422
// make sure that the newPid doesn't exists

test/edu/ucsb/nceas/metacat/dataone/CNodeServiceIT.java

+36
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.dataone.service.exceptions.BaseException;
1616
import org.dataone.service.exceptions.IdentifierNotUnique;
1717
import org.dataone.service.exceptions.InvalidRequest;
18+
import org.dataone.service.exceptions.InvalidSystemMetadata;
1819
import org.dataone.service.exceptions.NotAuthorized;
1920
import org.dataone.service.exceptions.NotFound;
2021
import org.dataone.service.exceptions.NotImplemented;
@@ -244,6 +245,41 @@ public void testCreate() {
244245
}
245246
}
246247

248+
/**
249+
* Test object creation without object format id in the system metadata
250+
*/
251+
@Test
252+
public void testCreateWithoutObjectFormatId() throws Exception {
253+
D1NodeServiceTest.printTestHeader("testCreateWithoutObjectFormatId");
254+
Identifier guid = new Identifier();
255+
guid.setValue("testCreate." + System.currentTimeMillis());
256+
Session session =d1NodeServiceTest.getCNSession();
257+
try {
258+
InputStream object =
259+
new ByteArrayInputStream("test".getBytes(StandardCharsets.UTF_8));
260+
SystemMetadata sysmeta =
261+
D1NodeServiceTest.createSystemMetadata(guid, session.getSubject(), object);
262+
sysmeta.setFormatId(null);
263+
Identifier pid = d1NodeServiceTest.cnCreate(session, guid, object, sysmeta);
264+
fail("It should fail since the format id is null.");
265+
} catch (Exception e) {
266+
assertTrue(e instanceof InvalidSystemMetadata);
267+
}
268+
try {
269+
InputStream object =
270+
new ByteArrayInputStream("test".getBytes(StandardCharsets.UTF_8));
271+
SystemMetadata sysmeta =
272+
D1NodeServiceTest.createSystemMetadata(guid, session.getSubject(), object);
273+
ObjectFormatIdentifier formatIdentifier = new ObjectFormatIdentifier();
274+
formatIdentifier.setValue("");
275+
sysmeta.setFormatId(formatIdentifier);
276+
Identifier pid = d1NodeServiceTest.cnCreate(session, guid, object, sysmeta);
277+
fail("It should fail since the format id is blank.");
278+
} catch (Exception e) {
279+
assertTrue(e instanceof InvalidSystemMetadata);
280+
}
281+
}
282+
247283
/**
248284
* Test the scenario that the pid doesn't match the one in the system metadata
249285
* in the create request

test/edu/ucsb/nceas/metacat/dataone/MNodeServiceIT.java

+85
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,42 @@ public void testCreate() {
324324
}
325325
}
326326

327+
/**
328+
* Test object creation without object format id in the system metadata
329+
*/
330+
@Test
331+
public void testCreateWithoutObjectFormatId() throws Exception {
332+
D1NodeServiceTest.printTestHeader("testCreateWithoutObjectFormatId");
333+
Identifier guid = new Identifier();
334+
guid.setValue("testCreate." + System.currentTimeMillis());
335+
Session session = null;
336+
session = d1NodeTest.getTestSession();
337+
try {
338+
InputStream object =
339+
new ByteArrayInputStream("test".getBytes(StandardCharsets.UTF_8));
340+
SystemMetadata sysmeta =
341+
D1NodeServiceTest.createSystemMetadata(guid, session.getSubject(), object);
342+
sysmeta.setFormatId(null);
343+
Identifier pid = d1NodeTest.mnCreate(session, guid, object, sysmeta);
344+
fail("It should fail since the format id is null.");
345+
} catch (Exception e) {
346+
assertTrue(e instanceof InvalidSystemMetadata);
347+
}
348+
try {
349+
InputStream object =
350+
new ByteArrayInputStream("test".getBytes(StandardCharsets.UTF_8));
351+
SystemMetadata sysmeta =
352+
D1NodeServiceTest.createSystemMetadata(guid, session.getSubject(), object);
353+
ObjectFormatIdentifier formatIdentifier = new ObjectFormatIdentifier();
354+
formatIdentifier.setValue("");
355+
sysmeta.setFormatId(formatIdentifier);
356+
Identifier pid = d1NodeTest.mnCreate(session, guid, object, sysmeta);
357+
fail("It should fail since the format id is blank.");
358+
} catch (Exception e) {
359+
assertTrue(e instanceof InvalidSystemMetadata);
360+
}
361+
}
362+
327363
/**
328364
* Test object creation
329365
*/
@@ -786,6 +822,55 @@ public void testUpdate() {
786822
}
787823
}
788824

825+
/**
826+
* Test to update objects with the system metadata without the format id
827+
*/
828+
@Test
829+
public void testUpdateWithoutFormatId() {
830+
D1NodeServiceTest.printTestHeader("testUpdateWiouthFormatId");
831+
try {
832+
Session session = d1NodeTest.getTestSession();
833+
Identifier guid = new Identifier();
834+
guid.setValue("testUpdate." + System.currentTimeMillis());
835+
InputStream object =
836+
new ByteArrayInputStream("test".getBytes(StandardCharsets.UTF_8));
837+
SystemMetadata sysmeta =
838+
D1NodeServiceTest.createSystemMetadata(guid, session.getSubject(), object);
839+
// original
840+
Identifier pid =
841+
d1NodeTest.mnCreate(session, guid, object, sysmeta);
842+
843+
//Test the case that format id is null
844+
Identifier newPid = new Identifier();
845+
newPid.setValue(
846+
"testUpdate." + (System.currentTimeMillis() + 1));
847+
object = new ByteArrayInputStream("test".getBytes(StandardCharsets.UTF_8));
848+
SystemMetadata newMeta =
849+
D1NodeServiceTest.createSystemMetadata(newPid, session.getSubject(), object);
850+
newMeta.setFormatId(null);
851+
object = new ByteArrayInputStream("test".getBytes(StandardCharsets.UTF_8));
852+
try {
853+
d1NodeTest.mnUpdate(session, pid, object, newPid, newMeta);
854+
fail("we shouldn't get here since the format id is null");
855+
} catch (Exception e) {
856+
assertTrue(e instanceof InvalidSystemMetadata);
857+
}
858+
859+
object = new ByteArrayInputStream("test".getBytes(StandardCharsets.UTF_8));
860+
ObjectFormatIdentifier formatIdentifier = new ObjectFormatIdentifier();
861+
formatIdentifier.setValue("");
862+
newMeta.setFormatId(formatIdentifier);
863+
try {
864+
d1NodeTest.mnUpdate(session, pid, object, newPid, newMeta);
865+
fail("we shouldn't get here since the format id is blank");
866+
} catch (Exception e) {
867+
assertTrue(e instanceof InvalidSystemMetadata);
868+
}
869+
} catch (Exception e) {
870+
fail("Unexpected error: " + e.getMessage());
871+
}
872+
}
873+
789874
/**
790875
* Test object updating
791876
*/

0 commit comments

Comments
 (0)