Commit a574749f authored by devesh bajpai's avatar devesh bajpai
Browse files

get latest version of mapping

parent 92d2cffe
Pipeline #36255 passed with stages
in 19 minutes and 20 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,15 +25,30 @@ 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);
if(mappingInfos.isEmpty()) {
return Collections.emptyList();
}
List<MappingInfo> mappingInfos = getMappingInfos(authorityName, entityNamePrefix, sourceName, majorVersion);
return getMappingDefinitions(mappingInfos);
}
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.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 +39,51 @@ 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);
when(mappingStore.getMappingInfo("tenant1", "entity","source", 1)).thenReturn(Collections.singletonList(
mappingInfoWithHigherVersion));
List<MappingInfo> mappings = mappingService.getMappingInfos("tenant1", "entity","source", 1);
assertNotNull(mappings);
//Then only one mapping is returned
assertEquals(1,mappings.size());
// And returned mapping is of higher mapping version.
assertEquals(200000, mappings.get(0).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());
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment