Skip to content

Commit

Permalink
Dx 1967 sdk tests (#27)
Browse files Browse the repository at this point in the history
* DX-1967 added texting tests

* Added media test

* Added all api tests

* updated workflow

* apparently mvn install runs tests

* missed a secret

* Added bxml tests

* added missing packages

* added cron job
  • Loading branch information
jmulford-bw authored Apr 22, 2021
1 parent 5d20cfc commit a35b584
Show file tree
Hide file tree
Showing 5 changed files with 792 additions and 2 deletions.
17 changes: 15 additions & 2 deletions .github/workflows/on-pull-reqeust.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ on:
pull_request:
branches:
- main
schedule:
- cron: "0 4 * * *"

jobs:
maven-install-java:
name: Maven Install
validate-sdk:
name: Validate SDK
runs-on: ubuntu-latest
steps:
- name: Checkout
Expand All @@ -16,3 +18,14 @@ jobs:
uses: ./.github/actions/maven-install
env:
GPG_KEY_PASSPHRASE: ${{ secrets.PRIVATE_KEY_PASSPHRASE }}
BW_ACCOUNT_ID: ${{ secrets.BW_ACCOUNT_ID }}
BW_PASSWORD: ${{ secrets.BW_PASSWORD }}
BW_USERNAME: ${{ secrets.BW_USERNAME }}
MESSAGING_APPLICATION_ID: ${{ secrets.MESSAGING_APPLICATION_ID }}
MFA_MESSAGING_APPLICATION_ID: ${{ secrets.MFA_MESSAGING_APPLICATION_ID }}
MFA_VOICE_APPLICATION_ID: ${{ secrets.MFA_VOICE_APPLICATION_ID }}
PHONE_NUMBER_INBOUND: ${{ secrets.PHONE_NUMBER_INBOUND }}
PHONE_NUMBER_MFA: ${{ secrets.PHONE_NUMBER_MFA }}
PHONE_NUMBER_OUTBOUND: ${{ secrets.PHONE_NUMBER_OUTBOUND }}
VOICE_APPLICATION_ID: ${{ secrets.VOICE_APPLICATION_ID }}
VOICE_CALLBACK_URL: ${{ secrets.VOICE_CALLBACK_URL }}
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,16 @@
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
Expand Down
302 changes: 302 additions & 0 deletions src/test/java/com/bandwidth/ApiTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,302 @@
package com.bandwidth;

import org.junit.Test;
import org.junit.Before;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.Reader;

import com.bandwidth.*;
import com.bandwidth.Environment;
import com.bandwidth.voice.models.*;
import com.bandwidth.voice.controllers.*;
import com.bandwidth.voice.exceptions.*;
import com.bandwidth.messaging.models.*;
import com.bandwidth.messaging.controllers.*;
import com.bandwidth.messaging.exceptions.*;
import com.bandwidth.twofactorauth.models.*;
import com.bandwidth.twofactorauth.controllers.*;
import com.bandwidth.twofactorauth.exceptions.*;
import com.bandwidth.exceptions.ApiException;
import com.bandwidth.http.response.ApiResponse;
import com.bandwidth.utilities.FileWrapper;

/**
* Integration tests for API interactions
*/
public class ApiTest {

private BandwidthClient client;
private com.bandwidth.messaging.controllers.APIController messagingController;
private com.bandwidth.voice.controllers.APIController voiceController;
private com.bandwidth.twofactorauth.controllers.MFAController mfaController;

@Before
public void init() {
this.client = new BandwidthClient.Builder()
.messagingBasicAuthCredentials(System.getenv("BW_USERNAME"), System.getenv("BW_PASSWORD"))
.voiceBasicAuthCredentials(System.getenv("BW_USERNAME"), System.getenv("BW_PASSWORD"))
.twoFactorAuthBasicAuthCredentials(System.getenv("BW_USERNAME"), System.getenv("BW_PASSWORD"))
.build();
this.messagingController = client.getMessagingClient().getAPIController();
this.voiceController = client.getVoiceClient().getAPIController();
this.mfaController = client.getTwoFactorAuthClient().getMFAController();
}

@Test
public void testCreateMessage() throws Exception {
String accountId = System.getenv("BW_ACCOUNT_ID");
String to = System.getenv("PHONE_NUMBER_INBOUND");
ArrayList<String> toNumbers = new ArrayList<String>();
toNumbers.add(to);
String from = System.getenv("PHONE_NUMBER_OUTBOUND");
String applicationId = System.getenv("MESSAGING_APPLICATION_ID");
String text = "Java Test";

MessageRequest body = new MessageRequest();
body.setTo(toNumbers);
body.setFrom(from);
body.setText(text);
body.setApplicationId(applicationId);

ApiResponse<BandwidthMessage> response = messagingController.createMessage(accountId, body);
assertEquals("Application ID not equal", applicationId, response.getResult().getApplicationId());
assertEquals("To phone number not equal", to, response.getResult().getTo().get(0));
assertEquals("From phone number not equal", from, response.getResult().getFrom());
assertEquals("Text not equal", text, response.getResult().getText());
}

@Test(expected = MessagingException.class)
public void testCreateMessageInvalidPhoneNumber() throws Exception {
String accountId = System.getenv("BW_ACCOUNT_ID");
String to = "+1invalid";
ArrayList<String> toNumbers = new ArrayList<String>();
toNumbers.add(to);
String from = System.getenv("PHONE_NUMBER_OUTBOUND");
String applicationId = System.getenv("MESSAGING_APPLICATION_ID");
String text = "Java Test";

MessageRequest body = new MessageRequest();
body.setTo(toNumbers);
body.setFrom(from);
body.setText(text);
body.setApplicationId(applicationId);

messagingController.createMessage(accountId, body);
}

@Test
public void testUploadDownloadMedia() throws Exception {
String fileName = "src/test/resources/mediaUpload.png";
File file = new File(fileName);
FileInputStream inStream = new FileInputStream(fileName);
String fileContents = ApiTest.convertInputStreamToString(inStream);
FileWrapper body = new FileWrapper(file, "image/png");
String accountId = System.getenv("BW_ACCOUNT_ID");
String mediaId = "java-media-test";
String fileType = "image/png";
String cache = "no-cache";

messagingController.uploadMedia(accountId, mediaId, fileContents.length(), body, fileType, cache);

ApiResponse<InputStream> response = messagingController.getMedia(accountId, mediaId);
String resultString = ApiTest.convertInputStreamToString(response.getResult());

assertEquals("Media download not equal to media upload", fileContents, resultString);
}

@Test
public void testCreateCallAndGetCallState() throws Exception {
String accountId = System.getenv("BW_ACCOUNT_ID");
String to = System.getenv("PHONE_NUMBER_INBOUND");
String from = System.getenv("PHONE_NUMBER_OUTBOUND");
String applicationId = System.getenv("VOICE_APPLICATION_ID");
String answerUrl = System.getenv("VOICE_CALLBACK_URL");

ApiCreateCallRequest body = new ApiCreateCallRequest();
body.setTo(to);
body.setFrom(from);
body.setApplicationId(applicationId);
body.setAnswerUrl(answerUrl);

ApiResponse<ApiCallResponse> createCallResponse = voiceController.createCall(accountId, body);
assertEquals("Application ID for create call not equal", applicationId, createCallResponse.getResult().getApplicationId());
assertEquals("To phone number for create call not equal", to, createCallResponse.getResult().getTo());
assertEquals("From phone number for create call not equal", from, createCallResponse.getResult().getFrom());

//get call state
String callId = createCallResponse.getResult().getCallId();
ApiResponse<ApiCallStateResponse> callStateResponse = voiceController.getCallState(accountId, callId);
assertEquals("Application ID for call state not equal", applicationId, callStateResponse.getResult().getApplicationId());
assertEquals("To phone number for call state not equal", to, callStateResponse.getResult().getTo());
assertEquals("From phone number for call state not equal", from, callStateResponse.getResult().getFrom());
assertEquals("Call ID not equal", callId, callStateResponse.getResult().getCallId());
}

@Test(expected = ApiErrorResponseException.class)
public void testCreateCallInvalidPhoneNumber() throws Exception {
String accountId = System.getenv("BW_ACCOUNT_ID");
String to = "+1invalid";
String from = System.getenv("PHONE_NUMBER_OUTBOUND");
String applicationId = System.getenv("VOICE_APPLICATION_ID");
String answerUrl = System.getenv("VOICE_CALLBACK_URL");

ApiCreateCallRequest body = new ApiCreateCallRequest();
body.setTo(to);
body.setFrom(from);
body.setApplicationId(applicationId);
body.setAnswerUrl(answerUrl);

voiceController.createCall(accountId, body);
}

@Test
public void testMfaMessaging() throws Exception {
String accountId = System.getenv("BW_ACCOUNT_ID");
String to = System.getenv("PHONE_NUMBER_INBOUND");
String from = System.getenv("PHONE_NUMBER_MFA");
String applicationId = System.getenv("MFA_MESSAGING_APPLICATION_ID");
String scope = "scope";
int digits = 6;
String message = "Your temporary {NAME} {SCOPE} code is {CODE}";

TwoFactorCodeRequestSchema body = new TwoFactorCodeRequestSchema();
body.setTo(to);
body.setFrom(from);
body.setApplicationId(applicationId);
body.setScope(scope);
body.setDigits(digits);
body.setMessage(message);

ApiResponse<TwoFactorMessagingResponse> response = mfaController.createMessagingTwoFactor(accountId, body);
assertTrue("Message ID not defined", response.getResult().getMessageId().length() > 0);
}

@Test
public void testMfaVoice() throws Exception {
String accountId = System.getenv("BW_ACCOUNT_ID");
String to = System.getenv("PHONE_NUMBER_INBOUND");
String from = System.getenv("PHONE_NUMBER_MFA");
String applicationId = System.getenv("MFA_VOICE_APPLICATION_ID");
String scope = "scope";
int digits = 6;
String message = "Your temporary {NAME} {SCOPE} code is {CODE}";

TwoFactorCodeRequestSchema body = new TwoFactorCodeRequestSchema();
body.setTo(to);
body.setFrom(from);
body.setApplicationId(applicationId);
body.setScope(scope);
body.setDigits(digits);
body.setMessage(message);

ApiResponse<TwoFactorVoiceResponse> response = mfaController.createVoiceTwoFactor(accountId, body);
assertTrue("Call ID not defined", response.getResult().getCallId().length() > 0);
}

@Test(expected = ErrorWithRequestException.class)
public void testMfaMessagingInvalidPhoneNumber() throws Exception {
String accountId = System.getenv("BW_ACCOUNT_ID");
String to = "+1invalid";
String from = System.getenv("PHONE_NUMBER_MFA");
String applicationId = System.getenv("MFA_MESSAGING_APPLICATION_ID");
String scope = "scope";
int digits = 6;
String message = "Your temporary {NAME} {SCOPE} code is {CODE}";

TwoFactorCodeRequestSchema body = new TwoFactorCodeRequestSchema();
body.setTo(to);
body.setFrom(from);
body.setApplicationId(applicationId);
body.setScope(scope);
body.setDigits(digits);
body.setMessage(message);

mfaController.createMessagingTwoFactor(accountId, body);
}

@Test(expected = ErrorWithRequestException.class)
public void testMfaVoiceInvalidPhoneNumber() throws Exception {
String accountId = System.getenv("BW_ACCOUNT_ID");
String to = "+1invalid";
String from = System.getenv("PHONE_NUMBER_MFA");
String applicationId = System.getenv("MFA_VOICE_APPLICATION_ID");
String scope = "scope";
int digits = 6;
String message = "Your temporary {NAME} {SCOPE} code is {CODE}";

TwoFactorCodeRequestSchema body = new TwoFactorCodeRequestSchema();
body.setTo(to);
body.setFrom(from);
body.setApplicationId(applicationId);
body.setScope(scope);
body.setDigits(digits);
body.setMessage(message);

mfaController.createVoiceTwoFactor(accountId, body);
}

@Test
public void testMfaVerify() throws Exception {
String accountId = System.getenv("BW_ACCOUNT_ID");
String to = System.getenv("PHONE_NUMBER_INBOUND");
String applicationId = System.getenv("MFA_VOICE_APPLICATION_ID");
String scope = "scope";
String code = "123456";
int expirationTimeInMinutes = 3;

TwoFactorVerifyRequestSchema body = new TwoFactorVerifyRequestSchema();
body.setTo(to);
body.setApplicationId(applicationId);
body.setScope(scope);
body.setCode(code);
body.setExpirationTimeInMinutes(expirationTimeInMinutes);

ApiResponse<TwoFactorVerifyCodeResponse> response = mfaController.createVerifyTwoFactor(accountId, body);
assertTrue("Valid not defined as a boolean", response.getResult().getValid() == true || response.getResult().getValid() == false);
}

@Test(expected = ErrorWithRequestException.class)
public void testMfaVerifyInvalidPhoneNumber() throws Exception {
String accountId = System.getenv("BW_ACCOUNT_ID");
String to = "+1invalid";
String applicationId = System.getenv("MFA_VOICE_APPLICATION_ID");
String scope = "scope";
String code = "123456";
int expirationTimeInMinutes = 3;

TwoFactorVerifyRequestSchema body = new TwoFactorVerifyRequestSchema();
body.setTo(to);
body.setApplicationId(applicationId);
body.setScope(scope);
body.setCode(code);
body.setExpirationTimeInMinutes(expirationTimeInMinutes);

mfaController.createVerifyTwoFactor(accountId, body);
}

/*
* Taken from https://mkyong.com/java/how-to-convert-inputstream-to-string-in-java/
*/
private static String convertInputStreamToString(InputStream inputStream) throws IOException {
final char[] buffer = new char[8192];
final StringBuilder result = new StringBuilder();

// InputStream -> Reader
try (Reader reader = new InputStreamReader(inputStream)) {
int charsRead;
while ((charsRead = reader.read(buffer, 0, buffer.length)) > 0) {
result.append(buffer, 0, charsRead);
}
}

return result.toString();
}
}
Loading

0 comments on commit a35b584

Please sign in to comment.