Skip to content

Commit

Permalink
fix: Support creating ongoing Legal Hold policy with start date (#1281)
Browse files Browse the repository at this point in the history
Closes: SDK-4293
  • Loading branch information
lukaszsocha2 authored Dec 6, 2024
1 parent 8cf8696 commit d9564e2
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 37 deletions.
59 changes: 28 additions & 31 deletions src/intTest/java/com/box/sdk/BoxAIIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.eclipsesource.json.JsonObject;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -227,47 +228,43 @@ public void aiExtractStructuredWithFields() throws InterruptedException {
BoxAIAgentExtractStructured agentExtractStructured = (BoxAIAgentExtractStructured) agent;

BoxFile uploadedFile = uploadFileToUniqueFolder(api, "[aiExtractStructuredWithFields] Test File.txt",
"My name is John Doe. I was born in 4th July 1990. I am 34 years old. My hobby is guitar and books.");
"My name is John Doe. I was born in 4th July 1990. I am 34 years old. My hobby is guitar.");

try {
// When a file has been just uploaded, AI service may not be ready to return text response
// and 412 is returned
retry(() -> {
BoxAIExtractStructuredResponse response = BoxAI.extractMetadataStructured(api,
Collections.singletonList(new BoxAIItem(uploadedFile.getID(), BoxAIItem.Type.FILE)),
new ArrayList<BoxAIExtractField>() {{
add(new BoxAIExtractField("firstName"));
add(new BoxAIExtractField("lastName"));
add(new BoxAIExtractField("date",
Arrays.asList(
new BoxAIExtractField("firstName"),
new BoxAIExtractField("lastName"),
new BoxAIExtractField("date",
"Person date of birth",
"Birth date",
"dateOfBirth",
null,
"What is the date of your birth?"));
add(new BoxAIExtractField("float",
"What is the date of your birth?"),
new BoxAIExtractField("float",
"Person age",
"Age",
"age",
null,
"How old are you?"));
add(new BoxAIExtractField("multiSelect",
"How old are you?"),
new BoxAIExtractField("multiSelect",
"Person hobby",
"Hobby",
"hobby",
new ArrayList<BoxAIExtractFieldOption>() {{
add(new BoxAIExtractFieldOption("guitar"));
add(new BoxAIExtractFieldOption("books"));
}},
"What is your hobby?"));
}},
Collections.singletonList(new BoxAIExtractFieldOption("guitar")),
"What is your hobby?")
),
agentExtractStructured);
JsonObject sourceJson = response.getSourceJson();
assertThat(sourceJson.get("firstName").asString(), is(equalTo("John")));
assertThat(sourceJson.get("lastName").asString(), is(equalTo("Doe")));
assertThat(sourceJson.get("dateOfBirth").asString(), is(equalTo("1990-07-04")));
assertThat(sourceJson.get("age").asInt(), is(equalTo(34)));
assertThat(sourceJson.get("hobby").asArray().get(0).asString(), is(equalTo("guitar")));
assertThat(sourceJson.get("hobby").asArray().get(1).asString(), is(equalTo("books")));
}, 2, 2000);
} finally {
deleteFile(uploadedFile);
Expand All @@ -281,30 +278,31 @@ public void aiExtractStructuredWithMetadataTemplate() throws InterruptedExceptio
BoxAIAgentExtractStructured agentExtractStructured = (BoxAIAgentExtractStructured) agent;

BoxFile uploadedFile = uploadFileToUniqueFolder(api, "[aiExtractStructuredWithMetadataTemplate] Test File.txt",
"My name is John Doe. I was born in 4th July 1990. I am 34 years old. My hobby is guitar and books.");
String templateKey = "key" + java.util.UUID.randomUUID().toString();
"My name is John Doe. I was born in 4th July 1990. I am 34 years old. My hobby is guitar.");
String templateKey = "key" + java.util.UUID.randomUUID();
MetadataTemplate template = MetadataTemplate.createMetadataTemplate(api,
"enterprise",
templateKey,
templateKey,
false,
new ArrayList<MetadataTemplate.Field>() {{
add(new MetadataTemplate.Field(Json.parse(
Arrays.asList(
new MetadataTemplate.Field(Json.parse(
"{\"key\":\"firstName\",\"displayName\":\"First name\","
+ "\"description\":\"Person first name\",\"type\":\"string\"}").asObject()));
add(new MetadataTemplate.Field(Json.parse(
+ "\"description\":\"Person first name\",\"type\":\"string\"}").asObject()),
new MetadataTemplate.Field(Json.parse(
"{\"key\":\"lastName\",\"displayName\":\"Last name\","
+ "\"description\":\"Person last name\",\"type\":\"string\"}").asObject()));
add(new MetadataTemplate.Field(Json.parse(
+ "\"description\":\"Person last name\",\"type\":\"string\"}").asObject()),
new MetadataTemplate.Field(Json.parse(
"{\"key\":\"dateOfBirth\",\"displayName\":\"Birth date\","
+ "\"description\":\"Person date of birth\",\"type\":\"date\"}").asObject()));
add(new MetadataTemplate.Field(Json.parse(
+ "\"description\":\"Person date of birth\",\"type\":\"date\"}").asObject()),
new MetadataTemplate.Field(Json.parse(
"{\"key\":\"age\",\"displayName\":\"Age\","
+ "\"description\":\"Person age\",\"type\":\"float\"}").asObject()));
add(new MetadataTemplate.Field(Json.parse(
+ "\"description\":\"Person age\",\"type\":\"float\"}").asObject()),
new MetadataTemplate.Field(Json.parse(
"{\"key\":\"hobby\",\"displayName\":\"Hobby\","
+ "\"description\":\"Person hobby\",\"type\":\"multiSelect\"}").asObject()));
}});
+ "\"description\":\"Person hobby\",\"type\":\"multiSelect\","
+ " \"options\":[{\"key\": \"guitar\"}, {\"key\": \"books\"}]}").asObject())
));

try {
// When a file has been just uploaded, AI service may not be ready to return text response
Expand All @@ -320,7 +318,6 @@ public void aiExtractStructuredWithMetadataTemplate() throws InterruptedExceptio
assertThat(sourceJson.get("dateOfBirth").asString(), is(equalTo("1990-07-04T00:00:00Z")));
assertThat(sourceJson.get("age").asInt(), is(equalTo(34)));
assertThat(sourceJson.get("hobby").asArray().get(0).asString(), is(equalTo("guitar")));
assertThat(sourceJson.get("hobby").asArray().get(1).asString(), is(equalTo("books")));
}, 2, 2000);
} finally {
deleteFile(uploadedFile);
Expand Down
23 changes: 21 additions & 2 deletions src/main/java/com/box/sdk/BoxLegalHoldPolicy.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,33 @@ public static BoxLegalHoldPolicy.Info create(BoxAPIConnection api, String name,
* @return information about the Legal Hold Policy created.
*/
public static BoxLegalHoldPolicy.Info createOngoing(BoxAPIConnection api, String name, String description) {
return createOngoing(api, name, description, null);
}


/**
* Creates a new ongoing Legal Hold Policy.
*
* @param api the API connection to be used by the resource.
* @param name the name of Legal Hold Policy.
* @param description the description of Legal Hold Policy.
* @param filterStartedAt optional date filter applies to Custodian assignments only.
* @return information about the Legal Hold Policy created.
*/
public static BoxLegalHoldPolicy.Info createOngoing(
BoxAPIConnection api, String name, String description, Date filterStartedAt
) {
URL url = ALL_LEGAL_HOLD_URL_TEMPLATE.build(api.getBaseURL());
BoxJSONRequest request = new BoxJSONRequest(api, url, "POST");
JsonObject requestJSON = new JsonObject()
.add("policy_name", name)
.add("is_ongoing", true);
.add("policy_name", name)
.add("is_ongoing", true);
if (description != null) {
requestJSON.add("description", description);
}
if (filterStartedAt != null) {
requestJSON.add("filter_started_at", BoxDateFormat.format(filterStartedAt));
}
request.setBody(requestJSON.toString());
try (BoxJSONResponse response = request.send()) {
JsonObject responseJSON = Json.parse(response.getJSON()).asObject();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"type": "legal_hold_policy",
"id": "11111",
"policy_name": "Trial Documents",
"description": "This is a description.",
"status": "active",
"assignment_counts": {
"user": 0,
"folder": 0,
"file": 0,
"file_version": 0
},
"is_ongoing": true,
"created_by": {
"type": "user",
"id": "33333",
"name": "Test User",
"login": "[email protected]"
},
"created_at": "2018-04-25T16:37:05-07:00",
"modified_at": "2018-04-25T16:37:05-07:00",
"deleted_at": null,
"filter_started_at": "2018-04-25T16:37:05-07:00",
"filter_ended_at": null
}
9 changes: 5 additions & 4 deletions src/test/java/com/box/sdk/BoxAITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -364,10 +365,10 @@ public void testExtractMetadataStructuredWithFieldSuccess() {
"The name of the file",
"Name",
"name",
new ArrayList<BoxAIExtractFieldOption>() {{
add(new BoxAIExtractFieldOption("option 1"));
add(new BoxAIExtractFieldOption("option 2"));
}},
Arrays.asList(
new BoxAIExtractFieldOption("option 1"),
new BoxAIExtractFieldOption("option 2")
),
"What is the name of the file?");

final JsonObject expectedRequestBody = new JsonObject()
Expand Down
41 changes: 41 additions & 0 deletions src/test/java/com/box/sdk/BoxLegalHoldPolicyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,47 @@ public void testCreateOngoingNewLegalHoldPolicySucceedsAndSendsCorrectJson() {
Assert.assertTrue(policyInfo.getIsOngoing());
}

@Test
public void testCreateOngoingWithStartDateNewLegalHoldPolicySucceedsAndSendsCorrectJson() throws ParseException {
final String legalHoldsURL = "/2.0/legal_hold_policies";
final String policyID = "11111";
final String createdByID = "33333";
final String createdByName = "Test User";
final String createdByLogin = "[email protected]";
final String policyName = "Trial Documents";
final String description = "This is a description.";
final String startTimeString = "2018-04-25T23:37:05Z";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
final Date startTime = dateFormat.parse("2018-04-25T16:37:05-07:00");

JsonObject policyObject = new JsonObject()
.add("policy_name", policyName)
.add("is_ongoing", true)
.add("description", description)
.add("filter_started_at", startTimeString);

String result = TestUtils.getFixture("BoxLegalHold/PostOngoingWithStartDateLegalHoldPolicies201");

wireMockRule.stubFor(WireMock.post(WireMock.urlPathEqualTo(legalHoldsURL))
.withRequestBody(WireMock.equalToJson(policyObject.toString()))
.willReturn(WireMock.aResponse()
.withHeader("Content-Type", APPLICATION_JSON)
.withBody(result)));

BoxLegalHoldPolicy.Info policyInfo = BoxLegalHoldPolicy.createOngoing(
this.api, policyName, description, startTime
);

Assert.assertEquals(policyID, policyInfo.getID());
Assert.assertEquals(createdByID, policyInfo.getCreatedBy().getID());
Assert.assertEquals(createdByName, policyInfo.getCreatedBy().getName());
Assert.assertEquals(createdByLogin, policyInfo.getCreatedBy().getLogin());
Assert.assertEquals(policyName, policyInfo.getPolicyName());
Assert.assertEquals(description, policyInfo.getDescription());
Assert.assertEquals(startTime, policyInfo.getFilterStartedAt());
Assert.assertTrue(policyInfo.getIsOngoing());
}


@Test
public void testUpdateLegalHoldPolicySucceedsAndSendsCorrectJson() {
Expand Down

0 comments on commit d9564e2

Please sign in to comment.