|
| 1 | +/** |
| 2 | + * Copyright (c) 2002-2015 "Neo Technology," |
| 3 | + * Network Engine for Objects in Lund AB [http://neotechnology.com] |
| 4 | + * |
| 5 | + * This file is part of Neo4j. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | +package org.neo4j.driver.v1.integration; |
| 20 | + |
| 21 | +import org.junit.Rule; |
| 22 | +import org.junit.Test; |
| 23 | + |
| 24 | +import java.io.File; |
| 25 | + |
| 26 | +import org.neo4j.driver.v1.Config; |
| 27 | +import org.neo4j.driver.v1.Driver; |
| 28 | +import org.neo4j.driver.v1.GraphDatabase; |
| 29 | +import org.neo4j.driver.v1.ResultCursor; |
| 30 | +import org.neo4j.driver.v1.Session; |
| 31 | +import org.neo4j.driver.v1.Transaction; |
| 32 | +import org.neo4j.driver.v1.Values; |
| 33 | +import org.neo4j.driver.v1.util.StdIOCapture; |
| 34 | +import org.neo4j.driver.v1.util.TestNeo4j; |
| 35 | + |
| 36 | +import static java.util.Arrays.asList; |
| 37 | +import static org.hamcrest.Matchers.equalTo; |
| 38 | +import static org.junit.Assert.assertThat; |
| 39 | +import static org.neo4j.driver.v1.Config.TlsAuthenticationConfig.usingKnownCerts; |
| 40 | +import static org.neo4j.driver.v1.Config.TlsAuthenticationConfig.usingTrustedCert; |
| 41 | + |
| 42 | +/** |
| 43 | + * The tests below are examples that get pulled into the Driver Manual using the tags inside the tests. |
| 44 | + * |
| 45 | + * DO NOT add tests to this file that are not for that exact purpose. |
| 46 | + * DO NOT modify these tests without ensuring they remain consistent with the equivalent examples in other drivers |
| 47 | + */ |
| 48 | +public class ExamplesIT |
| 49 | +{ |
| 50 | + @Rule |
| 51 | + public TestNeo4j neo4j = new TestNeo4j(); |
| 52 | + |
| 53 | + @Test |
| 54 | + public void minimumViableSnippet() throws Throwable |
| 55 | + { |
| 56 | + StdIOCapture stdIO = new StdIOCapture(); |
| 57 | + try( AutoCloseable captured = stdIO.capture() ) |
| 58 | + { |
| 59 | + // tag::minimum-snippet[] |
| 60 | + Driver driver = GraphDatabase.driver( "bolt://localhost" ); |
| 61 | + Session session = driver.session(); |
| 62 | + |
| 63 | + session.run( "CREATE (neo:Person {name:'Neo', age:23})" ); |
| 64 | + |
| 65 | + ResultCursor result = session.run( "MATCH (p:Person) WHERE p.name = 'Neo' RETURN p.age" ); |
| 66 | + while ( result.next() ) |
| 67 | + { |
| 68 | + System.out.println( "Neo is " + result.value( "p.age" ).asInt() + " years old." ); |
| 69 | + } |
| 70 | + |
| 71 | + session.close(); |
| 72 | + driver.close(); |
| 73 | + // end::minimum-snippet[] |
| 74 | + } |
| 75 | + |
| 76 | + // Then |
| 77 | + assertThat( stdIO.stdout(), equalTo( asList("Neo is 23 years old.") ) ); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void statement() throws Throwable |
| 82 | + { |
| 83 | + StdIOCapture stdIO = new StdIOCapture(); |
| 84 | + try( AutoCloseable captured = stdIO.capture(); |
| 85 | + Driver driver = GraphDatabase.driver( "bolt://localhost" ); |
| 86 | + Session session = driver.session() ) |
| 87 | + { |
| 88 | + // tag::statement[] |
| 89 | + ResultCursor result = session.run( "CREATE (p:Person { name: {name} })", Values.parameters( "name", "The One" ) ); |
| 90 | + |
| 91 | + int theOnesCreated = result.summarize().updateStatistics().nodesCreated(); |
| 92 | + System.out.println("There were " + theOnesCreated + " the ones created."); |
| 93 | + // end::statement[] |
| 94 | + } |
| 95 | + |
| 96 | + // Then |
| 97 | + assertThat( stdIO.stdout(), equalTo( asList("There were 1 the ones created.") ) ); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void statementWithoutParameters() throws Throwable |
| 102 | + { |
| 103 | + StdIOCapture stdIO = new StdIOCapture(); |
| 104 | + try( AutoCloseable captured = stdIO.capture(); |
| 105 | + Driver driver = GraphDatabase.driver( "bolt://localhost" ); |
| 106 | + Session session = driver.session() ) |
| 107 | + { |
| 108 | + // tag::statement-without-parameters[] |
| 109 | + ResultCursor result = session.run( "CREATE (p:Person { name: 'The One' })" ); |
| 110 | + |
| 111 | + int theOnesCreated = result.summarize().updateStatistics().nodesCreated(); |
| 112 | + System.out.println("There were " + theOnesCreated + " the ones created."); |
| 113 | + // end::statement-without-parameters[] |
| 114 | + } |
| 115 | + |
| 116 | + // Then |
| 117 | + assertThat( stdIO.stdout(), equalTo( asList("There were 1 the ones created.") ) ); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void transactionCommit() throws Throwable |
| 122 | + { |
| 123 | + try( Driver driver = GraphDatabase.driver( "bolt://localhost" ); |
| 124 | + Session session = driver.session() ) |
| 125 | + { |
| 126 | + session.run("MATCH (n) DETACH DELETE n"); |
| 127 | + |
| 128 | + // tag::transaction-commit[] |
| 129 | + try( Transaction tx = session.beginTransaction() ) |
| 130 | + { |
| 131 | + tx.run( "CREATE (p:Person { name: 'The One' })" ); |
| 132 | + tx.success(); |
| 133 | + } |
| 134 | + // end::transaction-commit[] |
| 135 | + |
| 136 | + // Then |
| 137 | + assertThat( session.run( "MATCH (p:Person) RETURN count(p)" ).peek().value( 0 ).asInt(), equalTo( 1 ) ); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + public void transactionRollback() throws Throwable |
| 143 | + { |
| 144 | + try( Driver driver = GraphDatabase.driver( "bolt://localhost" ); |
| 145 | + Session session = driver.session() ) |
| 146 | + { |
| 147 | + session.run("MATCH (n) DETACH DELETE n"); |
| 148 | + |
| 149 | + // tag::transaction-rollback[] |
| 150 | + try( Transaction tx = session.beginTransaction() ) |
| 151 | + { |
| 152 | + tx.run( "CREATE (p:Person { name: 'The One' })" ); |
| 153 | + tx.failure(); |
| 154 | + } |
| 155 | + // end::transaction-rollback[] |
| 156 | + |
| 157 | + // Then |
| 158 | + assertThat( session.run( "MATCH (p:Person) RETURN count(p)" ).peek().value( 0 ).asInt(), equalTo( 0 ) ); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + @Test |
| 163 | + public void requireEncryption() throws Throwable |
| 164 | + { |
| 165 | + // tag::tls-require-encryption[] |
| 166 | + Driver driver = GraphDatabase.driver( "bolt://localhost", Config.build() |
| 167 | + .withTlsEnabled( true ) |
| 168 | + .toConfig() ); |
| 169 | + // end::tls-require-encryption[] |
| 170 | + driver.close(); |
| 171 | + } |
| 172 | + |
| 173 | + @Test |
| 174 | + public void trustOnFirstUse() throws Throwable |
| 175 | + { |
| 176 | + // tag::tls-trust-on-first-use[] |
| 177 | + Driver driver = GraphDatabase.driver( "bolt://localhost", Config.build() |
| 178 | + .withTlsEnabled( true ) |
| 179 | + .withTlsAuthConfig( usingKnownCerts( new File("/path/to/neo4j_known_hosts") ) ) |
| 180 | + .toConfig() ); |
| 181 | + // end::tls-trust-on-first-use[] |
| 182 | + driver.close(); |
| 183 | + } |
| 184 | + |
| 185 | + @Test |
| 186 | + public void trustSignedCertificates() throws Throwable |
| 187 | + { |
| 188 | + // tag::tls-signed[] |
| 189 | + Driver driver = GraphDatabase.driver( "bolt://localhost", Config.build() |
| 190 | + .withTlsEnabled( true ) |
| 191 | + .withTlsAuthConfig( usingTrustedCert( new File("/path/to/ca-certificate.pem") ) ) |
| 192 | + .toConfig() ); |
| 193 | + // end::tls-signed[] |
| 194 | + driver.close(); |
| 195 | + } |
| 196 | +} |
0 commit comments