Commit 6152ad29 authored by devesh bajpai's avatar devesh bajpai
Browse files

Merge branch 'pick-latest-version-of-mapping-file' into 'master'

Pick latest version of mapping file

See merge request !47
parents 16601004 76850ea6
Pipeline #38104 passed with stages
in 20 minutes and 38 seconds
package org.opengroup.osdu.wks.service.mapping;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
import org.opengroup.osdu.wks.service.transformer.model.mapping.MappingDefinition;
import org.opengroup.osdu.wks.service.transformer.model.mapping.MappingInfo;
......@@ -10,6 +9,7 @@ import org.opengroup.osdu.wks.provider.interfaces.MappingStore;
import org.opengroup.osdu.wks.util.KindUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import static java.util.stream.Collectors.groupingBy;
@Service
public class MappingService {
......@@ -25,7 +25,7 @@ public class MappingService {
String entityNamePrefix = kindUtil.retrieveEntityName(kind);
String sourceName = kindUtil.retrieveSourceName(kind);
int majorVersion = kindUtil.retrieveMajorVersion(kind);
List<MappingInfo> mappingInfos = mappingStore.getMappingInfo(authorityName, entityNamePrefix, sourceName, majorVersion);
List<MappingInfo> mappingInfos = getMappingInfos(authorityName, entityNamePrefix, sourceName, majorVersion);
if(mappingInfos.isEmpty()) {
return Collections.emptyList();
}
......@@ -33,7 +33,25 @@ public class MappingService {
}
public List<MappingInfo> getMappingInfos(final String authority, final String entity, final String source, final int majorVersion) {
return mappingStore.getMappingInfo(authority, entity, source, majorVersion);
List<MappingInfo> mappingInfos = mappingStore.getMappingInfo(authority, entity, source, majorVersion);
if(mappingInfos.isEmpty()) {
return Collections.emptyList();
}
return selectLatestVersionForEachMappingId(
groupByMappingId(mappingInfos));
}
private Collection<List<MappingInfo>> groupByMappingId(List<MappingInfo> mappingInfoList) {
return mappingInfoList.stream().collect(groupingBy(MappingInfo::getId)).values();
}
private List<MappingInfo> selectLatestVersionForEachMappingId(Collection<List<MappingInfo>> mappingsGroupedByMappingId) {
return mappingsGroupedByMappingId
.stream()
.map(list -> list.stream().max(Comparator.comparing(MappingInfo::getVersion)))
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.toList());
}
private List<MappingDefinition> getMappingDefinitions(List<MappingInfo> mappingInfos){
......
package org.opengroup.osdu.wks.service;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.opengroup.osdu.wks.service.transformer.model.mapping.MappingDefinition;
import org.opengroup.osdu.wks.service.transformer.model.mapping.MappingInfo;
import org.opengroup.osdu.wks.provider.interfaces.MappingStore;
import org.opengroup.osdu.wks.service.mapping.MappingService;
import org.opengroup.osdu.wks.service.transformer.model.mapping.MappingDefinition;
import org.opengroup.osdu.wks.service.transformer.model.mapping.MappingInfo;
import org.opengroup.osdu.wks.util.KindUtil;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.Collections;
import java.util.List;
import static org.junit.Assert.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
@RunWith(SpringJUnit4ClassRunner.class)
......@@ -38,8 +40,58 @@ public class MappingServiceTest {
when(kindUtil.retrieveEntityName(KIND)).thenReturn("entity");
when(kindUtil.retrieveMajorVersion(KIND)).thenReturn(1);
MappingInfo mappingInfo = new MappingInfo();
when(mappingStore.getMappingInfo("tenant1", "source", "entity", 1)).thenReturn(Collections.singletonList(mappingInfo));
mappingInfo.setId("sampleMapping");
mappingInfo.setVersion(100000);
when(mappingStore.getMappingInfo("tenant1", "entity","source", 1)).thenReturn(Collections.singletonList(mappingInfo));
List<MappingInfo> mappings = mappingService.getMappingInfos("tenant1", "entity","source", 1);
assertNotNull(mappings);
assertEquals("sampleMapping",mappings.get(0).getId());
}
// Scenario: If multiple version of same mappingId is present then pick latest version only
@Test
public void testGetMappingInfoMultipleVersionOfSameMapping() {
when(kindUtil.retrieveAuthorityName(KIND)).thenReturn("tenant1");
when(kindUtil.retrieveSourceName(KIND)).thenReturn("source");
when(kindUtil.retrieveEntityName(KIND)).thenReturn("entity");
when(kindUtil.retrieveMajorVersion(KIND)).thenReturn(1);
MappingInfo mappingInfoWithLowerVersion = new MappingInfo();
mappingInfoWithLowerVersion.setId("sampleMapping");
mappingInfoWithLowerVersion.setVersion(100000);
MappingInfo mappingInfoWithHigherVersion = new MappingInfo();
mappingInfoWithHigherVersion.setId("sampleMapping");
mappingInfoWithHigherVersion.setVersion(200000);
MappingInfo mappingInfoWithDIfferentId = new MappingInfo();
mappingInfoWithDIfferentId.setId("differentMapping");
mappingInfoWithDIfferentId.setVersion(200000);
List<MappingInfo> mappingInfo = Arrays.asList(mappingInfoWithDIfferentId,mappingInfoWithHigherVersion,mappingInfoWithLowerVersion);
when(mappingStore.getMappingInfo("tenant1", "entity","source", 1)).thenReturn(mappingInfo);
List<MappingInfo> mappings = mappingService.getMappingInfos("tenant1", "entity","source", 1);
assertNotNull(mappings);
//Then only one mapping is returned
assertEquals(2,mappings.size());
// And returned mapping is of higher mapping version.
assertEquals("sampleMapping", mappings.get(0).getId());
assertEquals(200000, mappings.get(0).getVersion());
// And returned mapping is of higher mapping version.
assertEquals("differentMapping", mappings.get(1).getId());
assertEquals(200000, mappings.get(1).getVersion());
}
@Test
public void getMapping(){
when(kindUtil.retrieveAuthorityName(KIND)).thenReturn("tenant1");
when(kindUtil.retrieveSourceName(KIND)).thenReturn("source");
when(kindUtil.retrieveEntityName(KIND)).thenReturn("entity");
when(kindUtil.retrieveMajorVersion(KIND)).thenReturn(1);
MappingInfo mappingInfo = new MappingInfo();
mappingInfo.setId("sampleMapping");
mappingInfo.setVersion(100000);
when(mappingStore.getMappingInfo("tenant1", "entity","source", 1)).thenReturn(Collections.singletonList(mappingInfo));
when(mappingStore.getMappingDefinitionFromStorage(any(MappingInfo.class))).thenReturn(new MappingDefinition());
List<MappingDefinition> mappings = mappingService.getMapping(KIND);
assertNotNull(mappings);
//Then: Returned mapping definition should have same mappingInfo.
assertEquals("sampleMapping",mappings.get(0).getMappingInfo().getId());
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment