diff --git a/examples/src/main/java/org/neo4j/docs/driver/ConfigCustomResolverExample.java b/examples/src/main/java/org/neo4j/docs/driver/ConfigCustomResolverExample.java new file mode 100644 index 0000000000..ae1ecf1ae1 --- /dev/null +++ b/examples/src/main/java/org/neo4j/docs/driver/ConfigCustomResolverExample.java @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2002-2018 "Neo4j," + * Neo4j Sweden AB [http://neo4j.com] + * + * This file is part of Neo4j. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.neo4j.docs.driver; + +import java.util.Arrays; +import java.util.HashSet; + +import org.neo4j.driver.v1.AccessMode; +import org.neo4j.driver.v1.AuthTokens; +import org.neo4j.driver.v1.Config; +import org.neo4j.driver.v1.Driver; +import org.neo4j.driver.v1.GraphDatabase; +import org.neo4j.driver.v1.Session; +import org.neo4j.driver.v1.StatementResult; +import org.neo4j.driver.v1.net.ServerAddress; + +import static org.neo4j.driver.v1.Values.parameters; + +public class ConfigCustomResolverExample implements AutoCloseable +{ + private final Driver driver; + + public ConfigCustomResolverExample( String virtualUri, ServerAddress... addresses ) + { + driver = GraphDatabase.driver( virtualUri, AuthTokens.none(), + Config.build().withoutEncryption().withResolver( address -> new HashSet<>( Arrays.asList( addresses ) ) ).toConfig() ); + + } + + // tag::config-custom-resolver[] + private Driver createDriver( String virtualUri, String user, String password, ServerAddress... addresses ) + { + return GraphDatabase.driver( virtualUri, AuthTokens.basic( user, password ), + Config.build().withResolver( address -> new HashSet<>( Arrays.asList( addresses ) ) ).toConfig() ); + } + + private void addPerson( String name ) + { + String username = "neo4j"; + String password = "some password"; + + try ( Driver driver = createDriver( "bolt+routing://x.acme.com", username, password, ServerAddress.of( "a.acme.com", 7676 ), + ServerAddress.of( "b.acme.com", 8787 ), ServerAddress.of( "c.acme.com", 9898 ) ) ) + { + try ( Session session = driver.session( AccessMode.WRITE ) ) + { + session.run( "CREATE (a:Person {name: $name})", parameters( "name", name ) ); + } + } + } + // end::config-custom-resolver[] + + @Override + public void close() throws Exception + { + driver.close(); + } + + public boolean canConnect() + { + StatementResult result = driver.session( AccessMode.READ ).run( "RETURN 1" ); + return result.single().get( 0 ).asInt() == 1; + } +} diff --git a/examples/src/test/java/org/neo4j/docs/driver/ExamplesStubIT.java b/examples/src/test/java/org/neo4j/docs/driver/ExamplesStubIT.java new file mode 100644 index 0000000000..8d10c40df1 --- /dev/null +++ b/examples/src/test/java/org/neo4j/docs/driver/ExamplesStubIT.java @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2002-2018 "Neo4j," + * Neo4j Sweden AB [http://neo4j.com] + * + * This file is part of Neo4j. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.neo4j.docs.driver; + +import org.junit.jupiter.api.Test; + +import org.neo4j.driver.v1.net.ServerAddress; +import org.neo4j.driver.v1.util.StubServer; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class ExamplesStubIT +{ + @Test + void testShouldRunConfigCustomResolverExample() throws Exception + { + StubServer server1 = StubServer.start( "get_routing_table_only.script", 9001 ); + StubServer server2 = StubServer.start( "return_1.script", 9002 ); + + // Given + try ( ConfigCustomResolverExample example = new ConfigCustomResolverExample( "bolt+routing://x.acme.com", ServerAddress.of( "localhost", 9001 ) ) ) + { + // Then + assertTrue( example.canConnect() ); + } + finally + { + assertEquals( 0, server1.exitStatus() ); + assertEquals( 0, server2.exitStatus() ); + } + } +} diff --git a/examples/src/test/resources/get_routing_table_only.script b/examples/src/test/resources/get_routing_table_only.script new file mode 100644 index 0000000000..1b664f21f6 --- /dev/null +++ b/examples/src/test/resources/get_routing_table_only.script @@ -0,0 +1,11 @@ +!: AUTO INIT +!: AUTO RESET +!: AUTO PULL_ALL + +S: SUCCESS {"server": "Neo4j/3.2.2"} +C: RUN "CALL dbms.cluster.routing.getRoutingTable({context})" {"context": {}} + PULL_ALL +S: SUCCESS {"fields": ["ttl", "servers"]} + RECORD [9223372036854775807, [{"addresses": ["127.0.0.1:9001"],"role": "WRITE"}, {"addresses": ["127.0.0.1:9002"], "role": "READ"},{"addresses": ["127.0.0.1:9001"], "role": "ROUTE"}]] + SUCCESS {} +S: diff --git a/examples/src/test/resources/return_1.script b/examples/src/test/resources/return_1.script new file mode 100644 index 0000000000..17a015c773 --- /dev/null +++ b/examples/src/test/resources/return_1.script @@ -0,0 +1,12 @@ +!: AUTO INIT +!: AUTO RESET +!: AUTO PULL_ALL +!: AUTO RUN "COMMIT" {} +!: AUTO RUN "ROLLBACK" {} +!: AUTO RUN "BEGIN" {} + +C: RUN "RETURN 1" {} + PULL_ALL +S: SUCCESS {"fields": ["1"]} + RECORD [1] + SUCCESS {}