use of uk.gov.gchq.koryphe.tuple.function.TupleAdaptedFunction in project Gaffer by gchq.
the class ElementTransformerTest method shouldBuildTransformer.
@Test
public void shouldBuildTransformer() {
// Given
final String property1 = "property 1";
final String property2a = "property 2a";
final String property2b = "property 2b";
final IdentifierType identifier3 = IdentifierType.SOURCE;
final String property1Proj = "property 1 proj";
final String property2aProj = "property 2a proj";
final String property2bProj = "property 2b proj";
final IdentifierType identifier3Proj = IdentifierType.DESTINATION;
final Function func1 = mock(Function.class);
final Function func2 = mock(Function.class);
final Function func3 = mock(Function.class);
// When - check you can build the selection/function/projections in any order,
// although normally it will be done - select, execute then project.
final ElementTransformer transformer = new ElementTransformer.Builder().select(property1).execute(func1).project(property1Proj).select(property2a, property2b).execute(func2).project(property2aProj, property2bProj).select(identifier3.name()).execute(func3).project(identifier3Proj.name()).build();
// Then
int i = 0;
TupleAdaptedFunction<String, ?, ?> context = transformer.getComponents().get(i++);
assertThat(context.getSelection()).hasSize(1);
assertEquals(property1, context.getSelection()[0]);
assertSame(func1, context.getFunction());
assertThat(context.getProjection()).hasSize(1);
assertEquals(property1Proj, context.getProjection()[0]);
context = transformer.getComponents().get(i++);
assertThat(context.getSelection()).hasSize(2);
assertEquals(property2a, context.getSelection()[0]);
assertEquals(property2b, context.getSelection()[1]);
assertSame(func2, context.getFunction());
assertThat(context.getProjection()).hasSize(2);
assertEquals(property2aProj, context.getProjection()[0]);
assertEquals(property2bProj, context.getProjection()[1]);
context = transformer.getComponents().get(i++);
assertSame(func3, context.getFunction());
assertThat(context.getSelection()).hasSize(1);
assertEquals(identifier3.name(), context.getSelection()[0]);
assertThat(context.getProjection()).hasSize(1);
assertEquals(identifier3Proj.name(), context.getProjection()[0]);
assertEquals(i, transformer.getComponents().size());
}
use of uk.gov.gchq.koryphe.tuple.function.TupleAdaptedFunction in project Gaffer by gchq.
the class FunctionAuthoriserTest method shouldNotAllowGetElementsOperationWithUnauthorisedFunctionsInTheView.
@Test
public void shouldNotAllowGetElementsOperationWithUnauthorisedFunctionsInTheView() {
final OperationChain<CloseableIterable<? extends Element>> viewOperation = new OperationChain.Builder().first(new GetElements.Builder().view(new View.Builder().globalElements(new GlobalViewElementDefinition.Builder().transformFunctions(Lists.newArrayList(new TupleAdaptedFunction(new String[] { "input" }, new DivideBy(6), new String[] { "output" }))).build()).build()).build()).build();
FunctionAuthoriser functionAuthoriser = new FunctionAuthoriser();
// When
functionAuthoriser.setUnauthorisedFunctions(Lists.newArrayList(DivideBy.class));
// Then
assertThatExceptionOfType(UnauthorisedException.class).isThrownBy(() -> functionAuthoriser.preExecute(viewOperation, new Context())).withMessage("Operation chain contained an unauthorised function: uk.gov.gchq.koryphe.impl.function.DivideBy");
}
use of uk.gov.gchq.koryphe.tuple.function.TupleAdaptedFunction in project Gaffer by gchq.
the class ViewElementDefinitionTest method shouldJsonSerialiseAndDeserialise.
@Test
public void shouldJsonSerialiseAndDeserialise() throws SerialisationException {
// Given
final ViewElementDefinition elementDef = new ViewElementDefinition.Builder().transientProperty(TestPropertyNames.PROP_1, String.class).transientProperty(TestPropertyNames.PROP_2, String.class).properties(TestPropertyNames.COUNT, TestPropertyNames.DATE).preAggregationFilter(new ElementFilter.Builder().select(TestPropertyNames.COUNT).execute(new IsMoreThan(5)).build()).aggregator(new ElementAggregator.Builder().select(TestPropertyNames.COUNT).execute(new Max()).build()).postAggregationFilter(new ElementFilter.Builder().select(TestPropertyNames.COUNT).execute(new IsLessThan(10)).build()).transformer(new ElementTransformer.Builder().select(TestPropertyNames.COUNT).execute(new TestTransform()).project(TestPropertyNames.PROP_1).build()).postTransformFilter(new ElementFilter.Builder().select(TestPropertyNames.PROP_1).execute(new IsEqual("9")).build()).build();
// When
final byte[] json = JSONSerialiser.serialise(elementDef, true);
final ViewElementDefinition deserialisedElementDef = JSONSerialiser.deserialise(json, ViewElementDefinition.class);
assertEquals(Sets.newHashSet(TestPropertyNames.COUNT, TestPropertyNames.DATE), deserialisedElementDef.getProperties());
assertNull(deserialisedElementDef.getExcludeProperties());
final List<TupleAdaptedPredicate<String, ?>> preFilterComponents = deserialisedElementDef.getPreAggregationFilter().getComponents();
assertThat(preFilterComponents).hasSize(1);
assertArrayEquals(new String[] { TestPropertyNames.COUNT }, preFilterComponents.get(0).getSelection());
assertEquals(new IsMoreThan(5), preFilterComponents.get(0).getPredicate());
final List<TupleAdaptedBinaryOperator<String, ?>> aggComponents = deserialisedElementDef.getAggregator().getComponents();
assertThat(aggComponents).hasSize(1);
assertArrayEquals(new String[] { TestPropertyNames.COUNT }, aggComponents.get(0).getSelection());
assertEquals(new Max(), aggComponents.get(0).getBinaryOperator());
final List<TupleAdaptedPredicate<String, ?>> postFilterComponents = deserialisedElementDef.getPostAggregationFilter().getComponents();
assertThat(postFilterComponents).hasSize(1);
assertArrayEquals(new String[] { TestPropertyNames.COUNT }, postFilterComponents.get(0).getSelection());
assertEquals(new IsLessThan(10), postFilterComponents.get(0).getPredicate());
final List<TupleAdaptedFunction<String, ?, ?>> transformComponents = deserialisedElementDef.getTransformer().getComponents();
assertThat(transformComponents).hasSize(1);
assertArrayEquals(new String[] { TestPropertyNames.COUNT }, transformComponents.get(0).getSelection());
assertEquals(new TestTransform(), transformComponents.get(0).getFunction());
assertArrayEquals(new String[] { TestPropertyNames.PROP_1 }, transformComponents.get(0).getProjection());
final List<TupleAdaptedPredicate<String, ?>> postTransformFilterComponents = deserialisedElementDef.getPostTransformFilter().getComponents();
assertThat(postTransformFilterComponents).hasSize(1);
assertArrayEquals(new String[] { TestPropertyNames.PROP_1 }, postTransformFilterComponents.get(0).getSelection());
assertEquals(new IsEqual("9"), postTransformFilterComponents.get(0).getPredicate());
}
use of uk.gov.gchq.koryphe.tuple.function.TupleAdaptedFunction in project Gaffer by gchq.
the class TransformValidator method validateTransformPropertyClasses.
/**
* Validates that the functions to be executed are assignable to the corresponding properties
*
* @param elementDef The SchemaElementDefinition to validate against.
* @param transformer The ElementFilter to be validated against
* @return ValidationResult of the validation
*/
private ValidationResult validateTransformPropertyClasses(final SchemaElementDefinition elementDef, final ElementTransformer transformer) {
final ValidationResult result = new ValidationResult();
if (null != elementDef) {
final List<TupleAdaptedFunction<String, ?, ?>> components = transformer.getComponents();
for (final TupleAdaptedFunction<String, ?, ?> component : components) {
final Map<String, String> properties = elementDef.getPropertyMap();
if (!properties.isEmpty()) {
if (null == component.getFunction()) {
result.addError(transformer.getClass().getSimpleName());
} else {
final Class[] selectionClasses = getTypeClasses(component.getSelection(), elementDef);
if (!ArrayUtils.contains(selectionClasses, null)) {
final Signature inputSig = Signature.getInputSignature(component.getFunction());
result.add(inputSig.assignable(selectionClasses));
}
final Class[] projectionClasses = getTypeClasses(component.getProjection(), elementDef);
if (!ArrayUtils.contains(projectionClasses, null)) {
final Signature outputSig = Signature.getOutputSignature(component.getFunction());
result.add(outputSig.assignable(projectionClasses));
}
}
}
}
}
return result;
}
use of uk.gov.gchq.koryphe.tuple.function.TupleAdaptedFunction in project Gaffer by gchq.
the class GetAllElementsIT method shouldAllowBiFunctionInView.
@Test
@TraitRequirement({ StoreTrait.TRANSFORMATION })
public void shouldAllowBiFunctionInView() throws OperationException {
final Map<String, Class<?>> transientProperties = new HashMap<>();
transientProperties.put("propLong", Long.class);
transientProperties.put("combined", Long.class);
final List<TupleAdaptedFunction<String, ?, ?>> transformFunctions = new ArrayList<>();
final TupleAdaptedFunction<String, Integer, Long> convertToLong = new TupleAdaptedFunction<>();
convertToLong.setSelection(new String[] { TestPropertyNames.INT });
convertToLong.setFunction((Function) new ToLong());
convertToLong.setProjection(new String[] { "propLong" });
final TupleAdaptedFunction<String, Integer, Long> sum = new TupleAdaptedFunction<>();
sum.setSelection(new String[] { "propLong", TestPropertyNames.COUNT });
sum.setFunction(new ApplyBiFunction(new Sum()));
sum.setProjection(new String[] { "combined" });
transformFunctions.add(convertToLong);
transformFunctions.add(sum);
final GetAllElements get = new GetAllElements.Builder().view(new View.Builder().edge(TestGroups.EDGE, new ViewElementDefinition.Builder().transientProperties(transientProperties).addTransformFunctions(transformFunctions).build()).build()).build();
final CloseableIterable<? extends Element> results = graph.execute(get, user);
for (final Element result : results) {
final Long expectedResult = (Long) result.getProperty("propLong") + (Long) result.getProperty(TestPropertyNames.COUNT);
final Long combined = (Long) result.getProperty("combined");
assertThat(combined).isEqualTo(expectedResult);
}
}
Aggregations