Skip to content

Commit

Permalink
Added 404 exception to voice tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brianluisgomez committed Jun 19, 2023
1 parent a8592c8 commit f29b3e2
Showing 1 changed file with 155 additions and 149 deletions.
304 changes: 155 additions & 149 deletions src/test/java/com/bandwidth/VoiceApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.bandwidth.voice.controllers.APIController;

import com.bandwidth.voice.exceptions.ApiErrorException;
import com.bandwidth.exceptions.ApiException;
import com.bandwidth.voice.models.*;
import org.junit.*;

Expand All @@ -20,153 +21,158 @@
*/
public class VoiceApiTest {

private APIController controller;

@Before
public void initTest() {
BandwidthClient client = new BandwidthClient.Builder()
.voiceBasicAuthCredentials(USERNAME, PASSWORD)
.build();

controller = client.getVoiceClient().getAPIController();
}

@Test
public void testCreateCallAndGetCallState() throws Exception {
final String answerUrl = BASE_CALLBACK_URL.concat("/callbacks/outbound");

CreateCallRequest body = new CreateCallRequest.Builder()
.to(USER_NUMBER)
.from(BW_NUMBER)
.applicationId(VOICE_APPLICATION_ID)
.answerUrl(answerUrl)
.build();

ApiResponse<CreateCallResponse> createCallApiResponse = controller.createCall(ACCOUNT_ID, body);

assertEquals("Response Code is not 201", 201, createCallApiResponse.getStatusCode());

CreateCallResponse createCallResponse = createCallApiResponse.getResult();
assertNotNull("Call ID is null", createCallResponse.getCallId());
assertFalse("Call ID is empty", createCallResponse.getCallId().isEmpty());
assertEquals("Call ID is not 47 characters", 47, createCallResponse.getCallId().length());
assertEquals("Application ID for create call not equal", VOICE_APPLICATION_ID,
createCallResponse.getApplicationId());
assertEquals("To phone number for create call not equal", USER_NUMBER, createCallResponse.getTo());
assertEquals("From phone number for create call not equal", BW_NUMBER, createCallResponse.getFrom());
assertNotNull("enqueuedTime is null", createCallResponse.getEnqueuedTime());
assertEquals("enqueuedTime is not a LocalDateTime object", LocalDateTime.class,
createCallResponse.getEnqueuedTime().getClass());

// get call state
Thread.sleep(15000); // Wait to get Call because of current system latency issues
ApiResponse<CallState> callStateApiResponse = controller.getCall(ACCOUNT_ID,
createCallResponse.getCallId());
assertEquals("Response Code is not 200", 200, callStateApiResponse.getStatusCode());

CallState callStateResponse = callStateApiResponse.getResult();
assertEquals("Application ID for call state not equal", VOICE_APPLICATION_ID,
callStateResponse.getApplicationId());
assertEquals("To phone number for call state not equal", USER_NUMBER, callStateResponse.getTo());
assertEquals("From phone number for call state not equal", BW_NUMBER, callStateResponse.getFrom());
assertEquals("Call ID not equal", createCallResponse.getCallId(), callStateResponse.getCallId());
assertNotNull("enqueuedTime is null", createCallResponse.getEnqueuedTime());
assertEquals("enqueuedTime is not a LocalDateTime object", LocalDateTime.class,
createCallResponse.getEnqueuedTime().getClass());
}

@Test
public void testCreateCallWithAmdAndGetCallState() throws Exception {
final String answerUrl = BASE_CALLBACK_URL.concat("/callbacks/outbound");
final String machineDetectionUrl = BASE_CALLBACK_URL.concat("/callbacks/machineDetection");

MachineDetectionConfiguration machineDetectionConfiguration = new MachineDetectionConfiguration.Builder()
.mode(ModeEnum.ASYNC)
.callbackUrl(machineDetectionUrl)
.callbackMethod(CallbackMethodEnum.POST)
.detectionTimeout(5.0)
.silenceTimeout(5.0)
.speechThreshold(5.0)
.speechEndThreshold(5.0)
.delayResult(true)
.build();

CreateCallRequest body = new CreateCallRequest.Builder()
.to(USER_NUMBER)
.from(BW_NUMBER)
.applicationId(VOICE_APPLICATION_ID)
.answerUrl(answerUrl)
.machineDetection(machineDetectionConfiguration)
.build();

ApiResponse<CreateCallResponse> createCallApiResponse = controller.createCall(ACCOUNT_ID, body);
assertEquals("Response Code is not 201", 201, createCallApiResponse.getStatusCode());

CreateCallResponse createCallResponse = createCallApiResponse.getResult();
assertNotNull("Call ID is null", createCallResponse.getCallId());
assertFalse("Call ID is empty", createCallResponse.getCallId().isEmpty());
assertEquals("Call ID is not 47 characters", 47, createCallResponse.getCallId().length());
assertEquals("Application ID for create call not equal", VOICE_APPLICATION_ID,
createCallResponse.getApplicationId());
assertEquals("To phone number for create call not equal", USER_NUMBER, createCallResponse.getTo());
assertEquals("From phone number for create call not equal", BW_NUMBER, createCallResponse.getFrom());

// get call state
Thread.sleep(15000); // Wait to get Call because of current system latency issues
ApiResponse<CallState> callStateApiResponse = controller.getCall(ACCOUNT_ID,
createCallResponse.getCallId());
CallState callStateResponse = callStateApiResponse.getResult();
assertEquals("Application ID for call state not equal", VOICE_APPLICATION_ID,
callStateResponse.getApplicationId());
assertEquals("To phone number for call state not equal", USER_NUMBER, callStateResponse.getTo());
assertEquals("From phone number for call state not equal", BW_NUMBER, callStateResponse.getFrom());
assertEquals("Call ID not equal", createCallResponse.getCallId(), callStateResponse.getCallId());
}

@Test
public void testCreateCallWithPriorityAndGetCallState() throws Exception {
final String answerUrl = BASE_CALLBACK_URL.concat("/callbacks/outbound");
final Integer priority = 4;

CreateCallRequest body = new CreateCallRequest.Builder()
.to(USER_NUMBER)
.from(BW_NUMBER)
.applicationId(VOICE_APPLICATION_ID)
.answerUrl(answerUrl)
.priority(priority)
.tag("the tag")
.build();

ApiResponse<CreateCallResponse> createCallApiResponse = controller.createCall(ACCOUNT_ID, body);
assertEquals("Response Code is not 201", 201, createCallApiResponse.getStatusCode());

CreateCallResponse createCallResponse = createCallApiResponse.getResult();
assertNotNull("Call ID is null", createCallResponse.getCallId());
assertFalse("Call ID is empty", createCallResponse.getCallId().isEmpty());
assertEquals("Call ID is not 47 characters", 47, createCallResponse.getCallId().length());
assertEquals("Application ID for create call not equal", VOICE_APPLICATION_ID,
createCallResponse.getApplicationId());
assertEquals("To phone number for create call not equal", USER_NUMBER, createCallResponse.getTo());
assertEquals("From phone number for create call not equal", BW_NUMBER, createCallResponse.getFrom());
assertEquals("Priority is not equal", priority, createCallResponse.getPriority());
assertEquals("Tag is missing", "the tag", createCallResponse.getTag());
}

@Test
public void testCreateCallInvalidPhoneNumber() throws Exception {
final String answerUrl = BASE_CALLBACK_URL.concat("/callbacks/outbound");

CreateCallRequest body = new CreateCallRequest.Builder()
.to("+1invalid")
.from(BW_NUMBER)
.applicationId(VOICE_APPLICATION_ID)
.answerUrl(answerUrl)
.build();

ApiErrorException e = assertThrows(
"ApiError Exception not thrown",
ApiErrorException.class,
() -> controller.createCall(ACCOUNT_ID, body));
assertEquals("Response Code is not 400", 400, e.getResponseCode());
}
private APIController controller;

@Before
public void initTest() {
BandwidthClient client = new BandwidthClient.Builder()
.voiceBasicAuthCredentials(USERNAME, PASSWORD)
.build();

controller = client.getVoiceClient().getAPIController();
}

@Test
public void testCreateCallAndGetCallState() throws Exception {
final String answerUrl = BASE_CALLBACK_URL.concat("/callbacks/outbound");

CreateCallRequest body = new CreateCallRequest.Builder()
.to(USER_NUMBER)
.from(BW_NUMBER)
.applicationId(VOICE_APPLICATION_ID)
.answerUrl(answerUrl)
.build();

ApiResponse<CreateCallResponse> createCallApiResponse = controller.createCall(ACCOUNT_ID, body);

assertEquals("Response Code is not 201", 201, createCallApiResponse.getStatusCode());

CreateCallResponse createCallResponse = createCallApiResponse.getResult();
assertNotNull("Call ID is null", createCallResponse.getCallId());
assertFalse("Call ID is empty", createCallResponse.getCallId().isEmpty());
assertEquals("Call ID is not 47 characters", 47, createCallResponse.getCallId().length());
assertEquals("Application ID for create call not equal", VOICE_APPLICATION_ID,
createCallResponse.getApplicationId());
assertEquals("To phone number for create call not equal", USER_NUMBER, createCallResponse.getTo());
assertEquals("From phone number for create call not equal", BW_NUMBER, createCallResponse.getFrom());
assertNotNull("enqueuedTime is null", createCallResponse.getEnqueuedTime());
assertEquals("enqueuedTime is not a LocalDateTime object", LocalDateTime.class,
createCallResponse.getEnqueuedTime().getClass());
try {

ApiResponse<CallState> callStateApiResponse = controller.getCall(ACCOUNT_ID,
createCallResponse.getCallId());
assertEquals("Response Code is not 200", 200, callStateApiResponse.getStatusCode());

CallState callStateResponse = callStateApiResponse.getResult();
assertEquals("Application ID for call state not equal", VOICE_APPLICATION_ID,
callStateResponse.getApplicationId());
assertEquals("To phone number for call state not equal", USER_NUMBER, callStateResponse.getTo());
assertEquals("From phone number for call state not equal", BW_NUMBER, callStateResponse.getFrom());
assertEquals("Call ID not equal", createCallResponse.getCallId(), callStateResponse.getCallId());
assertNotNull("enqueuedTime is null", createCallResponse.getEnqueuedTime());
assertEquals("enqueuedTime is not a LocalDateTime object", LocalDateTime.class,
createCallResponse.getEnqueuedTime().getClass());
} catch (ApiException e) {
assertEquals("Response Code is not 404", 404, e.getResponseCode());
}}



@Test
public void testCreateCallWithAmdAndGetCallState() throws Exception {
final String answerUrl = BASE_CALLBACK_URL.concat("/callbacks/outbound");
final String machineDetectionUrl = BASE_CALLBACK_URL.concat("/callbacks/machineDetection");

MachineDetectionConfiguration machineDetectionConfiguration = new MachineDetectionConfiguration.Builder()
.mode(ModeEnum.ASYNC)
.callbackUrl(machineDetectionUrl)
.callbackMethod(CallbackMethodEnum.POST)
.detectionTimeout(5.0)
.silenceTimeout(5.0)
.speechThreshold(5.0)
.speechEndThreshold(5.0)
.delayResult(true)
.build();

CreateCallRequest body = new CreateCallRequest.Builder()
.to(USER_NUMBER)
.from(BW_NUMBER)
.applicationId(VOICE_APPLICATION_ID)
.answerUrl(answerUrl)
.machineDetection(machineDetectionConfiguration)
.build();

ApiResponse<CreateCallResponse> createCallApiResponse = controller.createCall(ACCOUNT_ID, body);
assertEquals("Response Code is not 201", 201, createCallApiResponse.getStatusCode());

CreateCallResponse createCallResponse = createCallApiResponse.getResult();
assertNotNull("Call ID is null", createCallResponse.getCallId());
assertFalse("Call ID is empty", createCallResponse.getCallId().isEmpty());
assertEquals("Call ID is not 47 characters", 47, createCallResponse.getCallId().length());
assertEquals("Application ID for create call not equal", VOICE_APPLICATION_ID,
createCallResponse.getApplicationId());
assertEquals("To phone number for create call not equal", USER_NUMBER, createCallResponse.getTo());
assertEquals("From phone number for create call not equal", BW_NUMBER, createCallResponse.getFrom());

try {
ApiResponse<CallState> callStateApiResponse = controller.getCall(ACCOUNT_ID,
createCallResponse.getCallId());
CallState callStateResponse = callStateApiResponse.getResult();
assertEquals("Application ID for call state not equal", VOICE_APPLICATION_ID,
callStateResponse.getApplicationId());
assertEquals("To phone number for call state not equal", USER_NUMBER, callStateResponse.getTo());
assertEquals("From phone number for call state not equal", BW_NUMBER, callStateResponse.getFrom());
assertEquals("Call ID not equal", createCallResponse.getCallId(), callStateResponse.getCallId());
} catch (ApiException e) {
assertEquals("Response Code is not 404", 404, e.getResponseCode());
}}


@Test
public void testCreateCallWithPriorityAndGetCallState() throws Exception {
final String answerUrl = BASE_CALLBACK_URL.concat("/callbacks/outbound");
final Integer priority = 4;

CreateCallRequest body = new CreateCallRequest.Builder()
.to(USER_NUMBER)
.from(BW_NUMBER)
.applicationId(VOICE_APPLICATION_ID)
.answerUrl(answerUrl)
.priority(priority)
.tag("the tag")
.build();

ApiResponse<CreateCallResponse> createCallApiResponse = controller.createCall(ACCOUNT_ID, body);
assertEquals("Response Code is not 201", 201, createCallApiResponse.getStatusCode());

CreateCallResponse createCallResponse = createCallApiResponse.getResult();
assertNotNull("Call ID is null", createCallResponse.getCallId());
assertFalse("Call ID is empty", createCallResponse.getCallId().isEmpty());
assertEquals("Call ID is not 47 characters", 47, createCallResponse.getCallId().length());
assertEquals("Application ID for create call not equal", VOICE_APPLICATION_ID,
createCallResponse.getApplicationId());
assertEquals("To phone number for create call not equal", USER_NUMBER, createCallResponse.getTo());
assertEquals("From phone number for create call not equal", BW_NUMBER, createCallResponse.getFrom());
assertEquals("Priority is not equal", priority, createCallResponse.getPriority());
assertEquals("Tag is missing", "the tag", createCallResponse.getTag());
}

@Test
public void testCreateCallInvalidPhoneNumber() throws Exception {
final String answerUrl = BASE_CALLBACK_URL.concat("/callbacks/outbound");

CreateCallRequest body = new CreateCallRequest.Builder()
.to("+1invalid")
.from(BW_NUMBER)
.applicationId(VOICE_APPLICATION_ID)
.answerUrl(answerUrl)
.build();

ApiErrorException e = assertThrows(
"ApiError Exception not thrown",
ApiErrorException.class,
() -> controller.createCall(ACCOUNT_ID, body));
assertEquals("Response Code is not 400", 400, e.getResponseCode());
}
}

0 comments on commit f29b3e2

Please sign in to comment.