Skip to content

Commit

Permalink
Implement delete synonym map scenarios (Azure#226)
Browse files Browse the repository at this point in the history
* Implement delete synonym scenarios
  • Loading branch information
sakintoye authored Nov 6, 2019
1 parent eab743b commit c5c35b9
Show file tree
Hide file tree
Showing 8 changed files with 468 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1288,19 +1288,69 @@ Mono<Response<SynonymMap>> createOrUpdateSynonymMapWithResponse(SynonymMap synon
}

/**
* @return a reactive response signalling completion.
* @throws NotImplementedException not implemented
* Deletes an Azure Cognitive Search synonym map.
*
* @param synonymMapName the name of the synonym map to delete
* @return a response signalling completion.
*/
public Mono<Void> deleteSynonymMap() {
throw logger.logExceptionAsError(new NotImplementedException("not implemented."));
public Mono<Void> deleteSynonymMap(String synonymMapName) {
return this.deleteSynonymMap(synonymMapName, null, null);
}

/**
* @return a reactive response signalling completion.
* @throws NotImplementedException not implemented
* Deletes an Azure Cognitive Search synonym map.
*
* @param synonymMapName the name of the synonym map to delete
* @param accessCondition the condition where the operation will be performed if the ETag on the server matches or
* doesn't match specified values
* @return a response signalling completion.
*/
public Mono<Response<Response<Void>>> deleteSynonymMapWithResponse() {
throw logger.logExceptionAsError(new NotImplementedException("not implemented."));
public Mono<Void> deleteSynonymMap(String synonymMapName, AccessCondition accessCondition) {
return this.deleteSynonymMap(synonymMapName, accessCondition, null);
}

/**
* Deletes an Azure Cognitive Search synonym map.
*
* @param synonymMapName the name of the synonym map to delete
* @param accessCondition the condition where the operation will be performed if the ETag on the server matches or
* doesn't match specified values
* @param requestOptions additional parameters for the operation.
* Contains the tracking ID sent with the request to help with debugging
* @return a response signalling completion.
*/
public Mono<Void> deleteSynonymMap(String synonymMapName,
AccessCondition accessCondition,
RequestOptions requestOptions) {
return this.deleteSynonymMapWithResponse(synonymMapName, accessCondition, requestOptions)
.flatMap(FluxUtil::toMono);
}

/**
* Deletes an Azure Cognitive Search synonym map.
*
* @param synonymMapName the name of the synonym map to delete
* @param accessCondition the condition where the operation will be performed if the ETag on the server matches or
* doesn't match specified values
* @param requestOptions additional parameters for the operation.
* Contains the tracking ID sent with the request to help with debugging
* @return a response signalling completion.
*/
public Mono<Response<Void>> deleteSynonymMapWithResponse(String synonymMapName,
AccessCondition accessCondition,
RequestOptions requestOptions) {
return withContext(context ->
this.deleteSynonymMapWithResponse(synonymMapName, accessCondition, requestOptions, context));
}

Mono<Response<Void>> deleteSynonymMapWithResponse(String synonymMapName,
AccessCondition accessCondition,
RequestOptions requestOptions,
Context context) {
return restClient
.synonymMaps()
.deleteWithRestResponseAsync(synonymMapName, requestOptions, accessCondition, context)
.map(Function.identity());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1200,18 +1200,80 @@ public Response<SynonymMap> createOrUpdateSynonymMapWithResponse(SynonymMap syno
}

/**
* @throws NotImplementedException not implemented
* Deletes an Azure Cognitive Search synonym map.
*
* @param synonymMapName the name of the synonym map to delete
*/
public void deleteSynonymMap() {
throw logger.logExceptionAsError(new NotImplementedException("not implemented."));
public void deleteSynonymMap(String synonymMapName) {
asyncClient.deleteSynonymMap(synonymMapName).block();
}

/**
* @return a response signalling completion
* @throws NotImplementedException not implemented
* Deletes an Azure Cognitive Search synonym map.
*
* @param synonymMapName the name of the synonym map to delete
* @param accessCondition the condition where the operation will be performed if the ETag on the server matches or
* doesn't match specified values
*/
public Response<Response<Void>> deleteSynonymMapWithResponse() {
throw logger.logExceptionAsError(new NotImplementedException("not implemented."));
public void deleteSynonymMap(String synonymMapName, AccessCondition accessCondition) {
asyncClient.deleteSynonymMap(synonymMapName, accessCondition).block();
}

/**
* Deletes an Azure Cognitive Search synonym map.
*
* @param synonymMapName the name of the synonym map to delete
* @param accessCondition the condition where the operation will be performed if the ETag on the server matches or
* doesn't match specified values
* @param requestOptions additional parameters for the operation.
* Contains the tracking ID sent with the request to help with debugging
*/
public void deleteSynonymMap(String synonymMapName,
AccessCondition accessCondition,
RequestOptions requestOptions) {
asyncClient.deleteSynonymMap(synonymMapName, accessCondition, requestOptions).block();
}

/**
* Deletes an Azure Cognitive Search synonym map.
*
* @param synonymMapName the name of the synonym map to delete
* @param accessCondition the condition where the operation will be performed if the ETag on the server matches or
* doesn't match specified values
* @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
* Contains the tracking ID sent with the request to help with debugging
*/
public void deleteSynonymMap(String synonymMapName,
AccessCondition accessCondition,
RequestOptions requestOptions,
Context context) {
this.deleteSynonymMapWithResponse(synonymMapName,
accessCondition,
requestOptions,
context);
}

/**
* Deletes an Azure Cognitive Search synonym map.
*
* @param synonymMapName the name of the synonym map to delete
* @param accessCondition the condition where the operation will be performed if the ETag on the server matches or
* doesn't match specified values
* @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 a response signalling completion.
*/
public Response<Void> deleteSynonymMapWithResponse(String synonymMapName,
AccessCondition accessCondition,
RequestOptions requestOptions,
Context context) {
return asyncClient.deleteSynonymMapWithResponse(synonymMapName,
accessCondition,
requestOptions,
context).block();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.azure.core.exception.HttpResponseException;

import com.azure.core.http.rest.PagedFlux;
import com.azure.core.http.rest.Response;
import com.azure.search.models.AccessCondition;
import com.azure.search.models.RequestOptions;
import com.azure.search.models.SynonymMap;
Expand Down Expand Up @@ -266,17 +267,70 @@ public void createOrUpdateSynonymMapIfNotChangedFailsWhenResourceChanged() {

@Override
public void deleteSynonymMapIfNotChangedWorksOnlyOnCurrentResource() {
SynonymMap synonymMap = createTestSynonymMap();
SynonymMap staleResource = client.createOrUpdateSynonymMap(synonymMap).block();
SynonymMap currentResource = client.createOrUpdateSynonymMap(staleResource.setSynonyms("updatedword1,updatedword2")).block();

StepVerifier
.create(client.deleteSynonymMap(synonymMap.getName(), generateIfMatchAccessCondition(staleResource.getETag())))
.verifyErrorSatisfies(error -> {
Assert.assertEquals(HttpResponseException.class, error.getClass());
Assert.assertEquals(HttpResponseStatus.PRECONDITION_FAILED.code(), ((HttpResponseException) error).getResponse().getStatusCode());
});

Response<Void> response = client.deleteSynonymMapWithResponse(synonymMap.getName(),
generateIfMatchAccessCondition(currentResource.getETag()),
null,
null
).block();
Assert.assertEquals(HttpResponseStatus.NO_CONTENT.code(), response.getStatusCode());
}

@Override
public void deleteSynonymMapIfExistsWorksOnlyWhenResourceExists() {
SynonymMap synonymMap = createTestSynonymMap();
client.createSynonymMap(synonymMap).block();

client.deleteSynonymMap(synonymMap.getName(), generateIfExistsAccessCondition()).block();
StepVerifier
.create(client.deleteSynonymMap(synonymMap.getName(), generateIfExistsAccessCondition()))
.verifyErrorSatisfies(error -> {
Assert.assertEquals(HttpResponseException.class, error.getClass());
Assert.assertEquals(HttpResponseStatus.PRECONDITION_FAILED.code(), ((HttpResponseException) error).getResponse().getStatusCode());
});
}

@Override
public void deleteSynonymMapIsIdempotent() {
SynonymMap synonymMap = createTestSynonymMap();

StepVerifier
.create(client.deleteSynonymMapWithResponse(synonymMap.getName(), null, null, null))
.assertNext(synonymMapResponse -> {
Assert.assertEquals(HttpResponseStatus.NOT_FOUND.code(), synonymMapResponse.getStatusCode());
})
.verifyComplete();

StepVerifier
.create(client.createSynonymMapWithResponse(synonymMap, null, null))
.assertNext(synonymMapResponse -> {
Assert.assertEquals(HttpResponseStatus.CREATED.code(), synonymMapResponse.getStatusCode());
})
.verifyComplete();

StepVerifier
.create(client.deleteSynonymMapWithResponse(synonymMap.getName(), null, null, null))
.assertNext(synonymMapResponse -> {
Assert.assertEquals(HttpResponseStatus.NO_CONTENT.code(), synonymMapResponse.getStatusCode());
})
.verifyComplete();

StepVerifier
.create(client.deleteSynonymMapWithResponse(synonymMap.getName(), null, null, null))
.assertNext(synonymMapResponse -> {
Assert.assertEquals(HttpResponseStatus.NOT_FOUND.code(), synonymMapResponse.getStatusCode());
})
.verifyComplete();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,17 +243,54 @@ public void createOrUpdateSynonymMapIfNotChangedFailsWhenResourceChanged() {

@Override
public void deleteSynonymMapIfNotChangedWorksOnlyOnCurrentResource() {
SynonymMap synonymMap = createTestSynonymMap();
SynonymMap staleResource = client.createOrUpdateSynonymMap(synonymMap);
SynonymMap currentResource = client.createOrUpdateSynonymMap(staleResource.setSynonyms("updatedword1,updatedword2"));

try {
client.deleteSynonymMap(synonymMap.getName(), generateIfMatchAccessCondition(staleResource.getETag()));
Assert.fail("deleteSynonym did not throw an expected Exception");
} catch (Exception ex) {
Assert.assertEquals(HttpResponseException.class, ex.getClass());
Assert.assertEquals(HttpResponseStatus.PRECONDITION_FAILED.code(), ((HttpResponseException) ex).getResponse().getStatusCode());
}

Response<Void> response = client.deleteSynonymMapWithResponse(synonymMap.getName(),
generateIfMatchAccessCondition(currentResource.getETag()),
null,
null);
Assert.assertEquals(HttpResponseStatus.NO_CONTENT.code(), response.getStatusCode());
}

@Override
public void deleteSynonymMapIfExistsWorksOnlyWhenResourceExists() {
SynonymMap synonymMap = createTestSynonymMap();
client.createSynonymMap(synonymMap);

client.deleteSynonymMap(synonymMap.getName(), generateIfExistsAccessCondition());
try {
client.deleteSynonymMap(synonymMap.getName(), generateIfExistsAccessCondition());
Assert.fail("deleteSynonymMap did not throw an expected Exception");
} catch (Exception ex) {
Assert.assertEquals(HttpResponseException.class, ex.getClass());
Assert.assertEquals(HttpResponseStatus.PRECONDITION_FAILED.code(), ((HttpResponseException) ex).getResponse().getStatusCode());
}
}

@Override
public void deleteSynonymMapIsIdempotent() {
SynonymMap synonymMap = createTestSynonymMap();
Response<Void> deleteResponse = client.deleteSynonymMapWithResponse(synonymMap.getName(), null, null, null);
Assert.assertEquals(HttpResponseStatus.NOT_FOUND.code(), deleteResponse.getStatusCode());

Response<SynonymMap> createResponse = client.createSynonymMapWithResponse(synonymMap, null, null);
Assert.assertEquals(HttpResponseStatus.CREATED.code(), createResponse.getStatusCode());

deleteResponse = client.deleteSynonymMapWithResponse(synonymMap.getName(), null, null, null);
Assert.assertEquals(HttpResponseStatus.NO_CONTENT.code(), deleteResponse.getStatusCode());

deleteResponse = client.deleteSynonymMapWithResponse(synonymMap.getName(), null, null, null);
Assert.assertEquals(HttpResponseStatus.NOT_FOUND.code(), deleteResponse.getStatusCode());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,13 @@ public abstract class SynonymMapManagementTestBase extends SearchServiceTestBase
@Test
public abstract void createOrUpdateSynonymMapIfNotChangedFailsWhenResourceChanged();

@Test
public abstract void deleteSynonymMapIfNotChangedWorksOnlyOnCurrentResource();

@Test
public abstract void deleteSynonymMapIfExistsWorksOnlyWhenResourceExists();

@Test
public abstract void deleteSynonymMapIsIdempotent();

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"networkCallRecords" : [ {
"Method" : "POST",
"Uri" : "https://azs-sdk2f925909d842.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" : "a2a81072-a279-4156-b958-b5ccdcdd2738",
"StatusCode" : "201",
"Date" : "Wed, 06 Nov 2019 00:16:38 GMT",
"Strict-Transport-Security" : "max-age=15724800; includeSubDomains",
"Cache-Control" : "no-cache",
"ETag" : "W/\"0x8D7624E97B6E25F\"",
"elapsed-time" : "66",
"OData-Version" : "4.0",
"Expires" : "-1",
"Content-Length" : "198",
"Body" : "{\"@odata.context\":\"https://azs-sdk2f925909d842.search.windows.net/$metadata#synonymmaps/$entity\",\"@odata.etag\":\"\\\"0x8D7624E97B6E25F\\\"\",\"name\":\"test-synonym\",\"format\":\"solr\",\"synonyms\":\"word1,word2\"}",
"Preference-Applied" : "odata.include-annotations=\"*\"",
"Content-Type" : "application/json; odata.metadata=minimal",
"Location" : "https://azs-sdk2f925909d842.search.windows.net/synonymmaps('test-synonym')?api-version=2019-05-06"
},
"Exception" : null
}, {
"Method" : "DELETE",
"Uri" : "https://azs-sdk2f925909d842.search.windows.net/synonymmaps('test-synonym')?api-version=2019-05-06",
"Headers" : { },
"Response" : {
"Strict-Transport-Security" : "max-age=15724800; includeSubDomains",
"Cache-Control" : "no-cache",
"elapsed-time" : "29",
"Expires" : "-1",
"Pragma" : "no-cache",
"retry-after" : "0",
"request-id" : "406fcd1b-223c-4e32-a8b7-9230815e8d2e",
"StatusCode" : "204",
"Date" : "Wed, 06 Nov 2019 00:16:38 GMT"
},
"Exception" : null
}, {
"Method" : "DELETE",
"Uri" : "https://azs-sdk2f925909d842.search.windows.net/synonymmaps('test-synonym')?api-version=2019-05-06",
"Headers" : { },
"Response" : {
"Pragma" : "no-cache",
"retry-after" : "0",
"request-id" : "288b7fae-b7c1-4755-8fba-4154ebfdb38d",
"StatusCode" : "412",
"Date" : "Wed, 06 Nov 2019 00:16:38 GMT",
"Strict-Transport-Security" : "max-age=15724800; includeSubDomains",
"Cache-Control" : "no-cache",
"elapsed-time" : "6",
"OData-Version" : "4.0",
"Expires" : "-1",
"Content-Length" : "160",
"Body" : "{\"error\":{\"code\":\"\",\"message\":\"The precondition given in one of the request headers evaluated to false. No change was made to the resource from this request.\"}}",
"Preference-Applied" : "odata.include-annotations=\"*\"",
"Content-Language" : "en",
"Content-Type" : "application/json; odata.metadata=minimal"
},
"Exception" : null
} ],
"variables" : [ ]
}
Loading

0 comments on commit c5c35b9

Please sign in to comment.