Exemplo n.º 1
0
  @Test
  @Category(IntegrationTest.class)
  public void createAndUpdateSharedLinkSucceeds() {
    BoxAPIConnection api = new BoxAPIConnection(TestConfig.getAccessToken());
    BoxFolder rootFolder = BoxFolder.getRootFolder(api);
    String fileName = "[createAndUpdateSharedLinkSucceeds] Test File.txt";
    byte[] fileBytes = "Non-empty string".getBytes(StandardCharsets.UTF_8);

    InputStream uploadStream = new ByteArrayInputStream(fileBytes);
    BoxFile uploadedFile = rootFolder.uploadFile(uploadStream, fileName).getResource();
    BoxSharedLink.Permissions permissions = new BoxSharedLink.Permissions();
    permissions.setCanDownload(true);
    permissions.setCanPreview(true);
    BoxSharedLink sharedLink =
        uploadedFile.createSharedLink(BoxSharedLink.Access.OPEN, null, permissions);

    assertThat(sharedLink.getURL(), not(isEmptyOrNullString()));

    sharedLink.getPermissions().setCanDownload(false);
    BoxFile.Info info = uploadedFile.new Info();
    info.setSharedLink(sharedLink);
    uploadedFile.updateInfo(info);

    assertThat(info.getSharedLink().getPermissions().getCanDownload(), is(false));

    uploadedFile.delete();
  }
Exemplo n.º 2
0
  @Test
  @Category(IntegrationTest.class)
  public void downloadFileRangeSucceeds() throws IOException {
    BoxAPIConnection api = new BoxAPIConnection(TestConfig.getAccessToken());
    BoxFolder rootFolder = BoxFolder.getRootFolder(api);
    String fileName = "Tamme-Lauri_tamm_suvepäeval.jpg";
    URL fileURL = this.getClass().getResource("/sample-files/" + fileName);
    String filePath = URLDecoder.decode(fileURL.getFile(), "utf-8");
    long fileSize = new File(filePath).length();
    byte[] fileContent = readAllBytes(filePath);

    InputStream uploadStream = new FileInputStream(filePath);
    ProgressListener mockUploadListener = mock(ProgressListener.class);
    BoxFile.Info uploadedFileInfo =
        rootFolder.uploadFile(uploadStream, fileName, fileSize, mockUploadListener);
    BoxFile uploadedFile = uploadedFileInfo.getResource();
    long firstHalf = uploadedFileInfo.getSize() / 2;

    ByteArrayOutputStream downloadStream = new ByteArrayOutputStream();
    uploadedFile.downloadRange(downloadStream, 0, firstHalf);
    uploadedFile.downloadRange(downloadStream, firstHalf + 1);
    byte[] downloadedFileContent = downloadStream.toByteArray();

    assertThat(downloadedFileContent, is(equalTo(fileContent)));
    assertThat(
        rootFolder,
        hasItem(Matchers.<BoxItem.Info>hasProperty("ID", equalTo(uploadedFile.getID()))));
    verify(mockUploadListener, atLeastOnce())
        .onProgressChanged(anyLong(), longThat(is(equalTo(fileSize))));

    uploadedFile.delete();
  }
Exemplo n.º 3
0
  @Test
  @Category(IntegrationTest.class)
  public void updateFileWithSpecialCharsInNameSucceeds() {
    BoxAPIConnection api = new BoxAPIConnection(TestConfig.getAccessToken());
    BoxFolder rootFolder = BoxFolder.getRootFolder(api);
    String originalFileName = "[updateFileWithSpecialCharsInNameSucceeds] abc\";def.txt";
    String fileContent = "Test file";
    byte[] fileBytes = fileContent.getBytes(StandardCharsets.UTF_8);

    InputStream uploadStream = new ByteArrayInputStream(fileBytes);
    BoxFile.Info uploadedFileInfo = rootFolder.uploadFile(uploadStream, originalFileName);
    BoxFile uploadedFile = uploadedFileInfo.getResource();

    assertThat(uploadedFileInfo.getName(), is(equalTo(originalFileName)));

    uploadedFile.delete();
  }
Exemplo n.º 4
0
  @Test
  @Category(IntegrationTest.class)
  public void getInfoWithOnlyTheNameField() {
    BoxAPIConnection api = new BoxAPIConnection(TestConfig.getAccessToken());
    BoxFolder rootFolder = BoxFolder.getRootFolder(api);
    String fileName = "[getInfoWithOnlyTheNameField] Test File.txt";
    String fileContent = "Test file";
    byte[] fileBytes = fileContent.getBytes(StandardCharsets.UTF_8);

    InputStream uploadStream = new ByteArrayInputStream(fileBytes);
    BoxFile uploadedFile = rootFolder.uploadFile(uploadStream, fileName).getResource();
    BoxFile.Info uploadedFileInfo = uploadedFile.getInfo("name");

    assertThat(uploadedFileInfo.getName(), is(equalTo(fileName)));
    assertThat(uploadedFileInfo.getDescription(), is(nullValue()));
    assertThat(uploadedFileInfo.getSize(), is(equalTo(0L)));

    uploadedFileInfo.getResource().delete();
  }
Exemplo n.º 5
0
  @Test
  @Category(IntegrationTest.class)
  public void fileLockAndUnlockSucceeds() {
    BoxAPIConnection api = new BoxAPIConnection(TestConfig.getAccessToken());
    BoxFolder rootFolder = BoxFolder.getRootFolder(api);
    String fileName = "[getInfoWithOnlyTheLockField] Test File.txt";
    String fileContent = "Test file";
    byte[] fileBytes = fileContent.getBytes(StandardCharsets.UTF_8);

    InputStream uploadStream = new ByteArrayInputStream(fileBytes);
    BoxFile uploadedFile = rootFolder.uploadFile(uploadStream, fileName).getResource();

    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.DAY_OF_YEAR, 1);
    Date expiresAt = calendar.getTime();
    uploadedFile.lock(expiresAt, false);

    BoxFile.Info uploadedFileInfo = uploadedFile.getInfo("lock", "created_by");
    BoxLock fileLock = uploadedFileInfo.getLock();

    assertThat(fileLock, is(instanceOf(BoxLock.class)));
    assertThat(fileLock.getExpiresAt().toString(), is(equalTo(expiresAt.toString())));
    assertThat(fileLock.getIsDownloadPrevented(), is(equalTo(false)));
    assertThat(
        fileLock.getCreatedBy().getID(), is(equalTo(uploadedFileInfo.getCreatedBy().getID())));

    uploadedFile.unlock();

    BoxFile.Info updatedFileInfo = uploadedFile.getInfo("lock");
    assertThat(updatedFileInfo.getLock(), is(nullValue()));

    updatedFileInfo.getResource().delete();
  }
Exemplo n.º 6
0
  @Test
  @Category(IntegrationTest.class)
  public void copyFileSucceeds() throws UnsupportedEncodingException {
    BoxAPIConnection api = new BoxAPIConnection(TestConfig.getAccessToken());
    BoxFolder rootFolder = BoxFolder.getRootFolder(api);
    String originalFileName = "[copyFileSucceeds] Original File.txt";
    String newFileName = "[copyFileSucceeds] New File.txt";
    String fileContent = "Test file";
    byte[] fileBytes = fileContent.getBytes(StandardCharsets.UTF_8);

    InputStream uploadStream = new ByteArrayInputStream(fileBytes);
    BoxFile uploadedFile = rootFolder.uploadFile(uploadStream, originalFileName).getResource();

    BoxFile.Info copiedFileInfo = uploadedFile.copy(rootFolder, newFileName);
    BoxFile copiedFile = copiedFileInfo.getResource();

    ByteArrayOutputStream downloadStream = new ByteArrayOutputStream();
    copiedFile.download(downloadStream);
    String downloadedContent = downloadStream.toString(StandardCharsets.UTF_8.name());
    assertThat(downloadedContent, equalTo(fileContent));

    uploadedFile.delete();
    copiedFile.delete();
  }
Exemplo n.º 7
0
  @Test
  @Category(IntegrationTest.class)
  public void getInfoWithAllFields() {
    BoxAPIConnection api = new BoxAPIConnection(TestConfig.getAccessToken());
    BoxFolder rootFolder = BoxFolder.getRootFolder(api);
    String fileName = "[getInfoWithAllFields] Test File.txt";
    String fileContent = "Test file";
    byte[] fileBytes = fileContent.getBytes(StandardCharsets.UTF_8);

    InputStream uploadStream = new ByteArrayInputStream(fileBytes);
    BoxFile uploadedFile = rootFolder.uploadFile(uploadStream, fileName).getResource();
    BoxFile.Info uploadedFileInfo = uploadedFile.getInfo(BoxFile.ALL_FIELDS);

    assertThat(uploadedFileInfo.getName(), is(equalTo(fileName)));
    assertThat(uploadedFileInfo.getVersionNumber(), is(equalTo("1")));
    assertThat(uploadedFileInfo.getCommentCount(), is(equalTo(0L)));
    assertThat(uploadedFileInfo.getExtension(), is(equalTo("txt")));
    assertThat(uploadedFileInfo.getIsPackage(), is(false));
    assertThat(uploadedFileInfo.getItemStatus(), is(equalTo("active")));
    assertThat(uploadedFileInfo.getVersion(), not(nullValue()));
    assertThat(uploadedFileInfo.getVersion().getVersionID(), not(nullValue()));

    uploadedFileInfo.getResource().delete();
  }