Commit ca8da059 authored by Sviatoslav Nekhaienko's avatar Sviatoslav Nekhaienko
Browse files

refactoring

parent b046996b
Pipeline #20456 failed with stages
in 6 minutes and 33 seconds
......@@ -88,7 +88,7 @@ public class PropertiesProcessor {
return definition.getProperties().entrySet().stream().flatMap(this::processPropertyEntry);
}
protected Stream<Map<String, Object>> processPropertyEntry(Map.Entry<String, TypeProperty> entry) {
private Stream<Map<String, Object>> processPropertyEntry(Map.Entry<String, TypeProperty> entry) {
Preconditions.checkNotNull(entry, "entry cannot be null");
if ("object".equals(entry.getValue().getType())
......@@ -120,7 +120,7 @@ public class PropertiesProcessor {
return storageSchemaEntry(getTypeByDefinitionProperty(entry.getValue()), pathPrefixWithDot + entry.getKey());
}
protected Stream<Map<String, Object>> storageSchemaEntry(String kind, String path) {
private Stream<Map<String, Object>> storageSchemaEntry(String kind, String path) {
Preconditions.checkNotNullOrEmpty(kind, "kind cannot be null or empty");
Preconditions.checkNotNullOrEmpty(path, "path cannot be null or empty");
......@@ -130,7 +130,7 @@ public class PropertiesProcessor {
return Stream.of(map);
}
protected String getTypeByDefinitionProperty(TypeProperty definitionProperty) {
private String getTypeByDefinitionProperty(TypeProperty definitionProperty) {
Preconditions.checkNotNull(definitionProperty, "definitionProperty cannot be null");
return Stream.of(
......@@ -143,32 +143,32 @@ public class PropertiesProcessor {
.orElse(getFromType(definitionProperty::getType)).get();
}
protected Supplier<String> getFromPattern(String pattern) {
private Supplier<String> getFromPattern(String pattern) {
return () -> Objects.nonNull(pattern) && pattern.startsWith(LINK_PREFIX) ? LINK_TYPE : null;
}
protected Supplier<String> getFromItemsPattern(Supplier<String> itemsPatternSupplier) {
private Supplier<String> getFromItemsPattern(Supplier<String> itemsPatternSupplier) {
return () -> {
String itemsPattern = itemsPatternSupplier.get();
return Objects.nonNull(itemsPattern) && itemsPattern.startsWith(LINK_PREFIX) ? LINK_TYPE : null;
};
}
protected Supplier<String> getFromType(Supplier<String> typeSupplier) {
private Supplier<String> getFromType(Supplier<String> typeSupplier) {
return () -> {
String type = typeSupplier.get();
return schemaConverterConfig.getPrimitiveTypesMap().getOrDefault(type, type);
};
}
protected Supplier<String> getFromFormat(Supplier<String> formatSupplier){
private Supplier<String> getFromFormat(Supplier<String> formatSupplier){
return () -> {
String format = formatSupplier.get();;
return Objects.nonNull(format) ? schemaConverterConfig.getPrimitiveTypesMap().getOrDefault(format, format) : null;
};
}
protected Supplier<String> getFromItemsType(Supplier<String> itemsTypeSupplier) {
private Supplier<String> getFromItemsType(Supplier<String> itemsTypeSupplier) {
return () -> {
String itemsType = itemsTypeSupplier.get();
return Objects.nonNull(itemsType) ? schemaConverterConfig.getPrimitiveTypesMap().getOrDefault(itemsType, itemsType) : null;
......
......@@ -64,7 +64,7 @@ public class SchemaToStorageFormatImpl implements SchemaToStorageFormat {
return convert(parserJsonString(schemaServiceFormat), kind);
}
protected SchemaRoot parserJsonString(final String schemaServiceFormat) {
private SchemaRoot parserJsonString(final String schemaServiceFormat) {
try {
return objectMapper.readValue(schemaServiceFormat, SchemaRoot.class);
} catch (JsonProcessingException e) {
......@@ -72,7 +72,7 @@ public class SchemaToStorageFormatImpl implements SchemaToStorageFormat {
}
}
protected String saveJsonToString(final Map<String, Object> schemaServiceFormat) {
private String saveJsonToString(final Map<String, Object> schemaServiceFormat) {
try {
return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(schemaServiceFormat);
} catch (JsonProcessingException e) {
......@@ -80,7 +80,7 @@ public class SchemaToStorageFormatImpl implements SchemaToStorageFormat {
}
}
public Map<String, Object> convert(SchemaRoot schemaServiceSchema, String kind) {
private Map<String, Object> convert(SchemaRoot schemaServiceSchema, String kind) {
Preconditions.checkNotNull(objectMapper, "schemaServiceSchema cannot be null");
Preconditions.checkNotNullOrEmpty(kind, "kind cannot be null or empty");
......
......@@ -160,7 +160,7 @@ public class IndexSchemaServiceImpl implements IndexSchemaService {
}
}
protected String getSchema(String kind) throws URISyntaxException, UnsupportedEncodingException {
private String getSchema(String kind) throws URISyntaxException, UnsupportedEncodingException {
return this.schemaProvider.getSchema(kind);
}
......
......@@ -67,7 +67,7 @@ public class SchemaProviderImpl implements SchemaService {
return getFromSchemaService(kind);
}
public String getFromSchemaService(String kind) throws UnsupportedEncodingException, URISyntaxException {
protected String getFromSchemaService(String kind) throws UnsupportedEncodingException, URISyntaxException {
HttpResponse response = getSchemaServiceResponse(kind);
if (response.getResponseCode() == HttpStatus.SC_NOT_FOUND) {
......@@ -79,7 +79,7 @@ public class SchemaProviderImpl implements SchemaService {
schemaToStorageFormat.convertToString(response.getBody(), kind);
}
public String getFromStorageService(String kind) throws URISyntaxException, UnsupportedEncodingException {
protected String getFromStorageService(String kind) throws URISyntaxException, UnsupportedEncodingException {
String schemaFromStorageService = storageService.getStorageSchema(kind);
if (schemaFromStorageService != null) {
......@@ -91,7 +91,7 @@ public class SchemaProviderImpl implements SchemaService {
return null;
}
protected HttpResponse getSchemaServiceResponse(String kind) throws UnsupportedEncodingException, URISyntaxException {
private HttpResponse getSchemaServiceResponse(String kind) throws UnsupportedEncodingException, URISyntaxException {
String url = String.format("%s/%s", configurationProperties.getSchemaHost(), URLEncoder.encode(kind, StandardCharsets.UTF_8.toString()));
FetchServiceHttpRequest request = FetchServiceHttpRequest.builder()
.httpMethod(HttpMethods.GET)
......
......@@ -12,31 +12,34 @@
// See the License for the specific language governing permissions and
// limitations under the License.package org.opengroup.osdu.indexer.service.impl;
package org.opengroup.osdu.indexer.service.impl;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpStatus;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.Spy;
import org.mockito.*;
import org.opengroup.osdu.core.common.http.IUrlFetchService;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.model.http.HttpResponse;
import org.opengroup.osdu.core.common.provider.interfaces.IRequestInfo;
import org.opengroup.osdu.indexer.config.IndexerConfigurationProperties;
import org.opengroup.osdu.indexer.schema.converter.SchemaToStorageFormatImpl;
import org.opengroup.osdu.indexer.service.StorageService;
import org.opengroup.osdu.indexer.service.impl.SchemaProviderImpl;
import org.powermock.api.mockito.PowerMockito;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.util.Map;
import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.*;
import static org.powermock.api.mockito.PowerMockito.when;
@RunWith(SpringRunner.class)
public class SchemaProviderImplTest {
......@@ -77,4 +80,98 @@ public class SchemaProviderImplTest {
"}", schema);
}
@Test
public void should_returnValidResponse_givenValidSchema() throws Exception {
String validSchemaFromSchemaService = "{\n" +
"\"properties\": {" +
" \"data\":{\n" +
" \"allOf\":[\n" +
" {\n" +
" \"type\":\"object\",\n" +
" \"properties\":{\n" +
" \"WellID\":{\n" +
" \"type\":\"string\",\n" +
" \"pattern\":\"^srn:<namespace>:master-data\\\\/Well:[^:]+:[0-9]*$\"\n" +
" }\n" +
" }\n" +
" }\n" +
" ]\n" +
" }\n" +
" }\n" +
"}";
String kind = "tenant:test:test:1.0.0";
HttpResponse httpResponse = new HttpResponse();
httpResponse.setResponseCode(org.springframework.http.HttpStatus.OK.value());
httpResponse.setBody(validSchemaFromSchemaService);
PowerMockito.when(this.urlFetchService.sendRequest(any())).thenReturn(httpResponse);
String recordSchemaResponse = this.sut.getSchema(kind);
Map<String, Object> result = objectMapper.readValue(recordSchemaResponse,
new TypeReference<Map<String,Object>>(){});
assertEquals("Schema must have two root items", 2, result.size());
assertEquals("Wrong kind", "tenant:test:test:1.0.0", result.get("kind"));
assertEquals("Wrong schema attributes", "[{path=WellID, kind=link}]", result.get("schema").toString());
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(org.springframework.http.HttpStatus.NOT_FOUND.value());
PowerMockito.when(this.urlFetchService.sendRequest(any())).thenReturn(httpResponse);
String recordSchemaResponse = this.sut.getSchema(kind);
assertNull(recordSchemaResponse);
}
@Test
public void should_call_Storage_then_Schema() throws Exception {
String kind = "tenant:test:test:1.0.0";
SchemaProviderImpl schemaService = Mockito.mock(SchemaProviderImpl.class);
PowerMockito.when(schemaService.getSchema(any())).thenCallRealMethod();
InOrder inOrder = inOrder(schemaService);
String recordSchemaResponse = schemaService.getSchema(kind);
assertNull(recordSchemaResponse);
inOrder.verify(schemaService).getSchema(any());
inOrder.verify(schemaService).getFromStorageService(any());
inOrder.verify(schemaService).getFromSchemaService(any());
verify(schemaService, times(1)).getFromStorageService(any());
verify(schemaService, times(1)).getFromSchemaService(any());
}
@Test
public void should_call_only_Storage_if_it_returns_result() throws Exception {
String kind = "tenant:test:test:1.0.0";
SchemaProviderImpl schemaService = Mockito.mock(SchemaProviderImpl.class);
when(schemaService.getSchema(any())).thenCallRealMethod();
String someSchema = "some schema";
when(schemaService.getFromStorageService(any())).thenReturn(someSchema);
InOrder inOrder = inOrder(schemaService);
String recordSchemaResponse = schemaService.getSchema(kind);
assertEquals(recordSchemaResponse, someSchema);
inOrder.verify(schemaService).getSchema(any());
inOrder.verify(schemaService).getFromStorageService(any());
verify(schemaService, times(0)).getFromSchemaService(any());
verify(schemaService, times(1)).getFromStorageService(any());
}
}
\ No newline at end of file
......@@ -23,19 +23,18 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.core.common.model.http.RequestStatus;
import org.opengroup.osdu.core.common.model.indexer.IndexSchema;
import org.opengroup.osdu.core.common.model.indexer.OperationType;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.search.ElasticIndexNameResolver;
import org.opengroup.osdu.core.common.search.IndicesService;
import org.opengroup.osdu.indexer.provider.interfaces.ISchemaCache;
import org.opengroup.osdu.indexer.service.IndexSchemaServiceImpl;
import org.opengroup.osdu.indexer.service.IndexerMappingService;
import org.opengroup.osdu.indexer.service.SchemaService;
import org.opengroup.osdu.indexer.service.StorageService;
import org.opengroup.osdu.core.common.model.http.RequestStatus;
import org.opengroup.osdu.core.common.search.IndicesService;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.indexer.util.ElasticClientHandler;
import org.opengroup.osdu.core.common.search.ElasticIndexNameResolver;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.springframework.test.context.junit4.SpringRunner;
......@@ -63,8 +62,6 @@ public class IndexerSchemaServiceTest {
@Mock
private JaxRsDpsLog log;
@Mock
private StorageService storageService;
@Mock
private ElasticClientHandler elasticClientHandler;
@Mock
private ElasticIndexNameResolver elasticIndexNameResolver;
......
// 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.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.*;
import org.opengroup.osdu.core.common.http.IUrlFetchService;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.model.http.HttpResponse;
import org.opengroup.osdu.core.common.provider.interfaces.IRequestInfo;
import org.opengroup.osdu.indexer.config.IndexerConfigurationProperties;
import org.opengroup.osdu.indexer.schema.converter.SchemaToStorageFormatImpl;
import org.opengroup.osdu.indexer.service.StorageService;
import org.opengroup.osdu.indexer.service.impl.SchemaProviderImpl;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
import static org.powermock.api.mockito.PowerMockito.when;
@RunWith(SpringRunner.class)
public class SchemaProviderTest {
private ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().build();
private JaxRsDpsLog log = Mockito.mock(JaxRsDpsLog.class);
@Spy
private SchemaToStorageFormatImpl schemaToStorageFormatImpl = new SchemaToStorageFormatImpl(objectMapper, log, null);
@Mock
private IUrlFetchService urlFetchService;
@Mock
private IRequestInfo requestInfo;
@Mock
private StorageService storageService;
@Mock
private IndexerConfigurationProperties configurationProperties;
@InjectMocks
private SchemaProviderImpl sut;
@Before
public void setup() {
when(this.requestInfo.getHeadersMap()).thenReturn(new HashMap<>());
}
@Test
public void should_returnValidResponse_givenValidSchema() throws Exception {
String validSchemaFromSchemaService = "{\n" +
"\"properties\": {" +
" \"data\":{\n" +
" \"allOf\":[\n" +
" {\n" +
" \"type\":\"object\",\n" +
" \"properties\":{\n" +
" \"WellID\":{\n" +
" \"type\":\"string\",\n" +
" \"pattern\":\"^srn:<namespace>:master-data\\\\/Well:[^:]+:[0-9]*$\"\n" +
" }\n" +
" }\n" +
" }\n" +
" ]\n" +
" }\n" +
" }\n" +
"}";
String kind = "tenant:test:test:1.0.0";
HttpResponse httpResponse = new HttpResponse();
httpResponse.setResponseCode(HttpStatus.OK.value());
httpResponse.setBody(validSchemaFromSchemaService);
when(this.urlFetchService.sendRequest(any())).thenReturn(httpResponse);
String recordSchemaResponse = this.sut.getSchema(kind);
Map<String, Object> result = objectMapper.readValue(recordSchemaResponse,
new TypeReference<Map<String,Object>>(){});
assertEquals("Schema must have two root items", 2, result.size());
assertEquals("Wrong kind", "tenant:test:test:1.0.0", result.get("kind"));
assertEquals("Wrong schema attributes", "[{path=WellID, kind=link}]", result.get("schema").toString());
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(any())).thenReturn(httpResponse);
String recordSchemaResponse = this.sut.getSchema(kind);
assertNull(recordSchemaResponse);
}
@Test
public void should_call_Storage_then_Schema() throws Exception {
String kind = "tenant:test:test:1.0.0";
SchemaProviderImpl schemaService = Mockito.mock(SchemaProviderImpl.class);
when(schemaService.getSchema(any())).thenCallRealMethod();
InOrder inOrder = inOrder(schemaService);
String recordSchemaResponse = schemaService.getSchema(kind);
assertNull(recordSchemaResponse);
inOrder.verify(schemaService).getSchema(any());
inOrder.verify(schemaService).getFromStorageService(any());
inOrder.verify(schemaService).getFromSchemaService(any());
verify(schemaService, times(1)).getFromStorageService(any());
verify(schemaService, times(1)).getFromSchemaService(any());
}
@Test
public void should_call_only_Storage_if_it_returns_result() throws Exception {
String kind = "tenant:test:test:1.0.0";
SchemaProviderImpl schemaService = Mockito.mock(SchemaProviderImpl.class);
when(schemaService.getSchema(any())).thenCallRealMethod();
String someSchema = "some schema";
when(schemaService.getFromStorageService(any())).thenReturn(someSchema);
InOrder inOrder = inOrder(schemaService);
String recordSchemaResponse = schemaService.getSchema(kind);
assertEquals(recordSchemaResponse, someSchema);
inOrder.verify(schemaService).getSchema(any());
inOrder.verify(schemaService).getFromStorageService(any());
verify(schemaService, times(0)).getFromSchemaService(any());
verify(schemaService, times(1)).getFromStorageService(any());
}
}
......@@ -169,6 +169,51 @@ public class StorageServiceTest {
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())).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())).thenReturn(httpResponse);
String recordSchemaResponse = this.sut.getStorageSchema(kind);
assertNull(recordSchemaResponse);
}
@Test
public void should_returnOneValidRecords_givenValidData_getValidStorageRecordsWithInvalidConversionTest() throws URISyntaxException {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment