Search in sources :

Example 1 with LocationSpec

use of com.icthh.xm.ms.entity.domain.spec.LocationSpec in project xm-ms-entity by xm-online.

the class XmEntityGeneratorServiceIntTest method generateXmEntityWithLocations.

@Test
@SneakyThrows
public void generateXmEntityWithLocations() {
    XmEntity generatedEntity = xmEntityGeneratorService.generateXmEntity(ENTITY_TYPE_WITH_TAGS_AND_LOCATIONS_KEY);
    log.info(new ObjectMapper().writeValueAsString(generatedEntity));
    Set<Location> locations = generatedEntity.getLocations();
    assertFalse("No locations generated", isEmpty(locations));
    TypeSpec specType = xmEntitySpecService.findTypeByKey("TYPE1.SUBTYPE1");
    List<LocationSpec> locationSpecs = specType.getLocations();
    Set<String> locationSpecKeys = locationSpecs.stream().map(LocationSpec::getKey).collect(toSet());
    Set<String> locationKeys = locations.stream().map(Location::getTypeKey).collect(toSet());
    assertTrue("Tag type not from locations specification", locationSpecKeys.containsAll(locationKeys));
    for (val location : locations) {
        assertFalse("Name of location is empty", isBlank(location.getName()));
        assertFalse("Coordinates and address is empty", isBlank(location.getAddressLine1()) && (location.getLatitude() == null || location.getLongitude() == null));
    }
}
Also used : lombok.val(lombok.val) LocationSpec(com.icthh.xm.ms.entity.domain.spec.LocationSpec) XmEntity(com.icthh.xm.ms.entity.domain.XmEntity) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Location(com.icthh.xm.ms.entity.domain.Location) TypeSpec(com.icthh.xm.ms.entity.domain.spec.TypeSpec) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) SneakyThrows(lombok.SneakyThrows)

Example 2 with LocationSpec

use of com.icthh.xm.ms.entity.domain.spec.LocationSpec in project xm-ms-entity by xm-online.

the class XmEntityGeneratorService method generateLocations.

private Set<Location> generateLocations(LocationSpec locationSpec) {
    Set<Location> result = new HashSet<>();
    List<Location> originalLocationStubs = getLocationStubs();
    val locationStubs = originalLocationStubs.stream().filter(l -> RandomUtils.nextBoolean()).collect(toList());
    if (isEmpty(locationStubs)) {
        int randomLocationPosition = RandomUtils.nextInt(0, originalLocationStubs.size() - 1);
        Location randomLocationStub = originalLocationStubs.get(randomLocationPosition);
        locationStubs.add(randomLocationStub);
    }
    int locationsLimit = min(locationSpec.getMax(), locationStubs.size());
    int countLocations = RandomUtils.nextInt(1, locationsLimit);
    IntStream.range(0, countLocations).forEachOrdered(i -> {
        Location locationStub = locationStubs.get(i);
        updateLocationStubToLocation(locationSpec, locationStub);
        result.add(locationStub);
    });
    return result;
}
Also used : lombok.val(lombok.val) RandomUtils(org.apache.commons.lang3.RandomUtils) IntStream(java.util.stream.IntStream) LocationSpec(com.icthh.xm.ms.entity.domain.spec.LocationSpec) ERR_METHOD_NOT_SUPPORTED(com.icthh.xm.commons.exceptions.ErrorConstants.ERR_METHOD_NOT_SUPPORTED) SneakyThrows(lombok.SneakyThrows) Constants(com.icthh.xm.ms.entity.config.Constants) ClassPathResource(org.springframework.core.io.ClassPathResource) HashMap(java.util.HashMap) Random(java.util.Random) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ImmutableList(com.google.common.collect.ImmutableList) Service(org.springframework.stereotype.Service) Map(java.util.Map) Integer.min(java.lang.Integer.min) TagSpec(com.icthh.xm.ms.entity.domain.spec.TagSpec) Collections.emptyMap(java.util.Collections.emptyMap) Collections.emptySet(java.util.Collections.emptySet) StringUtils.isEmpty(org.apache.commons.lang.StringUtils.isEmpty) BusinessException(com.icthh.xm.commons.exceptions.BusinessException) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Collection(java.util.Collection) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) lombok.val(lombok.val) Set(java.util.Set) CollectionType(com.fasterxml.jackson.databind.type.CollectionType) TypeSpec(com.icthh.xm.ms.entity.domain.spec.TypeSpec) UUID(java.util.UUID) Tag(com.icthh.xm.ms.entity.domain.Tag) Instant(java.time.Instant) Location(com.icthh.xm.ms.entity.domain.Location) IOUtils(org.apache.commons.io.IOUtils) Collectors.toList(java.util.stream.Collectors.toList) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) XmEntity(com.icthh.xm.ms.entity.domain.XmEntity) CollectionUtils.isEmpty(org.apache.commons.collections.CollectionUtils.isEmpty) XmAuthenticationContextHolder(com.icthh.xm.commons.security.XmAuthenticationContextHolder) NextSpec(com.icthh.xm.ms.entity.domain.spec.NextSpec) InputStream(java.io.InputStream) Location(com.icthh.xm.ms.entity.domain.Location) HashSet(java.util.HashSet)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Location (com.icthh.xm.ms.entity.domain.Location)2 XmEntity (com.icthh.xm.ms.entity.domain.XmEntity)2 LocationSpec (com.icthh.xm.ms.entity.domain.spec.LocationSpec)2 TypeSpec (com.icthh.xm.ms.entity.domain.spec.TypeSpec)2 SneakyThrows (lombok.SneakyThrows)2 lombok.val (lombok.val)2 CollectionType (com.fasterxml.jackson.databind.type.CollectionType)1 ImmutableList (com.google.common.collect.ImmutableList)1 BusinessException (com.icthh.xm.commons.exceptions.BusinessException)1 ERR_METHOD_NOT_SUPPORTED (com.icthh.xm.commons.exceptions.ErrorConstants.ERR_METHOD_NOT_SUPPORTED)1 XmAuthenticationContextHolder (com.icthh.xm.commons.security.XmAuthenticationContextHolder)1 Constants (com.icthh.xm.ms.entity.config.Constants)1 Tag (com.icthh.xm.ms.entity.domain.Tag)1 NextSpec (com.icthh.xm.ms.entity.domain.spec.NextSpec)1 TagSpec (com.icthh.xm.ms.entity.domain.spec.TagSpec)1 InputStream (java.io.InputStream)1 Integer.min (java.lang.Integer.min)1 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)1 Instant (java.time.Instant)1