|
| 1 | +package org.openapitools.client.api; |
| 2 | + |
| 3 | +import org.openapitools.client.ApiResponse; |
| 4 | +import org.openapitools.client.ApiException; |
| 5 | +import org.openapitools.client.ApiClient; |
| 6 | +import org.openapitools.client.auth.HttpBasicAuth; |
| 7 | +import org.openapitools.client.Configuration; |
| 8 | +import org.openapitools.client.model.LookupRequest; |
| 9 | +import org.openapitools.client.model.LookupStatus; |
| 10 | +import org.openapitools.client.model.LookupStatusEnum; |
| 11 | +import org.openapitools.client.model.CreateLookupResponse; |
| 12 | +import org.openapitools.client.model.LookupResult; |
| 13 | +import org.junit.jupiter.api.Assertions; |
| 14 | +import org.junit.jupiter.api.Disabled; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | + |
| 17 | +import java.util.concurrent.TimeUnit; |
| 18 | + |
| 19 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 20 | +import static org.hamcrest.CoreMatchers.instanceOf; |
| 21 | +import static org.hamcrest.Matchers.hasProperty; |
| 22 | +import static org.hamcrest.Matchers.is; |
| 23 | +import static org.hamcrest.Matchers.matchesRegex; |
| 24 | + |
| 25 | +import static org.openapitools.client.utils.TestingEnvironmentVariables.*; |
| 26 | + |
| 27 | +public class PhoneNumberLookupApiTest { |
| 28 | + |
| 29 | + ApiClient defaultClient = Configuration.getDefaultApiClient(); |
| 30 | + HttpBasicAuth Basic = (HttpBasicAuth) defaultClient.getAuthentication("Basic"); |
| 31 | + private final PhoneNumberLookupApi api = new PhoneNumberLookupApi(defaultClient); |
| 32 | + LookupRequest lookupRequest = new LookupRequest(); |
| 33 | + |
| 34 | + /** |
| 35 | + * Validate a LookupResult object |
| 36 | + * |
| 37 | + * @param result A LookupResult object |
| 38 | + * @param phoneNumber A String phone number in E164 format to check against the |
| 39 | + * E164 format value in the result |
| 40 | + */ |
| 41 | + private void validateResult(LookupResult result, String phoneNumber) { |
| 42 | + if (result.getMobileCountryCode() != null || result.getMobileNetworkCode() != null) { |
| 43 | + assertThat(result.getMobileCountryCode(), instanceOf(String.class)); |
| 44 | + assertThat(result.getMobileNetworkCode(), instanceOf(String.class)); |
| 45 | + } |
| 46 | + |
| 47 | + assertThat(result, hasProperty("responseCode")); |
| 48 | + assertThat(result, hasProperty("message")); |
| 49 | + assertThat(result, hasProperty("e164Format")); |
| 50 | + assertThat(result, hasProperty("formatted")); |
| 51 | + assertThat(result, hasProperty("country")); |
| 52 | + assertThat(result, hasProperty("lineType")); |
| 53 | + assertThat(result, hasProperty("lineProvider")); |
| 54 | + assertThat(result, hasProperty("mobileCountryCode")); |
| 55 | + assertThat(result, hasProperty("mobileNetworkCode")); |
| 56 | + |
| 57 | + assertThat(result.getE164Format(), is(phoneNumber)); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Poll for a completed TN Lookup order |
| 62 | + * |
| 63 | + * @param requestId String requestId to poll for |
| 64 | + * @return the completed lookup status |
| 65 | + * @throws Exception If status was not complete after 5 attempts |
| 66 | + */ |
| 67 | + private LookupStatus pollLookupStatus(String requestId) throws Exception { |
| 68 | + int attempt = 1; |
| 69 | + LookupStatus lookupStatus = this.api.getLookupStatus(BW_ACCOUNT_ID, requestId); |
| 70 | + |
| 71 | + do { |
| 72 | + try { |
| 73 | + lookupStatus = this.api.getLookupStatus(BW_ACCOUNT_ID, requestId); |
| 74 | + TimeUnit.SECONDS.sleep(2); |
| 75 | + attempt += 1; |
| 76 | + } catch (ApiException e) { |
| 77 | + throw new Exception( |
| 78 | + "Polling for TnLookup order status failed. \nStatus Code: " + String.valueOf(e.getCode()) |
| 79 | + + "\nMessage: " + e.getMessage()); |
| 80 | + } |
| 81 | + } while (attempt <= 5 && lookupStatus.getStatus() != LookupStatusEnum.COMPLETE); |
| 82 | + return lookupStatus; |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void successfulPhoneNumberLookup() throws Exception, ApiException { |
| 87 | + Basic.setUsername(BW_USERNAME); |
| 88 | + Basic.setPassword(BW_PASSWORD); |
| 89 | + |
| 90 | + lookupRequest.addTnsItem(BW_NUMBER); |
| 91 | + lookupRequest.addTnsItem(VZW_NUMBER); |
| 92 | + lookupRequest.addTnsItem(ATT_NUMBER); |
| 93 | + lookupRequest.addTnsItem(T_MOBILE_NUMBER); |
| 94 | + |
| 95 | + // Create the lookup request and validate the response |
| 96 | + ApiResponse<CreateLookupResponse> response = api.createLookupWithHttpInfo(BW_ACCOUNT_ID, lookupRequest); |
| 97 | + CreateLookupResponse lookupResponse = response.getData(); |
| 98 | + assertThat(response.getStatusCode(), is(202)); |
| 99 | + assertThat(response.getData(), instanceOf(CreateLookupResponse.class)); |
| 100 | + assertThat(lookupResponse.getStatus(), is(LookupStatusEnum.IN_PROGRESS)); |
| 101 | + assertThat(lookupResponse.getRequestId(), instanceOf(String.class)); |
| 102 | + assertThat(lookupResponse.getRequestId(), |
| 103 | + matchesRegex("^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$")); |
| 104 | + |
| 105 | + // Test GET LookupStatus Response |
| 106 | + ApiResponse<LookupStatus> lookupStatusResponse = api.getLookupStatusWithHttpInfo(BW_ACCOUNT_ID, |
| 107 | + lookupResponse.getRequestId()); |
| 108 | + assertThat(lookupStatusResponse.getStatusCode(), is(200)); |
| 109 | + |
| 110 | + LookupStatus completedLookup = null; |
| 111 | + try { |
| 112 | + completedLookup = pollLookupStatus(lookupStatusResponse.getData().getRequestId()); |
| 113 | + } catch (Exception e) { |
| 114 | + throw e; |
| 115 | + } |
| 116 | + assertThat(lookupStatusResponse.getData().getRequestId(), is(completedLookup.getRequestId())); |
| 117 | + |
| 118 | + for (LookupResult result : completedLookup.getResult()) { |
| 119 | + assertThat(result, instanceOf(LookupResult.class)); |
| 120 | + } |
| 121 | + |
| 122 | + LookupResult bwLookupResult = completedLookup.getResult().get(0); |
| 123 | + validateResult(bwLookupResult, BW_NUMBER); |
| 124 | + |
| 125 | + LookupResult vzwLookupResult = completedLookup.getResult().get(1); |
| 126 | + validateResult(vzwLookupResult, VZW_NUMBER); |
| 127 | + |
| 128 | + LookupResult attLookupResult = completedLookup.getResult().get(2); |
| 129 | + validateResult(attLookupResult, ATT_NUMBER); |
| 130 | + |
| 131 | + LookupResult tMobileLookupResult = completedLookup.getResult().get(3); |
| 132 | + validateResult(tMobileLookupResult, T_MOBILE_NUMBER); |
| 133 | + |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + public void failedPhoneNumberLookup() throws ApiException { |
| 138 | + Basic.setUsername(BW_USERNAME); |
| 139 | + Basic.setPassword(BW_PASSWORD); |
| 140 | + |
| 141 | + lookupRequest.addTnsItem("not a number"); |
| 142 | + |
| 143 | + ApiException exception = Assertions.assertThrows(ApiException.class, |
| 144 | + () -> api.createLookup(BW_ACCOUNT_ID, lookupRequest)); |
| 145 | + assertThat(exception.getCode(), is(400)); |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + public void duplicatePhoneNumberLookup() throws ApiException { |
| 150 | + Basic.setUsername(BW_USERNAME); |
| 151 | + Basic.setPassword(BW_PASSWORD); |
| 152 | + |
| 153 | + lookupRequest.addTnsItem(BW_NUMBER); |
| 154 | + lookupRequest.addTnsItem(BW_NUMBER); |
| 155 | + |
| 156 | + ApiException exception = Assertions.assertThrows(ApiException.class, |
| 157 | + () -> api.createLookup(BW_ACCOUNT_ID, lookupRequest)); |
| 158 | + assertThat(exception.getCode(), is(400)); |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + public void unauthorizedRequest() throws ApiException { |
| 163 | + Basic.setUsername("bad_username"); |
| 164 | + Basic.setPassword("bad_password"); |
| 165 | + |
| 166 | + lookupRequest.addTnsItem(BW_NUMBER); |
| 167 | + |
| 168 | + ApiException exception = Assertions.assertThrows(ApiException.class, |
| 169 | + () -> api.createLookup(BW_ACCOUNT_ID, lookupRequest)); |
| 170 | + assertThat(exception.getCode(), is(401)); |
| 171 | + } |
| 172 | + |
| 173 | + @Disabled(("403 Response is not implemented in the API")) |
| 174 | + @Test |
| 175 | + public void forbiddenRequest() throws ApiException { |
| 176 | + Basic.setUsername(FORBIDDEN_USERNAME); |
| 177 | + Basic.setPassword(FORBIDDEN_PASSWORD); |
| 178 | + |
| 179 | + lookupRequest.addTnsItem(BW_NUMBER); |
| 180 | + |
| 181 | + ApiException exception = Assertions.assertThrows(ApiException.class, |
| 182 | + () -> api.createLookup(BW_ACCOUNT_ID, lookupRequest)); |
| 183 | + assertThat(exception.getCode(), is(403)); |
| 184 | + } |
| 185 | +} |
0 commit comments