From d9564e2e86ea110af933ca3dd0f728111d7140ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Socha?= <31014760+lukaszsocha2@users.noreply.github.com> Date: Fri, 6 Dec 2024 14:20:06 +0100 Subject: [PATCH] fix: Support creating ongoing Legal Hold policy with start date (#1281) Closes: SDK-4293 --- src/intTest/java/com/box/sdk/BoxAIIT.java | 59 +++++++++---------- .../java/com/box/sdk/BoxLegalHoldPolicy.java | 23 +++++++- ...oingWithStartDateLegalHoldPolicies201.json | 25 ++++++++ src/test/java/com/box/sdk/BoxAITest.java | 9 +-- .../com/box/sdk/BoxLegalHoldPolicyTest.java | 41 +++++++++++++ 5 files changed, 120 insertions(+), 37 deletions(-) create mode 100644 src/test/Fixtures/BoxLegalHold/PostOngoingWithStartDateLegalHoldPolicies201.json diff --git a/src/intTest/java/com/box/sdk/BoxAIIT.java b/src/intTest/java/com/box/sdk/BoxAIIT.java index b292b0306..8e56e0afb 100644 --- a/src/intTest/java/com/box/sdk/BoxAIIT.java +++ b/src/intTest/java/com/box/sdk/BoxAIIT.java @@ -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; @@ -227,7 +228,7 @@ 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 @@ -235,31 +236,28 @@ public void aiExtractStructuredWithFields() throws InterruptedException { retry(() -> { BoxAIExtractStructuredResponse response = BoxAI.extractMetadataStructured(api, Collections.singletonList(new BoxAIItem(uploadedFile.getID(), BoxAIItem.Type.FILE)), - new ArrayList() {{ - 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() {{ - 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"))); @@ -267,7 +265,6 @@ public void aiExtractStructuredWithFields() throws InterruptedException { 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); @@ -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() {{ - 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 @@ -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); diff --git a/src/main/java/com/box/sdk/BoxLegalHoldPolicy.java b/src/main/java/com/box/sdk/BoxLegalHoldPolicy.java index caf777584..7cdf5b012 100644 --- a/src/main/java/com/box/sdk/BoxLegalHoldPolicy.java +++ b/src/main/java/com/box/sdk/BoxLegalHoldPolicy.java @@ -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(); diff --git a/src/test/Fixtures/BoxLegalHold/PostOngoingWithStartDateLegalHoldPolicies201.json b/src/test/Fixtures/BoxLegalHold/PostOngoingWithStartDateLegalHoldPolicies201.json new file mode 100644 index 000000000..5b26e7ade --- /dev/null +++ b/src/test/Fixtures/BoxLegalHold/PostOngoingWithStartDateLegalHoldPolicies201.json @@ -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": "testuser@example.com" + }, + "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 +} diff --git a/src/test/java/com/box/sdk/BoxAITest.java b/src/test/java/com/box/sdk/BoxAITest.java index a54fbcb5e..795141ec4 100644 --- a/src/test/java/com/box/sdk/BoxAITest.java +++ b/src/test/java/com/box/sdk/BoxAITest.java @@ -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; @@ -364,10 +365,10 @@ public void testExtractMetadataStructuredWithFieldSuccess() { "The name of the file", "Name", "name", - new ArrayList() {{ - 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() diff --git a/src/test/java/com/box/sdk/BoxLegalHoldPolicyTest.java b/src/test/java/com/box/sdk/BoxLegalHoldPolicyTest.java index af2aac6cd..c75e2653b 100644 --- a/src/test/java/com/box/sdk/BoxLegalHoldPolicyTest.java +++ b/src/test/java/com/box/sdk/BoxLegalHoldPolicyTest.java @@ -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 = "testuser@example.com"; + 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() {