diff --git a/testing/wks-test-core/src/test/resources/features/IntegrationTestScenarios.feature b/testing/wks-test-core/src/test/resources/features/IntegrationTestScenarios.feature index 0aa62fbd42cacff358e0dc990369d1635fd05bf8..3df8fb55f3bbc59580c09c6b12f53d7245849652 100644 --- a/testing/wks-test-core/src/test/resources/features/IntegrationTestScenarios.feature +++ b/testing/wks-test-core/src/test/resources/features/IntegrationTestScenarios.feature @@ -131,7 +131,8 @@ Feature: Covers all positive and negative test cases around WKS transformation s | relatedEntityPayload | parentPayload | expectedRelationshipBlock | | "/input_payloads/relationship/raw_having_wks_created_log_entity.json" | "/input_payloads/relationship/recordRelatedToEntityHavingWksCreatedLogEntity.json" | "/input_payloads/relationship/expectedRelationship_for_rawHavingWksCreated.json" | - @RelationshipValidation + + @Not_yet_implemented_for_gitlab_gcp Scenario Outline: WKS record relationships block should show relation to wks record created by wks service, with mapping definition targeted to schema of same authority and having higher major version Given I hit Storage service put end point with to persist a record say raw1 And I verify that three wks records are created for this raw record raw1 @@ -139,7 +140,7 @@ Feature: Covers all positive and negative test cases around WKS transformation s When I hit Storage service put end point with whose raw record contains relationship to record raw1 Then Transformed record should contain relationship to wks with same authority of raw1 and with higher major version as per - Examples: + Examples: | relatedEntityPayload | relatedEntityPayload2 | parentPayload | expectedRelationshipBlock | | "/input_payloads/relationship/raw_having_wks_created_well_entity.json" | "/input_payloads/relationship/recordRelatedToAnotherRawButNotCreatedByWKSService.json" | "/input_payloads/relationship/recordRelatedToEntityHavingWksCreatedWellEntity.json" | "/input_payloads/relationship/expectedRelationship_for_rawHavingWksCreated.json" | diff --git a/testing/wks-test-core/src/test/resources/input_payloads/relationship/recordRelatedToAnotherUnavailableRecord.json b/testing/wks-test-core/src/test/resources/input_payloads/relationship/recordRelatedToAnotherUnavailableRecord.json index 770efe85334c5b1b4cdd407d1812bf303586a81e..b0df81ec47a4998f2c24f4c332527e999725f5cb 100644 --- a/testing/wks-test-core/src/test/resources/input_payloads/relationship/recordRelatedToAnotherUnavailableRecord.json +++ b/testing/wks-test-core/src/test/resources/input_payloads/relationship/recordRelatedToAnotherUnavailableRecord.json @@ -3,7 +3,7 @@ "data": { "relationships": { "wellbore": { - "id": "non:existent:record-recordDoesNotExistInSystem" + "id": "non:existent:record-recordDoesNotExistInSystem-0a03169487847" } } }, diff --git a/wks-core/src/main/java/org/opengroup/osdu/wks/model/MappingInfo.java b/wks-core/src/main/java/org/opengroup/osdu/wks/model/MappingInfo.java index a682e03bba684872480390ee34f891993f5b4bf6..dbf7f69fe9ea2dd4d15b2d4d8a6d76c6c2c03b4e 100644 --- a/wks-core/src/main/java/org/opengroup/osdu/wks/model/MappingInfo.java +++ b/wks-core/src/main/java/org/opengroup/osdu/wks/model/MappingInfo.java @@ -4,6 +4,8 @@ import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; +import lombok.ToString; + import org.opengroup.osdu.wks.enums.MappingScope; import java.sql.Timestamp; @@ -12,6 +14,7 @@ import java.sql.Timestamp; @Setter @AllArgsConstructor @NoArgsConstructor +@ToString public class MappingInfo { private String id; private String mappingId; diff --git a/wks-core/src/main/java/org/opengroup/osdu/wks/service/MappingService.java b/wks-core/src/main/java/org/opengroup/osdu/wks/service/MappingService.java index 2e3b59025e054ebac7f9ca09949d400748d114dc..6fc962d4bc9f7300f2f15672cf0567f6fdebc04d 100644 --- a/wks-core/src/main/java/org/opengroup/osdu/wks/service/MappingService.java +++ b/wks-core/src/main/java/org/opengroup/osdu/wks/service/MappingService.java @@ -1,6 +1,7 @@ package org.opengroup.osdu.wks.service; import org.opengroup.osdu.wks.exceptions.ApplicationException; +import org.opengroup.osdu.wks.model.MappingInfo; import org.opengroup.osdu.wks.model.MappingsModel; import java.util.List; @@ -8,4 +9,6 @@ import java.util.List; public interface MappingService { List getMapping(String kind) throws ApplicationException; + List getMappingInfos(String authority, String entity, String source, String majorVersion); + } diff --git a/wks-core/src/main/java/org/opengroup/osdu/wks/service/MappingServiceImpl.java b/wks-core/src/main/java/org/opengroup/osdu/wks/service/MappingServiceImpl.java index 0d4cc6dac031ae467825647501c80d21523422ec..f96e7f7b3e05055ce95c21efbf648d88ddbd16bf 100644 --- a/wks-core/src/main/java/org/opengroup/osdu/wks/service/MappingServiceImpl.java +++ b/wks-core/src/main/java/org/opengroup/osdu/wks/service/MappingServiceImpl.java @@ -31,5 +31,10 @@ public class MappingServiceImpl implements MappingService { } return mappingStore.getMappingFilesFromStorage(mappingInfos); } + + @Override + public List getMappingInfos(final String authority, final String entity, final String source, final String majorVersion) { + return mappingStore.getMappingInfo(authority, entity, source, majorVersion); + } } diff --git a/wks-core/src/main/java/org/opengroup/osdu/wks/util/RelationshipBlockHandler.java b/wks-core/src/main/java/org/opengroup/osdu/wks/util/RelationshipBlockHandler.java index b0f7c3e83a7eec7f2896baa53c82e57f6dc127e6..13b08a164167470ac15e9f715f3437a8cc3ab6dd 100644 --- a/wks-core/src/main/java/org/opengroup/osdu/wks/util/RelationshipBlockHandler.java +++ b/wks-core/src/main/java/org/opengroup/osdu/wks/util/RelationshipBlockHandler.java @@ -7,7 +7,6 @@ import java.util.Iterator; import java.util.List; import java.util.Optional; import java.util.Set; -import java.util.function.Predicate; import java.util.stream.Collectors; import org.opengroup.osdu.wks.constants.Constants; @@ -21,7 +20,7 @@ import org.opengroup.osdu.wks.model.SearchByAncestryQuery; import org.opengroup.osdu.wks.model.SearchQuery; import org.opengroup.osdu.wks.model.SearchRequestPayload; import org.opengroup.osdu.wks.model.UpdatedRelationshipBlock; -import org.opengroup.osdu.wks.provider.interfaces.MappingStore; +import org.opengroup.osdu.wks.service.MappingService; import org.opengroup.osdu.wks.service.SearchService; import org.opengroup.osdu.wks.service.StorageService; import org.slf4j.Logger; @@ -45,17 +44,20 @@ public class RelationshipBlockHandler { private String targetSchemaKindValue; - private MappingStore mappingStore; + private MappingService mappingService; + + private KindUtil kindUtil; @Autowired public RelationshipBlockHandler( StorageService storageService, SearchService searchService, - MappingStore mappingStore) { + MappingService mappingService, + KindUtil kindUtil) { this.storageService = storageService; this.searchService = searchService; - this.mappingStore = mappingStore; - + this.mappingService = mappingService; + this.kindUtil = kindUtil; } public UpdatedRelationshipBlock updateRelationshipBlockWithWksIds(Tree rawRelationshipsBlockTree, String targetSchemaKind) { @@ -119,21 +121,97 @@ public class RelationshipBlockHandler { private Relationship getRelationshipBlockWithWksIds(Relationship rawRelationship) throws ApplicationException { List rawIds = rawRelationship.getIds(); List wksIds = getWksIds(rawIds); - + rawRelationship.setIds(wksIds); return rawRelationship; } - private List getWksIds(List rawIds) { - return rawIds.stream().map(this::getWksId).collect(Collectors.toList()); + private String getRelatedRecordIdUsingMapping(String relatedRecordId) { + Optional rawRecordOption = storageService.getRecord(relatedRecordId); + + if (rawRecordOption.isPresent()) { + Optional relatedRecordKindOption = getRelatedRecordKind(rawRecordOption.get()); + if (relatedRecordKindOption.isPresent()) { + Optional relatedRecordTargetKind = getTargetKind(relatedRecordKindOption.get()); + if (relatedRecordTargetKind.isPresent()) { + return WksIdGenerator.createRecordId(relatedRecordId, relatedRecordTargetKind.get()); + } + } + } + return relatedRecordId; + } + + private Optional getRelatedRecordKind(String rawRecord) { + try { + String relatedRecordKind = new Tree(rawRecord).get("kind").asString(); + return Optional.of(relatedRecordKind); + } catch (Exception e) { + LOGGER.warn("Error retrieving related record kind so keeping related record id"); + } + return Optional.ofNullable(null); + } + + private Optional getTargetKind(String kind) { + Optional mappingInfoOption = getLatestMappingInfoWithRelatedAuthority(kind); + + if(mappingInfoOption.isPresent()) { + MappingInfo mappingInfo = mappingInfoOption.get(); + return Optional.of(kindUtil.prepareKind(mappingInfo.getTargetSchemaAuthority(), + mappingInfo.getTargetSchemaSource(), mappingInfo.getTargetEntityType(), + mappingInfo.getTargetSchemaMajorVersion(), "0", "0")); + } + else { + return Optional.ofNullable(null); + } + } + + /** + * This method takes kind of related raw record. Using kind it finds all target + * mapping infos and then find related mapping infos using authority of + * container raw record. After this it returns Optional Mapping Info which has + * highest major version. Here we assume that mapping info version is not + * available only mapping major version is available. + * + * @param kind + * @return Optional + */ + private Optional getLatestMappingInfoWithRelatedAuthority(String kind) { + List mappingInfos = mappingService.getMappingInfos(kindUtil.retrieveAuthorityName(kind), + kindUtil.retrieveEntityName(kind), kindUtil.retrieveSourceName(kind), + kindUtil.retrieveMajorVersion(kind)); + if (mappingInfos.isEmpty()) { + return Optional.ofNullable(null); + } + + String authority = kindUtil.retrieveAuthorityName(targetSchemaKindValue); + mappingInfos = mappingInfos.stream() + .filter(mappingInfo -> mappingInfo.getTargetSchemaAuthority().equalsIgnoreCase(authority)) + .collect(Collectors.toList()); + return mappingInfos.stream().max(Comparator.comparing(MappingInfo::getMappingSchemaMajorVersion)); + } + + private List getWksIds(List relatedRawIds) { + List wksIds = new ArrayList<>(); + for(String relatedRawId : relatedRawIds) { + Optional wksIdOptional = getWksId(relatedRawId); + if(wksIdOptional.isPresent()) { + wksIds.add(wksIdOptional.get()); + } + else { + String relatedId = getRelatedRecordIdUsingMapping(relatedRawId); + wksIds.add(relatedId); + } + } + + return wksIds; } - private String getWksId(String rawId) { + private Optional getWksId(String rawId) { String searchResults = null; // TODO Look out for performance improvement through caching Optional optionalRawRecord = storageService.getRecord(rawId); if (!optionalRawRecord.isPresent()) { - return rawId; + return Optional.ofNullable(null); } String rawRecordJson = optionalRawRecord.get(); SearchRequestPayload searchRequestPayload = prepareSearchQueryPayload(rawRecordJson); @@ -149,16 +227,16 @@ public class RelationshipBlockHandler { return extractWksIdFromSearchResults(searchResults, rawId, rawKind); } - private String extractWksIdFromSearchResults(String searchResults, String rawId, Kind rawKind) { + private Optional extractWksIdFromSearchResults(String searchResults, String rawId, Kind rawKind) { List wksInfos = getWksInfos(searchResults); if (noWksFound(wksInfos)) { LOGGER.info("No matching WKS found for record id"); - return rawId; + return Optional.ofNullable(null); } return getUniqueWksId(rawId, wksInfos, rawKind); } - private String getUniqueWksId(String rawId, List wksInfos, Kind rawKind) { + private Optional getUniqueWksId(String rawId, List wksInfos, Kind rawKind) { // Add the logic for filtering WKS Records on the below criteria till unique Record found // 1. Choose records created by WKS service only // 2. Choose records having same universe as that of the mapping file @@ -169,7 +247,7 @@ public class RelationshipBlockHandler { LOGGER.info("Total {} Records created by WKS service", wksGeneratedByWKSService.size()); if(wksGeneratedByWKSService.isEmpty()) { - return rawId; + return Optional.ofNullable(null); } String targetSchemaAuthority = targetSchemaKindValue.split(Constants.COLON_SEPARATOR)[0]; @@ -178,18 +256,18 @@ public class RelationshipBlockHandler { LOGGER.info("Total {} Records have same authority as that of target schema", wksGeneratedByWKSService.size()); if(wksFromGivenAuthority.isEmpty()) { - return rawId; + return Optional.ofNullable(null); } if(wksFromGivenAuthority.size() == 1) { - return wksFromGivenAuthority.get(0).getId(); + return Optional.of(wksFromGivenAuthority.get(0).getId()); } List wksSortedOnMajorVersion = wksFromGivenAuthority.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList()); LOGGER.info("Unique WKS Record with id: {} found for relationship with Record {}", wksSortedOnMajorVersion.get(0).getId(), rawId); - return wksSortedOnMajorVersion.get(0).getId(); + return Optional.of(wksSortedOnMajorVersion.get(0).getId()); } private List getWKSRecordsCreatedByWKSService(String rawId, List wksInfos, Kind rawKind) { @@ -197,7 +275,7 @@ public class RelationshipBlockHandler { String majorVersion = rawKind.getVersion().split("\\.")[0]; // TODO Look out for performance improvement through caching (FYI: Once mapping service comes // online the entire logic of datastore look up will become obsolete) - List mappingInfos = mappingStore.getMappingInfo(rawKind.getAuthority(), rawKind.getEntityType(), rawKind.getSource(), majorVersion); + List mappingInfos = mappingService.getMappingInfos(rawKind.getAuthority(), rawKind.getEntityType(), rawKind.getSource(), majorVersion); SetwksRecordIds = new HashSet<>(); for(MappingInfo mappingInfo : mappingInfos) { String targetSchemaKind = generateKind(mappingInfo.getTargetSchemaAuthority(), mappingInfo.getTargetSchemaSource(), mappingInfo.getTargetEntityType(), mappingInfo.getTargetSchemaMajorVersion()); @@ -227,71 +305,10 @@ public class RelationshipBlockHandler { return wksRecordId.split("\\.")[1]; } - private String retrieveUniqueWksIdBasedOnPriority(String rawId, List wksWithLatestMajorVersionInKind) { - Optional optionalUniqueWksGenerateByWksService = fetchWksGeneratedByWksService(rawId, - wksWithLatestMajorVersionInKind); - return optionalUniqueWksGenerateByWksService - .orElseGet(() -> retrieveWksIdWithLatestRecordVersion(wksWithLatestMajorVersionInKind)); - } - - private String retrieveWksIdWithLatestRecordVersion(List wksWithLatestMajorVersionInKind) { - List sortWksByLatestRecordVersion = wksWithLatestMajorVersionInKind.stream() - .sorted((o1, o2) -> (int) (o2.getVersion() - o1.getVersion())) - .collect(Collectors.toList()); - return sortWksByLatestRecordVersion.get(0).getId(); - } - - private boolean multipleMatchingWksHavingSameMajorVersion(List wksWithLatestMajorVersionInKind) { - return wksWithLatestMajorVersionInKind.size() != 1; - } - - private Optional fetchWksGeneratedByWksService(String rawId, - List wksWithLatestMajorVersionInKind) { - List wksCreatedByWksService = retrieveWksCreatedByWksService(wksWithLatestMajorVersionInKind, rawId); - if (noUniqueWksFound(wksCreatedByWksService)) { - return Optional.empty(); - } else { - Tree wksRecordTree = wksCreatedByWksService.get(0); - return Optional.of(wksRecordTree.get("id").asString()); - } - } - - private List retrieveWksCreatedByWksService(List wksWithLatestMajorVersionInKind, String rawId) { - return wksWithLatestMajorVersionInKind - .stream() - .map(wksInfo -> storageService.getRecord(wksInfo.getId())) - .filter(Optional::isPresent) - .map(Optional::get) - .map(JsonTreeUtil::toTree) - .filter(wksRecordTree -> isWksCreatedByWksService(wksRecordTree, rawId)) - .collect(Collectors.toList()); - } - - private boolean noUniqueWksFound(List wksCreatedByWksService) { - return wksCreatedByWksService.size() != 1; - } - - private List retrieveWksWithLatestMajorVersionInKind(List wksInfos) { - List sortedWksInfos = sortWKSByMajorVersion(wksInfos); - Integer latestMajorVersion = sortedWksInfos.get(0).fetchMajorVersionFromKind(); - return sortedWksInfos - .stream() - .filter(wksHavingLatestMajorVersion(latestMajorVersion)) - .collect(Collectors.toList()); - } - private boolean noWksFound(List wksInfos) { return wksInfos.isEmpty(); } - private List sortWKSByMajorVersion(List wksInfos) { - return wksInfos.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList()); - } - - private Predicate wksHavingLatestMajorVersion(Integer latestMajorVersion) { - return wksInfo -> latestMajorVersion.equals(wksInfo.fetchMajorVersionFromKind()); - } - private List getWksInfos(String searchResults) { try { Tree searchResult = new Tree(searchResults); @@ -314,12 +331,4 @@ public class RelationshipBlockHandler { SearchQuery searchQuery = new SearchByAncestryQuery(rawRecordId, rawRecordVersion); return new SearchRequestPayload(wksKind, searchQuery); } - - private boolean isWksCreatedByWksService(Tree wksRecordTree, String rawId) { - if (null != wksRecordTree.get("id") && null != wksRecordTree.get("kind")) { - String generatedWksId = WksIdGenerator.createRecordId(rawId, wksRecordTree.get("kind").asString()); - return generatedWksId.equals(wksRecordTree.get("id").asString()); - } - return false; - } } diff --git a/wks-core/src/main/java/org/opengroup/osdu/wks/util/WksIdGenerator.java b/wks-core/src/main/java/org/opengroup/osdu/wks/util/WksIdGenerator.java index bdaad29ed1f92b2c54c82a9e0545ae85411bd1d8..4c01a7771a86f00acb7cf15ae07606ec16f03931 100644 --- a/wks-core/src/main/java/org/opengroup/osdu/wks/util/WksIdGenerator.java +++ b/wks-core/src/main/java/org/opengroup/osdu/wks/util/WksIdGenerator.java @@ -1,7 +1,6 @@ package org.opengroup.osdu.wks.util; import java.nio.charset.StandardCharsets; -import java.util.stream.Stream; import com.google.common.hash.Hashing; import org.opengroup.osdu.wks.constants.Constants; diff --git a/wks-core/src/test/java/org/opengroup/osdu/wks/util/RelationshipBlockHandlerTest.java b/wks-core/src/test/java/org/opengroup/osdu/wks/util/RelationshipBlockHandlerTest.java index 5302faa0adf9eef0392a90785975ea23d480e8ee..b290a7d646253b3469260ab1736e00c8e53ada56 100644 --- a/wks-core/src/test/java/org/opengroup/osdu/wks/util/RelationshipBlockHandlerTest.java +++ b/wks-core/src/test/java/org/opengroup/osdu/wks/util/RelationshipBlockHandlerTest.java @@ -23,7 +23,7 @@ import org.mockito.junit.jupiter.MockitoExtension; import org.opengroup.osdu.wks.exceptions.ApplicationException; import org.opengroup.osdu.wks.model.MappingInfo; import org.opengroup.osdu.wks.model.UpdatedRelationshipBlock; -import org.opengroup.osdu.wks.provider.interfaces.MappingStore; +import org.opengroup.osdu.wks.service.MappingService; import org.opengroup.osdu.wks.service.SearchService; import org.opengroup.osdu.wks.service.StorageService; import org.springframework.core.io.ClassPathResource; @@ -55,7 +55,10 @@ class RelationshipBlockHandlerTest { private SearchService searchService; @Mock - private MappingStore mappingStore; + private MappingService mappingService; + + @Mock + private KindUtil kindUtil; @InjectMocks private RelationshipBlockHandler relationshipBlockHandler; @@ -69,17 +72,66 @@ class RelationshipBlockHandlerTest { String searchResults = findJsonTree("search_results_wks_generated_record.json").toString(); List mappingInfoList = new ArrayList<>(); - MappingInfo mappingInfo = new MappingInfo(); - mappingInfo.setTargetSchemaAuthority(AUTHORITY); - mappingInfo.setTargetEntityType(ENTITY); - mappingInfo.setTargetSchemaSource(SOURCE); - mappingInfo.setTargetSchemaMajorVersion(MAJOR_VERSION); + MappingInfo mappingInfo = createMappingInfo(); + + mappingInfoList.add(mappingInfo); + + when(storageService.getRecord(any(String.class))).thenReturn(Optional.of(wksRecordStr)); + when(searchService.query(any(String.class))).thenReturn(searchResults); + when(mappingService.getMappingInfos(eq(AUTHORITY), eq(ENTITY), eq(SOURCE), eq(MAJOR_VERSION))).thenReturn(mappingInfoList); + + UpdatedRelationshipBlock response = relationshipBlockHandler + .updateRelationshipBlockWithWksIds(rawRelationshipsBlockTree, TARGET_SCHEMA_KIND); + + assertNotNull(response); + assertNotNull(response.getRelationshipTree()); + assertTrue(response.getPendingEnitites().size() == 0); + } + + @Test + public void testRelationshipForOneToManyRelationshipWhenNoRelatedWksIdFound() throws IOException, ApplicationException { + Tree rawRecordWithRelationship = findJsonTree("raw_record_valid_relationship_block_oneToMany.json"); + Tree rawRelationshipsBlockTree = rawRecordWithRelationship.get("data.relationships"); + + String wksRecordStr = findJsonTree("wks_record.json").toString(); + String searchResults = findJsonTree("search_results_wks_not_found.json").toString(); + + List mappingInfoList = new ArrayList<>(); + MappingInfo mappingInfo = createMappingInfo(); + + mappingInfoList.add(mappingInfo); + + when(storageService.getRecord(any(String.class))).thenReturn(Optional.of(wksRecordStr)); + when(searchService.query(any(String.class))).thenReturn(searchResults); + when(mappingService.getMappingInfos(eq(AUTHORITY), eq(ENTITY), eq(SOURCE), eq(MAJOR_VERSION))).thenReturn(mappingInfoList); + + UpdatedRelationshipBlock response = relationshipBlockHandler + .updateRelationshipBlockWithWksIds(rawRelationshipsBlockTree, TARGET_SCHEMA_KIND); + + assertNotNull(response); + assertNotNull(response.getRelationshipTree()); + assertTrue(response.getPendingEnitites().size() != 0); + } + + @Test + public void testRelationshipForOneToManyRelationshipWhenNoRelatedWksIdFoundForBoth() + throws IOException, ApplicationException { + Tree rawRecordWithRelationship = findJsonTree("raw_record_valid_relationship_block_oneToMany.json"); + Tree rawRelationshipsBlockTree = rawRecordWithRelationship.get("data.relationships"); + + String wksRecordStr = findJsonTree("wks_record.json").toString(); + String searchResults = findJsonTree("search_results_wks_not_found.json").toString(); + + List mappingInfoList = new ArrayList<>(); + MappingInfo mappingInfo = createMappingInfo(); mappingInfoList.add(mappingInfo); + when(kindUtil.prepareKind(any(), any(), any(), any(), any(), any())).thenReturn(TARGET_SCHEMA_KIND); + when(kindUtil.retrieveAuthorityName(TARGET_SCHEMA_KIND)).thenReturn(AUTHORITY); when(storageService.getRecord(any(String.class))).thenReturn(Optional.of(wksRecordStr)); when(searchService.query(any(String.class))).thenReturn(searchResults); - when(mappingStore.getMappingInfo(eq(AUTHORITY), eq(ENTITY), eq(SOURCE), eq(MAJOR_VERSION))).thenReturn(mappingInfoList); + when(mappingService.getMappingInfos(any(), any(), any(), any())).thenReturn(mappingInfoList); UpdatedRelationshipBlock response = relationshipBlockHandler .updateRelationshipBlockWithWksIds(rawRelationshipsBlockTree, TARGET_SCHEMA_KIND); @@ -88,7 +140,35 @@ class RelationshipBlockHandlerTest { assertNotNull(response.getRelationshipTree()); assertTrue(response.getPendingEnitites().size() == 0); } + + @Test + public void testRelationshipForOneToManyRelationshipWhenNoRelatedWksIdFoundForOne() + throws IOException, ApplicationException { + Tree rawRecordWithRelationship = findJsonTree("raw_record_valid_relationship_block_oneToMany.json"); + Tree rawRelationshipsBlockTree = rawRecordWithRelationship.get("data.relationships"); + + String wksRecordStr = findJsonTree("wks_record.json").toString(); + String searchResults = findJsonTree("search_results_wks_not_found.json").toString(); + + List mappingInfoList = new ArrayList<>(); + MappingInfo mappingInfo = createMappingInfo(); + + mappingInfoList.add(mappingInfo); + + when(kindUtil.prepareKind(any(), any(), any(), any(), any(), any())).thenReturn(TARGET_SCHEMA_KIND); + when(kindUtil.retrieveAuthorityName(TARGET_SCHEMA_KIND)).thenReturn(AUTHORITY); + when(storageService.getRecord(any(String.class))).thenReturn(Optional.of(wksRecordStr)); + when(searchService.query(any(String.class))).thenReturn(searchResults); + when(mappingService.getMappingInfos(any(), any(), any(), any())).thenReturn(mappingInfoList); + UpdatedRelationshipBlock response = relationshipBlockHandler + .updateRelationshipBlockWithWksIds(rawRelationshipsBlockTree, TARGET_SCHEMA_KIND); + + assertNotNull(response); + assertNotNull(response.getRelationshipTree()); + assertTrue(response.getPendingEnitites().size() == 0); + } + @Test public void testRelationshipForOneToOneRelationshipWhenNoWKSRecordCreatedByService() throws IOException, ApplicationException { Tree rawRecordWithRelationship = findJsonTree("raw_record_valid_relationship_block_oneToOne.json"); @@ -98,17 +178,13 @@ class RelationshipBlockHandlerTest { String searchResults = findJsonTree("search_results_wks_not_found.json").toString(); List mappingInfoList = new ArrayList<>(); - MappingInfo mappingInfo = new MappingInfo(); - mappingInfo.setTargetSchemaAuthority(AUTHORITY); - mappingInfo.setTargetEntityType(ENTITY); - mappingInfo.setTargetSchemaSource(SOURCE); - mappingInfo.setTargetSchemaMajorVersion(MAJOR_VERSION); + MappingInfo mappingInfo = createMappingInfo(); mappingInfoList.add(mappingInfo); when(storageService.getRecord(any(String.class))).thenReturn(Optional.of(wksRecordStr)); when(searchService.query(any(String.class))).thenReturn(searchResults); - when(mappingStore.getMappingInfo(eq(AUTHORITY), eq(ENTITY), eq(SOURCE), eq(MAJOR_VERSION))).thenReturn(mappingInfoList); + when(mappingService.getMappingInfos(eq(AUTHORITY), eq(ENTITY), eq(SOURCE), eq(MAJOR_VERSION))).thenReturn(mappingInfoList); UpdatedRelationshipBlock response = relationshipBlockHandler .updateRelationshipBlockWithWksIds(rawRelationshipsBlockTree, TARGET_SCHEMA_KIND); @@ -147,7 +223,7 @@ class RelationshipBlockHandlerTest { mappingInfoList.add(mappingInfo1); mappingInfoList.add(mappingInfo2); - when(mappingStore.getMappingInfo(eq(AUTHORITY), eq(ENTITY), eq(SOURCE), eq(MAJOR_VERSION))).thenReturn(mappingInfoList); + when(mappingService.getMappingInfos(eq(AUTHORITY), eq(ENTITY), eq(SOURCE), eq(MAJOR_VERSION))).thenReturn(mappingInfoList); when(storageService.getRecord(any())).thenReturn(Optional.of(wksRecordStr)); when(searchService.query(any(String.class))).thenReturn(searchResults); @@ -177,7 +253,7 @@ class RelationshipBlockHandlerTest { mappingInfoList.add(mappingInfo1); mappingInfoList.add(mappingInfo2); - when(mappingStore.getMappingInfo(eq(AUTHORITY), eq(ENTITY), eq(SOURCE), eq(MAJOR_VERSION))).thenReturn(mappingInfoList); + when(mappingService.getMappingInfos(eq(AUTHORITY), eq(ENTITY), eq(SOURCE), eq(MAJOR_VERSION))).thenReturn(mappingInfoList); when(storageService.getRecord(any())).thenReturn(Optional.of(wksRecordStr)); when(searchService.query(any(String.class))).thenReturn(searchResults); @@ -225,5 +301,14 @@ class RelationshipBlockHandlerTest { return mapper.readValue(file, new TypeReference>() { }); } + + private MappingInfo createMappingInfo() { + MappingInfo mappingInfo = new MappingInfo(); + mappingInfo.setTargetSchemaAuthority(AUTHORITY); + mappingInfo.setTargetEntityType(ENTITY); + mappingInfo.setTargetSchemaSource(SOURCE); + mappingInfo.setTargetSchemaMajorVersion(MAJOR_VERSION); + return mappingInfo; + } }