Skip to content

Commit

Permalink
Add build from ObjectInformation method
Browse files Browse the repository at this point in the history
Will help a lot with some tests I'm writing
  • Loading branch information
MrCreosote committed Oct 19, 2023
1 parent fe8df81 commit 42ae25c
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 20 deletions.
46 changes: 37 additions & 9 deletions src/us/kbase/workspace/database/ObjectInformation.java
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,17 @@ public static Builder getBuilder() {
return new Builder();
}

/** Get a builder for an {@link ObjectInformation}.
*
* Note that all fields other than the metadata are required, but all the fields are settable
* by builder methods for readability purposes.
* @param info an object info instance from which to populate the builder.
* @return the builder.
*/
public static Builder getBuilder(final ObjectInformation info) {
return new Builder(info);
}

/**
* A builder for an {@link ObjectInformation}.
*
Expand All @@ -271,21 +282,38 @@ public static class Builder {
// required fields
private long objectId = -1;
private String objectName = null;
private AbsoluteTypeDefId type = null;
private String type = null;
private Instant savedDate = null;
private int version = -1;
private WorkspaceUser savedBy = null;
private ResolvedWorkspaceID ws = null;
private long wsid = -1;
private String wsname = null;
private String chksum = null;
private long size = -1;
private List<Reference> refpath = null;

// optional fields
private UncheckedUserMetadata usermeta = null;
private UncheckedUserMetadata adminmeta = null;


private Builder() {}

private Builder(final ObjectInformation info) {
objectId = requireNonNull(info, "info").id;
objectName = info.name;
type = info.type;
savedDate = info.savedDate;
version = info.version;
savedBy = info.savedBy;
wsid = info.workspaceID;
wsname = info.workspaceName;
chksum = info.chksum;
size = info.size;
refpath = info.refpath;
usermeta = info.usermeta;
adminmeta = info.adminmeta;
}

/** Add the required object ID to the builder.
* @param id the object ID.
* @return this builder for chaining.
Expand Down Expand Up @@ -327,7 +355,7 @@ public Builder withObjectName(final String name) {
* @return this builder for chaining.
*/
public Builder withType(final AbsoluteTypeDefId type) {
this.type = requireNonNull(type, "type");
this.type = requireNonNull(type, "type").getTypeString();
return this;
}

Expand Down Expand Up @@ -373,7 +401,8 @@ public Builder withSavedBy(final WorkspaceUser user) {
* @return this builder for chaining.
*/
public Builder withWorkspace(final ResolvedWorkspaceID workspace) {
this.ws = requireNonNull(workspace, "workspace");
this.wsid = requireNonNull(workspace, "workspace").getID();
this.wsname = workspace.getName();
return this;
}

Expand Down Expand Up @@ -443,15 +472,14 @@ public ObjectInformation build() {
savedDate == null ||
version < 1 ||
savedBy == null ||
ws == null ||
wsid < 1 || // means wsname is set
chksum == null ||
size < 1) {
throw new IllegalArgumentException("One or more of the required arguments are "
+ "not set. Please check the documentation for the builder.");
}
return new ObjectInformation(objectId, objectName, type.getTypeString(), savedDate,
version, savedBy, ws.getID(), ws.getName(), chksum, size, usermeta, adminmeta,
null);
return new ObjectInformation(objectId, objectName, type, savedDate,
version, savedBy, wsid, wsname, chksum, size, usermeta, adminmeta, refpath);
}
}

Expand Down
70 changes: 59 additions & 11 deletions src/us/kbase/workspace/test/workspace/ObjectInformationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,7 @@ public void equals() throws Exception {

@Test
public void buildMinimalWithStandardInputs() {
final ObjectInformation oi = ObjectInformation.getBuilder()
.withObjectID(1)
.withObjectName("foo")
.withType(TYPE)
.withSavedDate(inst(10000))
.withVersion(3)
.withSavedBy(new WorkspaceUser("bar"))
.withWorkspace(new ResolvedWorkspaceID(4, "whee", false, false))
.withChecksum(MDFIVE)
.withSize(5)
.build();
final ObjectInformation oi = minimalOI();
assertThat("incorrect obj id", oi.getObjectId(), is(1L));
assertThat("incorrect obj name", oi.getObjectName(), is("foo"));
assertThat("incorrect obj type", oi.getTypeString(), is("type.t-3.7"));
Expand All @@ -78,6 +68,20 @@ public void buildMinimalWithStandardInputs() {
assertThat("incorrect ref path", oi.getReferencePath(),
is(Arrays.asList(new Reference(4, 1, 3))));
}

private ObjectInformation minimalOI() {
return ObjectInformation.getBuilder()
.withObjectID(1)
.withObjectName("foo")
.withType(TYPE)
.withSavedDate(inst(10000))
.withVersion(3)
.withSavedBy(new WorkspaceUser("bar"))
.withWorkspace(new ResolvedWorkspaceID(4, "whee", false, false))
.withChecksum(MDFIVE)
.withSize(5)
.build();
}

private ObjectInformation.Builder buildFullWithAlternativeInputsSetup() {
return ObjectInformation.getBuilder()
Expand Down Expand Up @@ -185,6 +189,40 @@ private void buildFullWithAlternativeInputsSharedChecks(final ObjectInformation
is(Arrays.asList(new Reference(1, 67, 24))));
}

@Test
public void buildFromObjectInfoMinimal() throws Exception {
final ObjectInformation source = minimalOI();
final ObjectInformation target = ObjectInformation.getBuilder(source).build();

assertThat("incorrect build", target, is(source));
}

@Test
public void buildFromObjectInfoMaximal() throws Exception {
final ObjectInformation source = buildFullWithAlternativeInputsSetup()
.withUserMetadata(new UncheckedUserMetadata(ImmutableMap.of("a", "b")))
.withAdminUserMetadata(new UncheckedUserMetadata(ImmutableMap.of("e", "f")))
.build();
final ObjectInformation target = ObjectInformation.getBuilder(source).build();

assertThat("incorrect build", target, is(source));
}

@Test
public void buildFromObjectInfoWithRefPath() throws Exception {
final ObjectInformation source = buildFullWithAlternativeInputsSetup()
.withUserMetadata(new UncheckedUserMetadata(ImmutableMap.of("a", "b")))
.withAdminUserMetadata(new UncheckedUserMetadata(ImmutableMap.of("e", "f")))
.build()
.updateReferencePath(Arrays.asList(
new Reference(3, 4, 5), new Reference(1, 67, 24)));
final ObjectInformation target = ObjectInformation.getBuilder(source).build();

assertThat("incorrect build", target, is(source));
assertThat("incorrect ref path", target.getReferencePath(), is(Arrays.asList(
new Reference(3, 4, 5), new Reference(1, 67, 24))));
}

@Test
public void refPathImmutable() throws Exception {
final ObjectInformation oi = ObjectInformation.getBuilder()
Expand Down Expand Up @@ -428,6 +466,16 @@ private void failBuildMissingFields(
+ "Please check the documentation for the builder."));
}
}

@Test
public void buildFailFromObjectInformation() throws Exception {
try {
ObjectInformation.getBuilder(null);
fail("expected exception");
} catch (Exception got) {
TestCommon.assertExceptionCorrect(got, new NullPointerException("info"));
}
}

@Test
public void refPath() {
Expand Down

0 comments on commit 42ae25c

Please sign in to comment.