Commit 732dd41b authored by Khilesh Sahu's avatar Khilesh Sahu Committed by ethiraj krishnamanaidu
Browse files

WKS Relationship Fixes implementation

parent 118bc73b
...@@ -131,7 +131,8 @@ Feature: Covers all positive and negative test cases around WKS transformation s ...@@ -131,7 +131,8 @@ Feature: Covers all positive and negative test cases around WKS transformation s
| relatedEntityPayload | parentPayload | expectedRelationshipBlock | | 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" | | "/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 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 <relatedEntityPayload> to persist a record say raw1 Given I hit Storage service put end point with <relatedEntityPayload> to persist a record say raw1
And I verify that three wks records are created for this raw record 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 ...@@ -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 <parentPayload> whose raw record contains relationship to record raw1 When I hit Storage service put end point with <parentPayload> whose raw record contains relationship to record raw1
Then Transformed record should contain relationship to wks with same authority of raw1 <relatedEntityPayload> and with higher major version as per <expectedRelationshipBlock> Then Transformed record should contain relationship to wks with same authority of raw1 <relatedEntityPayload> and with higher major version as per <expectedRelationshipBlock>
Examples: Examples:
| relatedEntityPayload | relatedEntityPayload2 | parentPayload | expectedRelationshipBlock | | 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" | | "/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" |
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
"data": { "data": {
"relationships": { "relationships": {
"wellbore": { "wellbore": {
"id": "non:existent:record-recordDoesNotExistInSystem" "id": "non:existent:record-recordDoesNotExistInSystem-0a03169487847"
} }
} }
}, },
......
...@@ -4,6 +4,8 @@ import lombok.AllArgsConstructor; ...@@ -4,6 +4,8 @@ import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.Setter; import lombok.Setter;
import lombok.ToString;
import org.opengroup.osdu.wks.enums.MappingScope; import org.opengroup.osdu.wks.enums.MappingScope;
import java.sql.Timestamp; import java.sql.Timestamp;
...@@ -12,6 +14,7 @@ import java.sql.Timestamp; ...@@ -12,6 +14,7 @@ import java.sql.Timestamp;
@Setter @Setter
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
@ToString
public class MappingInfo { public class MappingInfo {
private String id; private String id;
private String mappingId; private String mappingId;
......
package org.opengroup.osdu.wks.service; package org.opengroup.osdu.wks.service;
import org.opengroup.osdu.wks.exceptions.ApplicationException; import org.opengroup.osdu.wks.exceptions.ApplicationException;
import org.opengroup.osdu.wks.model.MappingInfo;
import org.opengroup.osdu.wks.model.MappingsModel; import org.opengroup.osdu.wks.model.MappingsModel;
import java.util.List; import java.util.List;
...@@ -8,4 +9,6 @@ import java.util.List; ...@@ -8,4 +9,6 @@ import java.util.List;
public interface MappingService { public interface MappingService {
List<MappingsModel> getMapping(String kind) throws ApplicationException; List<MappingsModel> getMapping(String kind) throws ApplicationException;
List<MappingInfo> getMappingInfos(String authority, String entity, String source, String majorVersion);
} }
...@@ -31,5 +31,10 @@ public class MappingServiceImpl implements MappingService { ...@@ -31,5 +31,10 @@ public class MappingServiceImpl implements MappingService {
} }
return mappingStore.getMappingFilesFromStorage(mappingInfos); return mappingStore.getMappingFilesFromStorage(mappingInfos);
} }
@Override
public List<MappingInfo> getMappingInfos(final String authority, final String entity, final String source, final String majorVersion) {
return mappingStore.getMappingInfo(authority, entity, source, majorVersion);
}
} }
...@@ -7,7 +7,6 @@ import java.util.Iterator; ...@@ -7,7 +7,6 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.opengroup.osdu.wks.constants.Constants; import org.opengroup.osdu.wks.constants.Constants;
...@@ -21,7 +20,7 @@ import org.opengroup.osdu.wks.model.SearchByAncestryQuery; ...@@ -21,7 +20,7 @@ import org.opengroup.osdu.wks.model.SearchByAncestryQuery;
import org.opengroup.osdu.wks.model.SearchQuery; import org.opengroup.osdu.wks.model.SearchQuery;
import org.opengroup.osdu.wks.model.SearchRequestPayload; import org.opengroup.osdu.wks.model.SearchRequestPayload;
import org.opengroup.osdu.wks.model.UpdatedRelationshipBlock; 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.SearchService;
import org.opengroup.osdu.wks.service.StorageService; import org.opengroup.osdu.wks.service.StorageService;
import org.slf4j.Logger; import org.slf4j.Logger;
...@@ -45,17 +44,20 @@ public class RelationshipBlockHandler { ...@@ -45,17 +44,20 @@ public class RelationshipBlockHandler {
private String targetSchemaKindValue; private String targetSchemaKindValue;
private MappingStore mappingStore; private MappingService mappingService;
private KindUtil kindUtil;
@Autowired @Autowired
public RelationshipBlockHandler( public RelationshipBlockHandler(
StorageService storageService, StorageService storageService,
SearchService searchService, SearchService searchService,
MappingStore mappingStore) { MappingService mappingService,
KindUtil kindUtil) {
this.storageService = storageService; this.storageService = storageService;
this.searchService = searchService; this.searchService = searchService;
this.mappingStore = mappingStore; this.mappingService = mappingService;
this.kindUtil = kindUtil;
} }
public UpdatedRelationshipBlock updateRelationshipBlockWithWksIds(Tree rawRelationshipsBlockTree, String targetSchemaKind) { public UpdatedRelationshipBlock updateRelationshipBlockWithWksIds(Tree rawRelationshipsBlockTree, String targetSchemaKind) {
...@@ -119,21 +121,97 @@ public class RelationshipBlockHandler { ...@@ -119,21 +121,97 @@ public class RelationshipBlockHandler {
private Relationship getRelationshipBlockWithWksIds(Relationship rawRelationship) throws ApplicationException { private Relationship getRelationshipBlockWithWksIds(Relationship rawRelationship) throws ApplicationException {
List<String> rawIds = rawRelationship.getIds(); List<String> rawIds = rawRelationship.getIds();
List<String> wksIds = getWksIds(rawIds); List<String> wksIds = getWksIds(rawIds);
rawRelationship.setIds(wksIds); rawRelationship.setIds(wksIds);
return rawRelationship; return rawRelationship;
} }
private List<String> getWksIds(List<String> rawIds) { private String getRelatedRecordIdUsingMapping(String relatedRecordId) {
return rawIds.stream().map(this::getWksId).collect(Collectors.toList()); Optional<String> rawRecordOption = storageService.getRecord(relatedRecordId);
if (rawRecordOption.isPresent()) {
Optional<String> relatedRecordKindOption = getRelatedRecordKind(rawRecordOption.get());
if (relatedRecordKindOption.isPresent()) {
Optional<String> relatedRecordTargetKind = getTargetKind(relatedRecordKindOption.get());
if (relatedRecordTargetKind.isPresent()) {
return WksIdGenerator.createRecordId(relatedRecordId, relatedRecordTargetKind.get());
}
}
}
return relatedRecordId;
}
private Optional<String> 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<String> getTargetKind(String kind) {
Optional<MappingInfo> 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<MappingInfo>
*/
private Optional<MappingInfo> getLatestMappingInfoWithRelatedAuthority(String kind) {
List<MappingInfo> 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<String> getWksIds(List<String> relatedRawIds) {
List<String> wksIds = new ArrayList<>();
for(String relatedRawId : relatedRawIds) {
Optional<String> 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<String> getWksId(String rawId) {
String searchResults = null; String searchResults = null;
// TODO Look out for performance improvement through caching // TODO Look out for performance improvement through caching
Optional<String> optionalRawRecord = storageService.getRecord(rawId); Optional<String> optionalRawRecord = storageService.getRecord(rawId);
if (!optionalRawRecord.isPresent()) { if (!optionalRawRecord.isPresent()) {
return rawId; return Optional.ofNullable(null);
} }
String rawRecordJson = optionalRawRecord.get(); String rawRecordJson = optionalRawRecord.get();
SearchRequestPayload searchRequestPayload = prepareSearchQueryPayload(rawRecordJson); SearchRequestPayload searchRequestPayload = prepareSearchQueryPayload(rawRecordJson);
...@@ -149,16 +227,16 @@ public class RelationshipBlockHandler { ...@@ -149,16 +227,16 @@ public class RelationshipBlockHandler {
return extractWksIdFromSearchResults(searchResults, rawId, rawKind); return extractWksIdFromSearchResults(searchResults, rawId, rawKind);
} }
private String extractWksIdFromSearchResults(String searchResults, String rawId, Kind rawKind) { private Optional<String> extractWksIdFromSearchResults(String searchResults, String rawId, Kind rawKind) {
List<WksInfo> wksInfos = getWksInfos(searchResults); List<WksInfo> wksInfos = getWksInfos(searchResults);
if (noWksFound(wksInfos)) { if (noWksFound(wksInfos)) {
LOGGER.info("No matching WKS found for record id"); LOGGER.info("No matching WKS found for record id");
return rawId; return Optional.ofNullable(null);
} }
return getUniqueWksId(rawId, wksInfos, rawKind); return getUniqueWksId(rawId, wksInfos, rawKind);
} }
private String getUniqueWksId(String rawId, List<WksInfo> wksInfos, Kind rawKind) { private Optional<String> getUniqueWksId(String rawId, List<WksInfo> wksInfos, Kind rawKind) {
// Add the logic for filtering WKS Records on the below criteria till unique Record found // Add the logic for filtering WKS Records on the below criteria till unique Record found
// 1. Choose records created by WKS service only // 1. Choose records created by WKS service only
// 2. Choose records having same universe as that of the mapping file // 2. Choose records having same universe as that of the mapping file
...@@ -169,7 +247,7 @@ public class RelationshipBlockHandler { ...@@ -169,7 +247,7 @@ public class RelationshipBlockHandler {
LOGGER.info("Total {} Records created by WKS service", wksGeneratedByWKSService.size()); LOGGER.info("Total {} Records created by WKS service", wksGeneratedByWKSService.size());
if(wksGeneratedByWKSService.isEmpty()) { if(wksGeneratedByWKSService.isEmpty()) {
return rawId; return Optional.ofNullable(null);
} }
String targetSchemaAuthority = targetSchemaKindValue.split(Constants.COLON_SEPARATOR)[0]; String targetSchemaAuthority = targetSchemaKindValue.split(Constants.COLON_SEPARATOR)[0];
...@@ -178,18 +256,18 @@ public class RelationshipBlockHandler { ...@@ -178,18 +256,18 @@ public class RelationshipBlockHandler {
LOGGER.info("Total {} Records have same authority as that of target schema", wksGeneratedByWKSService.size()); LOGGER.info("Total {} Records have same authority as that of target schema", wksGeneratedByWKSService.size());
if(wksFromGivenAuthority.isEmpty()) { if(wksFromGivenAuthority.isEmpty()) {
return rawId; return Optional.ofNullable(null);
} }
if(wksFromGivenAuthority.size() == 1) { if(wksFromGivenAuthority.size() == 1) {
return wksFromGivenAuthority.get(0).getId(); return Optional.of(wksFromGivenAuthority.get(0).getId());
} }
List<WksInfo> wksSortedOnMajorVersion = wksFromGivenAuthority.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList()); List<WksInfo> 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); 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<WksInfo> getWKSRecordsCreatedByWKSService(String rawId, List<WksInfo> wksInfos, Kind rawKind) { private List<WksInfo> getWKSRecordsCreatedByWKSService(String rawId, List<WksInfo> wksInfos, Kind rawKind) {
...@@ -197,7 +275,7 @@ public class RelationshipBlockHandler { ...@@ -197,7 +275,7 @@ public class RelationshipBlockHandler {
String majorVersion = rawKind.getVersion().split("\\.")[0]; String majorVersion = rawKind.getVersion().split("\\.")[0];
// TODO Look out for performance improvement through caching (FYI: Once mapping service comes // TODO Look out for performance improvement through caching (FYI: Once mapping service comes
// online the entire logic of datastore look up will become obsolete) // online the entire logic of datastore look up will become obsolete)
List<MappingInfo> mappingInfos = mappingStore.getMappingInfo(rawKind.getAuthority(), rawKind.getEntityType(), rawKind.getSource(), majorVersion); List<MappingInfo> mappingInfos = mappingService.getMappingInfos(rawKind.getAuthority(), rawKind.getEntityType(), rawKind.getSource(), majorVersion);
Set<String>wksRecordIds = new HashSet<>(); Set<String>wksRecordIds = new HashSet<>();
for(MappingInfo mappingInfo : mappingInfos) { for(MappingInfo mappingInfo : mappingInfos) {
String targetSchemaKind = generateKind(mappingInfo.getTargetSchemaAuthority(), mappingInfo.getTargetSchemaSource(), mappingInfo.getTargetEntityType(), mappingInfo.getTargetSchemaMajorVersion()); String targetSchemaKind = generateKind(mappingInfo.getTargetSchemaAuthority(), mappingInfo.getTargetSchemaSource(), mappingInfo.getTargetEntityType(), mappingInfo.getTargetSchemaMajorVersion());
...@@ -227,71 +305,10 @@ public class RelationshipBlockHandler { ...@@ -227,71 +305,10 @@ public class RelationshipBlockHandler {
return wksRecordId.split("\\.")[1]; return wksRecordId.split("\\.")[1];
} }
private String retrieveUniqueWksIdBasedOnPriority(String rawId, List<WksInfo> wksWithLatestMajorVersionInKind) {
Optional<String> optionalUniqueWksGenerateByWksService = fetchWksGeneratedByWksService(rawId,
wksWithLatestMajorVersionInKind);
return optionalUniqueWksGenerateByWksService
.orElseGet(() -> retrieveWksIdWithLatestRecordVersion(wksWithLatestMajorVersionInKind));
}
private String retrieveWksIdWithLatestRecordVersion(List<WksInfo> wksWithLatestMajorVersionInKind) {
List<WksInfo> sortWksByLatestRecordVersion = wksWithLatestMajorVersionInKind.stream()
.sorted((o1, o2) -> (int) (o2.getVersion() - o1.getVersion()))
.collect(Collectors.toList());
return sortWksByLatestRecordVersion.get(0).getId();
}
private boolean multipleMatchingWksHavingSameMajorVersion(List<WksInfo> wksWithLatestMajorVersionInKind) {
return wksWithLatestMajorVersionInKind.size() != 1;
}
private Optional<String> fetchWksGeneratedByWksService(String rawId,
List<WksInfo> wksWithLatestMajorVersionInKind) {
List<Tree> 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<Tree> retrieveWksCreatedByWksService(List<WksInfo> 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<Tree> wksCreatedByWksService) {
return wksCreatedByWksService.size() != 1;
}
private List<WksInfo> retrieveWksWithLatestMajorVersionInKind(List<WksInfo> wksInfos) {
List<WksInfo> sortedWksInfos = sortWKSByMajorVersion(wksInfos);
Integer latestMajorVersion = sortedWksInfos.get(0).fetchMajorVersionFromKind();
return sortedWksInfos
.stream()
.filter(wksHavingLatestMajorVersion(latestMajorVersion))
.collect(Collectors.toList());
}
private boolean noWksFound(List<WksInfo> wksInfos) { private boolean noWksFound(List<WksInfo> wksInfos) {
return wksInfos.isEmpty(); return wksInfos.isEmpty();
} }
private List<WksInfo> sortWKSByMajorVersion(List<WksInfo> wksInfos) {
return wksInfos.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
}
private Predicate<WksInfo> wksHavingLatestMajorVersion(Integer latestMajorVersion) {
return wksInfo -> latestMajorVersion.equals(wksInfo.fetchMajorVersionFromKind());
}
private List<WksInfo> getWksInfos(String searchResults) { private List<WksInfo> getWksInfos(String searchResults) {
try { try {
Tree searchResult = new Tree(searchResults); Tree searchResult = new Tree(searchResults);
...@@ -314,12 +331,4 @@ public class RelationshipBlockHandler { ...@@ -314,12 +331,4 @@ public class RelationshipBlockHandler {
SearchQuery searchQuery = new SearchByAncestryQuery(rawRecordId, rawRecordVersion); SearchQuery searchQuery = new SearchByAncestryQuery(rawRecordId, rawRecordVersion);
return new SearchRequestPayload(wksKind, searchQuery); 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;
}
} }
package org.opengroup.osdu.wks.util; package org.opengroup.osdu.wks.util;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.stream.Stream;
import com.google.common.hash.Hashing; import com.google.common.hash.Hashing;
import org.opengroup.osdu.wks.constants.Constants; import org.opengroup.osdu.wks.constants.Constants;
......
...@@ -23,7 +23,7 @@ import org.mockito.junit.jupiter.MockitoExtension; ...@@ -23,7 +23,7 @@ import org.mockito.junit.jupiter.MockitoExtension;
import org.opengroup.osdu.wks.exceptions.ApplicationException; import org.opengroup.osdu.wks.exceptions.ApplicationException;
import org.opengroup.osdu.wks.model.MappingInfo; import org.opengroup.osdu.wks.model.MappingInfo;
import org.opengroup.osdu.wks.model.UpdatedRelationshipBlock; 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.SearchService;
import org.opengroup.osdu.wks.service.StorageService; import org.opengroup.osdu.wks.service.StorageService;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
...@@ -55,7 +55,10 @@ class RelationshipBlockHandlerTest { ...@@ -55,7 +55,10 @@ class RelationshipBlockHandlerTest {
private SearchService searchService; private SearchService searchService;
@Mock @Mock
private MappingStore mappingStore; private MappingService mappingService;
@Mock
private KindUtil kindUtil;
@InjectMocks @InjectMocks
private RelationshipBlockHandler relationshipBlockHandler; private RelationshipBlockHandler relationshipBlockHandler;
...@@ -69,17 +72,66 @@ class RelationshipBlockHandlerTest { ...@@ -69,17 +72,66 @@ class RelationshipBlockHandlerTest {
String searchResults = findJsonTree("search_results_wks_generated_record.json").toString(); String searchResults = findJsonTree("search_results_wks_generated_record.json").toString();
List<MappingInfo> mappingInfoList = new ArrayList<>(); List<MappingInfo> mappingInfoList = new ArrayList<>();
MappingInfo mappingInfo = new MappingInfo(); MappingInfo mappingInfo = createMappingInfo();
mappingInfo.setTargetSchemaAuthority(AUTHORITY);
mappingInfo.setTargetEntityType(ENTITY); mappingInfoList.add(mappingInfo);
mappingInfo.setTargetSchemaSource(SOURCE);
mappingInfo.setTargetSchemaMajorVersion(MAJOR_VERSION); 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<MappingInfo> 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);