@Override public void evaluate() throws Throwable { try ( ServerControls sc = controls = builder.newServer() ) { try { base.evaluate(); } catch ( Throwable t ) { if ( dumpLogsOnFailureTarget != null ) { sc.printLogs( dumpLogsOnFailureTarget ); } throw t; } } } };
private void testStartupWithConnectors( boolean httpEnabled, boolean httpsEnabled, boolean boltEnabled ) { TestServerBuilder serverBuilder = newInProcessBuilder( testDir.directory() ) .withConfig( "dbms.connector.http.enabled", Boolean.toString( httpEnabled ) ) .withConfig( "dbms.connector.http.listen_address", ":0" ) .withConfig( "dbms.connector.https.enabled", Boolean.toString( httpsEnabled ) ) .withConfig( "dbms.connector.https.listen_address", ":0" ) .withConfig( "dbms.connector.bolt.enabled", Boolean.toString( boltEnabled ) ) .withConfig( "dbms.connector.bolt.listen_address", ":0" ); try ( ServerControls server = serverBuilder.newServer() ) { GraphDatabaseService db = server.graph(); assertDbAccessible( db ); verifyConnector( db, "http", httpEnabled ); verifyConnector( db, "https", httpsEnabled ); verifyConnector( db, "bolt", boltEnabled ); } }
@Override public Neo4jRule withFixture( String fixtureStatement ) { builder = builder.withFixture( fixtureStatement ); return this; }
@Test public void shouldHandleFixturesWithSyntaxErrorsGracefully() throws Exception { // Given two files in the root folder File targetFolder = testDir.directory(); FileUtils.writeToFile( new File( targetFolder, "fixture1.cyp" ), "this is not a valid cypher statement", false ); // When try ( ServerControls ignore = getServerBuilder( targetFolder ) .withFixture( targetFolder ).newServer() ) { fail("Should have thrown exception"); } catch ( RuntimeException e ) { assertThat( e.getMessage(), equalTo( "Invalid input 't': expected <init> (line 1, column 1 (offset: 0))" + lineSeparator() + "\"this is not a valid cypher statement\"" + lineSeparator() + " ^" ) ); } }
@BeforeClass public static void setUp() throws Exception { server = TestServerBuilders.newInProcessBuilder().withFixture(setup) .withConfig("dbms.security.auth_enabled", "false").newServer(); BOLT_URL = "'"+server.boltURI().toString()+"'"; db = new TestGraphDatabaseFactory().newImpermanentDatabaseBuilder().newGraphDatabase(); TestUtil.registerProcedure(db, Bolt.class); }
@Test public void testOgmPluginExtension() throws Exception { try (ServerControls controls = TestServer.newInProcessBuilder() .withConfig(GraphDatabaseSettings.auth_enabled, "false") .withExtension(TEST_PATH, TestOgmExtension.class) .newServer()) { URI testURI = controls.httpURI().resolve(TEST_PATH); HTTP.Response saveResponse = HTTP.POST(testURI.toString()); assertThat(saveResponse.status()).isEqualTo(200); HTTP.Response loadResponse = HTTP.GET(testURI.toString()); assertThat(loadResponse.rawContent()).isEqualTo("[{\"id\":0,\"name\":\"new user\"}]"); } }
@Test public void shouldMountUnmanagedExtensionsByClass() { // When try ( ServerControls server = getTestServerBuilder( testDir.directory() ) .withExtension( "/path/to/my/extension", MyUnmanagedExtension.class ) .newServer() ) { // Then assertThat( HTTP.GET( server.httpURI().toString() + "path/to/my/extension/myExtension" ).status(), equalTo( 234 ) ); } }
@Test public void shouldFailWhenProvidingANonDirectoryAsSource() throws IOException { File notADirectory = File.createTempFile( "prefix", "suffix" ); assertFalse( notADirectory.isDirectory() ); try ( ServerControls ignored = getTestServerBuilder( testDir.directory() ) .copyFrom( notADirectory ).newServer() ) { fail( "server should not start" ); } catch ( RuntimeException rte ) { Throwable cause = rte.getCause(); assertTrue( cause instanceof IOException ); assertTrue( cause.getMessage().contains( "exists but is not a directory" ) ); } }
@Override public Neo4jRule withConfig( Setting<?> key, String value ) { builder = builder.withConfig( key, value ); return this; }
@BeforeClass public static void initializeNeo4j() throws IOException { serverControls = TestServerBuilders.newInProcessBuilder().withProcedure(ApocLovesSwitch.class) .newServer(); boltURI = serverControls.boltURI(); }
@Test public void testMyExtensionWithFunctionFixture() throws Exception { // Given try ( ServerControls server = TestServerBuilders.newInProcessBuilder() .withExtension( "/myExtension", MyUnmanagedExtension.class ) .withFixture( new Function<GraphDatabaseService, Void>() { @Override public Void apply( GraphDatabaseService graphDatabaseService ) throws RuntimeException { try ( Transaction tx = graphDatabaseService.beginTx() ) { graphDatabaseService.createNode( DynamicLabel.label( "User" ) ); tx.success(); } return null; } } ) .newServer() ) { // When Result result = server.graph().execute( "MATCH (n:User) return n" ); // Then assertEquals( 1, IteratorUtil.count( result ) ); } } // END SNIPPET: testExtension
@Override public Neo4jRule withExtension( String mountPath, String packageName ) { builder = builder.withExtension( mountPath, packageName ); return this; }
@Override public Neo4jRule withProcedure( Class<?> procedureClass ) { builder = builder.withProcedure( procedureClass ); return this; }
@Override public Neo4jRule copyFrom( File sourceDirectory ) { builder = builder.copyFrom( sourceDirectory ); return this; }
@Test public void shouldHandleStringFixtures() throws Exception { // Given two files in the root folder File targetFolder = testDir.directory(); // When try ( ServerControls server = getServerBuilder( targetFolder ) .withFixture( "CREATE (a:User)" ) .newServer() ) { // Then HTTP.Response response = HTTP.POST( server.httpURI().toString() + "db/data/transaction/commit", quotedJson( "{'statements':[{'statement':'MATCH (n:User) RETURN n'}]}" ) ); assertThat( response.get( "results" ).get(0).get("data").size(), equalTo(1)); } }
@Test public void ogmExtensionShouldUseProvidedDatabase() throws Exception { try (ServerControls controls = TestServer.newInProcessBuilder() .withConfig(GraphDatabaseSettings.auth_enabled, "false") .withExtension(TEST_PATH, TestOgmExtension.class) .newServer()) { URI testURI = controls.httpURI().resolve(TEST_PATH); GraphDatabaseService service = controls.graph(); try (Transaction tx = service.beginTx()) { service.execute("CREATE (u:User {name:'new user'})"); tx.success(); } HTTP.Response loadResponse = HTTP.GET(testURI.toString()); assertThat(loadResponse.rawContent()).isEqualTo("[{\"id\":0,\"name\":\"new user\"}]"); } }
@Test public void shouldMountUnmanagedExtensionsByPackage() { // When try ( ServerControls server = getTestServerBuilder( testDir.directory() ) .withExtension( "/path/to/my/extension", "org.neo4j.harness.extensionpackage" ) .newServer() ) { // Then assertThat( HTTP.GET( server.httpURI().toString() + "path/to/my/extension/myExtension" ).status(), equalTo( 234 ) ); } }
@Override public Neo4jRule withConfig( String key, String value ) { builder = builder.withConfig( key, value ); return this; }
@Override public Neo4jRule withExtension( String mountPath, Class<?> extension ) { builder = builder.withExtension( mountPath, extension ); return this; }