Search in sources :

Example 6 with ConsumerProducerFunction

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

the class ViewValidator method validateFunctionProjectionTypes.

private boolean validateFunctionProjectionTypes(final ViewElementDefinition viewElDef, final SchemaElementDefinition schemaElDef, final ConsumerProducerFunctionContext<String, ? extends ConsumerFunction> consumerProducerContext) {
    final ConsumerProducerFunction function = consumerProducerContext.getFunction();
    final Class<?>[] outputTypes = function.getOutputClasses();
    if (null == outputTypes || 0 == outputTypes.length) {
        LOGGER.error("Function " + function.getClass().getName() + " is invalid. Output types have not been set.");
        return false;
    }
    if (outputTypes.length != consumerProducerContext.getProjection().size()) {
        LOGGER.error("Output types for function " + function.getClass().getName() + " are not equal to the projection property types.");
        return false;
    }
    int i = 0;
    for (final String key : consumerProducerContext.getProjection()) {
        final Class<?> clazz;
        final IdentifierType idType = IdentifierType.fromName(key);
        if (null != idType) {
            clazz = schemaElDef.getIdentifierClass(idType);
        } else {
            final Class<?> schemaClazz = schemaElDef.getPropertyClass(key);
            if (null != schemaClazz) {
                clazz = schemaClazz;
            } else {
                clazz = viewElDef.getTransientPropertyClass(key);
            }
        }
        if (null == clazz || !outputTypes[i].isAssignableFrom(clazz)) {
            LOGGER.error("Function " + function.getClass().getName() + " is not compatible with output types. Function output type " + outputTypes[i].getName() + " is not assignable from projection type " + (null != clazz ? clazz.getName() : "with a null class."));
            return false;
        }
        i++;
    }
    return true;
}
Also used : IdentifierType(uk.gov.gchq.gaffer.data.element.IdentifierType) ConsumerProducerFunction(uk.gov.gchq.gaffer.function.ConsumerProducerFunction)

Example 7 with ConsumerProducerFunction

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

the class SchemaElementDefinitionValidator method validateFunctionProjectionTypes.

private boolean validateFunctionProjectionTypes(final SchemaElementDefinition elementDef, final ConsumerProducerFunctionContext<String, ? extends ConsumerFunction> consumerProducerContext) {
    final ConsumerProducerFunction function = consumerProducerContext.getFunction();
    final Class<?>[] outputTypes = function.getOutputClasses();
    if (null == outputTypes || 0 == outputTypes.length) {
        LOGGER.error("Function " + function.getClass().getName() + " is invalid. Output types have not been set.");
        return false;
    }
    if (outputTypes.length != consumerProducerContext.getProjection().size()) {
        LOGGER.error("Output types for function " + function.getClass().getName() + " are not equal to the projection property types.");
        return false;
    }
    int i = 0;
    for (final String key : consumerProducerContext.getProjection()) {
        final Class<?> clazz = elementDef.getClass(key);
        if (null == clazz || !outputTypes[i].isAssignableFrom(clazz)) {
            LOGGER.error("Function " + function.getClass().getName() + " is not compatible with output types. Function output type " + outputTypes[i].getName() + " is not assignable from projection type " + (null != clazz ? clazz.getName() : "with a null class."));
            return false;
        }
        i++;
    }
    return true;
}
Also used : ConsumerProducerFunction(uk.gov.gchq.gaffer.function.ConsumerProducerFunction)

Example 8 with ConsumerProducerFunction

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

the class ConsumerProducerFunctionContextTest method shouldThrowExceptionWhenBuildContextWhenProjectCalledTwice.

@Test
public void shouldThrowExceptionWhenBuildContextWhenProjectCalledTwice() {
    // Given
    final String reference1 = "reference 1";
    final ConsumerProducerFunction func1 = mock(ConsumerProducerFunction.class);
    final String reference1Proj = "reference 2 proj";
    final String reference2Proj = "reference 2 proj";
    // When / Then
    try {
        new ConsumerProducerFunctionContext.Builder<String, ConsumerProducerFunction>().select(reference1).execute(func1).project(reference1Proj).project(reference2Proj).build();
        fail("Exception expected");
    } catch (final IllegalStateException e) {
        assertNotNull(e);
    }
}
Also used : ConsumerProducerFunction(uk.gov.gchq.gaffer.function.ConsumerProducerFunction) Test(org.junit.Test)

Example 9 with ConsumerProducerFunction

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

the class ConsumerProducerFunctionContextTest method shouldBuildContext.

@Test
public void shouldBuildContext() {
    // Given
    final String reference1 = "reference 1";
    final String reference2 = "reference 2";
    final ConsumerProducerFunction func1 = mock(ConsumerProducerFunction.class);
    final String reference1Proj = "reference 2 proj";
    final String reference2Proj = "reference 2 proj";
    // When
    final ConsumerProducerFunctionContext<String, ConsumerProducerFunction> context = new ConsumerProducerFunctionContext.Builder<String, ConsumerProducerFunction>().select(reference1, reference2).execute(func1).project(reference1Proj, reference2Proj).build();
    // Then
    assertEquals(2, context.getSelection().size());
    assertEquals(reference1, context.getSelection().get(0));
    assertEquals(reference2, context.getSelection().get(1));
    assertSame(func1, context.getFunction());
    assertEquals(2, context.getSelection().size());
    assertEquals(reference1Proj, context.getProjection().get(0));
    assertEquals(reference2Proj, context.getProjection().get(1));
}
Also used : ConsumerProducerFunction(uk.gov.gchq.gaffer.function.ConsumerProducerFunction) Test(org.junit.Test)

Aggregations

ConsumerProducerFunction (uk.gov.gchq.gaffer.function.ConsumerProducerFunction)9 Test (org.junit.Test)7 IdentifierType (uk.gov.gchq.gaffer.data.element.IdentifierType)1