|
5 | 5 | import com.bandwidth.voice.controllers.APIController; |
6 | 6 |
|
7 | 7 | import com.bandwidth.voice.exceptions.ApiErrorException; |
| 8 | +import com.bandwidth.exceptions.ApiException; |
8 | 9 | import com.bandwidth.voice.models.*; |
9 | 10 | import org.junit.*; |
10 | 11 |
|
|
20 | 21 | */ |
21 | 22 | public class VoiceApiTest { |
22 | 23 |
|
23 | | - private APIController controller; |
24 | | - |
25 | | - @Before |
26 | | - public void initTest() { |
27 | | - BandwidthClient client = new BandwidthClient.Builder() |
28 | | - .voiceBasicAuthCredentials(USERNAME, PASSWORD) |
29 | | - .build(); |
30 | | - |
31 | | - controller = client.getVoiceClient().getAPIController(); |
32 | | - } |
33 | | - |
34 | | - @Test |
35 | | - public void testCreateCallAndGetCallState() throws Exception { |
36 | | - final String answerUrl = BASE_CALLBACK_URL.concat("/callbacks/outbound"); |
37 | | - |
38 | | - CreateCallRequest body = new CreateCallRequest.Builder() |
39 | | - .to(USER_NUMBER) |
40 | | - .from(BW_NUMBER) |
41 | | - .applicationId(VOICE_APPLICATION_ID) |
42 | | - .answerUrl(answerUrl) |
43 | | - .build(); |
44 | | - |
45 | | - ApiResponse<CreateCallResponse> createCallApiResponse = controller.createCall(ACCOUNT_ID, body); |
46 | | - |
47 | | - assertEquals("Response Code is not 201", 201, createCallApiResponse.getStatusCode()); |
48 | | - |
49 | | - CreateCallResponse createCallResponse = createCallApiResponse.getResult(); |
50 | | - assertNotNull("Call ID is null", createCallResponse.getCallId()); |
51 | | - assertFalse("Call ID is empty", createCallResponse.getCallId().isEmpty()); |
52 | | - assertEquals("Call ID is not 47 characters", 47, createCallResponse.getCallId().length()); |
53 | | - assertEquals("Application ID for create call not equal", VOICE_APPLICATION_ID, |
54 | | - createCallResponse.getApplicationId()); |
55 | | - assertEquals("To phone number for create call not equal", USER_NUMBER, createCallResponse.getTo()); |
56 | | - assertEquals("From phone number for create call not equal", BW_NUMBER, createCallResponse.getFrom()); |
57 | | - assertNotNull("enqueuedTime is null", createCallResponse.getEnqueuedTime()); |
58 | | - assertEquals("enqueuedTime is not a LocalDateTime object", LocalDateTime.class, |
59 | | - createCallResponse.getEnqueuedTime().getClass()); |
60 | | - |
61 | | - // get call state |
62 | | - Thread.sleep(15000); // Wait to get Call because of current system latency issues |
63 | | - ApiResponse<CallState> callStateApiResponse = controller.getCall(ACCOUNT_ID, |
64 | | - createCallResponse.getCallId()); |
65 | | - assertEquals("Response Code is not 200", 200, callStateApiResponse.getStatusCode()); |
66 | | - |
67 | | - CallState callStateResponse = callStateApiResponse.getResult(); |
68 | | - assertEquals("Application ID for call state not equal", VOICE_APPLICATION_ID, |
69 | | - callStateResponse.getApplicationId()); |
70 | | - assertEquals("To phone number for call state not equal", USER_NUMBER, callStateResponse.getTo()); |
71 | | - assertEquals("From phone number for call state not equal", BW_NUMBER, callStateResponse.getFrom()); |
72 | | - assertEquals("Call ID not equal", createCallResponse.getCallId(), callStateResponse.getCallId()); |
73 | | - assertNotNull("enqueuedTime is null", createCallResponse.getEnqueuedTime()); |
74 | | - assertEquals("enqueuedTime is not a LocalDateTime object", LocalDateTime.class, |
75 | | - createCallResponse.getEnqueuedTime().getClass()); |
76 | | - } |
77 | | - |
78 | | - @Test |
79 | | - public void testCreateCallWithAmdAndGetCallState() throws Exception { |
80 | | - final String answerUrl = BASE_CALLBACK_URL.concat("/callbacks/outbound"); |
81 | | - final String machineDetectionUrl = BASE_CALLBACK_URL.concat("/callbacks/machineDetection"); |
82 | | - |
83 | | - MachineDetectionConfiguration machineDetectionConfiguration = new MachineDetectionConfiguration.Builder() |
84 | | - .mode(ModeEnum.ASYNC) |
85 | | - .callbackUrl(machineDetectionUrl) |
86 | | - .callbackMethod(CallbackMethodEnum.POST) |
87 | | - .detectionTimeout(5.0) |
88 | | - .silenceTimeout(5.0) |
89 | | - .speechThreshold(5.0) |
90 | | - .speechEndThreshold(5.0) |
91 | | - .delayResult(true) |
92 | | - .build(); |
93 | | - |
94 | | - CreateCallRequest body = new CreateCallRequest.Builder() |
95 | | - .to(USER_NUMBER) |
96 | | - .from(BW_NUMBER) |
97 | | - .applicationId(VOICE_APPLICATION_ID) |
98 | | - .answerUrl(answerUrl) |
99 | | - .machineDetection(machineDetectionConfiguration) |
100 | | - .build(); |
101 | | - |
102 | | - ApiResponse<CreateCallResponse> createCallApiResponse = controller.createCall(ACCOUNT_ID, body); |
103 | | - assertEquals("Response Code is not 201", 201, createCallApiResponse.getStatusCode()); |
104 | | - |
105 | | - CreateCallResponse createCallResponse = createCallApiResponse.getResult(); |
106 | | - assertNotNull("Call ID is null", createCallResponse.getCallId()); |
107 | | - assertFalse("Call ID is empty", createCallResponse.getCallId().isEmpty()); |
108 | | - assertEquals("Call ID is not 47 characters", 47, createCallResponse.getCallId().length()); |
109 | | - assertEquals("Application ID for create call not equal", VOICE_APPLICATION_ID, |
110 | | - createCallResponse.getApplicationId()); |
111 | | - assertEquals("To phone number for create call not equal", USER_NUMBER, createCallResponse.getTo()); |
112 | | - assertEquals("From phone number for create call not equal", BW_NUMBER, createCallResponse.getFrom()); |
113 | | - |
114 | | - // get call state |
115 | | - Thread.sleep(15000); // Wait to get Call because of current system latency issues |
116 | | - ApiResponse<CallState> callStateApiResponse = controller.getCall(ACCOUNT_ID, |
117 | | - createCallResponse.getCallId()); |
118 | | - CallState callStateResponse = callStateApiResponse.getResult(); |
119 | | - assertEquals("Application ID for call state not equal", VOICE_APPLICATION_ID, |
120 | | - callStateResponse.getApplicationId()); |
121 | | - assertEquals("To phone number for call state not equal", USER_NUMBER, callStateResponse.getTo()); |
122 | | - assertEquals("From phone number for call state not equal", BW_NUMBER, callStateResponse.getFrom()); |
123 | | - assertEquals("Call ID not equal", createCallResponse.getCallId(), callStateResponse.getCallId()); |
124 | | - } |
125 | | - |
126 | | - @Test |
127 | | - public void testCreateCallWithPriorityAndGetCallState() throws Exception { |
128 | | - final String answerUrl = BASE_CALLBACK_URL.concat("/callbacks/outbound"); |
129 | | - final Integer priority = 4; |
130 | | - |
131 | | - CreateCallRequest body = new CreateCallRequest.Builder() |
132 | | - .to(USER_NUMBER) |
133 | | - .from(BW_NUMBER) |
134 | | - .applicationId(VOICE_APPLICATION_ID) |
135 | | - .answerUrl(answerUrl) |
136 | | - .priority(priority) |
137 | | - .tag("the tag") |
138 | | - .build(); |
139 | | - |
140 | | - ApiResponse<CreateCallResponse> createCallApiResponse = controller.createCall(ACCOUNT_ID, body); |
141 | | - assertEquals("Response Code is not 201", 201, createCallApiResponse.getStatusCode()); |
142 | | - |
143 | | - CreateCallResponse createCallResponse = createCallApiResponse.getResult(); |
144 | | - assertNotNull("Call ID is null", createCallResponse.getCallId()); |
145 | | - assertFalse("Call ID is empty", createCallResponse.getCallId().isEmpty()); |
146 | | - assertEquals("Call ID is not 47 characters", 47, createCallResponse.getCallId().length()); |
147 | | - assertEquals("Application ID for create call not equal", VOICE_APPLICATION_ID, |
148 | | - createCallResponse.getApplicationId()); |
149 | | - assertEquals("To phone number for create call not equal", USER_NUMBER, createCallResponse.getTo()); |
150 | | - assertEquals("From phone number for create call not equal", BW_NUMBER, createCallResponse.getFrom()); |
151 | | - assertEquals("Priority is not equal", priority, createCallResponse.getPriority()); |
152 | | - assertEquals("Tag is missing", "the tag", createCallResponse.getTag()); |
153 | | - } |
154 | | - |
155 | | - @Test |
156 | | - public void testCreateCallInvalidPhoneNumber() throws Exception { |
157 | | - final String answerUrl = BASE_CALLBACK_URL.concat("/callbacks/outbound"); |
158 | | - |
159 | | - CreateCallRequest body = new CreateCallRequest.Builder() |
160 | | - .to("+1invalid") |
161 | | - .from(BW_NUMBER) |
162 | | - .applicationId(VOICE_APPLICATION_ID) |
163 | | - .answerUrl(answerUrl) |
164 | | - .build(); |
165 | | - |
166 | | - ApiErrorException e = assertThrows( |
167 | | - "ApiError Exception not thrown", |
168 | | - ApiErrorException.class, |
169 | | - () -> controller.createCall(ACCOUNT_ID, body)); |
170 | | - assertEquals("Response Code is not 400", 400, e.getResponseCode()); |
171 | | - } |
| 24 | + private APIController controller; |
| 25 | + |
| 26 | + @Before |
| 27 | + public void initTest() { |
| 28 | + BandwidthClient client = new BandwidthClient.Builder() |
| 29 | + .voiceBasicAuthCredentials(USERNAME, PASSWORD) |
| 30 | + .build(); |
| 31 | + |
| 32 | + controller = client.getVoiceClient().getAPIController(); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void testCreateCallAndGetCallState() throws Exception { |
| 37 | + final String answerUrl = BASE_CALLBACK_URL.concat("/callbacks/outbound"); |
| 38 | + |
| 39 | + CreateCallRequest body = new CreateCallRequest.Builder() |
| 40 | + .to(USER_NUMBER) |
| 41 | + .from(BW_NUMBER) |
| 42 | + .applicationId(VOICE_APPLICATION_ID) |
| 43 | + .answerUrl(answerUrl) |
| 44 | + .build(); |
| 45 | + |
| 46 | + ApiResponse<CreateCallResponse> createCallApiResponse = controller.createCall(ACCOUNT_ID, body); |
| 47 | + |
| 48 | + assertEquals("Response Code is not 201", 201, createCallApiResponse.getStatusCode()); |
| 49 | + |
| 50 | + CreateCallResponse createCallResponse = createCallApiResponse.getResult(); |
| 51 | + assertNotNull("Call ID is null", createCallResponse.getCallId()); |
| 52 | + assertFalse("Call ID is empty", createCallResponse.getCallId().isEmpty()); |
| 53 | + assertEquals("Call ID is not 47 characters", 47, createCallResponse.getCallId().length()); |
| 54 | + assertEquals("Application ID for create call not equal", VOICE_APPLICATION_ID, |
| 55 | + createCallResponse.getApplicationId()); |
| 56 | + assertEquals("To phone number for create call not equal", USER_NUMBER, createCallResponse.getTo()); |
| 57 | + assertEquals("From phone number for create call not equal", BW_NUMBER, createCallResponse.getFrom()); |
| 58 | + assertNotNull("enqueuedTime is null", createCallResponse.getEnqueuedTime()); |
| 59 | + assertEquals("enqueuedTime is not a LocalDateTime object", LocalDateTime.class, |
| 60 | + createCallResponse.getEnqueuedTime().getClass()); |
| 61 | + try { |
| 62 | + |
| 63 | + ApiResponse<CallState> callStateApiResponse = controller.getCall(ACCOUNT_ID, |
| 64 | + createCallResponse.getCallId()); |
| 65 | + assertEquals("Response Code is not 200", 200, callStateApiResponse.getStatusCode()); |
| 66 | + |
| 67 | + CallState callStateResponse = callStateApiResponse.getResult(); |
| 68 | + assertEquals("Application ID for call state not equal", VOICE_APPLICATION_ID, |
| 69 | + callStateResponse.getApplicationId()); |
| 70 | + assertEquals("To phone number for call state not equal", USER_NUMBER, callStateResponse.getTo()); |
| 71 | + assertEquals("From phone number for call state not equal", BW_NUMBER, callStateResponse.getFrom()); |
| 72 | + assertEquals("Call ID not equal", createCallResponse.getCallId(), callStateResponse.getCallId()); |
| 73 | + assertNotNull("enqueuedTime is null", createCallResponse.getEnqueuedTime()); |
| 74 | + assertEquals("enqueuedTime is not a LocalDateTime object", LocalDateTime.class, |
| 75 | + createCallResponse.getEnqueuedTime().getClass()); |
| 76 | + } catch (ApiException e) { |
| 77 | + assertEquals("Response Code is not 404", 404, e.getResponseCode()); |
| 78 | + }} |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testCreateCallWithAmdAndGetCallState() throws Exception { |
| 84 | + final String answerUrl = BASE_CALLBACK_URL.concat("/callbacks/outbound"); |
| 85 | + final String machineDetectionUrl = BASE_CALLBACK_URL.concat("/callbacks/machineDetection"); |
| 86 | + |
| 87 | + MachineDetectionConfiguration machineDetectionConfiguration = new MachineDetectionConfiguration.Builder() |
| 88 | + .mode(ModeEnum.ASYNC) |
| 89 | + .callbackUrl(machineDetectionUrl) |
| 90 | + .callbackMethod(CallbackMethodEnum.POST) |
| 91 | + .detectionTimeout(5.0) |
| 92 | + .silenceTimeout(5.0) |
| 93 | + .speechThreshold(5.0) |
| 94 | + .speechEndThreshold(5.0) |
| 95 | + .delayResult(true) |
| 96 | + .build(); |
| 97 | + |
| 98 | + CreateCallRequest body = new CreateCallRequest.Builder() |
| 99 | + .to(USER_NUMBER) |
| 100 | + .from(BW_NUMBER) |
| 101 | + .applicationId(VOICE_APPLICATION_ID) |
| 102 | + .answerUrl(answerUrl) |
| 103 | + .machineDetection(machineDetectionConfiguration) |
| 104 | + .build(); |
| 105 | + |
| 106 | + ApiResponse<CreateCallResponse> createCallApiResponse = controller.createCall(ACCOUNT_ID, body); |
| 107 | + assertEquals("Response Code is not 201", 201, createCallApiResponse.getStatusCode()); |
| 108 | + |
| 109 | + CreateCallResponse createCallResponse = createCallApiResponse.getResult(); |
| 110 | + assertNotNull("Call ID is null", createCallResponse.getCallId()); |
| 111 | + assertFalse("Call ID is empty", createCallResponse.getCallId().isEmpty()); |
| 112 | + assertEquals("Call ID is not 47 characters", 47, createCallResponse.getCallId().length()); |
| 113 | + assertEquals("Application ID for create call not equal", VOICE_APPLICATION_ID, |
| 114 | + createCallResponse.getApplicationId()); |
| 115 | + assertEquals("To phone number for create call not equal", USER_NUMBER, createCallResponse.getTo()); |
| 116 | + assertEquals("From phone number for create call not equal", BW_NUMBER, createCallResponse.getFrom()); |
| 117 | + |
| 118 | + try { |
| 119 | + ApiResponse<CallState> callStateApiResponse = controller.getCall(ACCOUNT_ID, |
| 120 | + createCallResponse.getCallId()); |
| 121 | + CallState callStateResponse = callStateApiResponse.getResult(); |
| 122 | + assertEquals("Application ID for call state not equal", VOICE_APPLICATION_ID, |
| 123 | + callStateResponse.getApplicationId()); |
| 124 | + assertEquals("To phone number for call state not equal", USER_NUMBER, callStateResponse.getTo()); |
| 125 | + assertEquals("From phone number for call state not equal", BW_NUMBER, callStateResponse.getFrom()); |
| 126 | + assertEquals("Call ID not equal", createCallResponse.getCallId(), callStateResponse.getCallId()); |
| 127 | + } catch (ApiException e) { |
| 128 | + assertEquals("Response Code is not 404", 404, e.getResponseCode()); |
| 129 | + }} |
| 130 | + |
| 131 | + |
| 132 | + @Test |
| 133 | + public void testCreateCallWithPriorityAndGetCallState() throws Exception { |
| 134 | + final String answerUrl = BASE_CALLBACK_URL.concat("/callbacks/outbound"); |
| 135 | + final Integer priority = 4; |
| 136 | + |
| 137 | + CreateCallRequest body = new CreateCallRequest.Builder() |
| 138 | + .to(USER_NUMBER) |
| 139 | + .from(BW_NUMBER) |
| 140 | + .applicationId(VOICE_APPLICATION_ID) |
| 141 | + .answerUrl(answerUrl) |
| 142 | + .priority(priority) |
| 143 | + .tag("the tag") |
| 144 | + .build(); |
| 145 | + |
| 146 | + ApiResponse<CreateCallResponse> createCallApiResponse = controller.createCall(ACCOUNT_ID, body); |
| 147 | + assertEquals("Response Code is not 201", 201, createCallApiResponse.getStatusCode()); |
| 148 | + |
| 149 | + CreateCallResponse createCallResponse = createCallApiResponse.getResult(); |
| 150 | + assertNotNull("Call ID is null", createCallResponse.getCallId()); |
| 151 | + assertFalse("Call ID is empty", createCallResponse.getCallId().isEmpty()); |
| 152 | + assertEquals("Call ID is not 47 characters", 47, createCallResponse.getCallId().length()); |
| 153 | + assertEquals("Application ID for create call not equal", VOICE_APPLICATION_ID, |
| 154 | + createCallResponse.getApplicationId()); |
| 155 | + assertEquals("To phone number for create call not equal", USER_NUMBER, createCallResponse.getTo()); |
| 156 | + assertEquals("From phone number for create call not equal", BW_NUMBER, createCallResponse.getFrom()); |
| 157 | + assertEquals("Priority is not equal", priority, createCallResponse.getPriority()); |
| 158 | + assertEquals("Tag is missing", "the tag", createCallResponse.getTag()); |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + public void testCreateCallInvalidPhoneNumber() throws Exception { |
| 163 | + final String answerUrl = BASE_CALLBACK_URL.concat("/callbacks/outbound"); |
| 164 | + |
| 165 | + CreateCallRequest body = new CreateCallRequest.Builder() |
| 166 | + .to("+1invalid") |
| 167 | + .from(BW_NUMBER) |
| 168 | + .applicationId(VOICE_APPLICATION_ID) |
| 169 | + .answerUrl(answerUrl) |
| 170 | + .build(); |
| 171 | + |
| 172 | + ApiErrorException e = assertThrows( |
| 173 | + "ApiError Exception not thrown", |
| 174 | + ApiErrorException.class, |
| 175 | + () -> controller.createCall(ACCOUNT_ID, body)); |
| 176 | + assertEquals("Response Code is not 400", 400, e.getResponseCode()); |
| 177 | + } |
172 | 178 | } |
0 commit comments