Skip to content

Commit

Permalink
Remove support for byte[] default and Cypher literals to byte[]
Browse files Browse the repository at this point in the history
  • Loading branch information
craigtaverner committed Apr 4, 2018
1 parent 3728aed commit 4b40547
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 101 deletions.
Expand Up @@ -63,15 +63,9 @@ public Object map( Object input )
for ( int a = 0; a < bytes.length; a++ ) for ( int a = 0; a < bytes.length; a++ )
{ {
Object value = list.get( a ); Object value = list.get( a );
// We do not check for Number here in order to rule out floats if ( value instanceof Byte )
if ( value instanceof Long || value instanceof Integer || value instanceof Byte )
{ {
Number number = (Number) value; bytes[a] = (Byte) value;
bytes[a] = ((Number) value).byteValue();
if ( ((Byte) bytes[a]).longValue() != number.longValue() )
{
throw new IllegalArgumentException( "Cannot convert " + value + " to byte for input to procedure - valid values are -128 to +127" );
}
} }
else else
{ {
Expand Down
113 changes: 20 additions & 93 deletions integrationtests/src/test/java/org/neo4j/procedure/ProcedureIT.java
Expand Up @@ -405,29 +405,14 @@ public void shouldCallProcedureWithGenericListWithDefault() throws Throwable
} }
} }


@Test
public void shouldCallProcedureWithByteArrayWithDefault() throws Throwable
{
// Given
try ( Transaction ignore = db.beginTx() )
{
// When
Result res = db.execute( "CALL org.neo4j.procedure.incrBytesWithDefault" );

// Then
assertThat( res.columnAs( "bytes" ).next(), equalTo( new byte[]{4, 3, 2} ) );
assertFalse( res.hasNext() );
}
}

@Test @Test
public void shouldCallProcedureWithByteArrayWithParameter() throws Throwable public void shouldCallProcedureWithByteArrayWithParameter() throws Throwable
{ {
// Given // Given
try ( Transaction ignore = db.beginTx() ) try ( Transaction ignore = db.beginTx() )
{ {
// When // When
Result res = db.execute( "CALL org.neo4j.procedure.incrBytesWithDefault($param)", map( "param", new byte[]{4, 5, 6} ) ); Result res = db.execute( "CALL org.neo4j.procedure.incrBytes($param)", map( "param", new byte[]{4, 5, 6} ) );


// Then // Then
assertThat( res.columnAs( "bytes" ).next(), equalTo( new byte[]{5, 6, 7} ) ); assertThat( res.columnAs( "bytes" ).next(), equalTo( new byte[]{5, 6, 7} ) );
Expand All @@ -442,7 +427,7 @@ public void shouldCallProcedureWithByteArrayWithParameterAndYield() throws Throw
try ( Transaction ignore = db.beginTx() ) try ( Transaction ignore = db.beginTx() )
{ {
// When // When
Result res = db.execute( "CALL org.neo4j.procedure.incrBytesWithDefault([7,8,9]) YIELD bytes RETURN bytes" ); Result res = db.execute( "WITH $param AS b CALL org.neo4j.procedure.incrBytes(b) YIELD bytes RETURN bytes", map( "param", new byte[]{7, 8, 9} ) );


// Then // Then
assertThat( res.columnAs( "bytes" ).next(), equalTo( new byte[]{8, 9, 10} ) ); assertThat( res.columnAs( "bytes" ).next(), equalTo( new byte[]{8, 9, 10} ) );
Expand All @@ -457,29 +442,29 @@ public void shouldCallProcedureWithByteArrayWithParameterAndYieldAndParameterReu
try ( Transaction ignore = db.beginTx() ) try ( Transaction ignore = db.beginTx() )
{ {
// When // When
Result res = db.execute( "WITH [10,11,12] as param CALL org.neo4j.procedure.incrBytesWithDefault(param) YIELD bytes RETURN bytes, param" ); Result res = db.execute( "WITH $param AS param CALL org.neo4j.procedure.incrBytes(param) YIELD bytes RETURN bytes, param",
map( "param", new byte[]{10, 11, 12} ) );


// Then // Then
assertTrue(res.hasNext()); assertTrue(res.hasNext());
Map<String,Object> results = res.next(); Map<String,Object> results = res.next();
assertFalse( res.hasNext() ); assertFalse( res.hasNext() );
assertThat( results.get( "bytes" ), equalTo( new byte[]{11, 12, 13} ) ); assertThat( results.get( "bytes" ), equalTo( new byte[]{11, 12, 13} ) );
assertThat( results.get( "param" ), equalTo( asList( 10L, 11L, 12L ) ) ); assertThat( results.get( "param" ), equalTo( new byte[]{10, 11, 12} ) );
} }
} }


@Test @Test
public void shouldNotAllowNonByteValuesInImplicitByteArrayConversionInProcedures() throws Throwable public void shouldNotBeAbleCallWithCypherLiteralInByteArrayProcedure() throws Throwable
{ {
//Expect //Expect
exception.expect( QueryExecutionException.class ); exception.expect( QueryExecutionException.class );
exception.expectMessage( "Cannot convert 500 to byte for input to procedure" ); exception.expectMessage( "Cannot convert 1 to byte for input to procedure" );


// When // When
try ( Transaction ignore = db.beginTx() ) try ( Transaction ignore = db.beginTx() )
{ {
//Make sure argument here is not auto parameterized away as that will drop all type information on the floor Result result = db.execute( "CALL org.neo4j.procedure.incrBytes([1,2,3])" );
Result result = db.execute( "CALL org.neo4j.procedure.incrBytesWithDefault([1,2,500])" );
result.next(); result.next();
} }
} }
Expand Down Expand Up @@ -1356,66 +1341,36 @@ public void shouldShowDescriptionWhenListingFunctions() throws Throwable
assertThat( results.get( 0 ).get( "description" ), equalTo( "This is a description" ) ); assertThat( results.get( 0 ).get( "description" ), equalTo( "This is a description" ) );
} }


@Test
public void shouldCallFunctionWithByteArrayWithDefault() throws Throwable
{
// Given
try ( Transaction ignore = db.beginTx() )
{
// When
Result res = db.execute( "RETURN org.neo4j.procedure.decrBytesWithDefault() AS bytes" );

// Then
assertThat( res.columnAs( "bytes" ).next(), equalTo( new byte[]{2, 1, 0} ) );
assertFalse( res.hasNext() );
}
}

@Test @Test
public void shouldCallFunctionWithByteArrayWithParameter() throws Throwable public void shouldCallFunctionWithByteArrayWithParameter() throws Throwable
{ {
// Given // Given
try ( Transaction ignore = db.beginTx() ) try ( Transaction ignore = db.beginTx() )
{ {
// When // When
Result res = db.execute( "RETURN org.neo4j.procedure.decrBytesWithDefault($param) AS bytes", map( "param", new byte[]{4, 5, 6} ) ); Result res = db.execute( "RETURN org.neo4j.procedure.decrBytes($param) AS bytes", map( "param", new byte[]{4, 5, 6} ) );


// Then // Then
assertThat( res.columnAs( "bytes" ).next(), equalTo( new byte[]{3, 4, 5} ) ); assertThat( res.columnAs( "bytes" ).next(), equalTo( new byte[]{3, 4, 5} ) );
assertFalse( res.hasNext() ); assertFalse( res.hasNext() );
} }
} }


@Test
public void shouldCallFunctionWithByteArrayWithLiteral() throws Throwable
{
// Given
try ( Transaction ignore = db.beginTx() )
{
// When
Result res = db.execute( "RETURN org.neo4j.procedure.decrBytesWithDefault([7,8,9]) AS bytes" );

// Then
assertThat( res.columnAs( "bytes" ).next(), equalTo( new byte[]{6, 7, 8} ) );
assertFalse( res.hasNext() );
}
}

@Test @Test
public void shouldCallFuctionWithByteArrayWithBoundLiteral() throws Throwable public void shouldCallFuctionWithByteArrayWithBoundLiteral() throws Throwable
{ {
// Given // Given
try ( Transaction ignore = db.beginTx() ) try ( Transaction ignore = db.beginTx() )
{ {
// When // When
Result res = db.execute( "WITH [10,11,12] as param RETURN org.neo4j.procedure.decrBytesWithDefault(param) AS bytes, param" ); Result res = db.execute( "WITH $param as param RETURN org.neo4j.procedure.decrBytes(param) AS bytes, param", map( "param", new byte[]{10, 11, 12} ) );


// Then // Then
assertTrue(res.hasNext()); assertTrue(res.hasNext());
Map<String,Object> results = res.next(); Map<String,Object> results = res.next();
assertFalse( res.hasNext() ); assertFalse( res.hasNext() );
assertThat( results.get( "bytes" ), equalTo( new byte[]{9, 10, 11} ) ); assertThat( results.get( "bytes" ), equalTo( new byte[]{9, 10, 11} ) );
assertThat( results.get( "param" ), equalTo( asList( 10L, 11L, 12L ) ) ); assertThat( results.get( "param" ), equalTo( new byte[]{10, 11, 12} ) );
} }
} }


Expand All @@ -1424,45 +1379,13 @@ public void shouldNotAllowNonByteValuesInImplicitByteArrayConversionWithUserDefi
{ {
//Expect //Expect
exception.expect( QueryExecutionException.class ); exception.expect( QueryExecutionException.class );
exception.expectMessage( "Cannot convert 500 to byte for input to procedure" ); exception.expectMessage( "Cannot convert 1 to byte for input to procedure" );

// When
try ( Transaction ignore = db.beginTx() )
{
//Make sure argument here is not auto parameterized away as that will drop all type information on the floor
Result result = db.execute( "RETURN org.neo4j.procedure.decrBytesWithDefault([1,2,500]) AS bytes" );
result.next();
}
}

@Test
public void shouldNotAllowNonIntegerValuesInImplicitByteArrayConversionWithUserDefinedFunction() throws Throwable
{
//Expect
exception.expect( QueryExecutionException.class );
exception.expectMessage( "Cannot convert 3.4 to byte for input to procedure" );

// When
try ( Transaction ignore = db.beginTx() )
{
//Make sure argument here is not auto parameterized away as that will drop all type information on the floor
Result result = db.execute( "RETURN org.neo4j.procedure.decrBytesWithDefault([1,2,3.4]) AS bytes" );
result.next();
}
}

@Test
public void shouldNotAllowNonNumberValuesInImplicitByteArrayConversionWithUserDefinedFunction() throws Throwable
{
//Expect
exception.expect( QueryExecutionException.class );
exception.expectMessage( "Cannot convert X to byte for input to procedure" );


// When // When
try ( Transaction ignore = db.beginTx() ) try ( Transaction ignore = db.beginTx() )
{ {
//Make sure argument here is not auto parameterized away as that will drop all type information on the floor //Make sure argument here is not auto parameterized away as that will drop all type information on the floor
Result result = db.execute( "RETURN org.neo4j.procedure.decrBytesWithDefault([1,2,'X']) AS bytes" ); Result result = db.execute( "RETURN org.neo4j.procedure.decrBytes([1,2,5]) AS bytes" );
result.next(); result.next();
} }
} }
Expand All @@ -1474,7 +1397,11 @@ public void shouldCallAggregationFunctionWithByteArrays() throws Throwable
try ( Transaction ignore = db.beginTx() ) try ( Transaction ignore = db.beginTx() )
{ {
// When // When
Result res = db.execute( "UNWIND [[1,2,3],[3,2,1],[1,2,1]] AS bytes RETURN org.neo4j.procedure.aggregateByteArrays(bytes) AS bytes" ); byte[][] data = new byte[3][];
data[0] = new byte[]{1, 2, 3};
data[1] = new byte[]{3, 2, 1};
data[2] = new byte[]{1, 2, 1};
Result res = db.execute( "UNWIND $data AS bytes RETURN org.neo4j.procedure.aggregateByteArrays(bytes) AS bytes", map( "data", data ) );


// Then // Then
assertThat( res.columnAs( "bytes" ).next(), equalTo( new byte[]{5, 6, 5} ) ); assertThat( res.columnAs( "bytes" ).next(), equalTo( new byte[]{5, 6, 5} ) );
Expand Down Expand Up @@ -1761,7 +1688,7 @@ public Stream<ListOutput> genericListWithDefault( @Name( value = "list", default
} }


@Procedure @Procedure
public Stream<BytesOutput> incrBytesWithDefault( @Name( value = "bytes", defaultValue = "[3, 2, 1]" ) byte[] bytes ) public Stream<BytesOutput> incrBytes( @Name( value = "bytes" ) byte[] bytes )
{ {
for ( int i = 0; i < bytes.length; i++ ) for ( int i = 0; i < bytes.length; i++ )
{ {
Expand Down Expand Up @@ -2051,7 +1978,7 @@ public long functionWithDescription()
} }


@UserFunction @UserFunction
public byte[] decrBytesWithDefault( @Name( value = "bytes", defaultValue = "[3, 2, 1]" ) byte[] bytes ) public byte[] decrBytes( @Name( value = "bytes" ) byte[] bytes )
{ {
for ( int i = 0; i < bytes.length; i++ ) for ( int i = 0; i < bytes.length; i++ )
{ {
Expand Down

0 comments on commit 4b40547

Please sign in to comment.