BoltConfig boltConfig = getBoltConfiguration(); Config.ConfigBuilder configBuilder = Config.build(); configBuilder.withMaxSessions(boltConfig.sessionPoolSize); if (boltConfig.encryptionLevel.equals(Config.EncryptionLevel.REQUIRED)) { configBuilder.withEncryption(); } else { configBuilder.withoutEncryption(); configBuilder.withTrustStrategy( Config.TrustStrategy.trustOnFirstUse(new File(new URI(boltConfig.trustCertFile)))); configBuilder.withTrustStrategy( Config.TrustStrategy.trustSignedBy(new File(new URI(boltConfig.trustCertFile)))); configBuilder.withConnectionLivenessCheckTimeout(boltConfig.connectionLivenessCheckTimeout, TimeUnit.MILLISECONDS); return configBuilder.toConfig(); } catch (Exception e) { throw new ConnectionException("Unable to build driver configuration", e);
public BoltCypherExecutor(String url, String username, String password) { boolean hasPassword = password != null && !password.isEmpty(); AuthToken token = hasPassword ? AuthTokens.basic(username, password) : AuthTokens.none(); driver = GraphDatabase.driver(url, token, Config.build().withEncryptionLevel(Config.EncryptionLevel.NONE).toConfig()); }
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();
BoltConfig boltConfig = getBoltConfiguration(driverConfig); Config.ConfigBuilder configBuilder = Config.build(); configBuilder.withMaxSessions(boltConfig.sessionPoolSize); if (boltConfig.encryptionLevel.equals(Config.EncryptionLevel.REQUIRED)) { configBuilder.withEncryption(); } else { configBuilder.withoutEncryption(); configBuilder.withTrustStrategy( Config.TrustStrategy.trustOnFirstUse(new File(new URI(boltConfig.trustCertFile)))); configBuilder.withTrustStrategy( Config.TrustStrategy.trustSignedBy(new File(new URI(boltConfig.trustCertFile)))); configBuilder.withConnectionLivenessCheckTimeout(boltConfig.connectionLivenessCheckTimeout, TimeUnit.MILLISECONDS); return configBuilder.toConfig(); } catch (Exception e) { throw new ConnectionException("Unable to build driver configuration", e);
@Test public void shouldCreateAndPatchANewStateWithAdditionalLabelAndDateButWithANewProp() throws Throwable { // This is in a try-block, to make sure we close the driver after the test try (Driver driver = GraphDatabase .driver(neo4j.boltURI(), Config.build().withEncryption().toConfig()); Session session = driver.session()) { // Given session.run("CREATE (e:Entity {key:'immutableValue'})-[:CURRENT {date:localdatetime('1988-10-27T00:00:00')}]->(s:State {key:'initialValue'})"); session.run("MATCH (e:Entity)-[:CURRENT]->(s:State) CREATE (e)-[:HAS_STATE {startDate:localdatetime('1988-10-27T00:00:00')}]->(s)"); // When StatementResult result = session.run("MATCH (e:Entity) WITH e CALL graph.versioner.patch(e, {newKey:'newestValue'}, 'Error', localdatetime('1988-10-27T02:46:40')) YIELD node RETURN node"); Node currentState = result.single().get("node").asNode(); StatementResult countStateResult = session.run("MATCH (s:State) RETURN count(s) as s"); StatementResult nextResult = session.run("MATCH (s1:State)-[:PREVIOUS]->(s2:State) return s2"); StatementResult correctStateResult = session.run("MATCH (s1:State)-[:PREVIOUS]->(s2:State) WITH s1 MATCH (e:Entity)-[:CURRENT]->(s1) return e"); StatementResult currentStateResult = session.run("MATCH (e:Entity)-[:CURRENT]->(s) return s"); StatementResult dateResult = session.run("MATCH (e:Entity)-[r:CURRENT]->(s) RETURN r.date as relDate"); StatementResult hasStatusDateResult = session.run("MATCH (e:Entity)-[:CURRENT]->(s:State)-[:PREVIOUS]->(s2:State)<-[rel:HAS_STATE]-(e) RETURN rel.endDate as endDate"); // Then assertThat(countStateResult.single().get("s").asLong(), equalTo(2L)); assertThat(nextResult.single().get("s2").asNode().id(), equalTo(1L)); assertThat(correctStateResult.single().get("e").asNode().id(), equalTo(0L)); assertThat(currentStateResult.single().get("s").asNode().hasLabel("Error"), equalTo(true)); assertThat(dateResult.single().get("relDate").asLocalDateTime(), equalTo(convertEpochToLocalDateTime(593920000000L))); assertThat(hasStatusDateResult.single().get("endDate").asLocalDateTime(), equalTo(convertEpochToLocalDateTime(593920000000L))); assertThat(currentState.get("key").asString(), equalTo("initialValue")); assertThat(currentState.get("newKey").asString(), equalTo("newestValue")); } }
@Test public void shouldCreateACopyOfTheGivenStateWithAdditionalDate() throws Throwable { // This is in a try-block, to make sure we close the driver after the test try (Driver driver = GraphDatabase .driver(neo4j.boltURI(), Config.build().withEncryption().toConfig()); Session session = driver.session()) { // Given session.run("CREATE (e:Entity {key:'immutableValue'})-[:CURRENT {date:localdatetime('1988-10-27T00:00:00')}]->(s:State {key:'initialValue', newKey:'oldestValue'})"); session.run("MATCH (e:Entity {key:'immutableValue'})-[:CURRENT {date:localdatetime('1988-10-27T00:00:00')}]->(s:State {key:'initialValue'}) CREATE (e)-[:HAS_STATE {startDate:localdatetime('1988-10-27T00:00:00')}]->(s)"); session.run("MATCH (e:Entity) CREATE (e)-[:HAS_STATE {startDate:localdatetime('1988-10-26T00:00:00'), endDate:localdatetime('1988-10-27T00:00:00')}]->(s:State:Test {newKey:'newestValue'})"); session.run("MATCH (sc:State)<-[:CURRENT]-(e:Entity)-[:HAS_STATE]->(s:Test) CREATE (sc)-[:PREVIOUS {date:localdatetime('1988-10-26T00:00:00')}]->(s)"); StatementResult stateResult = session.run("MATCH (s:Test) RETURN s"); Node originalState = stateResult.single().get("s").asNode(); // When StatementResult result = session.run("MATCH (e:Entity)-[:HAS_STATE]->(s:Test) WITH e, s CALL graph.versioner.patch.from(e, s, false, localdatetime('1988-10-27T02:46:40')) YIELD node RETURN node"); Node currentState = result.single().get("node").asNode(); StatementResult countStateResult = session.run("MATCH (s:State) RETURN count(s) as s"); StatementResult correctStateResult = session.run("MATCH (s1:State)-[:PREVIOUS]->(s2:State) WITH s1 MATCH (e:Entity)-[:CURRENT]->(s1) return e"); StatementResult dateResult = session.run("MATCH (e:Entity)-[r:CURRENT]->(s) RETURN r.date as relDate"); StatementResult hasStatusDateResult = session.run("MATCH (e:Entity)-[:CURRENT]->(s:State)-[:PREVIOUS]->(s2:State)<-[rel:HAS_STATE]-(e) RETURN rel.endDate as endDate"); // Then assertThat(countStateResult.single().get("s").asLong(), equalTo(3L)); assertThat(correctStateResult.single().get("e").asNode().id(), equalTo(0L)); assertThat(currentState.get("key").asString(), equalTo("initialValue")); assertThat(currentState.get("newKey").asString(), equalTo("newestValue")); assertThat(dateResult.single().get("relDate").asLocalDateTime(), equalTo(convertEpochToLocalDateTime(593920000000L))); assertThat(hasStatusDateResult.single().get("endDate").asLocalDateTime(), equalTo(convertEpochToLocalDateTime(593920000000L))); } }
@Test public void shouldRollbackNthWorkCorrectly() { // This is in a try-block, to make sure we close the driver after the test try (Driver driver = GraphDatabase .driver(neo4j.boltURI(), Config.build().withEncryption().toConfig()); Session session = driver.session()) { // Given session.run("CREATE (:Entity {key:'immutableValue'})-[:CURRENT {date:localdatetime('1988-10-27T00:00:00')}]->(:State:Error {key:'initialValue'})" + "-[:PREVIOUS {date: localdatetime('1988-10-26T00:00:00')}]->(:State {key:'value'})-[:PREVIOUS {date: localdatetime('1988-10-25T00:00:00')}]->(:State:Test {key:'testValue'})"); session.run("MATCH (e:Entity)-[:CURRENT]->(s:Error) CREATE (e)-[:HAS_STATE {startDate:localdatetime('1988-10-27T00:00:00')}]->(s)"); session.run("MATCH (e:Entity), (s:State {key:'value'}) CREATE (e)-[:HAS_STATE {startDate:localdatetime('1988-10-26T00:00:00'), endDate:localdatetime('1988-10-27T00:00:00')}]->(s)"); session.run("MATCH (e:Entity), (s:Test) CREATE (e)-[:HAS_STATE {startDate:localdatetime('1988-10-25T00:00:00'), endDate:localdatetime('1988-10-26T00:00:00')}]->(s)"); // When StatementResult result = session.run("MATCH (e:Entity) WITH e CALL graph.versioner.rollback.nth(e, 2) YIELD node RETURN node"); // Then boolean failure = true; while (result.hasNext()) { failure = false; assertThat(result.next().get("node").asNode().hasLabel("Test"), equalTo(true)); } session.run("MATCH (e:Entity)-[:CURRENT]->(s:State) RETURN s"); while(!failure && result.hasNext()) { assertThat(result.next().get("node").asNode().hasLabel("Test"), equalTo(true)); } if (failure) { fail(); } } }
@Test public void shouldCreateACopyOfTheGivenStateWithAdditionalDateButWithANewProp() throws Throwable { // This is in a try-block, to make sure we close the driver after the test try (Driver driver = GraphDatabase .driver(neo4j.boltURI(), Config.build().withEncryption().toConfig()); Session session = driver.session()) { // Given session.run("CREATE (e:Entity {key:'immutableValue'})-[:CURRENT {date:localdatetime('1988-10-27T00:00:00')}]->(s:State {key:'initialValue'})"); session.run("MATCH (e:Entity {key:'immutableValue'})-[:CURRENT {date:localdatetime('1988-10-27T00:00:00')}]->(s:State {key:'initialValue'}) CREATE (e)-[:HAS_STATE {startDate:localdatetime('1988-10-27T00:00:00')}]->(s)"); session.run("MATCH (e:Entity) CREATE (e)-[:HAS_STATE {startDate:localdatetime('1988-10-26T00:00:00'), endDate:localdatetime('1988-10-27T00:00:00')}]->(s:State:Test {newKey:'newestValue'})"); session.run("MATCH (sc:State)<-[:CURRENT]-(e:Entity)-[:HAS_STATE]->(s:Test) CREATE (sc)-[:PREVIOUS {date:localdatetime('1988-10-26T00:00:00')}]->(s)"); StatementResult stateResult = session.run("MATCH (s:Test) RETURN s"); Node originalState = stateResult.single().get("s").asNode(); // When StatementResult result = session.run("MATCH (e:Entity)-[:HAS_STATE]->(s:Test) WITH e, s CALL graph.versioner.patch.from(e, s, false,localdatetime('1988-10-27T02:46:40')) YIELD node RETURN node"); Node currentState = result.single().get("node").asNode(); StatementResult countStateResult = session.run("MATCH (s:State) RETURN count(s) as s"); StatementResult correctStateResult = session.run("MATCH (s1:State)-[:PREVIOUS]->(s2:State) WITH s1 MATCH (e:Entity)-[:CURRENT]->(s1) return e"); StatementResult dateResult = session.run("MATCH (e:Entity)-[r:CURRENT]->(s) RETURN r.date as relDate"); StatementResult hasStatusDateResult = session.run("MATCH (e:Entity)-[:CURRENT]->(s:State)-[:PREVIOUS]->(s2:State)<-[rel:HAS_STATE]-(e) RETURN rel.endDate as endDate"); // Then //assertThat(currentState.id(), equalTo(21L)); assertThat(countStateResult.single().get("s").asLong(), equalTo(3L)); assertThat(correctStateResult.single().get("e").asNode().id(), equalTo(0L)); assertThat(currentState.get("key").asString(), equalTo("initialValue")); assertThat(currentState.get("newKey").asString(), equalTo("newestValue")); assertThat(dateResult.single().get("relDate").asLocalDateTime(), equalTo(convertEpochToLocalDateTime(593920000000L))); assertThat(hasStatusDateResult.single().get("endDate").asLocalDateTime(), equalTo(convertEpochToLocalDateTime(593920000000L))); } }
@Test public void shouldUseExistingRollbackRelationshipToRollBackAgain() { // This is in a try-block, to make sure we close the driver after the test try (Driver driver = GraphDatabase .driver(neo4j.boltURI(), Config.build().withEncryption().toConfig()); Session session = driver.session()) { // Given session.run("CREATE (e:Entity {key:'immutableValue'})-[:CURRENT {date:localdatetime('1988-10-27T00:00:00')}]->(s:State {key:'initialValue'})"); session.run("MATCH (e:Entity {key:'immutableValue'})-[:CURRENT {date:localdatetime('1988-10-27T00:00:00')}]->(s:State {key:'initialValue'}) CREATE (e)-[:HAS_STATE {startDate:localdatetime('1988-10-27T00:00:00')}]->(s)"); session.run("MATCH (e:Entity) CREATE (e)-[:HAS_STATE {startDate:localdatetime('1988-10-26T00:00:00'), endDate:localdatetime('1988-10-27T00:00:00')}]->(s:State {key:'initialValue'})"); session.run("MATCH (sc:State)<-[:CURRENT]-(e:Entity)-[:HAS_STATE {startDate:localdatetime('1988-10-26T00:00:00'), endDate:localdatetime('1988-10-27T00:00:00')}]->(s:State) CREATE (sc)-[:PREVIOUS {date:localdatetime('1988-10-26T00:00:00')}]->(s)"); session.run("MATCH (e:Entity) CREATE (e)-[:HAS_STATE {startDate:localdatetime('1988-10-25T00:00:00'), endDate:localdatetime('1988-10-26T00:00:00')}]->(s:State:Test {key:'initialValue'})"); session.run("MATCH (new:State)<-[:HAS_STATE {startDate:localdatetime('1988-10-26T00:00:00'), endDate:localdatetime('1988-10-27T00:00:00')}]-(e:Entity)-[:HAS_STATE {startDate:localdatetime('1988-10-25T00:00:00'), endDate:localdatetime('1988-10-26T00:00:00')}]->(old:State) CREATE (new)-[:PREVIOUS {date:localdatetime('1988-10-25T00:00:00')}]->(old)"); // When session.run("MATCH (e:Entity) WITH e CALL graph.versioner.rollback(e) YIELD node RETURN node"); StatementResult finalResult = session.run("MATCH (e:Entity) WITH e CALL graph.versioner.rollback(e) YIELD node RETURN node"); // Then boolean failure = true; while (finalResult.hasNext()) { failure = false; assertThat(finalResult.next().get("node").asNode().hasLabel("Test"), equalTo(true)); } if (failure) { fail(); } } }
@Test public void shouldCreateAndPatchANewStateWithAdditionalLabelAndDate() throws Throwable { // This is in a try-block, to make sure we close the driver after the test try (Driver driver = GraphDatabase .driver(neo4j.boltURI(), Config.build().withEncryption().toConfig()); Session session = driver.session()) { // Given session.run("CREATE (e:Entity {key:'immutableValue'})-[:CURRENT {date:localdatetime('1988-10-27T00:00:00')}]->(s:State {key:'initialValue'})"); session.run("MATCH (e:Entity)-[:CURRENT]->(s:State) CREATE (e)-[:HAS_STATE {startDate:localdatetime('1988-10-27T00:00:00')}]->(s)"); // When StatementResult result = session.run("MATCH (e:Entity) WITH e CALL graph.versioner.patch(e, {key:'newValue', newKey:'newestValue'}, 'Error', localdatetime('1988-10-27T02:46:40')) YIELD node RETURN node"); Node currentState = result.single().get("node").asNode(); StatementResult countStateResult = session.run("MATCH (s:State) RETURN count(s) as s"); StatementResult nextResult = session.run("MATCH (s1:State)-[:PREVIOUS]->(s2:State) return s2"); StatementResult correctStateResult = session.run("MATCH (s1:State)-[:PREVIOUS]->(s2:State) WITH s1 MATCH (e:Entity)-[:CURRENT]->(s1) return e"); StatementResult currentStateResult = session.run("MATCH (e:Entity)-[:CURRENT]->(s) return s"); StatementResult dateResult = session.run("MATCH (e:Entity)-[r:CURRENT]->(s) RETURN r.date as relDate"); StatementResult hasStatusDateResult = session.run("MATCH (e:Entity)-[:CURRENT]->(s:State)-[:PREVIOUS]->(s2:State)<-[rel:HAS_STATE]-(e) RETURN rel.endDate as endDate"); // Then assertThat(countStateResult.single().get("s").asLong(), equalTo(2L)); assertThat(nextResult.single().get("s2").asNode().id(), equalTo(1L)); assertThat(correctStateResult.single().get("e").asNode().id(), equalTo(0L)); assertThat(currentStateResult.single().get("s").asNode().hasLabel("Error"), equalTo(true)); assertThat(dateResult.single().get("relDate").asLocalDateTime(), equalTo(convertEpochToLocalDateTime(593920000000L))); assertThat(hasStatusDateResult.single().get("endDate").asLocalDateTime(), equalTo(convertEpochToLocalDateTime(593920000000L))); assertThat(currentState.get("key").asString(), equalTo("newValue")); assertThat(currentState.get("newKey").asString(), equalTo("newestValue")); } }
@Test public void shouldGetAllStateNodesByGivenEntity() { // This is in a try-block, to make sure we close the driver after the test try (Driver driver = GraphDatabase .driver(neo4j.boltURI(), Config.build().withEncryption().toConfig()); Session session = driver.session()) { // Given session.run("CREATE (e:Entity {key:'immutableValue'})-[:CURRENT {date:localdatetime('1988-10-27T00:00:00')}]->(s:State {key:'initialValue'})"); session.run("MATCH (e:Entity {key:'immutableValue'})-[:CURRENT {date:localdatetime('1988-10-27T00:00:00')}]->(s:State {key:'initialValue'}) CREATE (e)-[:HAS_STATE {startDate:localdatetime('1988-10-27T00:00:00')}]->(s)"); session.run("MATCH (e)-[hs:HAS_STATE]->(s) CREATE (e)-[:HAS_STATE {startDate: localdatetime('1988-10-26T00:00:00'), endDate: hs.startDate}]->(:State{key:'oldState'})"); session.run("MATCH (s1:State {key:'oldState'}), (s2:State {key:'initialValue'}) CREATE (s1)<-[:PREVIOUS {date: localdatetime('1988-10-26T00:00:00')}]-(s2) "); Node entity = session.run("MATCH (e:Entity) RETURN e").single().get("e").asNode(); Node stateNew = session.run("MATCH (s:State {key:'initialValue'}) RETURN s").single().get("s").asNode(); Node stateOld = session.run("MATCH (s:State {key:'oldState'}) RETURN s").single().get("s").asNode(); // When StatementResult result = session.run("MATCH (e:Entity) WITH e CALL graph.versioner.get.all(e) YIELD path RETURN path"); Path current = result.single().get("path").asPath(); Iterator<Relationship> relsIterator = current.relationships().iterator(); Map<String, Object> rels = new HashMap<>(); while (relsIterator.hasNext()) { Relationship support = relsIterator.next(); rels.put(support.type(), support); } // Then assertThat(current.contains(entity), equalTo(true)); assertThat(current.contains(stateNew), equalTo(true)); assertThat(rels.containsKey(Utility.PREVIOUS_TYPE), equalTo(true)); assertThat(current.contains(stateOld), equalTo(true)); } }
@Test public void shouldCreateANewStateWithAdditionalLabelAndDate() throws Throwable { // This is in a try-block, to make sure we close the driver after the test try (Driver driver = GraphDatabase .driver(neo4j.boltURI(), Config.build().withEncryption().toConfig()); Session session = driver.session()) { // Given session.run("CREATE (e:Entity {key:'immutableValue'})-[:CURRENT {date:localdatetime('1988-10-27T00:00:00')}]->(s:State {key:'initialValue'})"); session.run("MATCH (e:Entity)-[:CURRENT]->(s:State) CREATE (e)-[:HAS_STATE {startDate:localdatetime('1988-10-27T00:00:00')}]->(s)"); // When StatementResult result = session.run("MATCH (e:Entity) WITH e CALL graph.versioner.update(e, {key:'newValue'}, 'Error', localdatetime('1988-10-27T02:46:40')) YIELD node RETURN node"); StatementResult countStateResult = session.run("MATCH (s:State) RETURN count(s) as s"); StatementResult nextResult = session.run("MATCH (s1:State)-[:PREVIOUS]->(s2:State) return s2"); StatementResult correctStateResult = session.run("MATCH (s1:State)-[:PREVIOUS]->(s2:State) WITH s1 MATCH (e:Entity)-[:CURRENT]->(s1) return e"); StatementResult currentStateResult = session.run("MATCH (e:Entity)-[:CURRENT]->(s) return s"); StatementResult dateResult = session.run("MATCH (e:Entity)-[r:CURRENT]->(s) RETURN r.date as relDate"); StatementResult hasStatusDateResult = session.run("MATCH (e:Entity)-[:CURRENT]->(s:State)-[:PREVIOUS]->(s2:State)<-[rel:HAS_STATE]-(e) RETURN rel.endDate as endDate"); // Then assertThat(countStateResult.single().get("s").asLong(), equalTo(2L)); assertThat(nextResult.single().get("s2").asNode().id(), equalTo(1L)); assertThat(correctStateResult.single().get("e").asNode().id(), equalTo(0L)); assertThat(currentStateResult.single().get("s").asNode().hasLabel("Error"), equalTo(true)); assertThat(dateResult.single().get("relDate").asLocalDateTime(), equalTo(convertEpochToLocalDateTime(593920000000L))); assertThat(hasStatusDateResult.single().get("endDate").asLocalDateTime(), equalTo(convertEpochToLocalDateTime(593920000000L))); } }
@Test public void shouldCreateACopyOfTheGivenStateWithoutAdditionalDate() throws Throwable { // This is in a try-block, to make sure we close the driver after the test try (Driver driver = GraphDatabase .driver(neo4j.boltURI(), Config.build().withEncryption().toConfig()); Session session = driver.session()) { // Given session.run("CREATE (e:Entity {key:'immutableValue'})-[:CURRENT {date:localdatetime('1988-10-27T00:00:00')}]->(s:State {key:'initialValue', newKey:'oldestValue'})"); session.run("MATCH (e:Entity {key:'immutableValue'})-[:CURRENT {date:localdatetime('1988-10-27T00:00:00')}]->(s:State {key:'initialValue'}) CREATE (e)-[:HAS_STATE {startDate:localdatetime('1988-10-27T00:00:00')}]->(s)"); session.run("MATCH (e:Entity) CREATE (e)-[:HAS_STATE {startDate:localdatetime('1988-10-26T00:00:00'), endDate:localdatetime('1988-10-27T00:00:00')}]->(s:State:Test {newKey:'newestValue'})"); session.run("MATCH (sc:State)<-[:CURRENT]-(e:Entity)-[:HAS_STATE]->(s:Test) CREATE (sc)-[:PREVIOUS {date:localdatetime('1988-10-26T00:00:00')}]->(s)"); StatementResult stateResult = session.run("MATCH (s:Test) RETURN s"); Node originalState = stateResult.single().get("s").asNode(); // When StatementResult result = session.run("MATCH (e:Entity)-[:HAS_STATE]->(s:Test) WITH e, s CALL graph.versioner.patch.from(e, s) YIELD node RETURN node"); Node currentState = result.single().get("node").asNode(); StatementResult countStateResult = session.run("MATCH (s:State) RETURN count(s) as s"); StatementResult correctStateResult = session.run("MATCH (s1:State)-[:PREVIOUS]->(s2:State) WITH s1 MATCH (e:Entity)-[:CURRENT]->(s1) return e"); // Then assertThat(countStateResult.single().get("s").asLong(), equalTo(3L)); assertThat(correctStateResult.single().get("e").asNode().id(), equalTo(0L)); assertThat(currentState.get("key").asString(), equalTo("initialValue")); assertThat(currentState.get("newKey").asString(), equalTo("newestValue")); } }
@Test public void shouldCreateAnEntityWithPropertiesWithAStateAndItsPropertiesWithAdditionalLabelAndDate() throws Throwable { // This is in a try-block, to make sure we close the driver after the test try (Driver driver = GraphDatabase .driver(neo4j.boltURI(), Config.build().withEncryption().toConfig()); Session session = driver.session()) { // When StatementResult result = session.run("CALL graph.versioner.init('Entity', {key:'value'}, {key:'value'}, 'Error', localdatetime('1988-10-27T02:46:40'))"); StatementResult entityResult = session.run("MATCH (e:Entity) RETURN properties(e) as props"); StatementResult stateResult = session.run("MATCH (s:State) RETURN s"); Node state = stateResult.single().get("s").asNode(); StatementResult stateProps = session.run("MATCH (s:State) RETURN properties(s) as props"); StatementResult currentResult = session.run("MATCH (e:Entity)-[:CURRENT]->(s:State) RETURN id(e) as id"); StatementResult hasStatusResult = session.run("MATCH (e:Entity)-[:HAS_STATE]->(s:State) RETURN id(e) as id"); StatementResult hasStatusDateResult = session.run("MATCH (e:Entity)-[rel:CURRENT]->(s:State) RETURN rel.date as date"); // Then assertThat(result.single().get("node").asNode().id(), equalTo(0L)); assertThat(entityResult.single().get("props").asMap().isEmpty(), equalTo(false)); assertThat(state.id(), equalTo(1L)); assertThat(stateProps.single().get("props").asMap().isEmpty(), equalTo(false)); assertThat(currentResult.single().get("id").asLong(), equalTo(0L)); assertThat(hasStatusResult.single().get("id").asLong(), equalTo(0L)); assertThat(state.hasLabel("Error"), equalTo(true)); assertThat(hasStatusDateResult.single().get("date").asLocalDateTime(), equalTo(convertEpochToLocalDateTime(593920000000L))); } }
@Test public void shouldCreateAnEntityWithPropertiesWithAStateAndItsPropertiesWithAdditionalLabelButNoDate() throws Throwable { // This is in a try-block, to make sure we close the driver after the test try (Driver driver = GraphDatabase .driver(neo4j.boltURI(), Config.build().withEncryption().toConfig()); Session session = driver.session()) { // When StatementResult result = session.run("CALL graph.versioner.init('Entity', {key:'value'}, {key:'value'}, 'Error')"); StatementResult entityResult = session.run("MATCH (e:Entity) RETURN properties(e) as props"); StatementResult stateResult = session.run("MATCH (s:State) RETURN s"); Node state = stateResult.single().get("s").asNode(); StatementResult stateProps = session.run("MATCH (s:State) RETURN properties(s) as props"); StatementResult currentResult = session.run("MATCH (e:Entity)-[:CURRENT]->(s:State) RETURN id(e) as id"); StatementResult hasStatusResult = session.run("MATCH (e:Entity)-[:HAS_STATE]->(s:State) RETURN id(e) as id"); // Then assertThat(result.single().get("node").asNode().id(), equalTo(0L)); assertThat(entityResult.single().get("props").asMap().isEmpty(), equalTo(false)); assertThat(state.id(), equalTo(1L)); assertThat(stateProps.single().get("props").asMap().isEmpty(), equalTo(false)); assertThat(currentResult.single().get("id").asLong(), equalTo(0L)); assertThat(hasStatusResult.single().get("id").asLong(), equalTo(0L)); assertThat(state.hasLabel("Error"), equalTo(true)); } }
@Test public void shouldGetOldNodeAfterARollbackOnATwoStateEntityNode() { // This is in a try-block, to make sure we close the driver after the test try (Driver driver = GraphDatabase .driver(neo4j.boltURI(), Config.build().withEncryption().toConfig()); Session session = driver.session()) { // Given session.run("CREATE (e:Entity {key:'immutableValue'})-[:CURRENT {date:localdatetime('1988-10-27T00:00:00')}]->(s:State {key:'initialValue'})"); session.run("MATCH (e:Entity {key:'immutableValue'})-[:CURRENT {date:localdatetime('1988-10-27T00:00:00')}]->(s:State {key:'initialValue'}) CREATE (e)-[:HAS_STATE {startDate:localdatetime('1988-10-27T00:00:00')}]->(s)"); session.run("MATCH (e:Entity) CREATE (e)-[:HAS_STATE {startDate:localdatetime('1988-10-26T00:00:00'), endDate:localdatetime('1988-10-27T00:00:00')}]->(s:State:Test {key:'initialValue'})"); session.run("MATCH (sc:State)<-[:CURRENT]-(e:Entity)-[:HAS_STATE]->(s:Test) CREATE (sc)-[:PREVIOUS {date:localdatetime('1988-10-26T00:00:00')}]->(s)"); // When StatementResult result = session.run("MATCH (e:Entity) WITH e CALL graph.versioner.rollback(e) YIELD node RETURN node"); // Then boolean failure = true; while (result.hasNext()) { failure = false; assertThat(result.next().get("node").asNode().hasLabel("Test"), equalTo(true)); } if (failure) { fail(); } } }
.driver(neo4j.boltURI(), Config.build().withEncryption().toConfig()); Session session = driver.session()) {
@Test public void shouldGetCurrentPathByGivenEntity() { // This is in a try-block, to make sure we close the driver after the test try (Driver driver = GraphDatabase .driver(neo4j.boltURI(), Config.build().withEncryption().toConfig()); Session session = driver.session()) { // Given session.run("CREATE (e:Entity {key:'immutableValue'})-[:CURRENT {date:localdatetime('1988-10-27T00:00:00')}]->(s:State {key:'initialValue'})"); session.run("MATCH (e:Entity {key:'immutableValue'})-[:CURRENT {date:localdatetime('1988-10-27T00:00:00')}]->(s:State {key:'initialValue'}) CREATE (e)-[:HAS_STATE {startDate:localdatetime('1988-10-27T00:00:00')}]->(s)"); Node entity = session.run("MATCH (e:Entity) RETURN e").single().get("e").asNode(); Node state = session.run("MATCH (s:State) RETURN s").single().get("s").asNode(); // When StatementResult result = session.run("MATCH (e:Entity) WITH e CALL graph.versioner.get.current.path(e) YIELD path RETURN path"); Path current = result.single().get("path").asPath(); Iterator<Relationship> relsIterator = current.relationships().iterator(); Map<String, Object> rels = new HashMap<>(); while (relsIterator.hasNext()) { Relationship support = relsIterator.next(); rels.put(support.type(), support); } // Then assertThat(current.contains(entity), equalTo(true)); assertThat(rels.containsKey(Utility.CURRENT_TYPE), equalTo(true)); assertThat(current.contains(state), equalTo(true)); } }
@Test public void shouldGetAllErrorStateNodeByGivenErrorLabel() { // This is in a try-block, to make sure we close the driver after the test try (Driver driver = GraphDatabase .driver(neo4j.boltURI(), Config.build().withEncryption().toConfig()); Session session = driver.session()) { // Given session.run("CREATE (e:Entity {key:'immutableValue'})-[:HAS_STATE {startDate:localdatetime('1988-10-27T00:00:00')}]->(s:State:Error {key:'initialValue'})"); session.run("MATCH (e:Entity) CREATE (e)-[:HAS_STATE {startDate:localdatetime('1988-10-27T00:00:00')}]->(s:State:Error {key:'initialValue'})"); session.run("MATCH (e:Entity) CREATE (e)-[:HAS_STATE {startDate:localdatetime('1988-10-27T00:00:00')}]->(s:State:Error {key:'initialValue'})"); // When StatementResult result = session.run("MATCH (e:Entity) WITH e CALL graph.versioner.get.by.label(e, 'Error') YIELD node RETURN node"); // Then boolean failure = true; while (result.hasNext()) { failure = false; assertThat(result.next().get("node").asNode().hasLabel("Error"), equalTo(true)); } if (failure) { fail(); } } }
@Test public void shouldCreateAndPatchANewStateWithoutAdditionalLabelAndDate() throws Throwable { // This is in a try-block, to make sure we close the driver after the test try (Driver driver = GraphDatabase .driver(neo4j.boltURI(), Config.build().withEncryption().toConfig()); Session session = driver.session()) { // Given session.run("CREATE (e:Entity {key:'immutableValue'})-[:CURRENT {date:localdatetime('1988-10-27T00:00:00')}]->(s:State {key:'initialValue'})"); session.run("MATCH (e:Entity)-[:CURRENT]->(s:State) CREATE (e)-[:HAS_STATE {startDate:localdatetime('1988-10-27T00:00:00')}]->(s)"); // When StatementResult result = session.run("MATCH (e:Entity) WITH e CALL graph.versioner.patch(e, {key:'newValue', newKey:'newestValue'}) YIELD node RETURN node"); Node currentState = result.single().get("node").asNode(); StatementResult countStateResult = session.run("MATCH (s:State) RETURN count(s) as s"); StatementResult nextResult = session.run("MATCH (s1:State)-[:PREVIOUS]->(s2:State) return s2"); StatementResult correctStateResult = session.run("MATCH (s1:State)-[:PREVIOUS]->(s2:State) WITH s1 MATCH (e:Entity)-[:CURRENT]->(s1) return e"); // Then assertThat(countStateResult.single().get("s").asLong(), equalTo(2L)); assertThat(nextResult.single().get("s2").asNode().id(), equalTo(1L)); assertThat(correctStateResult.single().get("e").asNode().id(), equalTo(0L)); assertThat(currentState.get("key").asString(), equalTo("newValue")); assertThat(currentState.get("newKey").asString(), equalTo("newestValue")); } }