Search in sources :

Example 1 with Sort

use of uk.gov.gchq.gaffer.operation.impl.compare.Sort in project Gaffer by gchq.

the class OperationAuthoriserTest method shouldAcceptOperationChainWhenUserHasAllOpAuths.

@Test
public void shouldAcceptOperationChainWhenUserHasAllOpAuths() {
    // Given
    final OperationAuthoriser hook = fromJson(OP_AUTHS_PATH);
    final OperationChain opChain = new OperationChain.Builder().first(new GetElements()).then(new GenerateObjects<>()).then(new DiscardOutput()).then(new TestOperationsImpl(Collections.singletonList(new Sort()))).build();
    final User user = new User.Builder().opAuths("SuperUser", "ReadUser", "User").build();
    // When
    hook.preExecute(opChain, new Context(user));
// Then - no exceptions
}
Also used : Context(uk.gov.gchq.gaffer.store.Context) GenerateObjects(uk.gov.gchq.gaffer.operation.impl.generate.GenerateObjects) User(uk.gov.gchq.gaffer.user.User) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) TestOperationsImpl(uk.gov.gchq.gaffer.operation.TestOperationsImpl) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) DiscardOutput(uk.gov.gchq.gaffer.operation.impl.DiscardOutput) Sort(uk.gov.gchq.gaffer.operation.impl.compare.Sort) Test(org.junit.jupiter.api.Test)

Example 2 with Sort

use of uk.gov.gchq.gaffer.operation.impl.compare.Sort in project Gaffer by gchq.

the class SortHandlerTest method shouldSortBasedOnProperty_reversed.

@Test
public void shouldSortBasedOnProperty_reversed() 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).property("property", 4).build();
    final List<Entity> input = Lists.newArrayList(entity1, entity2, entity3, entity4);
    final Sort sort = new Sort.Builder().input(input).comparators(new ElementPropertyComparator.Builder().groups(TestGroups.ENTITY).property("property").comparator(new PropertyComparatorImpl()).reverse(true).build()).build();
    final SortHandler handler = new SortHandler();
    // When
    final Iterable<? extends Element> result = handler.doOperation(sort, null, null);
    // Then
    assertEquals(Arrays.asList(entity4, entity3, entity2, entity1), Lists.newArrayList(result));
}
Also used : Entity(uk.gov.gchq.gaffer.data.element.Entity) Sort(uk.gov.gchq.gaffer.operation.impl.compare.Sort) ElementPropertyComparator(uk.gov.gchq.gaffer.data.element.comparison.ElementPropertyComparator) Test(org.junit.jupiter.api.Test)

Example 3 with Sort

use of uk.gov.gchq.gaffer.operation.impl.compare.Sort in project Gaffer by gchq.

the class SortHandlerTest method shouldSortBasedOnElement.

@Test
public void shouldSortBasedOnElement() throws OperationException {
    // 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", 2).property("property2", 2).build();
    final Entity entity3 = new Entity.Builder().group(TestGroups.ENTITY).property("property1", 3).property("property2", 3).build();
    final Entity entity4 = new Entity.Builder().group(TestGroups.ENTITY).property("property1", 4).property("property2", 4).build();
    final List<Entity> input = Lists.newArrayList(entity1, entity3, entity2, entity4);
    final Sort sort = new Sort.Builder().input(input).comparators(new ElementComparatorImpl()).build();
    final SortHandler handler = new SortHandler();
    // When
    final Iterable<? extends Element> result = handler.doOperation(sort, null, null);
    // Then
    int prev = Integer.MIN_VALUE;
    for (final Element element : result) {
        final int curr = (int) element.getProperty("property1");
        assertTrue(curr > prev);
        prev = curr;
    }
}
Also used : Entity(uk.gov.gchq.gaffer.data.element.Entity) Element(uk.gov.gchq.gaffer.data.element.Element) Sort(uk.gov.gchq.gaffer.operation.impl.compare.Sort) Test(org.junit.jupiter.api.Test)

Example 4 with Sort

use of uk.gov.gchq.gaffer.operation.impl.compare.Sort in project Gaffer by gchq.

the class SortHandlerTest method shouldReturnOriginalListIfBothComparatorsAreNull.

@Test
public void shouldReturnOriginalListIfBothComparatorsAreNull() throws OperationException {
    // Given
    final List<Entity> input = Lists.newArrayList();
    final Sort sort = new Sort.Builder().input(input).build();
    final SortHandler handler = new SortHandler();
    // When
    final Iterable<? extends Element> result = handler.doOperation(sort, null, null);
    // Then
    assertNull(result);
}
Also used : Entity(uk.gov.gchq.gaffer.data.element.Entity) Sort(uk.gov.gchq.gaffer.operation.impl.compare.Sort) Test(org.junit.jupiter.api.Test)

Example 5 with Sort

use of uk.gov.gchq.gaffer.operation.impl.compare.Sort in project Gaffer by gchq.

the class SortHandlerTest method shouldNotThrowExceptionIfIterableIsEmpty.

@Test
public void shouldNotThrowExceptionIfIterableIsEmpty() throws OperationException {
    // Given
    final List<Entity> input = Lists.newArrayList();
    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
    assertTrue(Streams.toStream(result).collect(Collectors.toList()).isEmpty());
}
Also used : Entity(uk.gov.gchq.gaffer.data.element.Entity) Sort(uk.gov.gchq.gaffer.operation.impl.compare.Sort) ElementPropertyComparator(uk.gov.gchq.gaffer.data.element.comparison.ElementPropertyComparator) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)12 Sort (uk.gov.gchq.gaffer.operation.impl.compare.Sort)12 Entity (uk.gov.gchq.gaffer.data.element.Entity)8 ElementPropertyComparator (uk.gov.gchq.gaffer.data.element.comparison.ElementPropertyComparator)7 Element (uk.gov.gchq.gaffer.data.element.Element)2 Random (java.util.Random)1 OperationChain (uk.gov.gchq.gaffer.operation.OperationChain)1 TestOperationsImpl (uk.gov.gchq.gaffer.operation.TestOperationsImpl)1 DiscardOutput (uk.gov.gchq.gaffer.operation.impl.DiscardOutput)1 GenerateObjects (uk.gov.gchq.gaffer.operation.impl.generate.GenerateObjects)1 GetElements (uk.gov.gchq.gaffer.operation.impl.get.GetElements)1 Context (uk.gov.gchq.gaffer.store.Context)1 User (uk.gov.gchq.gaffer.user.User)1