From 9eb7fd72d17f48f28aec1b00599504018aa3aec0 Mon Sep 17 00:00:00 2001 From: Jorge Beauregard Date: Tue, 6 Oct 2020 12:21:06 -0600 Subject: [PATCH 1/6] Added create search LRO --- .../PhoneNumberAsyncClient.java | 77 +++++++++++++++++++ .../administration/PhoneNumberClient.java | 18 +++++ ...PhoneNumberAsyncClientIntegrationTest.java | 26 +++++++ .../session-records/beginCreateSearch.json | 59 ++++++++++++++ 4 files changed, 180 insertions(+) create mode 100644 sdk/communication/azure-communication-administration/src/test/resources/session-records/beginCreateSearch.json diff --git a/sdk/communication/azure-communication-administration/src/main/java/com/azure/communication/administration/PhoneNumberAsyncClient.java b/sdk/communication/azure-communication-administration/src/main/java/com/azure/communication/administration/PhoneNumberAsyncClient.java index 61ac69041c37d..176bed2c7c737 100644 --- a/sdk/communication/azure-communication-administration/src/main/java/com/azure/communication/administration/PhoneNumberAsyncClient.java +++ b/sdk/communication/azure-communication-administration/src/main/java/com/azure/communication/administration/PhoneNumberAsyncClient.java @@ -25,6 +25,7 @@ import com.azure.communication.administration.models.UpdateNumberCapabilitiesResponse; import com.azure.communication.administration.models.NumberConfiguration; import com.azure.communication.administration.models.PhoneNumberSearch; +import com.azure.communication.administration.models.SearchStatus; import com.azure.communication.administration.models.UpdateNumberCapabilitiesRequest; import com.azure.communication.administration.models.UpdatePhoneNumberCapabilitiesResponse; import com.azure.communication.common.PhoneNumber; @@ -36,12 +37,18 @@ import com.azure.core.util.Context; import com.azure.core.util.FluxUtil; import com.azure.core.util.logging.ClientLogger; +import com.azure.core.util.polling.LongRunningOperationStatus; +import com.azure.core.util.polling.PollResponse; +import com.azure.core.util.polling.PollerFlux; +import com.azure.core.util.polling.PollingContext; import reactor.core.publisher.Mono; +import java.time.Duration; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.function.Function; import java.util.stream.Collectors; import static com.azure.core.util.FluxUtil.monoError; @@ -53,6 +60,7 @@ @ServiceClient(builder = PhoneNumberClientBuilder.class, isAsync = true) public final class PhoneNumberAsyncClient { private final ClientLogger logger = new ClientLogger(PhoneNumberAsyncClient.class); + private static final Duration DEFAULT_POLL_INTERVAL = Duration.ofSeconds(60); private final PhoneNumberAdministrationsImpl phoneNumberAdministrations; @@ -757,4 +765,73 @@ Mono> purchaseSearchWithResponse(String searchId, Context context return monoError(logger, ex); } } + + /** + * Initiates a search and returns a {@link PhoneNumberSearch} usable by other functions + * This function returns a Long Running Operation poller that allows you to + * wait indefinitely until the operation is complete. + * + * @param options A {@link CreateSearchOptions} with the search options + * @param lroDuration The time our long running operation will keep on polling + * until it gets a result from the server + * @return A {@link PollerFlux} object with the search result + */ + public PollerFlux beginCreateSearch( + CreateSearchOptions options, Duration lroDuration) { + final Duration interval = lroDuration != null ? lroDuration : DEFAULT_POLL_INTERVAL; + return new PollerFlux(interval, + createSearchActivationOperation(options), + createSearchPollOperation(), + (activationResponse, pollingContext) -> Mono.error(new RuntimeException("Cancellation is not supported")), + createSearchFetchResultOperation()); + } + + private Function, + Mono> createSearchActivationOperation(CreateSearchOptions options) { + return (pollingContext) -> { + Mono response = createSearch(options).flatMap(createSearchResponse -> + { + String searchId = createSearchResponse.getSearchId(); + PhoneNumberSearch phoneNumberSearch = new PhoneNumberSearch().setSearchId(searchId); + return Mono.just(phoneNumberSearch); + }); + return response; + }; + } + + private Function, + Mono>> createSearchPollOperation() { + return pollingContext -> + getSearchById(pollingContext.getActivationResponse().getValue().getSearchId()) + .flatMap(getSearchResponse -> { + if (getSearchResponse.getStatus().equals(SearchStatus.SUCCESS)) { + return Mono.just(new PollResponse<>( + LongRunningOperationStatus.SUCCESSFULLY_COMPLETED, getSearchResponse)); + } + if (getSearchResponse.getStatus().equals(SearchStatus.EXPIRED)) { + return Mono.just(new PollResponse<>( + LongRunningOperationStatus.SUCCESSFULLY_COMPLETED, getSearchResponse)); + } + if (getSearchResponse.getStatus().equals(SearchStatus.CANCELLED)) { + return Mono.just(new PollResponse<>( + LongRunningOperationStatus.SUCCESSFULLY_COMPLETED, getSearchResponse)); + } + if (getSearchResponse.getStatus().equals(SearchStatus.RESERVED)) { + return Mono.just(new PollResponse<>( + LongRunningOperationStatus.SUCCESSFULLY_COMPLETED, getSearchResponse)); + } + if (getSearchResponse.getStatus().equals(SearchStatus.ERROR)) { + return Mono.just(new PollResponse<>( + LongRunningOperationStatus.FAILED, getSearchResponse)); + } + return Mono.just(new PollResponse<>(LongRunningOperationStatus.IN_PROGRESS, getSearchResponse)); + }); + } + + private Function, + Mono> createSearchFetchResultOperation() { + return pollingContext -> { + return Mono.just(pollingContext.getLatestResponse().getValue()); + }; + } } diff --git a/sdk/communication/azure-communication-administration/src/main/java/com/azure/communication/administration/PhoneNumberClient.java b/sdk/communication/azure-communication-administration/src/main/java/com/azure/communication/administration/PhoneNumberClient.java index f06a4dcbb5f68..d15facfe211f5 100644 --- a/sdk/communication/azure-communication-administration/src/main/java/com/azure/communication/administration/PhoneNumberClient.java +++ b/sdk/communication/azure-communication-administration/src/main/java/com/azure/communication/administration/PhoneNumberClient.java @@ -27,6 +27,9 @@ import com.azure.core.http.rest.PagedIterable; import com.azure.core.http.rest.Response; import com.azure.core.util.Context; +import com.azure.core.util.polling.SyncPoller; + +import java.time.Duration; import java.util.List; import java.util.Map; @@ -521,4 +524,19 @@ public void purchaseSearch(String searchId) { public Response purchaseSearchWithResponse(String searchId, Context context) { return phoneNumberAsyncClient.purchaseSearchWithResponse(searchId, context).block(); } + + /** + * Initiates a search and returns a {@link PhoneNumberSearch} usable by other functions + * This function returns a Long Running Operation poller. + * + * @param options A {@link CreateSearchOptions} with the search options + * @param lroDuration The time our long running operation will keep on polling + * until it gets a result from the server + * @return A {@link SyncPoller} object with the search result + */ + @ServiceMethod(returns = ReturnType.SINGLE) + public SyncPoller beginCreateSearch( + CreateSearchOptions options, Duration lroDuration) { + return phoneNumberAsyncClient.beginCreateSearch(options, lroDuration).getSyncPoller(); + } } diff --git a/sdk/communication/azure-communication-administration/src/test/java/com/azure/communication/administration/PhoneNumberAsyncClientIntegrationTest.java b/sdk/communication/azure-communication-administration/src/test/java/com/azure/communication/administration/PhoneNumberAsyncClientIntegrationTest.java index 87b2771772c9d..b9b4a47f1d3d3 100644 --- a/sdk/communication/azure-communication-administration/src/test/java/com/azure/communication/administration/PhoneNumberAsyncClientIntegrationTest.java +++ b/sdk/communication/azure-communication-administration/src/test/java/com/azure/communication/administration/PhoneNumberAsyncClientIntegrationTest.java @@ -23,12 +23,15 @@ import com.azure.communication.common.PhoneNumber; import com.azure.core.http.rest.PagedFlux; import com.azure.core.http.rest.Response; +import com.azure.core.util.polling.PollerFlux; +import com.azure.core.util.polling.SyncPoller; import com.azure.core.util.Context; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable; import reactor.core.publisher.Mono; import reactor.test.StepVerifier; +import java.time.Duration; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -463,6 +466,29 @@ public void releasePhoneNumbersWithResponse() { .verifyComplete(); } + @Test() + public void beginCreateSearch() { + List phonePlanIds = new ArrayList<>(); + phonePlanIds.add("27b53eec-8ff4-4070-8900-fbeaabfd158a"); + + CreateSearchOptions createSearchOptions = new CreateSearchOptions(); + createSearchOptions + .setAreaCode(AREA_CODE_FOR_SEARCH) + .setDescription("testsearch20200014") + .setDisplayName("testsearch20200014") + .setPhonePlanIds(phonePlanIds) + .setQuantity(2); + + Duration duration = Duration.ofSeconds(5); + PhoneNumberAsyncClient client = this.getClient(); + PollerFlux res = client.beginCreateSearch(createSearchOptions, duration); + SyncPoller sync = res.getSyncPoller(); + sync.waitForCompletion(); + PhoneNumberSearch testResult = sync.getFinalResult(); + assertEquals(testResult.getPhoneNumbers().size(), 2); + assertNotNull(testResult.getSearchId()); + } + private PhoneNumberAsyncClient getClient() { return super.getClientBuilder().buildAsyncClient(); } diff --git a/sdk/communication/azure-communication-administration/src/test/resources/session-records/beginCreateSearch.json b/sdk/communication/azure-communication-administration/src/test/resources/session-records/beginCreateSearch.json new file mode 100644 index 0000000000000..6ec687b238d06 --- /dev/null +++ b/sdk/communication/azure-communication-administration/src/test/resources/session-records/beginCreateSearch.json @@ -0,0 +1,59 @@ +{ + "networkCallRecords" : [ { + "Method" : "POST", + "Uri" : "https://REDACTED.communication.azure.com/administration/phonenumbers/searches?api-version=2020-07-20-preview1", + "Headers" : { + "User-Agent" : "azsdk-java-azure-communication-administration/1.0.0-beta.1 (11.0.8; Windows 10; 10.0)", + "Content-Type" : "application/json" + }, + "Response" : { + "Transfer-Encoding" : "chunked", + "X-Processing-Time" : "2106ms", + "MS-CV" : "AudA+TBsSkC2dy5EORj0jg.0", + "retry-after" : "0", + "X-Azure-Ref" : "0/598XwAAAABNqI5PZ5BrTocglFXesDJDREVOMDJFREdFMDMxNQA5ZmM3YjUxOS1hOGNjLTRmODktOTM1ZS1jOTE0OGFlMDllODE=", + "StatusCode" : "201", + "Body" : "{\"searchId\":\"44b70e2f-6521-454e-a8ea-67ab95927a13\"}", + "Date" : "Tue, 06 Oct 2020 16:49:05 GMT", + "Content-Type" : "application/json; charset=utf-8" + }, + "Exception" : null + }, { + "Method" : "GET", + "Uri" : "https://REDACTED.communication.azure.com/administration/phonenumbers/searches/44b70e2f-6521-454e-a8ea-67ab95927a13?api-version=2020-07-20-preview1", + "Headers" : { + "User-Agent" : "azsdk-java-azure-communication-administration/1.0.0-beta.1 (11.0.8; Windows 10; 10.0)" + }, + "Response" : { + "Transfer-Encoding" : "chunked", + "X-Processing-Time" : "351ms", + "MS-CV" : "UsgvNonyy0W78Wb+YlAR/Q.0", + "retry-after" : "0", + "X-Azure-Ref" : "0B6B8XwAAAABJLU8C50vpQr8eJDv7BnURREVOMDJFREdFMDMxNQA5ZmM3YjUxOS1hOGNjLTRmODktOTM1ZS1jOTE0OGFlMDllODE=", + "StatusCode" : "200", + "Body" : "{\"searchId\":\"44b70e2f-6521-454e-a8ea-67ab95927a13\",\"displayName\":\"testsearch20200014\",\"createdAt\":\"2020-10-06T16:49:05.1044519+00:00\",\"description\":\"testsearch20200014\",\"phonePlanIds\":[\"27b53eec-8ff4-4070-8900-fbeaabfd158a\"],\"areaCode\":\"323\",\"quantity\":2,\"locationOptions\":[],\"status\":\"Pending\",\"phoneNumbers\":[]}", + "Date" : "Tue, 06 Oct 2020 16:49:11 GMT", + "Content-Type" : "application/json; charset=utf-8" + }, + "Exception" : null + }, { + "Method" : "GET", + "Uri" : "https://REDACTED.communication.azure.com/administration/phonenumbers/searches/44b70e2f-6521-454e-a8ea-67ab95927a13?api-version=2020-07-20-preview1", + "Headers" : { + "User-Agent" : "azsdk-java-azure-communication-administration/1.0.0-beta.1 (11.0.8; Windows 10; 10.0)" + }, + "Response" : { + "Transfer-Encoding" : "chunked", + "X-Processing-Time" : "292ms", + "MS-CV" : "rgPQQ3XR50SFtXr68Qefqw.0", + "retry-after" : "0", + "X-Azure-Ref" : "0DaB8XwAAAADdZnkaYhXqS76qjA9LTed8REVOMDJFREdFMDMxNQA5ZmM3YjUxOS1hOGNjLTRmODktOTM1ZS1jOTE0OGFlMDllODE=", + "StatusCode" : "200", + "Body" : "{\"searchId\":\"44b70e2f-6521-454e-a8ea-67ab95927a13\",\"displayName\":\"testsearch20200014\",\"createdAt\":\"2020-10-06T16:49:05.1044519+00:00\",\"description\":\"testsearch20200014\",\"phonePlanIds\":[\"27b53eec-8ff4-4070-8900-fbeaabfd158a\"],\"areaCode\":\"323\",\"quantity\":2,\"locationOptions\":[],\"status\":\"Reserved\",\"phoneNumbers\":[\"+13234866789\",\"+13234866792\"],\"reservationExpiryDate\":\"2020-10-06T17:05:13.5997531+00:00\"}", + "Date" : "Tue, 06 Oct 2020 16:49:16 GMT", + "Content-Type" : "application/json; charset=utf-8" + }, + "Exception" : null + } ], + "variables" : [ ] +} \ No newline at end of file From eb895c3b428e0e6544ec0df1d9515a28d0fae502 Mon Sep 17 00:00:00 2001 From: Jorge Beauregard Date: Tue, 6 Oct 2020 16:11:21 -0600 Subject: [PATCH 2/6] Addressed PR comments --- .../PhoneNumberAsyncClient.java | 41 +++++++++++-------- ...PhoneNumberAsyncClientIntegrationTest.java | 8 ++-- .../PhoneNumberIntegrationTestBase.java | 4 ++ 3 files changed, 33 insertions(+), 20 deletions(-) diff --git a/sdk/communication/azure-communication-administration/src/main/java/com/azure/communication/administration/PhoneNumberAsyncClient.java b/sdk/communication/azure-communication-administration/src/main/java/com/azure/communication/administration/PhoneNumberAsyncClient.java index 176bed2c7c737..92ed3f8138cf0 100644 --- a/sdk/communication/azure-communication-administration/src/main/java/com/azure/communication/administration/PhoneNumberAsyncClient.java +++ b/sdk/communication/azure-communication-administration/src/main/java/com/azure/communication/administration/PhoneNumberAsyncClient.java @@ -48,6 +48,7 @@ import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.function.BiFunction; import java.util.function.Function; import java.util.stream.Collectors; @@ -60,8 +61,6 @@ @ServiceClient(builder = PhoneNumberClientBuilder.class, isAsync = true) public final class PhoneNumberAsyncClient { private final ClientLogger logger = new ClientLogger(PhoneNumberAsyncClient.class); - private static final Duration DEFAULT_POLL_INTERVAL = Duration.ofSeconds(60); - private final PhoneNumberAdministrationsImpl phoneNumberAdministrations; PhoneNumberAsyncClient(PhoneNumberAdminClientImpl phoneNumberAdminClient) { @@ -772,28 +771,28 @@ Mono> purchaseSearchWithResponse(String searchId, Context context * wait indefinitely until the operation is complete. * * @param options A {@link CreateSearchOptions} with the search options - * @param lroDuration The time our long running operation will keep on polling + * @param pollInterval The time our long running operation will keep on polling * until it gets a result from the server * @return A {@link PollerFlux} object with the search result */ public PollerFlux beginCreateSearch( - CreateSearchOptions options, Duration lroDuration) { - final Duration interval = lroDuration != null ? lroDuration : DEFAULT_POLL_INTERVAL; - return new PollerFlux(interval, + CreateSearchOptions options, Duration pollInterval) { + Objects.requireNonNull(options, "'options' cannot be null."); + Objects.requireNonNull(pollInterval, "'pollInterval' cannot be null."); + return new PollerFlux(pollInterval, createSearchActivationOperation(options), createSearchPollOperation(), - (activationResponse, pollingContext) -> Mono.error(new RuntimeException("Cancellation is not supported")), + cancelSearchOperation(), createSearchFetchResultOperation()); } private Function, Mono> createSearchActivationOperation(CreateSearchOptions options) { return (pollingContext) -> { - Mono response = createSearch(options).flatMap(createSearchResponse -> - { + Mono response = createSearch(options).flatMap(createSearchResponse -> { String searchId = createSearchResponse.getSearchId(); - PhoneNumberSearch phoneNumberSearch = new PhoneNumberSearch().setSearchId(searchId); - return Mono.just(phoneNumberSearch); + Mono phoneNumberSearch = getSearchById(searchId); + return phoneNumberSearch; }); return response; }; @@ -804,23 +803,24 @@ Mono>> createSearchPollOperation() { return pollingContext -> getSearchById(pollingContext.getActivationResponse().getValue().getSearchId()) .flatMap(getSearchResponse -> { - if (getSearchResponse.getStatus().equals(SearchStatus.SUCCESS)) { + SearchStatus status = getSearchResponse.getStatus(); + if (status.equals(SearchStatus.SUCCESS)) { return Mono.just(new PollResponse<>( LongRunningOperationStatus.SUCCESSFULLY_COMPLETED, getSearchResponse)); } - if (getSearchResponse.getStatus().equals(SearchStatus.EXPIRED)) { + if (status.equals(SearchStatus.EXPIRED)) { return Mono.just(new PollResponse<>( LongRunningOperationStatus.SUCCESSFULLY_COMPLETED, getSearchResponse)); } - if (getSearchResponse.getStatus().equals(SearchStatus.CANCELLED)) { + if (status.equals(SearchStatus.CANCELLED)) { return Mono.just(new PollResponse<>( LongRunningOperationStatus.SUCCESSFULLY_COMPLETED, getSearchResponse)); } - if (getSearchResponse.getStatus().equals(SearchStatus.RESERVED)) { + if (status.equals(SearchStatus.RESERVED)) { return Mono.just(new PollResponse<>( LongRunningOperationStatus.SUCCESSFULLY_COMPLETED, getSearchResponse)); } - if (getSearchResponse.getStatus().equals(SearchStatus.ERROR)) { + if (status.equals(SearchStatus.ERROR)) { return Mono.just(new PollResponse<>( LongRunningOperationStatus.FAILED, getSearchResponse)); } @@ -828,6 +828,15 @@ Mono>> createSearchPollOperation() { }); } + private BiFunction, + PollResponse, + Mono> cancelSearchOperation() { + return (pollingContext, firstResponse) -> { + cancelSearch(pollingContext.getActivationResponse().getValue().getSearchId()); + return Mono.just(new PhoneNumberSearch()); + }; + } + private Function, Mono> createSearchFetchResultOperation() { return pollingContext -> { diff --git a/sdk/communication/azure-communication-administration/src/test/java/com/azure/communication/administration/PhoneNumberAsyncClientIntegrationTest.java b/sdk/communication/azure-communication-administration/src/test/java/com/azure/communication/administration/PhoneNumberAsyncClientIntegrationTest.java index b9b4a47f1d3d3..41b87f0f28790 100644 --- a/sdk/communication/azure-communication-administration/src/test/java/com/azure/communication/administration/PhoneNumberAsyncClientIntegrationTest.java +++ b/sdk/communication/azure-communication-administration/src/test/java/com/azure/communication/administration/PhoneNumberAsyncClientIntegrationTest.java @@ -469,17 +469,17 @@ public void releasePhoneNumbersWithResponse() { @Test() public void beginCreateSearch() { List phonePlanIds = new ArrayList<>(); - phonePlanIds.add("27b53eec-8ff4-4070-8900-fbeaabfd158a"); + phonePlanIds.add(PHONE_PLAN_ID); CreateSearchOptions createSearchOptions = new CreateSearchOptions(); createSearchOptions .setAreaCode(AREA_CODE_FOR_SEARCH) - .setDescription("testsearch20200014") - .setDisplayName("testsearch20200014") + .setDescription(SEARCH_OPTIONS_DESCRIPTION) + .setDisplayName(SEARCH_OPTIONS_NAME) .setPhonePlanIds(phonePlanIds) .setQuantity(2); - Duration duration = Duration.ofSeconds(5); + Duration duration = Duration.ofSeconds(1); PhoneNumberAsyncClient client = this.getClient(); PollerFlux res = client.beginCreateSearch(createSearchOptions, duration); SyncPoller sync = res.getSyncPoller(); diff --git a/sdk/communication/azure-communication-administration/src/test/java/com/azure/communication/administration/PhoneNumberIntegrationTestBase.java b/sdk/communication/azure-communication-administration/src/test/java/com/azure/communication/administration/PhoneNumberIntegrationTestBase.java index f2e7aadcf8583..6ba88408896c9 100644 --- a/sdk/communication/azure-communication-administration/src/test/java/com/azure/communication/administration/PhoneNumberIntegrationTestBase.java +++ b/sdk/communication/azure-communication-administration/src/test/java/com/azure/communication/administration/PhoneNumberIntegrationTestBase.java @@ -60,6 +60,10 @@ public class PhoneNumberIntegrationTestBase extends TestBase { Configuration.getGlobalConfiguration().get("LOCATION_OPTION_STATE", "CA"); protected static final String LOCATION_OPTION_CITY = Configuration.getGlobalConfiguration().get("LOCATION_OPTION_CITY", "NOAM-US-CA-LA"); + protected static final String SEARCH_OPTIONS_DESCRIPTION = + Configuration.getGlobalConfiguration().get("SEARCH_OPTIONS_DESCRIPTION", "testsearch20200014"); + protected static final String SEARCH_OPTIONS_NAME = + Configuration.getGlobalConfiguration().get("SEARCH_OPTIONS_NAME", "testsearch20200014"); protected PhoneNumberClientBuilder getClientBuilder() { HttpClient httpClient; From e5b1bee022b028e035f32f931625305651239e16 Mon Sep 17 00:00:00 2001 From: Jorge Beauregard Date: Wed, 7 Oct 2020 10:05:44 -0600 Subject: [PATCH 3/6] Addressed Ankit's comments --- .../administration/PhoneNumberAsyncClient.java | 9 +++++---- .../communication/administration/PhoneNumberClient.java | 6 +++--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/sdk/communication/azure-communication-administration/src/main/java/com/azure/communication/administration/PhoneNumberAsyncClient.java b/sdk/communication/azure-communication-administration/src/main/java/com/azure/communication/administration/PhoneNumberAsyncClient.java index 92ed3f8138cf0..f5146f2ff0980 100644 --- a/sdk/communication/azure-communication-administration/src/main/java/com/azure/communication/administration/PhoneNumberAsyncClient.java +++ b/sdk/communication/azure-communication-administration/src/main/java/com/azure/communication/administration/PhoneNumberAsyncClient.java @@ -775,6 +775,7 @@ Mono> purchaseSearchWithResponse(String searchId, Context context * until it gets a result from the server * @return A {@link PollerFlux} object with the search result */ + @ServiceMethod(returns = ReturnType.COLLECTION) public PollerFlux beginCreateSearch( CreateSearchOptions options, Duration pollInterval) { Objects.requireNonNull(options, "'options' cannot be null."); @@ -786,8 +787,8 @@ public PollerFlux beginCreateSearch( createSearchFetchResultOperation()); } - private Function, - Mono> createSearchActivationOperation(CreateSearchOptions options) { + private Function, Mono> + createSearchActivationOperation(CreateSearchOptions options) { return (pollingContext) -> { Mono response = createSearch(options).flatMap(createSearchResponse -> { String searchId = createSearchResponse.getSearchId(); @@ -829,8 +830,8 @@ Mono>> createSearchPollOperation() { } private BiFunction, - PollResponse, - Mono> cancelSearchOperation() { + PollResponse, Mono> + cancelSearchOperation() { return (pollingContext, firstResponse) -> { cancelSearch(pollingContext.getActivationResponse().getValue().getSearchId()); return Mono.just(new PhoneNumberSearch()); diff --git a/sdk/communication/azure-communication-administration/src/main/java/com/azure/communication/administration/PhoneNumberClient.java b/sdk/communication/azure-communication-administration/src/main/java/com/azure/communication/administration/PhoneNumberClient.java index d15facfe211f5..be3a27b8d70d7 100644 --- a/sdk/communication/azure-communication-administration/src/main/java/com/azure/communication/administration/PhoneNumberClient.java +++ b/sdk/communication/azure-communication-administration/src/main/java/com/azure/communication/administration/PhoneNumberClient.java @@ -530,13 +530,13 @@ public Response purchaseSearchWithResponse(String searchId, Context contex * This function returns a Long Running Operation poller. * * @param options A {@link CreateSearchOptions} with the search options - * @param lroDuration The time our long running operation will keep on polling + * @param pollInterval The time our long running operation will keep on polling * until it gets a result from the server * @return A {@link SyncPoller} object with the search result */ @ServiceMethod(returns = ReturnType.SINGLE) public SyncPoller beginCreateSearch( - CreateSearchOptions options, Duration lroDuration) { - return phoneNumberAsyncClient.beginCreateSearch(options, lroDuration).getSyncPoller(); + CreateSearchOptions options, Duration pollInterval) { + return phoneNumberAsyncClient.beginCreateSearch(options, pollInterval).getSyncPoller(); } } From e93f26e9a2b03b11e72a155eb68ac9fab8dc1b70 Mon Sep 17 00:00:00 2001 From: Jorge Beauregard Date: Wed, 7 Oct 2020 14:09:48 -0600 Subject: [PATCH 4/6] Cleaned poll operation --- .../PhoneNumberAsyncClient.java | 31 +++++++------------ ...PhoneNumberAsyncClientIntegrationTest.java | 8 ++--- 2 files changed, 15 insertions(+), 24 deletions(-) diff --git a/sdk/communication/azure-communication-administration/src/main/java/com/azure/communication/administration/PhoneNumberAsyncClient.java b/sdk/communication/azure-communication-administration/src/main/java/com/azure/communication/administration/PhoneNumberAsyncClient.java index f5146f2ff0980..a8e2677581968 100644 --- a/sdk/communication/azure-communication-administration/src/main/java/com/azure/communication/administration/PhoneNumberAsyncClient.java +++ b/sdk/communication/azure-communication-administration/src/main/java/com/azure/communication/administration/PhoneNumberAsyncClient.java @@ -799,27 +799,18 @@ public PollerFlux beginCreateSearch( }; } - private Function, - Mono>> createSearchPollOperation() { + private Function, Mono>> + createSearchPollOperation() { return pollingContext -> - getSearchById(pollingContext.getActivationResponse().getValue().getSearchId()) + getSearchById(pollingContext.getLatestResponse().getValue().getSearchId()) .flatMap(getSearchResponse -> { SearchStatus status = getSearchResponse.getStatus(); - if (status.equals(SearchStatus.SUCCESS)) { - return Mono.just(new PollResponse<>( - LongRunningOperationStatus.SUCCESSFULLY_COMPLETED, getSearchResponse)); - } - if (status.equals(SearchStatus.EXPIRED)) { - return Mono.just(new PollResponse<>( - LongRunningOperationStatus.SUCCESSFULLY_COMPLETED, getSearchResponse)); - } - if (status.equals(SearchStatus.CANCELLED)) { - return Mono.just(new PollResponse<>( - LongRunningOperationStatus.SUCCESSFULLY_COMPLETED, getSearchResponse)); - } - if (status.equals(SearchStatus.RESERVED)) { - return Mono.just(new PollResponse<>( - LongRunningOperationStatus.SUCCESSFULLY_COMPLETED, getSearchResponse)); + if (status.equals(SearchStatus.SUCCESS) + || status.equals(SearchStatus.EXPIRED) + || status.equals(SearchStatus.CANCELLED) + || status.equals(SearchStatus.RESERVED)) { + return Mono.just(new PollResponse<>( + LongRunningOperationStatus.SUCCESSFULLY_COMPLETED, getSearchResponse)); } if (status.equals(SearchStatus.ERROR)) { return Mono.just(new PollResponse<>( @@ -833,8 +824,8 @@ Mono>> createSearchPollOperation() { PollResponse, Mono> cancelSearchOperation() { return (pollingContext, firstResponse) -> { - cancelSearch(pollingContext.getActivationResponse().getValue().getSearchId()); - return Mono.just(new PhoneNumberSearch()); + cancelSearch(pollingContext.getLatestResponse().getValue().getSearchId()); + return Mono.just(pollingContext.getLatestResponse().getValue()); }; } diff --git a/sdk/communication/azure-communication-administration/src/test/java/com/azure/communication/administration/PhoneNumberAsyncClientIntegrationTest.java b/sdk/communication/azure-communication-administration/src/test/java/com/azure/communication/administration/PhoneNumberAsyncClientIntegrationTest.java index 41b87f0f28790..1bbeee7199585 100644 --- a/sdk/communication/azure-communication-administration/src/test/java/com/azure/communication/administration/PhoneNumberAsyncClientIntegrationTest.java +++ b/sdk/communication/azure-communication-administration/src/test/java/com/azure/communication/administration/PhoneNumberAsyncClientIntegrationTest.java @@ -481,10 +481,10 @@ public void beginCreateSearch() { Duration duration = Duration.ofSeconds(1); PhoneNumberAsyncClient client = this.getClient(); - PollerFlux res = client.beginCreateSearch(createSearchOptions, duration); - SyncPoller sync = res.getSyncPoller(); - sync.waitForCompletion(); - PhoneNumberSearch testResult = sync.getFinalResult(); + SyncPoller res = + client.beginCreateSearch(createSearchOptions, duration).getSyncPoller(); + res.waitForCompletion(); + PhoneNumberSearch testResult = res.getFinalResult(); assertEquals(testResult.getPhoneNumbers().size(), 2); assertNotNull(testResult.getSearchId()); } From 4808beeba5b9fa59ef77b64828959f96b3cfd176 Mon Sep 17 00:00:00 2001 From: Jorge Beauregard Date: Wed, 7 Oct 2020 14:34:59 -0600 Subject: [PATCH 5/6] Removed SUCCESS as a terminal state --- .../administration/PhoneNumberAsyncClient.java | 7 +++---- .../PhoneNumberAsyncClientIntegrationTest.java | 1 - 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/sdk/communication/azure-communication-administration/src/main/java/com/azure/communication/administration/PhoneNumberAsyncClient.java b/sdk/communication/azure-communication-administration/src/main/java/com/azure/communication/administration/PhoneNumberAsyncClient.java index a8e2677581968..979507befa82e 100644 --- a/sdk/communication/azure-communication-administration/src/main/java/com/azure/communication/administration/PhoneNumberAsyncClient.java +++ b/sdk/communication/azure-communication-administration/src/main/java/com/azure/communication/administration/PhoneNumberAsyncClient.java @@ -805,12 +805,11 @@ public PollerFlux beginCreateSearch( getSearchById(pollingContext.getLatestResponse().getValue().getSearchId()) .flatMap(getSearchResponse -> { SearchStatus status = getSearchResponse.getStatus(); - if (status.equals(SearchStatus.SUCCESS) - || status.equals(SearchStatus.EXPIRED) + if (status.equals(SearchStatus.EXPIRED) || status.equals(SearchStatus.CANCELLED) || status.equals(SearchStatus.RESERVED)) { - return Mono.just(new PollResponse<>( - LongRunningOperationStatus.SUCCESSFULLY_COMPLETED, getSearchResponse)); + return Mono.just(new PollResponse<>( + LongRunningOperationStatus.SUCCESSFULLY_COMPLETED, getSearchResponse)); } if (status.equals(SearchStatus.ERROR)) { return Mono.just(new PollResponse<>( diff --git a/sdk/communication/azure-communication-administration/src/test/java/com/azure/communication/administration/PhoneNumberAsyncClientIntegrationTest.java b/sdk/communication/azure-communication-administration/src/test/java/com/azure/communication/administration/PhoneNumberAsyncClientIntegrationTest.java index 1bbeee7199585..807c347e38cb6 100644 --- a/sdk/communication/azure-communication-administration/src/test/java/com/azure/communication/administration/PhoneNumberAsyncClientIntegrationTest.java +++ b/sdk/communication/azure-communication-administration/src/test/java/com/azure/communication/administration/PhoneNumberAsyncClientIntegrationTest.java @@ -23,7 +23,6 @@ import com.azure.communication.common.PhoneNumber; import com.azure.core.http.rest.PagedFlux; import com.azure.core.http.rest.Response; -import com.azure.core.util.polling.PollerFlux; import com.azure.core.util.polling.SyncPoller; import com.azure.core.util.Context; import org.junit.jupiter.api.Test; From 6176e0a9689e15a2545ac9dc775df4f5c3156903 Mon Sep 17 00:00:00 2001 From: Jorge Beauregard Date: Thu, 8 Oct 2020 14:29:41 -0600 Subject: [PATCH 6/6] Test redone to use async responses --- .../administration/PhoneNumberClient.java | 2 +- .../PhoneNumberAsyncClientIntegrationTest.java | 14 +++++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/sdk/communication/azure-communication-administration/src/main/java/com/azure/communication/administration/PhoneNumberClient.java b/sdk/communication/azure-communication-administration/src/main/java/com/azure/communication/administration/PhoneNumberClient.java index be3a27b8d70d7..823de72653424 100644 --- a/sdk/communication/azure-communication-administration/src/main/java/com/azure/communication/administration/PhoneNumberClient.java +++ b/sdk/communication/azure-communication-administration/src/main/java/com/azure/communication/administration/PhoneNumberClient.java @@ -534,7 +534,7 @@ public Response purchaseSearchWithResponse(String searchId, Context contex * until it gets a result from the server * @return A {@link SyncPoller} object with the search result */ - @ServiceMethod(returns = ReturnType.SINGLE) + @ServiceMethod(returns = ReturnType.COLLECTION) public SyncPoller beginCreateSearch( CreateSearchOptions options, Duration pollInterval) { return phoneNumberAsyncClient.beginCreateSearch(options, pollInterval).getSyncPoller(); diff --git a/sdk/communication/azure-communication-administration/src/test/java/com/azure/communication/administration/PhoneNumberAsyncClientIntegrationTest.java b/sdk/communication/azure-communication-administration/src/test/java/com/azure/communication/administration/PhoneNumberAsyncClientIntegrationTest.java index 807c347e38cb6..6e8edd8251f24 100644 --- a/sdk/communication/azure-communication-administration/src/test/java/com/azure/communication/administration/PhoneNumberAsyncClientIntegrationTest.java +++ b/sdk/communication/azure-communication-administration/src/test/java/com/azure/communication/administration/PhoneNumberAsyncClientIntegrationTest.java @@ -23,7 +23,9 @@ import com.azure.communication.common.PhoneNumber; import com.azure.core.http.rest.PagedFlux; import com.azure.core.http.rest.Response; -import com.azure.core.util.polling.SyncPoller; +import com.azure.core.util.polling.PollerFlux; +import com.azure.core.util.polling.AsyncPollResponse; +import com.azure.core.util.polling.LongRunningOperationStatus; import com.azure.core.util.Context; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable; @@ -480,10 +482,12 @@ public void beginCreateSearch() { Duration duration = Duration.ofSeconds(1); PhoneNumberAsyncClient client = this.getClient(); - SyncPoller res = - client.beginCreateSearch(createSearchOptions, duration).getSyncPoller(); - res.waitForCompletion(); - PhoneNumberSearch testResult = res.getFinalResult(); + PollerFlux poller = + client.beginCreateSearch(createSearchOptions, duration); + AsyncPollResponse asyncRes = + poller.takeUntil(apr -> apr.getStatus() == LongRunningOperationStatus.SUCCESSFULLY_COMPLETED) + .blockLast(); + PhoneNumberSearch testResult = asyncRes.getValue(); assertEquals(testResult.getPhoneNumbers().size(), 2); assertNotNull(testResult.getSearchId()); }