diff --git a/indexer-core/src/test/java/org/opengroup/osdu/indexer/service/IndicesServiceTest.java b/indexer-core/src/test/java/org/opengroup/osdu/indexer/service/IndicesServiceTest.java index 8fb08c6e6ac0be3e3116cec674108af1a6f8231d..8e975d58bebf7b2fdde4bb8fbb9a0374993a9b4d 100644 --- a/indexer-core/src/test/java/org/opengroup/osdu/indexer/service/IndicesServiceTest.java +++ b/indexer-core/src/test/java/org/opengroup/osdu/indexer/service/IndicesServiceTest.java @@ -24,8 +24,11 @@ import org.apache.http.util.EntityUtils; import org.elasticsearch.ElasticsearchStatusException; import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest; import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse; +import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest; import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.client.*; +import org.elasticsearch.client.indices.CreateIndexRequest; +import org.elasticsearch.client.indices.CreateIndexResponse; import org.elasticsearch.client.indices.GetIndexRequest; import org.elasticsearch.client.indices.GetIndexResponse; import org.elasticsearch.rest.RestStatus; @@ -47,10 +50,12 @@ import org.springframework.test.context.junit4.SpringRunner; import java.io.IOException; import java.lang.reflect.Type; +import java.util.HashMap; import java.util.List; import static junit.framework.TestCase.assertTrue; import static org.junit.Assert.*; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.*; import static org.mockito.MockitoAnnotations.initMocks; @@ -87,6 +92,51 @@ public class IndicesServiceTest { restHighLevelClient = PowerMockito.mock(RestHighLevelClient.class); } + @Test + public void create_elasticIndex() throws Exception { + String kind = "common:welldb:wellbore:1.2.0"; + String index = "common-welldb-wellbore-1.2.0"; + CreateIndexResponse indexResponse = new CreateIndexResponse(true, true, index); + AcknowledgedResponse acknowledgedResponse = new AcknowledgedResponse(true); + + when(elasticIndexNameResolver.getKindFromIndexName(any())).thenReturn(kind); + when(elasticIndexNameResolver.getIndexNameFromKind(any())).thenReturn(index); + when(elasticIndexNameResolver.getIndexAliasFromKind(any())).thenReturn("a12345678"); + when(restHighLevelClient.indices()).thenReturn(indicesClient); + when(indicesClient.create(any(CreateIndexRequest.class), any(RequestOptions.class))).thenReturn(indexResponse); + when(indicesClient.updateAliases(any(IndicesAliasesRequest.class), any(RequestOptions.class))).thenReturn(acknowledgedResponse); + boolean response = this.sut.createIndex(restHighLevelClient, index, null, "anytype", new HashMap<>()); + assertTrue(response); + when(this.indicesExistCache.get(index)).thenReturn(true); + verify(this.indicesClient, times(2)).updateAliases(any(IndicesAliasesRequest.class), any(RequestOptions.class)); + } + + @Test + public void create_elasticIndex_fail() throws Exception { + String index = "common-welldb-wellbore-1.2.0"; + CreateIndexResponse indexResponse = new CreateIndexResponse(false, false, index); + + when(restHighLevelClient.indices()).thenReturn(indicesClient); + when(indicesClient.create(any(CreateIndexRequest.class), any(RequestOptions.class))).thenReturn(indexResponse); + boolean response = this.sut.createIndex(restHighLevelClient, index, null, "anytype", new HashMap<>()); + assertFalse(response); + verify(this.indicesExistCache, times(0)).put(any(), any()); + verify(this.indicesClient, times(0)).updateAliases(any(IndicesAliasesRequest.class), any(RequestOptions.class)); + } + + @Test + public void create_existingElasticIndex() throws Exception { + String index = "common-welldb-wellbore-1.2.0"; + ElasticsearchStatusException elasticsearchStatusException = new ElasticsearchStatusException("resource_already_exists_exception", RestStatus.BAD_REQUEST); + + when(restHighLevelClient.indices()).thenReturn(indicesClient); + when(indicesClient.create(any(CreateIndexRequest.class), any(RequestOptions.class))).thenThrow(elasticsearchStatusException); + boolean response = this.sut.createIndex(restHighLevelClient, index, null, "anytype", new HashMap<>()); + assertTrue(response); + verify(this.indicesExistCache, times(1)).put(any(), any()); + verify(this.indicesClient, times(0)).updateAliases(any(IndicesAliasesRequest.class), any(RequestOptions.class)); + } + @Test public void delete_existingElasticIndex() throws Exception { AcknowledgedResponse indexResponse = new AcknowledgedResponse(true); @@ -290,4 +340,4 @@ public class IndicesServiceTest { verify(this.indicesExistCache, times(1)).get("anyIndex"); verify(this.indicesExistCache, times(1)).put("anyIndex", true); } -} \ No newline at end of file +}