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

Commit b1f55ac

Browse files
author
synapticloop
committed
fixed tests, cleaned up code
1 parent af67e3c commit b1f55ac

13 files changed

+87
-40
lines changed

CONTRIBUTORS.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
iterate GmbH (https://github.com/iterate-ch/backblaze-b2-java-api/)

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ group = 'synapticloop'
1313
archivesBaseName = 'backblaze-b2-java-api'
1414
description = """A java api for the truly excellent backblaze b2 storage service"""
1515

16-
version = '1.3.4'
16+
version = '2.0.0'
1717

1818
sourceCompatibility = 1.7
1919
targetCompatibility = 1.7

src/main/java/synapticloop/b2/response/B2DownloadFileResponse.java

+10
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public class B2DownloadFileResponse {
5555
ignoredHeaders.add(B2ResponseHeaders.HEADER_X_BZ_CONTENT_SHA1.toLowerCase(Locale.ENGLISH));
5656
ignoredHeaders.add(B2ResponseHeaders.HEADER_X_BZ_FILE_ID.toLowerCase(Locale.ENGLISH));
5757
ignoredHeaders.add(B2ResponseHeaders.HEADER_X_BZ_FILE_NAME.toLowerCase(Locale.ENGLISH));
58+
ignoredHeaders.add(B2ResponseHeaders.HEADER_X_BZ_UPLOAD_TIMESTAMP.toLowerCase(Locale.ENGLISH));
5859
}
5960

6061
private final InputStream stream;
@@ -63,6 +64,7 @@ public class B2DownloadFileResponse {
6364
private final String fileId;
6465
private final String fileName;
6566
private final String contentSha1;
67+
private final String uploadTimestamp;
6668

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

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

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

155+
/**
156+
* Get the upload timestamp of the file
157+
*
158+
* @return the upload timestamp of the file
159+
*/
160+
public String getUploadTimestamp() { return this.uploadTimestamp; }
161+
152162
/**
153163
* Get the file info for the file, this is stored as x-bz-info-* headers when
154164
* the file was uploaded. This will be mapped with the x-bz-info- header

src/main/java/synapticloop/b2/response/B2FileInfoResponse.java

-3
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
* this source code or binaries.
1717
*/
1818

19-
import java.util.HashMap;
20-
import java.util.Iterator;
2119
import java.util.Map;
2220

2321
import org.json.JSONObject;
@@ -49,7 +47,6 @@ public class B2FileInfoResponse extends BaseB2Response {
4947
*
5048
* @throws B2ApiException if there was an error parsing the response
5149
*/
52-
@SuppressWarnings("rawtypes")
5350
public B2FileInfoResponse(final JSONObject response) throws B2ApiException {
5451
super(response);
5552

src/main/java/synapticloop/b2/response/B2FileResponse.java

+18-4
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,8 @@
1616
* this source code or binaries.
1717
*/
1818

19-
import java.util.HashMap;
20-
import java.util.Iterator;
2119
import java.util.Map;
2220

23-
import org.json.JSONObject;
2421
import org.slf4j.Logger;
2522
import org.slf4j.LoggerFactory;
2623

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

4341
/**
4442
* Instantiate a file response with the JSON response as a string from
@@ -48,7 +46,6 @@ public class B2FileResponse extends BaseB2Response {
4846
*
4947
* @throws B2ApiException if there was an error parsing the response
5048
*/
51-
@SuppressWarnings("rawtypes")
5249
public B2FileResponse(String json) throws B2ApiException {
5350
super(json);
5451

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

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

135+
/**
136+
* Return the upload timestamp for this file
137+
*
138+
* @return the upload timestamp fot this file
139+
*/
140+
public Long getUploadTimestamp() { return this.uploadTimestamp; }
141+
137142
@Override
138143
protected Logger getLogger() { return LOGGER; }
139144

@@ -151,4 +156,13 @@ public String toString() {
151156
sb.append('}');
152157
return sb.toString();
153158
}
159+
160+
/**
161+
* Get the action that was performed on the
162+
*
163+
* @return the action that was performed
164+
*/
165+
public Action getAction() {
166+
return action;
167+
}
154168
}

src/main/java/synapticloop/b2/response/B2FinishLargeFileResponse.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package synapticloop.b2.response;
22

3+
import java.util.Map;
4+
35
/*
46
* Copyright (c) 2016 iterate GmbH.
57
*
@@ -16,16 +18,12 @@
1618
* this source code or binaries.
1719
*/
1820

19-
import org.json.JSONObject;
2021
import org.slf4j.Logger;
2122
import org.slf4j.LoggerFactory;
23+
2224
import synapticloop.b2.Action;
2325
import synapticloop.b2.exception.B2ApiException;
2426

25-
import java.util.HashMap;
26-
import java.util.Iterator;
27-
import java.util.Map;
28-
2927
public class B2FinishLargeFileResponse extends BaseB2Response {
3028
private static final Logger LOGGER = LoggerFactory.getLogger(B2FinishLargeFileResponse.class);
3129

src/main/java/synapticloop/b2/response/B2ResponseHeaders.java

+1
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ public final class B2ResponseHeaders {
2727
public static final String HEADER_X_BZ_FILE_ID = "X-Bz-File-Id";
2828
public static final String HEADER_X_BZ_INFO_PREFIX = "X-Bz-Info-";
2929
public static final String HEADER_X_BZ_PART_NUMBER = "X-Bz-Part-Number";
30+
public static final String HEADER_X_BZ_UPLOAD_TIMESTAMP = "X-Bz-Upload-Timestamp";
3031
}

src/main/java/synapticloop/b2/response/B2StartLargeFileResponse.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@
1616
* this source code or binaries.
1717
*/
1818

19-
import org.json.JSONObject;
19+
20+
import java.util.Map;
21+
2022
import org.slf4j.Logger;
2123
import org.slf4j.LoggerFactory;
22-
import synapticloop.b2.exception.B2ApiException;
2324

24-
import java.util.HashMap;
25-
import java.util.Iterator;
26-
import java.util.Map;
25+
import synapticloop.b2.exception.B2ApiException;
2726

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

src/main/java/synapticloop/b2/response/BaseB2Response.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,16 @@
1616
* this source code or binaries.
1717
*/
1818

19+
import java.util.HashMap;
1920
import java.util.Iterator;
21+
import java.util.Map;
2022

2123
import org.json.JSONArray;
2224
import org.json.JSONException;
2325
import org.json.JSONObject;
2426
import org.slf4j.Logger;
25-
import synapticloop.b2.exception.B2ApiException;
2627

27-
import java.util.HashMap;
28-
import java.util.Iterator;
29-
import java.util.Map;
28+
import synapticloop.b2.exception.B2ApiException;
3029

3130
public abstract class BaseB2Response {
3231
private final JSONObject response;
@@ -214,7 +213,7 @@ protected void warnOnMissedKeys() {
214213
Iterator keys = response.keys();
215214
while (keys.hasNext()) {
216215
String key = (String) keys.next();
217-
getLogger().warn("Found an unexpected key of '{}' in JSON that is not mapped to a field.", key);
216+
getLogger().warn("Found an unexpected key of '{}' in JSON that is not mapped to a field, with value '{}'.", key, response.get(key));
218217
}
219218
}
220219
}

src/test/java/synapticloop/b2/DeleteTestBuckets.java

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,34 @@
11
package synapticloop.b2;
22

3+
/*
4+
* Copyright (c) 2016 Synapticloop.
5+
*
6+
* All rights reserved.
7+
*
8+
* This code may contain contributions from other parties which, where
9+
* applicable, will be listed in the default build file for the project
10+
* ~and/or~ in a file named CONTRIBUTORS.txt in the root of the project.
11+
*
12+
* This source code and any derived binaries are covered by the terms and
13+
* conditions of the Licence agreement ("the Licence"). You may not use this
14+
* source code or any derived binaries except in compliance with the Licence.
15+
* A copy of the Licence is available in the file named LICENSE.txt shipped with
16+
* this source code or binaries.
17+
*/
18+
319
import java.util.List;
420

5-
import synapticloop.b2.exception.B2ApiException;
621
import synapticloop.b2.helper.B2TestHelper;
722
import synapticloop.b2.response.B2BucketResponse;
823
import synapticloop.b2.response.B2FileInfoResponse;
924

25+
/**
26+
* This is a utility class to delete all of the test buckets in the backblaze
27+
* service - this will delete all buckets that start with the prefix:
28+
* b2api-test-
29+
*
30+
*
31+
*/
1032
public class DeleteTestBuckets {
1133

1234
public static void main(String[] args) throws Exception {
@@ -32,6 +54,7 @@ public static void main(String[] args) throws Exception {
3254
B2ApiClient client = new B2ApiClient();
3355
client.authenticate(b2AccountId, b2ApplicationKey);
3456
List<B2BucketResponse> listBuckets = client.listBuckets();
57+
System.out.println("Found " + listBuckets.size() + " buckets.");
3558
for (B2BucketResponse b2BucketResponse : listBuckets) {
3659
if(b2BucketResponse.getBucketName().startsWith(B2TestHelper.B2_BUCKET_PREFIX)) {
3760
// go through and delete all of the files

src/test/java/synapticloop/b2/helper/B2TestHelper.java

+17-8
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
11
package synapticloop.b2.helper;
22

3-
import org.apache.http.impl.client.HttpClients;
4-
import synapticloop.b2.BucketType;
5-
import synapticloop.b2.exception.B2ApiException;
6-
import synapticloop.b2.request.*;
7-
import synapticloop.b2.response.*;
8-
import synapticloop.b2.util.ChecksumHelper;
9-
103
import java.io.File;
114
import java.io.FileWriter;
12-
import java.io.IOException;
135
import java.util.Map;
146
import java.util.UUID;
157

8+
import org.apache.http.impl.client.HttpClients;
9+
10+
import synapticloop.b2.BucketType;
11+
import synapticloop.b2.exception.B2ApiException;
12+
import synapticloop.b2.request.B2AuthorizeAccountRequest;
13+
import synapticloop.b2.request.B2CreateBucketRequest;
14+
import synapticloop.b2.request.B2DeleteBucketRequest;
15+
import synapticloop.b2.request.B2DeleteFileVersionRequest;
16+
import synapticloop.b2.request.B2GetUploadUrlRequest;
17+
import synapticloop.b2.request.B2UploadFileRequest;
18+
import synapticloop.b2.response.B2AuthorizeAccountResponse;
19+
import synapticloop.b2.response.B2BucketResponse;
20+
import synapticloop.b2.response.B2DeleteFileVersionResponse;
21+
import synapticloop.b2.response.B2FileResponse;
22+
import synapticloop.b2.response.B2GetUploadUrlResponse;
23+
import synapticloop.b2.util.ChecksumHelper;
24+
1625
public class B2TestHelper {
1726
public static final String B2_BUCKET_PREFIX = "b2api-test-";
1827
public static final String B2_ACCOUNT_ID = "B2_ACCOUNT_ID";
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package synapticloop.b2.request;
22

3+
import static org.junit.Assert.*;
4+
35
import org.apache.http.impl.client.HttpClients;
46
import org.junit.Test;
5-
import synapticloop.b2.exception.B2ApiException;
7+
68
import synapticloop.b2.helper.B2TestHelper;
79
import synapticloop.b2.response.B2AuthorizeAccountResponse;
810
import synapticloop.b2.response.B2BucketResponse;
911
import synapticloop.b2.response.B2ListFilesResponse;
1012

11-
import static org.junit.Assert.assertNull;
12-
1313
public class B2ListFileNamesRequestTest {
1414

1515
@Test
@@ -22,8 +22,6 @@ public void testListEmptyBucket() throws Exception {
2222
assertNull(b2ListFilesResponse.getNextFileId());
2323
assertNull(b2ListFilesResponse.getNextFileName());
2424

25-
new B2DeleteBucketRequest(HttpClients.createDefault(), b2AuthorizeAccountResponse, b2BucketResponse.getBucketId()).getResponse();
26-
2725
B2TestHelper.deleteBucket(b2BucketResponse.getBucketId());
2826
}
2927
}

src/test/java/synapticloop/b2/request/B2UploadPartRequestTest.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import org.apache.http.entity.StringEntity;
99
import org.apache.http.impl.client.CloseableHttpClient;
1010
import org.apache.http.impl.client.HttpClients;
11-
import org.junit.Test;
1211

1312
import synapticloop.b2.exception.B2ApiException;
1413
import synapticloop.b2.helper.B2TestHelper;
@@ -24,7 +23,6 @@
2423
public class B2UploadPartRequestTest {
2524

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

@@ -59,7 +57,7 @@ public void getResponse() throws Exception {
5957
fail();
6058
} catch (B2ApiException e) {
6159
assertEquals(400, e.getStatus());
62-
assertEquals("part number 1 is smaller than 100000000 bytes", e.getMessage());
60+
assertEquals("Part number 1 is smaller than 100000000 bytes", e.getMessage());
6361
}
6462

6563
final B2FileResponse b2FileResponse = new B2CancelLargeFileRequest(client, b2AuthorizeAccountResponse,

0 commit comments

Comments
 (0)