forked from opensearch-project/opensearch-migrations
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add OpenSearchClientTest to verify creation and retry behavior
Signed-off-by: Peter Nied <[email protected]>
- Loading branch information
Showing
2 changed files
with
119 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
RFS/src/test/java/com/rfs/common/OpenSearchClientTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package com.rfs.common; | ||
|
||
import java.util.Optional; | ||
|
||
import com.fasterxml.jackson.core.StreamReadFeature; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.json.JsonMapper; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import com.rfs.common.http.HttpResponse; | ||
import com.rfs.tracing.IRfsContexts.ICheckedIdempotentPutRequestContext; | ||
import lombok.SneakyThrows; | ||
import reactor.core.publisher.Mono; | ||
|
||
import static org.hamcrest.CoreMatchers.containsString; | ||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.CoreMatchers.not; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
class OpenSearchClientTest { | ||
private static final ObjectMapper OBJECT_MAPPER = JsonMapper.builder() | ||
.enable(StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION) | ||
.build(); | ||
|
||
@Test | ||
void testCreateIndex() { | ||
// Setup | ||
var checkIfExistsResponse = new HttpResponse(404, "", null, "does not exist"); | ||
var createdItemRawJson = "{\"created\":\"yup!\"}"; | ||
var createItemResponse = new HttpResponse(200, "", null, createdItemRawJson); | ||
|
||
var restClient = mock(RestClient.class); | ||
when(restClient.getAsync(any(), any())).thenReturn(Mono.just(checkIfExistsResponse)); | ||
when(restClient.putAsync(any(), any(), any())).thenReturn(Mono.just(createItemResponse)); | ||
|
||
// Action | ||
var rawJson = "{ }"; | ||
var result = createIndex(restClient, rawJson); | ||
|
||
// Assertions | ||
assertThat(result.get().toPrettyString(), containsString(rawJson)); | ||
// The interface is to send back the passed json if on success | ||
assertThat(result.get().toPrettyString(), not(containsString(createdItemRawJson))); | ||
} | ||
|
||
@Test | ||
void testCreateIndex_alreadyExists() { | ||
var checkIfExistsResponse = new HttpResponse(200, "", null, "I exist!"); | ||
|
||
var restClient = mock(RestClient.class); | ||
when(restClient.getAsync(any(), any())).thenReturn(Mono.just(checkIfExistsResponse)); | ||
|
||
var rawJson = "{ }"; | ||
var result = createIndex(restClient, rawJson); | ||
|
||
assertThat(result, equalTo(Optional.empty())); | ||
} | ||
|
||
@Test | ||
void testCreateIndex_errorOnCreation_retried() { | ||
// Setup | ||
var checkIfExistsResponse = new HttpResponse(404, "", null, "does not exist"); | ||
var createdItemRawJson = "{\"error\":\"unauthorized\"}"; | ||
var createItemResponse = new HttpResponse(403, "", null, createdItemRawJson); | ||
|
||
var restClient = mock(RestClient.class); | ||
when(restClient.getAsync(any(), any())).thenReturn(Mono.just(checkIfExistsResponse)); | ||
when(restClient.putAsync(any(), any(), any())).thenReturn(Mono.just(createItemResponse)); | ||
|
||
// Action | ||
var rawJson = "{ }"; | ||
var exception = assertThrows(IllegalStateException.class, () -> createIndex(restClient, rawJson)); | ||
|
||
// Assertions | ||
assertThat(exception.getClass().getName(), containsString("RetryExhaustedException")); | ||
assertThat(exception.getMessage(), containsString("Retries exhausted")); | ||
assertThat(exception.getCause().getMessage(), containsString("403")); | ||
assertThat(exception.getCause().getMessage(), containsString("unauthorized")); | ||
// The interface is to send back the passed json if on success | ||
} | ||
|
||
@Test | ||
void testCreateIndex_errorOnCreation_notRetriedOnBadRequest() { | ||
// Setup | ||
var checkIfExistsResponse = new HttpResponse(404, "", null, "does not exist"); | ||
var createdItemRawJson = "{\"error\":\"illegal_argument_exception\"}"; | ||
var createItemResponse = new HttpResponse(400, "", null, createdItemRawJson); | ||
|
||
var restClient = mock(RestClient.class); | ||
when(restClient.getAsync(any(), any())).thenReturn(Mono.just(checkIfExistsResponse)); | ||
when(restClient.putAsync(any(), any(), any())).thenReturn(Mono.just(createItemResponse)); | ||
|
||
// Action | ||
var rawJson = "{ }"; | ||
var exception = assertThrows(InvalidResponse.class, () -> createIndex(restClient, rawJson)); | ||
|
||
// Assertions | ||
assertThat(exception.getMessage(), containsString("illegal_argument_exception")); | ||
} | ||
|
||
@SneakyThrows | ||
private Optional<ObjectNode> createIndex(RestClient restClient, String rawJson) { | ||
var openSearchClient = new OpenSearchClient(restClient); | ||
|
||
var body = (ObjectNode) OBJECT_MAPPER.readTree(rawJson); | ||
return openSearchClient.createIndex("indexName", body, mock(ICheckedIdempotentPutRequestContext.class)); | ||
} | ||
} |