From dceafdb9d55e2fbe97dba513ce5ce6d57d3011ac Mon Sep 17 00:00:00 2001 From: Neelesh Thakur <NThakur4@slb.com> Date: Tue, 6 Jul 2021 14:27:25 -0500 Subject: [PATCH] add unit test and remove redundent ones from providers --- .../indexer/service/IndexSchemaService.java | 8 +- .../service/IndexSchemaServiceImpl.java | 58 +-- .../service/IndexerSchemaServiceTest.java | 49 +- .../service/IndexerSchemaServiceTest.java | 401 ---------------- .../ibm/service/IndexerSchemaServiceTest.java | 395 ---------------- .../service/IndexerSchemaServiceTest.java | 445 ------------------ 6 files changed, 56 insertions(+), 1300 deletions(-) rename {provider/indexer-azure/src/test/java/org/opengroup/osdu/indexer/azure => indexer-core/src/test/java/org/opengroup/osdu/indexer}/service/IndexerSchemaServiceTest.java (92%) delete mode 100644 provider/indexer-gcp/src/test/java/org/opengroup/osdu/indexer/service/IndexerSchemaServiceTest.java delete mode 100644 provider/indexer-ibm/src/test/java/org/opengroup/osdu/indexer/ibm/service/IndexerSchemaServiceTest.java delete mode 100644 provider/indexer-reference/src/test/java/org/opengroup/osdu/indexer/service/IndexerSchemaServiceTest.java diff --git a/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/IndexSchemaService.java b/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/IndexSchemaService.java index 1562f1956..9691ada93 100644 --- a/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/IndexSchemaService.java +++ b/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/IndexSchemaService.java @@ -20,18 +20,20 @@ import org.opengroup.osdu.core.common.model.indexer.IndexSchema; import org.opengroup.osdu.core.common.model.indexer.OperationType; import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.net.URISyntaxException; import java.util.List; import java.util.Map; public interface IndexSchemaService { - IndexSchema getIndexerInputSchema(String kind, List<String> errors) throws AppException; + IndexSchema getIndexerInputSchema(String kind, List<String> errors) throws AppException, UnsupportedEncodingException, URISyntaxException; - IndexSchema getIndexerInputSchema(String kind, boolean invalidateCached) throws AppException; + IndexSchema getIndexerInputSchema(String kind, boolean invalidateCached) throws AppException, UnsupportedEncodingException, URISyntaxException; void processSchemaMessages(Map<String, OperationType> schemaMsgs) throws IOException; - void syncIndexMappingWithStorageSchema(String kind) throws ElasticsearchException, IOException, AppException; + void syncIndexMappingWithStorageSchema(String kind) throws ElasticsearchException, IOException, AppException, URISyntaxException; boolean isStorageSchemaSyncRequired(String kind, boolean forceClean) throws IOException; } diff --git a/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/IndexSchemaServiceImpl.java b/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/IndexSchemaServiceImpl.java index b3d251883..a979c0868 100644 --- a/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/IndexSchemaServiceImpl.java +++ b/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/IndexSchemaServiceImpl.java @@ -72,14 +72,14 @@ public class IndexSchemaServiceImpl implements IndexSchemaService { schemaMsgs.entrySet().forEach(msg -> { try { processSchemaEvents(restClient, msg); - } catch (IOException | ElasticsearchStatusException e) { + } catch (IOException | ElasticsearchStatusException | URISyntaxException e) { throw new AppException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "unable to process schema update", e.getMessage()); } }); } } - private void processSchemaEvents(RestHighLevelClient restClient, Map.Entry<String, OperationType> msg) throws IOException, ElasticsearchStatusException { + private void processSchemaEvents(RestHighLevelClient restClient, Map.Entry<String, OperationType> msg) throws IOException, ElasticsearchStatusException, URISyntaxException { String kind = msg.getKey(); String index = this.elasticIndexNameResolver.getIndexNameFromKind(kind); @@ -122,7 +122,7 @@ public class IndexSchemaServiceImpl implements IndexSchemaService { } @Override - public IndexSchema getIndexerInputSchema(String kind, List<String> errors) throws AppException { + public IndexSchema getIndexerInputSchema(String kind, List<String> errors) throws AppException, UnsupportedEncodingException, URISyntaxException { try { return getIndexerInputSchema(kind, false); } catch (SchemaProcessingException ex) { @@ -137,41 +137,35 @@ public class IndexSchemaServiceImpl implements IndexSchemaService { } @Override - public IndexSchema getIndexerInputSchema(String kind, boolean invalidateCached) throws AppException { + public IndexSchema getIndexerInputSchema(String kind, boolean invalidateCached) throws AppException, UnsupportedEncodingException, URISyntaxException { if (invalidateCached) { this.invalidateCache(kind); } - try { - String schema = (String) this.schemaCache.get(kind); + String schema = (String) this.schemaCache.get(kind); + if (Strings.isNullOrEmpty(schema)) { + // get from storage + schema = this.schemaProvider.getSchema(kind); if (Strings.isNullOrEmpty(schema)) { - // get from storage - schema = getSchema(kind); - if (Strings.isNullOrEmpty(schema)) { - return this.getEmptySchema(kind); - } else { - // cache the schema - this.schemaCache.put(kind, schema); - // get flatten schema and cache it - IndexSchema flatSchemaObj = normalizeSchema(schema); - if (flatSchemaObj != null) { - this.schemaCache.put(kind + FLATTENED_SCHEMA, gson.toJson(flatSchemaObj)); - } - return flatSchemaObj; - } + return this.getEmptySchema(kind); } else { - // search flattened schema in memcache - String flattenedSchema = (String) this.schemaCache.get(kind + FLATTENED_SCHEMA); - if (Strings.isNullOrEmpty(flattenedSchema)) { - return this.getEmptySchema(kind); + // cache the schema + this.schemaCache.put(kind, schema); + // get flatten schema and cache it + IndexSchema flatSchemaObj = normalizeSchema(schema); + if (flatSchemaObj != null) { + this.schemaCache.put(kind + FLATTENED_SCHEMA, gson.toJson(flatSchemaObj)); } - return this.gson.fromJson(flattenedSchema, IndexSchema.class); + return flatSchemaObj; } - } catch (AppException e) { - throw e; - } catch (Exception e) { - throw new AppException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "Schema parse/read error", "Error reading schema.", e); + } else { + // search flattened schema in memcache + String flattenedSchema = (String) this.schemaCache.get(kind + FLATTENED_SCHEMA); + if (Strings.isNullOrEmpty(flattenedSchema)) { + return this.getEmptySchema(kind); + } + return this.gson.fromJson(flattenedSchema, IndexSchema.class); } } @@ -180,11 +174,7 @@ public class IndexSchemaServiceImpl implements IndexSchemaService { return normalizeSchema(gson.toJson(basicSchema)); } - private String getSchema(String kind) throws URISyntaxException, UnsupportedEncodingException { - return this.schemaProvider.getSchema(kind); - } - - public void syncIndexMappingWithStorageSchema(String kind) throws ElasticsearchException, IOException, AppException { + public void syncIndexMappingWithStorageSchema(String kind) throws ElasticsearchException, IOException, AppException, URISyntaxException { String index = this.elasticIndexNameResolver.getIndexNameFromKind(kind); try (RestHighLevelClient restClient = this.elasticClientHandler.createRestClient()) { if (this.indicesService.isIndexExist(restClient, index)) { diff --git a/provider/indexer-azure/src/test/java/org/opengroup/osdu/indexer/azure/service/IndexerSchemaServiceTest.java b/indexer-core/src/test/java/org/opengroup/osdu/indexer/service/IndexerSchemaServiceTest.java similarity index 92% rename from provider/indexer-azure/src/test/java/org/opengroup/osdu/indexer/azure/service/IndexerSchemaServiceTest.java rename to indexer-core/src/test/java/org/opengroup/osdu/indexer/service/IndexerSchemaServiceTest.java index 2fe57e434..705f8e98a 100644 --- a/provider/indexer-azure/src/test/java/org/opengroup/osdu/indexer/azure/service/IndexerSchemaServiceTest.java +++ b/indexer-core/src/test/java/org/opengroup/osdu/indexer/service/IndexerSchemaServiceTest.java @@ -12,13 +12,12 @@ // See the License for the specific language governing permissions and // limitations under the License. -package org.opengroup.osdu.indexer.azure.service; +package org.opengroup.osdu.indexer.service; import org.apache.http.HttpStatus; import org.elasticsearch.client.RestHighLevelClient; import org.junit.Assert; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; @@ -32,9 +31,6 @@ import org.opengroup.osdu.core.common.search.ElasticIndexNameResolver; import org.opengroup.osdu.core.common.search.IndicesService; import org.opengroup.osdu.indexer.provider.interfaces.ISchemaCache; import org.opengroup.osdu.indexer.schema.converter.exeption.SchemaProcessingException; -import org.opengroup.osdu.indexer.service.IndexSchemaServiceImpl; -import org.opengroup.osdu.indexer.service.IndexerMappingService; -import org.opengroup.osdu.indexer.service.SchemaService; import org.opengroup.osdu.indexer.util.ElasticClientHandler; import org.powermock.core.classloader.annotations.PrepareForTest; import org.springframework.test.context.junit4.SpringRunner; @@ -44,17 +40,26 @@ import java.io.UnsupportedEncodingException; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.HashMap; -import java.util.Map; import java.util.List; +import java.util.Map; -import static org.junit.Assert.*; -import static org.mockito.ArgumentMatchers.*; -import static org.mockito.Mockito.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.MockitoAnnotations.initMocks; import static org.powermock.api.mockito.PowerMockito.mock; import static org.powermock.api.mockito.PowerMockito.when; -@Ignore @RunWith(SpringRunner.class) @PrepareForTest({RestHighLevelClient.class}) public class IndexerSchemaServiceTest { @@ -132,7 +137,7 @@ public class IndexerSchemaServiceTest { } @Test - public void should_return_basic_schema_when_storage_returns_no_schema() { + public void should_return_basic_schema_when_storage_returns_no_schema() throws UnsupportedEncodingException, URISyntaxException { IndexSchema returnedSchema = this.sut.getIndexerInputSchema(kind, false); assertNotNull(returnedSchema.getDataSchema()); @@ -239,9 +244,9 @@ public class IndexerSchemaServiceTest { try { this.sut.processSchemaMessages(schemaMessages); } catch (AppException e){ - assertEquals(e.getError().getCode(), RequestStatus.SCHEMA_CONFLICT); - assertEquals(e.getError().getMessage(), "error creating or merging index mapping"); - assertEquals(e.getError().getReason(), reason); + assertEquals(RequestStatus.SCHEMA_CONFLICT, e.getError().getCode()); + assertEquals("error creating or merging index mapping", e.getError().getMessage()); + assertEquals(reason, e.getError().getReason()); } catch (Exception e) { fail("Should not throw this exception " + e.getMessage()); } @@ -272,9 +277,9 @@ public class IndexerSchemaServiceTest { try { this.sut.processSchemaMessages(schemaMessages); } catch (AppException e){ - assertEquals(e.getError().getCode(), HttpStatus.SC_FORBIDDEN); - assertEquals(e.getError().getMessage(), "blah"); - assertEquals(e.getError().getReason(), reason); + assertEquals(HttpStatus.SC_FORBIDDEN, e.getError().getCode()); + assertEquals("blah", e.getError().getMessage()); + assertEquals(reason, e.getError().getReason()); } catch (Exception e) { fail("Should not throw this exception " + e.getMessage()); } @@ -369,9 +374,9 @@ public class IndexerSchemaServiceTest { try { this.sut.syncIndexMappingWithStorageSchema(kind); } catch (AppException e) { - assertEquals(e.getError().getCode(), HttpStatus.SC_CONFLICT); - assertEquals(e.getError().getMessage(), "blah"); - assertEquals(e.getError().getReason(), "Index deletion error"); + assertEquals(HttpStatus.SC_CONFLICT, e.getError().getCode()); + assertEquals("blah", e.getError().getMessage()); + assertEquals("Index deletion error", e.getError().getReason()); } catch (Exception e) { fail("Should not throw this exception " + e.getMessage()); } @@ -418,7 +423,7 @@ public class IndexerSchemaServiceTest { IndexSchema indexSchema = this.sut.getIndexerInputSchema(kind, errors); assertNotNull(indexSchema); - assertTrue(errors.contains("error processing schema")); + assertTrue(errors.get(0).contains("error processing schema")); } @Test @@ -430,6 +435,6 @@ public class IndexerSchemaServiceTest { IndexSchema indexSchema = this.sut.getIndexerInputSchema(kind, errors); assertNotNull(indexSchema); - assertTrue(errors.contains("error processing schema, RuntimeException exception thrown")); + assertTrue(errors.get(0).contains("error processing schema, RuntimeException exception thrown")); } } diff --git a/provider/indexer-gcp/src/test/java/org/opengroup/osdu/indexer/service/IndexerSchemaServiceTest.java b/provider/indexer-gcp/src/test/java/org/opengroup/osdu/indexer/service/IndexerSchemaServiceTest.java deleted file mode 100644 index 631c840c2..000000000 --- a/provider/indexer-gcp/src/test/java/org/opengroup/osdu/indexer/service/IndexerSchemaServiceTest.java +++ /dev/null @@ -1,401 +0,0 @@ -// Copyright 2017-2019, Schlumberger -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package org.opengroup.osdu.indexer.service; - -import org.apache.http.HttpStatus; -import org.elasticsearch.client.RestHighLevelClient; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.opengroup.osdu.core.common.model.http.AppException; -import org.opengroup.osdu.core.common.model.indexer.OperationType; -import org.opengroup.osdu.core.common.logging.JaxRsDpsLog; -import org.opengroup.osdu.indexer.provider.interfaces.ISchemaCache; -import org.opengroup.osdu.core.common.model.indexer.IndexSchema; -import org.opengroup.osdu.core.common.model.http.RequestStatus; -import org.opengroup.osdu.core.common.search.IndicesService; -import org.opengroup.osdu.indexer.service.SchemaProviderImpl; -import org.opengroup.osdu.indexer.util.ElasticClientHandler; -import org.opengroup.osdu.core.common.search.ElasticIndexNameResolver; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.springframework.test.context.junit4.SpringRunner; - -import java.io.IOException; -import java.net.URISyntaxException; -import java.util.HashMap; -import java.util.Map; - -import static org.junit.Assert.*; -import static org.mockito.Mockito.*; -import static org.mockito.MockitoAnnotations.initMocks; -import static org.powermock.api.mockito.PowerMockito.mock; -import static org.powermock.api.mockito.PowerMockito.when; - -@RunWith(SpringRunner.class) -@PrepareForTest({RestHighLevelClient.class}) -public class IndexerSchemaServiceTest { - - private final String kind = "tenant:test:test:1.0.0"; - private final String emptySchema = null; - private final String someSchema = "{\"kind\":\"tenant:test:test:1.0.0\", \"schema\":[{\"path\":\"test-path\", \"kind\":\"tenant:test:test:1.0.0\"}]}"; - - @Mock - private JaxRsDpsLog log; - @Mock - private SchemaProviderImpl schemaProvider; - @Mock - private ElasticClientHandler elasticClientHandler; - @Mock - private ElasticIndexNameResolver elasticIndexNameResolver; - @Mock - private IndexerMappingService mappingService; - @Mock - private IndicesService indicesService; - @Mock - private ISchemaCache schemaCache; - @InjectMocks - private IndexSchemaServiceImpl sut; - - @Before - public void setup() { - initMocks(this); - RestHighLevelClient restHighLevelClient = mock(RestHighLevelClient.class); - when(elasticClientHandler.createRestClient()).thenReturn(restHighLevelClient); - } - - @Test - public void should_returnNull_givenEmptySchema_getIndexerInputSchemaSchemaTest() throws Exception { - when(schemaProvider.getSchema(any())).thenReturn(emptySchema); - - IndexSchema indexSchema = this.sut.getIndexerInputSchema(kind, false); - - Assert.assertNotNull(indexSchema); - } - - @Test - public void should_returnValidResponse_givenValidSchema_getIndexerInputSchemaTest() throws Exception { - when(schemaProvider.getSchema(any())).thenReturn(someSchema); - - IndexSchema indexSchema = this.sut.getIndexerInputSchema(kind, false); - - Assert.assertEquals(kind, indexSchema.getKind()); - } - - @Test - public void should_returnValidResponse_givenValidSchemaWithCacheHit_getIndexerInputSchemaTest() throws Exception { - when(schemaProvider.getSchema(any())).thenReturn(someSchema); - when(this.schemaCache.get(kind + "_flattened")).thenReturn(someSchema); - - IndexSchema indexSchema = this.sut.getIndexerInputSchema(kind, false); - - Assert.assertEquals(kind, indexSchema.getKind()); - } - - @Test - public void should_throw500_givenInvalidSchemaCacheHit_getIndexerInputSchemaTest() { - try { - String invalidSchema = "{}}"; - when(schemaProvider.getSchema(any())).thenReturn(invalidSchema); - - this.sut.getIndexerInputSchema(kind, false); - fail("Should throw exception"); - } catch (AppException e) { - Assert.assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getError().getCode()); - Assert.assertEquals("An error has occurred while normalizing the schema.", e.getError().getMessage()); - } catch (Exception e) { - fail("Should not throw exception" + e.getMessage()); - } - } - - @Test - public void should_return_basic_schema_when_storage_returns_no_schema() { - IndexSchema returnedSchema = this.sut.getIndexerInputSchema(kind, false); - - assertNotNull(returnedSchema.getDataSchema()); - assertNotNull(returnedSchema); - assertEquals(kind, returnedSchema.getKind()); - } - - @Test - public void should_create_schema_when_storage_returns_valid_schema() throws IOException, URISyntaxException { - String kind = "tenant1:avocet:completion:1.0.0"; - String storageSchema = "{" + - " \"kind\": \"tenant1:avocet:completion:1.0.0\"," + - " \"schema\": [" + - " {" + - " \"path\": \"status\"," + - " \"kind\": \"string\"" + - " }," + - " {" + - " \"path\": \"startDate\"," + - " \"kind\": \"string\"" + - " }," + - " {" + - " \"path\": \"endDate\"," + - " \"kind\": \"string\"" + - " }," + - " {" + - " \"path\": \"type \"," + - " \"kind\": \"string\"" + - " }," + - " {" + - " \"path\": \"itemguid\"," + - " \"kind\": \"string\"" + - " }" + - " ]" + - "}"; - Map<String, OperationType> schemaMessages = new HashMap<>(); - schemaMessages.put(kind, OperationType.create_schema); - - when(this.elasticIndexNameResolver.getIndexNameFromKind(kind)).thenReturn(kind.replace(":", "-")); - when(this.schemaCache.get(kind)).thenReturn(null); - when(this.indicesService.isIndexExist(any(), any())).thenReturn(false); - when(this.schemaProvider.getSchema(kind)).thenReturn(storageSchema); - - this.sut.processSchemaMessages(schemaMessages); - - verify(this.mappingService, times(1)).getIndexMappingFromRecordSchema(any()); - verify(this.indicesService, times(1)).createIndex(any(), any(), any(), any(), any()); - verifyNoMoreInteractions(this.mappingService); - } - - @Test - public void should_merge_mapping_when_storage_returns_valid_schema() throws IOException, URISyntaxException { - String kind = "tenant1:avocet:completion:1.0.0"; - String storageSchema = "{" + - " \"kind\": \"tenant1:avocet:completion:1.0.0\"," + - " \"schema\": [" + - " {" + - " \"path\": \"status\"," + - " \"kind\": \"string\"" + - " }," + - " {" + - " \"path\": \"startDate\"," + - " \"kind\": \"string\"" + - " }" + - " ]" + - "}"; - Map<String, OperationType> schemaMessages = new HashMap<>(); - schemaMessages.put(kind, OperationType.create_schema); - - when(this.elasticIndexNameResolver.getIndexNameFromKind(kind)).thenReturn(kind.replace(":", "-")); - when(this.schemaCache.get(kind)).thenReturn(null); - when(this.indicesService.isIndexExist(any(), any())).thenReturn(true); - when(this.schemaProvider.getSchema(kind)).thenReturn(storageSchema); - - this.sut.processSchemaMessages(schemaMessages); - - verify(this.indicesService, times(0)).createIndex(any(), any(), any(), any(), any()); - verify(this.mappingService, times(1)).createMapping(any(), any(), any(), anyBoolean()); - verifyNoMoreInteractions(this.mappingService); - } - - @Test - public void should_throw_mapping_conflict_when_elastic_backend_cannot_process_schema_changes() throws IOException, URISyntaxException { - String kind = "tenant1:avocet:completion:1.0.0"; - String reason = String.format("Could not create type mapping %s/completion.", kind.replace(":", "-")); - String storageSchema = "{" + - " \"kind\": \"tenant1:avocet:completion:1.0.0\"," + - " \"schema\": [" + - " {" + - " \"path\": \"status\"," + - " \"kind\": \"string\"" + - " }" + - " ]" + - "}"; - Map<String, OperationType> schemaMessages = new HashMap<>(); - schemaMessages.put(kind, OperationType.create_schema); - - when(this.elasticIndexNameResolver.getIndexNameFromKind(kind)).thenReturn(kind.replace(":", "-")); - when(this.schemaCache.get(kind)).thenReturn(null); - when(this.indicesService.isIndexExist(any(), any())).thenReturn(true); - when(this.schemaProvider.getSchema(kind)).thenReturn(storageSchema); - when(this.mappingService.createMapping(any(), any(), any(), anyBoolean())).thenThrow(new AppException(HttpStatus.SC_BAD_REQUEST, reason, "")); - - try { - this.sut.processSchemaMessages(schemaMessages); - } catch (AppException e) { - assertEquals(RequestStatus.SCHEMA_CONFLICT, e.getError().getCode()); - assertEquals("error creating or merging index mapping", e.getError().getMessage()); - assertEquals(reason, e.getError().getReason()); - } catch (Exception e) { - fail("Should not throw this exception " + e.getMessage()); - } - } - - @Test - public void should_throw_genericAppException_when_elastic_backend_cannot_process_schema_changes() throws IOException, URISyntaxException { - String kind = "tenant1:avocet:completion:1.0.0"; - String reason = String.format("Could not create type mapping %s/completion.", kind.replace(":", "-")); - String storageSchema = "{" + - " \"kind\": \"tenant1:avocet:completion:1.0.0\"," + - " \"schema\": [" + - " {" + - " \"path\": \"status\"," + - " \"kind\": \"string\"" + - " }" + - " ]" + - "}"; - Map<String, OperationType> schemaMessages = new HashMap<>(); - schemaMessages.put(kind, OperationType.create_schema); - - when(this.elasticIndexNameResolver.getIndexNameFromKind(kind)).thenReturn(kind.replace(":", "-")); - when(this.schemaCache.get(kind)).thenReturn(null); - when(this.indicesService.isIndexExist(any(), any())).thenReturn(true); - when(this.schemaProvider.getSchema(kind)).thenReturn(storageSchema); - when(this.mappingService.createMapping(any(), any(), any(), anyBoolean())).thenThrow(new AppException(HttpStatus.SC_FORBIDDEN, reason, "blah")); - - try { - this.sut.processSchemaMessages(schemaMessages); - } catch (AppException e) { - assertEquals(HttpStatus.SC_FORBIDDEN, e.getError().getCode()); - assertEquals("blah", e.getError().getMessage()); - assertEquals(reason, e.getError().getReason()); - } catch (Exception e) { - fail("Should not throw this exception " + e.getMessage()); - } - } - - @Test - public void should_log_and_do_nothing_when_storage_returns_invalid_schema() throws IOException, URISyntaxException { - String kind = "tenant1:avocet:completion:1.0.0"; - String storageSchema = "{" + - " \"kind\": \"tenant1:avocet:completion:1.0.0\"" + - "}"; - Map<String, OperationType> schemaMessages = new HashMap<>(); - schemaMessages.put(kind, OperationType.create_schema); - - when(this.elasticIndexNameResolver.getIndexNameFromKind(kind)).thenReturn(kind.replace(":", "-")); - when(this.schemaCache.get(kind)).thenReturn(null); - when(this.indicesService.isIndexExist(any(), any())).thenReturn(true); - when(this.schemaProvider.getSchema(kind)).thenReturn(storageSchema); - - this.sut.processSchemaMessages(schemaMessages); - - verify(this.log).warning(eq("schema not found for kind: tenant1:avocet:completion:1.0.0")); - } - - @Test - public void should_invalidateCache_when_purge_schema_and_schema_found_in_cache() throws IOException { - String kind = "tenant1:avocet:completion:1.0.0"; - Map<String, OperationType> schemaMessages = new HashMap<>(); - schemaMessages.put(kind, OperationType.purge_schema); - - when(this.elasticIndexNameResolver.getIndexNameFromKind(kind)).thenReturn(kind.replace(":", "-")); - when(this.indicesService.isIndexExist(any(), any())).thenReturn(true); - when(this.schemaCache.get(kind)).thenReturn("schema"); - when(this.schemaCache.get(kind + "_flattened")).thenReturn("flattened schema"); - - this.sut.processSchemaMessages(schemaMessages); - - verify(this.schemaCache, times(2)).get(anyString()); - verify(this.schemaCache, times(2)).delete(anyString()); - } - - @Test - public void should_log_warning_when_purge_schema_and_schema_not_found_in_cache() throws IOException { - String kind = "tenant1:avocet:completion:1.0.0"; - Map<String, OperationType> schemaMessages = new HashMap<>(); - schemaMessages.put(kind, OperationType.purge_schema); - - when(this.elasticIndexNameResolver.getIndexNameFromKind(kind)).thenReturn(kind.replace(":", "-")); - when(this.indicesService.isIndexExist(any(), any())).thenReturn(false); - - this.sut.processSchemaMessages(schemaMessages); - - verify(this.log).warning(eq(String.format("Kind: %s not found", kind))); - } - - @Test - public void should_sync_schema_with_storage() throws Exception { - String kind = "tenant1:avocet:completion:1.0.0"; - String storageSchema = "{" + - " \"kind\": \"tenant1:avocet:completion:1.0.0\"," + - " \"schema\": [" + - " {" + - " \"path\": \"status\"," + - " \"kind\": \"string\"" + - " }" + - " ]" + - "}"; - when(this.elasticIndexNameResolver.getIndexNameFromKind(kind)).thenReturn(kind.replace(":", "-")); - when(this.schemaCache.get(kind)).thenReturn(null); - when(this.indicesService.isIndexExist(any(), any())).thenReturn(true); - when(this.indicesService.deleteIndex(any(), any())).thenReturn(true); - when(this.schemaProvider.getSchema(kind)).thenReturn(storageSchema); - - this.sut.syncIndexMappingWithStorageSchema(kind); - - verify(this.mappingService, times(1)).getIndexMappingFromRecordSchema(any()); - verify(this.indicesService, times(1)).isIndexExist(any(), any()); - verify(this.indicesService, times(1)).deleteIndex(any(), any()); - verify(this.indicesService, times(1)).createIndex(any(), any(), any(), any(), any()); - verifyNoMoreInteractions(this.mappingService); - } - - @Test - public void should_throw_exception_while_snapshot_running_sync_schema_with_storage() throws Exception { - String kind = "tenant1:avocet:completion:1.0.0"; - when(this.elasticIndexNameResolver.getIndexNameFromKind(kind)).thenReturn(kind.replace(":", "-")); - when(this.schemaCache.get(kind)).thenReturn(null); - when(this.indicesService.isIndexExist(any(), any())).thenReturn(true); - when(this.indicesService.deleteIndex(any(), any())).thenThrow(new AppException(HttpStatus.SC_CONFLICT, "Index deletion error", "blah")); - - try { - this.sut.syncIndexMappingWithStorageSchema(kind); - } catch (AppException e) { - assertEquals(HttpStatus.SC_CONFLICT, e.getError().getCode()); - assertEquals("blah", e.getError().getMessage()); - assertEquals("Index deletion error", e.getError().getReason()); - } catch (Exception e) { - fail("Should not throw this exception " + e.getMessage()); - } - - verify(this.indicesService, times(1)).isIndexExist(any(), any()); - verify(this.indicesService, times(1)).deleteIndex(any(), any()); - verify(this.mappingService, never()).getIndexMappingFromRecordSchema(any()); - verify(this.indicesService, never()).createIndex(any(), any(), any(), any(), any()); - } - - @Test - public void should_return_true_while_if_forceClean_requested() throws IOException { - String kind = "tenant1:avocet:completion:1.0.0"; - when(this.elasticIndexNameResolver.getIndexNameFromKind(kind)).thenReturn(kind.replace(":", "-")); - when(this.indicesService.isIndexExist(any(), any())).thenReturn(true); - - assertTrue(this.sut.isStorageSchemaSyncRequired(kind, true)); - } - - @Test - public void should_return_true_while_if_forceClean_notRequested_and_indexNotFound() throws IOException { - String kind = "tenant1:avocet:completion:1.0.0"; - when(this.elasticIndexNameResolver.getIndexNameFromKind(kind)).thenReturn(kind.replace(":", "-")); - when(this.indicesService.isIndexExist(any(), any())).thenReturn(false); - - assertTrue(this.sut.isStorageSchemaSyncRequired(kind, false)); - } - - @Test - public void should_return_false_while_if_forceClean_notRequested_and_indexExist() throws IOException { - String kind = "tenant1:avocet:completion:1.0.0"; - when(this.elasticIndexNameResolver.getIndexNameFromKind(kind)).thenReturn(kind.replace(":", "-")); - when(this.indicesService.isIndexExist(any(), any())).thenReturn(true); - - assertFalse(this.sut.isStorageSchemaSyncRequired(kind, false)); - } -} diff --git a/provider/indexer-ibm/src/test/java/org/opengroup/osdu/indexer/ibm/service/IndexerSchemaServiceTest.java b/provider/indexer-ibm/src/test/java/org/opengroup/osdu/indexer/ibm/service/IndexerSchemaServiceTest.java deleted file mode 100644 index d85ba3011..000000000 --- a/provider/indexer-ibm/src/test/java/org/opengroup/osdu/indexer/ibm/service/IndexerSchemaServiceTest.java +++ /dev/null @@ -1,395 +0,0 @@ -/* Licensed Materials - Property of IBM */ -/* (c) Copyright IBM Corp. 2020. All Rights Reserved.*/ - -package org.opengroup.osdu.indexer.ibm.service; - -import org.apache.http.HttpStatus; -import org.elasticsearch.client.RestHighLevelClient; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.opengroup.osdu.core.common.model.indexer.IndexSchema; -import org.opengroup.osdu.core.common.model.indexer.OperationType; -import org.opengroup.osdu.core.common.logging.JaxRsDpsLog; -import org.opengroup.osdu.indexer.provider.interfaces.ISchemaCache; -import org.opengroup.osdu.indexer.service.IndexSchemaServiceImpl; -import org.opengroup.osdu.indexer.service.IndexerMappingService; -import org.opengroup.osdu.indexer.service.StorageService; -import org.opengroup.osdu.core.common.model.http.RequestStatus; -import org.opengroup.osdu.core.common.search.IndicesService; -import org.opengroup.osdu.core.common.model.http.AppException; -import org.opengroup.osdu.indexer.util.ElasticClientHandler; -import org.opengroup.osdu.core.common.search.ElasticIndexNameResolver; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.springframework.test.context.junit4.SpringRunner; - -import java.io.IOException; -import java.net.URISyntaxException; -import java.util.HashMap; -import java.util.Map; - -import static org.junit.Assert.*; -import static org.mockito.ArgumentMatchers.*; -import static org.mockito.Mockito.*; -import static org.mockito.MockitoAnnotations.initMocks; -import static org.powermock.api.mockito.PowerMockito.mock; -import static org.powermock.api.mockito.PowerMockito.when; - -@Ignore -@RunWith(SpringRunner.class) -@PrepareForTest({RestHighLevelClient.class}) -public class IndexerSchemaServiceTest { - - private final String kind = "tenant:test:test:1.0.0"; - private final String emptySchema = null; - private final String someSchema = "{\"kind\":\"tenant:test:test:1.0.0\", \"schema\":[{\"path\":\"test-path\", \"kind\":\"tenant:test:test:1.0.0\"}]}"; - - @Mock - private JaxRsDpsLog log; - @Mock - private StorageService storageService; - @Mock - private ElasticClientHandler elasticClientHandler; - @Mock - private ElasticIndexNameResolver elasticIndexNameResolver; - @Mock - private IndexerMappingService mappingService; - @Mock - private IndicesService indicesService; - @Mock - private ISchemaCache schemaCache; - @InjectMocks - private IndexSchemaServiceImpl sut; - - @Before - public void setup() { - initMocks(this); - RestHighLevelClient restHighLevelClient = mock(RestHighLevelClient.class); - when(elasticClientHandler.createRestClient()).thenReturn(restHighLevelClient); - } - - @Test - public void should_returnNull_givenEmptySchema_getIndexerInputSchemaSchemaTest() throws Exception { - when(storageService.getStorageSchema(any())).thenReturn(emptySchema); - - IndexSchema indexSchema = this.sut.getIndexerInputSchema(kind, false); - - Assert.assertNotNull(indexSchema); - } - - @Test - public void should_returnValidResponse_givenValidSchema_getIndexerInputSchemaTest() throws Exception { - when(storageService.getStorageSchema(any())).thenReturn(someSchema); - - IndexSchema indexSchema = this.sut.getIndexerInputSchema(kind, false); - - Assert.assertEquals(kind, indexSchema.getKind()); - } - - @Test - public void should_returnValidResponse_givenValidSchemaWithCacheHit_getIndexerInputSchemaTest() throws Exception { - when(storageService.getStorageSchema(any())).thenReturn(someSchema); - when(this.schemaCache.get(kind + "_flattened")).thenReturn(someSchema); - - IndexSchema indexSchema = this.sut.getIndexerInputSchema(kind, false); - - Assert.assertEquals(kind, indexSchema.getKind()); - } - - @Test - public void should_throw500_givenInvalidSchemaCacheHit_getIndexerInputSchemaTest() { - try { - String invalidSchema = "{}}"; - when(storageService.getStorageSchema(any())).thenReturn(invalidSchema); - - this.sut.getIndexerInputSchema(kind, false); - fail("Should throw exception"); - } catch (AppException e) { - Assert.assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getError().getCode()); - Assert.assertEquals("An error has occurred while normalizing the schema.", e.getError().getMessage()); - } catch (Exception e) { - fail("Should not throw exception" + e.getMessage()); - } - } - - @Test - public void should_return_basic_schema_when_storage_returns_no_schema() { - IndexSchema returnedSchema = this.sut.getIndexerInputSchema(kind, false); - - assertNotNull(returnedSchema.getDataSchema()); - assertNotNull(returnedSchema); - assertEquals(kind, returnedSchema.getKind()); - } - - @Test - public void should_create_schema_when_storage_returns_valid_schema() throws IOException, URISyntaxException { - String kind = "tenant1:avocet:completion:1.0.0"; - String storageSchema = "{" + - " \"kind\": \"tenant1:avocet:completion:1.0.0\"," + - " \"schema\": [" + - " {" + - " \"path\": \"status\"," + - " \"kind\": \"string\"" + - " }," + - " {" + - " \"path\": \"startDate\"," + - " \"kind\": \"string\"" + - " }," + - " {" + - " \"path\": \"endDate\"," + - " \"kind\": \"string\"" + - " }," + - " {" + - " \"path\": \"type \"," + - " \"kind\": \"string\"" + - " }," + - " {" + - " \"path\": \"itemguid\"," + - " \"kind\": \"string\"" + - " }" + - " ]" + - "}"; - Map<String, OperationType> schemaMessages = new HashMap<>(); - schemaMessages.put(kind, OperationType.create_schema); - - when(this.elasticIndexNameResolver.getIndexNameFromKind(kind)).thenReturn(kind.replace(":", "-")); - when(this.schemaCache.get(kind)).thenReturn(null); - when(this.indicesService.isIndexExist(any(), any())).thenReturn(false); - when(this.storageService.getStorageSchema(kind)).thenReturn(storageSchema); - - this.sut.processSchemaMessages(schemaMessages); - - verify(this.mappingService, times(1)).getIndexMappingFromRecordSchema(any()); - verify(this.indicesService, times(1)).createIndex(any(), any(), any(), any(), any()); - verifyNoMoreInteractions(this.mappingService); - } - - @Test - public void should_merge_mapping_when_storage_returns_valid_schema() throws IOException, URISyntaxException { - String kind = "tenant1:avocet:completion:1.0.0"; - String storageSchema = "{" + - " \"kind\": \"tenant1:avocet:completion:1.0.0\"," + - " \"schema\": [" + - " {" + - " \"path\": \"status\"," + - " \"kind\": \"string\"" + - " }," + - " {" + - " \"path\": \"startDate\"," + - " \"kind\": \"string\"" + - " }" + - " ]" + - "}"; - Map<String, OperationType> schemaMessages = new HashMap<>(); - schemaMessages.put(kind, OperationType.create_schema); - - when(this.elasticIndexNameResolver.getIndexNameFromKind(kind)).thenReturn(kind.replace(":", "-")); - when(this.schemaCache.get(kind)).thenReturn(null); - when(this.indicesService.isIndexExist(any(), any())).thenReturn(true); - when(this.storageService.getStorageSchema(kind)).thenReturn(storageSchema); - - this.sut.processSchemaMessages(schemaMessages); - - verify(this.indicesService, times(0)).createIndex(any(), any(), any(), any(), any()); - verify(this.mappingService, times(1)).createMapping(any(), any(), any(), anyBoolean()); - verifyNoMoreInteractions(this.mappingService); - } - - @Test - public void should_throw_mapping_conflict_when_elastic_backend_cannot_process_schema_changes() throws IOException, URISyntaxException { - String kind = "tenant1:avocet:completion:1.0.0"; - String reason = String.format("Could not create type mapping %s/completion.", kind.replace(":", "-")); - String storageSchema = "{" + - " \"kind\": \"tenant1:avocet:completion:1.0.0\"," + - " \"schema\": [" + - " {" + - " \"path\": \"status\"," + - " \"kind\": \"string\"" + - " }" + - " ]" + - "}"; - Map<String, OperationType> schemaMessages = new HashMap<>(); - schemaMessages.put(kind, OperationType.create_schema); - - when(this.elasticIndexNameResolver.getIndexNameFromKind(kind)).thenReturn(kind.replace(":", "-")); - when(this.schemaCache.get(kind)).thenReturn(null); - when(this.indicesService.isIndexExist(any(), any())).thenReturn(true); - when(this.storageService.getStorageSchema(kind)).thenReturn(storageSchema); - when(this.mappingService.createMapping(any(), any(), any(), anyBoolean())).thenThrow(new AppException(HttpStatus.SC_BAD_REQUEST, reason, "")); - - try { - this.sut.processSchemaMessages(schemaMessages); - } catch (AppException e) { - assertEquals(e.getError().getCode(), RequestStatus.SCHEMA_CONFLICT); - assertEquals(e.getError().getMessage(), "error creating or merging index mapping"); - assertEquals(e.getError().getReason(), reason); - } catch (Exception e) { - fail("Should not throw this exception " + e.getMessage()); - } - } - - @Test - public void should_throw_genericAppException_when_elastic_backend_cannot_process_schema_changes() throws IOException, URISyntaxException { - String kind = "tenant1:avocet:completion:1.0.0"; - String reason = String.format("Could not create type mapping %s/completion.", kind.replace(":", "-")); - String storageSchema = "{" + - " \"kind\": \"tenant1:avocet:completion:1.0.0\"," + - " \"schema\": [" + - " {" + - " \"path\": \"status\"," + - " \"kind\": \"string\"" + - " }" + - " ]" + - "}"; - Map<String, OperationType> schemaMessages = new HashMap<>(); - schemaMessages.put(kind, OperationType.create_schema); - - when(this.elasticIndexNameResolver.getIndexNameFromKind(kind)).thenReturn(kind.replace(":", "-")); - when(this.schemaCache.get(kind)).thenReturn(null); - when(this.indicesService.isIndexExist(any(), any())).thenReturn(true); - when(this.storageService.getStorageSchema(kind)).thenReturn(storageSchema); - when(this.mappingService.createMapping(any(), any(), any(), anyBoolean())).thenThrow(new AppException(HttpStatus.SC_FORBIDDEN, reason, "blah")); - - try { - this.sut.processSchemaMessages(schemaMessages); - } catch (AppException e) { - assertEquals(e.getError().getCode(), HttpStatus.SC_FORBIDDEN); - assertEquals(e.getError().getMessage(), "blah"); - assertEquals(e.getError().getReason(), reason); - } catch (Exception e) { - fail("Should not throw this exception " + e.getMessage()); - } - } - - @Test - public void should_log_and_do_nothing_when_storage_returns_invalid_schema() throws IOException, URISyntaxException { - String kind = "tenant1:avocet:completion:1.0.0"; - String storageSchema = "{" + - " \"kind\": \"tenant1:avocet:completion:1.0.0\"" + - "}"; - Map<String, OperationType> schemaMessages = new HashMap<>(); - schemaMessages.put(kind, OperationType.create_schema); - - when(this.elasticIndexNameResolver.getIndexNameFromKind(kind)).thenReturn(kind.replace(":", "-")); - when(this.schemaCache.get(kind)).thenReturn(null); - when(this.indicesService.isIndexExist(any(), any())).thenReturn(true); - when(this.storageService.getStorageSchema(kind)).thenReturn(storageSchema); - - this.sut.processSchemaMessages(schemaMessages); - - verify(this.log).warning(eq("schema not found for kind: tenant1:avocet:completion:1.0.0")); - } - - @Test - public void should_invalidateCache_when_purge_schema_and_schema_found_in_cache() throws IOException { - String kind = "tenant1:avocet:completion:1.0.0"; - Map<String, OperationType> schemaMessages = new HashMap<>(); - schemaMessages.put(kind, OperationType.purge_schema); - - when(this.elasticIndexNameResolver.getIndexNameFromKind(kind)).thenReturn(kind.replace(":", "-")); - when(this.indicesService.isIndexExist(any(), any())).thenReturn(true); - when(this.schemaCache.get(kind)).thenReturn("schema"); - when(this.schemaCache.get(kind + "_flattened")).thenReturn("flattened schema"); - - this.sut.processSchemaMessages(schemaMessages); - - verify(this.schemaCache, times(2)).get(anyString()); - verify(this.schemaCache, times(2)).delete(anyString()); - } - - @Test - public void should_log_warning_when_purge_schema_and_schema_not_found_in_cache() throws IOException { - String kind = "tenant1:avocet:completion:1.0.0"; - Map<String, OperationType> schemaMessages = new HashMap<>(); - schemaMessages.put(kind, OperationType.purge_schema); - - when(this.elasticIndexNameResolver.getIndexNameFromKind(kind)).thenReturn(kind.replace(":", "-")); - when(this.indicesService.isIndexExist(any(), any())).thenReturn(false); - - this.sut.processSchemaMessages(schemaMessages); - - verify(this.log).warning(eq(String.format("Kind: %s not found", kind))); - } - - @Test - public void should_sync_schema_with_storage() throws Exception { - String kind = "tenant1:avocet:completion:1.0.0"; - String storageSchema = "{" + - " \"kind\": \"tenant1:avocet:completion:1.0.0\"," + - " \"schema\": [" + - " {" + - " \"path\": \"status\"," + - " \"kind\": \"string\"" + - " }" + - " ]" + - "}"; - when(this.elasticIndexNameResolver.getIndexNameFromKind(kind)).thenReturn(kind.replace(":", "-")); - when(this.schemaCache.get(kind)).thenReturn(null); - when(this.indicesService.isIndexExist(any(), any())).thenReturn(true); - when(this.indicesService.deleteIndex(any(), any())).thenReturn(true); - when(this.storageService.getStorageSchema(kind)).thenReturn(storageSchema); - - this.sut.syncIndexMappingWithStorageSchema(kind); - - verify(this.mappingService, times(1)).getIndexMappingFromRecordSchema(any()); - verify(this.indicesService, times(1)).isIndexExist(any(), any()); - verify(this.indicesService, times(1)).deleteIndex(any(), any()); - verify(this.indicesService, times(1)).createIndex(any(), any(), any(), any(), any()); - verifyNoMoreInteractions(this.mappingService); - } - - @Test - public void should_throw_exception_while_snapshot_running_sync_schema_with_storage() throws Exception { - String kind = "tenant1:avocet:completion:1.0.0"; - when(this.elasticIndexNameResolver.getIndexNameFromKind(kind)).thenReturn(kind.replace(":", "-")); - when(this.schemaCache.get(kind)).thenReturn(null); - when(this.indicesService.isIndexExist(any(), any())).thenReturn(true); - when(this.indicesService.deleteIndex(any(), any())).thenThrow(new AppException(HttpStatus.SC_CONFLICT, "Index deletion error", "blah")); - - try { - this.sut.syncIndexMappingWithStorageSchema(kind); - } catch (AppException e) { - assertEquals(e.getError().getCode(), HttpStatus.SC_CONFLICT); - assertEquals(e.getError().getMessage(), "blah"); - assertEquals(e.getError().getReason(), "Index deletion error"); - } catch (Exception e) { - fail("Should not throw this exception " + e.getMessage()); - } - - verify(this.indicesService, times(1)).isIndexExist(any(), any()); - verify(this.indicesService, times(1)).deleteIndex(any(), any()); - verify(this.mappingService, never()).getIndexMappingFromRecordSchema(any()); - verify(this.indicesService, never()).createIndex(any(), any(), any(), any(), any()); - } - - @Test - public void should_return_true_while_if_forceClean_requested() throws IOException { - String kind = "tenant1:avocet:completion:1.0.0"; - when(this.elasticIndexNameResolver.getIndexNameFromKind(kind)).thenReturn(kind.replace(":", "-")); - when(this.indicesService.isIndexExist(any(), any())).thenReturn(true); - - assertTrue(this.sut.isStorageSchemaSyncRequired(kind, true)); - } - - @Test - public void should_return_true_while_if_forceClean_notRequested_and_indexNotFound() throws IOException { - String kind = "tenant1:avocet:completion:1.0.0"; - when(this.elasticIndexNameResolver.getIndexNameFromKind(kind)).thenReturn(kind.replace(":", "-")); - when(this.indicesService.isIndexExist(any(), any())).thenReturn(false); - - assertTrue(this.sut.isStorageSchemaSyncRequired(kind, false)); - } - - @Test - public void should_return_false_while_if_forceClean_notRequested_and_indexExist() throws IOException { - String kind = "tenant1:avocet:completion:1.0.0"; - when(this.elasticIndexNameResolver.getIndexNameFromKind(kind)).thenReturn(kind.replace(":", "-")); - when(this.indicesService.isIndexExist(any(), any())).thenReturn(true); - - assertFalse(this.sut.isStorageSchemaSyncRequired(kind, false)); - } -} diff --git a/provider/indexer-reference/src/test/java/org/opengroup/osdu/indexer/service/IndexerSchemaServiceTest.java b/provider/indexer-reference/src/test/java/org/opengroup/osdu/indexer/service/IndexerSchemaServiceTest.java deleted file mode 100644 index 3b59eb203..000000000 --- a/provider/indexer-reference/src/test/java/org/opengroup/osdu/indexer/service/IndexerSchemaServiceTest.java +++ /dev/null @@ -1,445 +0,0 @@ -/* - * Copyright 2021 Google LLC - * Copyright 2021 EPAM Systems, Inc - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.opengroup.osdu.indexer.service; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; -import static org.mockito.Mockito.any; -import static org.mockito.Mockito.anyBoolean; -import static org.mockito.Mockito.anyString; -import static org.mockito.Mockito.eq; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoMoreInteractions; -import static org.mockito.MockitoAnnotations.initMocks; -import static org.powermock.api.mockito.PowerMockito.mock; -import static org.powermock.api.mockito.PowerMockito.when; - -import java.io.IOException; -import java.net.URISyntaxException; -import java.util.HashMap; -import java.util.Map; -import org.apache.http.HttpStatus; -import org.elasticsearch.client.RestHighLevelClient; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.opengroup.osdu.core.common.logging.JaxRsDpsLog; -import org.opengroup.osdu.core.common.model.http.AppException; -import org.opengroup.osdu.core.common.model.http.RequestStatus; -import org.opengroup.osdu.core.common.model.indexer.IndexSchema; -import org.opengroup.osdu.core.common.model.indexer.OperationType; -import org.opengroup.osdu.core.common.search.ElasticIndexNameResolver; -import org.opengroup.osdu.core.common.search.IndicesService; -import org.opengroup.osdu.indexer.provider.interfaces.ISchemaCache; -import org.opengroup.osdu.indexer.service.SchemaProviderImpl; -import org.opengroup.osdu.indexer.util.ElasticClientHandler; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.springframework.test.context.junit4.SpringRunner; - -@RunWith(SpringRunner.class) -@PrepareForTest({RestHighLevelClient.class}) -public class IndexerSchemaServiceTest { - - private final String kind = "tenant:test:test:1.0.0"; - private final String emptySchema = null; - private final String someSchema = "{\"kind\":\"tenant:test:test:1.0.0\", \"schema\":[{\"path\":\"test-path\", \"kind\":\"tenant:test:test:1.0.0\"}]}"; - - @Mock - private JaxRsDpsLog log; - @Mock - private SchemaProviderImpl schemaProvider; - @Mock - private ElasticClientHandler elasticClientHandler; - @Mock - private ElasticIndexNameResolver elasticIndexNameResolver; - @Mock - private IndexerMappingService mappingService; - @Mock - private IndicesService indicesService; - @Mock - private ISchemaCache schemaCache; - @InjectMocks - private IndexSchemaServiceImpl sut; - - @Before - public void setup() { - initMocks(this); - RestHighLevelClient restHighLevelClient = mock(RestHighLevelClient.class); - when(elasticClientHandler.createRestClient()).thenReturn(restHighLevelClient); - } - - @Test - public void should_returnNull_givenEmptySchema_getIndexerInputSchemaSchemaTest() - throws Exception { - when(schemaProvider.getSchema(any())).thenReturn(emptySchema); - - IndexSchema indexSchema = this.sut.getIndexerInputSchema(kind, false); - - Assert.assertNotNull(indexSchema); - } - - @Test - public void should_returnValidResponse_givenValidSchema_getIndexerInputSchemaTest() - throws Exception { - when(schemaProvider.getSchema(any())).thenReturn(someSchema); - - IndexSchema indexSchema = this.sut.getIndexerInputSchema(kind, false); - - Assert.assertEquals(kind, indexSchema.getKind()); - } - - @Test - public void should_returnValidResponse_givenValidSchemaWithCacheHit_getIndexerInputSchemaTest() - throws Exception { - when(schemaProvider.getSchema(any())).thenReturn(someSchema); - when(this.schemaCache.get(kind + "_flattened")).thenReturn(someSchema); - - IndexSchema indexSchema = this.sut.getIndexerInputSchema(kind, false); - - Assert.assertEquals(kind, indexSchema.getKind()); - } - - @Test - public void should_throw500_givenInvalidSchemaCacheHit_getIndexerInputSchemaTest() { - try { - String invalidSchema = "{}}"; - when(schemaProvider.getSchema(any())).thenReturn(invalidSchema); - - this.sut.getIndexerInputSchema(kind, false); - fail("Should throw exception"); - } catch (AppException e) { - Assert.assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getError().getCode()); - Assert.assertEquals("An error has occurred while normalizing the schema.", - e.getError().getMessage()); - } catch (Exception e) { - fail("Should not throw exception" + e.getMessage()); - } - } - - @Test - public void should_return_basic_schema_when_storage_returns_no_schema() { - IndexSchema returnedSchema = this.sut.getIndexerInputSchema(kind, false); - - assertNotNull(returnedSchema.getDataSchema()); - assertNotNull(returnedSchema); - assertEquals(kind, returnedSchema.getKind()); - } - - @Test - public void should_create_schema_when_storage_returns_valid_schema() - throws IOException, URISyntaxException { - String kind = "tenant1:avocet:completion:1.0.0"; - String storageSchema = "{" + - " \"kind\": \"tenant1:avocet:completion:1.0.0\"," + - " \"schema\": [" + - " {" + - " \"path\": \"status\"," + - " \"kind\": \"string\"" + - " }," + - " {" + - " \"path\": \"startDate\"," + - " \"kind\": \"string\"" + - " }," + - " {" + - " \"path\": \"endDate\"," + - " \"kind\": \"string\"" + - " }," + - " {" + - " \"path\": \"type \"," + - " \"kind\": \"string\"" + - " }," + - " {" + - " \"path\": \"itemguid\"," + - " \"kind\": \"string\"" + - " }" + - " ]" + - "}"; - Map<String, OperationType> schemaMessages = new HashMap<>(); - schemaMessages.put(kind, OperationType.create_schema); - - when(this.elasticIndexNameResolver.getIndexNameFromKind(kind)) - .thenReturn(kind.replace(":", "-")); - when(this.schemaCache.get(kind)).thenReturn(null); - when(this.indicesService.isIndexExist(any(), any())).thenReturn(false); - when(this.schemaProvider.getSchema(kind)).thenReturn(storageSchema); - - this.sut.processSchemaMessages(schemaMessages); - - verify(this.mappingService, times(1)).getIndexMappingFromRecordSchema(any()); - verify(this.indicesService, times(1)).createIndex(any(), any(), any(), any(), any()); - verifyNoMoreInteractions(this.mappingService); - } - - @Test - public void should_merge_mapping_when_storage_returns_valid_schema() - throws IOException, URISyntaxException { - String kind = "tenant1:avocet:completion:1.0.0"; - String storageSchema = "{" + - " \"kind\": \"tenant1:avocet:completion:1.0.0\"," + - " \"schema\": [" + - " {" + - " \"path\": \"status\"," + - " \"kind\": \"string\"" + - " }," + - " {" + - " \"path\": \"startDate\"," + - " \"kind\": \"string\"" + - " }" + - " ]" + - "}"; - Map<String, OperationType> schemaMessages = new HashMap<>(); - schemaMessages.put(kind, OperationType.create_schema); - - when(this.elasticIndexNameResolver.getIndexNameFromKind(kind)) - .thenReturn(kind.replace(":", "-")); - when(this.schemaCache.get(kind)).thenReturn(null); - when(this.indicesService.isIndexExist(any(), any())).thenReturn(true); - when(this.schemaProvider.getSchema(kind)).thenReturn(storageSchema); - - this.sut.processSchemaMessages(schemaMessages); - - verify(this.indicesService, times(0)).createIndex(any(), any(), any(), any(), any()); - verify(this.mappingService, times(1)).createMapping(any(), any(), any(), anyBoolean()); - verifyNoMoreInteractions(this.mappingService); - } - - @Test - public void should_throw_mapping_conflict_when_elastic_backend_cannot_process_schema_changes() - throws IOException, URISyntaxException { - String kind = "tenant1:avocet:completion:1.0.0"; - String reason = String - .format("Could not create type mapping %s/completion.", kind.replace(":", "-")); - String storageSchema = "{" + - " \"kind\": \"tenant1:avocet:completion:1.0.0\"," + - " \"schema\": [" + - " {" + - " \"path\": \"status\"," + - " \"kind\": \"string\"" + - " }" + - " ]" + - "}"; - Map<String, OperationType> schemaMessages = new HashMap<>(); - schemaMessages.put(kind, OperationType.create_schema); - - when(this.elasticIndexNameResolver.getIndexNameFromKind(kind)) - .thenReturn(kind.replace(":", "-")); - when(this.schemaCache.get(kind)).thenReturn(null); - when(this.indicesService.isIndexExist(any(), any())).thenReturn(true); - when(this.schemaProvider.getSchema(kind)).thenReturn(storageSchema); - when(this.mappingService.createMapping(any(), any(), any(), anyBoolean())) - .thenThrow(new AppException(HttpStatus.SC_BAD_REQUEST, reason, "")); - - try { - this.sut.processSchemaMessages(schemaMessages); - } catch (AppException e) { - assertEquals(RequestStatus.SCHEMA_CONFLICT, e.getError().getCode()); - assertEquals("error creating or merging index mapping", e.getError().getMessage()); - assertEquals(reason, e.getError().getReason()); - } catch (Exception e) { - fail("Should not throw this exception " + e.getMessage()); - } - } - - @Test - public void should_throw_genericAppException_when_elastic_backend_cannot_process_schema_changes() - throws IOException, URISyntaxException { - String kind = "tenant1:avocet:completion:1.0.0"; - String reason = String - .format("Could not create type mapping %s/completion.", kind.replace(":", "-")); - String storageSchema = "{" + - " \"kind\": \"tenant1:avocet:completion:1.0.0\"," + - " \"schema\": [" + - " {" + - " \"path\": \"status\"," + - " \"kind\": \"string\"" + - " }" + - " ]" + - "}"; - Map<String, OperationType> schemaMessages = new HashMap<>(); - schemaMessages.put(kind, OperationType.create_schema); - - when(this.elasticIndexNameResolver.getIndexNameFromKind(kind)) - .thenReturn(kind.replace(":", "-")); - when(this.schemaCache.get(kind)).thenReturn(null); - when(this.indicesService.isIndexExist(any(), any())).thenReturn(true); - when(this.schemaProvider.getSchema(kind)).thenReturn(storageSchema); - when(this.mappingService.createMapping(any(), any(), any(), anyBoolean())) - .thenThrow(new AppException(HttpStatus.SC_FORBIDDEN, reason, "blah")); - - try { - this.sut.processSchemaMessages(schemaMessages); - } catch (AppException e) { - assertEquals(HttpStatus.SC_FORBIDDEN, e.getError().getCode()); - assertEquals("blah", e.getError().getMessage()); - assertEquals(reason, e.getError().getReason()); - } catch (Exception e) { - fail("Should not throw this exception " + e.getMessage()); - } - } - - @Test - public void should_log_and_do_nothing_when_storage_returns_invalid_schema() - throws IOException, URISyntaxException { - String kind = "tenant1:avocet:completion:1.0.0"; - String storageSchema = "{" + - " \"kind\": \"tenant1:avocet:completion:1.0.0\"" + - "}"; - Map<String, OperationType> schemaMessages = new HashMap<>(); - schemaMessages.put(kind, OperationType.create_schema); - - when(this.elasticIndexNameResolver.getIndexNameFromKind(kind)) - .thenReturn(kind.replace(":", "-")); - when(this.schemaCache.get(kind)).thenReturn(null); - when(this.indicesService.isIndexExist(any(), any())).thenReturn(true); - when(this.schemaProvider.getSchema(kind)).thenReturn(storageSchema); - - this.sut.processSchemaMessages(schemaMessages); - - verify(this.log).warning(eq("schema not found for kind: tenant1:avocet:completion:1.0.0")); - } - - @Test - public void should_invalidateCache_when_purge_schema_and_schema_found_in_cache() - throws IOException { - String kind = "tenant1:avocet:completion:1.0.0"; - Map<String, OperationType> schemaMessages = new HashMap<>(); - schemaMessages.put(kind, OperationType.purge_schema); - - when(this.elasticIndexNameResolver.getIndexNameFromKind(kind)) - .thenReturn(kind.replace(":", "-")); - when(this.indicesService.isIndexExist(any(), any())).thenReturn(true); - when(this.schemaCache.get(kind)).thenReturn("schema"); - when(this.schemaCache.get(kind + "_flattened")).thenReturn("flattened schema"); - - this.sut.processSchemaMessages(schemaMessages); - - verify(this.schemaCache, times(2)).get(anyString()); - verify(this.schemaCache, times(2)).delete(anyString()); - } - - @Test - public void should_log_warning_when_purge_schema_and_schema_not_found_in_cache() - throws IOException { - String kind = "tenant1:avocet:completion:1.0.0"; - Map<String, OperationType> schemaMessages = new HashMap<>(); - schemaMessages.put(kind, OperationType.purge_schema); - - when(this.elasticIndexNameResolver.getIndexNameFromKind(kind)) - .thenReturn(kind.replace(":", "-")); - when(this.indicesService.isIndexExist(any(), any())).thenReturn(false); - - this.sut.processSchemaMessages(schemaMessages); - - verify(this.log).warning(eq(String.format("Kind: %s not found", kind))); - } - - @Test - public void should_sync_schema_with_storage() throws Exception { - String kind = "tenant1:avocet:completion:1.0.0"; - String storageSchema = "{" + - " \"kind\": \"tenant1:avocet:completion:1.0.0\"," + - " \"schema\": [" + - " {" + - " \"path\": \"status\"," + - " \"kind\": \"string\"" + - " }" + - " ]" + - "}"; - when(this.elasticIndexNameResolver.getIndexNameFromKind(kind)) - .thenReturn(kind.replace(":", "-")); - when(this.schemaCache.get(kind)).thenReturn(null); - when(this.indicesService.isIndexExist(any(), any())).thenReturn(true); - when(this.indicesService.deleteIndex(any(), any())).thenReturn(true); - when(this.schemaProvider.getSchema(kind)).thenReturn(storageSchema); - - this.sut.syncIndexMappingWithStorageSchema(kind); - - verify(this.mappingService, times(1)).getIndexMappingFromRecordSchema(any()); - verify(this.indicesService, times(1)).isIndexExist(any(), any()); - verify(this.indicesService, times(1)).deleteIndex(any(), any()); - verify(this.indicesService, times(1)).createIndex(any(), any(), any(), any(), any()); - verifyNoMoreInteractions(this.mappingService); - } - - @Test - public void should_throw_exception_while_snapshot_running_sync_schema_with_storage() - throws Exception { - String kind = "tenant1:avocet:completion:1.0.0"; - when(this.elasticIndexNameResolver.getIndexNameFromKind(kind)) - .thenReturn(kind.replace(":", "-")); - when(this.schemaCache.get(kind)).thenReturn(null); - when(this.indicesService.isIndexExist(any(), any())).thenReturn(true); - when(this.indicesService.deleteIndex(any(), any())) - .thenThrow(new AppException(HttpStatus.SC_CONFLICT, "Index deletion error", "blah")); - - try { - this.sut.syncIndexMappingWithStorageSchema(kind); - } catch (AppException e) { - assertEquals(HttpStatus.SC_CONFLICT, e.getError().getCode()); - assertEquals("blah", e.getError().getMessage()); - assertEquals("Index deletion error", e.getError().getReason()); - } catch (Exception e) { - fail("Should not throw this exception " + e.getMessage()); - } - - verify(this.indicesService, times(1)).isIndexExist(any(), any()); - verify(this.indicesService, times(1)).deleteIndex(any(), any()); - verify(this.mappingService, never()).getIndexMappingFromRecordSchema(any()); - verify(this.indicesService, never()).createIndex(any(), any(), any(), any(), any()); - } - - @Test - public void should_return_true_while_if_forceClean_requested() throws IOException { - String kind = "tenant1:avocet:completion:1.0.0"; - when(this.elasticIndexNameResolver.getIndexNameFromKind(kind)) - .thenReturn(kind.replace(":", "-")); - when(this.indicesService.isIndexExist(any(), any())).thenReturn(true); - - assertTrue(this.sut.isStorageSchemaSyncRequired(kind, true)); - } - - @Test - public void should_return_true_while_if_forceClean_notRequested_and_indexNotFound() - throws IOException { - String kind = "tenant1:avocet:completion:1.0.0"; - when(this.elasticIndexNameResolver.getIndexNameFromKind(kind)) - .thenReturn(kind.replace(":", "-")); - when(this.indicesService.isIndexExist(any(), any())).thenReturn(false); - - assertTrue(this.sut.isStorageSchemaSyncRequired(kind, false)); - } - - @Test - public void should_return_false_while_if_forceClean_notRequested_and_indexExist() - throws IOException { - String kind = "tenant1:avocet:completion:1.0.0"; - when(this.elasticIndexNameResolver.getIndexNameFromKind(kind)) - .thenReturn(kind.replace(":", "-")); - when(this.indicesService.isIndexExist(any(), any())).thenReturn(true); - - assertFalse(this.sut.isStorageSchemaSyncRequired(kind, false)); - } -} -- GitLab