@Test public void shouldSetUsernameAndPasswordCredentialsForBoltProtocol() { String username = "neo4j"; String password = "password"; Configuration dbConfig = new Configuration.Builder().uri("bolt://" + username + ":" + password + "@localhost") .build(); Credentials credentials = dbConfig.getCredentials(); UsernamePasswordCredentials basic = (UsernamePasswordCredentials) credentials; assertThat(basic).isNotNull(); assertThat(basic.getUsername()).isEqualTo(username); assertThat(basic.getPassword()).isEqualTo(password); } }
@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 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 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); } } }
public static Builder copy(Builder builder) { return new Builder() .uri(builder.uri) .connectionPoolSize(builder.connectionPoolSize) .encryptionLevel(builder.encryptionLevel) .trustStrategy(builder.trustStrategy) .trustCertFile(builder.trustCertFile) .connectionLivenessCheckTimeout(builder.connectionLivenessCheckTimeout) .verifyConnection(builder.verifyConnection) .autoIndex(builder.autoIndex) .generatedIndexesOutputDir(builder.generatedIndexesOutputDir) .generatedIndexesOutputFilename(builder.generatedIndexesOutputFilename) .neo4jConfLocation(builder.neo4jConfLocation) .credentials(builder.username, builder.password) .customProperties(new HashMap<>(builder.customProperties)); }
/** * Create the {@link Configuration}. * * Reads the values from environment variables. If they are not set, falls back to default values. * * @return the {@link Configuration} */ public static Configuration getConfiguration() { String uri = getEnvOrDefault(ENV_URI, "bolt://localhost"); String username = getEnvOrDefault(ENV_USERNAME, "neo4j"); String password = getEnvOrDefault(ENV_SECRET, "neo4j"); LOGGER.info("Creating configuration for: {} with username: {}", uri, username); return new Configuration.Builder() .uri(uri) .credentials(username, password) .build(); }
@Test public void throwCorrectExceptionOnUnavailableCluster() throws Exception { Configuration configuration = new Configuration.Builder() .uri("bolt+routing://localhost:1022") .uris(new String[] { "bolt+routing://localhost:1023" }) .verifyConnection(true) .build(); try { new SessionFactory(configuration, "org.neo4j.ogm.domain.social"); } catch (Exception e) { Throwable cause = e.getCause(); assertThat(cause).isInstanceOf(ServiceUnavailableException.class); assertThat(cause).hasMessage("Failed to discover an available server"); } }
@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 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"); }
@BeforeClass public static void setupMultiDriverTestEnvironment() { if (baseConfiguration.build().getDriverClassName().equals(EmbeddedDriver.class.getCanonicalName())) { baseConfiguration.uri(graphStore.toURI().toString()).build(); } else { baseConfiguration.uri(testServer.getUri()).credentials(testServer.getUsername(), testServer.getPassword()); } if (driver == null) { Configuration configuration = getBaseConfiguration().build(); driver = newDriverInstance(configuration.getDriverClassName()); driver.configure(configuration); } }
@Test public void shouldLoadHttpDriver() { Configuration driverConfiguration = new Configuration.Builder().uri("http://neo4j:password@localhost:7474") .build(); SessionFactory sf = new SessionFactory(driverConfiguration, "org.neo4j.ogm.domain.social.User"); Driver driver = sf.unwrap(Driver.class); assertThat(driver).isNotNull(); sf.close(); }
@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(); }
@Test public void shouldConfigureCredentialsFromURIWithUTF8Characters() { Configuration configuration = new Configuration.Builder() .uri("http://franti\u0161ek:Pass123@localhost:8080") .build(); assertThat(configuration.getCredentials().credentials().toString()).isEqualTo("ZnJhbnRpxaFlazpQYXNzMTIz"); }
@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"); }
@BeforeClass public static void initSessionFactory() { Driver driver = new HttpDriver(); driver.configure(new Configuration.Builder().uri(serverControls.httpURI().toString()).build()); sessionFactory = new SessionFactory(driver, DOMAIN_PACKAGE); }
@Test @Ignore("this needs local causal cluster running") public void connectToCCUsingConfiguration() throws Exception { Configuration configuration = new Configuration.Builder() .uri("bolt+routing://localhost:1023") .uris(new String[] { "bolt+routing://localhost:7688", "bolt+routing://localhost:7687", "bolt+routing://localhost:7689" }) .verifyConnection(true) .build(); new SessionFactory(configuration, "org.neo4j.ogm.domain.social"); }
@Before public void setUp() throws Exception { configBuilder = new Configuration.Builder(new ClasspathConfigurationSource("ogm-bolt.properties")) .uri("bolt://localhost:" + TestUtils.getAvailablePort()); } }
@Test(expected = RuntimeException.class) public void uriWithNoScheme() { Configuration configuration = new Configuration.Builder().uri("target/noe4j/my.db").build(); fail("Should have thrown a runtime exception about a missing URI Scheme"); }
@Test public void shouldLoadEmbeddedDriver() { Configuration driverConfiguration = new Configuration.Builder().uri(TMP_NEO4J_DB.toUri().toString()).build(); SessionFactory sf = new SessionFactory(driverConfiguration, "org.neo4j.ogm.domain.social.User"); Driver driver = sf.unwrap(Driver.class); assertThat(driver).isNotNull(); driver.close(); sf.close(); }
@Before public void setUp() throws Exception { configBuilder = new Configuration.Builder(new ClasspathConfigurationSource("ogm-http.properties")) .uri("http://neo4j:neo4j12@localhost:" + TestUtils.getAvailablePort()); } }