Commit 0fa52d83 authored by ethiraj krishnamanaidu's avatar ethiraj krishnamanaidu
Browse files

Merge branch 'latest_version_schema_changes' into 'master'

Latest version schema changes

See merge request !16
parents f510f529 d74925fc
Pipeline #5971 passed with stages
in 7 minutes and 47 seconds
......@@ -2,10 +2,12 @@ package org.opengroup.osdu.schema.service.serviceimpl;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.TreeMap;
import java.util.Map.Entry;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
......@@ -32,7 +34,6 @@ import org.opengroup.osdu.schema.service.ISchemaService;
import org.opengroup.osdu.schema.service.ISourceService;
import org.opengroup.osdu.schema.util.SchemaResolver;
import org.opengroup.osdu.schema.util.SchemaUtil;
import org.opengroup.osdu.schema.util.VersionHierarchyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
......@@ -74,7 +75,7 @@ public class SchemaService implements ISchemaService {
@Autowired
DpsHeaders headers;
/**
* Method to get schema
*
......@@ -246,8 +247,11 @@ public class SchemaService implements ISchemaService {
if (queryParams.getLatestVersion() != null && queryParams.getLatestVersion()) {
schemaList = getLatestVersionSchemaList(schemaList);
}
Comparator<SchemaInfo> compareByCreatedDate = (s1,s2) -> s1.getDateCreated().compareTo(s2.getDateCreated());
List<SchemaInfo> schemaFinalList = schemaList.stream().skip(queryParams.getOffset())
.sorted(compareByCreatedDate)
.limit(queryParams.getLimit()).collect(Collectors.toList());
return SchemaInfoResponse.builder().schemaInfos(schemaFinalList).count(schemaFinalList.size())
......@@ -256,6 +260,7 @@ public class SchemaService implements ISchemaService {
private void getSchemaInfos(QueryParams queryParams, List<SchemaInfo> schemaList, String tenant)
throws ApplicationException {
schemaInfoStore.getSchemaInfoList(queryParams, tenant).forEach(schemaList::add);
}
......@@ -287,46 +292,61 @@ public class SchemaService implements ISchemaService {
private List<SchemaInfo> getLatestVersionSchemaList(List<SchemaInfo> filteredSchemaList) {
List<SchemaInfo> latestSchemaList = new LinkedList<>();
SchemaInfo previousSchemaInfo = null;
TreeMap<VersionHierarchyUtil, SchemaInfo> sortedMap = new TreeMap<>(
new VersionHierarchyUtil.SortingVersionComparator());
for (SchemaInfo schemaInfoObject : filteredSchemaList) {
if ((previousSchemaInfo != null) && !(checkAuthorityMatch(previousSchemaInfo, schemaInfoObject)
&& checkSourceMatch(previousSchemaInfo, schemaInfoObject)
&& checkEntityMatch(previousSchemaInfo, schemaInfoObject))) {
Entry<VersionHierarchyUtil, SchemaInfo> latestVersionEntry = sortedMap.firstEntry();
latestSchemaList.add(latestVersionEntry.getValue());
sortedMap.clear();
}
previousSchemaInfo = schemaInfoObject;
SchemaIdentity schemaIdentity = schemaInfoObject.getSchemaIdentity();
VersionHierarchyUtil version = new VersionHierarchyUtil(schemaIdentity.getSchemaVersionMajor(),
schemaIdentity.getSchemaVersionMinor(), schemaIdentity.getSchemaVersionPatch());
sortedMap.put(version, schemaInfoObject);
}
if (sortedMap.size() != 0) {
Entry<VersionHierarchyUtil, SchemaInfo> latestVersionEntry = sortedMap.firstEntry();
latestSchemaList.add(latestVersionEntry.getValue());
}
return latestSchemaList;
List<SchemaInfo> latestSchemaList = new ArrayList<>();
Map<String, SchemaInfo> latestSchemaMap = new HashMap<>();
for(SchemaInfo schemaInfo :filteredSchemaList) {
String key = getGroupingKey(schemaInfo);
latestSchemaMap.computeIfAbsent(key, k -> schemaInfo);
SchemaInfo value = latestSchemaMap.get(key);
if(compareSchemaVersion(schemaInfo, value) >= 0)
latestSchemaMap.put(key, schemaInfo);
}
latestSchemaList.addAll(latestSchemaMap.values());
return latestSchemaList;
}
private boolean checkEntityMatch(SchemaInfo previousSchemaInfo, SchemaInfo schemaInfoObject) {
return schemaInfoObject.getSchemaIdentity().getEntityType()
.equalsIgnoreCase(previousSchemaInfo.getSchemaIdentity().getEntityType());
}
private boolean checkSourceMatch(SchemaInfo previousSchemaInfo, SchemaInfo schemaInfoObject) {
return schemaInfoObject.getSchemaIdentity().getSource()
.equalsIgnoreCase(previousSchemaInfo.getSchemaIdentity().getSource());
}
private boolean checkAuthorityMatch(SchemaInfo previousSchemaInfo, SchemaInfo schemaInfoObject) {
return schemaInfoObject.getSchemaIdentity().getAuthority()
.equalsIgnoreCase(previousSchemaInfo.getSchemaIdentity().getAuthority());
}
}
/***
* This method creates a key based on Athority:Source:EntityType
*
* @param schemaInfo SchemaInfo whose key is to be formed
* @return String based key formed using Athority:Source:EntityType
*/
private String getGroupingKey(SchemaInfo schemaInfo){
return String.join(":", schemaInfo.getSchemaIdentity().getAuthority(),
schemaInfo.getSchemaIdentity().getSource(),
schemaInfo.getSchemaIdentity().getEntityType());
}
/****
* This method compares the schema versions of two SchemaInfo attribute. The comparison is done based on the following order <br>
* 1. Major Version <br>
* 2. Minor Version <br>
* 3. Patch Version <br>
*
* @param scInfo1 SchemaInfo version
* @param scInfo2 SchemaInfo
* @return Returns positive integer if version of scInfo1 is greater than version of scInfo2
*/
private int compareSchemaVersion(SchemaInfo scInfo1, SchemaInfo scInfo2){
Comparator<SchemaInfo> compareByMajor =
(s1,s2) -> s1.getSchemaIdentity().getSchemaVersionMajor().compareTo(s2.getSchemaIdentity().getSchemaVersionMajor());
Comparator<SchemaInfo> compareByMinor =
(s1,s2) -> s1.getSchemaIdentity().getSchemaVersionMinor().compareTo(s2.getSchemaIdentity().getSchemaVersionMinor());
Comparator<SchemaInfo> compareByPatch =
(s1,s2) -> s1.getSchemaIdentity().getSchemaVersionPatch().compareTo(s2.getSchemaIdentity().getSchemaVersionPatch());
return compareByMajor.thenComparing(compareByMinor).thenComparing(compareByPatch).compare(scInfo1, scInfo2);
}
}
\ No newline at end of file
......@@ -112,27 +112,55 @@ public class SchemaServiceStepDef_PUT implements En {
});
Given("I hit schema service PUT API with {string}, data-partition-id as {string} for superceded input",
(String inputPayload, String tenant) -> {
(String inputPayload, String tenant) -> {
tenant = selectTenant(tenant);
String body = this.context.getFileUtils().read(inputPayload);
JsonElement jsonBody = new Gson().fromJson(body, JsonElement.class);
String newSchemaStr = this.context.getFileUtils().read(inputPayload);
JsonElement newSchemaJsonBody = new Gson().fromJson(newSchemaStr, JsonElement.class);
int currentMinorVersion = Integer.parseInt(this.context.getSchemaVersionMinor());
int currentMajorVersion = Integer.parseInt(this.context.getSchemaVersionMajor());
String id = "SchemaSanityTest:testSource:testEntity:" + currentMajorVersion + "."
+ currentMinorVersion + ".0";
String supersededById = jsonBody.getAsJsonObject().getAsJsonObject("schemaInfo").get("supersededBy")
.getAsJsonObject().get("id").getAsString();
updateVersionInJsonBody(jsonBody, currentMinorVersion, currentMajorVersion, id);
this.context.setSchemaIdFromInputPayload(id);
this.context.setSupersededById(supersededById);
body = new Gson().toJson(jsonBody);
int patchMajorVersion = Integer.parseInt(this.context.getSchemaVersionPatch());
String latestSchemaResp = this.context.getHttpResponse().getBody();
JsonElement latestSchemaRespJsonBody = new Gson().fromJson(latestSchemaResp, JsonElement.class);
JsonElement supersededByBody = new Gson().fromJson(latestSchemaRespJsonBody.getAsJsonObject().getAsJsonArray("schemaInfos")
.get(0).getAsJsonObject().getAsJsonObject(TestConstants.SCHEMA_IDENTITY).toString(), JsonElement.class);
String newID = "SchemaSanityTest:testSource:testEntity:" + (currentMajorVersion+1) + "."
+ currentMinorVersion + "."+patchMajorVersion;
String supersededById = "SchemaSanityTest:testSource:testEntity:" + currentMajorVersion + "."
+ currentMinorVersion + "."+patchMajorVersion;
updateVersionInJsonBody(newSchemaJsonBody, currentMinorVersion, currentMajorVersion+1, newID);
newSchemaStr = new Gson().toJson(newSchemaJsonBody);
Map<String, String> headers = this.context.getAuthHeaders();
headers.put(TestConstants.DATA_PARTITION_ID, tenant);
//Create new Schema
HttpRequest httpPostRequest = HttpRequest.builder().url(TestConstants.HOST + TestConstants.POST_ENDPOINT)
.body(newSchemaStr).httpMethod(HttpRequest.POST).requestHeaders(this.context.getAuthHeaders())
.build();
HttpResponse postResponse = HttpClientFactory.getInstance().send(httpPostRequest);
assertEquals(201, postResponse.getCode());
//Update with superceded by ID
String postSchemaBody = postResponse.getBody();
JsonElement postSchemaJsonBody = new Gson().fromJson(postSchemaBody, JsonElement.class);
postSchemaJsonBody.getAsJsonObject().add(TestConstants.SUPERSEDED_BY, supersededByBody);
JsonObject putRequest = new JsonObject();
putRequest.add("schemaInfo", postSchemaJsonBody);
putRequest.add("schema", new Gson().fromJson("{}", JsonElement.class));
this.context.setSchemaIdFromInputPayload(newID);
this.context.setSupersededById(supersededById);
HttpRequest httpRequest = HttpRequest.builder().url(TestConstants.HOST + TestConstants.PUT_ENDPOINT)
.body(body).httpMethod(HttpRequest.PUT).requestHeaders(this.context.getAuthHeaders())
.body(putRequest.toString()).httpMethod(HttpRequest.PUT).requestHeaders(this.context.getAuthHeaders())
.build();
HttpResponse response = HttpClientFactory.getInstance().send(httpRequest);
this.context.setHttpResponse(response);
this.context.setHttpResponse(response);
});
Given("I hit schema service PUT API with {string}, data-partition-id as {string} with increased minor version only",
......@@ -211,14 +239,11 @@ public class SchemaServiceStepDef_PUT implements En {
assertEquals(ReponseStatusCode, String.valueOf(response.getCode()));
});
Then("the put service for supersededBy should respond back with {string} and {string}",
(String ReponseStatusCode, String ResponseMessage) -> {
String body = this.context.getFileUtils().read(ResponseMessage);
JsonObject jsonBody = new Gson().fromJson(body, JsonObject.class);
Then("the put service for supersededBy should respond back with {string}",
(String ReponseStatusCode) -> {
HttpResponse response = this.context.getHttpResponse();
if (response != null) {
assertEquals(ReponseStatusCode, String.valueOf(response.getCode()));
commonAssertion(response, jsonBody);
Assert.assertNotNull(getResponseValue(TestConstants.SUPERSEDED_BY));
assertEquals(
getResponseValue(TestConstants.SCHEMA_IDENTITY + TestConstants.DOT + TestConstants.ID),
......
......@@ -163,20 +163,20 @@ Feature: To verify functionality of PUT schema Service
@SchemaService
Scenario Outline: Verify that create Schema Service supersededBy functionality work correctly
Given I hit schema service PUT API for supersededBy with <InputPayload> and data-partition-id as <tenant>
Then the put service for supersededBy should respond back with <ReponseStatusCode> and <ResponseMessage>
Then the put service for supersededBy should respond back with <ReponseStatusCode>
Examples:
| parameter | value | latestVersion | InputPayload | tenant | ReponseStatusCode | ResponseMessage |
| "authority" | "SchemaSanityTest" | "true" | "/input_payloads/supercededInputPayload_positive.json" | "TENANT1" | "201" | "/output_payloads/SchemaPost_PrivateScope_SuccessfulCreation.json" |
| parameter | value | latestVersion | InputPayload | tenant | ReponseStatusCode |
| "authority" | "SchemaSanityTest" | "true" | "/input_payloads/supercededInputPayload_positive.json" | "TENANT1" | "201" |
@SchemaService
Scenario Outline: Verify that update Schema Service supersededBy functionality work correctly
Given I hit schema service PUT API with <InputPayload>, data-partition-id as <tenant> for superceded input
Then the put service for supersededBy should respond back with <ReponseStatusCode> and <ResponseMessage>
Then the put service for supersededBy should respond back with <ReponseStatusCode>
Examples:
| parameter | value | latestVersion | InputPayload | tenant | ReponseStatusCode | ResponseMessage |
| "authority" | "SchemaSanityTest" | "true" | "/input_payloads/supercededInputPayload_positive.json" | "TENANT1" | "200" | "/output_payloads/SchemaPost_PrivateScope_SuccessfulCreation.json" |
| parameter | value | latestVersion | InputPayload | tenant | ReponseStatusCode |
| "authority" | "SchemaSanityTest" | "true" | "/input_payloads/postSchemaService_EmptySchema.json" | "TENANT1" | "200" |
@SchemaService
Scenario Outline: Verify that Schema Service's PUT API throws correct error if input payload is not valid
......
......@@ -22,5 +22,5 @@
"status": "DEVELOPMENT",
"scope": "INTERNAL"
},
"schema": {"name": "ABHISHEK2"}
"schema": {"name": "Test"}
}
\ No newline at end of file
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