Databases Reference
In-Depth Information
{
db . shutdown ();
}
@Test
public void shouldReturnShortestPathBetweenTwoFriends () throws Exception
{
// when
ExecutionResult results = queries . distance ( "Ben" , "Mike" );
// then
assertTrue ( results . iterator (). hasNext () );
assertEquals ( 4 , results . iterator (). next (). get ( "distance" ) );
}
// more tests
}
This test fixture includes an initialization method, annotated with @BeforeClass , which
executes before any tests start. Here we call createDatabase() to create an instance of
the sample graph, and an instance of SocialNetworkQueries , which houses the queries
under development.
Our first test, shouldReturnShortestPathBetweenTwoFriends() , tests that the query
under development can find a path between any two members of the network—in this
case, Ben and Mike . Given the contents of the sample graph, we know that Ben and Mike
are connected, but only remotely, at a distance of 4. The test, therefore, asserts that the
query returns a nonempty result containing a distance value of 4.
Having written the test, we now start developing our first query. Here's the implemen‐
tation of SocialNetworkQueries :
public class SocialNetworkQueries
{
private final ExecutionEngine executionEngine ;
public SocialNetworkQueries ( GraphDatabaseService db )
{
this . executionEngine = new ExecutionEngine ( db );
}
public ExecutionResult distance ( String firstUser , String secondUser )
{
String query = "START first=node:user({firstUserQuery}),\n" +
"second=node:user({secondUserQuery})\n" +
"MATCH p=shortestPath(first-[*..4]-second)\n" +
"RETURN length(p) AS distance" ;
Map < String , Object > params = new HashMap < String , Object >();
params . put ( "firstUserQuery" , "name:" + firstUser );
params . put ( "secondUserQuery" , "name:" + secondUser );
Search WWH ::




Custom Search