use of uk.gov.gchq.gaffer.function.ConsumerFunction in project Gaffer by gchq.
the class ConsumerFunctionContextTest method shouldBuildContext.
@Test
public void shouldBuildContext() {
// Given
final String reference1 = "reference 1";
final String reference2 = "reference 2";
final ConsumerFunction func1 = mock(ConsumerFunction.class);
// When
final ConsumerFunctionContext<String, ConsumerFunction> context = new ConsumerFunctionContext.Builder<String, ConsumerFunction>().select(reference1, reference2).execute(func1).build();
// Then
assertEquals(2, context.getSelection().size());
assertEquals(reference1, context.getSelection().get(0));
assertEquals(reference2, context.getSelection().get(1));
assertSame(func1, context.getFunction());
}
use of uk.gov.gchq.gaffer.function.ConsumerFunction in project Gaffer by gchq.
the class ViewValidator method validateFunctionSelectionTypes.
private boolean validateFunctionSelectionTypes(final ViewElementDefinition viewElDef, final SchemaElementDefinition schemaElDef, final ConsumerFunctionContext<String, ? extends ConsumerFunction> context) {
final ConsumerFunction function = context.getFunction();
final Class<?>[] inputTypes = function.getInputClasses();
if (null == inputTypes || 0 == inputTypes.length) {
LOGGER.error("Function " + function.getClass().getName() + " is invalid. Input types have not been set.");
return false;
}
if (inputTypes.length != context.getSelection().size()) {
LOGGER.error("Input types for function " + function.getClass().getName() + " are not equal to the selection property types.");
return false;
}
int i = 0;
for (final String key : context.getSelection()) {
final IdentifierType idType = IdentifierType.fromName(key);
final Class<?> clazz;
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) {
if (null != idType) {
final String typeName = schemaElDef.getIdentifierTypeName(idType);
if (null != typeName) {
LOGGER.error("No class type found for type definition " + typeName + " used by identifier " + idType + ". Please ensure it is defined in the schema.");
} else {
LOGGER.error("No type definition defined for identifier " + idType + ". Please ensure it is defined in the schema.");
}
} else {
final String typeName = schemaElDef.getPropertyTypeName(key);
if (null != typeName) {
LOGGER.error("No class type found for type definition " + typeName + " used by property " + key + ". Please ensure it is defined in the schema.");
} else {
LOGGER.error("No class type found for transient property " + key + ". Please ensure it is defined in the view.");
}
}
return false;
}
if (!inputTypes[i].isAssignableFrom(clazz)) {
LOGGER.error("Function " + function.getClass().getName() + " is not compatible with selection types. Function input type " + inputTypes[i].getName() + " is not assignable from selection type " + clazz.getName() + ".");
return false;
}
i++;
}
return true;
}
use of uk.gov.gchq.gaffer.function.ConsumerFunction in project Gaffer by gchq.
the class SchemaElementDefinitionValidatorTest method shouldValidateFunctionSelectionsAndReturnTrueWhenAllFunctionsAreValid.
@Test
public void shouldValidateFunctionSelectionsAndReturnTrueWhenAllFunctionsAreValid() {
// Given
final SchemaElementDefinition elementDef = mock(SchemaElementDefinition.class);
final Processor<String, ConsumerFunctionContext<String, ConsumerFunction>> processor = mock(Processor.class);
final SchemaElementDefinitionValidator validator = new SchemaElementDefinitionValidator();
final List<ConsumerFunctionContext<String, ConsumerFunction>> functions = new ArrayList<>();
for (int i = 0; i < 10; i++) {
final ConsumerFunctionContext<String, ConsumerFunction> context = mock(ConsumerFunctionContext.class);
final ConsumerFunction function = mock(ConsumerFunction.class);
given(function.getInputClasses()).willReturn(new Class<?>[] { CharSequence.class, CharSequence.class });
given(context.getFunction()).willReturn(function);
given(context.getSelection()).willReturn(Arrays.asList("key" + i + "a", "key" + i + "b"));
functions.add(context);
given(elementDef.getClass(context.getSelection().get(0))).willReturn((Class) CharSequence.class);
given(elementDef.getClass(context.getSelection().get(1))).willReturn((Class) String.class);
}
given(processor.getFunctions()).willReturn(functions);
// When
final boolean isValid = validator.validateFunctionArgumentTypes(processor, elementDef);
// Then
assertTrue(isValid);
}
use of uk.gov.gchq.gaffer.function.ConsumerFunction in project Gaffer by gchq.
the class SchemaElementDefinitionValidatorTest method shouldValidateFunctionSelectionsAndReturnFalseWhenFunctionTypeDoesNotEqualSelectionType.
@Test
public void shouldValidateFunctionSelectionsAndReturnFalseWhenFunctionTypeDoesNotEqualSelectionType() {
// Given
final SchemaElementDefinition elementDef = mock(SchemaElementDefinition.class);
final Processor<String, ConsumerFunctionContext<String, ConsumerFunction>> processor = mock(Processor.class);
final SchemaElementDefinitionValidator validator = new SchemaElementDefinitionValidator();
final List<ConsumerFunctionContext<String, ConsumerFunction>> functions = new ArrayList<>();
for (int i = 0; i < 10; i++) {
final ConsumerFunctionContext<String, ConsumerFunction> context = mock(ConsumerFunctionContext.class);
final ConsumerFunction function = mock(ConsumerFunction.class);
given(function.getInputClasses()).willReturn(new Class<?>[] { String.class });
given(context.getFunction()).willReturn(function);
given(context.getSelection()).willReturn(Collections.singletonList("key" + i + "a"));
functions.add(context);
given(elementDef.getClass(context.getSelection().get(0))).willReturn((Class) Integer.class);
}
given(processor.getFunctions()).willReturn(functions);
// When
final boolean isValid = validator.validateFunctionArgumentTypes(processor, elementDef);
// Then
assertFalse(isValid);
}
use of uk.gov.gchq.gaffer.function.ConsumerFunction in project Gaffer by gchq.
the class SchemaElementDefinitionValidator method validateFunctionSelectionTypes.
private boolean validateFunctionSelectionTypes(final SchemaElementDefinition elementDef, final ConsumerFunctionContext<String, ? extends ConsumerFunction> context) {
final ConsumerFunction function = context.getFunction();
final Class<?>[] inputTypes = function.getInputClasses();
if (null == inputTypes || 0 == inputTypes.length) {
LOGGER.error("Function " + function.getClass().getName() + " is invalid. Input types have not been set.");
return false;
}
if (inputTypes.length != context.getSelection().size()) {
LOGGER.error("Input types for function " + function.getClass().getName() + " are not equal to the selection property types.");
return false;
}
int i = 0;
for (final String key : context.getSelection()) {
final Class<?> clazz = elementDef.getClass(key);
if (null == clazz) {
final String typeName;
final IdentifierType idType = IdentifierType.fromName(key);
if (null == idType) {
typeName = elementDef.getPropertyTypeName(key);
} else {
typeName = elementDef.getIdentifierTypeName(idType);
}
if (null != typeName) {
LOGGER.error("No class type found for type definition " + typeName + " used by " + key + ". Please ensure it is defined in the schema.");
} else {
LOGGER.error("No type definition defined for " + key + ". Please ensure it is defined in the schema.");
}
return false;
}
if (!inputTypes[i].isAssignableFrom(clazz)) {
LOGGER.error("Function " + function.getClass().getName() + " is not compatible with selection types. Function input type " + inputTypes[i].getName() + " is not assignable from selection type " + clazz.getName());
return false;
}
i++;
}
return true;
}
Aggregations