Skip to content

Commit 2b3aa35

Browse files
author
Hans-Peter Klett
committed
Changed to mockito. Abstracted Ds3Client interface
Updated the tests to use mockito instead of jmockit because it doesn't use instrumentation. This way we can't do things that require magic to mock, and we don't conflict with code coverage tools.
1 parent 011b8e6 commit 2b3aa35

File tree

10 files changed

+361
-304
lines changed

10 files changed

+361
-304
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ atlassian-ide-plugin.xml
1414
*.txt
1515
*.class
1616
.*.swp
17+
/bin

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ dependencies {
3636
compile 'org.codehaus.woodstox:woodstox-core-asl:4.2.0'
3737
compile 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.2.3'
3838
compile 'com.google.guava:guava:16.0.1'
39+
testCompile 'org.mockito:mockito-all:1.9.5'
3940
testCompile 'junit:junit:4.11'
4041
}
4142

src/main/java/com/spectralogic/ds3client/Ds3Client.java

Lines changed: 39 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,19 @@
1616
package com.spectralogic.ds3client;
1717

1818
import java.io.IOException;
19-
import java.net.URI;
20-
import java.net.URISyntaxException;
2119
import java.security.SignatureException;
2220

2321
import com.spectralogic.ds3client.commands.*;
24-
import com.spectralogic.ds3client.models.Credentials;
25-
import com.spectralogic.ds3client.networking.NetworkClient;
2622

2723
/**
28-
* The main class for communicating with a DS3 appliance. All communication with a DS3 appliance should start with
24+
* The main interface for communicating with a DS3 appliance. All communication with a DS3 appliance should start with
2925
* this class.
3026
*
31-
* Here is an example showing how the Ds3Client class is used to get a list of buckets from a remote DS3 appliance.
27+
* Here is an example showing how the Ds3Client interface is used to get a list of buckets from a remote DS3 appliance.
3228
*
3329
* <pre>
3430
* {@code
35-
* final Ds3Client client = Ds3Client.builder("ds3Endpoint:8080",
31+
* final Ds3Client client = Ds3ClientBuilder.create("ds3Endpoint:8080",
3632
* new Credentials("accessKey", "secretKey")).build();
3733
*
3834
* final GetServiceResponse response = client.getService(new GetServiceRequest());
@@ -43,106 +39,7 @@
4339
* }
4440
* </pre>
4541
*/
46-
public class Ds3Client {
47-
48-
/**
49-
* A Builder class used to create a Ds3Client instance.
50-
*/
51-
public static class Builder implements com.spectralogic.ds3client.utils.Builder<Ds3Client> {
52-
53-
final private String endpoint;
54-
final private Credentials credentials;
55-
56-
private boolean secure = true;
57-
private URI proxy = null;
58-
private int retries = 5;
59-
60-
private Builder(final String endpoint, final Credentials credentials) throws IllegalArgumentException {
61-
if (endpoint == null || endpoint.isEmpty()) {
62-
throw new IllegalArgumentException("Endpoint must be non empty");
63-
}
64-
if(credentials == null || !credentials.isValid()) {
65-
throw new IllegalArgumentException("Credentials must be filled out.");
66-
}
67-
this.endpoint = endpoint;
68-
this.credentials = credentials;
69-
}
70-
71-
/**
72-
* Specifies if the library should use HTTP or HTTPS. The default is HTTP.
73-
* @param secure True will use HTTPS, false will use HTTP.
74-
* @return The current builder.
75-
*/
76-
public Builder withHttpSecure(final boolean secure) {
77-
this.secure = secure;
78-
return this;
79-
}
80-
81-
/**
82-
* Sets a HTTP proxy.
83-
* @param proxy The endpoint of the HTTP proxy.
84-
* @return The current builder.
85-
* @throws IllegalArgumentException This will be thrown if the proxy endpoint is not a valid URI.
86-
*/
87-
public Builder withProxy(final String proxy) throws IllegalArgumentException {
88-
try {
89-
final URI proxyUri;
90-
if(!proxy.startsWith("http")) {
91-
throw new IllegalArgumentException("Invalid proxy format. The web address must start with either http or https.");
92-
}
93-
proxyUri = new URI(proxy);
94-
95-
this.proxy = proxyUri;
96-
} catch (final URISyntaxException e) {
97-
throw new IllegalArgumentException("Invalid proxy format. Must be a web address.");
98-
}
99-
100-
return this;
101-
}
102-
103-
/**
104-
* Sets the number of retries the library will attempt to perform when it receives 307 redirects from a
105-
* DS3 appliance. The default is 5.
106-
* @param retries The number of times the library should perform retries on 307.
107-
* @return The current builder.
108-
*/
109-
public Builder withRedirectRetries(final int retries) {
110-
this.retries = retries;
111-
return this;
112-
}
113-
114-
/**
115-
* Returns a new Ds3Client instance.
116-
*/
117-
@Override
118-
public Ds3Client build() {
119-
final ConnectionDetailsImpl.Builder connBuilder = ConnectionDetailsImpl.builder(this.endpoint, this.credentials)
120-
.withProxy(this.proxy).withSecure(this.secure).withRedirectRetries(this.retries);
121-
122-
final NetworkClient netClient = new NetworkClientImpl(connBuilder.build());
123-
return new Ds3Client(netClient);
124-
}
125-
}
126-
127-
/**
128-
* Returns a Builder which is used to customize the behavior of the Ds3Client library.
129-
* @param endpoint The DS3 endpoint the library should connect to.
130-
* @param creds The {@link Credentials} used for connecting to a DS3 endpoint.
131-
* @return The Builder for the {@link Ds3Client} object.
132-
*/
133-
public static Builder builder(final String endpoint, final Credentials creds) {
134-
return new Builder(endpoint, creds);
135-
}
136-
137-
private final NetworkClient netClient;
138-
139-
Ds3Client(final NetworkClient netClient) {
140-
this.netClient = netClient;
141-
}
142-
143-
NetworkClient getNetClient() {
144-
return this.netClient;
145-
}
42+
public interface Ds3Client {
14643

14744
/**
14845
* Gets the list of buckets.
@@ -151,9 +48,8 @@ NetworkClient getNetClient() {
15148
* @throws IOException
15249
* @throws SignatureException
15350
*/
154-
public GetServiceResponse getService(final GetServiceRequest request) throws IOException, SignatureException {
155-
return new GetServiceResponse(this.netClient.getResponse(request));
156-
}
51+
public abstract GetServiceResponse getService(GetServiceRequest request)
52+
throws IOException, SignatureException;
15753

15854
/**
15955
* Gets the list of objects in a bucket.
@@ -167,9 +63,8 @@ public GetServiceResponse getService(final GetServiceRequest request) throws IOE
16763
* @throws IOException
16864
* @throws SignatureException
16965
*/
170-
public GetBucketResponse getBucket(final GetBucketRequest request) throws IOException, SignatureException {
171-
return new GetBucketResponse(this.netClient.getResponse(request));
172-
}
66+
public abstract GetBucketResponse getBucket(GetBucketRequest request)
67+
throws IOException, SignatureException;
17368

17469
/**
17570
* Puts a new bucket to a DS3 endpoint
@@ -182,9 +77,8 @@ public GetBucketResponse getBucket(final GetBucketRequest request) throws IOExce
18277
* @throws IOException
18378
* @throws SignatureException
18479
*/
185-
public PutBucketResponse putBucket(final PutBucketRequest request) throws IOException, SignatureException {
186-
return new PutBucketResponse(this.netClient.getResponse(request));
187-
}
80+
public abstract PutBucketResponse putBucket(PutBucketRequest request)
81+
throws IOException, SignatureException;
18882

18983
/**
19084
* Performs a HTTP HEAD for a bucket. The HEAD will return information about if the bucket exists, or if the user
@@ -195,9 +89,8 @@ public PutBucketResponse putBucket(final PutBucketRequest request) throws IOExce
19589
* @throws IOException
19690
* @throws SignatureException
19791
*/
198-
public HeadBucketResponse headBucket(final HeadBucketRequest request) throws IOException, SignatureException {
199-
return new HeadBucketResponse(this.netClient.getResponse(request));
200-
}
92+
public abstract HeadBucketResponse headBucket(HeadBucketRequest request)
93+
throws IOException, SignatureException;
20194

20295
/**
20396
* Deletes a bucket from a DS3 endpoint. <b>Note:</b> all objects must be deleted first before deleteBucket will
@@ -211,9 +104,8 @@ public HeadBucketResponse headBucket(final HeadBucketRequest request) throws IOE
211104
* @throws IOException
212105
* @throws SignatureException
213106
*/
214-
public DeleteBucketResponse deleteBucket(final DeleteBucketRequest request) throws IOException, SignatureException {
215-
return new DeleteBucketResponse(this.netClient.getResponse(request));
216-
}
107+
public abstract DeleteBucketResponse deleteBucket(
108+
DeleteBucketRequest request) throws IOException, SignatureException;
217109

218110
/**
219111
* Deletes an object in a bucket from a DS3 endpoint
@@ -226,9 +118,8 @@ public DeleteBucketResponse deleteBucket(final DeleteBucketRequest request) thro
226118
* @throws IOException
227119
* @throws SignatureException
228120
*/
229-
public DeleteObjectResponse deleteObject(final DeleteObjectRequest request) throws IOException, SignatureException {
230-
return new DeleteObjectResponse(this.netClient.getResponse(request));
231-
}
121+
public abstract DeleteObjectResponse deleteObject(
122+
DeleteObjectRequest request) throws IOException, SignatureException;
232123

233124
/**
234125
* Get an object in a bucket from a DS3 endpoint
@@ -240,9 +131,8 @@ public DeleteObjectResponse deleteObject(final DeleteObjectRequest request) thro
240131
* @throws IOException
241132
* @throws SignatureException
242133
*/
243-
public GetObjectResponse getObject(final GetObjectRequest request) throws IOException, SignatureException {
244-
return new GetObjectResponse(this.netClient.getResponse(request));
245-
}
134+
public abstract GetObjectResponse getObject(GetObjectRequest request)
135+
throws IOException, SignatureException;
246136

247137
/**
248138
* Puts a new object to an existing bucket to a DS3 endpoint
@@ -255,9 +145,8 @@ public GetObjectResponse getObject(final GetObjectRequest request) throws IOExce
255145
* @throws IOException
256146
* @throws SignatureException
257147
*/
258-
public PutObjectResponse putObject(final PutObjectRequest request) throws IOException, SignatureException {
259-
return new PutObjectResponse(this.netClient.getResponse(request));
260-
}
148+
public abstract PutObjectResponse putObject(PutObjectRequest request)
149+
throws IOException, SignatureException;
261150

262151
/**
263152
* Primes the Ds3 appliance for a Bulk Get. This does not perform the gets for each individual files. See
@@ -270,9 +159,8 @@ public PutObjectResponse putObject(final PutObjectRequest request) throws IOExce
270159
* @throws IOException
271160
* @throws SignatureException
272161
*/
273-
public BulkGetResponse bulkGet(final BulkGetRequest request) throws IOException, SignatureException {
274-
return new BulkGetResponse(this.netClient.getResponse(request));
275-
}
162+
public abstract BulkGetResponse bulkGet(BulkGetRequest request)
163+
throws IOException, SignatureException;
276164

277165
/**
278166
* Primes the Ds3 appliance for a Bulk Put. This does not perform the puts for each individual files. See
@@ -285,18 +173,23 @@ public BulkGetResponse bulkGet(final BulkGetRequest request) throws IOException,
285173
* @throws IOException
286174
* @throws SignatureException
287175
*/
288-
public BulkPutResponse bulkPut(final BulkPutRequest request) throws IOException, SignatureException {
289-
return new BulkPutResponse(this.netClient.getResponse(request));
290-
}
176+
public abstract BulkPutResponse bulkPut(BulkPutRequest request)
177+
throws IOException, SignatureException;
178+
179+
/**
180+
* Queries the list of active jobs on the server.
181+
* @throws IOException
182+
* @throws SignatureException
183+
*/
184+
public abstract GetJobListResponse getJobList(GetJobListRequest request)
185+
throws IOException, SignatureException;
291186

292-
//TODO
293-
public GetJobListResponse getJobList(final GetJobListRequest request) throws IOException, SignatureException {
294-
return new GetJobListResponse(this.netClient.getResponse(request));
295-
}
187+
/**
188+
* Queries the job details for a given job id. Includes the objects that are in cache or haven't been transferred.
189+
* @throws IOException
190+
* @throws SignatureException
191+
*/
192+
public abstract GetJobResponse getJob(GetJobRequest request)
193+
throws IOException, SignatureException;
296194

297-
//TODO
298-
public GetJobResponse getJob(final GetJobRequest request) throws IOException, SignatureException {
299-
return new GetJobResponse(this.netClient.getResponse(request));
300-
}
301195
}
302-
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.spectralogic.ds3client;
2+
3+
import java.net.URI;
4+
import java.net.URISyntaxException;
5+
6+
import com.spectralogic.ds3client.models.Credentials;
7+
import com.spectralogic.ds3client.networking.NetworkClient;
8+
9+
/**
10+
* A Builder class used to create a Ds3Client instance.
11+
*/
12+
public class Ds3ClientBuilder implements com.spectralogic.ds3client.utils.Builder<Ds3Client> {
13+
14+
final private String endpoint;
15+
final private Credentials credentials;
16+
17+
private boolean secure = true;
18+
private URI proxy = null;
19+
private int retries = 5;
20+
21+
private Ds3ClientBuilder(final String endpoint, final Credentials credentials) throws IllegalArgumentException {
22+
if (endpoint == null || endpoint.isEmpty()) {
23+
throw new IllegalArgumentException("Endpoint must be non empty");
24+
}
25+
if(credentials == null || !credentials.isValid()) {
26+
throw new IllegalArgumentException("Credentials must be filled out.");
27+
}
28+
this.endpoint = endpoint;
29+
this.credentials = credentials;
30+
}
31+
32+
/**
33+
* Returns a Builder which is used to customize the behavior of the Ds3Client library.
34+
* @param endpoint The DS3 endpoint the library should connect to.
35+
* @param creds The {@link Credentials} used for connecting to a DS3 endpoint.
36+
* @return The Builder for the {@link Ds3ClientImpl} object.
37+
*/
38+
public static Ds3ClientBuilder create(final String endpoint, final Credentials creds) {
39+
return new Ds3ClientBuilder(endpoint, creds);
40+
}
41+
42+
/**
43+
* Specifies if the library should use HTTP or HTTPS. The default is HTTP.
44+
* @param secure True will use HTTPS, false will use HTTP.
45+
* @return The current builder.
46+
*/
47+
public Ds3ClientBuilder withHttpSecure(final boolean secure) {
48+
this.secure = secure;
49+
return this;
50+
}
51+
52+
/**
53+
* Sets a HTTP proxy.
54+
* @param proxy The endpoint of the HTTP proxy.
55+
* @return The current builder.
56+
* @throws IllegalArgumentException This will be thrown if the proxy endpoint is not a valid URI.
57+
*/
58+
public Ds3ClientBuilder withProxy(final String proxy) throws IllegalArgumentException {
59+
try {
60+
final URI proxyUri;
61+
if(!proxy.startsWith("http")) {
62+
throw new IllegalArgumentException("Invalid proxy format. The web address must start with either http or https.");
63+
}
64+
proxyUri = new URI(proxy);
65+
66+
this.proxy = proxyUri;
67+
} catch (final URISyntaxException e) {
68+
throw new IllegalArgumentException("Invalid proxy format. Must be a web address.");
69+
}
70+
71+
return this;
72+
}
73+
74+
/**
75+
* Sets the number of retries the library will attempt to perform when it receives 307 redirects from a
76+
* DS3 appliance. The default is 5.
77+
* @param retries The number of times the library should perform retries on 307.
78+
* @return The current builder.
79+
*/
80+
public Ds3ClientBuilder withRedirectRetries(final int retries) {
81+
this.retries = retries;
82+
return this;
83+
}
84+
85+
/**
86+
* Returns a new Ds3Client instance.
87+
*/
88+
@Override
89+
public Ds3Client build() {
90+
final ConnectionDetailsImpl.Builder connBuilder = ConnectionDetailsImpl.builder(this.endpoint, this.credentials)
91+
.withProxy(this.proxy).withSecure(this.secure).withRedirectRetries(this.retries);
92+
93+
final NetworkClient netClient = new NetworkClientImpl(connBuilder.build());
94+
return new Ds3ClientImpl(netClient);
95+
}
96+
}

0 commit comments

Comments
 (0)