diff --git a/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/IndicesServiceImpl.java b/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/IndicesServiceImpl.java index 6672dc8d1e1de6582c233cc224b37bfc50ae2994..3cc6ed21b90bd6e26b24d9c2e157fe0bd8e3c47d 100644 --- a/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/IndicesServiceImpl.java +++ b/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/IndicesServiceImpl.java @@ -123,13 +123,7 @@ public class IndicesServiceImpl implements IndexerIndicesService { */ public boolean isIndexExist(RestHighLevelClient client, String index) throws IOException { try { - try { - Boolean isIndexExist = (Boolean) this.indexCache.get(index); - if (isIndexExist != null && isIndexExist) return true; - } catch (RedisException ex) { - //In case the format of cache changes then clean the cache - this.indexCache.delete(index); - } + if (this.indexExistInCache(index)) return true; GetIndexRequest request = new GetIndexRequest(index); boolean exists = client.indices().exists(request, RequestOptions.DEFAULT); if (exists) this.indexCache.put(index, true); @@ -145,7 +139,7 @@ public class IndicesServiceImpl implements IndexerIndicesService { } /** - * Check if an index already exists + * Check if an index ready for indexing * * @param index Index name * @return index details if index already exists @@ -153,13 +147,7 @@ public class IndicesServiceImpl implements IndexerIndicesService { */ public boolean isIndexReady(RestHighLevelClient client, String index) throws IOException { try { - try { - Boolean isIndexExist = (Boolean) this.indexCache.get(index); - if (isIndexExist != null && isIndexExist) return true; - } catch (RedisException ex) { - //In case the format of cache changes then clean the cache - this.indexCache.delete(index); - } + if (this.indexExistInCache(index)) return true; GetIndexRequest request = new GetIndexRequest(index); boolean exists = client.indices().exists(request, RequestOptions.DEFAULT); if (!exists) return false; @@ -183,6 +171,17 @@ public class IndicesServiceImpl implements IndexerIndicesService { } } + private boolean indexExistInCache(String index) { + try { + Boolean isIndexExist = (Boolean) this.indexCache.get(index); + if (isIndexExist != null && isIndexExist) return true; + } catch (RedisException ex) { + //In case the format of cache changes then clean the cache + this.indexCache.delete(index); + } + return false; + } + /** * Deletes index if user has required role: search.admin * 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 0051012ef572f075e91df197310e2557786c69ce..3f52c4c6c77349ea4996f1e3ef7ceed8aa93f07f 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 @@ -23,8 +23,11 @@ import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; 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.support.master.AcknowledgedResponse; import org.elasticsearch.client.*; +import org.elasticsearch.client.indices.GetIndexRequest; import org.elasticsearch.rest.RestStatus; import org.junit.Before; import org.junit.Test; @@ -52,7 +55,7 @@ import static org.mockito.Mockito.*; import static org.mockito.MockitoAnnotations.initMocks; @RunWith(SpringRunner.class) -@PrepareForTest({RestHighLevelClient.class, IndicesClient.class, EntityUtils.class}) +@PrepareForTest({RestHighLevelClient.class, IndicesClient.class, ClusterClient.class, EntityUtils.class}) public class IndicesServiceTest { @Mock private ElasticClientHandler elasticClientHandler; @@ -74,11 +77,13 @@ public class IndicesServiceTest { private RestHighLevelClient restHighLevelClient; private IndicesClient indicesClient; + private ClusterClient clusterClient; @Before public void setup() { initMocks(this); indicesClient = PowerMockito.mock(IndicesClient.class); + clusterClient = PowerMockito.mock(ClusterClient.class); restHighLevelClient = PowerMockito.mock(RestHighLevelClient.class); } @@ -219,4 +224,54 @@ public class IndicesServiceTest { assertEquals("1", indicesList.get(0).getDocumentCount()); assertEquals("1551996907769", indicesList.get(0).getCreationDate()); } + + @Test + public void should_returnTrue_indexExistInCache() throws IOException { + when(this.indicesExistCache.get("anyIndex")).thenReturn(true); + + boolean result = this.sut.isIndexExist(any(RestHighLevelClient.class), "anyIndex"); + + assertTrue(result); + } + + @Test + public void should_getIndexExist_whenIndexNotInCache() throws IOException { + when(this.indicesExistCache.get("anyIndex")).thenReturn(false); + + doReturn(indicesClient).when(restHighLevelClient).indices(); + doReturn(true).when(indicesClient).exists(any(GetIndexRequest.class), any(RequestOptions.class)); + + boolean result = this.sut.isIndexExist(restHighLevelClient, "anyIndex"); + + assertTrue(result); + verify(this.indicesExistCache, times(1)).get("anyIndex"); + verify(this.indicesExistCache, times(1)).put("anyIndex", true); + } + + @Test + public void should_getIndexReadyStatus_whenIndexInCache() throws IOException { + when(this.indicesExistCache.get("anyIndex")).thenReturn(true); + + boolean result = this.sut.isIndexReady(any(RestHighLevelClient.class), "anyIndex"); + + assertTrue(result); + } + + @Test + public void should_getIndexReadyStatus_whenIndexNotInCache() throws IOException { + when(this.indicesExistCache.get("anyIndex")).thenReturn(false); + doReturn(indicesClient).when(restHighLevelClient).indices(); + doReturn(true).when(indicesClient).exists(any(GetIndexRequest.class), any(RequestOptions.class)); + + ClusterHealthResponse healthResponse = mock(ClusterHealthResponse.class); + when(healthResponse.status()).thenReturn(RestStatus.OK); + doReturn(clusterClient).when(restHighLevelClient).cluster(); + doReturn(healthResponse).when(clusterClient).health(any(ClusterHealthRequest.class), any(RequestOptions.class)); + + boolean result = this.sut.isIndexReady(restHighLevelClient, "anyIndex"); + + assertTrue(result); + verify(this.indicesExistCache, times(1)).get("anyIndex"); + verify(this.indicesExistCache, times(1)).put("anyIndex", true); + } } \ No newline at end of file