Skip to content

Commit 74731e0

Browse files
Refactored Tests
Moved tests for each API into their own files. Created a utility class to hold environment variables constants. Streamlined code where possible.
1 parent c133b7f commit 74731e0

File tree

8 files changed

+453
-67
lines changed

8 files changed

+453
-67
lines changed

src/test/java/com/bandwidth/ApiTest.java

Lines changed: 66 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
package com.bandwidth;
22

33
import com.bandwidth.voice.controllers.APIController;
4-
import org.junit.BeforeClass;
54
import org.junit.Test;
65
import org.junit.Before;
76

87
import static org.junit.Assert.*;
98

9+
import java.io.*;
10+
import java.nio.file.Files;
1011
import java.util.ArrayList;
11-
import java.io.File;
12-
import java.io.FileInputStream;
13-
import java.io.InputStream;
14-
import java.io.InputStreamReader;
15-
import java.io.IOException;
16-
import java.io.Reader;
1712

1813
import com.bandwidth.*;
1914
import com.bandwidth.Environment;
@@ -34,26 +29,22 @@
3429
import com.bandwidth.phonenumberlookup.models.*;
3530
import com.bandwidth.phonenumberlookup.controllers.*;
3631

32+
import static com.bandwidth.TestingEnvironmentVariables.*;
33+
3734
/**
3835
* Integration tests for API interactions
36+
* ---
37+
* In Progress: Breaking out into separate test classes for each API
3938
*/
4039
public class ApiTest {
4140

42-
private static final String USERNAME = System.getenv("BW_USERNAME");
43-
private static final String PASSWORD = System.getenv("BW_PASSWORD");
44-
private static final String ACCOUNT_ID = System.getenv("BW_ACCOUNT_ID");
45-
private static final String USER_NUMBER = System.getenv("USER_NUMBER");
46-
private static final String BW_NUMBER = System.getenv("BW_NUMBER");
47-
private static final String MESSAGING_APPLICATION_ID = System.getenv("BW_MESSAGING_APPLICATION_ID");
48-
private static final String VOICE_APPLICATION_ID = System.getenv("BW_VOICE_APPLICATION_ID");
49-
private static final String BASE_CALLBACK_URL = System.getenv("BASE_CALLBACK_URL");
50-
5141
private com.bandwidth.messaging.controllers.APIController messagingController;
5242
private com.bandwidth.voice.controllers.APIController voiceController;
5343
private com.bandwidth.multifactorauth.controllers.MFAController mfaController;
5444
private com.bandwidth.webrtc.controllers.APIController webrtcController;
5545
private com.bandwidth.phonenumberlookup.controllers.APIController phoneNumberLookupController;
5646

47+
/*
5748
@Before
5849
public void initTest() {
5950
BandwidthClient client = new BandwidthClient.Builder()
@@ -69,7 +60,9 @@ public void initTest() {
6960
this.webrtcController = client.getWebRtcClient().getAPIController();
7061
this.phoneNumberLookupController = client.getPhoneNumberLookupClient().getAPIController();
7162
}
63+
*/
7264

65+
/* Moved to MessagingApiTests.java
7366
@Test
7467
public void testCreateMessage() throws Exception {
7568
ArrayList<String> toNumbers = new ArrayList<>();
@@ -89,7 +82,9 @@ public void testCreateMessage() throws Exception {
8982
assertEquals("From phone number not equal", BW_NUMBER, response.getResult().getFrom());
9083
assertEquals("Text not equal", text, response.getResult().getText());
9184
}
85+
*/
9286

87+
/* Moved to MessagingApiTests.java
9388
@Test(expected = MessagingException.class)
9489
public void testCreateMessageInvalidPhoneNumber() throws Exception {
9590
ArrayList<String> toNumbers = new ArrayList<>();
@@ -103,26 +98,35 @@ public void testCreateMessageInvalidPhoneNumber() throws Exception {
10398
10499
messagingController.createMessage(ACCOUNT_ID, body);
105100
}
101+
*/
106102

103+
/* Moved to MessagingApiTests.java
107104
@Test
108105
public void testUploadDownloadMedia() throws Exception {
109106
final String fileName = "src/test/resources/mediaUpload.png";
110-
File file = new File(fileName);
111-
FileInputStream inStream = new FileInputStream(fileName);
112-
String fileContents = ApiTest.convertInputStreamToString(inStream);
113-
FileWrapper body = new FileWrapper(file, "image/png");
107+
final String contentType = "image/png";
108+
final File file = new File(fileName);
109+
final byte[] fileContents = Files.readAllBytes(file.toPath());
110+
FileWrapper body = new FileWrapper(file, contentType);
114111
String mediaId = "java-media-test";
115-
String fileType = "image/png";
116-
String cache = "no-cache";
117112
118-
messagingController.uploadMedia(ACCOUNT_ID, mediaId, body, fileType, cache);
113+
messagingController.uploadMedia(ACCOUNT_ID, mediaId, body, contentType, "no-cache");
119114
120115
ApiResponse<InputStream> response = messagingController.getMedia(ACCOUNT_ID, mediaId);
121-
String resultString = ApiTest.convertInputStreamToString(response.getResult());
116+
InputStream responseBody = response.getResult();
122117
123-
assertEquals("Media download not equal to media upload", fileContents, resultString);
118+
int bRead;
119+
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
120+
while ((bRead = responseBody.read()) != -1){
121+
byteArrayOutputStream.write(bRead);
122+
}
123+
byte[] responseContents = byteArrayOutputStream.toByteArray();
124+
125+
assertArrayEquals("Media download not equal to media upload", fileContents, responseContents);
124126
}
127+
*/
125128

129+
/* Moved to VoiceApiTests.java
126130
@Test
127131
public void testCreateCallAndGetCallState() throws Exception {
128132
String answerUrl = BASE_CALLBACK_URL.concat("/callbacks/outbound");
@@ -146,15 +150,13 @@ public void testCreateCallAndGetCallState() throws Exception {
146150
assertEquals("From phone number for call state not equal", BW_NUMBER, callStateResponse.getResult().getFrom());
147151
assertEquals("Call ID not equal", callId, callStateResponse.getResult().getCallId());
148152
}
153+
*/
149154

155+
/* Moved to VoiceApiTests.java
150156
@Test
151157
public void testCreateCallWithAmdAndGetCallState() throws Exception {
152-
String accountId = System.getenv("BW_ACCOUNT_ID");
153-
String to = System.getenv("USER_NUMBER");
154-
String from = System.getenv("BW_NUMBER");
155-
String applicationId = System.getenv("BW_VOICE_APPLICATION_ID");
156-
String answerUrl = System.getenv("BASE_CALLBACK_URL").concat("/callbacks/outbound");
157-
String machineDetectionUrl = System.getenv("BASE_CALLBACK_URL").concat("/callbacks/machineDetection");
158+
final String answerUrl = BASE_CALLBACK_URL.concat("/callbacks/outbound");
159+
final String machineDetectionUrl = BASE_CALLBACK_URL.concat("/callbacks/machineDetection");
158160
159161
MachineDetectionRequest machineDetection = new MachineDetectionRequest();
160162
machineDetection.setMode(ModeEnum.ASYNC);
@@ -164,29 +166,31 @@ public void testCreateCallWithAmdAndGetCallState() throws Exception {
164166
machineDetection.setSilenceTimeout(5.0);
165167
machineDetection.setSpeechThreshold(5.0);
166168
machineDetection.setSpeechEndThreshold(5.0);
167-
machineDetection.setDelayResult(Boolean.TRUE);
169+
machineDetection.setDelayResult(true);
168170
169171
CreateCallRequest body = new CreateCallRequest();
170-
body.setTo(to);
171-
body.setFrom(from);
172-
body.setApplicationId(applicationId);
172+
body.setTo(USER_NUMBER);
173+
body.setFrom(BW_NUMBER);
174+
body.setApplicationId(VOICE_APPLICATION_ID);
173175
body.setAnswerUrl(answerUrl);
174176
body.setMachineDetection(machineDetection);
175177
176-
ApiResponse<CreateCallResponse> createCallResponse = voiceController.createCall(accountId, body);
177-
assertEquals("Application ID for create call not equal", applicationId, createCallResponse.getResult().getApplicationId());
178-
assertEquals("To phone number for create call not equal", to, createCallResponse.getResult().getTo());
179-
assertEquals("From phone number for create call not equal", from, createCallResponse.getResult().getFrom());
178+
ApiResponse<CreateCallResponse> createCallResponse = voiceController.createCall(ACCOUNT_ID, body);
179+
assertEquals("Application ID for create call not equal", VOICE_APPLICATION_ID, createCallResponse.getResult().getApplicationId());
180+
assertEquals("To phone number for create call not equal", USER_NUMBER, createCallResponse.getResult().getTo());
181+
assertEquals("From phone number for create call not equal", BW_NUMBER, createCallResponse.getResult().getFrom());
180182
181183
//get call state
182184
String callId = createCallResponse.getResult().getCallId();
183-
ApiResponse<CallState> callStateResponse = voiceController.getCall(accountId, callId);
184-
assertEquals("Application ID for call state not equal", applicationId, callStateResponse.getResult().getApplicationId());
185-
assertEquals("To phone number for call state not equal", to, callStateResponse.getResult().getTo());
186-
assertEquals("From phone number for call state not equal", from, callStateResponse.getResult().getFrom());
185+
ApiResponse<CallState> callStateResponse = voiceController.getCall(ACCOUNT_ID, callId);
186+
assertEquals("Application ID for call state not equal", VOICE_APPLICATION_ID, callStateResponse.getResult().getApplicationId());
187+
assertEquals("To phone number for call state not equal", USER_NUMBER, callStateResponse.getResult().getTo());
188+
assertEquals("From phone number for call state not equal", BW_NUMBER, callStateResponse.getResult().getFrom());
187189
assertEquals("Call ID not equal", callId, callStateResponse.getResult().getCallId());
188190
}
191+
*/
189192

193+
/* Moved to VoiceApi.java
190194
@Test(expected = ApiErrorException.class)
191195
public void testCreateCallInvalidPhoneNumber() throws Exception {
192196
final String answerUrl = BASE_CALLBACK_URL.concat("/callbacks/outbound");
@@ -199,7 +203,9 @@ public void testCreateCallInvalidPhoneNumber() throws Exception {
199203
200204
voiceController.createCall(ACCOUNT_ID, body);
201205
}
206+
*/
202207

208+
/* Moved to MfaApiTests.java
203209
@Test
204210
public void testMfaMessaging() throws Exception {
205211
TwoFactorCodeRequestSchema body = new TwoFactorCodeRequestSchema();
@@ -211,9 +217,10 @@ public void testMfaMessaging() throws Exception {
211217
body.setMessage("Your temporary {NAME} {SCOPE} code is {CODE}");
212218
213219
ApiResponse<TwoFactorMessagingResponse> response = mfaController.createMessagingTwoFactor(ACCOUNT_ID, body);
214-
assertTrue("Message ID not defined", response.getResult().getMessageId().length() > 0);
215220
}
221+
*/
216222

223+
/* Moved to MfaApiTests.java
217224
@Test
218225
public void testMfaVoice() throws Exception {
219226
TwoFactorCodeRequestSchema body = new TwoFactorCodeRequestSchema();
@@ -225,9 +232,10 @@ public void testMfaVoice() throws Exception {
225232
body.setMessage("Your temporary {NAME} {SCOPE} code is {CODE}");
226233
227234
ApiResponse<TwoFactorVoiceResponse> response = mfaController.createVoiceTwoFactor(ACCOUNT_ID, body);
228-
assertTrue("Call ID not defined", response.getResult().getCallId().length() > 0);
229235
}
236+
*/
230237

238+
/* Moved to MfaTests.java
231239
@Test(expected = ErrorWithRequestException.class)
232240
public void testMfaMessagingInvalidPhoneNumber() throws Exception {
233241
TwoFactorCodeRequestSchema body = new TwoFactorCodeRequestSchema();
@@ -240,7 +248,9 @@ public void testMfaMessagingInvalidPhoneNumber() throws Exception {
240248
241249
mfaController.createMessagingTwoFactor(ACCOUNT_ID, body);
242250
}
251+
*/
243252

253+
/* Moved to MfaApiTests.java
244254
@Test(expected = ErrorWithRequestException.class)
245255
public void testMfaVoiceInvalidPhoneNumber() throws Exception {
246256
TwoFactorCodeRequestSchema body = new TwoFactorCodeRequestSchema();
@@ -253,21 +263,24 @@ public void testMfaVoiceInvalidPhoneNumber() throws Exception {
253263
254264
mfaController.createVoiceTwoFactor(ACCOUNT_ID, body);
255265
}
266+
*/
256267

268+
/* Moved to MfaApiTests.java
257269
@Test
258270
public void testMfaVerify() throws Exception {
259271
260272
TwoFactorVerifyRequestSchema body = new TwoFactorVerifyRequestSchema();
261273
body.setTo(USER_NUMBER);
262274
body.setApplicationId(VOICE_APPLICATION_ID);
263275
body.setScope("scope");
264-
body.setCode("123456");
276+
body.setCode("1234567");
265277
body.setExpirationTimeInMinutes(3);
266278
267279
ApiResponse<TwoFactorVerifyCodeResponse> response = mfaController.createVerifyTwoFactor(ACCOUNT_ID, body);
268-
assertTrue("Valid not defined as a boolean", response.getResult().getValid() == true || response.getResult().getValid() == false);
269280
}
281+
*/
270282

283+
/* Moved to MfaApiTests.java
271284
@Test(expected = ErrorWithRequestException.class)
272285
public void testMfaVerifyInvalidPhoneNumber() throws Exception {
273286
TwoFactorVerifyRequestSchema body = new TwoFactorVerifyRequestSchema();
@@ -279,7 +292,9 @@ public void testMfaVerifyInvalidPhoneNumber() throws Exception {
279292
280293
mfaController.createVerifyTwoFactor(ACCOUNT_ID, body);
281294
}
295+
*/
282296

297+
/* Moved to WebRtcApiTests.java
283298
@Test
284299
public void testWebRtcParticipantSessionManagement() throws Exception {
285300
@@ -290,14 +305,16 @@ public void testWebRtcParticipantSessionManagement() throws Exception {
290305
String sessionId = createSessionResponse.getResult().getId();
291306
292307
Participant createParticipantBody = new Participant();
293-
createParticipantBody.setCallbackUrl("https://sample.com");
308+
createParticipantBody.setCallbackUrl(BASE_CALLBACK_URL.concat("/callbacks/webRtc"));
294309
295310
ApiResponse<AccountsParticipantsResponse> createParticipantResponse = webrtcController.createParticipant(ACCOUNT_ID, createParticipantBody);
296311
String participantId = createParticipantResponse.getResult().getParticipant().getId();
297312
298313
webrtcController.addParticipantToSession(ACCOUNT_ID, sessionId, participantId, null);
299314
}
315+
*/
300316

317+
/* Moved to TnLookupApiTests.java
301318
@Test
302319
public void testPhoneNumberLookup() throws Exception {
303320
ArrayList<String> checkNumbers = new ArrayList<>();
@@ -315,22 +332,5 @@ public void testPhoneNumberLookup() throws Exception {
315332
316333
assertTrue("status not defined properly", status.length() > 0);
317334
}
318-
319-
/*
320-
* Taken from https://mkyong.com/java/how-to-convert-inputstream-to-string-in-java/
321-
*/
322-
private static String convertInputStreamToString(InputStream inputStream) throws IOException {
323-
final char[] buffer = new char[8192];
324-
final StringBuilder result = new StringBuilder();
325-
326-
// InputStream -> Reader
327-
try (Reader reader = new InputStreamReader(inputStream)) {
328-
int charsRead;
329-
while ((charsRead = reader.read(buffer, 0, buffer.length)) > 0) {
330-
result.append(buffer, 0, charsRead);
331-
}
332-
}
333-
334-
return result.toString();
335-
}
335+
*/
336336
}

src/test/java/com/bandwidth/BxmlTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
package com.bandwidth.voice.bxml.verbs;
1+
package com.bandwidth;
2+
3+
import com.bandwidth.voice.bxml.verbs.*;
4+
import com.bandwidth.voice.bxml.verbs.Record;
25
import com.bandwidth.webrtc.utils.WebRtcTransfer;
36

47
import org.junit.Test;

0 commit comments

Comments
 (0)