From 04806c85c7b90c2935af0e91fbdae5957e9e9b0d Mon Sep 17 00:00:00 2001 From: Abhishek Kumar Date: Thu, 13 Aug 2020 22:56:10 +0530 Subject: [PATCH 1/3] Changes related to finding latest schema version --- .../service/serviceimpl/SchemaService.java | 120 +++++--- .../serviceimpl/SchemaServiceTest.java | 278 +++++++++++------- .../stepdefs/SchemaServiceStepDef_PUT.java | 54 +++- .../IntegrationTest_SchemaService_PUT.feature | 12 +- .../supercededInputPayload_positive.json | 2 +- 5 files changed, 300 insertions(+), 166 deletions(-) diff --git a/schema-core/src/main/java/org/opengroup/osdu/schema/service/serviceimpl/SchemaService.java b/schema-core/src/main/java/org/opengroup/osdu/schema/service/serviceimpl/SchemaService.java index 5bf462a3..4ed72e93 100644 --- a/schema-core/src/main/java/org/opengroup/osdu/schema/service/serviceimpl/SchemaService.java +++ b/schema-core/src/main/java/org/opengroup/osdu/schema/service/serviceimpl/SchemaService.java @@ -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 * @@ -256,7 +257,12 @@ public class SchemaService implements ISchemaService { private void getSchemaInfos(QueryParams queryParams, List schemaList, String tenant) throws ApplicationException { + + long start = System.currentTimeMillis(); schemaInfoStore.getSchemaInfoList(queryParams, tenant).forEach(schemaList::add); + long end = System.currentTimeMillis(); + log.info("Total schemas fetched :"+(null == schemaList ? 0 : schemaList.size())); + log.info("Time lapsed to fetch schemas :"+(end-start)+" ms"); } private void latestVersionMajorMinorFiltersCheck(QueryParams queryParams) throws BadRequestException { @@ -287,46 +293,68 @@ public class SchemaService implements ISchemaService { private List getLatestVersionSchemaList(List filteredSchemaList) { - List latestSchemaList = new LinkedList<>(); - SchemaInfo previousSchemaInfo = null; - TreeMap sortedMap = new TreeMap<>( - new VersionHierarchyUtil.SortingVersionComparator()); - - for (SchemaInfo schemaInfoObject : filteredSchemaList) { - if ((previousSchemaInfo != null) && !(checkAuthorityMatch(previousSchemaInfo, schemaInfoObject) - && checkSourceMatch(previousSchemaInfo, schemaInfoObject) - && checkEntityMatch(previousSchemaInfo, schemaInfoObject))) { - Entry 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 latestVersionEntry = sortedMap.firstEntry(); - latestSchemaList.add(latestVersionEntry.getValue()); - } - - 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()); + + long start = System.currentTimeMillis(); + List latestSchemaList = new ArrayList<>(); + Map 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()); + + long end = System.currentTimeMillis(); + + log.info("Latest schema version size :"+(null == latestSchemaList ? 0 : latestSchemaList.size())); + log.info("Time lapsed to fetch latest version schemas :"+(end-start)+" ms"); + + return latestSchemaList; } - - 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 schemaInfo.getSchemaIdentity().getAuthority()+":" + + schemaInfo.getSchemaIdentity().getSource()+":" + + schemaInfo.getSchemaIdentity().getEntityType(); + + } + + /**** + * This method compares the schema versions of two SchemaInfo attribute. The comparison is done based in following order
+ * 1. Major Version
+ * 2. Minor Version
+ * 3. Patch Version
+ * + * @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 compareByMajor = + (s1,s2) -> s1.getSchemaIdentity().getSchemaVersionMajor().compareTo(s2.getSchemaIdentity().getSchemaVersionMajor()); + + Comparator compareByMinor = + (s1,s2) -> s1.getSchemaIdentity().getSchemaVersionMinor().compareTo(s2.getSchemaIdentity().getSchemaVersionMinor()); + + Comparator 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 diff --git a/schema-core/src/test/java/org/opengroup/osdu/schema/service/serviceimpl/SchemaServiceTest.java b/schema-core/src/test/java/org/opengroup/osdu/schema/service/serviceimpl/SchemaServiceTest.java index b33dc086..e68cab24 100644 --- a/schema-core/src/test/java/org/opengroup/osdu/schema/service/serviceimpl/SchemaServiceTest.java +++ b/schema-core/src/test/java/org/opengroup/osdu/schema/service/serviceimpl/SchemaServiceTest.java @@ -5,6 +5,7 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.fail; import static org.mockito.Mockito.when; +import java.util.Date; import java.util.LinkedList; import java.util.List; @@ -74,10 +75,11 @@ public class SchemaServiceTest { @Mock JaxRsDpsLog log; + + private Date currDate = new Date(); @Rule public ExpectedException expectedException = ExpectedException.none(); - String bucketName = "evd-ddl-us-services-schema"; @Test public void testGetSchema_EmptySchemaId() throws BadRequestException, NotFoundException, ApplicationException { @@ -126,6 +128,10 @@ public class SchemaServiceTest { throws ApplicationException, NotFoundException, BadRequestException, JsonProcessingException { String dataPartitionId = "tenant"; String schemaId = "os:wks:well:1.1.1"; + + SchemaRequest schReqPubInt = getMockSchemaObject_published_InternalScope(); + SchemaInfo schInfo = getMockSchemaInfo_Published_InternalScope(); + when(schemaInfoStore.isUnique(schemaId, "common")).thenReturn(true); when(schemaInfoStore.isUnique(schemaId, "tenant")).thenReturn(true); when(headers.getPartitionId()).thenReturn(dataPartitionId); @@ -133,20 +139,19 @@ public class SchemaServiceTest { .thenThrow(NotFoundException.class); Mockito.when(schemaStore.getSchema(dataPartitionId, schemaId)).thenThrow(NotFoundException.class); Mockito.when(authorityService.checkAndRegisterAuthorityIfNotPresent( - getMockSchemaObject_published_InternalScope().getSchemaInfo().getSchemaIdentity().getAuthority())) + schReqPubInt.getSchemaInfo().getSchemaIdentity().getAuthority())) .thenReturn(true); Mockito.when(sourceService.checkAndRegisterSourceIfNotPresent( - getMockSchemaObject_published_InternalScope().getSchemaInfo().getSchemaIdentity().getSource())) + schReqPubInt.getSchemaInfo().getSchemaIdentity().getSource())) .thenReturn(true); Mockito.when(entityTypeService.checkAndRegisterEntityTypeIfNotPresent( - getMockSchemaObject_published_InternalScope().getSchemaInfo().getSchemaIdentity().getEntityType())) + schReqPubInt.getSchemaInfo().getSchemaIdentity().getEntityType())) .thenReturn(true); Mockito.when(schemaResolver.resolveSchema(Mockito.anyString())).thenReturn("{}"); Mockito.when(schemaStore.createSchema(Mockito.anyString(), Mockito.anyString())).thenReturn("{}"); - Mockito.when(schemaInfoStore.createSchemaInfo(getMockSchemaObject_published_InternalScope())) - .thenReturn(getMockSchemaInfo_Published_InternalScope()); + Mockito.when(schemaInfoStore.createSchemaInfo(schReqPubInt)).thenReturn(schInfo); assertEquals(SchemaStatus.PUBLISHED, - schemaService.createSchema(getMockSchemaObject_published_InternalScope()).getStatus()); + schemaService.createSchema(schReqPubInt).getStatus()); } @Test @@ -154,31 +159,37 @@ public class SchemaServiceTest { throws ApplicationException, NotFoundException, BadRequestException, JsonProcessingException { Mockito.when(headers.getPartitionId()).thenReturn("tenant"); String schemaId = "os:wks:well:1.1.1"; + + SchemaRequest schReq = getMockSchemaObject_published_InternalScope(); + SchemaInfo schInfo = getMockSchemaInfo_Published_InternalScope(); + when(schemaInfoStore.isUnique(schemaId, "common")).thenReturn(true); when(schemaInfoStore.isUnique(schemaId, "tenant")).thenReturn(true); Mockito.when(schemaStore.getSchema(Mockito.anyString(), Mockito.anyString())) .thenThrow(NotFoundException.class); Mockito.when(authorityService.checkAndRegisterAuthorityIfNotPresent( - getMockSchemaObject_published_InternalScope().getSchemaInfo().getSchemaIdentity().getAuthority())) + schReq.getSchemaInfo().getSchemaIdentity().getAuthority())) .thenReturn(true); Mockito.when(sourceService.checkAndRegisterSourceIfNotPresent( - getMockSchemaObject_published_InternalScope().getSchemaInfo().getSchemaIdentity().getSource())) + schReq.getSchemaInfo().getSchemaIdentity().getSource())) .thenReturn(true); Mockito.when(entityTypeService.checkAndRegisterEntityTypeIfNotPresent( - getMockSchemaObject_published_InternalScope().getSchemaInfo().getSchemaIdentity().getEntityType())) + schReq.getSchemaInfo().getSchemaIdentity().getEntityType())) .thenReturn(true); Mockito.when(schemaResolver.resolveSchema(Mockito.anyString())).thenReturn("{}"); Mockito.when(schemaStore.createSchema(Mockito.anyString(), Mockito.anyString())).thenReturn("{}"); - Mockito.when(schemaInfoStore.createSchemaInfo(getMockSchemaObject_published_InternalScope())) - .thenReturn(getMockSchemaInfo_Published_InternalScope()); - assertEquals(getMockSchemaInfo_Published_InternalScope(), - schemaService.createSchema(getMockSchemaObject_published_InternalScope())); + Mockito.when(schemaInfoStore.createSchemaInfo(schReq)) + .thenReturn(schInfo); + assertEquals(schInfo, schemaService.createSchema(schReq)); } @Test public void testCreateSchema_FailScenario_CleanUpScenario() throws ApplicationException, NotFoundException, BadRequestException, JsonProcessingException { expectedException.expect(ApplicationException.class); + + SchemaRequest schReq = getMockSchemaObject_published_InternalScope(); + Mockito.when(headers.getPartitionId()).thenReturn("tenant"); String schemaId = "os:wks:well:1.1.1"; when(schemaInfoStore.isUnique(schemaId, "common")).thenReturn(true); @@ -186,18 +197,18 @@ public class SchemaServiceTest { Mockito.when(schemaStore.getSchema(Mockito.anyString(), Mockito.anyString())) .thenThrow(NotFoundException.class); Mockito.when(authorityService.checkAndRegisterAuthorityIfNotPresent( - getMockSchemaObject_published_InternalScope().getSchemaInfo().getSchemaIdentity().getAuthority())) + schReq.getSchemaInfo().getSchemaIdentity().getAuthority())) .thenReturn(true); Mockito.when(sourceService.checkAndRegisterSourceIfNotPresent( - getMockSchemaObject_published_InternalScope().getSchemaInfo().getSchemaIdentity().getSource())) + schReq.getSchemaInfo().getSchemaIdentity().getSource())) .thenReturn(true); Mockito.when(entityTypeService.checkAndRegisterEntityTypeIfNotPresent( - getMockSchemaObject_published_InternalScope().getSchemaInfo().getSchemaIdentity().getEntityType())) + schReq.getSchemaInfo().getSchemaIdentity().getEntityType())) .thenReturn(true); Mockito.when(schemaResolver.resolveSchema(Mockito.anyString())).thenReturn("{}"); Mockito.when(schemaStore.createSchema(Mockito.anyString(), Mockito.anyString())) .thenThrow(ApplicationException.class); - schemaService.createSchema(getMockSchemaObject_published_InternalScope()); + schemaService.createSchema(schReq); } @Test @@ -205,24 +216,29 @@ public class SchemaServiceTest { throws JsonProcessingException, ApplicationException, BadRequestException, NotFoundException { String dataPartitionId = "common"; String schemaId = "os:wks:well:1.1.1"; + + SchemaRequest schReqInt = getMockSchemaObject_published_InternalScope(); + SchemaRequest schReqPub = getMockSchemaObject_published(); + SchemaInfo schInfoPub = getMockSchemaInfo_Published_SharedScope(); + Mockito.when(headers.getPartitionId()).thenReturn(dataPartitionId); when(schemaInfoStore.isUnique(Mockito.anyString(), Mockito.anyString())).thenReturn(true); Mockito.when(schemaStore.getSchema(SchemaConstants.ACCOUNT_ID_COMMON_PROJECT, schemaId)) .thenThrow(NotFoundException.class); Mockito.when(authorityService.checkAndRegisterAuthorityIfNotPresent( - getMockSchemaObject_published_InternalScope().getSchemaInfo().getSchemaIdentity().getAuthority())) + schReqInt.getSchemaInfo().getSchemaIdentity().getAuthority())) .thenReturn(true); Mockito.when(sourceService.checkAndRegisterSourceIfNotPresent( - getMockSchemaObject_published_InternalScope().getSchemaInfo().getSchemaIdentity().getSource())) + schReqInt.getSchemaInfo().getSchemaIdentity().getSource())) .thenReturn(true); Mockito.when(entityTypeService.checkAndRegisterEntityTypeIfNotPresent( - getMockSchemaObject_published_InternalScope().getSchemaInfo().getSchemaIdentity().getEntityType())) + schReqInt.getSchemaInfo().getSchemaIdentity().getEntityType())) .thenReturn(true); Mockito.when(schemaResolver.resolveSchema(Mockito.anyString())).thenReturn("{}"); Mockito.when(schemaStore.createSchema(Mockito.anyString(), Mockito.anyString())).thenReturn("{}"); - Mockito.when(schemaInfoStore.createSchemaInfo(getMockSchemaObject_published_SharedScope())) - .thenReturn(getMockSchemaInfo_Published_SharedScope()); - assertEquals(SchemaStatus.PUBLISHED, schemaService.createSchema(getMockSchemaObject_published()).getStatus()); + Mockito.when(schemaInfoStore.createSchemaInfo(schReqPub)).thenReturn(schInfoPub); + + assertEquals(SchemaStatus.PUBLISHED, schemaService.createSchema(schReqPub).getStatus()); } @@ -254,12 +270,13 @@ public class SchemaServiceTest { Mockito.when(schemaStore.getSchema(Mockito.anyString(), Mockito.anyString())) .thenThrow(NotFoundException.class); ObjectMapper mapper = new ObjectMapper(); - String inputSchema = mapper.writeValueAsString(getMockSchemaObject_BreakingChanges().getSchema()); + SchemaRequest schReqBreakingChange = getMockSchemaObject_BreakingChanges(); + String inputSchema = mapper.writeValueAsString(schReqBreakingChange.getSchema()); String latestSchema = "{\"key\":\"value\"}"; Mockito.doThrow(BadRequestException.class).when(SchemaUtil).checkBreakingChange(inputSchema, latestSchema); Mockito.when(schemaInfoStore.getLatestMinorVerSchema(getMockSchemaInfo_Published_InternalScope())) .thenReturn(latestSchema); - schemaService.createSchema(getMockSchemaObject_BreakingChanges()); + schemaService.createSchema(schReqBreakingChange); } @Test @@ -268,31 +285,34 @@ public class SchemaServiceTest { Mockito.when(headers.getPartitionId()).thenReturn("tenant"); Mockito.when(schemaStore.getSchema(Mockito.anyString(), Mockito.anyString())) .thenThrow(NotFoundException.class); + + SchemaRequest mockSchReqPubInt = getMockSchemaObject_published_InternalScope(); + SchemaInfo mockSchInfoPubInt = getMockSchemaInfo_Published_InternalScope(); + SchemaRequest schReqBreakingChange = getMockSchemaObject_BreakingChanges(); ObjectMapper mapper = new ObjectMapper(); - String inputSchema = mapper.writeValueAsString(getMockSchemaObject_BreakingChanges().getSchema()); + String inputSchema = mapper.writeValueAsString(schReqBreakingChange.getSchema()); String latestSchema = "{\"key\":\"value1\"}"; when(schemaInfoStore.isUnique("os:wks:well:1.1.1", "common")).thenReturn(true); when(schemaInfoStore.isUnique("os:wks:well:1.1.1", "tenant")).thenReturn(true); Mockito.doNothing().when(SchemaUtil).checkBreakingChange(inputSchema, latestSchema); - Mockito.when(schemaInfoStore.getLatestMinorVerSchema(getMockSchemaInfo_Published_InternalScope())) + Mockito.when(schemaInfoStore.getLatestMinorVerSchema(mockSchInfoPubInt)) .thenReturn(latestSchema); Mockito.when(authorityService.checkAndRegisterAuthorityIfNotPresent( - getMockSchemaObject_published_InternalScope().getSchemaInfo().getSchemaIdentity().getAuthority())) + mockSchReqPubInt.getSchemaInfo().getSchemaIdentity().getAuthority())) .thenReturn(true); Mockito.when(sourceService.checkAndRegisterSourceIfNotPresent( - getMockSchemaObject_published_InternalScope().getSchemaInfo().getSchemaIdentity().getSource())) + mockSchReqPubInt.getSchemaInfo().getSchemaIdentity().getSource())) .thenReturn(true); Mockito.when(entityTypeService.checkAndRegisterEntityTypeIfNotPresent( - getMockSchemaObject_published_InternalScope().getSchemaInfo().getSchemaIdentity().getEntityType())) + mockSchReqPubInt.getSchemaInfo().getSchemaIdentity().getEntityType())) .thenReturn(true); Mockito.when(schemaResolver.resolveSchema(Mockito.anyString())).thenReturn(latestSchema); Mockito.when(schemaStore.createSchema(Mockito.anyString(), Mockito.anyString())).thenReturn(latestSchema); - Mockito.when(schemaInfoStore.createSchemaInfo(getMockSchemaObject_published_InternalScope())) - .thenReturn(getMockSchemaInfo_Published_InternalScope()); - assertEquals(getMockSchemaInfo_Published_InternalScope(), - schemaService.createSchema(getMockSchemaObject_published_InternalScope())); + Mockito.when(schemaInfoStore.createSchemaInfo(mockSchReqPubInt)) + .thenReturn(mockSchInfoPubInt); + assertEquals(mockSchInfoPubInt, schemaService.createSchema(mockSchReqPubInt)); - schemaService.createSchema(getMockSchemaObject_BreakingChanges()); + schemaService.createSchema(schReqBreakingChange); } @Test @@ -303,15 +323,17 @@ public class SchemaServiceTest { Mockito.when(headers.getPartitionId()).thenReturn("tenant"); when(schemaInfoStore.isUnique("os:wks:well:1.1.1", "common")).thenReturn(true); when(schemaInfoStore.isUnique("os:wks:well:1.1.1", "tenant")).thenReturn(true); + SchemaRequest schReqPub = getMockSchemaObject_published(); + Mockito.when(schemaStore.getSchema(Mockito.anyString(), Mockito.anyString())) .thenThrow(NotFoundException.class); Mockito.when(authorityService.checkAndRegisterAuthorityIfNotPresent( - getMockSchemaObject_published().getSchemaInfo().getSchemaIdentity().getAuthority())).thenReturn(true); + schReqPub.getSchemaInfo().getSchemaIdentity().getAuthority())).thenReturn(true); Mockito.when(sourceService.checkAndRegisterSourceIfNotPresent( - getMockSchemaObject_published().getSchemaInfo().getSchemaIdentity().getSource())).thenReturn(true); + schReqPub.getSchemaInfo().getSchemaIdentity().getSource())).thenReturn(true); Mockito.when(entityTypeService.checkAndRegisterEntityTypeIfNotPresent( - getMockSchemaObject_published().getSchemaInfo().getSchemaIdentity().getEntityType())).thenReturn(false); - schemaService.createSchema(getMockSchemaObject_published()); + schReqPub.getSchemaInfo().getSchemaIdentity().getEntityType())).thenReturn(false); + schemaService.createSchema(schReqPub); } @Test @@ -322,15 +344,16 @@ public class SchemaServiceTest { Mockito.when(headers.getPartitionId()).thenReturn("tenant"); when(schemaInfoStore.isUnique("os:wks:well:1.1.1", "common")).thenReturn(true); when(schemaInfoStore.isUnique("os:wks:well:1.1.1", "tenant")).thenReturn(true); + SchemaRequest schReqPub = getMockSchemaObject_published(); Mockito.when(schemaStore.getSchema(Mockito.anyString(), Mockito.anyString())) .thenThrow(NotFoundException.class); Mockito.when(authorityService.checkAndRegisterAuthorityIfNotPresent( - getMockSchemaObject_published().getSchemaInfo().getSchemaIdentity().getAuthority())).thenReturn(false); + schReqPub.getSchemaInfo().getSchemaIdentity().getAuthority())).thenReturn(false); Mockito.when(sourceService.checkAndRegisterSourceIfNotPresent( - getMockSchemaObject_published().getSchemaInfo().getSchemaIdentity().getSource())).thenReturn(true); + schReqPub.getSchemaInfo().getSchemaIdentity().getSource())).thenReturn(true); Mockito.when(entityTypeService.checkAndRegisterEntityTypeIfNotPresent( - getMockSchemaObject_published().getSchemaInfo().getSchemaIdentity().getEntityType())).thenReturn(true); - schemaService.createSchema(getMockSchemaObject_published()); + schReqPub.getSchemaInfo().getSchemaIdentity().getEntityType())).thenReturn(true); + schemaService.createSchema(schReqPub); } @Test @@ -343,14 +366,15 @@ public class SchemaServiceTest { when(schemaInfoStore.isUnique("os:wks:well:1.1.1", "tenant")).thenReturn(true); Mockito.when(schemaStore.getSchema(Mockito.anyString(), Mockito.anyString())) .thenThrow(NotFoundException.class); + SchemaRequest schReqPub = getMockSchemaObject_published(); Mockito.when(authorityService.checkAndRegisterAuthorityIfNotPresent( - getMockSchemaObject_published().getSchemaInfo().getSchemaIdentity().getAuthority())).thenReturn(true); + schReqPub.getSchemaInfo().getSchemaIdentity().getAuthority())).thenReturn(true); Mockito.when(sourceService.checkAndRegisterSourceIfNotPresent( - getMockSchemaObject_published().getSchemaInfo().getSchemaIdentity().getSource())).thenReturn(false); + schReqPub.getSchemaInfo().getSchemaIdentity().getSource())).thenReturn(false); Mockito.when(entityTypeService.checkAndRegisterEntityTypeIfNotPresent( - getMockSchemaObject_published().getSchemaInfo().getSchemaIdentity().getEntityType())).thenReturn(true); + schReqPub.getSchemaInfo().getSchemaIdentity().getEntityType())).thenReturn(true); - schemaService.createSchema(getMockSchemaObject_published()); + schemaService.createSchema(schReqPub); } @Test @@ -370,16 +394,18 @@ public class SchemaServiceTest { public void testUpdateSchema_WithPublishedState() { try { Mockito.when(headers.getPartitionId()).thenReturn("tenant"); + SchemaRequest schReqPub = getMockSchemaObject_published(); + Mockito.when(authorityService.checkAndRegisterAuthorityIfNotPresent( - getMockSchemaObject_published().getSchemaInfo().getSchemaIdentity().getAuthority())) + schReqPub.getSchemaInfo().getSchemaIdentity().getAuthority())) .thenReturn(true); Mockito.when(sourceService.checkAndRegisterSourceIfNotPresent( - getMockSchemaObject_published().getSchemaInfo().getSchemaIdentity().getSource())).thenReturn(true); + schReqPub.getSchemaInfo().getSchemaIdentity().getSource())).thenReturn(true); Mockito.when(entityTypeService.checkAndRegisterEntityTypeIfNotPresent( - getMockSchemaObject_published().getSchemaInfo().getSchemaIdentity().getEntityType())) + schReqPub.getSchemaInfo().getSchemaIdentity().getEntityType())) .thenReturn(true); Mockito.when(schemaInfoStore.getSchemaInfo("os:wks:well:1.1.1")).thenReturn(getMockSchemaInfo()); - schemaService.updateSchema(getMockSchemaObject_published()); + schemaService.updateSchema(schReqPub); fail("Should not succeed"); } catch (BadRequestException e) { @@ -394,15 +420,16 @@ public class SchemaServiceTest { Mockito.when(headers.getPartitionId()).thenReturn("tenant"); when(schemaInfoStore.isUnique("os:wks:well:1.1.1", "common")).thenReturn(true); when(schemaInfoStore.isUnique("os:wks:well:1.1.1", "tenant")).thenReturn(true); + SchemaRequest schReqPub = getMockSchemaObject_published(); Mockito.when(authorityService.checkAndRegisterAuthorityIfNotPresent( - getMockSchemaObject_published().getSchemaInfo().getSchemaIdentity().getAuthority())).thenReturn(true); + schReqPub.getSchemaInfo().getSchemaIdentity().getAuthority())).thenReturn(true); Mockito.when(sourceService.checkAndRegisterSourceIfNotPresent( - getMockSchemaObject_published().getSchemaInfo().getSchemaIdentity().getSource())).thenReturn(true); + schReqPub.getSchemaInfo().getSchemaIdentity().getSource())).thenReturn(true); Mockito.when(entityTypeService.checkAndRegisterEntityTypeIfNotPresent( - getMockSchemaObject_published().getSchemaInfo().getSchemaIdentity().getEntityType())).thenReturn(true); + schReqPub.getSchemaInfo().getSchemaIdentity().getEntityType())).thenReturn(true); Mockito.when(schemaInfoStore.getSchemaInfo("os:wks:well:1.1.1")).thenThrow(NotFoundException.class); try { - schemaService.updateSchema(getMockSchemaObject_published()); + schemaService.updateSchema(schReqPub); fail("Should not succeed"); } catch (BadRequestException e) { @@ -417,12 +444,13 @@ public class SchemaServiceTest { Mockito.when(headers.getPartitionId()).thenReturn("tenant"); when(schemaInfoStore.isUnique("os:wks:well:1.1.1", "common")).thenReturn(true); when(schemaInfoStore.isUnique("os:wks:well:1.1.1", "tenant")).thenReturn(true); + SchemaRequest schReqPub = getMockSchemaObject_published(); Mockito.when(authorityService.checkAndRegisterAuthorityIfNotPresent( - getMockSchemaObject_published().getSchemaInfo().getSchemaIdentity().getAuthority())).thenReturn(true); + schReqPub.getSchemaInfo().getSchemaIdentity().getAuthority())).thenReturn(true); Mockito.when(sourceService.checkAndRegisterSourceIfNotPresent( - getMockSchemaObject_published().getSchemaInfo().getSchemaIdentity().getSource())).thenReturn(true); + schReqPub.getSchemaInfo().getSchemaIdentity().getSource())).thenReturn(true); Mockito.when(entityTypeService.checkAndRegisterEntityTypeIfNotPresent( - getMockSchemaObject_published().getSchemaInfo().getSchemaIdentity().getEntityType())).thenReturn(true); + schReqPub.getSchemaInfo().getSchemaIdentity().getEntityType())).thenReturn(true); Mockito.when(schemaInfoStore.getSchemaInfo("os:wks:well:1.1.1")).thenThrow(NotFoundException.class); try { schemaService.updateSchema(getMockSchemaObject_Development()); @@ -441,16 +469,17 @@ public class SchemaServiceTest { Mockito.when(headers.getPartitionId()).thenReturn("tenant"); when(schemaInfoStore.isUnique("os:wks:well:1.1.1", "common")).thenReturn(true); when(schemaInfoStore.isUnique("os:wks:well:1.1.1", "tenant")).thenReturn(true); + SchemaRequest schReqPub = getMockSchemaObject_published(); Mockito.when(authorityService.checkAndRegisterAuthorityIfNotPresent( - getMockSchemaObject_published().getSchemaInfo().getSchemaIdentity().getAuthority())) + schReqPub.getSchemaInfo().getSchemaIdentity().getAuthority())) .thenReturn(true); Mockito.when(sourceService.checkAndRegisterSourceIfNotPresent( - getMockSchemaObject_published().getSchemaInfo().getSchemaIdentity().getSource())).thenReturn(true); + schReqPub.getSchemaInfo().getSchemaIdentity().getSource())).thenReturn(true); Mockito.when(entityTypeService.checkAndRegisterEntityTypeIfNotPresent( - getMockSchemaObject_published().getSchemaInfo().getSchemaIdentity().getEntityType())) + schReqPub.getSchemaInfo().getSchemaIdentity().getEntityType())) .thenReturn(true); Mockito.when(schemaInfoStore.getSchemaInfo("os:wks:well:1.1.1")).thenThrow(NotFoundException.class); - schemaService.updateSchema(getMockSchemaObject_published()); + schemaService.updateSchema(schReqPub); fail("Should not succeed"); } catch (BadRequestException e) { @@ -515,12 +544,22 @@ public class SchemaServiceTest { @Test public void testGetSchemaInfoList_LatestVersion_MajorMinorConflict_NoLatestVersion() throws ApplicationException, NotFoundException, BadRequestException { - List schemaInfos = new LinkedList(); + + List schemaInt = new LinkedList(); + List schemaPub = new LinkedList(); + List schemaComb = new LinkedList(); + + schemaInt.add(getMockSchemaInfo_Published_InternalScope()); + schemaPub.add(getMockSchemaInfo()); + schemaComb.addAll(schemaPub); + schemaComb.addAll(schemaInt); + QueryParams queryParams = QueryParams.builder().authority("test").latestVersion(true).limit(10).build(); + Mockito.when(headers.getPartitionId()).thenReturn("tenant"); - schemaInfos.add(getMockSchemaInfo()); - Mockito.when(schemaInfoStore.getSchemaInfoList(queryParams, "tenant")).thenReturn(schemaInfos); - Mockito.when(schemaInfoStore.getSchemaInfoList(queryParams, "tenant")).thenReturn(schemaInfos); + Mockito.when(schemaInfoStore.getSchemaInfoList(queryParams, "common")).thenReturn(schemaPub); + Mockito.when(schemaInfoStore.getSchemaInfoList(queryParams, "tenant")).thenReturn(schemaInt); + Assert.assertEquals(1, (schemaService.getSchemaInfoList(queryParams).getSchemaInfos().size())); } @@ -580,13 +619,23 @@ public class SchemaServiceTest { @Test public void testGetSchemaInfoList_LatestVersion_MajorMinorConflict_MajorVersionWithoutMinorAndPatch() throws ApplicationException, NotFoundException, BadRequestException { - List schemaInfos = new LinkedList(); - QueryParams queryParams = QueryParams.builder().authority("test").latestVersion(true).schemaVersionMajor(1l) + QueryParams queryParams = QueryParams.builder().authority("os").latestVersion(true).schemaVersionMajor(1l) .limit(10).build(); + + List schemaInt = new LinkedList(); + List schemaPub = new LinkedList(); + List schemaComb = new LinkedList(); + + schemaInt.add(getMockSchemaInfo_Published_InternalScope()); + schemaPub.add(getMockSchemaInfo()); + schemaComb.addAll(schemaPub); + schemaComb.addAll(schemaInt); + Mockito.when(headers.getPartitionId()).thenReturn("tenant"); - schemaInfos.add(getMockSchemaInfo()); - Mockito.when(schemaInfoStore.getSchemaInfoList(queryParams, "tenant")).thenReturn(schemaInfos); - Mockito.when(schemaInfoStore.getSchemaInfoList(queryParams, "tenant")).thenReturn(schemaInfos); + + Mockito.when(schemaInfoStore.getSchemaInfoList(queryParams, "common")).thenReturn(schemaPub); + Mockito.when(schemaInfoStore.getSchemaInfoList(queryParams, "tenant")).thenReturn(schemaInt); + Assert.assertEquals(1, (schemaService.getSchemaInfoList(queryParams).getSchemaInfos().size())); } @@ -613,24 +662,43 @@ public class SchemaServiceTest { @Test public void testGetSchemaInfoList_LatestVersion_WithoutScope() throws ApplicationException, NotFoundException, BadRequestException { - List schemaInfos = new LinkedList(); - QueryParams queryParams = QueryParams.builder().latestVersion(true).limit(10).build(); - schemaInfos.add(getMockSchemaInfo()); - schemaInfos.add(getMockSchemaInfo_INTERNAL_EntityWellBore()); - Mockito.when(headers.getPartitionId()).thenReturn("tenant"); - Mockito.when(schemaInfoStore.getSchemaInfoList(queryParams, "tenant")).thenReturn(schemaInfos); - Assert.assertTrue(schemaService.getSchemaInfoList(queryParams).getSchemaInfos().size() > 0); + + List schemaInt = new LinkedList(); + List schemaPub = new LinkedList(); + List schemaComb = new LinkedList(); + + schemaInt.add(getMockSchemaInfo_Published_InternalScope()); + schemaPub.add(getMockSchemaInfo()); + schemaComb.addAll(schemaPub); + schemaComb.addAll(schemaInt); + + QueryParams queryParams = QueryParams.builder().latestVersion(true).limit(10).build(); + + Mockito.when(headers.getPartitionId()).thenReturn("tenant"); + Mockito.when(schemaInfoStore.getSchemaInfoList(queryParams, "common")).thenReturn(schemaPub); + Mockito.when(schemaInfoStore.getSchemaInfoList(queryParams, "tenant")).thenReturn(schemaInt); + + Assert.assertEquals(1, schemaService.getSchemaInfoList(queryParams).getSchemaInfos().size()); } @Test public void testGetSchemaInfoList_LatestVersion_WithoutScope_ForOneAuthority() throws ApplicationException, NotFoundException, BadRequestException { - List schemaInfos = new LinkedList(); - QueryParams queryParams = QueryParams.builder().authority("test").latestVersion(true).limit(10).build(); - schemaInfos.add(getMockSchemaInfo()); - schemaInfos.add(getMockSchemaInfo_Published_InternalScope()); + List schemaInt = new LinkedList(); + List schemaPub = new LinkedList(); + List schemaComb = new LinkedList(); + + schemaInt.add(getMockSchemaInfo_Published_InternalScope()); + schemaPub.add(getMockSchemaInfo()); + schemaComb.addAll(schemaPub); + schemaComb.addAll(schemaInt); + + QueryParams queryParams = QueryParams.builder().authority("os").latestVersion(true).limit(10).build(); + Mockito.when(headers.getPartitionId()).thenReturn("tenant"); - Mockito.when(schemaInfoStore.getSchemaInfoList(queryParams, "tenant")).thenReturn(schemaInfos); + Mockito.when(schemaInfoStore.getSchemaInfoList(queryParams, "common")).thenReturn(schemaPub); + Mockito.when(schemaInfoStore.getSchemaInfoList(queryParams, "tenant")).thenReturn(schemaInt); + Assert.assertEquals(1, (schemaService.getSchemaInfoList(queryParams).getSchemaInfos().size())); } @@ -639,10 +707,12 @@ public class SchemaServiceTest { throws ApplicationException, NotFoundException, BadRequestException { List schemaInfos = new LinkedList(); QueryParams queryParams = QueryParams.builder().latestVersion(true).scope("INTERNAL").limit(10).build(); + schemaInfos.add(getMockSchemaInfo_INTERNAL_EntityWellBore()); schemaInfos.add(getMockSchemaInfo_Published_InternalScope()); + Mockito.when(headers.getPartitionId()).thenReturn("tenant"); - Mockito.when(schemaInfoStore.getSchemaInfoList(queryParams, "tenant")).thenReturn(schemaInfos); + Mockito.when(schemaInfoStore.getSchemaInfoList(queryParams, "tenant")).thenReturn(schemaInfos.subList(0, 1)); Assert.assertTrue(schemaService.getSchemaInfoList(queryParams).getSchemaInfos().size() > 0); @@ -656,8 +726,9 @@ public class SchemaServiceTest { public void testGetSchemaInfoList_LatestVersion_WithScopeInternal_WithOneAuthority() throws ApplicationException, NotFoundException, BadRequestException { List schemaInfos = new LinkedList(); - QueryParams queryParams = QueryParams.builder().authority("test").latestVersion(true).scope("INTERNAL").limit(10).build(); + QueryParams queryParams = QueryParams.builder().authority("os").latestVersion(true).scope("INTERNAL").limit(10).build(); schemaInfos.add(getMockSchemaInfo_Published_InternalScope()); + Mockito.when(headers.getPartitionId()).thenReturn("tenant"); Mockito.when(schemaInfoStore.getSchemaInfoList(queryParams, "tenant")).thenReturn(schemaInfos); @@ -672,11 +743,15 @@ public class SchemaServiceTest { QueryParams queryParams = QueryParams.builder().latestVersion(true).scope("SHARED").limit(10).build(); schemaInfos.add(getMockSchemaInfo_Published_SharedScope()); schemaInfos.add(getMockSchemaInfo_SHARED_EntityWellBore()); + Mockito.when(headers.getPartitionId()).thenReturn("tenant"); Mockito.when(schemaInfoStore.getSchemaInfoList(queryParams, "common")).thenReturn(schemaInfos); - Assert.assertTrue(schemaService.getSchemaInfoList(queryParams).getSchemaInfos().size() > 0); - for(SchemaInfo outputSchemaInfo : schemaService.getSchemaInfoList(queryParams).getSchemaInfos()) { + List finalList = schemaService.getSchemaInfoList(queryParams).getSchemaInfos(); + + Assert.assertTrue(finalList.size() == 2); + + for(SchemaInfo outputSchemaInfo : finalList) { Assert.assertTrue("SHARED".equals(outputSchemaInfo.getScope().toString())); } @@ -685,11 +760,14 @@ public class SchemaServiceTest { @Test public void testGetSchemaInfoList_LatestVersion_WithScopeShared_WithOneAuthority() throws ApplicationException, NotFoundException, BadRequestException { - List schemaInfos = new LinkedList(); + + List schemaInfos = new LinkedList(); QueryParams queryParams = QueryParams.builder().authority("os").latestVersion(true).scope("SHARED").limit(10).build(); schemaInfos.add(getMockSchemaInfo_Published_SharedScope()); + Mockito.when(headers.getPartitionId()).thenReturn("tenant"); Mockito.when(schemaInfoStore.getSchemaInfoList(queryParams, "common")).thenReturn(schemaInfos); + Assert.assertEquals(1, (schemaService.getSchemaInfoList(queryParams).getSchemaInfos().size())); Assert.assertTrue("SHARED".equals(schemaService.getSchemaInfoList(queryParams).getSchemaInfos().get(0).getScope().toString())); Assert.assertTrue("os".equals(schemaService.getSchemaInfoList(queryParams).getSchemaInfos().get(0).getSchemaIdentity().getAuthority())); @@ -723,7 +801,7 @@ public class SchemaServiceTest { .schemaIdentity(SchemaIdentity.builder().authority("os").source("wks").entityType("well") .schemaVersionMajor(1L).schemaVersionMinor(1L).schemaVersionPatch(1L) .id("os:wks:well:1.1.1").build()) - .scope(SchemaScope.SHARED).status(SchemaStatus.PUBLISHED).build()) + .dateCreated(currDate).scope(SchemaScope.SHARED).status(SchemaStatus.PUBLISHED).build()) .build(); } @@ -734,7 +812,7 @@ public class SchemaServiceTest { .schemaIdentity(SchemaIdentity.builder().authority("os").source("wks").entityType("well") .schemaVersionMajor(1L).schemaVersionMinor(1L).schemaVersionPatch(1L) .id("os:wks:well:1.1.1").build()) - .scope(SchemaScope.INTERNAL).status(SchemaStatus.PUBLISHED).build()) + .dateCreated(currDate).scope(SchemaScope.INTERNAL).status(SchemaStatus.PUBLISHED).build()) .build(); } @@ -745,7 +823,7 @@ public class SchemaServiceTest { .schemaIdentity(SchemaIdentity.builder().authority("os").source("wks").entityType("well") .schemaVersionMajor(1L).schemaVersionMinor(1L).schemaVersionPatch(1L) .id("os:wks:well:1.1.1").build()) - .scope(SchemaScope.SHARED).status(SchemaStatus.PUBLISHED).build()) + .dateCreated(currDate).scope(SchemaScope.SHARED).status(SchemaStatus.PUBLISHED).build()) .build(); } @@ -756,7 +834,7 @@ public class SchemaServiceTest { .schemaIdentity(SchemaIdentity.builder().authority("os").source("wks").entityType("well") .schemaVersionMajor(1L).schemaVersionMinor(1L).schemaVersionPatch(1L) .id("os:wks:well:1.1.1").build()) - .scope(SchemaScope.INTERNAL).status(SchemaStatus.PUBLISHED).build()) + .dateCreated(currDate).scope(SchemaScope.INTERNAL).status(SchemaStatus.PUBLISHED).build()) .build(); } @@ -767,7 +845,7 @@ public class SchemaServiceTest { .schemaIdentity(SchemaIdentity.builder().authority("os").source("wks").entityType("well") .schemaVersionMajor(1L).schemaVersionMinor(1L).schemaVersionPatch(1L) .id("os:wks:well:1.1.1").build()) - .scope(SchemaScope.INTERNAL).status(SchemaStatus.DEVELOPMENT).build()) + .dateCreated(currDate).scope(SchemaScope.INTERNAL).status(SchemaStatus.DEVELOPMENT).build()) .build(); } @@ -777,7 +855,7 @@ public class SchemaServiceTest { .schemaIdentity( SchemaIdentity.builder().authority("os").source("wks").entityType("well").schemaVersionMajor(1L) .schemaVersionMinor(1L).schemaVersionPatch(1L).id("os:wks:well:1.1.1").build()) - .scope(SchemaScope.SHARED).status(SchemaStatus.PUBLISHED).build(); + .dateCreated(currDate).scope(SchemaScope.SHARED).status(SchemaStatus.PUBLISHED).build(); } @@ -786,7 +864,7 @@ public class SchemaServiceTest { .schemaIdentity( SchemaIdentity.builder().authority("os").source("abc").entityType("wellbore").schemaVersionMajor(1L) .schemaVersionMinor(1L).schemaVersionPatch(1L).id("os:wks:well:1.1.1").build()) - .scope(SchemaScope.SHARED).status(SchemaStatus.PUBLISHED).build(); + .dateCreated(currDate).scope(SchemaScope.SHARED).status(SchemaStatus.PUBLISHED).build(); } @@ -795,7 +873,7 @@ public class SchemaServiceTest { .schemaIdentity( SchemaIdentity.builder().authority("os").source("abc").entityType("wellbore").schemaVersionMajor(1L) .schemaVersionMinor(1L).schemaVersionPatch(1L).id("os:wks:well:1.1.1").build()) - .scope(SchemaScope.INTERNAL).status(SchemaStatus.PUBLISHED).build(); + .dateCreated(currDate).scope(SchemaScope.INTERNAL).status(SchemaStatus.PUBLISHED).build(); } @@ -804,7 +882,7 @@ public class SchemaServiceTest { .schemaIdentity( SchemaIdentity.builder().authority("os").source("wks").entityType("well").schemaVersionMajor(1L) .schemaVersionMinor(1L).schemaVersionPatch(1L).id("os:wks:well:1.1.1").build()) - .scope(SchemaScope.INTERNAL).status(SchemaStatus.PUBLISHED).build(); + .dateCreated(currDate).scope(SchemaScope.INTERNAL).status(SchemaStatus.PUBLISHED).build(); } private SchemaInfo getMockSchemaInfo_Published_SharedScope() { @@ -812,7 +890,7 @@ public class SchemaServiceTest { .schemaIdentity( SchemaIdentity.builder().authority("os").source("wks").entityType("well").schemaVersionMajor(1L) .schemaVersionMinor(1L).schemaVersionPatch(1L).id("os:wks:well:1.1.1").build()) - .scope(SchemaScope.SHARED).status(SchemaStatus.PUBLISHED).build(); + .dateCreated(currDate).scope(SchemaScope.SHARED).status(SchemaStatus.PUBLISHED).build(); } private SchemaInfo getMockSchemaInfo_development_status() { @@ -820,7 +898,7 @@ public class SchemaServiceTest { .schemaIdentity( SchemaIdentity.builder().authority("os").source("wks").entityType("well").schemaVersionMajor(1L) .schemaVersionMinor(1L).schemaVersionPatch(1L).id("os:wks:well:1.1.1").build()) - .scope(SchemaScope.INTERNAL).status(SchemaStatus.DEVELOPMENT).build(); + .dateCreated(currDate).scope(SchemaScope.INTERNAL).status(SchemaStatus.DEVELOPMENT).build(); } } \ No newline at end of file diff --git a/testing/schema-test-core/src/test/java/org/opengroup/osdu/schema/stepdefs/SchemaServiceStepDef_PUT.java b/testing/schema-test-core/src/test/java/org/opengroup/osdu/schema/stepdefs/SchemaServiceStepDef_PUT.java index 9dba2e65..bc941f0c 100644 --- a/testing/schema-test-core/src/test/java/org/opengroup/osdu/schema/stepdefs/SchemaServiceStepDef_PUT.java +++ b/testing/schema-test-core/src/test/java/org/opengroup/osdu/schema/stepdefs/SchemaServiceStepDef_PUT.java @@ -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 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", diff --git a/testing/schema-test-core/src/test/resources/features/IntegrationTest_SchemaService_PUT.feature b/testing/schema-test-core/src/test/resources/features/IntegrationTest_SchemaService_PUT.feature index da2630ff..6f6c057a 100644 --- a/testing/schema-test-core/src/test/resources/features/IntegrationTest_SchemaService_PUT.feature +++ b/testing/schema-test-core/src/test/resources/features/IntegrationTest_SchemaService_PUT.feature @@ -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 and data-partition-id as - Then the put service for supersededBy should respond back with and + Then the put service for supersededBy should respond back with 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 , data-partition-id as for superceded input - Then the put service for supersededBy should respond back with and + Then the put service for supersededBy should respond back with 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 diff --git a/testing/schema-test-core/src/test/resources/input_payloads/supercededInputPayload_positive.json b/testing/schema-test-core/src/test/resources/input_payloads/supercededInputPayload_positive.json index 2615acad..256f7ec9 100644 --- a/testing/schema-test-core/src/test/resources/input_payloads/supercededInputPayload_positive.json +++ b/testing/schema-test-core/src/test/resources/input_payloads/supercededInputPayload_positive.json @@ -22,5 +22,5 @@ "status": "DEVELOPMENT", "scope": "INTERNAL" }, - "schema": {"name": "ABHISHEK2"} + "schema": {"name": "Test"} } \ No newline at end of file -- GitLab From 3eba07405599828dfdbd644a7aa4dbe3c7ab1097 Mon Sep 17 00:00:00 2001 From: Abhishek Kumar Date: Fri, 14 Aug 2020 13:37:30 +0530 Subject: [PATCH 2/3] Test cases and logs updated --- .../service/serviceimpl/SchemaService.java | 19 ++++--------------- .../stepdefs/SchemaServiceStepDef_PUT.java | 7 ++----- 2 files changed, 6 insertions(+), 20 deletions(-) diff --git a/schema-core/src/main/java/org/opengroup/osdu/schema/service/serviceimpl/SchemaService.java b/schema-core/src/main/java/org/opengroup/osdu/schema/service/serviceimpl/SchemaService.java index 4ed72e93..c9d1e887 100644 --- a/schema-core/src/main/java/org/opengroup/osdu/schema/service/serviceimpl/SchemaService.java +++ b/schema-core/src/main/java/org/opengroup/osdu/schema/service/serviceimpl/SchemaService.java @@ -258,11 +258,7 @@ public class SchemaService implements ISchemaService { private void getSchemaInfos(QueryParams queryParams, List schemaList, String tenant) throws ApplicationException { - long start = System.currentTimeMillis(); schemaInfoStore.getSchemaInfoList(queryParams, tenant).forEach(schemaList::add); - long end = System.currentTimeMillis(); - log.info("Total schemas fetched :"+(null == schemaList ? 0 : schemaList.size())); - log.info("Time lapsed to fetch schemas :"+(end-start)+" ms"); } private void latestVersionMajorMinorFiltersCheck(QueryParams queryParams) throws BadRequestException { @@ -294,7 +290,6 @@ public class SchemaService implements ISchemaService { private List getLatestVersionSchemaList(List filteredSchemaList) { - long start = System.currentTimeMillis(); List latestSchemaList = new ArrayList<>(); Map latestSchemaMap = new HashMap<>(); @@ -312,11 +307,6 @@ public class SchemaService implements ISchemaService { latestSchemaList.addAll(latestSchemaMap.values()); - long end = System.currentTimeMillis(); - - log.info("Latest schema version size :"+(null == latestSchemaList ? 0 : latestSchemaList.size())); - log.info("Time lapsed to fetch latest version schemas :"+(end-start)+" ms"); - return latestSchemaList; } @@ -327,14 +317,13 @@ public class SchemaService implements ISchemaService { * @return String based key formed using Athority:Source:EntityType */ private String getGroupingKey(SchemaInfo schemaInfo){ - return schemaInfo.getSchemaIdentity().getAuthority()+":" - + schemaInfo.getSchemaIdentity().getSource()+":" - + schemaInfo.getSchemaIdentity().getEntityType(); - + 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 in following order
+ * This method compares the schema versions of two SchemaInfo attribute. The comparison is done based on the following order
* 1. Major Version
* 2. Minor Version
* 3. Patch Version
diff --git a/testing/schema-test-core/src/test/java/org/opengroup/osdu/schema/stepdefs/SchemaServiceStepDef_PUT.java b/testing/schema-test-core/src/test/java/org/opengroup/osdu/schema/stepdefs/SchemaServiceStepDef_PUT.java index bc941f0c..ddf15bba 100644 --- a/testing/schema-test-core/src/test/java/org/opengroup/osdu/schema/stepdefs/SchemaServiceStepDef_PUT.java +++ b/testing/schema-test-core/src/test/java/org/opengroup/osdu/schema/stepdefs/SchemaServiceStepDef_PUT.java @@ -239,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), -- GitLab From 176d297af90c69c8a3e07c142aea8e0d90df7e51 Mon Sep 17 00:00:00 2001 From: Abhishek Kumar Date: Tue, 18 Aug 2020 00:06:19 +0530 Subject: [PATCH 3/3] Order by created date --- .../osdu/schema/service/serviceimpl/SchemaService.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/schema-core/src/main/java/org/opengroup/osdu/schema/service/serviceimpl/SchemaService.java b/schema-core/src/main/java/org/opengroup/osdu/schema/service/serviceimpl/SchemaService.java index c9d1e887..d2fb9a58 100644 --- a/schema-core/src/main/java/org/opengroup/osdu/schema/service/serviceimpl/SchemaService.java +++ b/schema-core/src/main/java/org/opengroup/osdu/schema/service/serviceimpl/SchemaService.java @@ -247,8 +247,11 @@ public class SchemaService implements ISchemaService { if (queryParams.getLatestVersion() != null && queryParams.getLatestVersion()) { schemaList = getLatestVersionSchemaList(schemaList); } + + Comparator compareByCreatedDate = (s1,s2) -> s1.getDateCreated().compareTo(s2.getDateCreated()); List schemaFinalList = schemaList.stream().skip(queryParams.getOffset()) + .sorted(compareByCreatedDate) .limit(queryParams.getLimit()).collect(Collectors.toList()); return SchemaInfoResponse.builder().schemaInfos(schemaFinalList).count(schemaFinalList.size()) -- GitLab