private OutputMapper mapper( Class<?> clazz ) throws ProcedureException { return new OutputMappers( new TypeMappers() ).mapper( clazz ); }
public Procedures( EmbeddedProxySPI proxySPI, ThrowingConsumer<Procedures,ProcedureException> builtin, File pluginDir, Log log, ProcedureConfig config ) { this.builtin = builtin; this.pluginDir = pluginDir; this.log = log; this.typeMappers = new TypeMappers( proxySPI ); this.compiler = new ReflectiveProcedureCompiler( typeMappers, safeComponents, allComponents, log, config ); }
@Test public void shouldMapSimpleFunctionWithString() throws Throwable { // When Method echo = ClassWithProcedureWithSimpleArgs.class.getMethod( "echo", String.class ); List<Neo4jTypes.AnyType> signature = new MethodSignatureCompiler( new TypeMappers() ).inputTypesFor( echo ); // THen assertThat(signature, contains( Neo4jTypes.NTString)); }
@Test public void shouldMapSimpleRecordWithString() throws Throwable { // When Method echo = ClassWithProcedureWithSimpleArgs.class.getMethod( "echo", String.class ); List<FieldSignature> signature = new MethodSignatureCompiler( new TypeMappers() ).signatureFor( echo ); // THen assertThat(signature, contains( FieldSignature.inputField( "name", Neo4jTypes.NTString ) )); }
@Before public void setUp() { components = new ComponentRegistry(); procedureCompiler = new ReflectiveProcedureCompiler( new TypeMappers(), components, components, NullLog.getInstance(), ProcedureConfig.DEFAULT ); }
@Before public void setUp() { components = new ComponentRegistry(); procedureCompiler = new ReflectiveProcedureCompiler( new TypeMappers(), components, components, NullLog.getInstance(), ProcedureConfig.DEFAULT ); }
@Before public void setUp() { components = new ComponentRegistry(); procedureCompiler = new ReflectiveProcedureCompiler( new TypeMappers(), components, components, NullLog.getInstance(), ProcedureConfig.DEFAULT ); }
@Test public void shouldGiveHelpfulErrorOnMissingAnnotations() throws Throwable { // Given Method echo = ClassWithProcedureWithSimpleArgs.class.getMethod( "echoWithoutAnnotations", String.class, String.class); // Expect exception.expect( ProcedureException.class ); exception.expectMessage( String.format("Argument at position 1 in method `echoWithoutAnnotations` is missing an `@Name` " + "annotation.%n" + "Please add the annotation, recompile the class and try again." )); // When new MethodSignatureCompiler( new TypeMappers() ).signatureFor( echo ); } }
@Test public void shouldDetectCorrectType() throws Throwable { // When Neo4jTypes.AnyType type = new TypeMappers().toNeo4jType( javaClass ); // Then assertEquals( neoType, type ); }
@Test public void shouldNotLoadNoneWhiteListedFunction() throws Throwable { // Given Log log = spy(Log.class); procedureCompiler = new ReflectiveProcedureCompiler( new TypeMappers(), components, new ComponentRegistry(), log, new ProcedureConfig( Config.defaults( GraphDatabaseSettings.procedure_whitelist, "WrongName" ) ) ); List<CallableUserAggregationFunction> method = compile( SingleAggregationFunction.class ); verify( log ).warn( "The function 'org.neo4j.kernel.impl.proc.collectCool' is not on the whitelist and won't be loaded." ); assertThat( method.size(), equalTo( 0 ) ); }
@Test public void shouldNotLoadAnyFunctionIfConfigIsEmpty() throws Throwable { // Given Log log = spy(Log.class); procedureCompiler = new ReflectiveProcedureCompiler( new TypeMappers(), components, new ComponentRegistry(), log, new ProcedureConfig( Config.defaults( GraphDatabaseSettings.procedure_whitelist, "" ) ) ); List<CallableUserAggregationFunction> method = compile( SingleAggregationFunction.class ); verify( log ).warn( "The function 'org.neo4j.kernel.impl.proc.collectCool' is not on the whitelist and won't be loaded." ); assertThat( method.size(), equalTo( 0 ) ); }
@Test public void shouldNotLoadNoneWhiteListedFunction() throws Throwable { // Given Log log = spy(Log.class); procedureCompiler = new ReflectiveProcedureCompiler( new TypeMappers(), components, new ComponentRegistry(), log, new ProcedureConfig( Config.defaults( GraphDatabaseSettings.procedure_whitelist, "WrongName" ) ) ); List<CallableUserFunction> method = compile( SingleReadOnlyFunction.class ); verify( log ).warn( "The function 'org.neo4j.kernel.impl.proc.listCoolPeople' is not on the whitelist and won't be loaded." ); assertThat( method.size(), equalTo( 0 ) ); }
@Test public void shouldNotLoadAnyFunctionIfConfigIsEmpty() throws Throwable { // Given Log log = spy(Log.class); procedureCompiler = new ReflectiveProcedureCompiler( new TypeMappers(), components, new ComponentRegistry(), log, new ProcedureConfig( Config.defaults( GraphDatabaseSettings.procedure_whitelist, "" ) ) ); List<CallableUserFunction> method = compile( SingleReadOnlyFunction.class ); verify( log ).warn( "The function 'org.neo4j.kernel.impl.proc.listCoolPeople' is not on the whitelist and won't be loaded." ); assertThat( method.size(), equalTo( 0 ) ); }
private List<CallableProcedure> compile( Class<?> clazz ) throws KernelException { return new ReflectiveProcedureCompiler( new TypeMappers(), new ComponentRegistry(), new ComponentRegistry(), NullLog.getInstance(), ProcedureConfig.DEFAULT ).compileProcedure( clazz, null, true ); } }
@Test public void shouldIgnoreWhiteListingIfFullAccess() throws Throwable { // Given ProcedureConfig config = new ProcedureConfig( Config.defaults( procedure_whitelist, "empty" ) ); Log log = mock(Log.class); ReflectiveProcedureCompiler procedureCompiler = new ReflectiveProcedureCompiler( new TypeMappers(), components, components, log, config ); // When CallableProcedure proc = procedureCompiler.compileProcedure( SingleReadOnlyProcedure.class, null, true ).get( 0 ); // Then RawIterator<Object[],ProcedureException> result = proc.apply( new BasicContext(), new Object[0], resourceTracker ); assertEquals( result.next()[0], "Bonnie" ); }
@Test public void shouldReturnEmptySetOnNullArgument() throws Exception { // given ProcedureJarLoader jarloader = new ProcedureJarLoader( new ReflectiveProcedureCompiler( new TypeMappers(), new ComponentRegistry(), registryWithUnsafeAPI(), log, procedureConfig() ), NullLog.getInstance() ); // when ProcedureJarLoader.Callables callables = jarloader.loadProceduresFromDir( null ); // then assertEquals( 0, callables.procedures().size() + callables.functions().size() ); }
@Test public void shouldMapCorrectly() throws Throwable { // Given TypeChecker mapper = new TypeMappers().checkerFor( javaClass ); // When Object converted = mapper.typeCheck( javaValue ); // Then assertEquals( expectedNeoValue, converted ); }
@Test public void shouldLoadWhiteListedFunction() throws Throwable { // Given procedureCompiler = new ReflectiveProcedureCompiler( new TypeMappers(), components, new ComponentRegistry(), NullLog.getInstance(), new ProcedureConfig( Config.defaults( GraphDatabaseSettings.procedure_whitelist, "org.neo4j.kernel.impl.proc.listCoolPeople" ) ) ); CallableUserFunction method = compile( SingleReadOnlyFunction.class ).get( 0 ); // Expect Object out = method.apply( new BasicContext(), new AnyValue[0] ); assertThat(out, equalTo( ValueUtils.of( Arrays.asList("Bonnie", "Clyde") ) ) ); }
@Test public void shouldLoadWhiteListedFunction() throws Throwable { // Given procedureCompiler = new ReflectiveProcedureCompiler( new TypeMappers(), components, new ComponentRegistry(), NullLog.getInstance(), new ProcedureConfig( Config.defaults( GraphDatabaseSettings.procedure_whitelist, "org.neo4j.kernel.impl.proc.collectCool" ) ) ); CallableUserAggregationFunction method = compile( SingleAggregationFunction.class ).get( 0 ); // Expect UserAggregator created = method.create( new BasicContext() ); created.update( new Object[]{"Bonnie"} ); assertThat(created.result(), equalTo( Collections.singletonList( "Bonnie" ) ) ); }
@Before public void setUp() { ComponentRegistry safeComponents = new ComponentRegistry(); ComponentRegistry allComponents = new ComponentRegistry(); safeComponents.register( MyAwesomeAPI.class, ctx -> new MyAwesomeAPI() ); allComponents.register( MyAwesomeAPI.class, ctx -> new MyAwesomeAPI() ); allComponents.register( MyUnsafeAPI.class, ctx -> new MyUnsafeAPI() ); compiler = new ReflectiveProcedureCompiler( new TypeMappers(), safeComponents, allComponents, log, ProcedureConfig.DEFAULT ); }