Search in sources :

Example 21 with CallableUserFunction

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

the class UserFunctionTest method shouldNotLoadAnyFunctionIfConfigIsEmpty.

@Test
void shouldNotLoadAnyFunctionIfConfigIsEmpty() throws Throwable {
    // Given
    Log log = spy(Log.class);
    procedureCompiler = new ProcedureCompiler(new TypeCheckers(), components, new ComponentRegistry(), log, new ProcedureConfig(Config.defaults(GraphDatabaseSettings.procedure_allowlist, List.of(""))));
    List<CallableUserFunction> method = compile(SingleReadOnlyFunction.class);
    verify(log).warn("The function 'org.neo4j.procedure.impl.listCoolPeople' is not on the allowlist and won't be loaded.");
    assertThat(method.size()).isEqualTo(0);
}
Also used : CallableUserFunction(org.neo4j.kernel.api.procedure.CallableUserFunction) Log(org.neo4j.logging.Log) NullLog(org.neo4j.logging.NullLog) Test(org.junit.jupiter.api.Test)

Example 22 with CallableUserFunction

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

the class UserFunctionTest method shouldGiveHelpfulErrorOnNullMessageException.

@Test
void shouldGiveHelpfulErrorOnNullMessageException() throws Throwable {
    // Given
    CallableUserFunction proc = compile(FunctionThatThrowsNullMsgExceptionAtInvocation.class).get(0);
    // When
    ProcedureException exception = assertThrows(ProcedureException.class, () -> proc.apply(prepareContext(), new AnyValue[0]));
    assertThat(exception.getMessage()).isEqualTo("Failed to invoke function `org.neo4j.procedure.impl.throwsAtInvocation`: Caused by: java.lang.IndexOutOfBoundsException");
}
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 23 with CallableUserFunction

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

the class UserFunctionTest method shouldSupportFunctionDeprecation.

@Test
void shouldSupportFunctionDeprecation() throws Throwable {
    // Given
    Log log = mock(Log.class);
    ProcedureCompiler procedureCompiler = new ProcedureCompiler(new TypeCheckers(), components, new ComponentRegistry(), log, ProcedureConfig.DEFAULT);
    // When
    List<CallableUserFunction> funcs = procedureCompiler.compileFunction(FunctionWithDeprecation.class, false);
    // Then
    verify(log).warn("Use of @UserFunction(deprecatedBy) without @Deprecated in org.neo4j.procedure.impl.badFunc");
    verifyNoMoreInteractions(log);
    for (CallableUserFunction func : funcs) {
        String name = func.signature().name().name();
        func.apply(prepareContext(), new AnyValue[0]);
        switch(name) {
            case "newFunc":
                assertFalse(func.signature().deprecated().isPresent(), "Should not be deprecated");
                break;
            case "oldFunc":
            case "badFunc":
                assertTrue(func.signature().deprecated().isPresent(), "Should be deprecated");
                assertThat(func.signature().deprecated().get()).isEqualTo("newFunc");
                break;
            default:
                fail("Unexpected function: " + name);
        }
    }
}
Also used : CallableUserFunction(org.neo4j.kernel.api.procedure.CallableUserFunction) Log(org.neo4j.logging.Log) NullLog(org.neo4j.logging.NullLog) Test(org.junit.jupiter.api.Test)

Example 24 with CallableUserFunction

use of org.neo4j.kernel.api.procedure.CallableUserFunction 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 25 with CallableUserFunction

use of org.neo4j.kernel.api.procedure.CallableUserFunction 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)

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