|
| 1 | +package com.bandwidth; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | +import org.junit.Before; |
| 5 | + |
| 6 | +import static org.junit.Assert.*; |
| 7 | + |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.io.File; |
| 10 | +import java.io.FileInputStream; |
| 11 | +import java.io.InputStream; |
| 12 | +import java.io.InputStreamReader; |
| 13 | +import java.io.IOException; |
| 14 | +import java.io.Reader; |
| 15 | + |
| 16 | +import com.bandwidth.*; |
| 17 | +import com.bandwidth.Environment; |
| 18 | +import com.bandwidth.voice.models.*; |
| 19 | +import com.bandwidth.voice.controllers.*; |
| 20 | +import com.bandwidth.voice.exceptions.*; |
| 21 | +import com.bandwidth.messaging.models.*; |
| 22 | +import com.bandwidth.messaging.controllers.*; |
| 23 | +import com.bandwidth.messaging.exceptions.*; |
| 24 | +import com.bandwidth.twofactorauth.models.*; |
| 25 | +import com.bandwidth.twofactorauth.controllers.*; |
| 26 | +import com.bandwidth.twofactorauth.exceptions.*; |
| 27 | +import com.bandwidth.exceptions.ApiException; |
| 28 | +import com.bandwidth.http.response.ApiResponse; |
| 29 | +import com.bandwidth.utilities.FileWrapper; |
| 30 | + |
| 31 | +/** |
| 32 | + * Integration tests for API interactions |
| 33 | + */ |
| 34 | +public class ApiTest { |
| 35 | + |
| 36 | + private BandwidthClient client; |
| 37 | + private com.bandwidth.messaging.controllers.APIController messagingController; |
| 38 | + private com.bandwidth.voice.controllers.APIController voiceController; |
| 39 | + private com.bandwidth.twofactorauth.controllers.MFAController mfaController; |
| 40 | + |
| 41 | + @Before |
| 42 | + public void init() { |
| 43 | + this.client = new BandwidthClient.Builder() |
| 44 | + .messagingBasicAuthCredentials(System.getenv("BW_USERNAME"), System.getenv("BW_PASSWORD")) |
| 45 | + .voiceBasicAuthCredentials(System.getenv("BW_USERNAME"), System.getenv("BW_PASSWORD")) |
| 46 | + .twoFactorAuthBasicAuthCredentials(System.getenv("BW_USERNAME"), System.getenv("BW_PASSWORD")) |
| 47 | + .build(); |
| 48 | + this.messagingController = client.getMessagingClient().getAPIController(); |
| 49 | + this.voiceController = client.getVoiceClient().getAPIController(); |
| 50 | + this.mfaController = client.getTwoFactorAuthClient().getMFAController(); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void testCreateMessage() throws Exception { |
| 55 | + String accountId = System.getenv("BW_ACCOUNT_ID"); |
| 56 | + String to = System.getenv("PHONE_NUMBER_INBOUND"); |
| 57 | + ArrayList<String> toNumbers = new ArrayList<String>(); |
| 58 | + toNumbers.add(to); |
| 59 | + String from = System.getenv("PHONE_NUMBER_OUTBOUND"); |
| 60 | + String applicationId = System.getenv("MESSAGING_APPLICATION_ID"); |
| 61 | + String text = "Java Test"; |
| 62 | + |
| 63 | + MessageRequest body = new MessageRequest(); |
| 64 | + body.setTo(toNumbers); |
| 65 | + body.setFrom(from); |
| 66 | + body.setText(text); |
| 67 | + body.setApplicationId(applicationId); |
| 68 | + |
| 69 | + ApiResponse<BandwidthMessage> response = messagingController.createMessage(accountId, body); |
| 70 | + assertEquals("Application ID not equal", applicationId, response.getResult().getApplicationId()); |
| 71 | + assertEquals("To phone number not equal", to, response.getResult().getTo().get(0)); |
| 72 | + assertEquals("From phone number not equal", from, response.getResult().getFrom()); |
| 73 | + assertEquals("Text not equal", text, response.getResult().getText()); |
| 74 | + } |
| 75 | + |
| 76 | + @Test(expected = MessagingException.class) |
| 77 | + public void testCreateMessageInvalidPhoneNumber() throws Exception { |
| 78 | + String accountId = System.getenv("BW_ACCOUNT_ID"); |
| 79 | + String to = "+1invalid"; |
| 80 | + ArrayList<String> toNumbers = new ArrayList<String>(); |
| 81 | + toNumbers.add(to); |
| 82 | + String from = System.getenv("PHONE_NUMBER_OUTBOUND"); |
| 83 | + String applicationId = System.getenv("MESSAGING_APPLICATION_ID"); |
| 84 | + String text = "Java Test"; |
| 85 | + |
| 86 | + MessageRequest body = new MessageRequest(); |
| 87 | + body.setTo(toNumbers); |
| 88 | + body.setFrom(from); |
| 89 | + body.setText(text); |
| 90 | + body.setApplicationId(applicationId); |
| 91 | + |
| 92 | + messagingController.createMessage(accountId, body); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void testUploadDownloadMedia() throws Exception { |
| 97 | + String fileName = "src/test/resources/mediaUpload.png"; |
| 98 | + File file = new File(fileName); |
| 99 | + FileInputStream inStream = new FileInputStream(fileName); |
| 100 | + String fileContents = ApiTest.convertInputStreamToString(inStream); |
| 101 | + FileWrapper body = new FileWrapper(file, "image/png"); |
| 102 | + String accountId = System.getenv("BW_ACCOUNT_ID"); |
| 103 | + String mediaId = "java-media-test"; |
| 104 | + String fileType = "image/png"; |
| 105 | + String cache = "no-cache"; |
| 106 | + |
| 107 | + messagingController.uploadMedia(accountId, mediaId, fileContents.length(), body, fileType, cache); |
| 108 | + |
| 109 | + ApiResponse<InputStream> response = messagingController.getMedia(accountId, mediaId); |
| 110 | + String resultString = ApiTest.convertInputStreamToString(response.getResult()); |
| 111 | + |
| 112 | + assertEquals("Media download not equal to media upload", fileContents, resultString); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void testCreateCallAndGetCallState() throws Exception { |
| 117 | + String accountId = System.getenv("BW_ACCOUNT_ID"); |
| 118 | + String to = System.getenv("PHONE_NUMBER_INBOUND"); |
| 119 | + String from = System.getenv("PHONE_NUMBER_OUTBOUND"); |
| 120 | + String applicationId = System.getenv("VOICE_APPLICATION_ID"); |
| 121 | + String answerUrl = System.getenv("VOICE_CALLBACK_URL"); |
| 122 | + |
| 123 | + ApiCreateCallRequest body = new ApiCreateCallRequest(); |
| 124 | + body.setTo(to); |
| 125 | + body.setFrom(from); |
| 126 | + body.setApplicationId(applicationId); |
| 127 | + body.setAnswerUrl(answerUrl); |
| 128 | + |
| 129 | + ApiResponse<ApiCallResponse> createCallResponse = voiceController.createCall(accountId, body); |
| 130 | + assertEquals("Application ID for create call not equal", applicationId, createCallResponse.getResult().getApplicationId()); |
| 131 | + assertEquals("To phone number for create call not equal", to, createCallResponse.getResult().getTo()); |
| 132 | + assertEquals("From phone number for create call not equal", from, createCallResponse.getResult().getFrom()); |
| 133 | + |
| 134 | + //get call state |
| 135 | + String callId = createCallResponse.getResult().getCallId(); |
| 136 | + ApiResponse<ApiCallStateResponse> callStateResponse = voiceController.getCallState(accountId, callId); |
| 137 | + assertEquals("Application ID for call state not equal", applicationId, callStateResponse.getResult().getApplicationId()); |
| 138 | + assertEquals("To phone number for call state not equal", to, callStateResponse.getResult().getTo()); |
| 139 | + assertEquals("From phone number for call state not equal", from, callStateResponse.getResult().getFrom()); |
| 140 | + assertEquals("Call ID not equal", callId, callStateResponse.getResult().getCallId()); |
| 141 | + } |
| 142 | + |
| 143 | + @Test(expected = ApiErrorResponseException.class) |
| 144 | + public void testCreateCallInvalidPhoneNumber() throws Exception { |
| 145 | + String accountId = System.getenv("BW_ACCOUNT_ID"); |
| 146 | + String to = "+1invalid"; |
| 147 | + String from = System.getenv("PHONE_NUMBER_OUTBOUND"); |
| 148 | + String applicationId = System.getenv("VOICE_APPLICATION_ID"); |
| 149 | + String answerUrl = System.getenv("VOICE_CALLBACK_URL"); |
| 150 | + |
| 151 | + ApiCreateCallRequest body = new ApiCreateCallRequest(); |
| 152 | + body.setTo(to); |
| 153 | + body.setFrom(from); |
| 154 | + body.setApplicationId(applicationId); |
| 155 | + body.setAnswerUrl(answerUrl); |
| 156 | + |
| 157 | + voiceController.createCall(accountId, body); |
| 158 | + } |
| 159 | + |
| 160 | + @Test |
| 161 | + public void testMfaMessaging() throws Exception { |
| 162 | + String accountId = System.getenv("BW_ACCOUNT_ID"); |
| 163 | + String to = System.getenv("PHONE_NUMBER_INBOUND"); |
| 164 | + String from = System.getenv("PHONE_NUMBER_MFA"); |
| 165 | + String applicationId = System.getenv("MFA_MESSAGING_APPLICATION_ID"); |
| 166 | + String scope = "scope"; |
| 167 | + int digits = 6; |
| 168 | + String message = "Your temporary {NAME} {SCOPE} code is {CODE}"; |
| 169 | + |
| 170 | + TwoFactorCodeRequestSchema body = new TwoFactorCodeRequestSchema(); |
| 171 | + body.setTo(to); |
| 172 | + body.setFrom(from); |
| 173 | + body.setApplicationId(applicationId); |
| 174 | + body.setScope(scope); |
| 175 | + body.setDigits(digits); |
| 176 | + body.setMessage(message); |
| 177 | + |
| 178 | + ApiResponse<TwoFactorMessagingResponse> response = mfaController.createMessagingTwoFactor(accountId, body); |
| 179 | + assertTrue("Message ID not defined", response.getResult().getMessageId().length() > 0); |
| 180 | + } |
| 181 | + |
| 182 | + @Test |
| 183 | + public void testMfaVoice() throws Exception { |
| 184 | + String accountId = System.getenv("BW_ACCOUNT_ID"); |
| 185 | + String to = System.getenv("PHONE_NUMBER_INBOUND"); |
| 186 | + String from = System.getenv("PHONE_NUMBER_MFA"); |
| 187 | + String applicationId = System.getenv("MFA_VOICE_APPLICATION_ID"); |
| 188 | + String scope = "scope"; |
| 189 | + int digits = 6; |
| 190 | + String message = "Your temporary {NAME} {SCOPE} code is {CODE}"; |
| 191 | + |
| 192 | + TwoFactorCodeRequestSchema body = new TwoFactorCodeRequestSchema(); |
| 193 | + body.setTo(to); |
| 194 | + body.setFrom(from); |
| 195 | + body.setApplicationId(applicationId); |
| 196 | + body.setScope(scope); |
| 197 | + body.setDigits(digits); |
| 198 | + body.setMessage(message); |
| 199 | + |
| 200 | + ApiResponse<TwoFactorVoiceResponse> response = mfaController.createVoiceTwoFactor(accountId, body); |
| 201 | + assertTrue("Call ID not defined", response.getResult().getCallId().length() > 0); |
| 202 | + } |
| 203 | + |
| 204 | + @Test(expected = ErrorWithRequestException.class) |
| 205 | + public void testMfaMessagingInvalidPhoneNumber() throws Exception { |
| 206 | + String accountId = System.getenv("BW_ACCOUNT_ID"); |
| 207 | + String to = "+1invalid"; |
| 208 | + String from = System.getenv("PHONE_NUMBER_MFA"); |
| 209 | + String applicationId = System.getenv("MFA_MESSAGING_APPLICATION_ID"); |
| 210 | + String scope = "scope"; |
| 211 | + int digits = 6; |
| 212 | + String message = "Your temporary {NAME} {SCOPE} code is {CODE}"; |
| 213 | + |
| 214 | + TwoFactorCodeRequestSchema body = new TwoFactorCodeRequestSchema(); |
| 215 | + body.setTo(to); |
| 216 | + body.setFrom(from); |
| 217 | + body.setApplicationId(applicationId); |
| 218 | + body.setScope(scope); |
| 219 | + body.setDigits(digits); |
| 220 | + body.setMessage(message); |
| 221 | + |
| 222 | + mfaController.createMessagingTwoFactor(accountId, body); |
| 223 | + } |
| 224 | + |
| 225 | + @Test(expected = ErrorWithRequestException.class) |
| 226 | + public void testMfaVoiceInvalidPhoneNumber() throws Exception { |
| 227 | + String accountId = System.getenv("BW_ACCOUNT_ID"); |
| 228 | + String to = "+1invalid"; |
| 229 | + String from = System.getenv("PHONE_NUMBER_MFA"); |
| 230 | + String applicationId = System.getenv("MFA_VOICE_APPLICATION_ID"); |
| 231 | + String scope = "scope"; |
| 232 | + int digits = 6; |
| 233 | + String message = "Your temporary {NAME} {SCOPE} code is {CODE}"; |
| 234 | + |
| 235 | + TwoFactorCodeRequestSchema body = new TwoFactorCodeRequestSchema(); |
| 236 | + body.setTo(to); |
| 237 | + body.setFrom(from); |
| 238 | + body.setApplicationId(applicationId); |
| 239 | + body.setScope(scope); |
| 240 | + body.setDigits(digits); |
| 241 | + body.setMessage(message); |
| 242 | + |
| 243 | + mfaController.createVoiceTwoFactor(accountId, body); |
| 244 | + } |
| 245 | + |
| 246 | + @Test |
| 247 | + public void testMfaVerify() throws Exception { |
| 248 | + String accountId = System.getenv("BW_ACCOUNT_ID"); |
| 249 | + String to = System.getenv("PHONE_NUMBER_INBOUND"); |
| 250 | + String applicationId = System.getenv("MFA_VOICE_APPLICATION_ID"); |
| 251 | + String scope = "scope"; |
| 252 | + String code = "123456"; |
| 253 | + int expirationTimeInMinutes = 3; |
| 254 | + |
| 255 | + TwoFactorVerifyRequestSchema body = new TwoFactorVerifyRequestSchema(); |
| 256 | + body.setTo(to); |
| 257 | + body.setApplicationId(applicationId); |
| 258 | + body.setScope(scope); |
| 259 | + body.setCode(code); |
| 260 | + body.setExpirationTimeInMinutes(expirationTimeInMinutes); |
| 261 | + |
| 262 | + ApiResponse<TwoFactorVerifyCodeResponse> response = mfaController.createVerifyTwoFactor(accountId, body); |
| 263 | + assertTrue("Valid not defined as a boolean", response.getResult().getValid() == true || response.getResult().getValid() == false); |
| 264 | + } |
| 265 | + |
| 266 | + @Test(expected = ErrorWithRequestException.class) |
| 267 | + public void testMfaVerifyInvalidPhoneNumber() throws Exception { |
| 268 | + String accountId = System.getenv("BW_ACCOUNT_ID"); |
| 269 | + String to = "+1invalid"; |
| 270 | + String applicationId = System.getenv("MFA_VOICE_APPLICATION_ID"); |
| 271 | + String scope = "scope"; |
| 272 | + String code = "123456"; |
| 273 | + int expirationTimeInMinutes = 3; |
| 274 | + |
| 275 | + TwoFactorVerifyRequestSchema body = new TwoFactorVerifyRequestSchema(); |
| 276 | + body.setTo(to); |
| 277 | + body.setApplicationId(applicationId); |
| 278 | + body.setScope(scope); |
| 279 | + body.setCode(code); |
| 280 | + body.setExpirationTimeInMinutes(expirationTimeInMinutes); |
| 281 | + |
| 282 | + mfaController.createVerifyTwoFactor(accountId, body); |
| 283 | + } |
| 284 | + |
| 285 | + /* |
| 286 | + * Taken from https://mkyong.com/java/how-to-convert-inputstream-to-string-in-java/ |
| 287 | + */ |
| 288 | + private static String convertInputStreamToString(InputStream inputStream) throws IOException { |
| 289 | + final char[] buffer = new char[8192]; |
| 290 | + final StringBuilder result = new StringBuilder(); |
| 291 | + |
| 292 | + // InputStream -> Reader |
| 293 | + try (Reader reader = new InputStreamReader(inputStream)) { |
| 294 | + int charsRead; |
| 295 | + while ((charsRead = reader.read(buffer, 0, buffer.length)) > 0) { |
| 296 | + result.append(buffer, 0, charsRead); |
| 297 | + } |
| 298 | + } |
| 299 | + |
| 300 | + return result.toString(); |
| 301 | + } |
| 302 | +} |
0 commit comments