use of org.neo4j.kernel.api.procedure.Context 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.kernel.api.procedure.Context 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.kernel.api.procedure.Context in project neo4j by neo4j.
the class ProcedureCompilationTest method procedureShouldAccessContext.
@Test
void procedureShouldAccessContext() throws ProcedureException, NoSuchFieldException {
// Given
ProcedureSignature signature = ProcedureSignature.procedureSignature("test", "foo").in("in", NTString).out(singletonList(inputField("name", NTString))).build();
FieldSetter setter1 = createSetter(InnerClass.class, "transaction", Context::internalTransaction);
FieldSetter setter2 = createSetter(InnerClass.class, "thread", Context::thread);
Method stringStream = method(InnerClass.class, "stringStream");
// Then
String threadName = Thread.currentThread().getName();
assertEquals(stringValue("NULL AND NULL"), compileProcedure(signature, emptyList(), stringStream).apply(ctx, EMPTY, RESOURCE_TRACKER).next()[0]);
assertEquals(stringValue("I'm transaction AND NULL"), compileProcedure(signature, singletonList(setter1), stringStream).apply(ctx, EMPTY, RESOURCE_TRACKER).next()[0]);
assertEquals(stringValue("NULL AND " + threadName), compileProcedure(signature, singletonList(setter2), stringStream).apply(ctx, EMPTY, RESOURCE_TRACKER).next()[0]);
assertEquals(stringValue("I'm transaction AND " + threadName), compileProcedure(signature, asList(setter1, setter2), stringStream).apply(ctx, EMPTY, RESOURCE_TRACKER).next()[0]);
}
use of org.neo4j.kernel.api.procedure.Context in project neo4j by neo4j.
the class ProcedureCompilationTest method shouldCallVoidProcedure.
@Test
void shouldCallVoidProcedure() throws ProcedureException, NoSuchFieldException {
// Given
ProcedureSignature signature = ProcedureSignature.procedureSignature("test", "foo").build();
// When
FieldSetter setter = createSetter(InnerClass.class, "transaction", Context::internalTransaction);
CallableProcedure voidMethod = compileProcedure(signature, singletonList(setter), method(InnerClass.class, "voidMethod"));
// Then
RawIterator<AnyValue[], ProcedureException> iterator = voidMethod.apply(ctx, EMPTY, RESOURCE_TRACKER);
assertFalse(iterator.hasNext());
verify(TRANSACTION).traversalDescription();
}
Aggregations