|
| 1 | +package com.bandwidth.sdk.smoke; |
| 2 | + |
| 3 | +import com.bandwidth.sdk.api.CallsApi; |
| 4 | +import com.bandwidth.sdk.api.ConferencesApi; |
| 5 | +import com.bandwidth.sdk.ApiResponse; |
| 6 | +import com.bandwidth.sdk.ApiClient; |
| 7 | +import com.bandwidth.sdk.auth.HttpBasicAuth; |
| 8 | +import com.bandwidth.sdk.Configuration; |
| 9 | +import com.bandwidth.sdk.model.ConferenceRecordingMetadata; |
| 10 | +import com.bandwidth.sdk.model.ConferenceStateEnum; |
| 11 | +import com.bandwidth.sdk.model.CreateCall; |
| 12 | +import com.bandwidth.sdk.model.CreateCallResponse; |
| 13 | +import com.bandwidth.sdk.model.FileFormatEnum; |
| 14 | +import com.bandwidth.sdk.model.Conference; |
| 15 | +import com.bandwidth.sdk.model.ConferenceMember; |
| 16 | +import com.bandwidth.sdk.model.RedirectMethodEnum; |
| 17 | +import com.bandwidth.sdk.model.UpdateConference; |
| 18 | +import com.bandwidth.sdk.model.UpdateConferenceMember; |
| 19 | +import com.bandwidth.sdk.utils.MantecaStatusResponse; |
| 20 | + |
| 21 | +import com.google.gson.Gson; |
| 22 | + |
| 23 | +import okhttp3.Call; |
| 24 | +import okhttp3.MediaType; |
| 25 | +import okhttp3.OkHttpClient; |
| 26 | +import okhttp3.Request; |
| 27 | +import okhttp3.RequestBody; |
| 28 | +import okhttp3.Response; |
| 29 | + |
| 30 | +import org.junit.jupiter.api.BeforeAll; |
| 31 | +import org.junit.jupiter.api.Disabled; |
| 32 | +import org.junit.jupiter.api.MethodOrderer; |
| 33 | +import org.junit.jupiter.api.Order; |
| 34 | +import org.junit.jupiter.api.AfterAll; |
| 35 | +import org.junit.jupiter.api.Test; |
| 36 | +import org.junit.jupiter.api.TestInstance; |
| 37 | +import org.junit.jupiter.api.TestMethodOrder; |
| 38 | + |
| 39 | +import java.io.File; |
| 40 | +import java.io.IOException; |
| 41 | +import java.net.URI; |
| 42 | +import java.net.URISyntaxException; |
| 43 | +import java.util.List; |
| 44 | +import java.util.concurrent.TimeUnit; |
| 45 | + |
| 46 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 47 | +import static org.hamcrest.Matchers.is; |
| 48 | +import static com.bandwidth.sdk.utils.TestingEnvironmentVariables.*; |
| 49 | +import static com.bandwidth.sdk.utils.CallCleanup.Cleanup; |
| 50 | + |
| 51 | +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) |
| 52 | +@TestInstance(TestInstance.Lifecycle.PER_CLASS) |
| 53 | +public class ConferencesApiTest { |
| 54 | + public ApiClient defaultClient = Configuration.getDefaultApiClient(); |
| 55 | + public HttpBasicAuth Basic = (HttpBasicAuth) defaultClient.getAuthentication("Basic"); |
| 56 | + public final CallsApi callsApi = new CallsApi(defaultClient); |
| 57 | + public final ConferencesApi conferencesApi = new ConferencesApi(defaultClient); |
| 58 | + |
| 59 | + private static final OkHttpClient mantecaClient = new OkHttpClient(); |
| 60 | + public static final MediaType jsonMediaType = MediaType.get("application/json; charset=utf-8"); |
| 61 | + |
| 62 | + private static String testId; |
| 63 | + private static String callId; |
| 64 | + private static String conferenceId; |
| 65 | + private static URI answerUrl; |
| 66 | + private static URI conferenceRedirectUrl; |
| 67 | + private static String updateRecordingBxml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Bxml><StartRecording/><SpeakSentence locale=\"en_US\" gender=\"female\" voice=\"susan\">This should be a conference recording.</SpeakSentence><StopRecording/></Bxml>"; |
| 68 | + private static int TEST_SLEEP = 6; |
| 69 | + private static int MAX_RETRIES = 60; |
| 70 | + |
| 71 | + @BeforeAll |
| 72 | + public static void setUpBeforeClass() throws URISyntaxException { |
| 73 | + answerUrl = new URI(MANTECA_BASE_URL + "/bxml/joinConferencePause"); |
| 74 | + conferenceRedirectUrl = new URI(MANTECA_BASE_URL + "/bxml/pause"); |
| 75 | + } |
| 76 | + |
| 77 | + @AfterAll |
| 78 | + public void tearDownAfterClass() throws Exception { |
| 79 | + TimeUnit.SECONDS.sleep(TEST_SLEEP); |
| 80 | + Cleanup(this, callId); |
| 81 | + } |
| 82 | + |
| 83 | + static final String constructMantecaJsonBody(String os, String language) { |
| 84 | + return "{\"os\": \"" + os + "\", \"language\":\"JAVA" + JAVA_VERSION + "\", \"type\":\"conference\"}"; |
| 85 | + } |
| 86 | + |
| 87 | + public void validateRecording(ConferenceRecordingMetadata recording, String conferenceId) { |
| 88 | + |
| 89 | + } |
| 90 | + |
| 91 | + public Boolean getTestRecordedStatus(String mantecaTestId) throws Exception { |
| 92 | + try { |
| 93 | + // Setup the test with Manteca |
| 94 | + Request mantecaStatusRequest = new Request.Builder() |
| 95 | + .url(MANTECA_STATUS_URL + mantecaTestId) |
| 96 | + .build(); |
| 97 | + Call mantecaStatusApiCall = mantecaClient.newCall(mantecaStatusRequest); |
| 98 | + Response mantecaStatusResponse = mantecaStatusApiCall.execute(); |
| 99 | + |
| 100 | + Gson gson = new Gson(); |
| 101 | + MantecaStatusResponse mantecaStatus = gson.fromJson( |
| 102 | + mantecaStatusResponse.peekBody(2048).string(), |
| 103 | + MantecaStatusResponse.class); |
| 104 | + if (mantecaStatusResponse.isSuccessful()) { |
| 105 | + return mantecaStatus.callRecorded; |
| 106 | + } else { |
| 107 | + System.out.println(mantecaStatusResponse.body().string()); |
| 108 | + throw new Exception( |
| 109 | + "Received HTTP " + String.valueOf(mantecaStatusResponse.code()) |
| 110 | + + " status code from Manteca"); |
| 111 | + } |
| 112 | + } catch (IOException e) { |
| 113 | + System.out.println(e.toString()); |
| 114 | + throw new Exception("Failed to get test status from Manteca, aborting test run :("); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + @Order(1) |
| 120 | + public void testCreateAndFetchConference() throws Exception { |
| 121 | + Basic.setUsername(BW_USERNAME); |
| 122 | + Basic.setPassword(BW_PASSWORD); |
| 123 | + |
| 124 | + String mantecaJsonBody = constructMantecaJsonBody(OPERATING_SYSTEM, JAVA_VERSION); |
| 125 | + RequestBody mantecaRequestBody = RequestBody.create(mantecaJsonBody, jsonMediaType); |
| 126 | + |
| 127 | + try { |
| 128 | + // Setup the test with Manteca |
| 129 | + Request mantecaRequest = new Request.Builder() |
| 130 | + .url(MANTECA_BASE_URL + "/tests") |
| 131 | + .post(mantecaRequestBody) |
| 132 | + .build(); |
| 133 | + Call mantecaApiCall = mantecaClient.newCall(mantecaRequest); |
| 134 | + testId = mantecaApiCall.execute().body().string(); |
| 135 | + } catch (IOException e) { |
| 136 | + System.out.println(e.toString()); |
| 137 | + throw new Exception("Failed to initialize conference tests with Manteca, aborting test run :("); |
| 138 | + } |
| 139 | + |
| 140 | + CreateCall conferenceCallBody = new CreateCall(); |
| 141 | + conferenceCallBody.setTo(MANTECA_IDLE_NUMBER); |
| 142 | + conferenceCallBody.setFrom(MANTECA_ACTIVE_NUMBER); |
| 143 | + conferenceCallBody.setApplicationId(MANTECA_APPLICATION_ID); |
| 144 | + conferenceCallBody.setAnswerUrl(answerUrl); |
| 145 | + conferenceCallBody.setTag(testId); |
| 146 | + |
| 147 | + ApiResponse<CreateCallResponse> createCallResponse = callsApi.createCallWithHttpInfo(BW_ACCOUNT_ID, |
| 148 | + conferenceCallBody); |
| 149 | + |
| 150 | + assertThat(createCallResponse.getStatusCode(), is(201)); |
| 151 | + callId = createCallResponse.getData().getCallId(); |
| 152 | + |
| 153 | + // TODO: Remove after successful test in GHA runners |
| 154 | + System.out.println("TestId: " + testId); |
| 155 | + System.out.println("CallId: " + callId); |
| 156 | + |
| 157 | + TimeUnit.SECONDS.sleep(TEST_SLEEP); |
| 158 | + |
| 159 | + ApiResponse<List<Conference>> listConferencesResponse = conferencesApi |
| 160 | + .listConferencesWithHttpInfo(BW_ACCOUNT_ID, testId, null, null, null, null); |
| 161 | + assertThat(listConferencesResponse.getStatusCode(), is(200)); |
| 162 | + |
| 163 | + conferenceId = listConferencesResponse.getData().get(0).getId(); |
| 164 | + |
| 165 | + ApiResponse<Conference> getConferenceResponse = conferencesApi.getConferenceWithHttpInfo(BW_ACCOUNT_ID, |
| 166 | + conferenceId); |
| 167 | + assertThat(getConferenceResponse.getStatusCode(), is(200)); |
| 168 | + |
| 169 | + } |
| 170 | + |
| 171 | + @Test |
| 172 | + @Order(2) |
| 173 | + public void testConferenceAndMembers() throws Exception { |
| 174 | + Basic.setUsername(BW_USERNAME); |
| 175 | + Basic.setPassword(BW_PASSWORD); |
| 176 | + |
| 177 | + ApiResponse<ConferenceMember> listConferenceMembersResponse = conferencesApi |
| 178 | + .getConferenceMemberWithHttpInfo(BW_ACCOUNT_ID, conferenceId, callId); |
| 179 | + assertThat(listConferenceMembersResponse.getStatusCode(), is(200)); |
| 180 | + |
| 181 | + UpdateConferenceMember updateMember = new UpdateConferenceMember(); |
| 182 | + updateMember.setMute(false); |
| 183 | + |
| 184 | + ApiResponse<Void> updateConferenceMemberResponse = conferencesApi |
| 185 | + .updateConferenceMemberWithHttpInfo(BW_ACCOUNT_ID, conferenceId, callId, updateMember); |
| 186 | + assertThat(updateConferenceMemberResponse.getStatusCode(), is(204)); |
| 187 | + |
| 188 | + UpdateConference updateConference = new UpdateConference(); |
| 189 | + updateConference.setStatus(ConferenceStateEnum.ACTIVE); |
| 190 | + updateConference.setRedirectUrl(conferenceRedirectUrl); |
| 191 | + updateConference.setRedirectMethod(RedirectMethodEnum.POST); |
| 192 | + updateConference.setUsername("myUsername"); |
| 193 | + updateConference.setPassword("myPassword1!"); |
| 194 | + updateConference.setRedirectFallbackUrl(conferenceRedirectUrl); |
| 195 | + updateConference.setRedirectFallbackMethod(RedirectMethodEnum.POST); |
| 196 | + updateConference.setFallbackUsername("myUsername"); |
| 197 | + updateConference.setFallbackPassword("myPassword1!"); |
| 198 | + |
| 199 | + ApiResponse<Void> updateConferenceResponse = conferencesApi.updateConferenceWithHttpInfo(BW_ACCOUNT_ID, |
| 200 | + conferenceId, updateConference); |
| 201 | + assertThat(updateConferenceResponse.getStatusCode(), is(204)); |
| 202 | + |
| 203 | + ApiResponse<Void> updateConferenceBxmlResponse = conferencesApi.updateConferenceBxmlWithHttpInfo( |
| 204 | + BW_ACCOUNT_ID, |
| 205 | + conferenceId, updateRecordingBxml); |
| 206 | + assertThat(updateConferenceBxmlResponse.getStatusCode(), is(204)); |
| 207 | + } |
| 208 | + |
| 209 | + @Test |
| 210 | + @Order(3) |
| 211 | + @Disabled // issues with PV API, can re-enable after fixed |
| 212 | + public void testConferenceRecordings() throws Exception { |
| 213 | + Basic.setUsername(BW_USERNAME); |
| 214 | + Basic.setPassword(BW_PASSWORD); |
| 215 | + |
| 216 | + Boolean testRecordingStatus = false; |
| 217 | + for (int i = 0; i < MAX_RETRIES; i++) { |
| 218 | + TimeUnit.SECONDS.sleep(TEST_SLEEP); |
| 219 | + testRecordingStatus = getTestRecordedStatus(testId); |
| 220 | + } |
| 221 | + assertThat(testRecordingStatus, is(true)); |
| 222 | + |
| 223 | + ApiResponse<List<ConferenceRecordingMetadata>> listConferenceRecordingsResponse = conferencesApi |
| 224 | + .listConferenceRecordingsWithHttpInfo(BW_ACCOUNT_ID, conferenceId); |
| 225 | + assertThat(listConferenceRecordingsResponse.getStatusCode(), is(200)); |
| 226 | + |
| 227 | + ConferenceRecordingMetadata conferenceRecording = listConferenceRecordingsResponse.getData().get(0); |
| 228 | + |
| 229 | + ApiResponse<ConferenceRecordingMetadata> conferenceRecordingMetadataResponse = conferencesApi |
| 230 | + .getConferenceRecordingWithHttpInfo(BW_ACCOUNT_ID, conferenceId, |
| 231 | + conferenceRecording.getRecordingId()); |
| 232 | + assertThat(conferenceRecordingMetadataResponse.getStatusCode(), is(200)); |
| 233 | + assertThat(conferenceRecordingMetadataResponse.getData().getStatus(), is("complete")); |
| 234 | + assertThat(conferenceRecordingMetadataResponse.getData().getFileFormat(), is(FileFormatEnum.WAV)); |
| 235 | + |
| 236 | + ApiResponse<File> downloadRecordingResponse = conferencesApi.downloadConferenceRecordingWithHttpInfo( |
| 237 | + BW_ACCOUNT_ID, conferenceId, |
| 238 | + conferenceRecording.getRecordingId()); |
| 239 | + assertThat(downloadRecordingResponse.getStatusCode(), is(200)); |
| 240 | + } |
| 241 | +} |
0 commit comments