From b1f55ace058698f18ffbd8d68060d1bd641ab0dd Mon Sep 17 00:00:00 2001 From: synapticloop Date: Sat, 2 Jul 2016 12:00:06 +0100 Subject: [PATCH] fixed tests, cleaned up code --- CONTRIBUTORS.txt | 1 + build.gradle | 2 +- .../b2/response/B2DownloadFileResponse.java | 10 ++++++++ .../b2/response/B2FileInfoResponse.java | 3 --- .../b2/response/B2FileResponse.java | 22 +++++++++++++--- .../response/B2FinishLargeFileResponse.java | 8 +++--- .../b2/response/B2ResponseHeaders.java | 1 + .../b2/response/B2StartLargeFileResponse.java | 9 +++---- .../b2/response/BaseB2Response.java | 9 +++---- .../synapticloop/b2/DeleteTestBuckets.java | 25 ++++++++++++++++++- .../synapticloop/b2/helper/B2TestHelper.java | 25 +++++++++++++------ .../request/B2ListFileNamesRequestTest.java | 8 +++--- .../b2/request/B2UploadPartRequestTest.java | 4 +-- 13 files changed, 87 insertions(+), 40 deletions(-) create mode 100644 CONTRIBUTORS.txt diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt new file mode 100644 index 0000000..6373805 --- /dev/null +++ b/CONTRIBUTORS.txt @@ -0,0 +1 @@ +iterate GmbH (https://github.com/iterate-ch/backblaze-b2-java-api/) \ No newline at end of file diff --git a/build.gradle b/build.gradle index 4514dff..8e7d0ff 100644 --- a/build.gradle +++ b/build.gradle @@ -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 diff --git a/src/main/java/synapticloop/b2/response/B2DownloadFileResponse.java b/src/main/java/synapticloop/b2/response/B2DownloadFileResponse.java index 00220c2..aea97ab 100644 --- a/src/main/java/synapticloop/b2/response/B2DownloadFileResponse.java +++ b/src/main/java/synapticloop/b2/response/B2DownloadFileResponse.java @@ -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; @@ -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 fileInfo = new HashMap<>(); @@ -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(); @@ -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 diff --git a/src/main/java/synapticloop/b2/response/B2FileInfoResponse.java b/src/main/java/synapticloop/b2/response/B2FileInfoResponse.java index a3d02a7..38d0c75 100644 --- a/src/main/java/synapticloop/b2/response/B2FileInfoResponse.java +++ b/src/main/java/synapticloop/b2/response/B2FileInfoResponse.java @@ -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; @@ -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); diff --git a/src/main/java/synapticloop/b2/response/B2FileResponse.java b/src/main/java/synapticloop/b2/response/B2FileResponse.java index cdfcf7a..6826e1c 100644 --- a/src/main/java/synapticloop/b2/response/B2FileResponse.java +++ b/src/main/java/synapticloop/b2/response/B2FileResponse.java @@ -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; @@ -39,6 +36,7 @@ public class B2FileResponse extends BaseB2Response { private final String contentType; private final Map fileInfo; private Action action; + private final Long uploadTimestamp; /** * Instantiate a file response with the JSON response as a string from @@ -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); @@ -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) { @@ -134,6 +132,13 @@ public B2FileResponse(String json) throws B2ApiException { */ public Map 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; } @@ -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; + } } diff --git a/src/main/java/synapticloop/b2/response/B2FinishLargeFileResponse.java b/src/main/java/synapticloop/b2/response/B2FinishLargeFileResponse.java index de0c335..e8d459e 100644 --- a/src/main/java/synapticloop/b2/response/B2FinishLargeFileResponse.java +++ b/src/main/java/synapticloop/b2/response/B2FinishLargeFileResponse.java @@ -1,5 +1,7 @@ package synapticloop.b2.response; +import java.util.Map; + /* * Copyright (c) 2016 iterate GmbH. * @@ -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); diff --git a/src/main/java/synapticloop/b2/response/B2ResponseHeaders.java b/src/main/java/synapticloop/b2/response/B2ResponseHeaders.java index 9c9d423..1839b16 100644 --- a/src/main/java/synapticloop/b2/response/B2ResponseHeaders.java +++ b/src/main/java/synapticloop/b2/response/B2ResponseHeaders.java @@ -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"; } diff --git a/src/main/java/synapticloop/b2/response/B2StartLargeFileResponse.java b/src/main/java/synapticloop/b2/response/B2StartLargeFileResponse.java index b9676bc..8213d9f 100644 --- a/src/main/java/synapticloop/b2/response/B2StartLargeFileResponse.java +++ b/src/main/java/synapticloop/b2/response/B2StartLargeFileResponse.java @@ -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); diff --git a/src/main/java/synapticloop/b2/response/BaseB2Response.java b/src/main/java/synapticloop/b2/response/BaseB2Response.java index d500076..b84ebc2 100644 --- a/src/main/java/synapticloop/b2/response/BaseB2Response.java +++ b/src/main/java/synapticloop/b2/response/BaseB2Response.java @@ -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; @@ -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)); } } } diff --git a/src/test/java/synapticloop/b2/DeleteTestBuckets.java b/src/test/java/synapticloop/b2/DeleteTestBuckets.java index 1145738..4dfb405 100644 --- a/src/test/java/synapticloop/b2/DeleteTestBuckets.java +++ b/src/test/java/synapticloop/b2/DeleteTestBuckets.java @@ -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 { @@ -32,6 +54,7 @@ public static void main(String[] args) throws Exception { B2ApiClient client = new B2ApiClient(); client.authenticate(b2AccountId, b2ApplicationKey); List 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 diff --git a/src/test/java/synapticloop/b2/helper/B2TestHelper.java b/src/test/java/synapticloop/b2/helper/B2TestHelper.java index 7772eb3..197c24e 100644 --- a/src/test/java/synapticloop/b2/helper/B2TestHelper.java +++ b/src/test/java/synapticloop/b2/helper/B2TestHelper.java @@ -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"; diff --git a/src/test/java/synapticloop/b2/request/B2ListFileNamesRequestTest.java b/src/test/java/synapticloop/b2/request/B2ListFileNamesRequestTest.java index 6a216e0..1b8e763 100644 --- a/src/test/java/synapticloop/b2/request/B2ListFileNamesRequestTest.java +++ b/src/test/java/synapticloop/b2/request/B2ListFileNamesRequestTest.java @@ -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 @@ -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()); } } \ No newline at end of file diff --git a/src/test/java/synapticloop/b2/request/B2UploadPartRequestTest.java b/src/test/java/synapticloop/b2/request/B2UploadPartRequestTest.java index 648a05c..fdf7b0f 100644 --- a/src/test/java/synapticloop/b2/request/B2UploadPartRequestTest.java +++ b/src/test/java/synapticloop/b2/request/B2UploadPartRequestTest.java @@ -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; @@ -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(); @@ -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,