From 2a076c521712555a5e9048512368740a77da83b7 Mon Sep 17 00:00:00 2001 From: Stanislav Riabokon <Stanislav_Riabokon@epam.com> Date: Mon, 11 Jan 2021 12:38:48 +0400 Subject: [PATCH] Fixed sonar comments for reference provider --- .../indexer/service/CronServiceImplTest.java | 8 +- .../service/ElasticSettingServiceTest.java | 4 + .../service/IndexerMappingServiceTest.java | 10 +- .../service/IndexerSchemaServiceTest.java | 41 +- .../indexer/service/IndexerServiceTest.java | 354 ------------------ .../indexer/service/ReindexServiceTest.java | 9 +- .../indexer/service/StorageServiceTest.java | 6 +- .../ServiceAccountJwtGcpClientImplTest.java | 10 +- 8 files changed, 49 insertions(+), 393 deletions(-) delete mode 100644 provider/indexer-reference/src/test/java/org/opengroup/osdu/indexer/service/IndexerServiceTest.java diff --git a/provider/indexer-reference/src/test/java/org/opengroup/osdu/indexer/service/CronServiceImplTest.java b/provider/indexer-reference/src/test/java/org/opengroup/osdu/indexer/service/CronServiceImplTest.java index 80e25bbb4..9633d1289 100644 --- a/provider/indexer-reference/src/test/java/org/opengroup/osdu/indexer/service/CronServiceImplTest.java +++ b/provider/indexer-reference/src/test/java/org/opengroup/osdu/indexer/service/CronServiceImplTest.java @@ -29,10 +29,10 @@ import org.opengroup.osdu.core.common.model.search.IndexInfo; import org.opengroup.osdu.core.common.logging.JaxRsDpsLog; import org.opengroup.osdu.core.common.provider.interfaces.IRequestInfo; import org.opengroup.osdu.core.common.search.IndicesService; +import org.opengroup.osdu.indexer.config.IndexerConfigurationProperties; import org.opengroup.osdu.indexer.util.ElasticClientHandler; import org.powermock.core.classloader.annotations.PrepareForTest; import org.springframework.test.context.junit4.SpringRunner; -import org.springframework.test.util.ReflectionTestUtils; import java.io.IOException; import java.time.Instant; @@ -52,6 +52,8 @@ public class CronServiceImplTest { @Mock private ElasticClientHandler elasticClientHandler; @Mock + private IndexerConfigurationProperties configurationProperties; + @Mock private IRequestInfo requestInfo; @Mock private JaxRsDpsLog log; @@ -66,8 +68,8 @@ public class CronServiceImplTest { when(this.requestInfo.getHeaders()).thenReturn(dpsHeaders); - ReflectionTestUtils.setField(this.sut, "CRON_INDEX_CLEANUP_THRESHOLD_DAYS", "3"); - ReflectionTestUtils.setField(this.sut, "CRON_EMPTY_INDEX_CLEANUP_THRESHOLD_DAYS", "7"); + when(configurationProperties.getCronIndexCleanupThresholdDays()).thenReturn(3); + when(configurationProperties.getCronEmptyIndexCleanupThresholdDays()).thenReturn(3); } @Test diff --git a/provider/indexer-reference/src/test/java/org/opengroup/osdu/indexer/service/ElasticSettingServiceTest.java b/provider/indexer-reference/src/test/java/org/opengroup/osdu/indexer/service/ElasticSettingServiceTest.java index 9fcf3b4a7..f9645a509 100644 --- a/provider/indexer-reference/src/test/java/org/opengroup/osdu/indexer/service/ElasticSettingServiceTest.java +++ b/provider/indexer-reference/src/test/java/org/opengroup/osdu/indexer/service/ElasticSettingServiceTest.java @@ -30,6 +30,7 @@ import org.opengroup.osdu.core.common.logging.JaxRsDpsLog; import org.opengroup.osdu.core.common.provider.interfaces.IElasticCredentialsCache; import org.opengroup.osdu.core.common.provider.interfaces.IElasticRepository; import org.opengroup.osdu.core.common.multitenancy.ITenantInfoService; +import org.opengroup.osdu.indexer.config.IndexerConfigurationProperties; import org.springframework.test.context.junit4.SpringRunner; import static org.junit.Assert.assertEquals; @@ -46,6 +47,8 @@ public class ElasticSettingServiceTest { @Mock private IElasticCredentialsCache elasticCredentialCache; @Mock + private IndexerConfigurationProperties configurationProperties; + @Mock private TenantInfo tenantInfo; @InjectMocks private ElasticSettingServiceImpl sut; @@ -72,6 +75,7 @@ public class ElasticSettingServiceTest { when(tenantInfo.getName()).thenReturn("tenant1"); when(this.headersInfo.getPartitionId()).thenReturn("tenant1"); when(this.tenantInfoService.getTenantInfo()).thenReturn(tenantInfo); + when(configurationProperties.getGaeService()).thenReturn("indexer"); clusterSettings = ClusterSettings.builder().host(host).port(port).userNameAndPassword(credentials).build(); cacheKey = String.format("%s-%s", GAE_SERVICE, tenantInfo.getName()); } diff --git a/provider/indexer-reference/src/test/java/org/opengroup/osdu/indexer/service/IndexerMappingServiceTest.java b/provider/indexer-reference/src/test/java/org/opengroup/osdu/indexer/service/IndexerMappingServiceTest.java index d091cf83b..b4eca3a44 100644 --- a/provider/indexer-reference/src/test/java/org/opengroup/osdu/indexer/service/IndexerMappingServiceTest.java +++ b/provider/indexer-reference/src/test/java/org/opengroup/osdu/indexer/service/IndexerMappingServiceTest.java @@ -38,7 +38,7 @@ import org.mockito.Mock; import org.opengroup.osdu.core.common.model.http.AppException; import org.opengroup.osdu.core.common.model.indexer.IndexSchema; import org.opengroup.osdu.core.common.model.search.RecordMetaAttribute; -import org.opengroup.osdu.core.common.search.Config; +import org.opengroup.osdu.indexer.config.IndexerConfigurationProperties; import org.opengroup.osdu.indexer.util.ElasticClientHandler; import org.opengroup.osdu.indexer.util.TypeMapper; import org.powermock.api.mockito.PowerMockito; @@ -54,12 +54,11 @@ import static org.mockito.Matchers.any; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; import static org.mockito.MockitoAnnotations.initMocks; -import static org.powermock.api.mockito.PowerMockito.mockStatic; import static org.powermock.api.mockito.PowerMockito.when; @Ignore @RunWith(SpringRunner.class) -@PrepareForTest({ RestHighLevelClient.class, IndicesClient.class, Config.class }) +@PrepareForTest({ RestHighLevelClient.class, IndicesClient.class}) public class IndexerMappingServiceTest { private final String kind = "tenant:test:test:1.0.0"; @@ -67,6 +66,8 @@ public class IndexerMappingServiceTest { private final String type = "test"; private final String mappingValid = "{\"dynamic\":false,\"properties\":{\"data\":{\"properties\":{\"Msg\":{\"type\":\"text\",\"analyzer\":\"de_indexer_analyzer\",\"search_analyzer\":\"de_search_analyzer\"},\"Location\":{\"type\":\"geo_point\"}}},\"id\":{\"type\":\"keyword\"},\"acl\":{\"properties\":{\"viewers\":{\"type\":\"keyword\"},\"owners\":{\"type\":\"keyword\"}}}}}"; + @Mock + private IndexerConfigurationProperties configurationProperties; @Mock private RestClient restClient; @Mock @@ -94,8 +95,7 @@ public class IndexerMappingServiceTest { @Before public void setup() throws IOException { initMocks(this); - mockStatic(Config.class); - when(Config.isPreDemo()).thenReturn(true); + when(configurationProperties.isPreDemo()).thenReturn(true); Map<String, String> dataMapping = new HashMap<>(); dataMapping.put("Location", "geo_point"); dataMapping.put("Msg", "text"); 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 index e1dd9c641..27b5f786c 100644 --- 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 @@ -32,6 +32,7 @@ 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.impl.SchemaProviderImpl; import org.opengroup.osdu.indexer.util.ElasticClientHandler; import org.opengroup.osdu.core.common.search.ElasticIndexNameResolver; import org.powermock.core.classloader.annotations.PrepareForTest; @@ -59,7 +60,7 @@ public class IndexerSchemaServiceTest { @Mock private JaxRsDpsLog log; @Mock - private StorageService storageService; + private SchemaProviderImpl schemaProvider; @Mock private ElasticClientHandler elasticClientHandler; @Mock @@ -82,7 +83,7 @@ public class IndexerSchemaServiceTest { @Test public void should_returnNull_givenEmptySchema_getIndexerInputSchemaSchemaTest() throws Exception { - when(storageService.getStorageSchema(any())).thenReturn(emptySchema); + when(schemaProvider.getSchema(any())).thenReturn(emptySchema); IndexSchema indexSchema = this.sut.getIndexerInputSchema(kind, false); @@ -91,7 +92,7 @@ public class IndexerSchemaServiceTest { @Test public void should_returnValidResponse_givenValidSchema_getIndexerInputSchemaTest() throws Exception { - when(storageService.getStorageSchema(any())).thenReturn(someSchema); + when(schemaProvider.getSchema(any())).thenReturn(someSchema); IndexSchema indexSchema = this.sut.getIndexerInputSchema(kind, false); @@ -100,7 +101,7 @@ public class IndexerSchemaServiceTest { @Test public void should_returnValidResponse_givenValidSchemaWithCacheHit_getIndexerInputSchemaTest() throws Exception { - when(storageService.getStorageSchema(any())).thenReturn(someSchema); + when(schemaProvider.getSchema(any())).thenReturn(someSchema); when(this.schemaCache.get(kind + "_flattened")).thenReturn(someSchema); IndexSchema indexSchema = this.sut.getIndexerInputSchema(kind, false); @@ -112,7 +113,7 @@ public class IndexerSchemaServiceTest { public void should_throw500_givenInvalidSchemaCacheHit_getIndexerInputSchemaTest() { try { String invalidSchema = "{}}"; - when(storageService.getStorageSchema(any())).thenReturn(invalidSchema); + when(schemaProvider.getSchema(any())).thenReturn(invalidSchema); this.sut.getIndexerInputSchema(kind, false); fail("Should throw exception"); @@ -167,7 +168,7 @@ public class IndexerSchemaServiceTest { 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); + when(this.schemaProvider.getSchema(kind)).thenReturn(storageSchema); this.sut.processSchemaMessages(schemaMessages); @@ -198,7 +199,7 @@ public class IndexerSchemaServiceTest { 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.schemaProvider.getSchema(kind)).thenReturn(storageSchema); this.sut.processSchemaMessages(schemaMessages); @@ -226,15 +227,15 @@ public class IndexerSchemaServiceTest { 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.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(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()); } @@ -259,15 +260,15 @@ public class IndexerSchemaServiceTest { 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.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(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()); } @@ -285,7 +286,7 @@ public class IndexerSchemaServiceTest { 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.schemaProvider.getSchema(kind)).thenReturn(storageSchema); this.sut.processSchemaMessages(schemaMessages); @@ -339,7 +340,7 @@ public class IndexerSchemaServiceTest { 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); + when(this.schemaProvider.getSchema(kind)).thenReturn(storageSchema); this.sut.syncIndexMappingWithStorageSchema(kind); @@ -361,9 +362,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()); } diff --git a/provider/indexer-reference/src/test/java/org/opengroup/osdu/indexer/service/IndexerServiceTest.java b/provider/indexer-reference/src/test/java/org/opengroup/osdu/indexer/service/IndexerServiceTest.java deleted file mode 100644 index f2a8886f1..000000000 --- a/provider/indexer-reference/src/test/java/org/opengroup/osdu/indexer/service/IndexerServiceTest.java +++ /dev/null @@ -1,354 +0,0 @@ -/* - * Copyright 2020 Google LLC - * Copyright 2020 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;//// 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.opendes.indexer.service; -// -//import com.google.gson.Gson; -//import com.google.gson.reflect.TypeToken; -//import org.elasticsearch.action.bulk.BulkItemResponse; -//import org.elasticsearch.action.bulk.BulkResponse; -//import org.elasticsearch.client.RequestOptions; -//import org.elasticsearch.client.RestHighLevelClient; -//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.mockito.Spy; -//import org.opendes.client.api.DpsHeaders; -//import org.opendes.core.logging.JaxRsDpsLog; -//import org.opendes.core.model.DeploymentEnvironment; -//import org.opendes.core.model.RecordChangedMessages; -//import org.opendes.core.service.IndicesService; -//import org.opendes.core.util.Config; -//import org.opendes.core.util.ElasticClientHandler; -//import org.opendes.core.util.ElasticIndexNameResolver; -//import org.opendes.core.util.HeadersUtil; -//import org.opendes.indexer.logging.AuditLogger; -//import org.opendes.indexer.model.*; -//import org.opendes.indexer.publish.IPublisher; -//import org.opendes.indexer.util.IRequestInfo; -//import org.opendes.indexer.util.IndexerQueueTaskBuilder; -//import org.opendes.indexer.util.JobStatus; -//import org.opendes.indexer.util.RecordInfo; -//import org.powermock.core.classloader.annotations.PrepareForTest; -//import javax.inject.Inject; -//import org.springframework.context.annotation.Lazy; -//import org.springframework.test.context.junit4.SpringRunner; -// -//import java.io.IOException; -//import java.lang.reflect.Type; -//import java.util.*; -// -//import static java.util.Collections.singletonList; -//import static org.junit.Assert.*; -//import static org.mockito.Matchers.any; -//import static org.mockito.Mockito.verify; -//import static org.mockito.Mockito.when; -//import static org.powermock.api.mockito.PowerMockito.mock; -//import static org.powermock.api.mockito.PowerMockito.mockStatic; -// -//@Ignore -//@RunWith(SpringRunner.class) -//@PrepareForTest({RestHighLevelClient.class, BulkResponse.class, StorageAcl.class, HeadersUtil.class, Config.class}) -//public class IndexerServiceTest { -// -// private final String pubsubMsg = "[{\"id\":\"tenant1:doc:test1\",\"kind\":\"tenant1:testindexer1:well:1.0.0\",\"op\":\"update\"}," + -// "{\"id\":\"tenant1:doc:test2\",\"kind\":\"tenant1:testindexer2:well:1.0.0\",\"op\":\"create\"}]"; -// private final String kind1 = "tenant1:testindexer1:well:1.0.0"; -// private final String kind2 = "tenant1:testindexer2:well:1.0.0"; -// private final String recordId1 = "tenant1:doc:test1"; -// private final String recordId2 = "tenant1:doc:test2"; -// private final String failureMassage = "test failure"; -// -// @Mock -// private IndexSchemaService indexSchemaService; -// @Mock -// private IndicesService indicesService; -// @Mock -// private IndexerMappingService indexerMappingService; -// @Mock -// private StorageService storageService; -// @Mock -// private IPublisher publisherImpl; -// @Mock -// private RestHighLevelClient restHighLevelClient; -// @Mock -// private ElasticClientHandler elasticClientHandler; -// @Mock -// private BulkResponse bulkResponse; -// @Mock -// private IRequestInfo requestInfo; -// @Mock -// private ElasticIndexNameResolver elasticIndexNameResolver; -// @Mock -// private AttributeParsingServiceImpl attributeParsingServiceImpl; -// @Mock -// private IndexerQueueTaskBuilder indexerQueueTaskBuilder; -// @Mock -// private JaxRsDpsLog log; -// @Mock -// private AuditLogger auditLogger; -// @InjectMocks -// private IndexerServiceImpl sut; -// @InjectMocks @Spy -// private JobStatus jobStatus = new JobStatus(); -// -// @Inject -// @Lazy -// private DpsHeaders dpsHeaders; -// private RecordChangedMessages recordChangedMessages; -// private List<RecordInfo> recordInfos; -// -// @Before -// public void setup() throws IOException { -// -// mockStatic(StorageAcl.class); -// mockStatic(Config.class); -// -// when(Config.getDeploymentEnvironment()).thenReturn(DeploymentEnvironment.LOCAL); -// when(Config.getElasticClusterName()).thenReturn("CLUSTER"); -// when(Config.getElasticServerAddress()).thenReturn("testsite"); -// -// dpsHeaders = new DpsHeaders(); -// dpsHeaders.put(AppEngineHeaders.TASK_QUEUE_RETRY_COUNT, "1"); -// dpsHeaders.put(DpsHeaders.AUTHORIZATION, "testAuth"); -// when(requestInfo.getHeaders()).thenReturn(dpsHeaders); -// when(requestInfo.getHeadersMapWithDwdAuthZ()).thenReturn(dpsHeaders.getHeaders()); -// -// Type listType = new TypeToken<List<RecordInfo>>() {}.getType(); -// recordInfos = (new Gson()).fromJson(pubsubMsg, listType); -// -// when(elasticClientHandler.createRestClient()).thenReturn(restHighLevelClient); -// when(restHighLevelClient.bulk(any(), any(RequestOptions.class))).thenReturn(bulkResponse); -// -// BulkItemResponse[] responses = new BulkItemResponse[]{prepareResponseFail(), prepareResponseSuccess()}; -// when(bulkResponse.getItems()).thenReturn(responses); -// Map<String, String> attr = new HashMap<>(); -// attr.put(DpsHeaders.ACCOUNT_ID, "slb"); -// recordChangedMessages = RecordChangedMessages.builder().attributes(attr).messageId("xxxx").publishTime("2000-01-02T10:10:44+0000").data("{}").build(); -// when(StorageAcl.flattenAcl(any())).thenReturn(null); -// } -// -// @Test -// public void should_returnNull_givenEmptyJobSubInfo_processRecordChangedMessageTest() throws Exception { -// JobStatus jobStatus = this.sut.processRecordChangedMessages(recordChangedMessages, new ArrayList<>()); -// -// assertNull(jobStatus); -// } -// -// @Test -// public void should_returnValidJobStatus_givenNullSchema_processRecordChangedMessageTest() { -// try { -// indexSchemaServiceMock(kind1, null); -// indexSchemaServiceMock(kind2, null); -// List<ConversionStatus> conversionStatus = new LinkedList<>(); -// List<Records.Entity> validRecords = new ArrayList<>(); -// Map<String, Object> storageData = new HashMap<>(); -// storageData.put("schema1", "test-value"); -// storageData.put("schema2", "test-value"); -// storageData.put("schema3", "test-value"); -// storageData.put("schema4", "test-value"); -// storageData.put("schema5", "test-value"); -// storageData.put("schema6", "test-value"); -// validRecords.add(Records.Entity.builder().id(recordId2).kind(kind2).data(storageData).build()); -// Records storageRecords = Records.builder().records(validRecords).conversionStatuses(conversionStatus).build(); -// -// when(storageService.getStorageRecords(any())).thenReturn(storageRecords); -// when(indicesService.createIndex(any(), any(), any(), any(), any())).thenReturn(true); -// -// JobStatus jobStatus = this.sut.processRecordChangedMessages(recordChangedMessages, recordInfos); -// -// assertEquals(2, jobStatus.getStatusesList().size()); -// assertEquals(1, jobStatus.getIdsByIndexingStatus(IndexingStatus.FAIL).size()); -// assertEquals(1, jobStatus.getIdsByIndexingStatus(IndexingStatus.WARN).size()); -// } catch (Exception e) { -// fail("Should not throw this exception" + e.getMessage()); -// } -// } -// -// @Test -// public void should_returnValidJobStatus_givenFailedUnitsConversion_processRecordChangedMessageTest() { -// try { -// indexSchemaServiceMock(kind1, null); -// indexSchemaServiceMock(kind2, null); -// List<ConversionStatus> conversionStatuses = new LinkedList<>(); -// List<String> status=new ArrayList<>(); -// status.add("crs bla bla"); -// ConversionStatus conversionStatus=ConversionStatus.builder().status("ERROR").errors(status).id(recordId2).build(); -// conversionStatuses.add(conversionStatus); -// List<Records.Entity> validRecords = new ArrayList<>(); -// Map<String, Object> storageData = new HashMap<>(); -// storageData.put("schema1", "test-value"); -// storageData.put("schema2", "test-value"); -// storageData.put("schema3", "test-value"); -// storageData.put("schema4", "test-value"); -// storageData.put("schema5", "test-value"); -// storageData.put("schema6", "test-value"); -// validRecords.add(Records.Entity.builder().id(recordId2).kind(kind2).data(storageData).build()); -// Records storageRecords = Records.builder().records(validRecords).conversionStatuses(conversionStatuses).build(); -// -// when(storageService.getStorageRecords(any())).thenReturn(storageRecords); -// when(indicesService.createIndex(any(), any(), any(), any(), any())).thenReturn(true); -// -// JobStatus jobStatus = this.sut.processRecordChangedMessages(recordChangedMessages, recordInfos); -// -// assertEquals(2, jobStatus.getStatusesList().size()); -// assertEquals(1, jobStatus.getIdsByIndexingStatus(IndexingStatus.FAIL).size()); -// assertEquals(1, jobStatus.getIdsByIndexingStatus(IndexingStatus.WARN).size()); -// assertTrue(jobStatus.getJobStatusByRecordId(jobStatus.getIdsByIndexingStatus(IndexingStatus.WARN).get(0)).getIndexProgress().getTrace().contains("crs bla bla")); -// } catch (Exception e) { -// fail("Should not throw this exception" + e.getMessage()); -// } -// } -// -// @Test -// public void should_returnValidJobStatus_givenNullSchemaForARecord_processRecordChangedMessageTest() { -// try { -// List<Records.Entity> validRecords = new ArrayList<>(); -// List<ConversionStatus> conversionStatus = new LinkedList<>(); -// Map<String, Object> storageData = new HashMap<>(); -// storageData.put("schema1", "test-value"); -// storageData.put("schema2", "test-value"); -// storageData.put("schema3", "test-value"); -// storageData.put("schema4", "test-value"); -// storageData.put("schema5", "test-value"); -// storageData.put("schema6", "test-value"); -// validRecords.add(Records.Entity.builder().id(recordId2).kind(kind2).data(storageData).build()); -// Records storageRecords = Records.builder().records(validRecords).conversionStatuses(conversionStatus).build(); -// when(storageService.getStorageRecords(any())).thenReturn(storageRecords); -// -// Map<String, String> schema = createSchema(); -// indexSchemaServiceMock(kind1, schema); -// indexSchemaServiceMock(kind2, null); -// when(elasticIndexNameResolver.getIndexNameFromKind(kind2)).thenReturn("tenant1-testindexer2-well-1.0.0"); -// when(indicesService.createIndex(any(), any(), any(), any(), any())).thenReturn(true); -// JobStatus jobStatus = sut.processRecordChangedMessages(recordChangedMessages, recordInfos); -// -// assertEquals(2, jobStatus.getStatusesList().size()); -// assertEquals(1, jobStatus.getIdsByIndexingStatus(IndexingStatus.FAIL).size()); -// assertEquals(1, jobStatus.getIdsByIndexingStatus(IndexingStatus.WARN).size()); -// assertEquals("Indexed Successfully", jobStatus.getStatusesList().get(1).getIndexProgress().getTrace().pop()); -// assertEquals("schema not found", jobStatus.getStatusesList().get(1).getIndexProgress().getTrace().pop()); -// } catch (Exception e) { -// fail("Should not throw this exception" + e.getMessage()); -// } -// } -// -// @Test -// public void should_returnValidJobStatus_givenValidCreateAndUpdateRecords_processRecordChangedMessagesTest() { -// try { -// Map<String, Object> storageData = new HashMap<>(); -// storageData.put("schema1", "test-value"); -// List<ConversionStatus> conversionStatus = new LinkedList<>(); -// List<Records.Entity> validRecords = new ArrayList<>(); -// validRecords.add(Records.Entity.builder().id(recordId2).kind(kind2).data(storageData).build()); -// Records storageRecords = Records.builder().records(validRecords).conversionStatuses(conversionStatus).build(); -// -// when(storageService.getStorageRecords(any())).thenReturn(storageRecords); -// when(indicesService.createIndex(any(), any(), any(), any(), any())).thenReturn(true); -// Map<String, String> schema = createSchema(); -// indexSchemaServiceMock(kind2, schema); -// indexSchemaServiceMock(kind1, null); -// JobStatus jobStatus = sut.processRecordChangedMessages(recordChangedMessages, recordInfos); -// -// assertEquals(2, jobStatus.getStatusesList().size()); -// assertEquals(1, jobStatus.getIdsByIndexingStatus(IndexingStatus.FAIL).size()); -// assertEquals(1, jobStatus.getIdsByIndexingStatus(IndexingStatus.SUCCESS).size()); -// } catch (Exception e) { -// fail("Should not throw this exception" + e.getMessage()); -// } -// } -// -// @Test -// public void should_properlyUpdateAuditLogs_givenValidCreateAndUpdateRecords() { -// try { -// Map<String, Object> storageData = new HashMap<>(); -// List<ConversionStatus> conversionStatus = new LinkedList<>(); -// -// storageData.put("schema1", "test-value"); -// List<Records.Entity> validRecords = new ArrayList<>(); -// validRecords.add(Records.Entity.builder().id(recordId2).kind(kind2).data(storageData).build()); -// Records storageRecords = Records.builder().records(validRecords).conversionStatuses(conversionStatus).build(); -// -// when(this.storageService.getStorageRecords(any())).thenReturn(storageRecords); -// when(this.indicesService.createIndex(any(), any(), any(), any(), any())).thenReturn(true); -// Map<String, String> schema = createSchema(); -// indexSchemaServiceMock(kind2, schema); -// indexSchemaServiceMock(kind1, null); -// JobStatus jobStatus = this.sut.processRecordChangedMessages(recordChangedMessages, recordInfos); -// -// assertEquals(2, jobStatus.getStatusesList().size()); -// assertEquals(1, jobStatus.getIdsByIndexingStatus(IndexingStatus.FAIL).size()); -// assertEquals(1, jobStatus.getIdsByIndexingStatus(IndexingStatus.SUCCESS).size()); -// -// verify(this.auditLogger).indexCreateRecordSuccess(singletonList("RecordStatus(id=tenant1:doc:test2, kind=tenant1:testindexer2:well:1.0.0, operationType=create, status=SUCCESS)")); -// verify(this.auditLogger).indexUpdateRecordFail(singletonList("RecordStatus(id=tenant1:doc:test1, kind=tenant1:testindexer1:well:1.0.0, operationType=update, status=FAIL)")); -// } catch (Exception e) { -// fail("Should not throw this exception" + e.getMessage()); -// } -// } -// -// private BulkItemResponse prepareResponseFail() { -// BulkItemResponse responseFail = mock(BulkItemResponse.class); -// when(responseFail.isFailed()).thenReturn(true); -// when(responseFail.getFailureMessage()).thenReturn(failureMassage); -// when(responseFail.getId()).thenReturn(recordId1); -// when(responseFail.getFailure()).thenReturn(new BulkItemResponse.Failure("failure index", "failure type", "failure id", new Exception("test failure"))); -// return responseFail; -// } -// -// private BulkItemResponse prepareResponseSuccess() { -// BulkItemResponse responseSuccess = mock(BulkItemResponse.class); -// when(responseSuccess.getId()).thenReturn(recordId2); -// return responseSuccess; -// } -// -// private void indexSchemaServiceMock(String kind, Map<String, String> schema) { -// if (schema == null) { -// IndexSchema indexSchema = IndexSchema.builder().kind(kind).dataSchema(null).build(); -// when(indexSchemaService.getIndexerInputSchema(kind)).thenReturn(indexSchema); -// } else { -// IndexSchema indexSchema = IndexSchema.builder().kind(kind).dataSchema(schema).build(); -// when(indexSchemaService.getIndexerInputSchema(kind)).thenReturn(indexSchema); -// } -// } -// -// private Map<String, String> createSchema() { -// Map<String, String> schema = new HashMap<>(); -// schema.put("schema1", "keyword"); -// schema.put("schema2", "boolean"); -// schema.put("schema3", "date"); -// schema.put("schema6", "object"); -// return schema; -// } -//} diff --git a/provider/indexer-reference/src/test/java/org/opengroup/osdu/indexer/service/ReindexServiceTest.java b/provider/indexer-reference/src/test/java/org/opengroup/osdu/indexer/service/ReindexServiceTest.java index 502699c25..8cc2f75c8 100644 --- a/provider/indexer-reference/src/test/java/org/opengroup/osdu/indexer/service/ReindexServiceTest.java +++ b/provider/indexer-reference/src/test/java/org/opengroup/osdu/indexer/service/ReindexServiceTest.java @@ -29,9 +29,8 @@ import org.opengroup.osdu.core.common.model.http.DpsHeaders; import org.opengroup.osdu.core.common.model.indexer.RecordQueryResponse; import org.opengroup.osdu.core.common.model.indexer.RecordReindexRequest; import org.opengroup.osdu.core.common.provider.interfaces.IRequestInfo; -import org.opengroup.osdu.core.common.search.Config; +import org.opengroup.osdu.indexer.config.IndexerConfigurationProperties; import org.opengroup.osdu.indexer.util.IndexerQueueTaskBuilder; -import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.modules.junit4.PowerMockRunnerDelegate; import org.springframework.test.context.junit4.SpringRunner; @@ -46,13 +45,14 @@ import static org.powermock.api.mockito.PowerMockito.when; @RunWith(PowerMockRunner.class) @PowerMockRunnerDelegate(SpringRunner.class) -@PrepareForTest({Config.class}) public class ReindexServiceTest { private final String cursor = "100"; private final String correlationId = UUID.randomUUID().toString(); + @Mock + private IndexerConfigurationProperties configurationProperties; @Mock private StorageService storageService; @Mock @@ -123,8 +123,7 @@ public class ReindexServiceTest { results.add("test1"); recordQueryResponse.setResults(results); - mockStatic(Config.class); - when(Config.getStorageRecordsBatchSize()).thenReturn(1); + when(configurationProperties.getStorageRecordsBatchSize()).thenReturn(1); when(storageService.getRecordsByKind(any())).thenReturn(recordQueryResponse); diff --git a/provider/indexer-reference/src/test/java/org/opengroup/osdu/indexer/service/StorageServiceTest.java b/provider/indexer-reference/src/test/java/org/opengroup/osdu/indexer/service/StorageServiceTest.java index e8388a141..b4ea76b8e 100644 --- a/provider/indexer-reference/src/test/java/org/opengroup/osdu/indexer/service/StorageServiceTest.java +++ b/provider/indexer-reference/src/test/java/org/opengroup/osdu/indexer/service/StorageServiceTest.java @@ -39,9 +39,9 @@ import org.opengroup.osdu.core.common.http.IUrlFetchService; import org.opengroup.osdu.core.common.model.indexer.RecordQueryResponse; import org.opengroup.osdu.core.common.model.indexer.RecordReindexRequest; import org.opengroup.osdu.core.common.model.indexer.Records; +import org.opengroup.osdu.indexer.config.IndexerConfigurationProperties; import org.springframework.http.HttpStatus; import org.springframework.test.context.junit4.SpringRunner; -import org.springframework.test.util.ReflectionTestUtils; import java.lang.reflect.Type; import java.net.URISyntaxException; @@ -67,6 +67,8 @@ public class StorageServiceTest { private JaxRsDpsLog log; @Mock private IRequestInfo requestInfo; + @Mock + private IndexerConfigurationProperties configurationProperties; @InjectMocks private StorageServiceImpl sut; @@ -89,7 +91,7 @@ public class StorageServiceTest { jobStatus.initialize(msgs); ids = Arrays.asList(RECORD_ID1, RECORDS_ID2); - ReflectionTestUtils.setField(this.sut, "STORAGE_RECORDS_BATCH_SIZE", "20"); + when(configurationProperties.getStorageRecordsBatchSize()).thenReturn(20); } @Test diff --git a/provider/indexer-reference/src/test/java/org/opengroup/osdu/indexer/util/ServiceAccountJwtGcpClientImplTest.java b/provider/indexer-reference/src/test/java/org/opengroup/osdu/indexer/util/ServiceAccountJwtGcpClientImplTest.java index 58dbef93e..2c681985f 100644 --- a/provider/indexer-reference/src/test/java/org/opengroup/osdu/indexer/util/ServiceAccountJwtGcpClientImplTest.java +++ b/provider/indexer-reference/src/test/java/org/opengroup/osdu/indexer/util/ServiceAccountJwtGcpClientImplTest.java @@ -41,7 +41,7 @@ import org.opengroup.osdu.core.common.logging.JaxRsDpsLog; import org.opengroup.osdu.core.common.model.search.DeploymentEnvironment; import org.opengroup.osdu.core.common.model.search.IdToken; import org.opengroup.osdu.core.common.provider.interfaces.IJwtCache; -import org.opengroup.osdu.core.common.search.Config; +import org.opengroup.osdu.indexer.config.IndexerConfigurationProperties; import org.powermock.core.classloader.annotations.PrepareForTest; import org.springframework.test.context.junit4.SpringRunner; @@ -52,11 +52,13 @@ import static org.powermock.api.mockito.PowerMockito.when; @Ignore @RunWith(SpringRunner.class) -@PrepareForTest({GoogleNetHttpTransport.class, GoogleCredential.class, NetHttpTransport.class, SignJwtResponse.class, Iam.Builder.class, HttpClients.class, EntityUtils.class, Config.class}) +@PrepareForTest({GoogleNetHttpTransport.class, GoogleCredential.class, NetHttpTransport.class, SignJwtResponse.class, Iam.Builder.class, HttpClients.class, EntityUtils.class, IndexerConfigurationProperties.class}) public class ServiceAccountJwtGcpClientImplTest { private static final String JWT_TOKEN = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6Ik1UVXlPREE0TXpFd09BPT0ifQ.eyJzdWIiOiJtemh1OUBzbGIuY29tIiwiaXNzIjoic2F1dGgtcHJldmlldy5zbGIuY29tIiwiYXVkIjoidGVzdC1zbGJkZXYtZGV2cG9ydGFsLnNsYmFwcC5jb20iLCJpYXQiOjE1MjgxNDg5MTUsImV4cCI6MTUyODIzNTMxNSwicHJvdmlkZXIiOiJzbGIuY29tIiwiY2xpZW50IjoidGVzdC1zbGJkZXYtZGV2cG9ydGFsLnNsYmFwcC5jb20iLCJ1c2VyaWQiOiJtemh1OUBzbGIuY29tIiwiZW1haWwiOiJtemh1OUBzbGIuY29tIiwiYXV0aHoiOiJ7XCJhY2NvdW50Q291bnRyeVwiOntcImNvZGVcIjpcInVzXCIsXCJpZFwiOjU3MTU5OTkxMDE4MTI3MzYsXCJuYW1lXCI6XCJVbml0ZWQgU3RhdGVzIG9mIEFtZXJpY2FcIn0sXCJhY2NvdW50SWRcIjo1NjkxODc4ODMzOTEzODU2LFwiYWNjb3VudE5hbWVcIjpcIlNJUyBJbnRlcm5hbCBIUVwiLFwiY3JlYXRlZFwiOlwiMjAxOC0wNS0wM1QxNzoyNTo1NS40NDNaXCIsXCJkZXBhcnRtZW50TWFuYWdlclwiOm51bGwsXCJzdWJzY3JpcHRpb25zXCI6W3tcImFjY291bnRJZFwiOjU2OTE4Nzg4MzM5MTM4NTYsXCJjb250cmFjdElkXCI6NTc1MTcwMDIxMjE1NDM2OCxcImNyZWF0ZWRcIjpcIjIwMTgtMDUtMDNUMTc6MzM6MDkuNTczWlwiLFwiY3JtQ29udHJhY3RJZFwiOlwiU0lTLUlOVEVSTkFMLUhRLVFBXCIsXCJjcm1Db250cmFjdEl0ZW1JZFwiOlwiZGV2bGlcIixcImV4cGlyYXRpb25cIjpcIjE5NzAtMDEtMDFUMDA6MDA6MDAuMDAwWlwiLFwiaWRcIjo1MDc5Mjg4NTA0MTIzMzkyLFwicHJvZHVjdFwiOntcImNvZGVcIjpcImRldmVsb3Blci1saWdodFwiLFwiY29tY2F0TmFtZVwiOlwiTm90IGluIENvbUNhdFwiLFwiZmVhdHVyZVNldHNcIjpbe1wiYXBwbGljYXRpb25cIjp7XCJjb2RlXCI6XCJhcGlkZXZlbG9wZXJwb3J0YWxcIixcImlkXCI6NTE2ODkzMDY5NTkzODA0OCxcIm5hbWVcIjpcIkFQSSBEZXZlbG9wZXIgUG9ydGFsXCIsXCJ0eXBlXCI6XCJXZWJBcHBcIn0sXCJjbGFpbXNcIjpudWxsLFwiaWRcIjo1MTkxNTcyMjg3MTI3NTUyLFwibmFtZVwiOlwiRGV2ZWxvcGVyXCIsXCJ0eXBlXCI6XCJCQVNFXCJ9XSxcImlkXCI6NTE1MDczMDE1MTI2NDI1NixcIm5hbWVcIjpcIkRldmVsb3BlciBQb3J0YWxcIixcInBhcnROdW1iZXJcIjpcIlNERUwtUEItU1VCVVwifX1dLFwidXNlckVtYWlsXCI6XCJtemh1OUBzbGIuY29tXCIsXCJ1c2VyTmFtZVwiOlwiTWluZ3lhbmcgWmh1XCJ9XG4iLCJsYXN0bmFtZSI6IlpodSIsImZpcnN0bmFtZSI6Ik1pbmd5YW5nIiwiY291bnRyeSI6IiIsImNvbXBhbnkiOiIiLCJqb2J0aXRsZSI6IiIsInN1YmlkIjoiNDE3YjczMjktYmMwNy00OTFmLWJiYzQtZTQ1YjRhMWFiYjVjLVd3U0c0dyIsImlkcCI6ImNvcnAyIiwiaGQiOiJzbGIuY29tIn0.WQfGr1Xu-6IdaXdoJ9Fwzx8O2el1UkFPWo1vk_ujiAfdOjAR46UG5SrBC7mzC7gYRyK3a4fimBmbv3uRVJjTNXdxXRLZDw0SvXUMIOqjUGLom491ESbrtka_Xz7vGO-tWyDcEQDTfFzQ91LaVN7XdzL18_EDTXZoPhKb-zquyk9WLQxP9Mw-3Yh-UrbvC9nl1-GRn1IVbzp568kqkpOVUFM9alYSGw-oMGDZNt1DIYOJnpGaw2RB5B3AKvNivZH_Xdac7ZTzQbsDOt8B8DL2BphuxcJ9jshCJkM2SHQ15uErv8sfnzMwdF08e_0QcC_30I8eX9l8yOu6TnwwqlXunw"; + @Mock + private IndexerConfigurationProperties indexerConfigurationProperties; @Mock private JaxRsDpsLog log; @Mock @@ -101,8 +103,8 @@ public class ServiceAccountJwtGcpClientImplTest { when(credential.createScoped(any())).thenReturn(credential); when(HttpClients.createDefault()).thenReturn(httpClient); when(httpClient.execute(any())).thenReturn(httpResponse); - when(Config.getDeploymentEnvironment()).thenReturn(DeploymentEnvironment.LOCAL); - when(Config.getGoogleAudiences()).thenReturn("aud"); + when(indexerConfigurationProperties.getDeploymentEnvironment()).thenReturn(DeploymentEnvironment.LOCAL); + when(indexerConfigurationProperties.getGoogleAudiences()).thenReturn("aud"); // when(this.tenantInfoServiceProvider).thenReturn(this.tenantInfoService); -- GitLab