Skip to content
Snippets Groups Projects
Commit e13ad39b authored by helayoty's avatar helayoty
Browse files

Move Services test cases to vendor projects

parent ec2e34e5
No related branches found
No related tags found
1 merge request!6Trusted ibm
Showing
with 2034 additions and 129 deletions
......@@ -101,6 +101,69 @@
<artifactId>msal4j</artifactId>
<version>0.5.0-preview</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.6.2</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>6.6.2</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.6.2</version>
</dependency>
<!-- Test Dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.powermock/powermock-api-mockito2 -->
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>2.0.2</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.powermock/powermock-module-junit4 -->
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.7</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
......
// Copyright © Microsoft Corporation
//
// 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.azure.persistence;
//
//import org.junit.Before;
//import org.junit.Test;
//import org.junit.runner.RunWith;
//import org.mockito.InjectMocks;
//import org.mockito.Mock;
//import org.opendes.indexer.azure.model.ElasticSettingSchema;
//import org.opengroup.osdu.client.api.DpsHeaders;
//import org.opengroup.osdu.client.multitenancy.ITenantFactory;
//import org.opengroup.osdu.client.multitenancy.TenantInfo;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.test.context.junit4.SpringRunner;
//
//import static org.mockito.Matchers.any;
//import static org.mockito.Mockito.verify;
//import static org.mockito.Mockito.when;
//
//@RunWith(SpringRunner.class)
//public class ElasticSettingSchemaRepositoryImplTest {
//
// private static final String TENANT_NAME = "common";
//
// @Mock
// private ITenantFactory tenantFactory;
//
// @Mock
// private TenantInfo tenant;
//
// @Mock
// private DpsHeaders headers;
//
// @InjectMocks
// private ElasticSettingSchemaRepositoryImpl sut;
//
// @Mock
// private CosmosDBElasticSettings cosmosDB;
//
// @Value("${ELASTIC_DATASTORE_KIND}")
// private String ELASTIC_DATASTORE_KIND;
//
// @Value("${ELASTIC_DATASTORE_ID}")
// private String ELASTIC_DATASTORE_ID;
//
//
// @Before
// public void setup() {
// when(this.headers.getPartitionIdWithFallbackToAccountId()).thenReturn(TENANT_NAME);
// when(this.tenant.getName()).thenReturn(TENANT_NAME);
// when(this.tenantFactory.exists(TENANT_NAME)).thenReturn(true);
// when(this.tenantFactory.getTenantInfo(TENANT_NAME)).thenReturn(this.tenant);
// }
//
// @Test
// public void should_getSearchSettingsSuccessfully_when_add() {
//
// ElasticSettingSchema schema = new ElasticSettingSchema();
// schema.setHost("HOST");
// schema.setPort("PORT");
// schema.setUsernameAndPassword("USERNAME_PASSWORD");
//
// String settingId = tenant.getName().concat("_").concat(ELASTIC_DATASTORE_ID);
//
// this.sut.add(schema,settingId);
//
// verify(this.sut.get(settingId).equals(schema));
// }
//}
// 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.azure.service;
import com.google.common.collect.Lists;
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.opengroup.osdu.core.api.DpsHeaders;
import org.opengroup.osdu.indexer.service.CronServiceImpl;
import org.opengroup.osdu.indexer.util.IRequestInfo;
import org.opengroup.osdu.is.core.logging.JaxRsDpsLog;
import org.opengroup.osdu.is.core.model.IndexInfo;
import org.opengroup.osdu.is.core.service.IndicesService;
import org.opengroup.osdu.is.core.util.Config;
import org.opengroup.osdu.is.core.util.ElasticClientHandler;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.IOException;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
@Ignore
@RunWith(SpringRunner.class)
@PrepareForTest({RestHighLevelClient.class})
public class CronServiceImplTest {
@Mock
private RestHighLevelClient restHighLevelClient;
@Mock
private IndicesService indicesService;
@Mock
private ElasticClientHandler elasticClientHandler;
@Mock
private IRequestInfo requestInfo;
@Mock
private JaxRsDpsLog log;
@InjectMocks
private CronServiceImpl sut;
@InjectMocks
private DpsHeaders dpsHeaders;
@Before
public void setup() {
mockStatic(Config.class);
when(this.requestInfo.getHeaders()).thenReturn(dpsHeaders);
when(Config.getIndexCleanupThresholdDays()).thenReturn(3);
when(Config.getEmptyIndexCleanupThresholdDays()).thenReturn(7);
}
@Test
public void run_cleanup_when_cron_job_runs_with_correct_pattern() throws Exception {
final String indexPattern = "tenant1-index-*";
IndexInfo info = IndexInfo.builder().name("tenant1-index-1.0.0").documentCount("10").creationDate(Long.toString(Instant.now().minus(4, ChronoUnit.DAYS).toEpochMilli())).build();
when(this.requestInfo.getPartitionId()).thenReturn("tenant1");
when(this.elasticClientHandler.createRestClient()).thenReturn(this.restHighLevelClient);
when(this.indicesService.getIndexInfo(this.restHighLevelClient, indexPattern)).thenReturn(Lists.newArrayList(info));
this.sut.cleanupIndices(indexPattern);
verify(this.indicesService, times(1)).deleteIndex(restHighLevelClient, "tenant1-index-1.0.0");
verify(this.indicesService, times(1)).getIndexInfo(restHighLevelClient, indexPattern);
}
@Test(expected = IOException.class)
public void run_cleanup_when_cron_job_runs_with_wrong_pattern() throws Exception {
IOException exception = new IOException("blah");
when(this.elasticClientHandler.createRestClient()).thenReturn(this.restHighLevelClient);
when(this.indicesService.getIndexInfo(this.restHighLevelClient, "tenant1-test-*")).thenThrow(exception);
this.sut.cleanupIndices("tenant1-test-*");
verify(this.indicesService, times(0)).deleteIndex(any(), any());
}
@Test
public void run_cleanup_when_backend_does_not_have_empty_stale_indices() throws Exception {
IndexInfo info = IndexInfo.builder().name("tenant1-index-1.0.0").documentCount("10").creationDate(Long.toString(Instant.now().minus(8, ChronoUnit.DAYS).toEpochMilli())).build();
when(this.requestInfo.getPartitionId()).thenReturn("tenant1");
when(this.elasticClientHandler.createRestClient()).thenReturn(this.restHighLevelClient);
when(this.indicesService.getIndexInfo(this.restHighLevelClient, null)).thenReturn(Lists.newArrayList(info));
this.sut.cleanupEmptyStaleIndices();
verify(this.indicesService, times(0)).deleteIndex(restHighLevelClient, null);
verify(this.indicesService, times(1)).getIndexInfo(restHighLevelClient, null);
}
@Test
public void run_cleanup_when_backend_have_empty_stale_indices() throws Exception {
IndexInfo info = IndexInfo.builder().name("tenant1-index-1.0.0").documentCount("0").creationDate(Long.toString(Instant.now().minus(8, ChronoUnit.DAYS).toEpochMilli())).build();
when(this.requestInfo.getPartitionId()).thenReturn("tenant1");
when(this.elasticClientHandler.createRestClient()).thenReturn(this.restHighLevelClient);
when(this.indicesService.getIndexInfo(this.restHighLevelClient, null)).thenReturn(Lists.newArrayList(info));
this.sut.cleanupEmptyStaleIndices();
verify(this.indicesService, times(1)).deleteIndex(restHighLevelClient, "tenant1-index-1.0.0");
verify(this.indicesService, times(1)).getIndexInfo(restHighLevelClient, null);
}
@Test(expected = IOException.class)
public void run_cleanup_when_backend_throws_exception() throws Exception {
IOException exception = new IOException("blah");
when(this.elasticClientHandler.createRestClient()).thenReturn(this.restHighLevelClient);
when(this.indicesService.getIndexInfo(this.restHighLevelClient, null)).thenThrow(exception);
this.sut.cleanupEmptyStaleIndices();
verify(this.indicesService, times(0)).deleteIndex(any(), any());
}
}
\ No newline at end of file
// 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.azure.service;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.opengroup.osdu.core.api.DpsHeaders;
import org.opengroup.osdu.core.multitenancy.TenantInfo;
import org.opengroup.osdu.indexer.service.ElasticSettingServiceImpl;
import org.opengroup.osdu.is.core.cache.ElasticCredentialsCache;
import org.opengroup.osdu.is.core.logging.JaxRsDpsLog;
import org.opengroup.osdu.is.core.model.ClusterSettings;
import org.opengroup.osdu.is.core.persistence.ElasticRepository;
import org.opengroup.osdu.is.core.service.TenantInfoService;
import org.opengroup.osdu.is.core.util.AppException;
import org.opengroup.osdu.is.core.util.IHeadersInfo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.util.ReflectionTestUtils;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.when;
@RunWith(SpringRunner.class)
public class ElasticSettingServiceTest {
@Mock
private TenantInfoService tenantInfoService;
@Mock
private ElasticRepository elasticRepository;
@Mock
private ElasticCredentialsCache elasticCredentialCache;
@Mock
private TenantInfo tenantInfo;
@InjectMocks
private ElasticSettingServiceImpl sut;
@Mock
private ClusterSettings clusterSettings;
@Mock
private IHeadersInfo headersInfo;
@Mock
private JaxRsDpsLog log;
public String GAE_SERVICE = "indexer";
private final String host = "db5c51c1.us-central1.gcp.cloud.es.io";
private final int port = 9243;
private final String credentials = "name:password";
String cacheKey = "";
@Before
public void setup() {
when(tenantInfo.getName()).thenReturn("tenant1");
when(this.headersInfo.getPartitionId()).thenReturn("tenant1");
when(this.tenantInfoService.getTenantInfo()).thenReturn(tenantInfo);
ReflectionTestUtils.setField( sut, "GAE_SERVICE", "indexer");
clusterSettings = ClusterSettings.builder().host(host).port(port).userNameAndPassword(credentials).build();
cacheKey = String.format("%s-%s", GAE_SERVICE, tenantInfo.getName());
}
@Test
public void should_getValid_clusterSettings_fromCache() {
when(this.elasticCredentialCache.get(cacheKey)).thenReturn(clusterSettings);
ClusterSettings response = this.sut.getElasticClusterInformation();
assertNotNull(response);
assertEquals(response.getHost(), host);
assertEquals(response.getPort(), port);
assertEquals(response.getUserNameAndPassword(), credentials);
}
@Test
public void should_getValid_clusterSettings_fromCosmosDB() {
when(this.elasticCredentialCache.get(cacheKey)).thenReturn(clusterSettings);
when(this.elasticRepository.getElasticClusterSettings(tenantInfo)).thenReturn(clusterSettings);
ClusterSettings response = this.sut.getElasticClusterInformation();
assertNotNull(response);
assertEquals(response.getHost(), host);
assertEquals(response.getPort(), port);
assertEquals(response.getUserNameAndPassword(), credentials);
}
@Test(expected = AppException.class)
public void should_throwAppException_when_tenantClusterInfo_not_found() throws AppException {
when(this.elasticRepository.getElasticClusterSettings(tenantInfo)).thenReturn(null);
this.sut.getElasticClusterInformation();
}
}
// 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.azure.service;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.http.HttpEntity;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
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.ArgumentMatchers;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.opengroup.osdu.core.api.DpsHeaders;
import org.opengroup.osdu.indexer.logging.AuditLogger;
import org.opengroup.osdu.indexer.service.IndexCopyServiceImpl;
import org.opengroup.osdu.indexer.service.IndexerMappingService;
import org.opengroup.osdu.indexer.util.IRequestInfo;
import org.opengroup.osdu.is.core.model.ClusterSettings;
import org.opengroup.osdu.is.core.service.ElasticSettingService;
import org.opengroup.osdu.is.core.service.IndicesService;
import org.opengroup.osdu.is.core.util.AppException;
import org.opengroup.osdu.is.core.util.ElasticClientHandler;
import org.opengroup.osdu.is.core.util.ElasticIndexNameResolver;
import org.opengroup.osdu.is.core.util.IHeadersInfo;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.powermock.api.mockito.PowerMockito.when;
@RunWith(SpringRunner.class)
@PrepareForTest({RestHighLevelClient.class, Response.class, RestClient.class, HttpEntity.class, EntityUtils.class})
public class IndexCopyServiceImplTest {
private final String correlationId = UUID.randomUUID().toString();
@Mock
private HttpEntity httpEntity;
@Mock
private HttpEntity httpEntityRequest;
@Mock
private IRequestInfo requestInfo;
@Mock
private IHeadersInfo headersInfo;
@Mock
private RestClient restClient;
@Mock
private RestHighLevelClient restHighLevelClient;
@Mock
private IndicesService indicesService;
@Mock
private IndexerMappingService mappingService;
@Mock
private ElasticClientHandler elasticClientHandler;
@Mock
private ElasticIndexNameResolver elasticIndexNameResolver;
@Mock
private Response response;
@Mock
private ElasticSettingService elasticSettingService;
@Mock
private AuditLogger auditLogger;
@Mock
private Map<String, String> httpHeaders;
@InjectMocks
private IndexCopyServiceImpl sut;
private ClusterSettings commonCluster;
private Map<String, Object> correctMap;
@Before
public void setup() {
commonCluster = ClusterSettings.builder().host("commonhost").port(8080).userNameAndPassword("username:pwd").build();
httpHeaders = new HashMap<>();
httpHeaders.put(DpsHeaders.AUTHORIZATION, "testAuth");
httpHeaders.put(DpsHeaders.CORRELATION_ID, correlationId);
DpsHeaders standardHeaders = DpsHeaders.createFromMap(httpHeaders);
when(headersInfo.getHeaders()).thenReturn(standardHeaders);
when(requestInfo.getHeadersMapWithDwdAuthZ()).thenReturn(httpHeaders);
when(response.getEntity()).thenReturn(httpEntity);
Type mapType = new TypeToken<Map<String, Object>>() {}.getType();
String afterFormat = "{\"properties\":{\"id\":{\"type\":\"keyword\"}}}";
correctMap = new Gson().fromJson(afterFormat, mapType);
restHighLevelClient = mock(RestHighLevelClient.class);
}
@Test(expected = IOException.class)
public void should_throwIOException_when_indexMappingNotFound() throws Exception {
IOException exception = new IOException("Fail to get mapping for the given index from common cluster.");
when(this.mappingService.getIndexMapping(ArgumentMatchers.any(), ArgumentMatchers.any())).thenThrow(exception);
this.sut.copyIndex("common:metadata:entity:1.0.0");
}
@Test(expected = IllegalArgumentException.class)
public void should_throwIllegalArgExceptionCopyIndexRequest_copyIndexTest() {
try {
this.sut.copyIndex(null);
} catch (IOException e) {
fail("Should not throw IOException but illegalArgumentException.");
}
}
@Test
public void should_returnIndexMapping_getIndexMappingFromCommonClustertest() {
String mappingJson = "{\"common-metadata-entity-1.0.0\":{\"mappings\":{\"entity\":{\"properties\":{\"id\":{\"type\":\"keyword\"}}}}}}";
when(elasticClientHandler.createRestClient()).thenReturn(restHighLevelClient);
try {
when(this.mappingService.getIndexMapping(ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(mappingJson);
Map<String, Object> resultMap = this.sut.getIndexMappingsFromCommonCluster("test", "test");
Assert.assertEquals(resultMap, correctMap);
} catch (Exception ignored) {
}
}
@Test
public void should_returnClusterInfo_getCommonClusterInformationtest() {
try {
String[] correctCommonCluster = {"https://commonhost:8080", "username", "pwd"};
when(elasticClientHandler.createRestClient()).thenReturn(restHighLevelClient);
when(elasticSettingService.getElasticClusterInformation()).thenReturn(commonCluster);
String[] resultCommonCluster = this.sut.getCommonClusterInformation();
Assert.assertEquals(correctCommonCluster[0], resultCommonCluster[0]);
Assert.assertEquals(correctCommonCluster[1], resultCommonCluster[1]);
Assert.assertEquals(correctCommonCluster[2], resultCommonCluster[2]);
} catch (IOException ignored) {
fail("Should not throw this exception " + ignored.getMessage());
}
}
@Test(expected = AppException.class)
public void should_throwException_failToCreateIndexInTenantCluster_createIndexInTenantClustertest() {
try {
when(elasticClientHandler.createRestClient()).thenReturn(restHighLevelClient);
when(indicesService.createIndex(ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(false);
this.sut.createIndexInTenantCluster("test", "test", "test", correctMap);
} catch (IOException ignored) {
fail("Should not throw this exception " + ignored.getMessage());
}
}
@Ignore
public void should_returnTaskIdResponse_reindexRequestSucceed_reindexInTenantClustertest() {
//TODO: fix the null Response from restHighLevelClient.getLowLevelClient().performRequest().
try {
String[] correctCommonCluster = {"https://commonhost:8080", "username", "pwd"};
Request request = new Request("POST", "/_reindex?wait_for_completion=false");
request.setEntity(httpEntityRequest);
when(elasticClientHandler.createRestClient()).thenReturn(restHighLevelClient);
when(indicesService.createIndex(ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(false);
when(restHighLevelClient.getLowLevelClient()).thenReturn(restClient);
when(restClient.performRequest(request)).thenReturn(response);
when(response.getEntity()).thenReturn(httpEntity);
Assert.assertEquals(httpEntity, this.sut.reindexInTenantCluster("test", "test", correctCommonCluster));
} catch (IOException ignored) {
}
}
}
// 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.azure.service;
import org.apache.http.StatusLine;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse;
import org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse.FieldMappingMetaData;
import org.elasticsearch.action.bulk.BulkItemResponse.Failure;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.*;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.reindex.BulkByScrollResponse;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentMatchers;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.opengroup.osdu.indexer.model.IndexSchema;
import org.opengroup.osdu.indexer.service.IndexerMappingServiceImpl;
import org.opengroup.osdu.is.core.model.RecordMetaAttribute;
import org.opengroup.osdu.is.core.util.AppException;
import org.opengroup.osdu.is.core.util.ElasticClientHandler;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.IOException;
import java.util.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.powermock.api.mockito.PowerMockito.when;
@Ignore
@RunWith(SpringRunner.class)
@PrepareForTest({ RestHighLevelClient.class, IndicesClient.class })
public class IndexerMappingServiceTest {
private final String kind = "tenant:test:test:1.0.0";
private final String index = "tenant-test-test-1.0.0";
private final String type = "test";
private final String mappingValid = "{\"dynamic\":false,\"properties\":{\"data\":{\"properties\":{\"Location\":{\"type\":\"geo_point\"}}},\"id\":{\"type\":\"keyword\"}}}";
@Mock
private RestClient restClient;
@Mock
private Response response;
@Mock
private StatusLine statusLine;
@InjectMocks
private IndexerMappingServiceImpl sut;
@Mock
private ElasticClientHandler elasticClientHandler;
@InjectMocks
private RestHighLevelClient restHighLevelClient;
@InjectMocks
private IndexSchema indexSchema;
@InjectMocks
private IndicesClient indicesClient;
@InjectMocks
private AcknowledgedResponse mappingResponse;
@Before
public void setup() throws IOException {
Map<String, String> dataMapping = new HashMap<>();
dataMapping.put("Location", "geo_point");
Map<String, Object> metaMapping = new HashMap<>();
metaMapping.put(RecordMetaAttribute.ID.getValue(), "keyword");
this.indexSchema = IndexSchema.builder().kind(kind).type(type).dataSchema(dataMapping).metaSchema(metaMapping)
.build();
this.indicesClient = PowerMockito.mock(IndicesClient.class);
this.restHighLevelClient = PowerMockito.mock(RestHighLevelClient.class);
when(this.restHighLevelClient.getLowLevelClient()).thenReturn(restClient);
when(this.restClient.performRequest(ArgumentMatchers.any())).thenReturn(response);
when(this.response.getStatusLine()).thenReturn(statusLine);
when(this.statusLine.getStatusCode()).thenReturn(200);
}
@Test
public void should_returnValidMapping_givenFalseMerge_createMappingTest() {
try {
String mapping = this.sut.createMapping(restHighLevelClient, indexSchema, index, false);
assertEquals(mappingValid, mapping);
} catch (Exception e) {
fail("Should not throw this exception" + e.getMessage());
}
}
@Test
public void should_returnValidMapping_givenTrueMerge_createMappingTest() {
try {
doReturn(this.indicesClient).when(this.restHighLevelClient).indices();
doReturn(mappingResponse).when(this.indicesClient).putMapping(ArgumentMatchers.any(), ArgumentMatchers.any(RequestOptions.class));
String mapping = this.sut.createMapping(this.restHighLevelClient, this.indexSchema, this.index, true);
assertEquals(this.mappingValid, mapping);
} catch (Exception e) {
fail("Should not throw this exception" + e.getMessage());
}
}
@Test
public void should_returnValidMapping_givenExistType_createMappingTest() {
try {
doReturn(this.indicesClient).when(this.restHighLevelClient).indices();
doReturn(mappingResponse).when(this.indicesClient).putMapping(ArgumentMatchers.any(), ArgumentMatchers.any(RequestOptions.class));
IndexerMappingServiceImpl indexerMappingServiceLocal = PowerMockito.spy(new IndexerMappingServiceImpl());
doReturn(false).when(indexerMappingServiceLocal).isTypeExist(ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any());
String mapping = this.sut.createMapping(this.restHighLevelClient, this.indexSchema, this.index, true);
assertEquals(this.mappingValid, mapping);
} catch (Exception e) {
fail("Should not throw this exception" + e.getMessage());
}
}
@Test
public void should_update_indices_field_with_keyword_when_valid_indices() throws Exception {
try {
Set<String> indices = new HashSet<String>();
indices.add("indices 1");
GetFieldMappingsResponse getFieldMappingsResponse = mock(GetFieldMappingsResponse.class);
doReturn(this.indicesClient).when(this.restHighLevelClient).indices();
when(this.indicesClient.getFieldMapping(ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(getFieldMappingsResponse);
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
builder.field("any field", new HashMap());
builder.endObject();
BytesReference bytesReference = BytesReference.bytes(builder);
FieldMappingMetaData mappingMetaData = new FieldMappingMetaData(index, bytesReference);
Map<String, FieldMappingMetaData> mapBuilder = new HashMap<>();
mapBuilder.put("data.any field", mappingMetaData);
Map<String, Map<String, FieldMappingMetaData>> mappingBuilder = new HashMap<>();
mappingBuilder.put("any index 1", mapBuilder);
mappingBuilder.put("any index 2", mapBuilder);
Map<String, Map<String, Map<String, FieldMappingMetaData>>> mapping = new HashMap<>();
mapping.put("indices 1", mappingBuilder);
when(getFieldMappingsResponse.mappings()).thenReturn(mapping);
doReturn(mappingResponse).when(this.indicesClient).putMapping(ArgumentMatchers.any(), ArgumentMatchers.any(RequestOptions.class));
BulkByScrollResponse response = mock(BulkByScrollResponse.class);
doReturn(response).when(this.restHighLevelClient).updateByQuery(ArgumentMatchers.any(), ArgumentMatchers.any(RequestOptions.class));
when(response.getBulkFailures()).thenReturn(new ArrayList<Failure>());
when(elasticClientHandler.createRestClient()).thenReturn(restHighLevelClient);
this.sut.updateIndexMappingForIndicesOfSameType( indices,"any field");
} catch (Exception e) {
fail("Should not throw this exception" + e.getMessage());
}
}
@Test(expected = AppException.class)
public void should_throw_exception_if_someIndex_is_invalid_andWeIndexfield_with_keyword() throws Exception {
try {
Set<String> indices = new HashSet<String>();
indices.add("invalid 1");
GetFieldMappingsResponse getFieldMappingsResponse = mock(GetFieldMappingsResponse.class);
doReturn(this.indicesClient).when(this.restHighLevelClient).indices();
when(this.indicesClient.getFieldMapping(ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(getFieldMappingsResponse);
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
builder.field("any field", new HashMap());
builder.endObject();
BytesReference bytesReference = BytesReference.bytes(builder);
FieldMappingMetaData mappingMetaData = new FieldMappingMetaData(index, bytesReference);
Map<String, FieldMappingMetaData> mapBuilder = new HashMap<>();
mapBuilder.put("data.any field", mappingMetaData);
Map<String, Map<String, FieldMappingMetaData>> mappingBuilder = new HashMap<>();
mappingBuilder.put("any index 1", mapBuilder);
mappingBuilder.put("any index 2", mapBuilder);
Map<String, Map<String, Map<String, FieldMappingMetaData>>> mapping = new HashMap<>();
mapping.put("indices 1", mappingBuilder);
when(getFieldMappingsResponse.mappings()).thenReturn(mapping);
doReturn(mappingResponse).when(this.indicesClient).putMapping(ArgumentMatchers.any(), ArgumentMatchers.any(RequestOptions.class));
BulkByScrollResponse response = mock(BulkByScrollResponse.class);
doReturn(response).when(this.restHighLevelClient).updateByQuery(ArgumentMatchers.any(), ArgumentMatchers.any(RequestOptions.class));
when(response.getBulkFailures()).thenReturn(new ArrayList<Failure>());
when(elasticClientHandler.createRestClient()).thenReturn(restHighLevelClient);
this.sut.updateIndexMappingForIndicesOfSameType(indices,"any field");
} catch (Exception e) {
throw e;
}
}
@Test(expected = AppException.class)
public void should_throw_exception_if_type_of_index_is_invalid_andWeIndexfield_with_keyword() throws Exception {
try {
Set<String> indices = new HashSet<String>();
indices.add("indices 1");
GetFieldMappingsResponse getFieldMappingsResponse = mock(GetFieldMappingsResponse.class);
doReturn(this.indicesClient).when(this.restHighLevelClient).indices();
when(this.indicesClient.getFieldMapping(ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(getFieldMappingsResponse);
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
builder.field("any field", new HashMap());
builder.endObject();
BytesReference bytesReference = BytesReference.bytes(builder);
FieldMappingMetaData mappingMetaData = new FieldMappingMetaData(index, bytesReference);
Map<String, FieldMappingMetaData> mapBuilder = new HashMap<>();
mapBuilder.put("data.any field", mappingMetaData);
Map<String, Map<String, FieldMappingMetaData>> mappingBuilder = new HashMap<>();
mappingBuilder.put("any index 1", mapBuilder);
mappingBuilder.put("any index 2", mapBuilder);
Map<String, Map<String, Map<String, FieldMappingMetaData>>> mapping = new HashMap<>();
mapping.put("indices 1", mappingBuilder);
when(getFieldMappingsResponse.mappings()).thenReturn(mapping);
doReturn(mappingResponse).when(this.indicesClient).putMapping(ArgumentMatchers.any(), ArgumentMatchers.any(RequestOptions.class));
BulkByScrollResponse response = mock(BulkByScrollResponse.class);
doReturn(response).when(this.restHighLevelClient).updateByQuery(ArgumentMatchers.any(), ArgumentMatchers.any(RequestOptions.class));
when(response.getBulkFailures()).thenReturn(new ArrayList<Failure>());
when(elasticClientHandler.createRestClient()).thenReturn(restHighLevelClient);
this.sut.updateIndexMappingForIndicesOfSameType(indices,"any field invalid");
} catch (Exception e) {
throw e;
}
}
@Test(expected = AppException.class)
public void should_throw_exception_if_elastic_search_failedToFetch_andWeIndexfield_with_keyword() throws Exception {
try {
Set<String> indices = new HashSet<String>();
indices.add("indices 1");
indices.add("indices Invalid");
GetFieldMappingsResponse getFieldMappingsResponse = mock(GetFieldMappingsResponse.class);
doReturn(this.indicesClient).when(this.restHighLevelClient).indices();
when(this.indicesClient.getFieldMapping(ArgumentMatchers.any(), ArgumentMatchers.any())).thenThrow(new ElasticsearchException(""));
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
builder.field("any field", new HashMap());
builder.endObject();
BytesReference bytesReference = BytesReference.bytes(builder);
FieldMappingMetaData mappingMetaData = new FieldMappingMetaData(index, bytesReference);
Map<String, FieldMappingMetaData> mapBuilder = new HashMap<>();
mapBuilder.put("data.any field", mappingMetaData);
Map<String, Map<String, FieldMappingMetaData>> mappingBuilder = new HashMap<>();
mappingBuilder.put("any index 1", mapBuilder);
mappingBuilder.put("any index 2", mapBuilder);
Map<String, Map<String, Map<String, FieldMappingMetaData>>> mapping = new HashMap<>();
mapping.put("indices 1", mappingBuilder);
when(getFieldMappingsResponse.mappings()).thenReturn(mapping);
doReturn(mappingResponse).when(this.indicesClient).putMapping(ArgumentMatchers.any(), ArgumentMatchers.any(RequestOptions.class));
BulkByScrollResponse response = mock(BulkByScrollResponse.class);
doReturn(response).when(this.restHighLevelClient).updateByQuery(ArgumentMatchers.any(), ArgumentMatchers.any(RequestOptions.class));
when(response.getBulkFailures()).thenReturn(new ArrayList<Failure>());
when(elasticClientHandler.createRestClient()).thenReturn(restHighLevelClient);
this.sut.updateIndexMappingForIndicesOfSameType(indices,"any field");
} catch (AppException e) {
throw e;
}
}
@Test(expected = AppException.class)
public void should_throw_exception_when_elastic_failedToIndex_indices_field_with_keyword() {
try {
Set<String> indices = new HashSet<String>();
indices.add("indices 1");
indices.add("indices Invalid");
GetFieldMappingsResponse getFieldMappingsResponse = mock(GetFieldMappingsResponse.class);
doReturn(this.indicesClient).when(this.restHighLevelClient).indices();
when(this.indicesClient.getFieldMapping(ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(getFieldMappingsResponse);
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
builder.field("any field", new HashMap());
builder.endObject();
BytesReference bytesReference = BytesReference.bytes(builder);
FieldMappingMetaData mappingMetaData = new FieldMappingMetaData(index, bytesReference);
Map<String, FieldMappingMetaData> mapBuilder = new HashMap<>();
mapBuilder.put("data.any field", mappingMetaData);
Map<String, Map<String, FieldMappingMetaData>> mappingBuilder = new HashMap<>();
mappingBuilder.put("any index 1", mapBuilder);
mappingBuilder.put("any index 2", mapBuilder);
Map<String, Map<String, Map<String, FieldMappingMetaData>>> mapping = new HashMap<>();
mapping.put("indices 1", mappingBuilder);
when(getFieldMappingsResponse.mappings()).thenReturn(mapping);
doReturn(mappingResponse).when(this.indicesClient).putMapping(ArgumentMatchers.any(), ArgumentMatchers.any(RequestOptions.class));
BulkByScrollResponse response = mock(BulkByScrollResponse.class);
doReturn(response).when(this.restHighLevelClient).updateByQuery(ArgumentMatchers.any(), ArgumentMatchers.any(RequestOptions.class));
when(response.getBulkFailures()).thenReturn(new ArrayList<Failure>());
when(this.indicesClient.putMapping(ArgumentMatchers.any(), ArgumentMatchers.any(RequestOptions.class))).thenThrow(new ElasticsearchException(""));
when(elasticClientHandler.createRestClient()).thenReturn(restHighLevelClient);
this.sut.updateIndexMappingForIndicesOfSameType(indices,"any field");
} catch (AppException e) {
throw e;
} catch (Exception e) {
fail("Should not throw this exception" + e.getMessage());
}
}
}
// 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.azure.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.indexer.cache.SchemaCache;
import org.opengroup.osdu.indexer.model.IndexSchema;
import org.opengroup.osdu.indexer.model.OperationType;
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.is.core.httpclient.RequestStatus;
import org.opengroup.osdu.is.core.logging.JaxRsDpsLog;
import org.opengroup.osdu.is.core.service.IndicesService;
import org.opengroup.osdu.is.core.util.AppException;
import org.opengroup.osdu.is.core.util.ElasticClientHandler;
import org.opengroup.osdu.is.core.util.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 SchemaCache 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);
Assert.assertNotNull(indexSchema);
}
@Test
public void should_returnValidResponse_givenValidSchema_getIndexerInputSchemaTest() throws Exception {
when(storageService.getStorageSchema(any())).thenReturn(someSchema);
IndexSchema indexSchema = this.sut.getIndexerInputSchema(kind);
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);
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);
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);
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)));
}
}
package org.opengroup.osdu.indexer.azure.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 org.springframework.beans.factory.annotation.Autowired;
//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();
//
// @Autowired
// @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;
// }
//}
// 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.azure.service;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentMatchers;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.opengroup.osdu.core.api.DpsHeaders;
import org.opengroup.osdu.indexer.model.RecordQueryResponse;
import org.opengroup.osdu.indexer.model.RecordReindexRequest;
import org.opengroup.osdu.indexer.service.ReindexServiceImpl;
import org.opengroup.osdu.indexer.service.StorageService;
import org.opengroup.osdu.indexer.util.IRequestInfo;
import org.opengroup.osdu.indexer.util.IndexerQueueTaskBuilder;
import org.opengroup.osdu.is.core.logging.JaxRsDpsLog;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.*;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
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)
public class ReindexServiceTest {
private final String cursor = "100";
private final String correlationId = UUID.randomUUID().toString();
@Mock
private StorageService storageService;
@Mock
private Map<String, String> httpHeaders;
@Mock
private IRequestInfo requestInfo;
@Mock
private IndexerQueueTaskBuilder indexerQueueTaskBuilder;
@Mock
private JaxRsDpsLog log;
@InjectMocks
private ReindexServiceImpl sut;
private RecordReindexRequest recordReindexRequest;
private RecordQueryResponse recordQueryResponse;
@Before
public void setup() {
initMocks(this);
mockStatic(UUID.class);
recordReindexRequest = RecordReindexRequest.builder().kind("tenant:test:test:1.0.0").cursor(cursor).build();
recordQueryResponse = new RecordQueryResponse();
httpHeaders = new HashMap<>();
httpHeaders.put(DpsHeaders.AUTHORIZATION, "testAuth");
httpHeaders.put(DpsHeaders.CORRELATION_ID, correlationId);
DpsHeaders standardHeaders = DpsHeaders.createFromMap(httpHeaders);
when(requestInfo.getHeaders()).thenReturn(standardHeaders);
when(requestInfo.getHeadersMapWithDwdAuthZ()).thenReturn(httpHeaders);
}
@Test
public void should_returnNull_givenNullResponseResult_reIndexRecordsTest() {
try {
recordQueryResponse.setResults(null);
when(storageService.getRecordsByKind(ArgumentMatchers.any())).thenReturn(recordQueryResponse);
String response = sut.reindexRecords(recordReindexRequest);
Assert.assertNull(response);
} catch (Exception e) {
fail("Should not throw this exception" + e.getMessage());
}
}
@Test
public void should_returnNull_givenEmptyResponseResult_reIndexRecordsTest() {
try {
recordQueryResponse.setResults(new ArrayList<>());
when(storageService.getRecordsByKind(ArgumentMatchers.any())).thenReturn(recordQueryResponse);
String response = sut.reindexRecords(recordReindexRequest);
Assert.assertNull(response);
} catch (Exception e) {
fail("Should not throw this exception" + e.getMessage());
}
}
@Test
public void should_returnRecordQueryRequestPayload_givenValidResponseResult_reIndexRecordsTest() {
try {
recordQueryResponse.setCursor(cursor);
List<String> results = new ArrayList<>();
results.add("test1");
recordQueryResponse.setResults(results);
when(storageService.getRecordsByKind(ArgumentMatchers.any())).thenReturn(recordQueryResponse);
String taskQueuePayload = sut.reindexRecords(recordReindexRequest);
Assert.assertEquals("{\"kind\":\"tenant:test:test:1.0.0\",\"cursor\":\"100\"}", taskQueuePayload);
} catch (Exception e) {
fail("Should not throw exception" + e.getMessage());
}
}
@Test
public void should_returnRecordChangedMessage_givenValidResponseResult_reIndexRecordsTest() {
try {
List<String> results = new ArrayList<>();
results.add("test1");
recordQueryResponse.setResults(results);
when(storageService.getRecordsByKind(ArgumentMatchers.any())).thenReturn(recordQueryResponse);
String taskQueuePayload = sut.reindexRecords(recordReindexRequest);
Assert.assertEquals(String.format("{\"data\":\"[{\\\"id\\\":\\\"test1\\\",\\\"kind\\\":\\\"tenant:test:test:1.0.0\\\",\\\"op\\\":\\\"create\\\"}]\",\"attributes\":{\"slb-correlation-id\":\"%s\"}}", correlationId), taskQueuePayload);
} catch (Exception e) {
fail("Should not throw exception" + e.getMessage());
}
}
}
// 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.azure.service;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentMatchers;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.opengroup.osdu.indexer.model.RecordQueryResponse;
import org.opengroup.osdu.indexer.model.RecordReindexRequest;
import org.opengroup.osdu.indexer.model.Records;
import org.opengroup.osdu.indexer.service.StorageServiceImpl;
import org.opengroup.osdu.indexer.util.IRequestInfo;
import org.opengroup.osdu.indexer.util.JobStatus;
import org.opengroup.osdu.indexer.util.RecordInfo;
import org.opengroup.osdu.is.core.httpclient.HttpResponse;
import org.opengroup.osdu.is.core.logging.JaxRsDpsLog;
import org.opengroup.osdu.is.core.service.UrlFetchService;
import org.opengroup.osdu.is.core.util.AppException;
import org.springframework.http.HttpStatus;
import org.springframework.test.context.junit4.SpringRunner;
import java.lang.reflect.Type;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import static org.junit.Assert.*;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.powermock.api.mockito.PowerMockito.when;
@Ignore
@RunWith(SpringRunner.class)
public class StorageServiceTest {
@Mock
private UrlFetchService urlFetchService;
@Mock
private JobStatus jobStatus;
@Mock
private JaxRsDpsLog log;
@Mock
private IRequestInfo requestInfo;
@InjectMocks
private StorageServiceImpl sut;
private List<String> ids;
@Before
public void setup() {
String recordChangedMessages = "[{\"id\":\"tenant1:doc:1dbf528e0e0549cab7a08f29fbfc8465\",\"kind\":\"tenant1:testindexer1528919679710:well:1.0.0\",\"op\":\"purge\"}," +
"{\"id\":\"tenant1:doc:1dbf528e0e0549cab7a08f29fbfc8465\",\"kind\":\"tenant1:testindexer1528919679710:well:1.0.0\",\"op\":\"create\"}]";
when(this.requestInfo.getHeadersMap()).thenReturn(new HashMap<>());
Type listType = new TypeToken<List<RecordInfo>>() {}.getType();
List<RecordInfo> msgs = (new Gson()).fromJson(recordChangedMessages, listType);
jobStatus.initialize(msgs);
ids = Arrays.asList("tenant1:doc:1dbf528e0e0549cab7a08f29fbfc8465", "tenant1:doc:1dbf528e0e0549cab7a08f29fbfc8465");
}
@Test
public void should_return404_givenNullData_getValidStorageRecordsTest() throws URISyntaxException {
HttpResponse httpResponse = mock(HttpResponse.class);
Mockito.when(httpResponse.getBody()).thenReturn(null);
when(this.urlFetchService.sendRequest(ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(httpResponse);
should_return404_getValidStorageRecordsTest();
}
@Test
public void should_return404_givenEmptyData_getValidStorageRecordsTest() throws URISyntaxException {
String emptyDataFromStorage = "{\"records\":[],\"notFound\":[]}";
HttpResponse httpResponse = mock(HttpResponse.class);
Mockito.when(httpResponse.getBody()).thenReturn(emptyDataFromStorage);
when(this.urlFetchService.sendRequest(ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(httpResponse);
should_return404_getValidStorageRecordsTest();
}
@Test
public void should_returnOneValidRecords_givenValidData_getValidStorageRecordsTest() throws URISyntaxException {
String validDataFromStorage = "{\"records\":[{\"id\":\"testid\", \"version\":1, \"kind\":\"tenant:test:test:1.0.0\"}],\"notFound\":[\"invalid1\"]}";
HttpResponse httpResponse = mock(HttpResponse.class);
Mockito.when(httpResponse.getBody()).thenReturn(validDataFromStorage);
when(this.urlFetchService.sendRequest(ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(httpResponse);
Records storageRecords = this.sut.getStorageRecords(ids);
assertEquals(1, storageRecords.getRecords().size());
}
@Test
public void should_returnValidResponse_givenValidRecordQueryRequest_getRecordListByKind() throws Exception {
RecordReindexRequest recordReindexRequest = RecordReindexRequest.builder().kind("tenant:test:test:1.0.0").cursor("100").build();
HttpResponse httpResponse = new HttpResponse();
httpResponse.setBody(new Gson().toJson(recordReindexRequest, RecordReindexRequest.class));
when(this.urlFetchService.sendRequest(ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(httpResponse);
RecordQueryResponse recordQueryResponse = this.sut.getRecordsByKind(recordReindexRequest);
assertEquals("100", recordQueryResponse.getCursor());
assertNull(recordQueryResponse.getResults());
}
@Test
public void should_returnValidResponse_givenValidKind_getSchemaByKind() throws Exception {
String validSchemaFromStorage = "{" +
" \"kind\": \"tenant:test:test:1.0.0\"," +
" \"schema\": [" +
" {" +
" \"path\": \"msg\"," +
" \"kind\": \"string\"" +
" }," +
" {" +
" \"path\": \"references.entity\"," +
" \"kind\": \"string\"" +
" }" +
" ]," +
" \"ext\": null" +
"}";
String kind = "tenant:test:test:1.0.0";
HttpResponse httpResponse = new HttpResponse();
httpResponse.setResponseCode(HttpStatus.OK.value());
httpResponse.setBody(validSchemaFromStorage);
when(this.urlFetchService.sendRequest(ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(httpResponse);
String recordSchemaResponse = this.sut.getStorageSchema(kind);
assertNotNull(recordSchemaResponse);
}
@Test
public void should_returnNullResponse_givenAbsentKind_getSchemaByKind() throws Exception {
String kind = "tenant:test:test:1.0.0";
HttpResponse httpResponse = new HttpResponse();
httpResponse.setResponseCode(HttpStatus.NOT_FOUND.value());
when(this.urlFetchService.sendRequest(ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(httpResponse);
String recordSchemaResponse = this.sut.getStorageSchema(kind);
assertNull(recordSchemaResponse);
}
@Test
public void should_returnOneValidRecords_givenValidData_getValidStorageRecordsWithInvalidConversionTest() throws URISyntaxException {
String validDataFromStorage = "{\"records\":[{\"id\":\"testid\", \"version\":1, \"kind\":\"tenant:test:test:1.0.0\"}],\"notFound\":[\"invalid1\"],\"conversionStatuses\": [{\"id\":\"testid\",\"status\":\"ERROR\",\"errors\":[\"conversion error occured\"] } ]}";
HttpResponse httpResponse = mock(HttpResponse.class);
Mockito.when(httpResponse.getBody()).thenReturn(validDataFromStorage);
when(this.urlFetchService.sendRequest(ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(httpResponse);
Records storageRecords = this.sut.getStorageRecords(ids);
assertEquals(1, storageRecords.getRecords().size());
assertEquals(1, storageRecords.getConversionStatuses().get(0).getErrors().size());
assertEquals("conversion error occured", storageRecords.getConversionStatuses().get(0).getErrors().get(0));
}
private void should_return404_getValidStorageRecordsTest() {
try {
this.sut.getStorageRecords(ids);
fail("Should throw exception");
} catch (AppException e) {
assertEquals(HttpStatus.NOT_FOUND, e.getError().getCode());
} catch (Exception e) {
fail("Should not throw this exception" + e.getMessage());
}
}
}
// 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.azure.service;
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.api.DpsHeaders;
import org.opengroup.osdu.core.multitenancy.ITenantFactory;
import org.opengroup.osdu.core.multitenancy.TenantInfo;
import org.opengroup.osdu.indexer.service.TenantInfoServiceImpl;
import org.opengroup.osdu.is.core.util.AppException;
import org.opengroup.osdu.is.core.util.IHeadersInfo;
import org.springframework.http.HttpHeaders;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.when;
@RunWith(SpringRunner.class)
public class TenantInfoServiceTest {
private static final String HEADER_NAME = "ANY_HEADER";
private static final String HEADER_VALUE = "ANY_VALUE";
@Mock
private ITenantFactory tenantFactory;
@Mock
private IHeadersInfo headersInfo;
@InjectMocks
private TenantInfoServiceImpl sut;
@Mock
private TenantInfo info;
@Mock
private HttpHeaders httpHeaders;
@InjectMocks
private DpsHeaders HEADERS;
@Before
public void setup() {
HEADERS.put(HEADER_NAME, HEADER_VALUE);
}
@Ignore
@Test
public void should_return_validTenant_given_validAccountId() {
when(this.info.getName()).thenReturn("tenant1");
when(tenantFactory.getTenantInfo("tenant1")).thenReturn(info);
when(this.headersInfo.getHeaders()).thenReturn(HEADERS);
when(this.headersInfo.getPartitionId()).thenReturn("tenant1");
when(this.sut.getTenantInfo()).thenReturn(info);
assertNotNull(this.sut.getTenantInfo());
assertEquals("tenant1", this.sut.getTenantInfo().getName());
}
@Test(expected = AppException.class)
public void should_throwException_given_invalidAccountId() {
when(this.info.getName()).thenReturn("tenant2");
when(tenantFactory.getTenantInfo("tenant1")).thenReturn(null);
when(this.sut.getTenantInfo()).thenReturn(info);
assertNotNull(this.sut.getTenantInfo());
}
}
\ No newline at end of file
......@@ -73,6 +73,22 @@
<version>0.8</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.6.2</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>6.6.2</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.6.2</version>
</dependency>
<!-- Test Dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
......@@ -126,6 +142,11 @@
<version>1.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.api.grpc</groupId>
<artifactId>proto-google-iam-v1</artifactId>
<version>0.12.0</version>
</dependency>
</dependencies>
......
......@@ -23,6 +23,7 @@ import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.opengroup.osdu.core.api.DpsHeaders;
import org.opengroup.osdu.indexer.util.IRequestInfo;
import org.opengroup.osdu.is.core.logging.JaxRsDpsLog;
import org.opengroup.osdu.is.core.model.IndexInfo;
import org.opengroup.osdu.is.core.provider.interfaces.util.IRequestInfo;
......
......@@ -20,7 +20,6 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.MockitoJUnitRunner;
import org.opengroup.osdu.core.multitenancy.TenantInfo;
import org.opengroup.osdu.is.core.cache.ElasticCredentialsCache;
......@@ -29,14 +28,16 @@ import org.opengroup.osdu.is.core.model.ClusterSettings;
import org.opengroup.osdu.is.core.provider.interfaces.persistence.ElasticRepository;
import org.opengroup.osdu.is.core.service.TenantInfoService;
import org.opengroup.osdu.is.core.util.AppException;
import org.opengroup.osdu.is.core.util.Config;
import org.opengroup.osdu.is.core.util.IHeadersInfo;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.util.ReflectionTestUtils;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.when;
@Ignore
@RunWith(MockitoJUnitRunner.class)
@RunWith(SpringRunner.class)
public class ElasticSettingServiceTest {
@Mock
......@@ -46,35 +47,41 @@ public class ElasticSettingServiceTest {
@Mock
private ElasticCredentialsCache elasticCredentialCache;
@Mock
private JaxRsDpsLog log;
private TenantInfo tenantInfo;
@InjectMocks
private ElasticSettingServiceImpl sut;
@Mock
private ClusterSettings clusterSettings;
@Mock
private IHeadersInfo headersInfo;
@Mock
private Config config;
private JaxRsDpsLog log;
@InjectMocks
private ElasticSettingServiceImpl sut;
private TenantInfo tenantInfo;
private ClusterSettings clusterSettings;
public String GAE_SERVICE = "indexer";
private final String host = "db5c51c1.us-central1.gcp.cloud.es.io";
private final int port = 9243;
private final String credentials = "name:password";
String cacheKey = "";
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
when(Config.getDeployedServiceId()).thenReturn("search");
tenantInfo = new TenantInfo();
tenantInfo.setName("tenant1");
when(tenantInfo.getName()).thenReturn("tenant1");
when(this.headersInfo.getPartitionId()).thenReturn("tenant1");
when(this.tenantInfoService.getTenantInfo()).thenReturn(tenantInfo);
ReflectionTestUtils.setField(sut, "GAE_SERVICE", "indexer");
clusterSettings = ClusterSettings.builder().host(host).port(port).userNameAndPassword(credentials).build();
cacheKey = String.format("%s-%s", GAE_SERVICE, tenantInfo.getName());
}
@Test
public void should_getValid_clusterSettings_fromCache() {
when(this.elasticCredentialCache.get("search-tenant1")).thenReturn(clusterSettings);
when(this.elasticCredentialCache.get(cacheKey)).thenReturn(clusterSettings);
ClusterSettings response = this.sut.getElasticClusterInformation();
assertNotNull(response);
......@@ -84,7 +91,10 @@ public class ElasticSettingServiceTest {
}
@Test
public void should_getValid_clusterSettings_fromDatastore() {
public void should_getValid_clusterSettings_fromCosmosDB() {
when(this.elasticCredentialCache.get(cacheKey)).thenReturn(clusterSettings);
when(this.elasticRepository.getElasticClusterSettings(tenantInfo)).thenReturn(clusterSettings);
ClusterSettings response = this.sut.getElasticClusterInformation();
......@@ -96,8 +106,10 @@ public class ElasticSettingServiceTest {
@Test(expected = AppException.class)
public void should_throwAppException_when_tenantClusterInfo_not_found() throws AppException {
when(this.elasticRepository.getElasticClusterSettings(tenantInfo)).thenReturn(null);
this.sut.getElasticClusterInformation();
}
}
......@@ -28,10 +28,13 @@ import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentMatchers;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.opengroup.osdu.core.api.DpsHeaders;
import org.opengroup.osdu.indexer.logging.AuditLogger;
import org.opengroup.osdu.core.api.DpsHeaders;
import org.opengroup.osdu.indexer.util.IRequestInfo;
import org.opengroup.osdu.is.core.model.ClusterSettings;
import org.opengroup.osdu.is.core.provider.interfaces.util.IHeadersInfo;
import org.opengroup.osdu.is.core.provider.interfaces.util.IRequestInfo;
......@@ -39,8 +42,11 @@ import org.opengroup.osdu.is.core.service.ElasticSettingService;
import org.opengroup.osdu.is.core.service.IndicesService;
import org.opengroup.osdu.is.core.util.AppException;
import org.opengroup.osdu.is.core.util.ElasticClientHandler;
import org.opengroup.osdu.is.core.util.ElasticIndexNameResolver;
import org.opengroup.osdu.is.core.util.IHeadersInfo;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.IOException;
import java.lang.reflect.Type;
......@@ -50,10 +56,10 @@ import java.util.UUID;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.powermock.api.mockito.PowerMockito.when;
@Ignore
@RunWith(PowerMockRunner.class)
@RunWith(SpringRunner.class)
@PrepareForTest({RestHighLevelClient.class, Response.class, RestClient.class, HttpEntity.class, EntityUtils.class})
public class IndexCopyServiceImplTest {
private final String correlationId = UUID.randomUUID().toString();
......@@ -77,6 +83,8 @@ public class IndexCopyServiceImplTest {
@Mock
private ElasticClientHandler elasticClientHandler;
@Mock
private ElasticIndexNameResolver elasticIndexNameResolver;
@Mock
private Response response;
@Mock
private ElasticSettingService elasticSettingService;
......@@ -93,7 +101,7 @@ public class IndexCopyServiceImplTest {
@Before
public void setup() {
// mockStatic(EntityUtils.class);
commonCluster = ClusterSettings.builder().host("commonhost").port(8080).userNameAndPassword("username:pwd").build();
httpHeaders = new HashMap<>();
......@@ -107,13 +115,16 @@ public class IndexCopyServiceImplTest {
Type mapType = new TypeToken<Map<String, Object>>() {}.getType();
String afterFormat = "{\"properties\":{\"id\":{\"type\":\"keyword\"}}}";
correctMap = new Gson().fromJson(afterFormat, mapType);
restHighLevelClient = mock(RestHighLevelClient.class);
}
@Test(expected = IOException.class)
public void should_throwIOException_when_indexMappingNotFound() throws Exception {
IOException exception = new IOException("Fail to get mapping for the given index from common cluster.");
when(this.mappingService.getIndexMapping(any(), any())).thenThrow(exception);
when(this.mappingService.getIndexMapping(ArgumentMatchers.any(), ArgumentMatchers.any())).thenThrow(exception);
this.sut.copyIndex("common:metadata:entity:1.0.0");
}
......@@ -132,7 +143,7 @@ public class IndexCopyServiceImplTest {
String mappingJson = "{\"common-metadata-entity-1.0.0\":{\"mappings\":{\"entity\":{\"properties\":{\"id\":{\"type\":\"keyword\"}}}}}}";
when(elasticClientHandler.createRestClient()).thenReturn(restHighLevelClient);
try {
when(this.mappingService.getIndexMapping(any(), any())).thenReturn(mappingJson);
when(this.mappingService.getIndexMapping(ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(mappingJson);
Map<String, Object> resultMap = this.sut.getIndexMappingsFromCommonCluster("test", "test");
Assert.assertEquals(resultMap, correctMap);
} catch (Exception ignored) {
......@@ -143,13 +154,17 @@ public class IndexCopyServiceImplTest {
public void should_returnClusterInfo_getCommonClusterInformationtest() {
try {
String[] correctCommonCluster = {"https://commonhost:8080", "username", "pwd"};
when(elasticClientHandler.createRestClient()).thenReturn(restHighLevelClient);
when(elasticSettingService.getElasticClusterInformation()).thenReturn(commonCluster);
String[] resultCommonCluster = this.sut.getCommonClusterInformation();
Assert.assertEquals(correctCommonCluster[0], resultCommonCluster[0]);
Assert.assertEquals(correctCommonCluster[1], resultCommonCluster[1]);
Assert.assertEquals(correctCommonCluster[2], resultCommonCluster[2]);
} catch (IOException ignored) {
fail("Should not throw this exception " + ignored.getMessage());
}
}
......@@ -157,9 +172,10 @@ public class IndexCopyServiceImplTest {
public void should_throwException_failToCreateIndexInTenantCluster_createIndexInTenantClustertest() {
try {
when(elasticClientHandler.createRestClient()).thenReturn(restHighLevelClient);
when(indicesService.createIndex(any(), any(), any(), any(), any())).thenReturn(false);
when(indicesService.createIndex(ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(false);
this.sut.createIndexInTenantCluster("test", "test", "test", correctMap);
} catch (IOException ignored) {
fail("Should not throw this exception " + ignored.getMessage());
}
}
......@@ -171,7 +187,7 @@ public class IndexCopyServiceImplTest {
Request request = new Request("POST", "/_reindex?wait_for_completion=false");
request.setEntity(httpEntityRequest);
when(elasticClientHandler.createRestClient()).thenReturn(restHighLevelClient);
when(indicesService.createIndex(any(), any(), any(), any(), any())).thenReturn(false);
when(indicesService.createIndex(ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(false);
when(restHighLevelClient.getLowLevelClient()).thenReturn(restClient);
when(restClient.performRequest(request)).thenReturn(response);
when(response.getEntity()).thenReturn(httpEntity);
......
......@@ -14,32 +14,13 @@
package org.opengroup.osdu.indexer.service;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.powermock.api.mockito.PowerMockito.spy;
import static org.powermock.api.mockito.PowerMockito.when;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.apache.http.StatusLine;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse;
import org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse.FieldMappingMetaData;
import org.elasticsearch.action.bulk.BulkItemResponse.Failure;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.IndicesClient;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.*;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
......@@ -56,9 +37,18 @@ import org.opengroup.osdu.is.core.util.AppException;
import org.opengroup.osdu.is.core.util.ElasticClientHandler;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.IOException;
import java.util.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.powermock.api.mockito.PowerMockito.when;
@Ignore
@RunWith(SpringRunner.class)
@PrepareForTest({ RestHighLevelClient.class, IndicesClient.class })
......
//// Copyright 2017-2019, Schlumberger
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.
......
......@@ -22,10 +22,10 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.opengroup.osdu.core.api.DpsHeaders;
import org.opengroup.osdu.indexer.model.RecordQueryResponse;
import org.opengroup.osdu.indexer.model.RecordReindexRequest;
import org.opengroup.osdu.indexer.util.IndexerQueueTaskBuilder;
import org.opengroup.osdu.core.api.DpsHeaders;
import org.opengroup.osdu.is.core.logging.JaxRsDpsLog;
import org.opengroup.osdu.is.core.provider.interfaces.util.IRequestInfo;
import org.springframework.test.context.junit4.SpringRunner;
......
......@@ -34,6 +34,10 @@ import org.opengroup.osdu.indexer.model.RecordReindexRequest;
import org.opengroup.osdu.indexer.model.Records;
import org.opengroup.osdu.indexer.util.JobStatus;
import org.opengroup.osdu.indexer.util.RecordInfo;
import org.opengroup.osdu.is.core.httpclient.HttpResponse;
import org.opengroup.osdu.is.core.logging.JaxRsDpsLog;
import org.opengroup.osdu.is.core.service.UrlFetchService;
import org.opengroup.osdu.is.core.util.AppException;
import org.springframework.http.HttpStatus;
import org.springframework.test.context.junit4.SpringRunner;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment