From 8904f9bd9114eddba8b8c003be89994af5a281a5 Mon Sep 17 00:00:00 2001 From: Ryan Garvey rpgarve Date: Tue, 7 Jan 2025 21:07:37 +0000 Subject: [PATCH] Updates based on code review --- .../java/emissary/core/DataObjectFactory.java | 19 +++++++++++++++ .../emissary/core/BaseDataObjectTest.java | 20 ++++++++++++++++ .../emissary/core/DataObjectFactoryTest.java | 23 +++++++++++++++++++ 3 files changed, 62 insertions(+) diff --git a/src/main/java/emissary/core/DataObjectFactory.java b/src/main/java/emissary/core/DataObjectFactory.java index 864904a3db..4845ff6c20 100755 --- a/src/main/java/emissary/core/DataObjectFactory.java +++ b/src/main/java/emissary/core/DataObjectFactory.java @@ -114,6 +114,15 @@ public static IBaseDataObject getInstance(final byte[] payload, final String fil return getInstance(payload, filename, fileTypeAndForm, fileTypeAndForm); } + /** + * Get an instance of the configured DataObject impl with filename, form, and file type set, and top level document + * + * @param payload the payload data + * @param filename the filename + * @param fileTypeAndForm the form and filetype to set on the IBDO + * @param tld The top level document + * @return an IBDO with the payload, filename, top level document set with the file type and form set to the same value + */ public static IBaseDataObject getInstance(final byte[] payload, final String filename, final String fileTypeAndForm, IBaseDataObject tld) { final Object o = Factory.create(clazz, payload, filename, fileTypeAndForm, tld); return (IBaseDataObject) o; @@ -133,6 +142,16 @@ public static IBaseDataObject getInstance(final byte[] payload, final String fil return (IBaseDataObject) o; } + /** + * Get an instance of the configured DataObject impl with filename, form, file type, and top level document set + * + * @param payload the payload data + * @param filename the filename + * @param form the form to set on the IBDO + * @param fileType the file type to set on the IBDO + * @param tld The top level document + * @return an IBDO with the payload, filename, file type, form, and top level document set + */ public static IBaseDataObject getInstance(final byte[] payload, final String filename, final String form, final String fileType, IBaseDataObject tld) { final Object o = Factory.create(clazz, payload, filename, form, fileType, tld); diff --git a/src/test/java/emissary/core/BaseDataObjectTest.java b/src/test/java/emissary/core/BaseDataObjectTest.java index 3cbb388761..23adb8e697 100755 --- a/src/test/java/emissary/core/BaseDataObjectTest.java +++ b/src/test/java/emissary/core/BaseDataObjectTest.java @@ -25,6 +25,7 @@ import java.lang.reflect.Field; import java.nio.ByteBuffer; import java.nio.channels.SeekableByteChannel; +import java.nio.charset.StandardCharsets; import java.time.Instant; import java.util.ArrayList; import java.util.Arrays; @@ -87,6 +88,25 @@ void testConstructors() { final BaseDataObject b3 = new BaseDataObject("test".getBytes(), "filename.txt", null); assertEquals("", b3.currentForm(), "Current form with null in ctor"); assertNotNull(b3.getCreationTimestamp()); + + final BaseDataObject tld = new BaseDataObject(); + final byte[] data = "content".getBytes(StandardCharsets.UTF_8); + final String fileName = "aChild"; + final String form = "UNKNOWN"; + final BaseDataObject b4 = new BaseDataObject(data, fileName, form, tld); + assertEquals(fileName, b4.getFilename()); + assertEquals(form, b4.currentForm()); + assertNotNull(b4.getCreationTimestamp()); + assertEquals(tld, b4.getTld()); + + final String fileType = "TEXT"; + BaseDataObject b5 = new BaseDataObject(data, fileName, form, fileType, tld); + assertEquals(fileName, b5.getFilename()); + assertEquals(form, b5.currentForm()); + assertEquals(fileType, b5.getFileType()); + assertNotNull(b5.getCreationTimestamp()); + assertEquals(tld, b5.getTld()); + } @Test diff --git a/src/test/java/emissary/core/DataObjectFactoryTest.java b/src/test/java/emissary/core/DataObjectFactoryTest.java index 3cce9775d4..f06f130aa4 100755 --- a/src/test/java/emissary/core/DataObjectFactoryTest.java +++ b/src/test/java/emissary/core/DataObjectFactoryTest.java @@ -6,6 +6,8 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import java.nio.charset.StandardCharsets; + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -106,6 +108,27 @@ void testFormAndFileType() { assertSame(testPayload, extract.data()); } + @Test + void testTLD() { + BaseDataObject tld = new BaseDataObject(); + final byte[] data = "content".getBytes(StandardCharsets.UTF_8); + final String fileName = "aChild"; + final String form = "UNKNOWN"; + IBaseDataObject ibdo = DataObjectFactory.getInstance(data, fileName, form, tld); + assertEquals(fileName, ibdo.getFilename()); + assertEquals(form, ibdo.currentForm()); + assertNotNull(ibdo.getCreationTimestamp()); + assertEquals(tld, ibdo.getTld()); + + final String fileType = "TEXT"; + ibdo = DataObjectFactory.getInstance(data, fileName, form, fileType, tld); + assertEquals(fileName, ibdo.getFilename()); + assertEquals(form, ibdo.currentForm()); + assertEquals(fileType, ibdo.getFileType()); + assertNotNull(ibdo.getCreationTimestamp()); + assertEquals(tld, ibdo.getTld()); + } + @SuppressWarnings("unused") public static class MyDataObject extends BaseDataObject { private static final long serialVersionUID = -2254597461746556210L;