use of uk.gov.gchq.gaffer.operation.impl.compare.Sort in project Gaffer by gchq.
the class SortHandlerTest method shouldSortLargeNumberOfElements.
@Test
public void shouldSortLargeNumberOfElements() throws OperationException {
// Given
final int streamSize = 10000;
final int resultLimit = 5000;
final Stream<Element> stream = // generate a few extra in case there
new Random().ints(streamSize * 2).distinct().limit(streamSize).mapToObj(i -> new Entity.Builder().group(TestGroups.ENTITY).property("property", i).build());
final Sort sort = new Sort.Builder().input(() -> stream.iterator()).comparators(new ElementPropertyComparator.Builder().groups(TestGroups.ENTITY).property("property").reverse(false).build()).resultLimit(resultLimit).deduplicate(true).build();
final SortHandler handler = new SortHandler();
// When
final Iterable<? extends Element> result = handler.doOperation(sort, null, null);
// Then
final ArrayList<? extends Element> elements = Lists.newArrayList(result);
final ArrayList<? extends Element> sortedElements = Lists.newArrayList(result);
sortedElements.sort(new ElementPropertyComparator.Builder().groups(TestGroups.ENTITY).property("property").reverse(false).build());
assertEquals(elements, sortedElements);
assertNotNull(result);
assertEquals(resultLimit, Iterables.size(result));
}
use of uk.gov.gchq.gaffer.operation.impl.compare.Sort in project Gaffer by gchq.
the class SortHandlerTest method shouldSortBasedOnPropertyIncludingNulls.
@Test
public void shouldSortBasedOnPropertyIncludingNulls() throws OperationException, JsonProcessingException {
// Given
final Entity entity1 = new Entity.Builder().group(TestGroups.ENTITY).property("property", 1).build();
final Entity entity2 = new Entity.Builder().group(TestGroups.ENTITY).property("property", 2).build();
final Entity entity3 = new Entity.Builder().group(TestGroups.ENTITY).property("property", 3).build();
final Entity entity4 = new Entity.Builder().group(TestGroups.ENTITY).build();
final Entity entity5 = new Entity.Builder().group(TestGroups.ENTITY).build();
final List<Entity> input = Lists.newArrayList(entity1, entity3, entity4, entity2, entity5);
final Sort sort = new Sort.Builder().input(input).comparators(new ElementPropertyComparator.Builder().property("property").groups(TestGroups.ENTITY).comparator(new PropertyComparatorImpl()).build()).deduplicate(true).build();
final SortHandler handler = new SortHandler();
// When
final Iterable<? extends Element> result = handler.doOperation(sort, null, null);
// Then
assertEquals(Arrays.asList(entity1, entity2, entity3, entity4), Lists.newArrayList(result));
}
use of uk.gov.gchq.gaffer.operation.impl.compare.Sort in project Gaffer by gchq.
the class SortHandlerTest method shouldReturnNullIfOperationInputIsNull.
@Test
public void shouldReturnNullIfOperationInputIsNull() throws OperationException {
// Given
final Sort sort = new Sort.Builder().build();
final SortHandler handler = new SortHandler();
// When
final Iterable<? extends Element> result = handler.doOperation(sort, null, null);
// Then
assertNull(result);
}
use of uk.gov.gchq.gaffer.operation.impl.compare.Sort in project Gaffer by gchq.
the class SortHandlerTest method shouldSortBasedOnProperty.
@Test
public void shouldSortBasedOnProperty() throws OperationException, JsonProcessingException {
// Given
final Entity entity1 = new Entity.Builder().group(TestGroups.ENTITY).property("property", 1).build();
final Entity entity2 = new Entity.Builder().group(TestGroups.ENTITY).property("property", 2).build();
final Entity entity3a = new Entity.Builder().group(TestGroups.ENTITY).property("property", 3).property("otherProp", "a").build();
final Entity entity3b = new Entity.Builder().group(TestGroups.ENTITY).property("property", 3).property("otherProp", "b").build();
final Entity entity4 = new Entity.Builder().group(TestGroups.ENTITY).property("property", 4).build();
final List<Entity> input = Lists.newArrayList(entity1, entity4, entity3a, entity3b, entity2);
final Sort sort = new Sort.Builder().input(input).comparators(new ElementPropertyComparator.Builder().groups(TestGroups.ENTITY).property("property").build()).build();
final SortHandler handler = new SortHandler();
// When
final Iterable<? extends Element> result = handler.doOperation(sort, null, null);
// Then
final List<? extends Element> resultList = Lists.newArrayList(result);
assertTrue(Arrays.asList(entity1, entity2, entity3a, entity3b, entity4).equals(resultList) || Arrays.asList(entity1, entity2, entity3b, entity3a, entity4).equals(resultList), "Expected: \n" + Arrays.asList(entity1, entity2, entity3a, entity3b, entity4) + "\n but got: \n" + resultList);
}
use of uk.gov.gchq.gaffer.operation.impl.compare.Sort in project Gaffer by gchq.
the class SortHandlerTest method shouldSortBasedOn2Properties.
@Test
public void shouldSortBasedOn2Properties() throws OperationException, JsonProcessingException {
// Given
final Entity entity1 = new Entity.Builder().group(TestGroups.ENTITY).property("property1", 1).property("property2", 1).build();
final Entity entity2 = new Entity.Builder().group(TestGroups.ENTITY).property("property1", 1).property("property2", 2).build();
final Entity entity3 = new Entity.Builder().group(TestGroups.ENTITY).property("property1", 2).property("property2", 2).build();
final Entity entity4 = new Entity.Builder().group(TestGroups.ENTITY).property("property1", 2).property("property2", 1).build();
final List<Entity> input = Lists.newArrayList(entity1, entity3, entity2, entity4);
final Sort sort = new Sort.Builder().input(input).comparators(new ElementPropertyComparator.Builder().groups(TestGroups.ENTITY).property("property1").build(), new ElementPropertyComparator.Builder().groups(TestGroups.ENTITY).property("property2").build()).build();
final SortHandler handler = new SortHandler();
// When
final Iterable<? extends Element> result = handler.doOperation(sort, null, null);
// Then
assertEquals(Arrays.asList(entity1, entity2, entity4, entity3), Lists.newArrayList(result));
}
Aggregations