Search in sources :

Example 11 with BasicContext

use of org.neo4j.kernel.api.proc.BasicContext in project neo4j by neo4j.

the class ReflectiveUserAggregationFunctionTest method shouldRunClassWithMultipleFunctionsDeclared.

@Test
public void shouldRunClassWithMultipleFunctionsDeclared() throws Throwable {
    // Given
    List<CallableUserAggregationFunction> compiled = compile(MultiFunction.class);
    CallableUserAggregationFunction f1 = compiled.get(0);
    CallableUserAggregationFunction f2 = compiled.get(1);
    // When
    CallableUserAggregationFunction.Aggregator f1Aggregator = f1.create(new BasicContext());
    f1Aggregator.update(new Object[] { "Bonnie" });
    f1Aggregator.update(new Object[] { "Clyde" });
    CallableUserAggregationFunction.Aggregator f2Aggregator = f2.create(new BasicContext());
    f2Aggregator.update(new Object[] { "Bonnie", 1337L });
    f2Aggregator.update(new Object[] { "Bonnie", 42L });
    // Then
    assertThat(f1Aggregator.result(), equalTo(Arrays.asList("Bonnie", "Clyde")));
    assertThat(((Map) f2Aggregator.result()).get("Bonnie"), equalTo(1337L));
}
Also used : CallableUserAggregationFunction(org.neo4j.kernel.api.proc.CallableUserAggregationFunction) BasicContext(org.neo4j.kernel.api.proc.BasicContext) Test(org.junit.Test)

Example 12 with BasicContext

use of org.neo4j.kernel.api.proc.BasicContext in project neo4j by neo4j.

the class ReflectiveUserAggregationFunctionTest method shouldSupportFunctionDeprecation.

@Test
public void shouldSupportFunctionDeprecation() throws Throwable {
    // Given
    Log log = mock(Log.class);
    ReflectiveProcedureCompiler procedureCompiler = new ReflectiveProcedureCompiler(new TypeMappers(), components, new ComponentRegistry(), log, ProcedureConfig.DEFAULT);
    // When
    List<CallableUserAggregationFunction> funcs = procedureCompiler.compileAggregationFunction(FunctionWithDeprecation.class);
    // Then
    verify(log).warn("Use of @UserAggregationFunction(deprecatedBy) without @Deprecated in org.neo4j.kernel.impl.proc.badFunc");
    verifyNoMoreInteractions(log);
    for (CallableUserAggregationFunction func : funcs) {
        String name = func.signature().name().name();
        func.create(new BasicContext());
        switch(name) {
            case "newFunc":
                assertFalse("Should not be deprecated", func.signature().deprecated().isPresent());
                break;
            case "oldFunc":
            case "badFunc":
                assertTrue("Should be deprecated", func.signature().deprecated().isPresent());
                assertThat(func.signature().deprecated().get(), equalTo("newFunc"));
                break;
            default:
                fail("Unexpected function: " + name);
        }
    }
}
Also used : CallableUserAggregationFunction(org.neo4j.kernel.api.proc.CallableUserAggregationFunction) Log(org.neo4j.logging.Log) NullLog(org.neo4j.logging.NullLog) BasicContext(org.neo4j.kernel.api.proc.BasicContext) Test(org.junit.Test)

Example 13 with BasicContext

use of org.neo4j.kernel.api.proc.BasicContext in project neo4j by neo4j.

the class ReflectiveUserAggregationFunctionTest method shouldRunAggregationFunction.

@Test
public void shouldRunAggregationFunction() throws Throwable {
    // Given
    CallableUserAggregationFunction func = compile(SingleAggregationFunction.class).get(0);
    // When
    CallableUserAggregationFunction.Aggregator aggregator = func.create(new BasicContext());
    aggregator.update(new Object[] { "Harry" });
    aggregator.update(new Object[] { "Bonnie" });
    aggregator.update(new Object[] { "Sally" });
    aggregator.update(new Object[] { "Clyde" });
    // Then
    assertThat(aggregator.result(), equalTo(Arrays.asList("Bonnie", "Clyde")));
}
Also used : CallableUserAggregationFunction(org.neo4j.kernel.api.proc.CallableUserAggregationFunction) BasicContext(org.neo4j.kernel.api.proc.BasicContext) Test(org.junit.Test)

Example 14 with BasicContext

use of org.neo4j.kernel.api.proc.BasicContext in project neo4j by neo4j.

the class ReflectiveUserAggregationFunctionTest method shouldGiveHelpfulErrorOnNullMessageException.

@Test
public void shouldGiveHelpfulErrorOnNullMessageException() throws Throwable {
    // Given
    CallableUserAggregationFunction method = compile(FunctionThatThrowsNullMsgExceptionAtInvocation.class).get(0);
    // Expect
    exception.expect(ProcedureException.class);
    exception.expectMessage("Failed to invoke function `org.neo4j.kernel.impl.proc.test`: " + "Caused by: java.lang.IndexOutOfBoundsException");
    // When
    method.create(new BasicContext()).update(new Object[] {});
}
Also used : CallableUserAggregationFunction(org.neo4j.kernel.api.proc.CallableUserAggregationFunction) BasicContext(org.neo4j.kernel.api.proc.BasicContext) Test(org.junit.Test)

Example 15 with BasicContext

use of org.neo4j.kernel.api.proc.BasicContext in project neo4j by neo4j.

the class ReflectiveUserFunctionTest method shouldLoadWhiteListedFunction.

@Test
public void shouldLoadWhiteListedFunction() throws Throwable {
    // Given
    procedureCompiler = new ReflectiveProcedureCompiler(new TypeMappers(), components, new ComponentRegistry(), NullLog.getInstance(), new ProcedureConfig(Config.defaults().with(MapUtil.stringMap(GraphDatabaseSettings.procedure_whitelist.name(), "org.neo4j.kernel.impl.proc.listCoolPeople"))));
    CallableUserFunction method = compile(SingleReadOnlyFunction.class).get(0);
    // Expect
    Object out = method.apply(new BasicContext(), new Object[0]);
    assertThat(out, equalTo(Arrays.asList("Bonnie", "Clyde")));
}
Also used : CallableUserFunction(org.neo4j.kernel.api.proc.CallableUserFunction) BasicContext(org.neo4j.kernel.api.proc.BasicContext) Test(org.junit.Test)

Aggregations

BasicContext (org.neo4j.kernel.api.proc.BasicContext)42 Test (org.junit.Test)36 CallableProcedure (org.neo4j.kernel.api.proc.CallableProcedure)16 ProcedureException (org.neo4j.kernel.api.exceptions.ProcedureException)12 CallableUserFunction (org.neo4j.kernel.api.proc.CallableUserFunction)10 Log (org.neo4j.logging.Log)8 NullLog (org.neo4j.logging.NullLog)8 CallableUserAggregationFunction (org.neo4j.kernel.api.proc.CallableUserAggregationFunction)7 URL (java.net.URL)3 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)3 ProcedureSignature (org.neo4j.kernel.api.proc.ProcedureSignature)3 RawIterator (org.neo4j.collection.RawIterator)2 Context (org.neo4j.kernel.api.proc.Context)2 NTString (org.neo4j.kernel.api.proc.Neo4jTypes.NTString)1 UserFunctionSignature (org.neo4j.kernel.api.proc.UserFunctionSignature)1 SecurityContext (org.neo4j.kernel.api.security.SecurityContext)1