Skip to content

Commit 01b98a2

Browse files
committed
Fixes formatting and consistency of tests
1 parent f892355 commit 01b98a2

File tree

3 files changed

+56
-61
lines changed

3 files changed

+56
-61
lines changed

src/main/java/com/toopher/ToopherAPI.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,6 @@ public static class Builder {
582582
public HttpClient httpClient;
583583
public AuthenticationStatusFactory authenticationStatusFactory;
584584

585-
586585
public Builder(String consumerKey, String consumerSecret) {
587586
this.consumerKey = consumerKey;
588587
this.consumerSecret = consumerSecret;

src/test/java/com/toopher/HttpClientMock.java

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
import java.util.Map;
3030
import java.util.concurrent.Semaphore;
3131

32-
33-
3432
@Ignore
3533
public class HttpClientMock extends DefaultHttpClient {
3634
public HttpParams lastParams;
@@ -52,7 +50,7 @@ public HttpClientMock(int responseStatus, String responseBody) throws Interrupte
5250

5351
public HttpClientMock(Map<URI, ResponseMock> responses) throws InterruptedException {
5452
expectedUriResponses = new HashMap<URI, ResponseMock>();
55-
for(URI url : responses.keySet()){
53+
for (URI url : responses.keySet()) {
5654
expectedUriResponses.put(url, responses.get(url));
5755
}
5856
done = new Semaphore(1);
@@ -72,22 +70,20 @@ public String getLastCalledData(String key) {
7270
}
7371
return null;
7472
}
75-
public URI getLastCalledEndpoint(){
76-
if(lastURI != null) {
73+
74+
public URI getLastCalledEndpoint() {
7775
return lastURI;
78-
}
79-
return null;
8076
}
8177

82-
public String getExpectedResponse(){
83-
if(expectedUriResponses != null) {
78+
public String getExpectedResponse() {
79+
if (expectedUriResponses != null) {
8480
return expectedUriResponses.get(lastURI).getResponseBody();
8581
}
8682
return null;
8783
}
8884

89-
public int getExpectedResponseStatus(){
90-
if(expectedUriResponses != null) {
85+
public int getExpectedResponseStatus() {
86+
if (expectedUriResponses != null) {
9187
return expectedUriResponses.get(lastURI).getStatusCode();
9288
}
9389
return -1;
@@ -96,10 +92,10 @@ public int getExpectedResponseStatus(){
9692
@Override
9793
public <T> T execute(HttpUriRequest req, ResponseHandler<? extends T> responseHandler) throws IOException {
9894
lastRequest = req;
99-
if (req instanceof HttpPost){
95+
if (req instanceof HttpPost) {
10096
try {
10197
lastParams = new BasicHttpParams();
102-
for(NameValuePair nvp : URLEncodedUtils.parse(((HttpPost) req).getEntity())){
98+
for (NameValuePair nvp : URLEncodedUtils.parse(((HttpPost) req).getEntity())) {
10399
lastParams.setParameter(nvp.getName(), nvp.getValue());
104100
}
105101
} catch (IOException e) {
@@ -109,7 +105,7 @@ public <T> T execute(HttpUriRequest req, ResponseHandler<? extends T> responseHa
109105
lastParams = req.getParams();
110106
}
111107
BasicHttpEntity entity = new BasicHttpEntity();
112-
if(expectedResponseBody == null && expectedUriResponses != null) {
108+
if (expectedResponseBody == null && expectedUriResponses != null) {
113109
expectedResponseBody = expectedUriResponses.get(req.getURI()).getResponseBody();
114110
expectedResponseStatus = expectedUriResponses.get(req.getURI()).getStatusCode();
115111
}

src/test/java/com/toopher/TestToopherAPI.java

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,13 @@ public class TestToopherAPI {
1717

1818
private static String AUTH_REQUEST_JSON = "{'id':'1', 'granted':true, 'pending':true,'automated':true, 'reason':'test', 'terminal':{'id':'1','name':'some user'}}".replace("'", "\"");
1919
private final String BASE_URI_STRING = "https://api.toopher.test/v1/";
20-
private final String CONSUMER_KEY = "key";
20+
private final String CONSUMER_KEY = "key";
2121
private final String CONSUMER_SECRET = "secret";
22-
private final String PAIRING_ID = "pairing_id";
23-
private final String TERMINAL_NAME = "terminal_name";
24-
private final String ACTION_NAME = "action_name";
22+
private final String PAIRING_ID = "pairing_id";
23+
private final String TERMINAL_NAME = "terminal_name";
24+
private final String ACTION_NAME = "action_name";
2525
private final String USER_NAME = "user_name";
2626

27-
2827
@Test
2928
public void testCreatePairing() throws InterruptedException, RequestError {
3029
HttpClientMock httpClient = new HttpClientMock(200,
@@ -36,7 +35,7 @@ public void testCreatePairing() throws InterruptedException, RequestError {
3635

3736
assertEquals(httpClient.getLastCalledMethod(), "POST");
3837
assertEquals(httpClient.getLastCalledData("pairing_phrase"), "awkward turtle");
39-
38+
4039
assertEquals(pairing.userId, "1");
4140
assertEquals(pairing.userName, "some user");
4241
assertTrue(pairing.pending);
@@ -62,32 +61,25 @@ public void testGetPairingStatus() throws InterruptedException, RequestError {
6261

6362
@Test
6463
public void testAuthenticateWithActionName() throws InterruptedException, RequestError {
65-
String pairingId = "pairing ID";
66-
String terminalName = "my computer";
67-
String actionName = "action";
6864
HttpClientMock httpClient = new HttpClientMock(200, AUTH_REQUEST_JSON);
6965
ToopherAPI toopherApi = new ToopherAPI("key", "secret",
7066
createURI("https://api.toopher.test/v1/"), httpClient);
71-
try{
72-
toopherApi.authenticate(pairingId, terminalName, actionName);
73-
} catch(RequestError re){}
67+
toopherApi.authenticate(PAIRING_ID, TERMINAL_NAME, ACTION_NAME);
7468
assertEquals(httpClient.getLastCalledMethod(), "POST");
75-
assertEquals(httpClient.getLastCalledData("pairing_id"), pairingId);
76-
assertEquals(httpClient.getLastCalledData("terminal_name"), terminalName);
77-
assertEquals(httpClient.getLastCalledData("action_name"), actionName);
69+
assertEquals(httpClient.getLastCalledData("pairing_id"), PAIRING_ID);
70+
assertEquals(httpClient.getLastCalledData("terminal_name"), TERMINAL_NAME);
71+
assertEquals(httpClient.getLastCalledData("action_name"), ACTION_NAME);
7872
}
7973

8074
@Test
8175
public void testAuthenticateThrowingRequestError() throws InterruptedException, RequestError {
82-
String pairingId = "pairing ID";
83-
String terminalName = "my computer";
8476
HttpClientMock httpClient = new HttpClientMock(200, "{'id':'1'}");
8577
ToopherAPI toopherApi = new ToopherAPI("key", "secret",
8678
createURI("https://api.toopher.test/v1/"), httpClient);
8779
try {
88-
toopherApi.authenticate(pairingId, terminalName);
89-
} catch(RequestError re){
90-
assertTrue(true);
80+
toopherApi.authenticate(PAIRING_ID, TERMINAL_NAME);
81+
fail();
82+
} catch (RequestError re) {
9183
}
9284
}
9385

@@ -97,7 +89,7 @@ public void testAuthenticateWithTerminalName() throws InterruptedException, Requ
9789
String terminalName = "my computer";
9890
HttpClientMock httpClient = new HttpClientMock(200, AUTH_REQUEST_JSON);
9991
ToopherAPI toopherApi = new ToopherAPI("key", "secret",
100-
createURI("https://api.toopher.test/v1/"), httpClient);
92+
createURI("https://api.toopher.test/v1/"), httpClient);
10193
toopherApi.authenticate(pairingId, terminalName);
10294
assertEquals(httpClient.getLastCalledMethod(), "POST");
10395
assertEquals(httpClient.getLastCalledData("pairing_id"), pairingId);
@@ -122,17 +114,15 @@ public void testAuthenticateWithExtras() throws InterruptedException, RequestErr
122114
assertEquals(httpClient.getLastCalledData("extra_parameter"), extraParameter);
123115
}
124116

125-
126-
127117
@Test
128118
public void testAuthenticateByUserNameNoExtras() throws InterruptedException, RequestError {
129119
String extraTerminalName = "terminalNameExtra";
130120
String userName = "userName";
131121
String actionName = "action";
132122
HttpClientMock httpClient = new HttpClientMock(200, AUTH_REQUEST_JSON);
133123
ToopherAPI toopherApi = new ToopherAPI("key", "secret",
134-
createURI("https://api.toopher.test/v1/"), httpClient);
135-
toopherApi.authenticateByUserName(userName, extraTerminalName, actionName, null);
124+
createURI("https://api.toopher.test/v1/"), httpClient);
125+
toopherApi.authenticateByUserName(userName, extraTerminalName, actionName, null);
136126
assertEquals(httpClient.getLastCalledData("user_name"), userName);
137127
assertEquals(httpClient.getLastCalledData("terminal_name_extra"), extraTerminalName);
138128
assertEquals(httpClient.getLastCalledData("action_name"), actionName);
@@ -147,7 +137,7 @@ public void testAuthenticateByUserNameWithExtras() throws InterruptedException,
147137
HttpClientMock httpClient = new HttpClientMock(200, AUTH_REQUEST_JSON);
148138
ToopherAPI toopherApi = new ToopherAPI("key", "secret",
149139
createURI("https://api.toopher.test/v1/"), httpClient);
150-
toopherApi.authenticateByUserName(userName, extraTerminalName, actionName, extras);
140+
toopherApi.authenticateByUserName(userName, extraTerminalName, actionName, extras);
151141
assertEquals(httpClient.getLastCalledData("user_name"), userName);
152142
assertEquals(httpClient.getLastCalledData("terminal_name_extra"), extraTerminalName);
153143
assertEquals(httpClient.getLastCalledData("action_name"), actionName);
@@ -174,9 +164,11 @@ public void testGetAuthenticationStatusThrowRequestError() throws InterruptedExc
174164
HttpClientMock httpClient = new HttpClientMock(200, "{'id':'1'}");
175165
ToopherAPI toopherApi = new ToopherAPI("key", "secret",
176166
createURI("https://api.toopher.test/v1/"), httpClient);
177-
try{
167+
try {
178168
toopherApi.getAuthenticationStatus("1");
179-
} catch (RequestError re){}
169+
fail("My method didn't throw when I expected it to");
170+
} catch (RequestError re) {
171+
}
180172
}
181173

182174
@Test
@@ -200,22 +192,21 @@ public void testGetAuthenticationStatusWithOTPThrowRequestError() throws Interru
200192
HttpClientMock httpClient = new HttpClientMock(200, "{'id':'1'}");
201193
ToopherAPI toopherApi = new ToopherAPI("key", "secret",
202194
createURI("https://api.toopher.test/v1/"), httpClient);
203-
try{
195+
try {
204196
toopherApi.getAuthenticationStatusWithOTP("1", "ImAPassword");
205-
} catch (RequestError re){}
197+
} catch (RequestError re) {
198+
}
206199
}
207200

208201
@Test
209202
public void testAssignUserFriendlyNameToTerminal() throws InterruptedException, RequestError {
210203
HttpClientMock httpClient = new HttpClientMock(200, AUTH_REQUEST_JSON);
211204
ToopherAPI toopherApi = new ToopherAPI("key", "secret",
212205
createURI("https://api.toopher.test/v1/"), httpClient);
213-
String userName = "some user";
214-
String terminalName = "my computer";
215-
String extraTerminalName = "my extra computer";
216-
toopherApi.assignUserFriendlyNameToTerminal(userName, terminalName, extraTerminalName);
217-
assertEquals(httpClient.getLastCalledData("user_name"), userName);
218-
assertEquals(httpClient.getLastCalledData("name"), terminalName);
206+
final String extraTerminalName = "my extra computer";
207+
toopherApi.assignUserFriendlyNameToTerminal(USER_NAME, TERMINAL_NAME, extraTerminalName);
208+
assertEquals(httpClient.getLastCalledData("user_name"), USER_NAME);
209+
assertEquals(httpClient.getLastCalledData("name"), TERMINAL_NAME);
219210
assertEquals(httpClient.getLastCalledData("name_extra"), extraTerminalName);
220211
}
221212

@@ -247,7 +238,8 @@ public void testEnableToopherForUserWhenUserNameIsNotPresent() throws Interrupte
247238
try {
248239
toopherApi.setToopherEnabledForUser("1", true);
249240
fail();
250-
} catch (RequestError re) {}
241+
} catch (RequestError re) {
242+
}
251243
}
252244

253245
@Test
@@ -261,7 +253,8 @@ public void testEnableToopherForUserWithDuplicateUserName() throws InterruptedEx
261253
try {
262254
toopherApi.setToopherEnabledForUser("1", true);
263255
fail();
264-
} catch (RequestError re) {}
256+
} catch (RequestError re) {
257+
}
265258
}
266259

267260
@Test
@@ -279,7 +272,7 @@ public void testCreateQrPairing() throws InterruptedException, RequestError {
279272
assertTrue(pairing.enabled);
280273
}
281274

282-
public URI createURI(String url) {
275+
private URI createURI(String url) {
283276
try {
284277
return new URL(url).toURI();
285278
} catch (MalformedURLException e) {
@@ -305,7 +298,7 @@ public void testVersion() {
305298
}
306299

307300
@Test
308-
public void testAuthenticationStatusFactory() throws InterruptedException, URISyntaxException {
301+
public void testAuthenticationStatusFactoryBuildsValidJSON() throws InterruptedException, URISyntaxException {
309302
HttpClientMock clientMock = new HttpClientMock(200, "{}");
310303
AuthenticationStatusFactoryMock factoryMock = new AuthenticationStatusFactoryMock();
311304

@@ -317,7 +310,9 @@ public void testAuthenticationStatusFactory() throws InterruptedException, URISy
317310

318311
try {
319312
api.authenticate(PAIRING_ID, TERMINAL_NAME);
320-
} catch (RequestError re) { fail(); }
313+
} catch (RequestError re) {
314+
fail();
315+
}
321316
}
322317

323318
@Test
@@ -337,7 +332,8 @@ public void testParseRequestErrorUserDisabled() throws InterruptedException, URI
337332
try {
338333
toopherApi.authenticateByUserName(USER_NAME, TERMINAL_NAME, ACTION_NAME, null);
339334
fail();
340-
} catch(ToopherUserDisabledError toopherUserDisabledError){}
335+
} catch (ToopherUserDisabledError toopherUserDisabledError) {
336+
}
341337

342338
}
343339

@@ -358,7 +354,8 @@ public void testParseRequestErrorUnknownUser() throws InterruptedException, URIS
358354
try {
359355
toopherApi.authenticateByUserName(USER_NAME, TERMINAL_NAME, ACTION_NAME, null);
360356
fail();
361-
} catch(ToopherUnknownUserError toopherUnknownUserError){}
357+
} catch (ToopherUnknownUserError toopherUnknownUserError) {
358+
}
362359

363360
}
364361

@@ -379,7 +376,8 @@ public void testParseRequestErrorUnknownTerminal() throws InterruptedException,
379376
try {
380377
toopherApi.authenticateByUserName(USER_NAME, TERMINAL_NAME, ACTION_NAME, null);
381378
fail();
382-
} catch(ToopherUnknownTerminalError toopherUnknownTerminalError){}
379+
} catch (ToopherUnknownTerminalError toopherUnknownTerminalError) {
380+
}
383381

384382
}
385383

@@ -400,7 +398,8 @@ public void testParseRequestErrorClient() throws InterruptedException, URISyntax
400398
try {
401399
toopherApi.authenticateByUserName(USER_NAME, TERMINAL_NAME, ACTION_NAME, null);
402400
fail();
403-
} catch(ToopherClientError toopherClientError){}
401+
} catch (ToopherClientError toopherClientError) {
402+
}
404403

405404
}
406405

@@ -421,7 +420,8 @@ public void testParseRequestErrorNotJSON() throws InterruptedException, URISynta
421420
try {
422421
toopherApi.authenticateByUserName(USER_NAME, TERMINAL_NAME, ACTION_NAME, null);
423422
fail();
424-
} catch(RequestError re){}
423+
} catch (RequestError re) {
424+
}
425425

426426
}
427427

0 commit comments

Comments
 (0)