Search in sources :

Example 26 with CallableUserFunction

use of org.neo4j.kernel.api.procedure.CallableUserFunction in project neo4j by neo4j.

the class ResourceInjectionTest method shouldCompileAndRunUserFunctions.

@Test
void shouldCompileAndRunUserFunctions() throws Throwable {
    // Given
    CallableUserFunction proc = compiler.compileFunction(FunctionWithInjectedAPI.class, false).get(0);
    // When
    AnyValue out = proc.apply(prepareContext(), new AnyValue[0]);
    // Then
    assertThat(out).isEqualTo(Values.of("[Bonnie, Clyde]"));
}
Also used : CallableUserFunction(org.neo4j.kernel.api.procedure.CallableUserFunction) AnyValue(org.neo4j.values.AnyValue) Test(org.junit.jupiter.api.Test)

Example 27 with CallableUserFunction

use of org.neo4j.kernel.api.procedure.CallableUserFunction in project neo4j by neo4j.

the class ResourceInjectionTest method shouldFailNicelyWhenUnsafeAPISafeModeFunction.

@Test
void shouldFailNicelyWhenUnsafeAPISafeModeFunction() throws Throwable {
    // When
    List<CallableUserFunction> procList = compiler.compileFunction(FunctionWithUnsafeAPI.class, false);
    assertThat(logProvider).forClass(getClass()).forLevel(WARN).containsMessages("org.neo4j.procedure.impl.listCoolPeople", "unavailable");
    assertThat(procList.size()).isEqualTo(1);
    ProcedureException exception = assertThrows(ProcedureException.class, () -> procList.get(0).apply(prepareContext(), new AnyValue[0]));
    assertThat(exception.getMessage()).contains("org.neo4j.procedure.impl.listCoolPeople", "unavailable");
}
Also used : CallableUserFunction(org.neo4j.kernel.api.procedure.CallableUserFunction) AnyValue(org.neo4j.values.AnyValue) ProcedureException(org.neo4j.internal.kernel.api.exceptions.ProcedureException) Test(org.junit.jupiter.api.Test)

Example 28 with CallableUserFunction

use of org.neo4j.kernel.api.procedure.CallableUserFunction in project neo4j by neo4j.

the class ProcedureRegistry method register.

/**
 * Register a new function.
 *
 * @param function the function.
 */
public void register(CallableUserFunction function, boolean overrideCurrentImplementation, boolean builtIn) throws ProcedureException {
    UserFunctionSignature signature = function.signature();
    QualifiedName name = signature.name();
    if (aggregationFunctions.get(name) != null) {
        throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, "Unable to register function, because the name `%s` is already in use as an aggregation function.", name);
    }
    CallableUserFunction oldImplementation = functions.get(name);
    if (oldImplementation == null) {
        functions.put(name, function, signature.caseInsensitive());
    } else {
        if (overrideCurrentImplementation) {
            functions.put(name, function, signature.caseInsensitive());
        } else {
            throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, "Unable to register function, because the name `%s` is already in use.", name);
        }
    }
    if (builtIn) {
        builtInFunctionIds.add(functions.idOf(name));
    }
}
Also used : CallableUserFunction(org.neo4j.kernel.api.procedure.CallableUserFunction) QualifiedName(org.neo4j.internal.kernel.api.procs.QualifiedName) ProcedureException(org.neo4j.internal.kernel.api.exceptions.ProcedureException) UserFunctionSignature(org.neo4j.internal.kernel.api.procs.UserFunctionSignature)

Aggregations

CallableUserFunction (org.neo4j.kernel.api.procedure.CallableUserFunction)28 Test (org.junit.jupiter.api.Test)23 UserFunctionSignature (org.neo4j.internal.kernel.api.procs.UserFunctionSignature)14 AnyValue (org.neo4j.values.AnyValue)10 ProcedureException (org.neo4j.internal.kernel.api.exceptions.ProcedureException)5 Log (org.neo4j.logging.Log)5 NullLog (org.neo4j.logging.NullLog)4 Collections.emptyList (java.util.Collections.emptyList)3 List (java.util.List)3 Method (java.lang.reflect.Method)2 Arrays.asList (java.util.Arrays.asList)2 Collections.singletonList (java.util.Collections.singletonList)2 FieldSignature (org.neo4j.internal.kernel.api.procs.FieldSignature)2 NTList (org.neo4j.internal.kernel.api.procs.Neo4jTypes.NTList)2 QualifiedName (org.neo4j.internal.kernel.api.procs.QualifiedName)2 ComponentInjectionException (org.neo4j.kernel.api.exceptions.ComponentInjectionException)2 CallableProcedure (org.neo4j.kernel.api.procedure.CallableProcedure)2 CallableUserAggregationFunction (org.neo4j.kernel.api.procedure.CallableUserAggregationFunction)2 FailedLoadFunction (org.neo4j.kernel.api.procedure.FailedLoadFunction)2 UserFunction (org.neo4j.procedure.UserFunction)2