private Driver createDriver(Configuration config, AuthToken authToken) { if (config.getURIS() == null) { return GraphDatabase.driver(config.getURI(), authToken, driverConfig); } else { List<URI> uris = new ArrayList<>(); uris.add(URI.create(config.getURI())); for (String additionalURI : config.getURIS()) { uris.add(URI.create(additionalURI)); } return GraphDatabase.routingDriver(uris, authToken, driverConfig); } }
private String autoCommitUrl() { return transactionEndpoint(configuration.getURI()).concat("/commit"); }
private String newTransactionUrl(Transaction.Type type) { String url = transactionEndpoint(configuration.getURI()); LOGGER.debug("Thread: {}, POST {}", Thread.currentThread().getId(), url); HttpPost request = new HttpPost(url); request.setHeader("X-WRITE", type == Transaction.Type.READ_ONLY ? "0" : "1"); try (CloseableHttpResponse response = executeHttpRequest(request)) { Header location = response.getHeaders("Location")[0]; return location.getValue(); } catch (IOException ioe) { throw new HttpRequestException(request, ioe); } }
private Driver createDriver(Configuration config, Config driverConfig, AuthToken authToken) { if (config.getURIS() == null) { return GraphDatabase.driver(config.getURI(), authToken, driverConfig); } else { List<URI> uris = new ArrayList<>(); uris.add(URI.create(config.getURI())); for (String additionalURI : config.getURIS()) { uris.add(URI.create(additionalURI)); } return GraphDatabase.routingDriver(uris, authToken, driverConfig); } }
public static GraphDatabaseService getGraphDatabaseService() { // if using an embedded config, return the db from the driver if (baseConfiguration.build().getURI().startsWith("file")) { if (driver != null) { return driver.unwrap(GraphDatabaseService.class); } else if (sessionFactory != null) { return sessionFactory.unwrap(GraphDatabaseService.class); } } // else (bolt, http), return just a test server (not really used except for indices ?) return testServer.getGraphDatabaseService(); }
@Test public void shouldConfigureFromFilesystemPropertiesFileURI() throws Exception { File file = new File(getConfigFileAsRelativePath()); FileConfigurationSource source = new FileConfigurationSource(file.toURI().toString()); Configuration configuration = new Configuration.Builder(source).build(); assertThat(configuration.getAutoIndex()).isEqualTo(AutoIndexMode.NONE); assertThat(configuration.getDriverClassName()).isEqualTo("org.neo4j.ogm.drivers.http.driver.HttpDriver"); assertThat(configuration.getCredentials().credentials().toString()).isEqualTo("bmVvNGo6cGFzc3dvcmQ="); assertThat(configuration.getURI()).isEqualTo("http://localhost:7474"); }
@Test public void shouldConfigureFromFilesystemPropertiesFilePath() throws Exception { File file = new File(getConfigFileAsRelativePath()); FileConfigurationSource source = new FileConfigurationSource(file.getAbsolutePath()); Configuration configuration = new Configuration.Builder(source).build(); assertThat(configuration.getAutoIndex()).isEqualTo(AutoIndexMode.NONE); assertThat(configuration.getDriverClassName()).isEqualTo("org.neo4j.ogm.drivers.http.driver.HttpDriver"); assertThat(configuration.getCredentials().credentials().toString()).isEqualTo("bmVvNGo6cGFzc3dvcmQ="); assertThat(configuration.getURI()).isEqualTo("http://localhost:7474"); }
@Test public void shouldParseHttpUriSchemesCaseInsensitive() { Configuration configuration = new Configuration.Builder() .uri("HTTP://localhost") .build(); assertThat(configuration.getDriverClassName()).isEqualTo("org.neo4j.ogm.drivers.http.driver.HttpDriver"); assertThat(configuration.getURI()).isEqualTo("HTTP://localhost"); configuration = new Configuration.Builder() .uri("HTTPS://localhost") .build(); assertThat(configuration.getDriverClassName()).isEqualTo("org.neo4j.ogm.drivers.http.driver.HttpDriver"); assertThat(configuration.getURI()).isEqualTo("HTTPS://localhost"); }
@Test public void shouldLoadBoltDriverConfigFromPropertiesFile() { Configuration driverConfig = new Configuration.Builder( new ClasspathConfigurationSource("bolt.driver.properties")).build(); assertThat(driverConfig.getURI()).isEqualTo("bolt://localhost"); assertThat(driverConfig.getConnectionPoolSize()).isEqualTo(150); assertThat(driverConfig.getEncryptionLevel()).isEqualTo("NONE"); assertThat(driverConfig.getTrustStrategy()).isEqualTo("TRUST_ON_FIRST_USE"); assertThat(driverConfig.getTrustCertFile()).isEqualTo("/tmp/cert"); }
@Test public void shouldParseEmbeddedUriSchemeCaseInsensitive() { Configuration configuration = new Configuration.Builder() .uri("FILE:///somewhere") .build(); assertThat(configuration.getDriverClassName()) .isEqualTo("org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver"); assertThat(configuration.getURI()).isEqualTo("FILE:///somewhere"); }
@Test public void shouldParseBoltUriSchemesCaseInsensitive() { Configuration configuration = new Configuration.Builder() .uri("BOLT://localhost") .build(); assertThat(configuration.getDriverClassName()).isEqualTo("org.neo4j.ogm.drivers.bolt.driver.BoltDriver"); assertThat(configuration.getURI()).isEqualTo("BOLT://localhost"); configuration = new Configuration.Builder() .uri("BOLT+ROUTING://localhost") .build(); assertThat(configuration.getDriverClassName()).isEqualTo("org.neo4j.ogm.drivers.bolt.driver.BoltDriver"); assertThat(configuration.getURI()).isEqualTo("BOLT+ROUTING://localhost"); }
@Test public void shouldConfigureFromSimplePropertiesFile() { Configuration configuration = new Configuration.Builder( new ClasspathConfigurationSource("ogm-simple.properties")).build(); assertThat(configuration.getAutoIndex()).isEqualTo(AutoIndexMode.NONE); assertThat(configuration.getDriverClassName()).isEqualTo("org.neo4j.ogm.drivers.http.driver.HttpDriver"); assertThat(configuration.getCredentials().credentials().toString()).isEqualTo("bmVvNGo6cGFzc3dvcmQ="); assertThat(configuration.getURI()).isEqualTo("http://localhost:7474"); assertThat(configuration.getBasePackages()).isEqualTo(new String[] {"org.neo4j.ogm.domain.bike"}); assertThat(configuration.getUseNativeTypes()).isEqualTo(Boolean.TRUE); }
@Test public void shouldConfigureCredentialsFromURI() { Configuration configuration = new Configuration.Builder().uri("http://fred:flintstone@localhost:8080").build(); // base 64 encoded credentials, e.g. use echo fred:flintstone | base64 assertThat(configuration.getCredentials().credentials().toString()).isEqualTo("ZnJlZDpmbGludHN0b25l"); assertThat(configuration.getURI()).isEqualTo("http://localhost:8080"); }
@Test public void shouldConfigureFromSpringBootPropertiesFile() { Configuration configuration = new Configuration.Builder( new ClasspathConfigurationSource("application.properties")).build(); assertThat(configuration.getAutoIndex()).isEqualTo(AutoIndexMode.NONE); assertThat(configuration.getDriverClassName()).isEqualTo("org.neo4j.ogm.drivers.http.driver.HttpDriver"); assertThat(configuration.getCredentials().credentials().toString()).isEqualTo("bmVvNGo6cGFzc3dvcmQ="); assertThat(configuration.getURI()).isEqualTo("http://localhost:7474"); }
@Test public void shouldConfigureFromNameSpacePropertiesFile() { Configuration configuration = new Configuration.Builder( new ClasspathConfigurationSource("ogm-namespace.properties")).build(); assertThat(configuration.getAutoIndex()).isEqualTo(AutoIndexMode.DUMP); assertThat(configuration.getDumpDir()).isEqualTo("hello"); assertThat(configuration.getDumpFilename()).isEqualTo("generated-indexes2.cql"); assertThat(configuration.getDriverClassName()).isEqualTo("org.neo4j.ogm.drivers.http.driver.HttpDriver"); assertThat(configuration.getCredentials().credentials().toString()).isEqualTo("bmVvNGo6cGFzc3dvcmQ="); assertThat(configuration.getURI()).isEqualTo("http://localhost:7474"); assertThat(configuration.getConnectionPoolSize()).isEqualTo(100); assertThat(configuration.getEncryptionLevel()).isEqualTo("NONE"); assertThat(configuration.getTrustStrategy()).isEqualTo("TRUST_ON_FIRST_USE"); assertThat(configuration.getTrustCertFile()).isEqualTo("/tmp/cert"); }
@Test // GH-169 public void shouldCreateDirectoryIfMissing() throws IOException { final String EMBEDDED_DIR = "/var/tmp/ogmEmbeddedDir"; Path path = Paths.get(EMBEDDED_DIR); if (Files.exists(path)) { FileUtils.deleteDirectory(path); } Configuration configuration = new Configuration.Builder().uri("file://" + EMBEDDED_DIR).build(); try (EmbeddedDriver driver = new EmbeddedDriver()) { driver.configure(configuration); assertThat(configuration.getURI()).isEqualTo("file://" + EMBEDDED_DIR); assertThat(driver.unwrap(GraphDatabaseService.class)).isNotNull(); assertThat(Files.exists(path)).isTrue(); } FileUtils.deleteDirectory(path); }
@Test public void shouldLoadHttpDriverConfigFromPropertiesFile() { Configuration driverConfig = new Configuration.Builder( new ClasspathConfigurationSource("http.driver.properties")).build(); assertThat(driverConfig.getURI()).isEqualTo("http://localhost:7474"); }
@Test public void shouldLoadEmbeddedDriverConfigFromPropertiesFile() { Configuration driverConfig = new Configuration.Builder( new ClasspathConfigurationSource("embedded.driver.properties")).build(); assertThat(driverConfig.getURI()).isEqualTo("file:///var/tmp/neo4j.db"); }
@Test public void shouldCreateImpermanentInstanceWhenNoURI() { Configuration configuration = new Configuration.Builder().build(); try (EmbeddedDriver driver = new EmbeddedDriver()) { driver.configure(configuration); assertThat(configuration.getURI()).isNull(); assertThat(driver.unwrap(GraphDatabaseService.class)).isNotNull(); } }
@Test public void loadLoadBoltDriver() { String uri = getBaseConfiguration().build().getURI(); Configuration driverConfiguration = new Configuration.Builder().uri(uri).build(); SessionFactory sf = new SessionFactory(driverConfiguration, "org.neo4j.ogm.domain.social.User"); Driver driver = sf.unwrap(Driver.class); assertThat(driver).isNotNull(); sf.close(); }