use of uk.gov.gchq.gaffer.operation.impl.function.Transform in project Gaffer by gchq.
the class TransformHandlerTest method shouldFailValidationWhenFunctionSignatureIsInvalid.
@Test
public void shouldFailValidationWhenFunctionSignatureIsInvalid() {
// Given
final Schema schema = new Schema.Builder().entity(TestGroups.ENTITY, new SchemaEntityDefinition.Builder().property(TestPropertyNames.PROP_1, TestPropertyNames.STRING).build()).type(TestPropertyNames.STRING, new TypeDefinition(String.class)).build();
given(store.getSchema()).willReturn(schema);
final Entity entity = new Entity.Builder().group(TestGroups.ENTITY).property(TestPropertyNames.PROP_1, TestPropertyNames.INT).property(TestPropertyNames.PROP_2, TestPropertyNames.STRING).build();
final Entity entity1 = new Entity.Builder().group(TestGroups.ENTITY).property(TestPropertyNames.PROP_1, TestPropertyNames.INT).build();
final ElementTransformer transformer = new ElementTransformer.Builder().select(TestPropertyNames.PROP_1).execute(new Divide()).project(TestPropertyNames.PROP_3).build();
input.add(entity);
input.add(entity1);
final Transform transform = new Transform.Builder().input(input).entity(TestGroups.ENTITY, transformer).build();
// When / Then
assertThatExceptionOfType(OperationException.class).isThrownBy(() -> handler.doOperation(transform, context, store)).withMessageContaining("Incompatible number of types");
}
use of uk.gov.gchq.gaffer.operation.impl.function.Transform in project Gaffer by gchq.
the class TransformHandlerTest method shouldFailValidationWhenSchemaElementDefinitionsAreNull.
@Test
public void shouldFailValidationWhenSchemaElementDefinitionsAreNull() {
// Given
given(store.getSchema()).willReturn(new Schema());
final Entity entity = new Entity.Builder().group(TestGroups.ENTITY).property(TestPropertyNames.PROP_1, TestPropertyNames.INT).property(TestPropertyNames.PROP_2, TestPropertyNames.STRING).build();
final Entity entity1 = new Entity.Builder().group(TestGroups.ENTITY).property(TestPropertyNames.PROP_1, TestPropertyNames.INT).build();
final ElementTransformer transformer = new ElementTransformer.Builder().select(TestPropertyNames.PROP_1).execute(new Identity()).project(TestPropertyNames.PROP_3).build();
input.add(entity);
input.add(entity1);
final Transform transform = new Transform.Builder().input(input).entity(TestGroups.ENTITY, transformer).build();
// When / Then
assertThatExceptionOfType(OperationException.class).isThrownBy(() -> handler.doOperation(transform, context, store)).withMessageContaining("Entity group: " + TestGroups.ENTITY + " does not exist in the schema.");
}
use of uk.gov.gchq.gaffer.operation.impl.function.Transform in project Gaffer by gchq.
the class TransformHandlerTest method shouldSelectAdjacentMatchedVertexForTransform.
@Test
public void shouldSelectAdjacentMatchedVertexForTransform() throws OperationException {
// Given
given(store.getSchema()).willReturn(schema);
final Edge edge = new Edge.Builder().group(TestGroups.EDGE).source("srcVal").dest("destVal").matchedVertex(EdgeId.MatchedVertex.SOURCE).build();
final Transform transform = new Transform.Builder().input(edge).edge(TestGroups.EDGE, new ElementTransformer.Builder().select(IdentifierType.ADJACENT_MATCHED_VERTEX.name()).execute(new Identity()).project(TestPropertyNames.PROP_3).build()).build();
// When
final Iterable<? extends Element> results = handler.doOperation(transform, context, store);
// Then
final Edge expectedEdge = new Edge.Builder().group(TestGroups.EDGE).source("srcVal").dest("destVal").matchedVertex(EdgeId.MatchedVertex.SOURCE).property(TestPropertyNames.PROP_3, "destVal").build();
ElementUtil.assertElementEquals(Collections.singletonList(expectedEdge), results);
}
use of uk.gov.gchq.gaffer.operation.impl.function.Transform in project Gaffer by gchq.
the class TransformHandlerTest method shouldTransformElementsUsingMockFunction.
@Test
public void shouldTransformElementsUsingMockFunction() throws OperationException {
// Given
final Function<String, Integer> function = mock(Function.class);
given(function.apply(TestPropertyNames.STRING)).willReturn(6);
given(store.getSchema()).willReturn(schema);
final Edge edge = new Edge.Builder().group(TestGroups.EDGE).property(TestPropertyNames.PROP_1, TestPropertyNames.STRING).build();
final Entity entity = new Entity.Builder().group(TestGroups.ENTITY).property(TestPropertyNames.PROP_1, TestPropertyNames.STRING).property(TestPropertyNames.PROP_2, 3).build();
final ElementTransformer transformer = new ElementTransformer.Builder().select(TestPropertyNames.PROP_1).execute(function).project(TestPropertyNames.PROP_3).build();
final Edge expectedEdge = new Edge.Builder().group(TestGroups.EDGE).property(TestPropertyNames.PROP_3, 6).build();
final Entity expectedEntity = new Entity.Builder().group(TestGroups.ENTITY).property(TestPropertyNames.PROP_3, 6).property(TestPropertyNames.PROP_2, 3).build();
final Transform transform = new Transform.Builder().input(input).edge(TestGroups.EDGE, transformer).entity(TestGroups.ENTITY, transformer).build();
input.add(edge);
input.add(entity);
expected.add(expectedEdge);
expected.add(expectedEntity);
// When
final Iterable<? extends Element> results = handler.doOperation(transform, context, store);
final List<Element> resultsList = Lists.newArrayList(results);
// Then
boolean isSame = false;
for (int i = 0; i < resultsList.size(); i++) {
isSame = expected.get(i).getProperty(TestPropertyNames.PROP_3).equals(resultsList.get(i).getProperty(TestPropertyNames.PROP_3));
}
assertTrue(isSame);
}
use of uk.gov.gchq.gaffer.operation.impl.function.Transform in project Gaffer by gchq.
the class TransformHandlerTest method shouldApplyDifferentTransformersToDifferentGroups.
@Test
public void shouldApplyDifferentTransformersToDifferentGroups() throws OperationException {
// Given
final Function<String, Integer> function = String::length;
final Function<String, String> function1 = String::toUpperCase;
final Map<String, ElementTransformer> edges = new HashMap<>();
given(store.getSchema()).willReturn(schema);
final Edge edge = new Edge.Builder().group(TestGroups.EDGE).property(TestPropertyNames.PROP_1, "testValue").property(TestPropertyNames.PROP_2, 5).build();
final Edge edge1 = new Edge.Builder().group(TestGroups.EDGE_2).property(TestPropertyNames.PROP_2, "otherValue").property(TestPropertyNames.PROP_3, 2L).build();
final ElementTransformer elementTransformer = new ElementTransformer.Builder().select(TestPropertyNames.PROP_1).execute(function).project(TestPropertyNames.PROP_4).build();
final ElementTransformer elementTransformer1 = new ElementTransformer.Builder().select(TestPropertyNames.PROP_2).execute(function1).project(TestPropertyNames.PROP_4).build();
final Edge expectedEdge = new Edge.Builder().group(TestGroups.EDGE).property(TestPropertyNames.PROP_4, 9).property(TestPropertyNames.PROP_2, 5).build();
final Edge expectedEdge1 = new Edge.Builder().group(TestGroups.EDGE_2).property(TestPropertyNames.PROP_4, "OTHERVALUE").property(TestPropertyNames.PROP_3, 2L).build();
edges.put(TestGroups.EDGE, elementTransformer);
edges.put(TestGroups.EDGE_2, elementTransformer1);
input.add(edge);
input.add(edge1);
expected.add(expectedEdge);
expected.add(expectedEdge1);
final Transform transform = new Transform.Builder().input(input).edges(edges).build();
// When
final Iterable<? extends Element> results = handler.doOperation(transform, context, store);
final List<Element> resultsList = Lists.newArrayList(results);
// Then
boolean isSame = false;
for (int i = 0; i < resultsList.size(); i++) {
isSame = expected.get(i).getProperty(TestPropertyNames.PROP_4).equals(resultsList.get(i).getProperty(TestPropertyNames.PROP_4));
}
assertTrue(isSame);
}
Aggregations