Skip to content

Commit 8371998

Browse files
Added Extra Asserts To Verify Response Codes
We're actually checking the response codes now.
1 parent ad39e30 commit 8371998

File tree

5 files changed

+123
-43
lines changed

5 files changed

+123
-43
lines changed

src/test/java/com/bandwidth/MessagingApiTests.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.bandwidth;
22

3+
import com.bandwidth.http.response.ApiResponse;
34
import com.bandwidth.messaging.controllers.APIController;
45

56
import com.bandwidth.messaging.models.MessageRequest;
@@ -45,14 +46,16 @@ public void testCreateMessage() throws Exception {
4546
.applicationId(MESSAGING_APPLICATION_ID)
4647
.build();
4748

48-
BandwidthMessage response = controller.createMessage(ACCOUNT_ID, body).getResult();
49+
ApiResponse<BandwidthMessage> apiResponse = controller.createMessage(ACCOUNT_ID, body);
50+
assertEquals("Response Code is not 202", 202, apiResponse.getStatusCode());
51+
BandwidthMessage response = apiResponse.getResult();
4952
assertEquals("Application ID not equal", MESSAGING_APPLICATION_ID, response.getApplicationId());
5053
assertEquals("To phone number not equal", USER_NUMBER, response.getTo().get(0));
5154
assertEquals("From phone number not equal", BW_NUMBER, response.getFrom());
5255
assertEquals("Text not equal", text, response.getText());
5356
}
5457

55-
@Test(expected = MessagingException.class)
58+
@Test
5659
public void testCreateMessageInvalidPhoneNumber() throws Exception {
5760
MessageRequest body = new MessageRequest.Builder()
5861
.to(Collections.singletonList("+1invalid"))
@@ -61,7 +64,12 @@ public void testCreateMessageInvalidPhoneNumber() throws Exception {
6164
.applicationId(MESSAGING_APPLICATION_ID)
6265
.build();
6366

64-
controller.createMessage(ACCOUNT_ID, body);
67+
MessagingException e = assertThrows(
68+
"Messaging Exception not thrown",
69+
MessagingException.class,
70+
() -> controller.createMessage(ACCOUNT_ID, body)
71+
);
72+
assertEquals("Response Code is not 400", 400, e.getResponseCode());
6573
}
6674

6775
@Test
@@ -75,19 +83,24 @@ public void testUploadDownloadDeleteMedia() throws Exception {
7583

7684
final String mediaId = "java-media-test";
7785

78-
controller.uploadMedia(ACCOUNT_ID, mediaId, body, contentType, "no-cache");
86+
ApiResponse<Void> uploadMediaApiResponse = controller.uploadMedia(ACCOUNT_ID, mediaId, body, contentType, "no-cache");
87+
assertEquals("Response Code is not 204", 204, uploadMediaApiResponse.getStatusCode());
88+
89+
ApiResponse<InputStream> downloadMediaApiResponse = controller.getMedia(ACCOUNT_ID, mediaId);
90+
assertEquals("Response Code is not 200", 200, downloadMediaApiResponse.getStatusCode());
7991

80-
InputStream response = controller.getMedia(ACCOUNT_ID, mediaId).getResult();
92+
InputStream downloadMediaResponse = downloadMediaApiResponse.getResult();
8193

8294
int bRead;
8395
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
84-
while ((bRead = response.read()) != -1){
96+
while ((bRead = downloadMediaResponse.read()) != -1){
8597
byteArrayOutputStream.write(bRead);
8698
}
8799
byte[] responseContents = byteArrayOutputStream.toByteArray();
88100

89101
assertArrayEquals("Media download not equal to media upload", fileContents, responseContents);
90102

91-
controller.deleteMedia(ACCOUNT_ID, mediaId);
103+
ApiResponse<Void> deleteMediaApiResponse = controller.deleteMedia(ACCOUNT_ID, mediaId);
104+
assertEquals("Response Code is not 200", 200, deleteMediaApiResponse.getStatusCode());
92105
}
93106
}

src/test/java/com/bandwidth/MfaApiTests.java

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.bandwidth;
22

3+
import com.bandwidth.http.response.ApiResponse;
34
import com.bandwidth.multifactorauth.controllers.MFAController;
45

56
import com.bandwidth.multifactorauth.exceptions.ErrorWithRequestException;
@@ -37,7 +38,10 @@ public void testMfaMessaging() throws Exception {
3738
.message("Your temporary {NAME} {SCOPE} code is {CODE}")
3839
.build();
3940

40-
TwoFactorMessagingResponse response = controller.createMessagingTwoFactor(ACCOUNT_ID, body).getResult();
41+
ApiResponse<TwoFactorMessagingResponse> apiResponse = controller.createMessagingTwoFactor(ACCOUNT_ID, body);
42+
assertEquals("Response Code is not 200", 200, apiResponse.getStatusCode());
43+
44+
TwoFactorMessagingResponse response = apiResponse.getResult();
4145
assertNotNull("MessageID is null", response.getMessageId());
4246
assertFalse("MessageID is empty", response.getMessageId().isEmpty());
4347
}
@@ -53,12 +57,15 @@ public void testMfaVoice() throws Exception {
5357
.message("Your temporary {NAME} {SCOPE} code is {CODE}")
5458
.build();
5559

56-
TwoFactorVoiceResponse response = controller.createVoiceTwoFactor(ACCOUNT_ID, body).getResult();
60+
ApiResponse<TwoFactorVoiceResponse> apiResponse = controller.createVoiceTwoFactor(ACCOUNT_ID, body);
61+
assertEquals("Response Code is not 200", 200, apiResponse.getStatusCode());
62+
63+
TwoFactorVoiceResponse response = apiResponse.getResult();
5764
assertNotNull("CallID is null", response.getCallId());
5865
assertFalse("CallID is empty", response.getCallId().isEmpty());
5966
}
6067

61-
@Test(expected = ErrorWithRequestException.class)
68+
@Test
6269
public void testMfaMessagingInvalidPhoneNumber() throws Exception {
6370
TwoFactorCodeRequestSchema body = new TwoFactorCodeRequestSchema.Builder()
6471
.to("+1invalid")
@@ -69,10 +76,15 @@ public void testMfaMessagingInvalidPhoneNumber() throws Exception {
6976
.message("Your temporary {NAME} {SCOPE} code is {CODE}")
7077
.build();
7178

72-
controller.createMessagingTwoFactor(ACCOUNT_ID, body);
79+
ErrorWithRequestException e = assertThrows(
80+
"ErrorWithRequest Exception not thrown",
81+
ErrorWithRequestException.class,
82+
()->controller.createMessagingTwoFactor(ACCOUNT_ID, body)
83+
);
84+
assertEquals("Response Code is not 400", 400, e.getResponseCode());
7385
}
7486

75-
@Test(expected = ErrorWithRequestException.class)
87+
@Test
7688
public void testMfaVoiceInvalidPhoneNumber() throws Exception {
7789
TwoFactorCodeRequestSchema body = new TwoFactorCodeRequestSchema.Builder()
7890
.to("+1invalid")
@@ -83,7 +95,12 @@ public void testMfaVoiceInvalidPhoneNumber() throws Exception {
8395
.message("Your temporary {NAME} {SCOPE} code is {CODE}")
8496
.build();
8597

86-
controller.createVoiceTwoFactor(ACCOUNT_ID, body);
98+
ErrorWithRequestException e = assertThrows(
99+
"ErrorWithRequest Exception not thrown",
100+
ErrorWithRequestException.class,
101+
()->controller.createVoiceTwoFactor(ACCOUNT_ID, body)
102+
);
103+
assertEquals("Response Code not 400", 400, e.getResponseCode());
87104
}
88105

89106
@Test
@@ -99,11 +116,14 @@ public void testMfaVerify() throws Exception {
99116
.expirationTimeInMinutes(3)
100117
.build();
101118

102-
TwoFactorVerifyCodeResponse response = controller.createVerifyTwoFactor(ACCOUNT_ID, body).getResult();
119+
ApiResponse<TwoFactorVerifyCodeResponse> apiResponse = controller.createVerifyTwoFactor(ACCOUNT_ID, body);
120+
assertEquals("Response Code is not 200", 200, apiResponse.getStatusCode());
121+
122+
TwoFactorVerifyCodeResponse response = apiResponse.getResult();
103123
assertFalse("Code should be invalid", response.getValid());
104124
}
105125

106-
@Test(expected = ErrorWithRequestException.class)
126+
@Test
107127
public void testMfaVerifyInvalidPhoneNumber() throws Exception {
108128
TwoFactorVerifyRequestSchema body = new TwoFactorVerifyRequestSchema.Builder()
109129
.to("+1invalid")
@@ -113,6 +133,11 @@ public void testMfaVerifyInvalidPhoneNumber() throws Exception {
113133
.expirationTimeInMinutes(3)
114134
.build();
115135

116-
controller.createVerifyTwoFactor(ACCOUNT_ID, body);
136+
ErrorWithRequestException e = assertThrows(
137+
"ErrorWithRequest Exception not thrown",
138+
ErrorWithRequestException.class,
139+
()->controller.createVerifyTwoFactor(ACCOUNT_ID, body)
140+
);
141+
assertEquals("Response Code not 400", 400, e.getResponseCode());
117142
}
118143
}

src/test/java/com/bandwidth/TnLookupApiTests.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.bandwidth;
22

3+
import com.bandwidth.http.response.ApiResponse;
34
import com.bandwidth.phonenumberlookup.controllers.APIController;
45

56
import com.bandwidth.phonenumberlookup.models.OrderRequest;
@@ -32,7 +33,10 @@ public void testPhoneNumberLookup() throws Exception {
3233
.tns(Collections.singletonList(USER_NUMBER))
3334
.build();
3435

35-
OrderResponse orderResponse = controller.createLookupRequest(ACCOUNT_ID, body).getResult();
36+
ApiResponse<OrderResponse> orderApiResponse = controller.createLookupRequest(ACCOUNT_ID, body);
37+
assertEquals("Response Code is not 202", 202, orderApiResponse.getStatusCode());
38+
39+
OrderResponse orderResponse = orderApiResponse.getResult();
3640
assertNotNull("RequestID is null", orderResponse.getRequestId());
3741
assertFalse("RequestID is empty", orderResponse.getRequestId().isEmpty());
3842
assertTrue(
@@ -42,7 +46,10 @@ public void testPhoneNumberLookup() throws Exception {
4246
orderResponse.getStatus().equals("FAILED")
4347
);
4448

45-
OrderStatus statusResponse = controller.getLookupRequestStatus(ACCOUNT_ID, orderResponse.getRequestId()).getResult();
49+
ApiResponse<OrderStatus> statusApiResponse = controller.getLookupRequestStatus(ACCOUNT_ID, orderResponse.getRequestId());
50+
assertEquals("Response Code is not 200", 200, statusApiResponse.getStatusCode());
51+
52+
OrderStatus statusResponse = statusApiResponse.getResult();
4653
assertNotNull("RequestID is null", statusResponse.getRequestId());
4754
assertFalse("RequestId is empty", statusResponse.getRequestId().isEmpty());
4855
assertTrue(

src/test/java/com/bandwidth/VoiceApiTests.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.bandwidth;
22

3+
import com.bandwidth.http.response.ApiResponse;
34
import com.bandwidth.voice.controllers.APIController;
45

56
import com.bandwidth.voice.exceptions.ApiErrorException;
@@ -36,18 +37,23 @@ public void testCreateCallAndGetCallState() throws Exception {
3637
.answerUrl(answerUrl)
3738
.build();
3839

39-
CreateCallResponse createCallResponse = controller.createCall(ACCOUNT_ID, body).getResult();
40+
ApiResponse<CreateCallResponse> createCallApiResponse = controller.createCall(ACCOUNT_ID, body);
41+
assertEquals("Response Code is not 201", 201, createCallApiResponse.getStatusCode());
42+
43+
CreateCallResponse createCallResponse = createCallApiResponse.getResult();
4044
assertEquals("Application ID for create call not equal", VOICE_APPLICATION_ID, createCallResponse.getApplicationId());
4145
assertEquals("To phone number for create call not equal", USER_NUMBER, createCallResponse.getTo());
4246
assertEquals("From phone number for create call not equal", BW_NUMBER, createCallResponse.getFrom());
4347

4448
//get call state
45-
String callId = createCallResponse.getCallId();
46-
CallState callStateResponse = controller.getCall(ACCOUNT_ID, callId).getResult();
49+
ApiResponse<CallState> callStateApiResponse = controller.getCall(ACCOUNT_ID, createCallResponse.getCallId());
50+
assertEquals("Response Code is not 200", 200, callStateApiResponse.getStatusCode());
51+
52+
CallState callStateResponse = callStateApiResponse.getResult();
4753
assertEquals("Application ID for call state not equal", VOICE_APPLICATION_ID, callStateResponse.getApplicationId());
4854
assertEquals("To phone number for call state not equal", USER_NUMBER, callStateResponse.getTo());
4955
assertEquals("From phone number for call state not equal", BW_NUMBER, callStateResponse.getFrom());
50-
assertEquals("Call ID not equal", callId, callStateResponse.getCallId());
56+
assertEquals("Call ID not equal", createCallResponse.getCallId(), callStateResponse.getCallId());
5157
}
5258

5359
@Test
@@ -74,20 +80,24 @@ public void testCreateCallWithAmdAndGetCallState() throws Exception {
7480
.machineDetection(machineDetectionConfiguration)
7581
.build();
7682

77-
CreateCallResponse createCallResponse = controller.createCall(ACCOUNT_ID, body).getResult();
83+
ApiResponse<CreateCallResponse> createCallApiResponse = controller.createCall(ACCOUNT_ID, body);
84+
assertEquals("Response Code is not 201", 201, createCallApiResponse.getStatusCode());
85+
86+
CreateCallResponse createCallResponse = createCallApiResponse.getResult();
7887
assertEquals("Application ID for create call not equal", VOICE_APPLICATION_ID, createCallResponse.getApplicationId());
7988
assertEquals("To phone number for create call not equal", USER_NUMBER, createCallResponse.getTo());
8089
assertEquals("From phone number for create call not equal", BW_NUMBER, createCallResponse.getFrom());
8190

8291
//get call state
83-
CallState callStateResponse = controller.getCall(ACCOUNT_ID, createCallResponse.getCallId()).getResult();
92+
ApiResponse<CallState> callStateApiResponse = controller.getCall(ACCOUNT_ID, createCallResponse.getCallId());
93+
CallState callStateResponse = callStateApiResponse.getResult();
8494
assertEquals("Application ID for call state not equal", VOICE_APPLICATION_ID, callStateResponse.getApplicationId());
8595
assertEquals("To phone number for call state not equal", USER_NUMBER, callStateResponse.getTo());
8696
assertEquals("From phone number for call state not equal", BW_NUMBER, callStateResponse.getFrom());
8797
assertEquals("Call ID not equal", createCallResponse.getCallId(), callStateResponse.getCallId());
8898
}
8999

90-
@Test(expected = ApiErrorException.class)
100+
@Test
91101
public void testCreateCallInvalidPhoneNumber() throws Exception {
92102
final String answerUrl = BASE_CALLBACK_URL.concat("/callbacks/outbound");
93103

@@ -98,6 +108,11 @@ public void testCreateCallInvalidPhoneNumber() throws Exception {
98108
.answerUrl(answerUrl)
99109
.build();
100110

101-
controller.createCall(ACCOUNT_ID, body);
111+
ApiErrorException e = assertThrows(
112+
"ApiError Exception not thrown",
113+
ApiErrorException.class,
114+
()->controller.createCall(ACCOUNT_ID, body)
115+
);
116+
assertEquals("Response Code is not 400", 400, e.getResponseCode());
102117
}
103118
}

0 commit comments

Comments
 (0)