private Config configuration() { return Config.build().withEncryptionLevel( Config.EncryptionLevel.NONE ) .withConnectionTimeout( 10, TimeUnit.SECONDS ) .toConfig(); }
@Test public void calls_procedures_with_different_modes_returning_void() { try ( Driver driver = GraphDatabase.driver( graphDb.boltURI(), configuration() ); Session session = driver.session() ) { session.run( "CALL " + procedureNamespace + ".performsWrites()" ); session.run( "CALL " + procedureNamespace + ".defaultMode()" ); session.run( "CALL " + procedureNamespace + ".readMode()" ); session.run( "CALL " + procedureNamespace + ".writeMode()" ); session.run( "CALL " + procedureNamespace + ".schemaMode()" ); session.run( "CALL " + procedureNamespace + ".dbmsMode()" ); } }
@Test public void calls_simplistic_procedure() { try ( Driver driver = GraphDatabase.driver( graphDb.boltURI(), configuration() ); Session session = driver.session() ) { StatementResult result = session.run( "CALL " + procedureNamespace + ".theAnswer()" ); assertThat( result.single().get( "value" ).asLong() ).isEqualTo( 42L ); } }
@Test public void shouldStart() { boolean actual = neo4jContainer.isRunning(); assertThat(actual).isTrue(); try (Driver driver = GraphDatabase .driver(neo4jContainer.getBoltUrl(), AuthTokens.basic("neo4j", "password")); Session session = driver.session() ) { long one = session.run("RETURN 1", Collections.emptyMap()).next().get(0).asLong(); assertThat(one).isEqualTo(1L); } catch (Exception e) { fail(e.getMessage()); } }
@Test public void shouldDisableAuthentication() { try ( Neo4jContainer neo4jContainer = new Neo4jContainer().withAdminPassword(null); ) { neo4jContainer.start(); try (Driver driver = getDriver(neo4jContainer); Session session = driver.session() ) { long one = session.run("RETURN 1", Collections.emptyMap()).next().get(0).asLong(); assertThat(one).isEqualTo(1L); } } }
@Test public void shouldRunEnterprise() { assumeThat(Neo4jContainerTest.class.getResource(ACCEPTANCE_FILE_LOCATION)).isNotNull(); try ( Neo4jContainer neo4jContainer = new Neo4jContainer() .withEnterpriseEdition() .withAdminPassword("Picard123") ) { neo4jContainer.start(); try ( Driver driver = getDriver(neo4jContainer); Session session = driver.session() ) { String edition = session .run("CALL dbms.components() YIELD edition RETURN edition", Collections.emptyMap()) .next().get(0).asString(); assertThat(edition).isEqualTo("enterprise"); } } }
@Test public void calls_procedures_with_simple_input_type_returning_record_with_primitive_fields() { try ( Driver driver = GraphDatabase.driver( graphDb.boltURI(), configuration() ); Session session = driver.session() ) { assertThat( session.run( "CALL " + procedureNamespace + ".simpleInput11('string') YIELD field04 AS p RETURN p" ).single() ).isNotNull(); assertThat( session.run( "CALL " + procedureNamespace + ".simpleInput12(42)" ).single() ).isNotNull(); assertThat( session.run( "CALL " + procedureNamespace + ".simpleInput13(42)" ).single() ).isNotNull(); assertThat( session.run( "CALL " + procedureNamespace + ".simpleInput14(4.2)" ).single() ).isNotNull(); assertThat( session.run( "CALL " + procedureNamespace + ".simpleInput15(true)" ).single() ).isNotNull(); assertThat( session.run( "CALL " + procedureNamespace + ".simpleInput16(false)" ).single() ).isNotNull(); assertThat( session.run( "CALL " + procedureNamespace + ".simpleInput17({foo:'bar'})" ).single() ) .isNotNull(); assertThat( session.run( "CALL " + procedureNamespace + ".simpleInput21()" ).single() ).isNotNull(); } }
String username = conf.getString(dbkey + ".user"); String password = conf.getString(dbkey + ".password"); Driver driver = GraphDatabase.driver(db, AuthTokens.basic(username, password)); Session session = driver.session(); props.setProperty(DBProperties.SERVER_ROOT_URI, db); keys.generate(Driver.class, dbkey, k -> binder.bind(k).toInstance(driver));
private Object toRelationship(Object value, boolean virtual, Map<Long, Object> nodesCache) { Value internalValue = ((InternalEntity) value).asValue(); Relationship relationship = internalValue.asRelationship(); if (virtual) { VirtualNode start = (VirtualNode) nodesCache.getOrDefault(relationship.startNodeId(), new VirtualNode(relationship.startNodeId(), db)); VirtualNode end = (VirtualNode) nodesCache.getOrDefault(relationship.endNodeId(), new VirtualNode(relationship.endNodeId(), db)); VirtualRelationship virtualRelationship = new VirtualRelationship(relationship.id(), start, end, RelationshipType.withName(relationship.type()), relationship.asMap()); return virtualRelationship; } else return Util.map("entityType", internalValue.type().name(), "type", relationship.type(), "id", relationship.id(), "start", relationship.startNodeId(), "end", relationship.endNodeId(), "properties", relationship.asMap()); }
@Procedure() @Description("apoc.bolt.load(url-or-key, kernelTransaction, params, config) - access to other databases via bolt for read") public Stream<RowResult> load(@Name("url") String url, @Name("kernelTransaction") String statement, @Name(value = "params", defaultValue = "{}") Map<String, Object> params, @Name(value = "config", defaultValue = "{}") Map<String, Object> config) throws URISyntaxException { if (params == null) params = Collections.emptyMap(); if (config == null) config = Collections.emptyMap(); boolean virtual = (boolean) config.getOrDefault("virtual", false); boolean addStatistics = (boolean) config.getOrDefault("statistics", false); boolean readOnly = (boolean) config.getOrDefault("readOnly", true); Config driverConfig = toDriverConfig(config.getOrDefault("driverConfig", map())); UriResolver uri = new UriResolver(url, "bolt"); uri.initialize(); try (Driver driver = GraphDatabase.driver(uri.getConfiguredUri(), uri.getToken(), driverConfig); Session session = driver.session()) { if (addStatistics) return Stream.of(new RowResult(toMap(runStatement(statement, session, params, readOnly).summary().counters()))); else return getRowResultStream(virtual, session, params, statement, readOnly); } catch (Exception e) { throw new RuntimeException("It's not possible to create a connection due to: " + e.getMessage()); } }
Long connectionTimeoutMillis = (Long) driverConfMap.getOrDefault("connectionTimeoutMillis", 5000L); Long maxRetryTimeMs = (Long) driverConfMap.getOrDefault("maxRetryTimeMs", 30000L); Config.ConfigBuilder config = Config.build(); config.withLogging(new JULogging(Level.parse(logging))); if(!encryption) config.withoutEncryption(); config.withTrustStrategy(Config.TrustStrategy.trustAllCertificates()); if(!logLeakedSessions) config.withoutEncryption(); config.withMaxIdleSessions(maxIdleConnectionPoolSize.intValue()); config.withConnectionLivenessCheckTimeout(idleTimeBeforeConnectionTest, TimeUnit.MILLISECONDS); config.withRoutingFailureLimit(routingFailureLimit.intValue()); config.withConnectionTimeout(connectionTimeoutMillis, TimeUnit.MILLISECONDS); config.withRoutingRetryDelay(routingRetryDelayMillis,TimeUnit.MILLISECONDS); config.withMaxTransactionRetryTime(maxRetryTimeMs, TimeUnit.MILLISECONDS); if(trustStrategy.equals("TRUST_ALL_CERTIFICATES")) config.withTrustStrategy(Config.TrustStrategy.trustAllCertificates()); else if(trustStrategy.equals("TRUST_SYSTEM_CA_SIGNED_CERTIFICATES")) config.withTrustStrategy(Config.TrustStrategy.trustSystemCertificates()); else { File file = new File(trustStrategy); config.withTrustStrategy(Config.TrustStrategy.trustCustomCertificateSignedBy(file)); return config.toConfig();
public void initialize() throws URISyntaxException { this.url = getConfiguredUri(this.url); URI uri; try { uri = new URI(this.url); } catch (URISyntaxException e) { throw new URISyntaxException(e.getInput(), e.getMessage()); } this.uri = uri; String[] userInfoArray = uri.getUserInfo() == null ? new String[2] : uri.getUserInfo().split(":"); String user = userInfoArray[0]; String password = userInfoArray[1]; if(user != null && password == null || user == null && password != null) throw new RuntimeException("user and password don't defined check your URL or if you use a key the property in your neo4j.conf file"); this.token = (user != null && password != null) ? AuthTokens.basic(user, password) : AuthTokens.none(); } }
private Stream<RowResult> getRowResultStream(boolean virtual, Session session, Map<String, Object> params, String statement, boolean read) { Map<Long, Object> nodesCache = new HashMap<>(); return StreamSupport.stream(Spliterators.spliteratorUnknownSize(runStatement(statement, session, params, read), 0), true) .map(record -> new RowResult(record.asMap(value -> { Object entity = value.asObject(); if (entity instanceof Node) return toNode(entity, virtual, nodesCache); if (entity instanceof Relationship) return toRelationship(entity, virtual, nodesCache); if (entity instanceof Path) return toPath(entity, virtual, nodesCache); return entity; }))); }
private Object toPath(Object value, boolean virtual, Map<Long, Object> nodesCache) { List<Object> entityList = new LinkedList<>(); Value internalValue = ((InternalPath) value).asValue(); internalValue.asPath().forEach(p -> { entityList.add(toNode(p.start(), virtual, nodesCache)); entityList.add(toRelationship(p.relationship(), virtual, nodesCache)); entityList.add(toNode(p.end(), virtual, nodesCache)); }); return entityList; }
private StatementResult runStatement(@Name("kernelTransaction") String statement, Session session, Map<String, Object> finalParams, boolean read) { if (read) return session.readTransaction((Transaction tx) -> tx.run(statement, finalParams)); else return session.writeTransaction((Transaction tx) -> tx.run(statement, finalParams)); }
@Test public void testMetaArray() throws Exception { Map<String, Object> param = map( "ARRAY", new String[]{"a","b","c"}, "ARRAY_FLOAT", new Float[]{1.2f, 2.2f}, "ARRAY_DOUBLE", new Double[]{1.2, 2.2}, "ARRAY_INT", new Integer[]{1, 2}, "ARRAY_OBJECT", new Object[]{1, "a"}, "ARRAY_POINT", new Object[]{Values.pointValue(CoordinateReferenceSystem.WGS84, 56.d, 12.78), Values.pointValue(CoordinateReferenceSystem.WGS84_3D, 56.d, 12.78, 100)}, "ARRAY_DURATION", new Object[]{isoDuration(5, 1, 43200, 0).asIsoDuration(), isoDuration(2, 1, 125454, 0).asIsoDuration()}, "ARRAY_ARRAY", new Object[]{1, "a", new Object[]{"a", 1}, isoDuration(5, 1, 43200, 0).asIsoDuration()}, "NULL", null); TestUtil.testCall(db, "RETURN apoc.meta.cypher.types({param}) AS value", singletonMap("param",param), row -> { Map<String, Object> r = (Map<String, Object>) row.get("value"); assertEquals("LIST OF STRING", r.get("ARRAY")); assertEquals("LIST OF FLOAT", r.get("ARRAY_FLOAT")); assertEquals("LIST OF FLOAT", r.get("ARRAY_DOUBLE")); assertEquals("LIST OF INTEGER", r.get("ARRAY_INT")); assertEquals("LIST OF ANY", r.get("ARRAY_OBJECT")); assertEquals("LIST OF POINT", r.get("ARRAY_POINT")); assertEquals("LIST OF DURATION", r.get("ARRAY_DURATION")); assertEquals("LIST OF ANY", r.get("ARRAY_ARRAY")); assertEquals("NULL", r.get("NULL")); }); }
@Test public void calls_procedures_with_simple_input_type_returning_void() { try ( Driver driver = GraphDatabase.driver( graphDb.boltURI(), configuration() ); Session session = driver.session() ) { session.run( "CALL " + procedureNamespace + ".simpleInput00()" ); session.run( "CALL " + procedureNamespace + ".simpleInput01('string')" ); session.run( "CALL " + procedureNamespace + ".simpleInput02(42)" ); session.run( "CALL " + procedureNamespace + ".simpleInput03(42)" ); session.run( "CALL " + procedureNamespace + ".simpleInput04(4.2)" ); session.run( "CALL " + procedureNamespace + ".simpleInput05(true)" ); session.run( "CALL " + procedureNamespace + ".simpleInput06(false)" ); session.run( "CALL " + procedureNamespace + ".simpleInput07({foo:'bar'})" ); session.run( "MATCH (n) CALL " + procedureNamespace + ".simpleInput08(n) RETURN n" ); session.run( "MATCH p=(()-[r]->()) CALL " + procedureNamespace + ".simpleInput09(p) RETURN p" ); session.run( "MATCH ()-[r]->() CALL " + procedureNamespace + ".simpleInput10(r) RETURN r" ); } }
@Test public void testMetaList() throws Exception { Map<String, Object> param = map( "LIST FLOAT", asList(1.2F, 2.1F), "LIST STRING", asList("a", "b"), "LIST CHAR", asList('a', 'a'), "LIST DATE", asList(LocalDate.of(2018,1,1), LocalDate.of(2018,2,2)), "LIST ANY", asList("test",1,"asd",isoDuration(5, 1, 43200, 0).asIsoDuration()), "LIST NULL", asList("test",null), "LIST POINT", asList(Values.pointValue(CoordinateReferenceSystem.WGS84, 56.d, 12.78), Values.pointValue(CoordinateReferenceSystem.Cartesian_3D, 2.3, 4.5, 1.2)), "LIST DURATION", asList(isoDuration(5, 1, 43200, 0).asIsoDuration(), isoDuration(2, 1, 125454, 0).asIsoDuration()), "LIST OBJECT", new Object[]{LocalDate.of(2018,1,1), "test"}, "LIST OF LIST", asList(asList("a", "b", "c"),asList("aa", "bb", "cc"),asList("aaa", "bbb", "ccc")), "LIST DOUBLE", asList(1.2D, 2.1D)); TestUtil.testCall(db, "RETURN apoc.meta.cypher.types({param}) AS value", singletonMap("param",param), row -> { Map<String, Object> r = (Map<String, Object>) row.get("value"); assertEquals("LIST OF FLOAT", r.get("LIST FLOAT")); assertEquals("LIST OF STRING", r.get("LIST STRING")); assertEquals("LIST OF ANY", r.get("LIST CHAR")); assertEquals("LIST OF DATE", r.get("LIST DATE")); assertEquals("LIST OF FLOAT", r.get("LIST DOUBLE")); assertEquals("LIST OF POINT", r.get("LIST POINT")); assertEquals("LIST OF DURATION", r.get("LIST DURATION")); assertEquals("LIST OF ANY", r.get("LIST ANY")); assertEquals("LIST OF ANY", r.get("LIST OBJECT")); assertEquals("LIST OF LIST", r.get("LIST OF LIST")); assertEquals("LIST OF ANY", r.get("LIST NULL")); }); }
@Test public void testMeta() throws Exception { Map<String, Object> param = map( "LIST", asList(1.2, 2.1), "STRING", "a", "BOOLEAN", true, "CHAR", 'a', "DURATION", 'a', "POINT_2D",Values.pointValue(CoordinateReferenceSystem.WGS84, 56.d, 12.78), "POINT_3D", Values.pointValue(CoordinateReferenceSystem.WGS84_3D, 56.7, 12.78, 100.0), "POINT_XYZ_2D", Values.pointValue(CoordinateReferenceSystem.Cartesian, 2.3, 4.5), "POINT_XYZ_3D", Values.pointValue(CoordinateReferenceSystem.Cartesian_3D, 2.3, 4.5, 1.2), "DURATION", isoDuration(5, 1, 43200, 0).asIsoDuration(), "MAP", Util.map("a", "b"), "NULL", null); TestUtil.testCall(db, "RETURN apoc.meta.cypher.types({param}) AS value", singletonMap("param",param), row -> { Map<String, Object> r = (Map<String, Object>) row.get("value"); assertEquals("LIST OF FLOAT", r.get("LIST")); assertEquals("STRING", r.get("STRING")); assertEquals("BOOLEAN", r.get("BOOLEAN")); assertEquals("Character", r.get("CHAR")); assertEquals("POINT", r.get("POINT_2D")); assertEquals("POINT", r.get("POINT_3D")); assertEquals("POINT", r.get("POINT_XYZ_2D")); assertEquals("POINT", r.get("POINT_XYZ_3D")); assertEquals("DURATION", r.get("DURATION")); assertEquals("MAP", r.get("MAP")); assertEquals("NULL", r.get("NULL")); }); }