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