Skip to content

Commit

Permalink
Fixes from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusmelke committed Sep 21, 2016
1 parent 27490e2 commit dc6cfb9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
Expand Up @@ -255,7 +255,7 @@ private static String toStringForThirdPartyPackageProperty( List<ThirdPartyJaxRs
/** /**
* A kernel extension used to ensure we load user-registered procedures * A kernel extension used to ensure we load user-registered procedures
* after other kernel extensions have initialized, since kernel extensions * after other kernel extensions have initialized, since kernel extensions
* can addProcedure custom injectables that procedures need. * can add custom injectables that procedures need.
*/ */
private static class Neo4jHarnessExtensions extends KernelExtensionFactory<Neo4jHarnessExtensions.Dependencies> private static class Neo4jHarnessExtensions extends KernelExtensionFactory<Neo4jHarnessExtensions.Dependencies>
{ {
Expand Down
Expand Up @@ -65,7 +65,7 @@ public static class MyFunctionsUsingMyService
@Context @Context
public SomeService service; public SomeService service;


@UserFunction("my.hello") @UserFunction( "my.hello" )
public String hello() public String hello()
{ {
return service.hello(); return service.hello();
Expand All @@ -77,7 +77,7 @@ public static class MyFunctionsUsingMyCoreAPI
@Context @Context
public MyCoreAPI myCoreAPI; public MyCoreAPI myCoreAPI;


@UserFunction( value = "my.willFail") @UserFunction( value = "my.willFail" )
public long willFail() throws ProcedureException public long willFail() throws ProcedureException
{ {
return myCoreAPI.makeNode( "Test" ); return myCoreAPI.makeNode( "Test" );
Expand All @@ -94,11 +94,14 @@ public long countNodes()
public void shouldLaunchWithDeclaredFunctions() throws Exception public void shouldLaunchWithDeclaredFunctions() throws Exception
{ {
// When // When
try(ServerControls server = TestServerBuilders.newInProcessBuilder().withFunction( MyFunctions.class ).newServer()) try ( ServerControls server = TestServerBuilders.newInProcessBuilder().withFunction( MyFunctions.class )
.newServer() )
{ {
// Then // Then
HTTP.Response response = HTTP.POST( server.httpURI().resolve( "db/data/transaction/commit" ).toString(), HTTP.Response response = HTTP.POST( server.httpURI().resolve( "db/data/transaction/commit" ).toString(),
quotedJson( "{ 'statements': [ { 'statement': 'RETURN org.neo4j.harness.myFunc() AS someNumber' } ] }" ) ); quotedJson(
"{ 'statements': [ { 'statement': 'RETURN org.neo4j.harness.myFunc() AS someNumber' } ] " +
"}" ) );


JsonNode result = response.get( "results" ).get( 0 ); JsonNode result = response.get( "results" ).get( 0 );
assertEquals( "someNumber", result.get( "columns" ).get( 0 ).asText() ); assertEquals( "someNumber", result.get( "columns" ).get( 0 ).asText() );
Expand All @@ -111,22 +114,28 @@ public void shouldLaunchWithDeclaredFunctions() throws Exception
public void shouldGetHelpfulErrorOnProcedureThrowsException() throws Exception public void shouldGetHelpfulErrorOnProcedureThrowsException() throws Exception
{ {
// When // When
try(ServerControls server = TestServerBuilders.newInProcessBuilder().withFunction( MyFunctions.class ).newServer()) try ( ServerControls server = TestServerBuilders.newInProcessBuilder().withFunction( MyFunctions.class )
.newServer() )
{ {
// Then // Then
HTTP.Response response = HTTP.POST( server.httpURI().resolve( "db/data/transaction/commit" ).toString(), HTTP.Response response = HTTP.POST( server.httpURI().resolve( "db/data/transaction/commit" ).toString(),
quotedJson( "{ 'statements': [ { 'statement': 'RETURN org.neo4j.harness.funcThatThrows()' } ] }" ) ); quotedJson(
"{ 'statements': [ { 'statement': 'RETURN org.neo4j.harness.funcThatThrows()' } ] }" ) );


String error = response.get( "errors" ).get( 0 ).get( "message" ).asText(); String error = response.get( "errors" ).get( 0 ).get( "message" ).asText();
assertEquals( "Failed to invoke function `org.neo4j.harness.funcThatThrows`: Caused by: java.lang.RuntimeException: This is an exception", error ); assertEquals(
"Failed to invoke function `org.neo4j.harness.funcThatThrows`: Caused by: java.lang" +
".RuntimeException: This is an exception",
error );
} }
} }


@Test @Test
public void shouldWorkWithInjectableFromKernelExtension() throws Throwable public void shouldWorkWithInjectableFromKernelExtension() throws Throwable
{ {
// When // When
try(ServerControls server = TestServerBuilders.newInProcessBuilder().withFunction( MyFunctionsUsingMyService.class ).newServer()) try ( ServerControls server = TestServerBuilders.newInProcessBuilder()
.withFunction( MyFunctionsUsingMyService.class ).newServer() )
{ {
// Then // Then
HTTP.Response response = HTTP.POST( server.httpURI().resolve( "db/data/transaction/commit" ).toString(), HTTP.Response response = HTTP.POST( server.httpURI().resolve( "db/data/transaction/commit" ).toString(),
Expand Down
Expand Up @@ -679,13 +679,16 @@ public void shouldCallFunctionWithFourProvidedRestDefaultArgument() throws Throw
assertFalse( res.hasNext() ); assertFalse( res.hasNext() );
} }


/**
* NOTE: this test tests user-defined functions added in this file {@link ClassWithFunctions}. These are not
* built-in functions in any shape or form.
*/
@Test @Test
public void shouldListAllFunctions() throws Throwable public void shouldListAllUserDefinedFunctions() throws Throwable
{ {
//Given/When //Given/When
Result res = db.execute( "CALL dbms.functions()" ); Result res = db.execute( "CALL dbms.functions()" );



String expected = String expected =
"+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+" + lineSeparator() + "+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+" + lineSeparator() +
"| name | signature | description |" + lineSeparator() + "| name | signature | description |" + lineSeparator() +
Expand Down Expand Up @@ -713,9 +716,9 @@ public void shouldListAllFunctions() throws Throwable
"| 'org.neo4j.procedure.simpleArgument' | 'org.neo4j.procedure.simpleArgument(someValue :: INTEGER?) :: (INTEGER?)' | '' |" + lineSeparator() + "| 'org.neo4j.procedure.simpleArgument' | 'org.neo4j.procedure.simpleArgument(someValue :: INTEGER?) :: (INTEGER?)' | '' |" + lineSeparator() +
"| 'org.neo4j.procedure.squareDouble' | 'org.neo4j.procedure.squareDouble(someValue :: FLOAT?) :: (FLOAT?)' | '' |" + lineSeparator() + "| 'org.neo4j.procedure.squareDouble' | 'org.neo4j.procedure.squareDouble(someValue :: FLOAT?) :: (FLOAT?)' | '' |" + lineSeparator() +
"| 'org.neo4j.procedure.squareLong' | 'org.neo4j.procedure.squareLong(someValue :: INTEGER?) :: (INTEGER?)' | '' |" + lineSeparator() + "| 'org.neo4j.procedure.squareLong' | 'org.neo4j.procedure.squareLong(someValue :: INTEGER?) :: (INTEGER?)' | '' |" + lineSeparator() +
"| 'org.neo4j.procedure.sum' | 'org.neo4j.procedure.sum(numbers :: LIST? OF NUMBER?) :: (NUMBER?)' | '' |" + lineSeparator() +
"| 'org.neo4j.procedure.throwsExceptionInStream' | 'org.neo4j.procedure.throwsExceptionInStream() :: (INTEGER?)' | '' |" + lineSeparator() + "| 'org.neo4j.procedure.throwsExceptionInStream' | 'org.neo4j.procedure.throwsExceptionInStream() :: (INTEGER?)' | '' |" + lineSeparator() +
"| 'org.neo4j.procedure.unsupportedFunction' | 'org.neo4j.procedure.unsupportedFunction() :: (STRING?)' | '' |" + lineSeparator() + "| 'org.neo4j.procedure.unsupportedFunction' | 'org.neo4j.procedure.unsupportedFunction() :: (STRING?)' | '' |" + lineSeparator() +
"| 'this.is.test.only.sum' | 'this.is.test.only.sum(numbers :: LIST? OF NUMBER?) :: (NUMBER?)' | '' |" + lineSeparator() +
"+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+" + lineSeparator() + "+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+" + lineSeparator() +
"26 rows" + lineSeparator(); "26 rows" + lineSeparator();


Expand All @@ -726,10 +729,10 @@ public void shouldListAllFunctions() throws Throwable
public void shouldCallFunctionWithSameNameAsBuiltIn() throws Throwable public void shouldCallFunctionWithSameNameAsBuiltIn() throws Throwable
{ {
//Given/When //Given/When
Result res = db.execute( "RETURN org.neo4j.procedure.sum([1337, 2.718281828, 3.1415]) AS result"); Result res = db.execute( "RETURN this.is.test.only.sum([1337, 2.718281828, 3.1415]) AS result" );


// Then // Then
assertThat( res.next().get("result"), equalTo( 1337 + 2.718281828 + 3.1415 ) ); assertThat( res.next().get( "result" ), equalTo( 1337 + 2.718281828 + 3.1415 ) );
assertFalse( res.hasNext() ); assertFalse( res.hasNext() );
} }


Expand Down Expand Up @@ -910,7 +913,7 @@ public long readOnlyCallingWriteProcedure()
return 1337L; return 1337L;
} }


@UserFunction @UserFunction("this.is.test.only.sum")
public Number sum(@Name("numbers") List<Number> numbers) public Number sum(@Name("numbers") List<Number> numbers)
{ {
return numbers.stream().mapToDouble( Number::doubleValue ).sum(); return numbers.stream().mapToDouble( Number::doubleValue ).sum();
Expand Down

0 comments on commit dc6cfb9

Please sign in to comment.