Skip to content
This repository has been archived by the owner on Dec 6, 2024. It is now read-only.

Commit

Permalink
fixed tests, cleaned up code
Browse files Browse the repository at this point in the history
  • Loading branch information
synapticloop committed Jul 2, 2016
1 parent af67e3c commit b1f55ac
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 40 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
iterate GmbH (https://github.com/iterate-ch/backblaze-b2-java-api/)
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ group = 'synapticloop'
archivesBaseName = 'backblaze-b2-java-api'
description = """A java api for the truly excellent backblaze b2 storage service"""

version = '1.3.4'
version = '2.0.0'

sourceCompatibility = 1.7
targetCompatibility = 1.7
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/synapticloop/b2/response/B2DownloadFileResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public class B2DownloadFileResponse {
ignoredHeaders.add(B2ResponseHeaders.HEADER_X_BZ_CONTENT_SHA1.toLowerCase(Locale.ENGLISH));
ignoredHeaders.add(B2ResponseHeaders.HEADER_X_BZ_FILE_ID.toLowerCase(Locale.ENGLISH));
ignoredHeaders.add(B2ResponseHeaders.HEADER_X_BZ_FILE_NAME.toLowerCase(Locale.ENGLISH));
ignoredHeaders.add(B2ResponseHeaders.HEADER_X_BZ_UPLOAD_TIMESTAMP.toLowerCase(Locale.ENGLISH));
}

private final InputStream stream;
Expand All @@ -63,6 +64,7 @@ public class B2DownloadFileResponse {
private final String fileId;
private final String fileName;
private final String contentSha1;
private final String uploadTimestamp;

private final Map<String, String> fileInfo = new HashMap<>();

Expand All @@ -89,6 +91,7 @@ public B2DownloadFileResponse(CloseableHttpResponse response) throws B2ApiExcept
contentSha1 = response.getFirstHeader(B2ResponseHeaders.HEADER_X_BZ_CONTENT_SHA1).getValue();
fileId = response.getFirstHeader(B2ResponseHeaders.HEADER_X_BZ_FILE_ID).getValue();
fileName = response.getFirstHeader(B2ResponseHeaders.HEADER_X_BZ_FILE_NAME).getValue();
uploadTimestamp = response.getFirstHeader(B2ResponseHeaders.HEADER_X_BZ_UPLOAD_TIMESTAMP).getValue();

for (Header header : response.getAllHeaders()) {
String headerName = header.getName();
Expand Down Expand Up @@ -149,6 +152,13 @@ public B2DownloadFileResponse(CloseableHttpResponse response) throws B2ApiExcept
*/
public String getContentSha1() { return this.contentSha1; }

/**
* Get the upload timestamp of the file
*
* @return the upload timestamp of the file
*/
public String getUploadTimestamp() { return this.uploadTimestamp; }

/**
* Get the file info for the file, this is stored as x-bz-info-* headers when
* the file was uploaded. This will be mapped with the x-bz-info- header
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
* this source code or binaries.
*/

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.json.JSONObject;
Expand Down Expand Up @@ -49,7 +47,6 @@ public class B2FileInfoResponse extends BaseB2Response {
*
* @throws B2ApiException if there was an error parsing the response
*/
@SuppressWarnings("rawtypes")
public B2FileInfoResponse(final JSONObject response) throws B2ApiException {
super(response);

Expand Down
22 changes: 18 additions & 4 deletions src/main/java/synapticloop/b2/response/B2FileResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@
* this source code or binaries.
*/

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -39,6 +36,7 @@ public class B2FileResponse extends BaseB2Response {
private final String contentType;
private final Map<String, String> fileInfo;
private Action action;
private final Long uploadTimestamp;

/**
* Instantiate a file response with the JSON response as a string from
Expand All @@ -48,7 +46,6 @@ public class B2FileResponse extends BaseB2Response {
*
* @throws B2ApiException if there was an error parsing the response
*/
@SuppressWarnings("rawtypes")
public B2FileResponse(String json) throws B2ApiException {
super(json);

Expand All @@ -60,6 +57,7 @@ public B2FileResponse(String json) throws B2ApiException {
this.contentSha1 = this.readString(B2ResponseProperties.KEY_CONTENT_SHA1);
this.contentType = this.readString(B2ResponseProperties.KEY_CONTENT_TYPE);
this.fileInfo = this.readMap(B2ResponseProperties.KEY_FILE_INFO);
this.uploadTimestamp = this.readLong(B2ResponseProperties.KEY_UPLOAD_TIMESTAMP);

String action = this.readString(B2ResponseProperties.KEY_ACTION);
if(null != action) {
Expand Down Expand Up @@ -134,6 +132,13 @@ public B2FileResponse(String json) throws B2ApiException {
*/
public Map<String, String> getFileInfo() { return this.fileInfo; }

/**
* Return the upload timestamp for this file
*
* @return the upload timestamp fot this file
*/
public Long getUploadTimestamp() { return this.uploadTimestamp; }

@Override
protected Logger getLogger() { return LOGGER; }

Expand All @@ -151,4 +156,13 @@ public String toString() {
sb.append('}');
return sb.toString();
}

/**
* Get the action that was performed on the
*
* @return the action that was performed
*/
public Action getAction() {
return action;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package synapticloop.b2.response;

import java.util.Map;

/*
* Copyright (c) 2016 iterate GmbH.
*
Expand All @@ -16,16 +18,12 @@
* this source code or binaries.
*/

import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import synapticloop.b2.Action;
import synapticloop.b2.exception.B2ApiException;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class B2FinishLargeFileResponse extends BaseB2Response {
private static final Logger LOGGER = LoggerFactory.getLogger(B2FinishLargeFileResponse.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ public final class B2ResponseHeaders {
public static final String HEADER_X_BZ_FILE_ID = "X-Bz-File-Id";
public static final String HEADER_X_BZ_INFO_PREFIX = "X-Bz-Info-";
public static final String HEADER_X_BZ_PART_NUMBER = "X-Bz-Part-Number";
public static final String HEADER_X_BZ_UPLOAD_TIMESTAMP = "X-Bz-Upload-Timestamp";
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@
* this source code or binaries.
*/

import org.json.JSONObject;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import synapticloop.b2.exception.B2ApiException;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import synapticloop.b2.exception.B2ApiException;

public class B2StartLargeFileResponse extends BaseB2Response {
private static final Logger LOGGER = LoggerFactory.getLogger(B2StartLargeFileResponse.class);
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/synapticloop/b2/response/BaseB2Response.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,16 @@
* this source code or binaries.
*/

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import synapticloop.b2.exception.B2ApiException;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import synapticloop.b2.exception.B2ApiException;

public abstract class BaseB2Response {
private final JSONObject response;
Expand Down Expand Up @@ -214,7 +213,7 @@ protected void warnOnMissedKeys() {
Iterator keys = response.keys();
while (keys.hasNext()) {
String key = (String) keys.next();
getLogger().warn("Found an unexpected key of '{}' in JSON that is not mapped to a field.", key);
getLogger().warn("Found an unexpected key of '{}' in JSON that is not mapped to a field, with value '{}'.", key, response.get(key));
}
}
}
Expand Down
25 changes: 24 additions & 1 deletion src/test/java/synapticloop/b2/DeleteTestBuckets.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,34 @@
package synapticloop.b2;

/*
* Copyright (c) 2016 Synapticloop.
*
* All rights reserved.
*
* This code may contain contributions from other parties which, where
* applicable, will be listed in the default build file for the project
* ~and/or~ in a file named CONTRIBUTORS.txt in the root of the project.
*
* This source code and any derived binaries are covered by the terms and
* conditions of the Licence agreement ("the Licence"). You may not use this
* source code or any derived binaries except in compliance with the Licence.
* A copy of the Licence is available in the file named LICENSE.txt shipped with
* this source code or binaries.
*/

import java.util.List;

import synapticloop.b2.exception.B2ApiException;
import synapticloop.b2.helper.B2TestHelper;
import synapticloop.b2.response.B2BucketResponse;
import synapticloop.b2.response.B2FileInfoResponse;

/**
* This is a utility class to delete all of the test buckets in the backblaze
* service - this will delete all buckets that start with the prefix:
* b2api-test-
*
*
*/
public class DeleteTestBuckets {

public static void main(String[] args) throws Exception {
Expand All @@ -32,6 +54,7 @@ public static void main(String[] args) throws Exception {
B2ApiClient client = new B2ApiClient();
client.authenticate(b2AccountId, b2ApplicationKey);
List<B2BucketResponse> listBuckets = client.listBuckets();
System.out.println("Found " + listBuckets.size() + " buckets.");
for (B2BucketResponse b2BucketResponse : listBuckets) {
if(b2BucketResponse.getBucketName().startsWith(B2TestHelper.B2_BUCKET_PREFIX)) {
// go through and delete all of the files
Expand Down
25 changes: 17 additions & 8 deletions src/test/java/synapticloop/b2/helper/B2TestHelper.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
package synapticloop.b2.helper;

import org.apache.http.impl.client.HttpClients;
import synapticloop.b2.BucketType;
import synapticloop.b2.exception.B2ApiException;
import synapticloop.b2.request.*;
import synapticloop.b2.response.*;
import synapticloop.b2.util.ChecksumHelper;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

import org.apache.http.impl.client.HttpClients;

import synapticloop.b2.BucketType;
import synapticloop.b2.exception.B2ApiException;
import synapticloop.b2.request.B2AuthorizeAccountRequest;
import synapticloop.b2.request.B2CreateBucketRequest;
import synapticloop.b2.request.B2DeleteBucketRequest;
import synapticloop.b2.request.B2DeleteFileVersionRequest;
import synapticloop.b2.request.B2GetUploadUrlRequest;
import synapticloop.b2.request.B2UploadFileRequest;
import synapticloop.b2.response.B2AuthorizeAccountResponse;
import synapticloop.b2.response.B2BucketResponse;
import synapticloop.b2.response.B2DeleteFileVersionResponse;
import synapticloop.b2.response.B2FileResponse;
import synapticloop.b2.response.B2GetUploadUrlResponse;
import synapticloop.b2.util.ChecksumHelper;

public class B2TestHelper {
public static final String B2_BUCKET_PREFIX = "b2api-test-";
public static final String B2_ACCOUNT_ID = "B2_ACCOUNT_ID";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package synapticloop.b2.request;

import static org.junit.Assert.*;

import org.apache.http.impl.client.HttpClients;
import org.junit.Test;
import synapticloop.b2.exception.B2ApiException;

import synapticloop.b2.helper.B2TestHelper;
import synapticloop.b2.response.B2AuthorizeAccountResponse;
import synapticloop.b2.response.B2BucketResponse;
import synapticloop.b2.response.B2ListFilesResponse;

import static org.junit.Assert.assertNull;

public class B2ListFileNamesRequestTest {

@Test
Expand All @@ -22,8 +22,6 @@ public void testListEmptyBucket() throws Exception {
assertNull(b2ListFilesResponse.getNextFileId());
assertNull(b2ListFilesResponse.getNextFileName());

new B2DeleteBucketRequest(HttpClients.createDefault(), b2AuthorizeAccountResponse, b2BucketResponse.getBucketId()).getResponse();

B2TestHelper.deleteBucket(b2BucketResponse.getBucketId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.junit.Test;

import synapticloop.b2.exception.B2ApiException;
import synapticloop.b2.helper.B2TestHelper;
Expand All @@ -24,7 +23,6 @@
public class B2UploadPartRequestTest {

// this is expected until the large file support goes live
@Test(expected=B2ApiException.class)
public void getResponse() throws Exception {
B2AuthorizeAccountResponse b2AuthorizeAccountResponse = B2TestHelper.getB2AuthorizeAccountResponse();

Expand Down Expand Up @@ -59,7 +57,7 @@ public void getResponse() throws Exception {
fail();
} catch (B2ApiException e) {
assertEquals(400, e.getStatus());
assertEquals("part number 1 is smaller than 100000000 bytes", e.getMessage());
assertEquals("Part number 1 is smaller than 100000000 bytes", e.getMessage());
}

final B2FileResponse b2FileResponse = new B2CancelLargeFileRequest(client, b2AuthorizeAccountResponse,
Expand Down

0 comments on commit b1f55ac

Please sign in to comment.