Search in sources :

Example 1 with ProcedureException

use of org.neo4j.internal.kernel.api.exceptions.ProcedureException in project neo4j by neo4j.

the class TypeCheckers method converterFor.

DefaultValueConverter converterFor(Type javaType) throws ProcedureException {
    if (isAnyValue(javaType)) {
        // For AnyValue we support subtyping
        javaType = findValidSuperClass(javaType);
    }
    DefaultValueConverter converter = javaToNeo.get(javaType);
    if (converter != null) {
        return converter;
    }
    if (javaType instanceof ParameterizedType) {
        ParameterizedType pt = (ParameterizedType) javaType;
        Type rawType = pt.getRawType();
        if (rawType == List.class) {
            Type type = pt.getActualTypeArguments()[0];
            return toList(converterFor(type), type);
        } else if (rawType == Map.class) {
            Type type = pt.getActualTypeArguments()[0];
            if (type != String.class) {
                throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, "Maps are required to have `String` keys - but this map has `%s` keys.", type.getTypeName());
            }
            return TO_MAP;
        }
    }
    throw javaToNeoMappingError(javaType);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) AnyType(org.neo4j.internal.kernel.api.procs.Neo4jTypes.AnyType) Type(java.lang.reflect.Type) ParameterizedType(java.lang.reflect.ParameterizedType) NTString(org.neo4j.internal.kernel.api.procs.Neo4jTypes.NTString) ProcedureException(org.neo4j.internal.kernel.api.exceptions.ProcedureException) Map(java.util.Map) HashMap(java.util.HashMap) NTMap(org.neo4j.internal.kernel.api.procs.Neo4jTypes.NTMap)

Example 2 with ProcedureException

use of org.neo4j.internal.kernel.api.exceptions.ProcedureException in project neo4j by neo4j.

the class ProcedureCompilationTest method shouldHandleNonStaticInnerClasses.

@Test
void shouldHandleNonStaticInnerClasses() throws ProcedureException {
    // Given
    ProcedureSignature signature = ProcedureSignature.procedureSignature("test", "foo").out(singletonList(inputField("name", NTString))).build();
    // When
    CallableProcedure stringStream = compileProcedure(signature, emptyList(), method(InnerClass.class, "innerStream"));
    // Then
    RawIterator<AnyValue[], ProcedureException> iterator = stringStream.apply(ctx, EMPTY, RESOURCE_TRACKER);
    assertArrayEquals(new AnyValue[] { stringValue("hello") }, iterator.next());
    assertFalse(iterator.hasNext());
}
Also used : ProcedureSignature(org.neo4j.internal.kernel.api.procs.ProcedureSignature) CallableProcedure(org.neo4j.kernel.api.procedure.CallableProcedure) ProcedureException(org.neo4j.internal.kernel.api.exceptions.ProcedureException) Test(org.junit.jupiter.api.Test)

Example 3 with ProcedureException

use of org.neo4j.internal.kernel.api.exceptions.ProcedureException in project neo4j by neo4j.

the class ProcedureCompilationTest method shouldCallSimpleProcedure.

@Test
void shouldCallSimpleProcedure() throws ProcedureException {
    // Given
    ProcedureSignature signature = ProcedureSignature.procedureSignature("test", "foo").in("in", NTInteger).out(singletonList(inputField("name", NTInteger))).build();
    // When
    CallableProcedure longStream = compileProcedure(signature, emptyList(), method("longStream", long.class));
    // Then
    RawIterator<AnyValue[], ProcedureException> iterator = longStream.apply(ctx, new AnyValue[] { longValue(1337L) }, RESOURCE_TRACKER);
    assertArrayEquals(new AnyValue[] { longValue(1337L) }, iterator.next());
    assertFalse(iterator.hasNext());
}
Also used : ProcedureSignature(org.neo4j.internal.kernel.api.procs.ProcedureSignature) CallableProcedure(org.neo4j.kernel.api.procedure.CallableProcedure) ProcedureException(org.neo4j.internal.kernel.api.exceptions.ProcedureException) Test(org.junit.jupiter.api.Test)

Example 4 with ProcedureException

use of org.neo4j.internal.kernel.api.exceptions.ProcedureException in project neo4j by neo4j.

the class ProcedureJarLoaderTest method shouldGiveHelpfulErrorOnInvalidProcedure.

@Test
void shouldGiveHelpfulErrorOnInvalidProcedure() throws Throwable {
    // Given
    URL jar = createJarFor(ClassWithOneProcedure.class, ClassWithInvalidProcedure.class);
    ProcedureException exception = assertThrows(ProcedureException.class, () -> jarloader.loadProceduresFromDir(parentDir(jar)));
    assertThat(exception.getMessage()).isEqualTo(format("Procedures must return a Stream of records, where a record is a concrete class%n" + "that you define, with public non-final fields defining the fields in the record.%n" + "If you''d like your procedure to return `boolean`, you could define a record class like:%n" + "public class Output '{'%n" + "    public boolean out;%n" + "'}'%n%n" + "And then define your procedure as returning `Stream<Output>`."));
}
Also used : ProcedureException(org.neo4j.internal.kernel.api.exceptions.ProcedureException) URL(java.net.URL) Test(org.junit.jupiter.api.Test)

Example 5 with ProcedureException

use of org.neo4j.internal.kernel.api.exceptions.ProcedureException in project neo4j by neo4j.

the class ProcedureWithArgumentsTest method shouldRunSimpleProcedure.

@Test
void shouldRunSimpleProcedure() throws Throwable {
    // Given
    CallableProcedure procedure = compile(ClassWithProcedureWithSimpleArgs.class).get(0);
    // When
    RawIterator<AnyValue[], ProcedureException> out = procedure.apply(prepareContext(), new AnyValue[] { stringValue("Pontus"), longValue(35L) }, EMPTY_RESOURCE_TRACKER);
    // Then
    List<AnyValue[]> collect = asList(out);
    assertThat(collect.get(0)[0]).isEqualTo(stringValue("Pontus is 35 years old."));
}
Also used : CallableProcedure(org.neo4j.kernel.api.procedure.CallableProcedure) ProcedureException(org.neo4j.internal.kernel.api.exceptions.ProcedureException) Test(org.junit.jupiter.api.Test)

Aggregations

ProcedureException (org.neo4j.internal.kernel.api.exceptions.ProcedureException)124 Test (org.junit.jupiter.api.Test)95 CallableProcedure (org.neo4j.kernel.api.procedure.CallableProcedure)21 AnyValue (org.neo4j.values.AnyValue)19 QualifiedName (org.neo4j.internal.kernel.api.procs.QualifiedName)14 KernelException (org.neo4j.exceptions.KernelException)10 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)10 KernelIntegrationTest (org.neo4j.kernel.impl.api.integrationtest.KernelIntegrationTest)9 Arrays (java.util.Arrays)8 List (java.util.List)8 RawIterator (org.neo4j.collection.RawIterator)8 UserFunctionSignature (org.neo4j.internal.kernel.api.procs.UserFunctionSignature)8 CallableUserFunction (org.neo4j.kernel.api.procedure.CallableUserFunction)8 Collectors (java.util.stream.Collectors)7 ProcedureSignature (org.neo4j.internal.kernel.api.procs.ProcedureSignature)7 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)7 Method (java.lang.reflect.Method)6 ArrayList (java.util.ArrayList)6 FieldSignature (org.neo4j.internal.kernel.api.procs.FieldSignature)6 CallableUserAggregationFunction (org.neo4j.kernel.api.procedure.CallableUserAggregationFunction)6