Skip to content

Commit

Permalink
Adds APIs for checking synonym map existence (Azure#209)
Browse files Browse the repository at this point in the history
* Adds APIs for checking synonym map existence
  • Loading branch information
sakintoye authored Oct 30, 2019
1 parent 0d4bee5 commit f4c9e8f
Show file tree
Hide file tree
Showing 7 changed files with 348 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -593,7 +594,7 @@ Mono<Response<Index>> getIndexWithResponse(String indexName, RequestOptions requ
* @return true if the index exists; false otherwise.
*/
public Mono<Boolean> indexExists(String indexName) {
return indexExistsWithResponse(indexName, null).map(Response::getValue);
return this.indexExistsWithResponse(indexName, null).map(Response::getValue);
}

/**
Expand All @@ -605,7 +606,7 @@ public Mono<Boolean> indexExists(String indexName) {
* @return true if the index exists; false otherwise.
*/
public Mono<Boolean> indexExists(String indexName, RequestOptions requestOptions) {
return indexExistsWithResponse(indexName, requestOptions).map(Response::getValue);
return this.indexExistsWithResponse(indexName, requestOptions).map(Response::getValue);
}

/**
Expand All @@ -617,22 +618,13 @@ public Mono<Boolean> indexExists(String indexName, RequestOptions requestOptions
* @return true if the index exists; false otherwise.
*/
public Mono<Response<Boolean>> indexExistsWithResponse(String indexName, RequestOptions requestOptions) {
return withContext(context -> indexExistsWithResponse(indexName, requestOptions, context));
return withContext(context -> this.indexExistsWithResponse(indexName, requestOptions, context));
}

Mono<Response<Boolean>> indexExistsWithResponse(String indexName,
RequestOptions requestOptions,
Context context) {
return this.getIndexWithResponse(indexName, requestOptions, context)
.map(i -> (Response<Boolean>) 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));
}

/**
Expand Down Expand Up @@ -1186,6 +1178,67 @@ public Mono<Response<Response<Void>>> 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<Boolean> 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<Boolean> 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<Response<Boolean>> synonymMapExistsWithResponse(String synonymMapName, RequestOptions requestOptions) {
return withContext(context -> this.synonymMapExistsWithResponse(synonymMapName, requestOptions, context));
}

Mono<Response<Boolean>> 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 <T> Mono<Response<Boolean>> resourceExistsWithResponse(Supplier<Mono<Response<T>>> action) {
return action.get()
.map(i ->
(Response<Boolean>) 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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1069,4 +1069,57 @@ public void deleteSynonymMap() {
public Response<Response<Void>> 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<Boolean> synonymMapExistsWithResponse(String synonymMapName,
RequestOptions requestOptions,
Context context) {
return asyncClient
.synonymMapExistsWithResponse(synonymMapName, requestOptions, context)
.block();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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" : [ ]
}
Loading

0 comments on commit f4c9e8f

Please sign in to comment.