Skip to content
Snippets Groups Projects
Commit c851f910 authored by Neelesh Thakur's avatar Neelesh Thakur
Browse files

unit tests

parent a6bb08ab
No related branches found
No related tags found
2 merge requests!346Merge branch 'aws-integration' into 'master',!235Wait for primary shards to be ready before start indexing
Pipeline #77763 waiting for manual action
......@@ -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
*
......
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment