Skip to content

Commit

Permalink
Merge pull request #691 from MrCreosote/develop
Browse files Browse the repository at this point in the history
Add administrative user metadata to ObjectInformation.
  • Loading branch information
MrCreosote authored Oct 12, 2023
2 parents 756aef4 + d8a2172 commit 8a48fc9
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 23 deletions.
54 changes: 43 additions & 11 deletions src/us/kbase/workspace/database/ObjectInformation.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public class ObjectInformation {
final private String workspaceName;
final private String chksum;
final private long size;
final private UncheckedUserMetadata meta;
final private UncheckedUserMetadata usermeta;
final private UncheckedUserMetadata adminmeta;
final private List<Reference> refpath;

private ObjectInformation(
Expand All @@ -48,7 +49,8 @@ private ObjectInformation(
final String workspaceName,
final String chksum,
final long size,
final UncheckedUserMetadata meta,
final UncheckedUserMetadata usermeta,
final UncheckedUserMetadata adminmeta,
final List<Reference> refpath) {
this.id = id;
this.name = name;
Expand All @@ -60,7 +62,8 @@ private ObjectInformation(
this.workspaceName = workspaceName;
this.chksum = chksum;
this.size = size;
this.meta = meta;
this.usermeta = usermeta;
this.adminmeta = adminmeta;
if (refpath == null) {
//could leave this as null and construct as needed to save mem, but meh for now
this.refpath = Arrays.asList(new Reference(workspaceId, id, version));
Expand Down Expand Up @@ -143,15 +146,30 @@ public String getCheckSum() {
* @return the object metadata.
*/
public Optional<UncheckedUserMetadata> getUserMetaData() {
return Optional.ofNullable(meta);
return Optional.ofNullable(usermeta);
}

/** Returns the user supplied and automatically generated metadata for the object as a map
* or null if no user metadata was provided.
* @return the metadata or null.
*/
public Map<String, String> getUserMetaDataMapOrNull() {
return meta == null ? null : meta.getMetadata();
return usermeta == null ? null : usermeta.getMetadata();
}

/** Returns the administrative user supplied metadata for the object.
* @return the object metadata.
*/
public Optional<UncheckedUserMetadata> getAdminUserMetaData() {
return Optional.ofNullable(adminmeta);
}

/** Returns the administrative user supplied metadata for the object as a map
* or null if no metadata was provided.
* @return the metadata or null.
*/
public Map<String, String> getAdminUserMetaDataMapOrNull() {
return adminmeta == null ? null : adminmeta.getMetadata();
}

/** Returns the resolved reference path to this object from a user-accessible object. There may
Expand Down Expand Up @@ -181,7 +199,7 @@ public ObjectInformation updateReferencePath(final List<Reference> refpath) {
"refpath must end with the same reference as the current refpath");
}
return new ObjectInformation(id, name, type, savedDate, version, savedBy, workspaceID,
workspaceName, chksum, size, meta, refpath);
workspaceName, chksum, size, usermeta, adminmeta, refpath);
}

private Reference getLast(final List<Reference> refpath) {
Expand All @@ -190,8 +208,8 @@ private Reference getLast(final List<Reference> refpath) {

@Override
public int hashCode() {
return Objects.hash(chksum, id, meta, name, refpath, savedBy, savedDate, size, type,
version, workspaceID, workspaceName);
return Objects.hash(adminmeta, chksum, id, name, refpath, savedBy, savedDate, size, type,
usermeta, version, workspaceID, workspaceName);
}

@Override
Expand All @@ -206,15 +224,16 @@ public boolean equals(Object obj) {
return false;
}
ObjectInformation other = (ObjectInformation) obj;
return Objects.equals(chksum, other.chksum)
return Objects.equals(adminmeta, other.adminmeta)
&& Objects.equals(chksum, other.chksum)
&& id == other.id
&& Objects.equals(meta, other.meta)
&& Objects.equals(name, other.name)
&& Objects.equals(refpath, other.refpath)
&& Objects.equals(savedBy, other.savedBy)
&& Objects.equals(savedDate, other.savedDate)
&& size == other.size
&& Objects.equals(type, other.type)
&& Objects.equals(usermeta, other.usermeta)
&& version == other.version
&& workspaceID == other.workspaceID
&& Objects.equals(workspaceName, other.workspaceName);
Expand Down Expand Up @@ -251,6 +270,7 @@ public static class Builder {

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


private Builder() {}
Expand Down Expand Up @@ -387,6 +407,17 @@ public Builder withUserMetadata(final UncheckedUserMetadata metadata) {
return this;
}

/** Add the optional administrative user provided metadata to the builder.
*
* A null argument will remove any previously set metadata.
* @param metadata the metadata
* @return this builder for chaining.
*/
public Builder withAdminUserMetadata(final UncheckedUserMetadata metadata) {
this.adminmeta = metadata;
return this;
}

/** Create the {@link ObjectInformation}. All fields are required other than the metadata.
* @return the new {@link ObjectInformation}.
*/
Expand All @@ -404,7 +435,8 @@ public ObjectInformation build() {
+ "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, null);
version, savedBy, ws.getID(), ws.getName(), chksum, size, usermeta, adminmeta,
null);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/us/kbase/workspace/database/UncheckedUserMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import java.util.HashMap;
import java.util.Map;

/** User provided metadata container. This class performs no checks on the
* metadata and is intended for returning metadata to the user from the
* database, as it is presumed that metadata stored in the database has
/** User (including, potentially administrative users) provided metadata container.
* This class performs no checks on the metadata and is intended for returning metadata to the
* user from the database, as it is presumed that metadata stored in the database has
* been checked already.
*
* Use WorkspaceUserMetadata for incoming metadata that needs checking.
Expand Down
59 changes: 50 additions & 9 deletions src/us/kbase/workspace/test/workspace/ObjectInformationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@ public void buildMinimalWithStandardInputs() {
assertThat("incorrect obj checksum", oi.getCheckSum(),
is("9a5b862b3f6969ec491ddeea83590e04"));
assertThat("incorrect obj size", oi.getSize(), is(5L));
assertThat("incorrect obj meta", oi.getUserMetaData(), is(Optional.empty()));
assertThat("incorrect obj meta", oi.getUserMetaDataMapOrNull(), is(nullValue()));
assertThat("incorrect user meta", oi.getUserMetaData(), is(Optional.empty()));
assertThat("incorrect user meta", oi.getUserMetaDataMapOrNull(), is(nullValue()));
assertThat("incorrect admin meta", oi.getAdminUserMetaData(), is(Optional.empty()));
assertThat("incorrect admin meta", oi.getAdminUserMetaDataMapOrNull(), is(nullValue()));
assertThat("incorrect ref path", oi.getReferencePath(),
is(Arrays.asList(new Reference(4, 1, 3))));
}
Expand All @@ -90,38 +92,56 @@ private ObjectInformation.Builder buildFullWithAlternativeInputsSetup() {
public void buildFullWithAlternativeInputsAndEmptyMetadata() {
final ObjectInformation oi = buildFullWithAlternativeInputsSetup()
.withUserMetadata(new UncheckedUserMetadata(new HashMap<>()))
.withAdminUserMetadata(new UncheckedUserMetadata(new HashMap<>()))
.build();
buildFullWithAlternativeInputsSharedChecks(oi);
assertThat("incorrect obj meta", oi.getUserMetaData(),
assertThat("incorrect user meta", oi.getUserMetaData(),
is(Optional.of(new UncheckedUserMetadata(new HashMap<>()))));
assertThat("incorrect obj meta", oi.getUserMetaDataMapOrNull(), is(new HashMap<>()));
assertThat("incorrect user meta", oi.getUserMetaDataMapOrNull(), is(new HashMap<>()));
assertThat("incorrect admin meta", oi.getAdminUserMetaData(),
is(Optional.of(new UncheckedUserMetadata(new HashMap<>()))));
assertThat("incorrect admin meta", oi.getAdminUserMetaDataMapOrNull(),
is(new HashMap<>()));
}

@Test
public void buildFullWithAlternativeInputsAndPopulatedMetadata() {
final ObjectInformation oi = buildFullWithAlternativeInputsSetup()
.withUserMetadata(new UncheckedUserMetadata(ImmutableMap.of(
"foo", "bar", "baz", "bat")))
.withAdminUserMetadata(new UncheckedUserMetadata(ImmutableMap.of(
"role", "admin", "why", "ImBetterThanYou")))
.build();
buildFullWithAlternativeInputsSharedChecks(oi);
assertThat("incorrect obj meta", oi.getUserMetaData(),
assertThat("incorrect user meta", oi.getUserMetaData(),
is(Optional.of(new UncheckedUserMetadata(ImmutableMap.of(
"foo", "bar", "baz", "bat")))));
assertThat("incorrect obj meta", oi.getUserMetaDataMapOrNull(), is(ImmutableMap.of(
assertThat("incorrect user meta", oi.getUserMetaDataMapOrNull(), is(ImmutableMap.of(
"foo", "bar", "baz", "bat")));
assertThat("incorrect admin meta", oi.getAdminUserMetaData(),
is(Optional.of(new UncheckedUserMetadata(ImmutableMap.of(
"role", "admin", "why", "ImBetterThanYou")))));
assertThat("incorrect admin meta", oi.getAdminUserMetaDataMapOrNull(), is(ImmutableMap.of(
"role", "admin", "why", "ImBetterThanYou")));
}

@Test
public void buildFullOverwriteMetadata() throws Exception {
final ObjectInformation oi = buildFullWithAlternativeInputsSetup()
.withUserMetadata(new UncheckedUserMetadata(ImmutableMap.of("a", "b")))
.withUserMetadata(new UncheckedUserMetadata(ImmutableMap.of("c", "d")))
.withAdminUserMetadata(new UncheckedUserMetadata(ImmutableMap.of("e", "f")))
.withAdminUserMetadata(new UncheckedUserMetadata(ImmutableMap.of("g", "h")))
.build();
buildFullWithAlternativeInputsSharedChecks(oi);
assertThat("incorrect obj meta", oi.getUserMetaData(),
assertThat("incorrect user meta", oi.getUserMetaData(),
is(Optional.of(new UncheckedUserMetadata(ImmutableMap.of("c", "d")))));
assertThat("incorrect obj meta", oi.getUserMetaDataMapOrNull(),
assertThat("incorrect user meta", oi.getUserMetaDataMapOrNull(),
is(ImmutableMap.of("c", "d")));
assertThat("incorrect admin meta", oi.getAdminUserMetaData(),
is(Optional.of(new UncheckedUserMetadata(ImmutableMap.of("g", "h")))));
assertThat("incorrect admin meta", oi.getAdminUserMetaDataMapOrNull(),
is(ImmutableMap.of("g", "h")));
}

private void buildFullWithAlternativeInputsSharedChecks(final ObjectInformation oi) {
Expand Down Expand Up @@ -395,7 +415,8 @@ public void refPath() {
.withWorkspace(new ResolvedWorkspaceID(4, "whee", false, false))
.withChecksum(new MD5("9a5b862b3f6969ec491ddeea83590e04"))
.withSize(5)
.withUserMetadata(new UncheckedUserMetadata(new HashMap<>()))
.withUserMetadata(new UncheckedUserMetadata(ImmutableMap.of("x", "y")))
.withAdminUserMetadata(new UncheckedUserMetadata(ImmutableMap.of("w", "z")))
.build();
final ObjectInformation oi2 = oi.updateReferencePath(
Arrays.asList(new Reference(7, 7, 7), new Reference(4, 1, 3)));
Expand All @@ -404,6 +425,26 @@ public void refPath() {
assertThat("incorrect new reference path", oi2.getReferencePath(),
is(Arrays.asList(new Reference(7, 7, 7), new Reference(4, 1, 3))));

// check that only the ref path changed
assertThat("incorrect obj id", oi2.getObjectId(), is(1L));
assertThat("incorrect obj name", oi2.getObjectName(), is("foo"));
assertThat("incorrect obj type", oi2.getTypeString(), is("type.t-3.7"));
assertThat("incorrect obj date", oi2.getSavedDate(), is(inst(10000)));
assertThat("incorrect obj ver", oi2.getVersion(), is(3));
assertThat("incorrect obj user", oi2.getSavedBy(), is(new WorkspaceUser("bar")));
assertThat("incorrect obj ws name", oi2.getWorkspaceName(), is("whee"));
assertThat("incorrect obj ws id", oi2.getWorkspaceId(), is(4L));
assertThat("incorrect obj checksum", oi2.getCheckSum(),
is("9a5b862b3f6969ec491ddeea83590e04"));
assertThat("incorrect obj size", oi2.getSize(), is(5L));
assertThat("incorrect user meta", oi2.getUserMetaData(),
is(Optional.of(new UncheckedUserMetadata(ImmutableMap.of("x", "y")))));
assertThat("incorrect user meta", oi2.getUserMetaDataMapOrNull(),
is(ImmutableMap.of("x", "y")));
assertThat("incorrect admin meta", oi2.getAdminUserMetaData(),
is(Optional.of(new UncheckedUserMetadata(ImmutableMap.of("w", "z")))));
assertThat("incorrect admin meta", oi2.getAdminUserMetaDataMapOrNull(),
is(ImmutableMap.of("w", "z")));
}

@Test
Expand Down

0 comments on commit 8a48fc9

Please sign in to comment.