Search in sources :

Example 86 with JsonArray

use of com.eclipsesource.json.JsonArray in project box-java-sdk by box.

the class BoxFile method setCollections.

/**
 * {@inheritDoc}
 */
@Override
public BoxFile.Info setCollections(BoxCollection... collections) {
    JsonArray jsonArray = new JsonArray();
    for (BoxCollection collection : collections) {
        JsonObject collectionJSON = new JsonObject();
        collectionJSON.add("id", collection.getID());
        jsonArray.add(collectionJSON);
    }
    JsonObject infoJSON = new JsonObject();
    infoJSON.add("collections", jsonArray);
    String queryString = new QueryStringBuilder().appendParam("fields", ALL_FIELDS).toString();
    URL url = FILE_URL_TEMPLATE.buildWithQuery(this.getAPI().getBaseURL(), queryString, this.getID());
    BoxJSONRequest request = new BoxJSONRequest(this.getAPI(), url, "PUT");
    request.setBody(infoJSON.toString());
    BoxJSONResponse response = (BoxJSONResponse) request.send();
    JsonObject jsonObject = Json.parse(response.getJSON()).asObject();
    return new Info(jsonObject);
}
Also used : JsonArray(com.eclipsesource.json.JsonArray) JsonObject(com.eclipsesource.json.JsonObject) URL(java.net.URL)

Example 87 with JsonArray

use of com.eclipsesource.json.JsonArray in project box-java-sdk by box.

the class BoxFolderTest method testSetMetadataReturnsCorrectly.

@Test
public void testSetMetadataReturnsCorrectly() throws IOException {
    final String folderID = "12345";
    final String metadataURL = "/folders/" + folderID + "/metadata/enterprise/testtemplate";
    ArrayList<String> secondValueArray = new ArrayList<>();
    secondValueArray.add("first");
    secondValueArray.add("second");
    secondValueArray.add("third");
    String postResult = TestConfig.getFixture("/BoxException/BoxResponseException409");
    String putResult = TestConfig.getFixture("/BoxFolder/UpdateMetadataOnFolder200");
    final String firstValue = "text";
    JsonArray secondValueJson = new JsonArray().add("first").add("second").add("third");
    final int thirdValue = 2;
    final float fourthValue = 1234567890f;
    final double fifthValue = 233333333333333340.0;
    JsonObject firstAttribute = new JsonObject().add("op", "add").add("path", "/test1").add("value", "text");
    JsonObject secondAttribute = new JsonObject().add("op", "add").add("path", "/test2").add("value", secondValueJson);
    JsonObject thirdAttribute = new JsonObject().add("op", "add").add("path", "/test3").add("value", thirdValue);
    JsonObject fourthAttribute = new JsonObject().add("op", "add").add("path", "/test4").add("value", fourthValue);
    JsonObject fifthAttribute = new JsonObject().add("op", "add").add("path", "/test5").add("value", fifthValue);
    JsonArray jsonArray = new JsonArray().add(firstAttribute).add(secondAttribute).add(thirdAttribute).add(fourthAttribute).add(fifthAttribute);
    wireMockRule.stubFor(WireMock.post(WireMock.urlPathEqualTo(metadataURL)).willReturn(WireMock.aResponse().withHeader("Content-Type", "application/json").withBody(postResult).withStatus(409)));
    wireMockRule.stubFor(WireMock.put(WireMock.urlPathEqualTo(metadataURL)).withRequestBody(WireMock.equalToJson(jsonArray.toString())).withHeader("Content-Type", WireMock.equalTo("application/json-patch+json")).willReturn(WireMock.aResponse().withHeader("Content-Type", "application/json-patch+json").withBody(putResult).withStatus(200)));
    BoxFolder folder = new BoxFolder(this.api, "12345");
    Metadata metadata = new Metadata().add("/test1", firstValue).add("/test2", secondValueArray).add("/test3", thirdValue).add("/test4", fourthValue).add("/test5", fifthValue);
    Metadata metadataValues = folder.setMetadata("testtemplate", "enterprise", metadata);
    assertEquals("folder_12345", metadataValues.getParentID());
    assertEquals("testtemplate", metadataValues.getTemplateName());
    assertEquals("enterprise_11111", metadataValues.getScope());
    assertEquals(firstValue, metadataValues.getString("/test1"));
    assertEquals(secondValueJson, metadataValues.getValue("/test2"));
    assertEquals(thirdValue, metadataValues.getDouble("/test3"), 0);
    assertEquals(fourthValue, metadataValues.getDouble("/test4"), 4);
    assertEquals(fifthValue, metadataValues.getDouble("/test5"), 0);
}
Also used : JsonArray(com.eclipsesource.json.JsonArray) ArrayList(java.util.ArrayList) JsonObject(com.eclipsesource.json.JsonObject) Test(org.junit.Test)

Example 88 with JsonArray

use of com.eclipsesource.json.JsonArray in project box-java-sdk by box.

the class BoxFolderTest method testRetryingChunkedUploadWith500Error.

@Test
public void testRetryingChunkedUploadWith500Error() throws IOException, InterruptedException {
    String javaVersion = System.getProperty("java.version");
    Assume.assumeFalse("Test is not run for JDK 7", javaVersion.contains("1.7"));
    String responseBody500 = TestConfig.getFixture("BoxException/BoxResponseException500");
    final String preflightURL = "/files/content";
    final String sessionURL = "/files/upload_sessions";
    final String uploadURL = "/files/upload_sessions/D5E3F8ADA11A38F0A66AD0B64AACA658";
    final String listPartsURL = "/files/upload_sessions/D5E3F8ADA11A38F0A66AD0B64AACA658/parts";
    final String commitURL = "/files/upload_sessions/D5E3F8ADA11A38F0A66AD0B64AACA658/commit";
    BoxFileTest.FakeStream stream = new BoxFileTest.FakeStream("aaaaa");
    String sessionResult = TestConfig.getFixture("BoxFile/CreateUploadSession201", wireMockRule.port());
    String uploadResult = TestConfig.getFixture("BoxFile/UploadPartOne200");
    String wrongPartsResult = TestConfig.getFixture("BoxFile/ListUploadedParts200");
    String commitResult = TestConfig.getFixture("BoxFile/CommitUpload201");
    JsonObject idObject = new JsonObject().add("id", "12345");
    JsonObject preflightObject = new JsonObject().add("name", "testfile.txt").add("size", 5).add("parent", idObject);
    JsonObject sessionObject = new JsonObject().add("folder_id", "12345").add("file_size", 5).add("file_name", "testfile.txt");
    JsonObject partOne = new JsonObject().add("part_id", "CFEB5BA9").add("offset", 0).add("size", 5);
    JsonArray parts = new JsonArray().add(partOne);
    JsonObject commitObject = new JsonObject().add("parts", parts);
    wireMockRule.stubFor(WireMock.options(WireMock.urlPathEqualTo(preflightURL)).withRequestBody(WireMock.equalToJson(preflightObject.toString())).willReturn(WireMock.aResponse().withHeader("Content-Type", "application/json").withStatus(200)));
    wireMockRule.stubFor(WireMock.post(WireMock.urlPathEqualTo(sessionURL)).withRequestBody(WireMock.equalToJson(sessionObject.toString())).willReturn(WireMock.aResponse().withHeader("Content-Type", "application/json").withBody(sessionResult).withStatus(201)));
    wireMockRule.stubFor(WireMock.put(WireMock.urlPathEqualTo(uploadURL)).withHeader("Digest", WireMock.containing("sha=31HjfCaaqU04+T5Te/biAgshQGw=")).withHeader("Content-Type", WireMock.containing("application/octet-stream")).withHeader("Content-Range", WireMock.containing("bytes 0-4/5")).withRequestBody(WireMock.equalTo("aaaaa")).willReturn(WireMock.aResponse().withHeader("Content-Type", "application/json").withBody(responseBody500).withStatus(500)));
    wireMockRule.stubFor(WireMock.get(WireMock.urlPathEqualTo(listPartsURL)).willReturn(WireMock.aResponse().withHeader("Content-Type", "application/json").withBody(wrongPartsResult).withStatus(200)));
    wireMockRule.stubFor(WireMock.put(WireMock.urlPathEqualTo(uploadURL)).withHeader("Digest", WireMock.containing("sha=31HjfCaaqU04+T5Te/biAgshQGw=")).withHeader("Content-Type", WireMock.containing("application/octet-stream")).withHeader("Content-Range", WireMock.containing("bytes 0-4/5")).withRequestBody(WireMock.equalTo("aaaaa")).willReturn(WireMock.aResponse().withHeader("Content-Type", "application/json").withBody(uploadResult).withStatus(200)));
    wireMockRule.stubFor(WireMock.post(WireMock.urlPathEqualTo(commitURL)).withHeader("Content-Type", WireMock.equalTo("application/json")).withRequestBody(WireMock.containing(commitObject.toString())).willReturn(WireMock.aResponse().withHeader("Content-Type", "application/json").withBody(commitResult).withStatus(201)));
    BoxFolder folder = new BoxFolder(this.api, "12345");
    BoxFile.Info uploadedFile = folder.uploadLargeFile(stream, "testfile.txt", 5);
    assertEquals("1111111", uploadedFile.getID());
    assertEquals("testuser@box.com", uploadedFile.getModifiedBy().getLogin());
    assertEquals("Test User", uploadedFile.getOwnedBy().getName());
    assertEquals("folder", uploadedFile.getParent().getType());
    assertEquals("testfile.txt", uploadedFile.getName());
}
Also used : JsonArray(com.eclipsesource.json.JsonArray) JsonObject(com.eclipsesource.json.JsonObject) Test(org.junit.Test)

Example 89 with JsonArray

use of com.eclipsesource.json.JsonArray in project box-java-sdk by box.

the class BoxFolderTest method testUpdateClassification.

@Test
public void testUpdateClassification() throws IOException {
    final String folderID = "12345";
    final String classificationType = "Internal";
    final String metadataURL = "/folders/" + folderID + "/metadata/enterprise/securityClassification-6VMVochwUWo";
    JsonObject metadataObject = new JsonObject().add("op", "replace").add("path", "/Box__Security__Classification__Key").add("value", "Internal");
    JsonArray metadataArray = new JsonArray().add(metadataObject);
    String result = TestConfig.getFixture("BoxFolder/UpdateClassificationOnFolder200");
    wireMockRule.stubFor(WireMock.put(WireMock.urlPathEqualTo(metadataURL)).withRequestBody(WireMock.equalToJson(metadataArray.toString())).willReturn(WireMock.aResponse().withHeader("Content-Type", "application/json-patch+json").withBody(result)));
    BoxFolder folder = new BoxFolder(this.api, folderID);
    String classification = folder.updateClassification(classificationType);
    assertEquals(classificationType, classification);
}
Also used : JsonArray(com.eclipsesource.json.JsonArray) JsonObject(com.eclipsesource.json.JsonObject) Test(org.junit.Test)

Example 90 with JsonArray

use of com.eclipsesource.json.JsonArray in project box-java-sdk by box.

the class BoxFolderTest method testChunkedUploadWith500Error.

@Test
public void testChunkedUploadWith500Error() throws IOException, InterruptedException {
    String javaVersion = System.getProperty("java.version");
    Assume.assumeFalse("Test is not run for JDK 7", javaVersion.contains("1.7"));
    String responseBody500 = TestConfig.getFixture("BoxException/BoxResponseException500");
    final String preflightURL = "/files/content";
    final String sessionURL = "/files/upload_sessions";
    final String uploadURL = "/files/upload_sessions/D5E3F8ADA11A38F0A66AD0B64AACA658";
    final String listPartsURL = "/files/upload_sessions/D5E3F8ADA11A38F0A66AD0B64AACA658/parts";
    final String commitURL = "/files/upload_sessions/D5E3F8ADA11A38F0A66AD0B64AACA658/commit";
    BoxFileTest.FakeStream stream = new BoxFileTest.FakeStream("aaaaa");
    String sessionResult = TestConfig.getFixture("BoxFile/CreateUploadSession201", wireMockRule.port());
    String partsResult = TestConfig.getFixture("BoxFile/ListUploadedPart200");
    String commitResult = TestConfig.getFixture("BoxFile/CommitUpload201");
    JsonObject idObject = new JsonObject().add("id", "12345");
    JsonObject preflightObject = new JsonObject().add("name", "testfile.txt").add("size", 5).add("parent", idObject);
    JsonObject sessionObject = new JsonObject().add("folder_id", "12345").add("file_size", 5).add("file_name", "testfile.txt");
    JsonObject partOne = new JsonObject().add("part_id", "CFEB5BA9").add("offset", 0).add("size", 5);
    JsonArray parts = new JsonArray().add(partOne);
    JsonObject commitObject = new JsonObject().add("parts", parts);
    wireMockRule.stubFor(WireMock.options(WireMock.urlPathEqualTo(preflightURL)).withRequestBody(WireMock.equalToJson(preflightObject.toString())).willReturn(WireMock.aResponse().withHeader("Content-Type", "application/json").withStatus(200)));
    wireMockRule.stubFor(WireMock.post(WireMock.urlPathEqualTo(sessionURL)).withRequestBody(WireMock.equalToJson(sessionObject.toString())).willReturn(WireMock.aResponse().withHeader("Content-Type", "application/json").withBody(sessionResult).withStatus(201)));
    wireMockRule.stubFor(WireMock.put(WireMock.urlPathEqualTo(uploadURL)).withHeader("Digest", WireMock.containing("sha=31HjfCaaqU04+T5Te/biAgshQGw=")).withHeader("Content-Type", WireMock.containing("application/octet-stream")).withHeader("Content-Range", WireMock.containing("bytes 0-4/5")).withRequestBody(WireMock.equalTo("aaaaa")).willReturn(WireMock.aResponse().withHeader("Content-Type", "application/json").withBody(responseBody500).withStatus(500)));
    wireMockRule.stubFor(WireMock.get(WireMock.urlPathEqualTo(listPartsURL)).willReturn(WireMock.aResponse().withHeader("Content-Type", "application/json").withBody(partsResult).withStatus(200)));
    wireMockRule.stubFor(WireMock.post(WireMock.urlPathEqualTo(commitURL)).withHeader("Content-Type", WireMock.equalTo("application/json")).withRequestBody(WireMock.containing(commitObject.toString())).willReturn(WireMock.aResponse().withHeader("Content-Type", "application/json").withBody(commitResult).withStatus(201)));
    BoxFolder folder = new BoxFolder(this.api, "12345");
    BoxFile.Info uploadedFile = folder.uploadLargeFile(stream, "testfile.txt", 5);
    assertEquals("1111111", uploadedFile.getID());
    assertEquals("testuser@box.com", uploadedFile.getModifiedBy().getLogin());
    assertEquals("Test User", uploadedFile.getOwnedBy().getName());
    assertEquals("folder", uploadedFile.getParent().getType());
    assertEquals("testfile.txt", uploadedFile.getName());
}
Also used : JsonArray(com.eclipsesource.json.JsonArray) JsonObject(com.eclipsesource.json.JsonObject) Test(org.junit.Test)

Aggregations

JsonArray (com.eclipsesource.json.JsonArray)111 JsonObject (com.eclipsesource.json.JsonObject)96 JsonValue (com.eclipsesource.json.JsonValue)32 Test (org.junit.Test)30 URL (java.net.URL)28 ArrayList (java.util.ArrayList)27 HashMap (java.util.HashMap)11 Matchers.containsString (org.hamcrest.Matchers.containsString)6 JsonUtil.getString (com.hazelcast.util.JsonUtil.getString)5 Map (java.util.Map)5 Link (org.eclipse.leshan.Link)4 Date (java.util.Date)3 LwM2mResource (org.eclipse.leshan.core.node.LwM2mResource)3 ClientEndPointDTO (com.hazelcast.internal.management.dto.ClientEndPointDTO)2 MalformedURLException (java.net.MalformedURLException)2 DecimalFormat (java.text.DecimalFormat)2 DecimalFormatSymbols (java.text.DecimalFormatSymbols)2 Collection (java.util.Collection)2 HashSet (java.util.HashSet)2 LwM2mObjectInstance (org.eclipse.leshan.core.node.LwM2mObjectInstance)2