use of org.neo4j.internal.kernel.api.procs.UserFunctionSignature in project neo4j by neo4j.
the class ProcedureCompilationTest method aggregationShouldAccessContext.
@Test
void aggregationShouldAccessContext() throws ProcedureException, NoSuchFieldException {
// Given
UserFunctionSignature signature = functionSignature("test", "foo").in("in", NTString).out(NTString).build();
FieldSetter setter = createSetter(InnerClass.class, "thread", Context::thread);
String threadName = Thread.currentThread().getName();
UserAggregator aggregator = compileAggregation(signature, singletonList(setter), method(InnerClass.class, "create"), method(InnerClass.Aggregator.class, "update", String.class), method(InnerClass.Aggregator.class, "result")).create(ctx);
// When
aggregator.update(new AnyValue[] { stringValue("1:") });
aggregator.update(new AnyValue[] { stringValue("2:") });
aggregator.update(new AnyValue[] { stringValue("3:") });
// Then
assertEquals(stringValue(format("1: %s, 2: %s, 3: %s", threadName, threadName, threadName)), aggregator.result());
}
use of org.neo4j.internal.kernel.api.procs.UserFunctionSignature in project neo4j by neo4j.
the class ProcedureCompilationTest method functionShouldAccessContext.
@Test
void functionShouldAccessContext() throws ProcedureException, NoSuchFieldException {
// Given
UserFunctionSignature signature = functionSignature("test", "foo").out(NTInteger).build();
FieldSetter setter1 = createSetter(InnerClass.class, "transaction", Context::internalTransaction);
FieldSetter setter2 = createSetter(InnerClass.class, "thread", Context::thread);
Method longMethod = method(InnerClass.class, "stringMethod");
// Then
String threadName = Thread.currentThread().getName();
assertEquals(stringValue("NULL AND NULL"), compileFunction(signature, emptyList(), longMethod).apply(ctx, EMPTY));
assertEquals(stringValue("I'm transaction AND NULL"), compileFunction(signature, singletonList(setter1), longMethod).apply(ctx, EMPTY));
assertEquals(stringValue("NULL AND " + threadName), compileFunction(signature, singletonList(setter2), longMethod).apply(ctx, EMPTY));
assertEquals(stringValue("I'm transaction AND " + threadName), compileFunction(signature, asList(setter1, setter2), longMethod).apply(ctx, EMPTY));
}
use of org.neo4j.internal.kernel.api.procs.UserFunctionSignature in project neo4j by neo4j.
the class ProcedureCompilationTest method shouldHandleStrings.
@Test
void shouldHandleStrings() throws ProcedureException {
// Given
UserFunctionSignature signature = functionSignature("test", "foo").in("string", NTString).out(NTString).build();
// When
CallableUserFunction stringMethod = compileFunction(signature, emptyList(), method("testMethod", String.class));
// Then
assertEquals(stringValue("good"), stringMethod.apply(ctx, new AnyValue[] { stringValue("good") }));
assertEquals(NO_VALUE, stringMethod.apply(ctx, new AnyValue[] { NO_VALUE }));
}
use of org.neo4j.internal.kernel.api.procs.UserFunctionSignature in project neo4j by neo4j.
the class ProcedureCompilationTest method shouldHandleAllTypes.
@Test
void shouldHandleAllTypes() throws ProcedureException {
Map<Type, Method> allTypes = typeMaps();
UserFunctionSignature signature = functionSignature("test", "foo").in("in", NTAny).out(NTAny).build();
for (Entry<Type, Method> entry : allTypes.entrySet()) {
CallableUserFunction function = compileFunction(signature, emptyList(), entry.getValue());
Type type = entry.getKey();
if (type.equals(long.class)) {
assertEquals(longValue(1337L), function.apply(ctx, new AnyValue[] { longValue(1337L) }));
} else if (type.equals(double.class)) {
assertEquals(PI, function.apply(ctx, new AnyValue[] { PI }));
} else if (type.equals(boolean.class)) {
assertEquals(TRUE, function.apply(ctx, new AnyValue[] { TRUE }));
} else if (type instanceof Class<?> && AnyValue.class.isAssignableFrom((Class<?>) type)) {
assertEquals(NO_VALUE, function.apply(ctx, new AnyValue[] { null }));
} else {
assertEquals(NO_VALUE, function.apply(ctx, new AnyValue[] { NO_VALUE }));
}
}
}
use of org.neo4j.internal.kernel.api.procs.UserFunctionSignature in project neo4j by neo4j.
the class ProcedureCompilationTest method shouldHandleThrowingAggregations.
@Test
void shouldHandleThrowingAggregations() throws ProcedureException {
UserFunctionSignature signature = functionSignature("test", "foo").out(NTInteger).build();
UserAggregator aggregator = compileAggregation(signature, emptyList(), method("blackAdder"), method(BlackAdder.class, "update"), method(BlackAdder.class, "result")).create(ctx);
assertThrows(ProcedureException.class, () -> aggregator.update(EMPTY));
assertThrows(ProcedureException.class, aggregator::result);
}
Aggregations