Commit 0b564709 authored by harshit aggarwal's avatar harshit aggarwal
Browse files

Addressing PR Comments

parent 17dab800
Pipeline #18253 failed with stages
in 20 seconds
......@@ -56,7 +56,7 @@ public class ProcessWKSTransform {
LOGGER.error("Invalid format for message with id: {}", message.getMessageId());
}
catch (Exception e) {
LOGGER.error("Exception encountered processing the message with id: {}", message.getMessageId());
LOGGER.error("Exception encountered processing the message with id: {}", message.getMessageId(), e);
}
ThreadScopeContextHolder.getContext().clear();
......
......@@ -38,7 +38,7 @@ public class StatusStoreServiceImpl implements StatusStoreService {
cosmosStore.upsertItem(requestIdentity.getDataPartitionId(), cosmosContainerConfig.getDatabase(), cosmosContainerConfig.getRelationshipStatusContainer(), doc.getId(), doc);
}
catch (AppException exception) {
LOGGER.error("Status update failed for Record ID: {} and Mapping ID: {} with exception {}", relationshipStatus.getRawRecordId(), relationshipStatus.getMappingId(), exception.getError().getMessage());
LOGGER.error("Status update failed for Record ID: {} and Mapping ID: {} with exception {}", relationshipStatus.getRawRecordId(), relationshipStatus.getMappingId(), exception.getError().getMessage(), exception);
}
}
}
......
......@@ -32,6 +32,11 @@ public class Constants {
public static final String COULD_NOT_TRANSFORM = "Could not transform";
public static final String TRANFORMATION_PROCESS_COMPLETED = "Transformation process completed";
public static final String NOT_CREATE_OR_UPDATE_OPERATION = "%d out of %d records do not have create or update operation code";
public static final int AUTHORITY_INDEX = 0;
public static final int SOURCE_INDEX = 1;
public static final int ENTITY_INDEX = 2;
public static final int VERSION_INDEX = 3;
public static final String POST_SCHEMA_ENDPOINT = "/schema";
private Constants() {
......
package org.opengroup.osdu.wks.enums;
public enum SchemaScope {
INTERNAL, SHARED
}
package org.opengroup.osdu.wks.enums;
public enum SchemaStatus {
PUBLISHED, OBSOLETE, DEVELOPMENT
}
package org.opengroup.osdu.wks.model;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Getter
@Setter
@ToString
public class SchemaIdentity {
private String authority;
private String source;
private String entityType;
private String schemaVersionMajor;
private String schemaVersionMinor;
private String schemaVersionPatch;
private String id;
}
\ No newline at end of file
package org.opengroup.osdu.wks.model;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.opengroup.osdu.wks.enums.SchemaScope;
import org.opengroup.osdu.wks.enums.SchemaStatus;
import java.util.Date;
@Getter
@Setter
@ToString
public class SchemaInfo {
private SchemaIdentity schemaIdentity;
private String createdBy;
private Date dateCreated;
private SchemaStatus status;
private SchemaScope scope;
private SchemaIdentity supersededBy;
}
package org.opengroup.osdu.wks.model;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Getter
@Setter
@ToString
public class SchemaInfoResponse {
SchemaInfo[] schemaInfos;
int offset;
int count;
int totalCount;
}
package org.opengroup.osdu.wks.service;
import org.opengroup.osdu.wks.constants.Constants;
import org.opengroup.osdu.wks.model.MappingInfo;
import org.opengroup.osdu.wks.model.MappingsModel;
import org.opengroup.osdu.wks.provider.interfaces.MappingStore;
import org.opengroup.osdu.wks.util.KindUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
......@@ -13,20 +13,18 @@ import java.util.List;
@Service
public class MappingServiceImpl implements MappingService {
private static final int AUTHORITY_INDEX = 0;
private static final int SOURCE_INDEX = 1;
private static final int ENTITY_INDEX = 2;
private static final int VERSION_INDEX = 3;
@Autowired
private MappingStore mappingStore;
@Autowired
private KindUtil kindUtil;
@Override
public List<MappingsModel> getMapping(String kind) {
String authorityName = retrieveAuthorityName(kind);
String entityNamePrefix = retrieveEntityName(kind);
String sourceName = retrieveSourceName(kind);
String majorVersion = retrieveMajorVersion(kind);
String authorityName = kindUtil.retrieveAuthorityName(kind);
String entityNamePrefix = kindUtil.retrieveEntityName(kind);
String sourceName = kindUtil.retrieveSourceName(kind);
String majorVersion = kindUtil.retrieveMajorVersion(kind);
List<MappingInfo> mappingInfos = mappingStore.getMappingInfo(authorityName, entityNamePrefix, sourceName, majorVersion);
if(mappingInfos.isEmpty()) {
return Collections.emptyList();
......@@ -34,25 +32,4 @@ public class MappingServiceImpl implements MappingService {
return mappingStore.getMappingFilesFromStorage(mappingInfos);
}
private String retrieveAuthorityName(String kind) {
String[] idArr = kind.split(Constants.COLON_SEPARATOR);
return idArr[AUTHORITY_INDEX];
}
private String retrieveEntityName(String id) {
String[] idArr = id.split(Constants.COLON_SEPARATOR);
return idArr[ENTITY_INDEX];
}
private String retrieveSourceName(String id) {
String[] idArr = id.split(Constants.COLON_SEPARATOR);
return idArr[SOURCE_INDEX];
}
private String retrieveMajorVersion(String kind) {
String[] idArr = kind.split(Constants.COLON_SEPARATOR);
String version = idArr[VERSION_INDEX];
return version.split("\\.")[0];
}
}
package org.opengroup.osdu.wks.service;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpStatus;
import org.apache.http.client.utils.URIBuilder;
import org.opengroup.osdu.core.common.model.tenant.TenantInfo;
......@@ -13,6 +11,8 @@ import org.opengroup.osdu.wks.constants.Constants;
import org.opengroup.osdu.wks.exceptions.ApplicationException;
import org.opengroup.osdu.wks.model.RequestParameters;
import org.opengroup.osdu.wks.model.RestResponse;
import org.opengroup.osdu.wks.model.SchemaInfo;
import org.opengroup.osdu.wks.model.SchemaInfoResponse;
import org.opengroup.osdu.wks.provider.interfaces.UserCredential;
import org.opengroup.osdu.wks.util.KindUtil;
import org.opengroup.osdu.wks.util.RestClient;
......@@ -41,7 +41,8 @@ public class SchemaService implements ISchemaService{
@Autowired
private RequestIdentity requestIdentity;
private final String schemaServiceUrl = getEnvironmentVariableOrDefaultValue("SCHEMA_API", "https://domain-name.org") + "/schema";
@Autowired
private ServiceConfiguration serviceConfig;
@Autowired
private KindUtil kindUtil;
......@@ -71,35 +72,37 @@ public class SchemaService implements ISchemaService{
}
}
private List<String> extractResponseBody(String response) {
private List<String> extractResponseBody(String response) throws ApplicationException {
List<String> kinds = new ArrayList<>();
Gson gsn = new Gson();
JsonObject schemaInfoList = gsn.fromJson(response, JsonObject.class);
JsonArray root = (JsonArray) schemaInfoList.get("schemaInfos");
if (root.size() > 0) {
for (JsonElement eachSchemaInfo : root) {
JsonObject schemaIdentity_ForEachSchemaInfo = (JsonObject) eachSchemaInfo.getAsJsonObject()
.get("schemaIdentity");
try {
List<String> kinds = new ArrayList<>();
ObjectMapper mapper = new ObjectMapper();
SchemaInfoResponse schemaInfoResponse = mapper.readValue(response, SchemaInfoResponse.class);
SchemaInfo[] schemaInfoList = schemaInfoResponse.getSchemaInfos();
for(SchemaInfo schemaInfo : schemaInfoList) {
kinds.add(kindUtil.prepareKind(
schemaIdentity_ForEachSchemaInfo.get("authority").getAsString(),
schemaIdentity_ForEachSchemaInfo.get("source").getAsString(),
schemaIdentity_ForEachSchemaInfo.get("entityType").getAsString(),
schemaIdentity_ForEachSchemaInfo.get("schemaVersionMajor").getAsString(),
schemaIdentity_ForEachSchemaInfo.get("schemaVersionMinor").getAsString(),
schemaIdentity_ForEachSchemaInfo.get("schemaVersionPatch").getAsString()
));
schemaInfo.getSchemaIdentity().getAuthority(),
schemaInfo.getSchemaIdentity().getSource(),
schemaInfo.getSchemaIdentity().getEntityType(),
schemaInfo.getSchemaIdentity().getSchemaVersionMajor(),
schemaInfo.getSchemaIdentity().getSchemaVersionMinor(),
schemaInfo.getSchemaIdentity().getSchemaVersionPatch()
));
}
return kinds;
}
catch (JsonProcessingException exception) {
throw new ApplicationException(exception.getMessage(), exception.getCause());
}
return kinds;
}
private String getRequestURL(String kind) throws ApplicationException {
try {
String[] kindArr = kind.split(Constants.COLON_SEPARATOR);
String majorVersion = kindUtil.getMajorVersionFromKind(kind).toString();
URIBuilder uriBuilder = new URIBuilder(schemaServiceUrl);
String schemaUrl = serviceConfig.getSchemaServiceUrl() + Constants.POST_SCHEMA_ENDPOINT;
URIBuilder uriBuilder = new URIBuilder(schemaUrl);
uriBuilder.addParameter("authority", kindArr[0]);
uriBuilder.addParameter("source", kindArr[1]);
uriBuilder.addParameter("entityType", kindArr[2]);
......@@ -109,16 +112,9 @@ public class SchemaService implements ISchemaService{
return uriBuilder.build().toString();
}
catch (URISyntaxException exception) {
throw new ApplicationException(exception.getMessage());
throw new ApplicationException(exception.getMessage(), exception.getCause());
}
}
private static String getEnvironmentVariableOrDefaultValue(String key, String defaultValue) {
String environmentVariable = System.getProperty(key, System.getenv(key));
if (environmentVariable == null) {
environmentVariable = defaultValue;
}
return environmentVariable;
}
}
package org.opengroup.osdu.wks.service;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
@Configuration
@Getter
class ServiceConfiguration {
@Value("${SCHEMA_API}")
private String schemaServiceUrl;
}
......@@ -24,4 +24,25 @@ public class KindUtil {
return String.join(Constants.POINT, kindUptoMajorVersion, minorVersion, patchVersion);
}
public String retrieveAuthorityName(String kind) {
String[] idArr = kind.split(Constants.COLON_SEPARATOR);
return idArr[Constants.AUTHORITY_INDEX];
}
public String retrieveEntityName(String id) {
String[] idArr = id.split(Constants.COLON_SEPARATOR);
return idArr[Constants.ENTITY_INDEX];
}
public String retrieveSourceName(String id) {
String[] idArr = id.split(Constants.COLON_SEPARATOR);
return idArr[Constants.SOURCE_INDEX];
}
public String retrieveMajorVersion(String kind) {
String[] idArr = kind.split(Constants.COLON_SEPARATOR);
String version = idArr[Constants.VERSION_INDEX];
return version.split("\\.")[0];
}
}
......@@ -7,6 +7,7 @@ import org.mockito.Mock;
import org.opengroup.osdu.wks.model.MappingInfo;
import org.opengroup.osdu.wks.model.MappingsModel;
import org.opengroup.osdu.wks.provider.interfaces.MappingStore;
import org.opengroup.osdu.wks.util.KindUtil;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.Collections;
......@@ -23,11 +24,18 @@ public class MappingServiceImplTest {
@Mock
private MappingStore mappingStore;
@Mock
private KindUtil kindUtil;
@InjectMocks
private MappingServiceImpl mappingService;
@Test
public void testGetMappingInfo(){
when(kindUtil.retrieveAuthorityName(KIND)).thenReturn("tenant1");
when(kindUtil.retrieveSourceName(KIND)).thenReturn("source");
when(kindUtil.retrieveEntityName(KIND)).thenReturn("entity");
when(kindUtil.retrieveMajorVersion(KIND)).thenReturn("1");
MappingInfo mappingInfo = new MappingInfo();
when(mappingStore.getMappingInfo("tenant1", "source", "entity", "1")).thenReturn(Collections.singletonList(mappingInfo));
List<MappingsModel> mappings = mappingService.getMapping(KIND);
......
package org.opengroup.osdu.wks.service;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.datatree.Tree;
import lombok.extern.java.Log;
import org.apache.http.HttpStatus;
import org.junit.jupiter.api.BeforeEach;
......@@ -21,16 +18,11 @@ import org.opengroup.osdu.wks.model.RestResponse;
import org.opengroup.osdu.wks.provider.interfaces.UserCredential;
import org.opengroup.osdu.wks.util.KindUtil;
import org.opengroup.osdu.wks.util.RestClient;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpMethod;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
......@@ -48,14 +40,12 @@ public class SchemaServiceTest {
private static final String CORRELATION_ID = "someCorrelationId";
private static final String DATA_PARTITION_ID = "someDataPartitionId";
private static final String TOKEN = "Some token";
private static final String RAW_RECORD_FILE_NAME = "schema_service_response.json";
private static final String UNIT_TEST_RESOURCES_FOLDER_PATH = "/unit_test_resources/";
private static final String EMPTY_MESSAGE = "";
private static final String TEST_KIND = "opendes:wks:wellbore:1.2.3";
private static final String AUTHORITY = "opendes";
private static final String SOURCE = "wks";
private static final String ENTITY_TYPE = "wellbore";
private static final String TEST_URL = "https://domain-name.org";
@InjectMocks
private SchemaService schemaService;
......@@ -81,19 +71,22 @@ public class SchemaServiceTest {
@Mock
private RestResponse restResponse;
@Mock
private ServiceConfiguration serviceConfig;
@BeforeEach
public void init() throws ApplicationException {
lenient().when(requestIdentity.getCorrelationId()).thenReturn(CORRELATION_ID);
lenient().when(requestIdentity.getDataPartitionId()).thenReturn(DATA_PARTITION_ID);
lenient().when(tenantFactory.getTenantInfo(eq(DATA_PARTITION_ID))).thenReturn(tenantInfo);
lenient().when(userCredential.getIdToken(any(TenantInfo.class))).thenReturn(TOKEN);
lenient().when(serviceConfig.getSchemaServiceUrl()).thenReturn(TEST_URL);
}
@Test
public void testSuccessfulResponse() throws ApplicationException {
Tree rawRecordTree = findJsonTree(RAW_RECORD_FILE_NAME);
String messageBody = rawRecordTree.asString();
String messageBody = "{\"schemaInfos\":[{\"schemaIdentity\":{\"authority\":\"opendes\",\"source\":\"wks\",\"entityType\":\"wellbore\",\"schemaVersionMajor\":\"1\",\"schemaVersionMinor\":\"2\",\"schemaVersionPatch\":\"3\"}}]}";
List<String> expectedResult = new ArrayList<String>(Collections.singleton(TEST_KIND));
ArgumentCaptor<RequestParameters> httpRequestArgument = ArgumentCaptor.forClass(RequestParameters.class);
when(restResponse.getStatus()).thenReturn(HttpStatus.SC_OK);
......@@ -141,27 +134,4 @@ public class SchemaServiceTest {
}
private static Tree findJsonTree(String fileName) {
File file;
Map<String, Object> jsonMap = null;
try {
file = findFile(fileName);
jsonMap = jsonFileToMap(file);
} catch (IOException e) {
log.severe("Could not parse json file");
}
return new Tree(jsonMap);
}
private static Map<String, Object> jsonFileToMap(File file) throws IOException {
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(file, new TypeReference<Map<String, Object>>() {
});
}
private static File findFile(String fileName) throws IOException {
Resource resource = new ClassPathResource(UNIT_TEST_RESOURCES_FOLDER_PATH + fileName);
return resource.getFile();
}
}
Supports Markdown
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