Skip to content

Commit

Permalink
CheckElasticsearch indexes are reset before each tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sebr72 committed Nov 29, 2023
1 parent 2affbcf commit 1435543
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package org.fao.geonet.kernel;

import com.fasterxml.jackson.databind.JsonNode;
import jeeves.server.context.ServiceContext;
import org.elasticsearch.action.search.SearchResponse;
import org.fao.geonet.AbstractCoreIntegrationTest;
import org.fao.geonet.domain.AbstractMetadata;
import org.fao.geonet.index.es.EsRestClient;
import org.fao.geonet.kernel.search.EsSearchManager;
import org.fao.geonet.kernel.setting.SettingManager;
import org.fao.geonet.kernel.setting.Settings;
Expand All @@ -13,8 +15,12 @@
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import static org.junit.Assert.*;

public class ElasticsearchIndexingTest extends AbstractIntegrationTestWithMockedSingletons {
Expand All @@ -31,6 +37,7 @@ public class ElasticsearchIndexingTest extends AbstractIntegrationTestWithMocked
public void setUp() throws Exception {
serviceContext = createServiceContext();
settingManager.setValue(Settings.SYSTEM_XLINKRESOLVER_ENABLE, true);
searchManager.init(true, Optional.of(List.of("records")));
}

@Test
Expand All @@ -49,6 +56,21 @@ public void complexDatesAreIndexedCheck() throws Exception {
SearchResponse response = this.searchManager.query("_id:" + dbInsertedMetadata.getUuid() + " AND resourceTitleObject.default:holocene", null, 0, 10);
long actualHitNbr = response.getHits().getTotalHits().value;
assertEquals(String.format("Incorrect indexation of Holocene data with complex date due to: %s and %s", response, dbInsertedMetadata), 1, actualHitNbr);
checkElasticsearchIndexStatus(2);
}

@Test
public void ensureElasticsearchIndexIsTrulyEmpty() throws IOException {
checkElasticsearchIndexStatus(0);
}

private void checkElasticsearchIndexStatus(int expectedIndexationCount) throws IOException {
EsRestClient esRestClient = searchManager.getClient();
String defaultIndex = searchManager.getDefaultIndex();
JsonNode indexStats = esRestClient.getIndexStats(defaultIndex);
// https://www.elastic.co/guide/en/elasticsearch/reference/7.17/cluster-nodes-stats.html
int count = indexStats.get("_all").get("primaries").get("indexing").get("index_total").asInt();
assertEquals("Incorrect number of indexing operations requested on index " + defaultIndex, expectedIndexationCount, count);
}

private AbstractMetadata loadMetadataWithTemporalExtentUsingSimpleDates() throws Exception {
Expand Down
6 changes: 6 additions & 0 deletions index/src/main/java/org/fao/geonet/index/es/EsRestClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package org.fao.geonet.index.es;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpHost;
Expand Down Expand Up @@ -510,4 +511,9 @@ public String getServerVersion() throws IOException, ElasticsearchException {

return version.getNumber();
}

public JsonNode getIndexStats(String index) throws IOException {
Response response = client.getLowLevelClient().performRequest(new Request("GET", String.format("%s/_stats", index)));
return new ObjectMapper().readTree(response.getEntity().getContent());
}
}

0 comments on commit 1435543

Please sign in to comment.