From bc261b4d65e4c90c9ec567d07663bf2f7283f6ce Mon Sep 17 00:00:00 2001 From: Harel M Date: Sat, 8 Feb 2025 00:27:30 +0200 Subject: [PATCH] Added unit test to elasticsearchhelper (#26) * Added unittest to elasticsearch helper * Added coverage for helper class * Improve coverage and quality * Improve coverage to max --- .github/workflows/CI.yml | 2 +- pom.xml | 14 +++ .../osm/israelhiking/ElasticsearchHelper.java | 7 ++ .../il/org/osm/israelhiking/MainClass.java | 4 + .../java/il/org/osm/israelhiking/E2ETest.java | 2 - .../israelhiking/ElasticsearchHelperTest.java | 98 +++++++++++++++++++ 6 files changed, 124 insertions(+), 3 deletions(-) create mode 100644 src/test/java/il/org/osm/israelhiking/ElasticsearchHelperTest.java diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index b4e45c2..428c7cd 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -35,7 +35,7 @@ jobs: - name: Set up JDK uses: actions/setup-java@v1 with: - java-version: 23 + java-version: 21 - name: End to End tests setup run: | docker compose up -d elasticsearch diff --git a/pom.xml b/pom.xml index 4caf73c..28d887a 100644 --- a/pom.xml +++ b/pom.xml @@ -47,6 +47,20 @@ 5.11.4 test + + + org.mockito + mockito-core + 5.15.2 + test + + + + org.mockito + mockito-junit-jupiter + 5.15.2 + test + diff --git a/src/main/java/il/org/osm/israelhiking/ElasticsearchHelper.java b/src/main/java/il/org/osm/israelhiking/ElasticsearchHelper.java index d9ce935..2801405 100644 --- a/src/main/java/il/org/osm/israelhiking/ElasticsearchHelper.java +++ b/src/main/java/il/org/osm/israelhiking/ElasticsearchHelper.java @@ -12,7 +12,14 @@ import co.elastic.clients.transport.rest_client.RestClientTransport; public class ElasticsearchHelper { + + /** + * Static utility class should not be instantiated. + */ + private ElasticsearchHelper() { } + public static ElasticsearchClient createElasticsearchClient(String esAddress) { + Logger.getLogger("org.elasticsearch.client.RestClient").setLevel(Level.OFF); RestClient restClient = RestClient diff --git a/src/main/java/il/org/osm/israelhiking/MainClass.java b/src/main/java/il/org/osm/israelhiking/MainClass.java index ac7ae95..84971d4 100644 --- a/src/main/java/il/org/osm/israelhiking/MainClass.java +++ b/src/main/java/il/org/osm/israelhiking/MainClass.java @@ -9,6 +9,10 @@ * The main entry point */ public class MainClass { + + /** Static utility class should not be instantiated. */ + private MainClass() {} + /** * Main entry point for the application. * @throws Exception diff --git a/src/test/java/il/org/osm/israelhiking/E2ETest.java b/src/test/java/il/org/osm/israelhiking/E2ETest.java index c643f49..b55c321 100644 --- a/src/test/java/il/org/osm/israelhiking/E2ETest.java +++ b/src/test/java/il/org/osm/israelhiking/E2ETest.java @@ -8,7 +8,5 @@ public class E2ETest { @Test public void test() throws Exception { MainClass.main(new String[]{ "--download", "--external-file-path", "./src/test/resources/extenal.geojson"}); - // Second run should not fail and also should not download the file again - MainClass.main(new String[]{ "--external-file-path", "./src/test/resources/extenal.geojson"}); } } diff --git a/src/test/java/il/org/osm/israelhiking/ElasticsearchHelperTest.java b/src/test/java/il/org/osm/israelhiking/ElasticsearchHelperTest.java new file mode 100644 index 0000000..bdfa8b4 --- /dev/null +++ b/src/test/java/il/org/osm/israelhiking/ElasticsearchHelperTest.java @@ -0,0 +1,98 @@ +package il.org.osm.israelhiking; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.ArgumentMatchers; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import co.elastic.clients.elasticsearch.ElasticsearchClient; +import co.elastic.clients.elasticsearch.indices.DeleteIndexRequest; +import co.elastic.clients.elasticsearch.indices.ElasticsearchIndicesClient; +import co.elastic.clients.elasticsearch.indices.ExistsAliasRequest; +import co.elastic.clients.elasticsearch.indices.ExistsRequest; +import co.elastic.clients.elasticsearch.indices.GetAliasRequest; +import co.elastic.clients.elasticsearch.indices.GetAliasResponse; +import co.elastic.clients.elasticsearch.indices.get_alias.IndexAliases; +import co.elastic.clients.transport.endpoints.BooleanResponse; +import co.elastic.clients.util.ObjectBuilder; + +import java.util.HashMap; +import java.util.function.Function; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.*; + +@Tag("unit") +@ExtendWith(MockitoExtension.class) +class ElasticsearchServiceTest { + + @Mock + private ElasticsearchClient esClient; + + @Mock + private ElasticsearchIndicesClient indicesClient; + + private final String[] supportedLanguages = {"en", "es"}; + private final String indexAlias = "test-index"; + + @BeforeEach + void setUp() throws Exception { + when(esClient.indices()).thenReturn(indicesClient); + } + + @Test + void testCreatePointIndexThatDoesntHaveAlias_ShouldDeleteItBeforeCreating() throws Exception { + when(indicesClient.existsAlias(ArgumentMatchers.>>any())).thenReturn(new BooleanResponse(false)); + when(indicesClient.exists(ArgumentMatchers.>>any())).thenReturn(new BooleanResponse(true)); + + var indexName = ElasticsearchHelper.createPointsIndex(esClient, indexAlias, supportedLanguages); + + verify(indicesClient).delete(ArgumentMatchers.>>any()); + assertEquals(indexAlias + "1", indexName); + } + + @Test + void testCreatePointIndexWhenOneExists_ShouldAddASecondOne() throws Exception { + when(indicesClient.existsAlias(ArgumentMatchers.>>any())).thenReturn(new BooleanResponse(true)); + when(indicesClient.exists(ArgumentMatchers.>>any())).thenReturn(new BooleanResponse(true)); + when(indicesClient.getAlias(ArgumentMatchers.>>any())) + .thenReturn(GetAliasResponse.of(b -> b.result(new HashMap() { + { + put(indexAlias + "1", null); + } + }))); + + var indexName = ElasticsearchHelper.createPointsIndex(esClient, indexAlias, supportedLanguages); + + assertEquals(indexAlias + "2", indexName); + } + + @Test + void testCreatePointIndexWhenSecondaryExists_ShouldAddTheFirstAndDelete() throws Exception { + when(indicesClient.existsAlias(ArgumentMatchers.>>any())).thenReturn(new BooleanResponse(true)); + when(indicesClient.exists(ArgumentMatchers.>>any())).thenReturn(new BooleanResponse(true)); + when(indicesClient.getAlias(ArgumentMatchers.>>any())) + .thenReturn(GetAliasResponse.of(b -> b.result(new HashMap() { + { + put(indexAlias + "2", null); + } + }))); + + var indexName = ElasticsearchHelper.createPointsIndex(esClient, indexAlias, supportedLanguages); + + assertEquals(indexAlias + "1", indexName); + verify(indicesClient).delete(ArgumentMatchers.>>any()); + } + + @Test + void testCreateBboxIndexThatDoesntHaveAlias_DeleteIt() throws Exception { + when(indicesClient.existsAlias(ArgumentMatchers.>>any())).thenReturn(new BooleanResponse(false)); + when(indicesClient.exists(ArgumentMatchers.>>any())).thenReturn(new BooleanResponse(true)); + + ElasticsearchHelper.createBBoxIndex(esClient, indexAlias, supportedLanguages); + + verify(indicesClient).delete(ArgumentMatchers.>>any()); + } +} \ No newline at end of file