Skip to content

Add custom resolver example #536

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
}
}
49 changes: 49 additions & 0 deletions examples/src/test/java/org/neo4j/docs/driver/ExamplesStubIT.java
Original file line number Diff line number Diff line change
@@ -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() );
}
}
}
11 changes: 11 additions & 0 deletions examples/src/test/resources/get_routing_table_only.script
Original file line number Diff line number Diff line change
@@ -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: <EXIT>
12 changes: 12 additions & 0 deletions examples/src/test/resources/return_1.script
Original file line number Diff line number Diff line change
@@ -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 {}