Search in sources :

Example 16 with UserFunctionSignature

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());
}
Also used : Context(org.neo4j.kernel.api.procedure.Context) UserAggregator(org.neo4j.internal.kernel.api.procs.UserAggregator) NTString(org.neo4j.internal.kernel.api.procs.Neo4jTypes.NTString) UserFunctionSignature(org.neo4j.internal.kernel.api.procs.UserFunctionSignature) Test(org.junit.jupiter.api.Test)

Example 17 with UserFunctionSignature

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));
}
Also used : Context(org.neo4j.kernel.api.procedure.Context) Method(java.lang.reflect.Method) NTString(org.neo4j.internal.kernel.api.procs.Neo4jTypes.NTString) UserFunctionSignature(org.neo4j.internal.kernel.api.procs.UserFunctionSignature) Test(org.junit.jupiter.api.Test)

Example 18 with UserFunctionSignature

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 }));
}
Also used : CallableUserFunction(org.neo4j.kernel.api.procedure.CallableUserFunction) AnyValue(org.neo4j.values.AnyValue) NTString(org.neo4j.internal.kernel.api.procs.Neo4jTypes.NTString) UserFunctionSignature(org.neo4j.internal.kernel.api.procs.UserFunctionSignature) Test(org.junit.jupiter.api.Test)

Example 19 with UserFunctionSignature

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 }));
        }
    }
}
Also used : CallableUserFunction(org.neo4j.kernel.api.procedure.CallableUserFunction) Type(java.lang.reflect.Type) AnyValue(org.neo4j.values.AnyValue) Method(java.lang.reflect.Method) UserFunctionSignature(org.neo4j.internal.kernel.api.procs.UserFunctionSignature) Test(org.junit.jupiter.api.Test)

Example 20 with UserFunctionSignature

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);
}
Also used : UserAggregator(org.neo4j.internal.kernel.api.procs.UserAggregator) UserFunctionSignature(org.neo4j.internal.kernel.api.procs.UserFunctionSignature) Test(org.junit.jupiter.api.Test)

Aggregations

UserFunctionSignature (org.neo4j.internal.kernel.api.procs.UserFunctionSignature)22 Test (org.junit.jupiter.api.Test)16 CallableUserFunction (org.neo4j.kernel.api.procedure.CallableUserFunction)13 AnyValue (org.neo4j.values.AnyValue)7 CallableUserAggregationFunction (org.neo4j.kernel.api.procedure.CallableUserAggregationFunction)6 ProcedureException (org.neo4j.internal.kernel.api.exceptions.ProcedureException)5 UserAggregator (org.neo4j.internal.kernel.api.procs.UserAggregator)4 Method (java.lang.reflect.Method)3 NTString (org.neo4j.internal.kernel.api.procs.Neo4jTypes.NTString)3 Arrays.asList (java.util.Arrays.asList)2 Collections.emptyList (java.util.Collections.emptyList)2 Collections.singletonList (java.util.Collections.singletonList)2 List (java.util.List)2 ClassGenerator (org.neo4j.codegen.ClassGenerator)2 ClassHandle (org.neo4j.codegen.ClassHandle)2 CodeBlock (org.neo4j.codegen.CodeBlock)2 CodeGenerator (org.neo4j.codegen.CodeGenerator)2 FieldReference (org.neo4j.codegen.FieldReference)2 FieldSignature (org.neo4j.internal.kernel.api.procs.FieldSignature)2 NTList (org.neo4j.internal.kernel.api.procs.Neo4jTypes.NTList)2