Skip to content

Commit

Permalink
fix: Add missing fields when update classification template (#1255)
Browse files Browse the repository at this point in the history
  • Loading branch information
congminh1254 authored Jun 21, 2024
1 parent 236a036 commit f17f817
Show file tree
Hide file tree
Showing 6 changed files with 317 additions and 12 deletions.
95 changes: 95 additions & 0 deletions src/intTest/java/com/box/sdk/BoxClassificationTemplateIT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.box.sdk;

import static com.box.sdk.BoxApiProvider.jwtApiForServiceAccount;
import static org.junit.Assert.assertEquals;

import java.util.ArrayList;
import java.util.List;
import org.junit.Test;

public class BoxClassificationTemplateIT {

public MetadataTemplate getOrCreateClassificationTemplate(BoxAPIConnection api) {
try {
return MetadataTemplate.getMetadataTemplate(api, Metadata.CLASSIFICATION_TEMPLATE_KEY);
} catch (BoxAPIException e) {
assertEquals(e.getResponseCode(), 404);
}

MetadataTemplate.Field classification = new MetadataTemplate.Field();
classification.setType("enum");
classification.setKey("Box__Security__Classification__Key");
classification.setDisplayName("Classification");
classification.setIsHidden(false);

List<String> options = new ArrayList<String>();
options.add("sensitive");
classification.setOptions(options);

List<MetadataTemplate.Field> fields = new ArrayList<MetadataTemplate.Field>();
fields.add(classification);

return MetadataTemplate.createMetadataTemplate(api, Metadata.ENTERPRISE_METADATA_SCOPE,
Metadata.CLASSIFICATION_TEMPLATE_KEY, "Classification", false, fields);
}

@Test
public void getUpdateClassificationTemplate() {
BoxAPIConnection api = jwtApiForServiceAccount();
MetadataTemplate template = getOrCreateClassificationTemplate(api);
String fieldKey = template.getFields().get(0).getKey();
String optionKey = template.getFields().get(0).getOptionsObjects().get(0).getKey();

List<MetadataTemplate.FieldOperation> updates = new ArrayList<MetadataTemplate.FieldOperation>();
String update = "{\n"
+ " \"op\": \"editEnumOption\",\n"
+ " \"fieldKey\": \"" + fieldKey + "\",\n"
+ " \"enumOptionKey\": \"" + optionKey + "\",\n"
+ " \"data\": {\n"
+ " \"key\": \"" + optionKey + "\",\n"
+ " \"staticConfig\": {\n"
+ " \"classification\": {\n"
+ " \"classificationDefinition\": \"Sensitive information.\",\n"
+ " \"colorID\": 4\n"
+ " }\n"
+ " }\n"
+ " }\n"
+ "}";
updates.add(new MetadataTemplate.FieldOperation(update));

MetadataTemplate updatedTemplate = MetadataTemplate.updateMetadataTemplate(api,
Metadata.ENTERPRISE_METADATA_SCOPE, Metadata.CLASSIFICATION_TEMPLATE_KEY, updates);
MetadataTemplate.Option updatedOption = updatedTemplate.getFields().get(0).getOptionsObjects().get(0);
assertEquals(updatedOption.getKey(), "Sensitive");
assertEquals(
updatedOption.getStaticConfig().getClassification().getString("classificationDefinition",
"null"),
"Sensitive information.");
assertEquals(updatedOption.getStaticConfig().getClassification().getInt("colorID", 0),
4);
update = "{\n"
+ " \"op\": \"editEnumOption\",\n"
+ " \"fieldKey\": \"" + fieldKey + "\",\n"
+ " \"enumOptionKey\": \"" + optionKey + "\",\n"
+ " \"data\": {\n"
+ " \"key\": \"" + optionKey + "\",\n"
+ " \"staticConfig\": {\n"
+ " \"classification\": {\n"
+ " \"classificationDefinition\": \"Top Sensitive information.\",\n"
+ " \"colorID\": 5\n"
+ " }\n"
+ " }\n"
+ " }\n"
+ "}";
updates.clear();
updates.add(new MetadataTemplate.FieldOperation(update));

updatedTemplate = MetadataTemplate.updateMetadataTemplate(api, Metadata.ENTERPRISE_METADATA_SCOPE,
Metadata.CLASSIFICATION_TEMPLATE_KEY, updates);
updatedOption = updatedTemplate.getFields().get(0).getOptionsObjects().get(0);
assertEquals(updatedOption.getKey(), "Sensitive");
assertEquals(updatedOption.getStaticConfig().getClassification().getString("classificationDefinition",
"null"), "Top Sensitive information.");
assertEquals(updatedOption.getStaticConfig().getClassification().getInt("colorID", 0), 5);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ public void attachPolicyToFileAndGetFilesUnderRetentionAndDeleteAttachment() thr
assignment.delete();
Retry.retry(() -> {
Iterable<BoxFile.Info> filesUnderRetention2 =
new BoxRetentionPolicyAssignment(api, assignmentInfo.getID()).getFilesUnderRetention(5);
new BoxRetentionPolicyAssignment(api, assignmentInfo.getID()).getFilesUnderRetention(50);

//then
List<BoxFile.Info> matchingFileWithRetention2 =
StreamSupport.stream(filesUnderRetention2.spliterator(), false)
.filter(f -> f.getID().equals(boxFile.getID()))
.collect(Collectors.toList());
assertTrue(matchingFileWithRetention2.isEmpty());
}, 5, 2000);
}, 10, 3000);
} finally {
//cleanup
deleteFolder(folder.getResource());
Expand Down Expand Up @@ -115,15 +115,15 @@ public void attachPolicyToFileAndGetFileVersionsUnderRetentionAndDeleteAttachmen
//when
assignment.delete();
Retry.retry(() -> {
Iterable<BoxFile.Info> filesVersionsUnderRetention2 = assignment.getFileVersionsUnderRetention(5);
Iterable<BoxFile.Info> filesVersionsUnderRetention2 = assignment.getFileVersionsUnderRetention(50);

//then
List<BoxFile.Info> matchingFileWithRetention2 =
StreamSupport.stream(filesVersionsUnderRetention2.spliterator(), false)
.filter(f -> f.getID().equals(boxFile.getID()))
.collect(Collectors.toList());
assertTrue(matchingFileWithRetention2.isEmpty());
}, 5, 2000);
}, 10, 3000);
} finally {
//cleanup
deleteFolder(folder);
Expand Down
18 changes: 14 additions & 4 deletions src/intTest/java/com/box/sdk/BoxSignRequestIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,13 @@ public void createListAndCancelSignRequest() throws InterruptedException {

// Test Create
assertNotNull(signRequestInfoCreate.getPrepareUrl());
assertEquals(file.getID(), fileInfoCreate.getID());
assertEquals(file2.getID(), file2InfoCreate.getID());
// The order of the files in the response is not guaranteed
if (file.getID().equals(fileInfoCreate.getID())) {
assertEquals(file2.getID(), file2InfoCreate.getID());
} else {
assertEquals(file.getID(), file2InfoCreate.getID());
assertEquals(file2.getID(), fileInfoCreate.getID());
}
assertEquals(signerEmail, signerCreate.getEmail());
assertNotNull(signRequestInfoCreate.getID());

Expand All @@ -118,8 +123,13 @@ public void createListAndCancelSignRequest() throws InterruptedException {
BoxSignRequestSigner signer = signRequestInfoGetByID.getSigners().get(1);

// Test Get by ID
assertEquals(file.getID(), fileInfo.getID());
assertEquals(file2.getID(), file2Info.getID());
// The order of the files in the response is not guaranteed
if (file.getID().equals(fileInfo.getID())) {
assertEquals(file2.getID(), file2Info.getID());
} else {
assertEquals(file.getID(), file2Info.getID());
assertEquals(file2.getID(), fileInfo.getID());
}
assertEquals(signerEmail, signer.getEmail());
assertEquals(signRequestIdCreate, signRequestInfoGetByID.getID());
assertEquals(signerLoginRequired, signer.getLoginRequired());
Expand Down
130 changes: 126 additions & 4 deletions src/main/java/com/box/sdk/MetadataTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,16 @@ private static JsonObject getFieldOperationJsonObject(FieldOperation fieldOperat
fieldObj.add("copyInstanceOnItemCopy", copyInstanceOnItemCopy);
}

StaticConfig staticConfig = field.getStaticConfig();
if (staticConfig != null) {
JsonObject staticConfigObj = new JsonObject();
JsonObject classification = staticConfig.getClassification();
if (classification != null) {
staticConfigObj.add("classification", classification);
}
fieldObj.add("staticConfig", staticConfigObj);
}

jsonObject.add("data", fieldObj);
}

Expand Down Expand Up @@ -733,6 +743,71 @@ public enum Operation {
reorderMultiSelectOptions
}

/**
* Class contains information about the static configuration for the classification.
*/
public static class StaticConfig extends BoxJSONObject {
private JsonObject classification;

/**
* Constructs an empty static configuration.
*/
public StaticConfig() {
super();
}

/**
* Constructs a static configuration from a JSON string.
*
* @param json the json encoded metadate template field.
*/
public StaticConfig(String json) {
super(json);
}

/** Constructs a static configuration from a JSON object.
*
* @param jsonObject the json encoded metadate template field.
*/
StaticConfig(JsonObject jsonObject) {
super(jsonObject);
}

/**
* Gets the classification of the static configuration.
*
* @return the classification of the static configuration.
*/
public JsonObject getClassification() {
return this.classification;
}

/**
* Sets the classification of the static configuration.
*
* @param classification the classification of the static configuration.
*/
public void setClassification(JsonObject classification) {
this.classification = classification;
}

/**
* {@inheritDoc}
*/
@Override
void parseJSONMember(JsonObject.Member member) {
JsonValue value = member.getValue();
String memberName = member.getName();
switch (memberName) {
case "classification":
this.classification = value.asObject();
break;
default:
break;
}
}
}

/**
* Class contains information about the metadata template field.
*/
Expand Down Expand Up @@ -778,6 +853,11 @@ public static class Field extends BoxJSONObject {
*/
private Boolean copyInstanceOnItemCopy;

/**
* @see #getStaticConfig()
*/
private StaticConfig staticConfig;

/**
* Constructs an empty metadata template.
*/
Expand Down Expand Up @@ -965,6 +1045,24 @@ public void setCopyInstanceOnItemCopy(Boolean copyInstanceOnItemCopy) {
this.copyInstanceOnItemCopy = copyInstanceOnItemCopy;
}

/**
* Gets static configuration for the classification.
*
* @return static configuration for the classification.
*/
public StaticConfig getStaticConfig() {
return this.staticConfig;
}

/**
* Sets static configuration for the classification.
*
* @param staticConfig static configuration for the classification.
*/
public void setStaticConfig(StaticConfig staticConfig) {
this.staticConfig = staticConfig;
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -1000,6 +1098,9 @@ void parseJSONMember(JsonObject.Member member) {
case "copyInstanceOnItemCopy":
this.copyInstanceOnItemCopy = value.asBoolean();
break;
case "staticConfig":
this.staticConfig = new StaticConfig(value.asObject());
break;
default:
break;
}
Expand All @@ -1018,6 +1119,10 @@ public static class Option extends BoxJSONObject {
* @see #getKey()
*/
private String key;
/**
* @see #getStaticConfig()
*/
private StaticConfig staticConfig;

/**
* Constructs an empty metadata template.
Expand Down Expand Up @@ -1062,17 +1167,34 @@ public String getKey() {
return this.key;
}

/**
* Gets static configuration for the classification.
*
* @return static configuration for the classification.
*/
public StaticConfig getStaticConfig() {
return this.staticConfig;
}

/**
* {@inheritDoc}
*/
@Override
void parseJSONMember(JsonObject.Member member) {
JsonValue value = member.getValue();
String memberName = member.getName();
if (memberName.equals("id")) {
this.id = value.asString();
} else if (memberName.equals("key")) {
this.key = value.asString();
switch (memberName) {
case "id":
this.id = value.asString();
break;
case "key":
this.key = value.asString();
break;
case "staticConfig":
this.staticConfig = new StaticConfig(value.asObject());
break;
default:
break;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

{
"id": "f7a9891f",
"templateKey": "documentFlow03",
"scope": "enterprise",
"displayName": "Document Flow 03",
"hidden": false,
"fields": [
{
"type": "enum",
"key": "Box__Security__Classification__Key",
"displayName": "Classification",
"hidden": false,
"options": [
{
"key": "Classified",
"staticConfig": {
"classification": {
"colorID": 4,
"classificationDefinition": "Top Seret"
}
}
}
]
}
],
"copyInstanceOnItemCopy": true
}
Loading

0 comments on commit f17f817

Please sign in to comment.