diff --git a/sdk/search/azure-search/src/main/java/com/azure/search/SearchServiceAsyncClient.java b/sdk/search/azure-search/src/main/java/com/azure/search/SearchServiceAsyncClient.java index ab6b985f3b0c0..f4a525d4182cd 100644 --- a/sdk/search/azure-search/src/main/java/com/azure/search/SearchServiceAsyncClient.java +++ b/sdk/search/azure-search/src/main/java/com/azure/search/SearchServiceAsyncClient.java @@ -35,6 +35,7 @@ import reactor.core.publisher.Mono; import java.util.function.Function; +import java.util.function.Supplier; import java.util.stream.Collectors; import static com.azure.core.implementation.util.FluxUtil.withContext; @@ -593,7 +594,7 @@ Mono> getIndexWithResponse(String indexName, RequestOptions requ * @return true if the index exists; false otherwise. */ public Mono indexExists(String indexName) { - return indexExistsWithResponse(indexName, null).map(Response::getValue); + return this.indexExistsWithResponse(indexName, null).map(Response::getValue); } /** @@ -605,7 +606,7 @@ public Mono indexExists(String indexName) { * @return true if the index exists; false otherwise. */ public Mono indexExists(String indexName, RequestOptions requestOptions) { - return indexExistsWithResponse(indexName, requestOptions).map(Response::getValue); + return this.indexExistsWithResponse(indexName, requestOptions).map(Response::getValue); } /** @@ -617,22 +618,13 @@ public Mono indexExists(String indexName, RequestOptions requestOptions * @return true if the index exists; false otherwise. */ public Mono> indexExistsWithResponse(String indexName, RequestOptions requestOptions) { - return withContext(context -> indexExistsWithResponse(indexName, requestOptions, context)); + return withContext(context -> this.indexExistsWithResponse(indexName, requestOptions, context)); } Mono> indexExistsWithResponse(String indexName, RequestOptions requestOptions, Context context) { - return this.getIndexWithResponse(indexName, requestOptions, context) - .map(i -> (Response) new SimpleResponse<>(i, true)) - .onErrorResume( - t -> t instanceof HttpResponseException - && ((HttpResponseException) t).getResponse().getStatusCode() == 404, - t -> { - HttpResponse response = ((HttpResponseException) t).getResponse(); - return Mono.just(new SimpleResponse<>(response.getRequest(), response.getStatusCode(), - response.getHeaders(), false)); - }); + return this.resourceExistsWithResponse(() -> this.getIndexWithResponse(indexName, requestOptions, context)); } /** @@ -1186,6 +1178,67 @@ public Mono>> deleteSynonymMapWithResponse() { throw logger.logExceptionAsError(new NotImplementedException("not implemented.")); } + /** + * Determines whether or not the given synonym map exists. + * + * @param synonymMapName the name of the synonym map + * @return true if the synonym map exists; false otherwise. + */ + public Mono synonymMapExists(String synonymMapName) { + return this.synonymMapExistsWithResponse(synonymMapName, null).map(Response::getValue); + } + + /** + * Determines whether or not the given synonym map exists. + * + * @param synonymMapName the name of the synonym map + * @param requestOptions additional parameters for the operation. + * Contains the tracking ID sent with the request to help with debugging + * @return true if the synonym map exists; false otherwise. + */ + public Mono synonymMapExists(String synonymMapName, RequestOptions requestOptions) { + return this.synonymMapExistsWithResponse(synonymMapName, requestOptions).map(Response::getValue); + } + + /** + * Determines whether or not the given synonym map exists. + * + * @param synonymMapName the name of the synonym map + * @param requestOptions additional parameters for the operation. + * Contains the tracking ID sent with the request to help with debugging + * @return true if the synonym map exists; false otherwise. + */ + public Mono> synonymMapExistsWithResponse(String synonymMapName, RequestOptions requestOptions) { + return withContext(context -> this.synonymMapExistsWithResponse(synonymMapName, requestOptions, context)); + } + + Mono> synonymMapExistsWithResponse(String synonymMapName, + RequestOptions requestOptions, + Context context) { + return resourceExistsWithResponse(() -> + this.getSynonymMapWithResponse(synonymMapName, requestOptions, context)); + } + + /** + * Runs an async action and determines if a resource exists or not + * + * @param action the runnable async action + * @return true if the resource exists (service returns a '200' status code); otherwise false. + */ + private static Mono> resourceExistsWithResponse(Supplier>> action) { + return action.get() + .map(i -> + (Response) new SimpleResponse<>(i, i.getStatusCode() == 200)) + .onErrorResume( + t -> t instanceof HttpResponseException + && ((HttpResponseException) t).getResponse().getStatusCode() == 404, + t -> { + HttpResponse response = ((HttpResponseException) t).getResponse(); + return Mono.just(new SimpleResponse<>(response.getRequest(), response.getStatusCode(), + response.getHeaders(), false)); + }); + } + private static String deserializeHeaders(HttpHeaders headers) { return headers.toMap().entrySet().stream().map((entry) -> entry.getKey() + "," + entry.getValue() diff --git a/sdk/search/azure-search/src/main/java/com/azure/search/SearchServiceClient.java b/sdk/search/azure-search/src/main/java/com/azure/search/SearchServiceClient.java index bdd0daf714807..3daca77022dd6 100644 --- a/sdk/search/azure-search/src/main/java/com/azure/search/SearchServiceClient.java +++ b/sdk/search/azure-search/src/main/java/com/azure/search/SearchServiceClient.java @@ -1069,4 +1069,57 @@ public void deleteSynonymMap() { public Response> deleteSynonymMapWithResponse() { throw logger.logExceptionAsError(new NotImplementedException("not implemented.")); } + + /** + * Determines whether or not the given synonym map exists. + * + * @param synonymMapName the name of the synonym map + * @return true if the synonym map exists; false otherwise. + */ + public Boolean synonymMapExists(String synonymMapName) { + return asyncClient.synonymMapExists(synonymMapName).block(); + } + + /** + * Determines whether or not the given synonym map exists. + * + * @param synonymMapName the name of the synonym map + * @param requestOptions additional parameters for the operation. + * Contains the tracking ID sent with the request to help with debugging + * @return true if the synonym map exists; false otherwise. + */ + public Boolean synonymMapExists(String synonymMapName, RequestOptions requestOptions) { + return asyncClient.synonymMapExists(synonymMapName, requestOptions).block(); + } + + /** + * Determines whether or not the given synonym map exists. + * + * @param synonymMapName the name of the synonym map + * @param requestOptions additional parameters for the operation. + * Contains the tracking ID sent with the request to help with debugging + * @param context additional context that is passed through the HTTP pipeline during the service call + * @return true if the index exists; false otherwise. + */ + public Boolean synonymMapExists(String synonymMapName, RequestOptions requestOptions, Context context) { + return this.synonymMapExistsWithResponse(synonymMapName, requestOptions, context) + .getValue(); + } + + /** + * Determines whether or not the given synonym map exists. + * + * @param synonymMapName the name of the synonym map + * @param requestOptions additional parameters for the operation. + * Contains the tracking ID sent with the request to help with debugging + * @param context additional context that is passed through the HTTP pipeline during the service call + * @return true if the index exists; false otherwise. + */ + public Response synonymMapExistsWithResponse(String synonymMapName, + RequestOptions requestOptions, + Context context) { + return asyncClient + .synonymMapExistsWithResponse(synonymMapName, requestOptions, context) + .block(); + } } diff --git a/sdk/search/azure-search/src/test/java/com/azure/search/SynonymMapManagementAsyncTests.java b/sdk/search/azure-search/src/test/java/com/azure/search/SynonymMapManagementAsyncTests.java index 5e495faf04325..d01bd98cac8ca 100644 --- a/sdk/search/azure-search/src/test/java/com/azure/search/SynonymMapManagementAsyncTests.java +++ b/sdk/search/azure-search/src/test/java/com/azure/search/SynonymMapManagementAsyncTests.java @@ -235,12 +235,50 @@ public void canListSynonymMapsWithSelectedField() { @Override public void existsReturnsTrueForExistingSynonymMap() { + SynonymMap synonymMap = createTestSynonymMap(); + RequestOptions requestOptions = new RequestOptions() + .setClientRequestId(UUID.randomUUID()); + + client.createSynonymMap(synonymMap).block(); + + StepVerifier + .create(client.synonymMapExists(synonymMap.getName())) + .assertNext(res -> Assert.assertTrue(res)) + .verifyComplete(); + + StepVerifier + .create(client.synonymMapExists(synonymMap.getName(), requestOptions)) + .assertNext(res -> Assert.assertTrue(res)) + .verifyComplete(); + + StepVerifier + .create(client.synonymMapExistsWithResponse(synonymMap.getName(), requestOptions)) + .assertNext(res -> Assert.assertTrue(res.getValue())) + .verifyComplete(); } @Override public void existsReturnsFalseForNonExistingSynonymMap() { + String synonymMapName = "thisSynonymMapDoesNotExist"; + RequestOptions requestOptions = new RequestOptions() + .setClientRequestId(UUID.randomUUID()); + + StepVerifier + .create(client.synonymMapExists(synonymMapName)) + .assertNext(res -> Assert.assertFalse(res)) + .verifyComplete(); + + StepVerifier + .create(client.synonymMapExists(synonymMapName, requestOptions)) + .assertNext(res -> Assert.assertFalse(res)) + .verifyComplete(); + + StepVerifier + .create(client.synonymMapExistsWithResponse(synonymMapName, requestOptions)) + .assertNext(res -> Assert.assertFalse(res.getValue())) + .verifyComplete(); } } diff --git a/sdk/search/azure-search/src/test/java/com/azure/search/SynonymMapManagementSyncTests.java b/sdk/search/azure-search/src/test/java/com/azure/search/SynonymMapManagementSyncTests.java index 0ef6233597839..84463bb082960 100644 --- a/sdk/search/azure-search/src/test/java/com/azure/search/SynonymMapManagementSyncTests.java +++ b/sdk/search/azure-search/src/test/java/com/azure/search/SynonymMapManagementSyncTests.java @@ -195,12 +195,36 @@ public void canListSynonymMapsWithSelectedField() { @Override public void existsReturnsTrueForExistingSynonymMap() { + SynonymMap synonymMap = createTestSynonymMap(); + RequestOptions requestOptions = new RequestOptions() + .setClientRequestId(UUID.randomUUID()); + Context context = new Context("key", "value"); + + client.createSynonymMap(synonymMap); + + Assert.assertTrue(client.synonymMapExists(synonymMap.getName())); + Assert.assertTrue(client.synonymMapExists(synonymMap.getName(), requestOptions)); + Assert.assertTrue(client.synonymMapExistsWithResponse(synonymMap.getName(), + requestOptions, + context) + .getValue()); } @Override public void existsReturnsFalseForNonExistingSynonymMap() { + String synonymMapName = "thisSynonymMapDoesNotExist"; + + RequestOptions requestOptions = new RequestOptions() + .setClientRequestId(UUID.randomUUID()); + Context context = new Context("key", "value"); + Assert.assertFalse(client.synonymMapExists(synonymMapName)); + Assert.assertFalse(client.synonymMapExists(synonymMapName, requestOptions)); + Assert.assertFalse(client.synonymMapExistsWithResponse(synonymMapName, + requestOptions, + context) + .getValue()); } private void validateSynonymMapNotFoundThrowsException(String synonymMapName, Runnable getSynonymMapAction) { diff --git a/sdk/search/azure-search/src/test/java/com/azure/search/SynonymMapManagementTestBase.java b/sdk/search/azure-search/src/test/java/com/azure/search/SynonymMapManagementTestBase.java index 7258e298ba40e..c0edcfe6c007f 100644 --- a/sdk/search/azure-search/src/test/java/com/azure/search/SynonymMapManagementTestBase.java +++ b/sdk/search/azure-search/src/test/java/com/azure/search/SynonymMapManagementTestBase.java @@ -48,8 +48,10 @@ public abstract class SynonymMapManagementTestBase extends SearchServiceTestBase @Test public abstract void canListSynonymMapsWithSelectedField(); + @Test public abstract void existsReturnsTrueForExistingSynonymMap(); + @Test public abstract void existsReturnsFalseForNonExistingSynonymMap(); protected void assertSynonymMapsEqual(SynonymMap actual, SynonymMap expected) { diff --git a/sdk/search/azure-search/src/test/resources/session-records/existsReturnsFalseForNonExistingSynonymMap.json b/sdk/search/azure-search/src/test/resources/session-records/existsReturnsFalseForNonExistingSynonymMap.json new file mode 100644 index 0000000000000..8906e870500d3 --- /dev/null +++ b/sdk/search/azure-search/src/test/resources/session-records/existsReturnsFalseForNonExistingSynonymMap.json @@ -0,0 +1,70 @@ +{ + "networkCallRecords" : [ { + "Method" : "GET", + "Uri" : "https://azs-sdkb01486379a06.search.windows.net/synonymmaps('thisSynonymMapDoesNotExist')?api-version=2019-05-06", + "Headers" : { }, + "Response" : { + "Pragma" : "no-cache", + "retry-after" : "0", + "request-id" : "50d1a7f8-1a1f-48d5-a966-ee9533fef79f", + "StatusCode" : "404", + "Date" : "Wed, 30 Oct 2019 00:35:58 GMT", + "Strict-Transport-Security" : "max-age=15724800; includeSubDomains", + "Cache-Control" : "no-cache", + "elapsed-time" : "31", + "OData-Version" : "4.0", + "Expires" : "-1", + "Content-Length" : "135", + "Body" : "{\"error\":{\"code\":\"\",\"message\":\"No synonym map with the name 'thisSynonymMapDoesNotExist' was found in service 'azs-sdkb01486379a06'.\"}}", + "Preference-Applied" : "odata.include-annotations=\"*\"", + "Content-Language" : "en", + "Content-Type" : "application/json; odata.metadata=minimal" + }, + "Exception" : null + }, { + "Method" : "GET", + "Uri" : "https://azs-sdkb01486379a06.search.windows.net/synonymmaps('thisSynonymMapDoesNotExist')?api-version=2019-05-06", + "Headers" : { }, + "Response" : { + "Pragma" : "no-cache", + "retry-after" : "0", + "request-id" : "95416ba8-87a7-49f3-9d42-ad5335a07018", + "StatusCode" : "404", + "Date" : "Wed, 30 Oct 2019 00:35:58 GMT", + "Strict-Transport-Security" : "max-age=15724800; includeSubDomains", + "Cache-Control" : "no-cache", + "elapsed-time" : "5", + "OData-Version" : "4.0", + "Expires" : "-1", + "Content-Length" : "135", + "Body" : "{\"error\":{\"code\":\"\",\"message\":\"No synonym map with the name 'thisSynonymMapDoesNotExist' was found in service 'azs-sdkb01486379a06'.\"}}", + "Preference-Applied" : "odata.include-annotations=\"*\"", + "Content-Language" : "en", + "Content-Type" : "application/json; odata.metadata=minimal" + }, + "Exception" : null + }, { + "Method" : "GET", + "Uri" : "https://azs-sdkb01486379a06.search.windows.net/synonymmaps('thisSynonymMapDoesNotExist')?api-version=2019-05-06", + "Headers" : { }, + "Response" : { + "Pragma" : "no-cache", + "retry-after" : "0", + "request-id" : "95416ba8-87a7-49f3-9d42-ad5335a07018", + "StatusCode" : "404", + "Date" : "Wed, 30 Oct 2019 00:35:58 GMT", + "Strict-Transport-Security" : "max-age=15724800; includeSubDomains", + "Cache-Control" : "no-cache", + "elapsed-time" : "4", + "OData-Version" : "4.0", + "Expires" : "-1", + "Content-Length" : "135", + "Body" : "{\"error\":{\"code\":\"\",\"message\":\"No synonym map with the name 'thisSynonymMapDoesNotExist' was found in service 'azs-sdkb01486379a06'.\"}}", + "Preference-Applied" : "odata.include-annotations=\"*\"", + "Content-Language" : "en", + "Content-Type" : "application/json; odata.metadata=minimal" + }, + "Exception" : null + } ], + "variables" : [ ] +} \ No newline at end of file diff --git a/sdk/search/azure-search/src/test/resources/session-records/existsReturnsTrueForExistingSynonymMap.json b/sdk/search/azure-search/src/test/resources/session-records/existsReturnsTrueForExistingSynonymMap.json new file mode 100644 index 0000000000000..7b332c0164061 --- /dev/null +++ b/sdk/search/azure-search/src/test/resources/session-records/existsReturnsTrueForExistingSynonymMap.json @@ -0,0 +1,95 @@ +{ + "networkCallRecords" : [ { + "Method" : "POST", + "Uri" : "https://azs-sdkbf09249480b7.search.windows.net/synonymmaps?api-version=2019-05-06", + "Headers" : { + "Content-Type" : "application/json; charset=utf-8" + }, + "Response" : { + "Pragma" : "no-cache", + "retry-after" : "0", + "request-id" : "3ee38fc8-fd66-4a0d-8cae-9617e74ee1f2", + "StatusCode" : "201", + "Date" : "Wed, 30 Oct 2019 00:37:21 GMT", + "Strict-Transport-Security" : "max-age=15724800; includeSubDomains", + "Cache-Control" : "no-cache", + "ETag" : "W/\"0x8D75CD15341222C\"", + "elapsed-time" : "61", + "OData-Version" : "4.0", + "Expires" : "-1", + "Content-Length" : "198", + "Body" : "{\"@odata.context\":\"https://azs-sdkbf09249480b7.search.windows.net/$metadata#synonymmaps/$entity\",\"@odata.etag\":\"\\\"0x8D75CD15341222C\\\"\",\"name\":\"test-synonym\",\"format\":\"solr\",\"synonyms\":\"word1,word2\"}", + "Preference-Applied" : "odata.include-annotations=\"*\"", + "Content-Type" : "application/json; odata.metadata=minimal", + "Location" : "https://azs-sdkbf09249480b7.search.windows.net/synonymmaps('test-synonym')?api-version=2019-05-06" + }, + "Exception" : null + }, { + "Method" : "GET", + "Uri" : "https://azs-sdkbf09249480b7.search.windows.net/synonymmaps('test-synonym')?api-version=2019-05-06", + "Headers" : { }, + "Response" : { + "Pragma" : "no-cache", + "retry-after" : "0", + "request-id" : "5abc08d2-40c2-46c8-9604-e4748bda0622", + "StatusCode" : "200", + "Date" : "Wed, 30 Oct 2019 00:37:21 GMT", + "Strict-Transport-Security" : "max-age=15724800; includeSubDomains", + "Cache-Control" : "no-cache", + "ETag" : "W/\"0x8D75CD15341222C\"", + "elapsed-time" : "8", + "OData-Version" : "4.0", + "Expires" : "-1", + "Content-Length" : "198", + "Body" : "{\"@odata.context\":\"https://azs-sdkbf09249480b7.search.windows.net/$metadata#synonymmaps/$entity\",\"@odata.etag\":\"\\\"0x8D75CD15341222C\\\"\",\"name\":\"test-synonym\",\"format\":\"solr\",\"synonyms\":\"word1,word2\"}", + "Preference-Applied" : "odata.include-annotations=\"*\"", + "Content-Type" : "application/json; odata.metadata=minimal" + }, + "Exception" : null + }, { + "Method" : "GET", + "Uri" : "https://azs-sdkbf09249480b7.search.windows.net/synonymmaps('test-synonym')?api-version=2019-05-06", + "Headers" : { }, + "Response" : { + "Pragma" : "no-cache", + "retry-after" : "0", + "request-id" : "4e802b09-b043-46e0-ac44-ae4b2ed2114e", + "StatusCode" : "200", + "Date" : "Wed, 30 Oct 2019 00:37:21 GMT", + "Strict-Transport-Security" : "max-age=15724800; includeSubDomains", + "Cache-Control" : "no-cache", + "ETag" : "W/\"0x8D75CD15341222C\"", + "elapsed-time" : "6", + "OData-Version" : "4.0", + "Expires" : "-1", + "Content-Length" : "198", + "Body" : "{\"@odata.context\":\"https://azs-sdkbf09249480b7.search.windows.net/$metadata#synonymmaps/$entity\",\"@odata.etag\":\"\\\"0x8D75CD15341222C\\\"\",\"name\":\"test-synonym\",\"format\":\"solr\",\"synonyms\":\"word1,word2\"}", + "Preference-Applied" : "odata.include-annotations=\"*\"", + "Content-Type" : "application/json; odata.metadata=minimal" + }, + "Exception" : null + }, { + "Method" : "GET", + "Uri" : "https://azs-sdkbf09249480b7.search.windows.net/synonymmaps('test-synonym')?api-version=2019-05-06", + "Headers" : { }, + "Response" : { + "Pragma" : "no-cache", + "retry-after" : "0", + "request-id" : "4e802b09-b043-46e0-ac44-ae4b2ed2114e", + "StatusCode" : "200", + "Date" : "Wed, 30 Oct 2019 00:37:21 GMT", + "Strict-Transport-Security" : "max-age=15724800; includeSubDomains", + "Cache-Control" : "no-cache", + "ETag" : "W/\"0x8D75CD15341222C\"", + "elapsed-time" : "5", + "OData-Version" : "4.0", + "Expires" : "-1", + "Content-Length" : "198", + "Body" : "{\"@odata.context\":\"https://azs-sdkbf09249480b7.search.windows.net/$metadata#synonymmaps/$entity\",\"@odata.etag\":\"\\\"0x8D75CD15341222C\\\"\",\"name\":\"test-synonym\",\"format\":\"solr\",\"synonyms\":\"word1,word2\"}", + "Preference-Applied" : "odata.include-annotations=\"*\"", + "Content-Type" : "application/json; odata.metadata=minimal" + }, + "Exception" : null + } ], + "variables" : [ ] +} \ No newline at end of file