Search in sources :

Example 6 with TransformFunction

use of uk.gov.gchq.gaffer.function.TransformFunction in project Gaffer by gchq.

the class TransformerTest method shouldCloneTransformer.

@Test
public void shouldCloneTransformer() {
    // Given
    final String reference1 = "reference1";
    final String reference2 = "reference2";
    final Transformer<String> transformer = new Transformer<>();
    final ConsumerProducerFunctionContext<String, TransformFunction> functionContext1 = mock(ConsumerProducerFunctionContext.class);
    final TransformFunction function = mock(TransformFunction.class);
    final TransformFunction clonedFunction = mock(TransformFunction.class);
    given(functionContext1.getFunction()).willReturn(function);
    given(functionContext1.getSelection()).willReturn(Collections.singletonList(reference1));
    given(functionContext1.getProjection()).willReturn(Collections.singletonList(reference2));
    given(function.statelessClone()).willReturn(clonedFunction);
    transformer.addFunction(functionContext1);
    // When
    final Transformer<String> clone = transformer.clone();
    // Then
    assertNotSame(transformer, clone);
    assertEquals(1, clone.getFunctions().size());
    final ConsumerProducerFunctionContext<String, TransformFunction> resultClonedFunction = clone.getFunctions().get(0);
    assertEquals(1, resultClonedFunction.getSelection().size());
    assertEquals(reference1, resultClonedFunction.getSelection().get(0));
    assertEquals(1, resultClonedFunction.getProjection().size());
    assertEquals(reference2, resultClonedFunction.getProjection().get(0));
    assertNotSame(functionContext1, resultClonedFunction);
    assertNotSame(function, resultClonedFunction.getFunction());
    assertSame(clonedFunction, resultClonedFunction.getFunction());
}
Also used : TransformFunction(uk.gov.gchq.gaffer.function.TransformFunction) Test(org.junit.Test)

Example 7 with TransformFunction

use of uk.gov.gchq.gaffer.function.TransformFunction in project Gaffer by gchq.

the class TransformerTest method shouldBuildTransformer.

@Test
public void shouldBuildTransformer() {
    // Given
    final String reference1 = "reference 1";
    final String reference2 = "reference 2";
    final String reference3a = "reference 3a";
    final String reference3b = "reference 3b";
    final String reference5 = "reference 5";
    final String reference1Proj = "reference 1 proj";
    final String reference2Proj = "reference 2 proj";
    final String reference3aProj = "reference 3a proj";
    final String reference3bProj = "reference 3b proj";
    final String reference5Proj = "reference 5 proj";
    final TransformFunction func1 = mock(TransformFunction.class);
    final TransformFunction func3 = mock(TransformFunction.class);
    final TransformFunction func4 = mock(TransformFunction.class);
    final TransformFunction func5 = mock(TransformFunction.class);
    // When - check you can build the selection/function/projections in any order,
    // although normally it will be done - select, execute then project.
    final Transformer<String> transformer = new Transformer.Builder<String>().select(reference1).execute(func1).project(reference1Proj).select(reference2).project(reference2Proj).project(new String[] { reference3aProj, reference3bProj }).select(new String[] { reference3a, reference3b }).execute(func3).execute(func4).execute(func5).project(reference5Proj).select(reference5).build();
    // Then
    int i = 0;
    ConsumerProducerFunctionContext<String, TransformFunction> context = transformer.getFunctions().get(i++);
    assertEquals(1, context.getSelection().size());
    assertEquals(reference1, context.getSelection().get(0));
    assertSame(func1, context.getFunction());
    assertEquals(1, context.getProjection().size());
    assertEquals(reference1Proj, context.getProjection().get(0));
    context = transformer.getFunctions().get(i++);
    assertEquals(1, context.getSelection().size());
    assertEquals(reference2, context.getSelection().get(0));
    assertEquals(1, context.getProjection().size());
    assertEquals(reference2Proj, context.getProjection().get(0));
    context = transformer.getFunctions().get(i++);
    assertEquals(2, context.getSelection().size());
    assertEquals(reference3a, context.getSelection().get(0));
    assertEquals(reference3b, context.getSelection().get(1));
    assertSame(func3, context.getFunction());
    assertEquals(2, context.getProjection().size());
    assertEquals(reference3aProj, context.getProjection().get(0));
    assertEquals(reference3bProj, context.getProjection().get(1));
    context = transformer.getFunctions().get(i++);
    assertSame(func4, context.getFunction());
    context = transformer.getFunctions().get(i++);
    assertSame(func5, context.getFunction());
    assertEquals(1, context.getSelection().size());
    assertEquals(reference5, context.getSelection().get(0));
    assertEquals(1, context.getProjection().size());
    assertEquals(reference5Proj, context.getProjection().get(0));
    assertEquals(i, transformer.getFunctions().size());
}
Also used : TransformFunction(uk.gov.gchq.gaffer.function.TransformFunction) Test(org.junit.Test)

Aggregations

TransformFunction (uk.gov.gchq.gaffer.function.TransformFunction)7 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)1 Element (uk.gov.gchq.gaffer.data.element.Element)1 ElementTuple (uk.gov.gchq.gaffer.data.element.ElementTuple)1 IdentifierType (uk.gov.gchq.gaffer.data.element.IdentifierType)1 ConsumerProducerFunctionContext (uk.gov.gchq.gaffer.function.context.ConsumerProducerFunctionContext)1