public URI boltURI() { if ( controls == null ) { throw new IllegalStateException( "Cannot access instance URI before or after the test runs." ); } return controls.boltURI(); }
public URI httpURI() { if ( controls == null ) { throw new IllegalStateException( "Cannot access instance URI before or after the test runs." ); } return controls.httpURI(); }
public GraphDatabaseService getGraphDatabaseService() { return controls.graph(); }
@AfterClass public static void tearDown() { db.shutdown(); server.close(); }
@Test public void shouldReturnBoltUriWhenDefaultBoltConnectorOffAndOtherConnectorConfigured() { TestServerBuilder serverBuilder = newInProcessBuilder( testDir.directory() ) .withConfig( "dbms.connector.bolt.enabled", "false" ) .withConfig( "dbms.connector.another_bolt.type", "BOLT" ) .withConfig( "dbms.connector.another_bolt.enabled", "true" ) .withConfig( "dbms.connector.another_bolt.listen_address", ":0" ); try ( ServerControls server = serverBuilder.newServer() ) { HostnamePort boltHostPort = connectorAddress( server.graph(), "bolt" ); HostnamePort anotherBoltHostPort = connectorAddress( server.graph(), "another_bolt" ); assertNull( boltHostPort ); assertNotNull( anotherBoltHostPort ); URI boltUri = server.boltURI(); assertEquals( "bolt", boltUri.getScheme() ); assertEquals( anotherBoltHostPort.getHost(), boltUri.getHost() ); assertEquals( anotherBoltHostPort.getPort(), boltUri.getPort() ); } }
@Test public void translatesServiceUnavailabeException() { try (ServerControls serverControls = TestServerBuilders.newInProcessBuilder().newServer()) { BoltDriver driver = new BoltDriver(); Configuration ogmConfiguration = new Configuration.Builder().uri(serverControls.boltURI().toString()) .verifyConnection(true).build(); driver.configure(ogmConfiguration); SessionFactory sessionFactory = new SessionFactory(driver, DOMAIN_PACKAGE); Session session = sessionFactory.openSession(); serverControls.close(); assertThatExceptionOfType(ConnectionException.class).isThrownBy(() -> { session.loadAll(ConstraintedNode.class); }).withCauseInstanceOf(ServiceUnavailableException.class); } } }
@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\"}]"); } }
public URI httpsURI() { if ( controls == null ) { throw new IllegalStateException( "Cannot access instance URI before or after the test runs." ); } return controls.httpsURI().orElseThrow( () -> new IllegalStateException( "HTTPS connector is not configured" ) ); }
@Override public void start() { if (serverControls == null) { serverControls = new InProcessServerBuilder().newServer(); Runtime.getRuntime().addShutdownHook(new Thread(() -> { try { serverControls.close(); } catch (Exception ignored) { } })); } }
@Test public void shouldReturnBoltUriWhenMultipleBoltConnectorsConfigured() { TestServerBuilder serverBuilder = newInProcessBuilder( testDir.directory() ) .withConfig( "dbms.connector.another_bolt.type", "BOLT" ) .withConfig( "dbms.connector.another_bolt.enabled", "true" ) .withConfig( "dbms.connector.another_bolt.listen_address", ":0" ) .withConfig( "dbms.connector.bolt.enabled", "true" ) .withConfig( "dbms.connector.bolt.listen_address", ":0" ); try ( ServerControls server = serverBuilder.newServer() ) { HostnamePort boltHostPort = connectorAddress( server.graph(), "bolt" ); HostnamePort anotherBoltHostPort = connectorAddress( server.graph(), "another_bolt" ); assertNotNull( boltHostPort ); assertNotNull( anotherBoltHostPort ); assertNotEquals( boltHostPort, anotherBoltHostPort ); URI boltUri = server.boltURI(); assertEquals( "bolt", boltUri.getScheme() ); assertEquals( boltHostPort.getHost(), boltUri.getHost() ); assertEquals( boltHostPort.getPort(), boltUri.getPort() ); } }
private static URI getHttpsUriFromNeo4jRule( URI configuredHttpsUri ) throws Throwable { ServerControls serverControls = mock( ServerControls.class ); when( serverControls.httpsURI() ).thenReturn( Optional.ofNullable( configuredHttpsUri ) ); TestServerBuilder serverBuilder = mock( TestServerBuilder.class ); when( serverBuilder.newServer() ).thenReturn( serverControls ); Neo4jRule rule = new Neo4jRule( serverBuilder ); AtomicReference<URI> uriRef = new AtomicReference<>(); Statement statement = rule.apply( new Statement() { @Override public void evaluate() throws Throwable { uriRef.set( rule.httpsURI() ); } }, createTestDescription( Neo4jRuleTest.class, "test" ) ); statement.evaluate(); return uriRef.get(); } }
@Test public void shouldOpenBoltPort() throws Throwable { // given try ( ServerControls controls = getTestServerBuilder( testDir.directory() ).newServer() ) { URI uri = controls.boltURI(); // when new SocketConnection().connect( new HostnamePort( uri.getHost(), uri.getPort() ) ); // then no exception } }
@Test public void shouldLaunchAServerInSpecifiedDirectory() { // Given File workDir = testDir.directory( "specific" ); // When try ( ServerControls server = getTestServerBuilder( workDir ).newServer() ) { // Then assertThat( HTTP.GET( server.httpURI().toString() ).status(), equalTo( 200 ) ); assertThat( workDir.list().length, equalTo( 1 ) ); } // And after it's been closed, it should've cleaned up after itself. assertThat( Arrays.toString( workDir.list() ), workDir.list().length, equalTo( 0 ) ); }
/** * Stops the underlying server bootstrapper and, in turn, the Neo4j server. */ public void shutdown() { if (database != null && database.isAvailable(100)) { LOGGER.info("Stopping {} server on: {}", enableBolt ? "BOLT" : "HTTP", port); database.shutdown(); database = null; } controls.close(); }
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 ); } }
public URI httpsURI() { if ( controls == null ) { throw new IllegalStateException( "Cannot access instance URI before or after the test runs." ); } return controls.httpsURI().orElseThrow( () -> new IllegalStateException( "HTTPS connector is not configured" ) ); }
public URI boltURI() { if ( controls == null ) { throw new IllegalStateException( "Cannot access instance URI before or after the test runs." ); } return controls.boltURI(); }
@Test public void shouldFindFreePort() { // Given one server is running try ( ServerControls firstServer = getTestServerBuilder( testDir.directory() ).newServer() ) { // When I start a second server try ( ServerControls secondServer = getTestServerBuilder( testDir.directory() ).newServer() ) { // Then assertThat( secondServer.httpURI().getPort(), not( firstServer.httpURI().getPort() ) ); } } }