Skip to content

Commit

Permalink
Added tests for point arrays in Bolt and REST
Browse files Browse the repository at this point in the history
  • Loading branch information
lutovich committed Feb 21, 2018
1 parent 90a9a61 commit bd2c9b3
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 8 deletions.
Expand Up @@ -32,6 +32,7 @@
import org.neo4j.values.AnyValue;
import org.neo4j.values.storable.CoordinateReferenceSystem;
import org.neo4j.values.storable.PointValue;
import org.neo4j.values.virtual.ListValue;

import static java.util.stream.Collectors.toList;
import static org.junit.Assert.assertEquals;
Expand All @@ -41,6 +42,7 @@
import static org.neo4j.values.storable.Values.doubleValue;
import static org.neo4j.values.storable.Values.intValue;
import static org.neo4j.values.storable.Values.pointValue;
import static org.neo4j.values.virtual.VirtualValues.list;

public class Neo4jPackV2Test
{
Expand Down Expand Up @@ -108,6 +110,31 @@ public void shouldPackAndUnpack3DPoints() throws IOException
testPackingAndUnpackingOfPoints( 3 );
}

@Test
public void shouldPackAndUnpackListsOf2DPoints() throws IOException
{
testPackingAndUnpackingOfListsOfPoints( 2 );
}

@Test
public void shouldPackAndUnpackListsOf3DPoints() throws IOException
{
testPackingAndUnpackingOfListsOfPoints( 3 );
}

private static void testPackingAndUnpackingOfListsOfPoints( int pointDimension ) throws IOException
{
List<ListValue> pointLists = IntStream.range( 0, 1000 )
.mapToObj( index -> randomListOfPoints( index, pointDimension ) )
.collect( toList() );

for ( ListValue original : pointLists )
{
ListValue unpacked = packAndUnpack( original );
assertEquals( "Failed on " + original, original, unpacked );
}
}

private static void testPackingAndUnpackingOfPoints( int dimension ) throws IOException
{
List<PointValue> points = IntStream.range( 0, 1000 )
Expand All @@ -117,10 +144,7 @@ private static void testPackingAndUnpackingOfPoints( int dimension ) throws IOEx
for ( PointValue original : points )
{
PointValue unpacked = packAndUnpack( original );

String message = "Failed on " + original;
assertEquals( message, original.getCoordinateReferenceSystem(), unpacked.getCoordinateReferenceSystem() );
assertEquals( message, original.getCoordinate(), unpacked.getCoordinate() );
assertEquals( "Failed on " + original, original, unpacked );
}
}

Expand Down Expand Up @@ -157,7 +181,18 @@ private static <T extends AnyValue> T unpack( PackedOutputArray output ) throws
Neo4jPackV2 neo4jPack = new Neo4jPackV2();
PackedInputArray input = new PackedInputArray( output.bytes() );
Neo4jPack.Unpacker unpacker = neo4jPack.newUnpacker( input );
return (T) unpacker.unpack();
AnyValue unpack = unpacker.unpack();
return (T) unpack;
}

private static ListValue randomListOfPoints( int index, int pointDimension )
{
PointValue[] pointValues = ThreadLocalRandom.current()
.ints( 100, 1, 100 )
.mapToObj( i -> randomPoint( index, pointDimension ) )
.toArray( PointValue[]::new );

return list( pointValues );
}

private static PointValue randomPoint( int index, int dimension )
Expand Down
Expand Up @@ -22,22 +22,55 @@
import org.codehaus.jackson.JsonNode;
import org.junit.Test;

import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.spatial.CRS;
import org.neo4j.graphdb.spatial.Point;
import org.neo4j.helpers.collection.Iterables;
import org.neo4j.kernel.impl.factory.GraphDatabaseFacade;
import org.neo4j.kernel.impl.store.GeometryType;
import org.neo4j.server.rest.AbstractRestFunctionalTestBase;
import org.neo4j.server.rest.domain.JsonParseException;
import org.neo4j.test.server.HTTP;
import org.neo4j.values.storable.CoordinateReferenceSystem;

import static java.util.Arrays.asList;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.neo4j.graphdb.Label.label;
import static org.neo4j.test.server.HTTP.POST;
import static org.neo4j.test.server.HTTP.RawPayload.quotedJson;
import static org.neo4j.values.storable.CoordinateReferenceSystem.Cartesian;
import static org.neo4j.values.storable.CoordinateReferenceSystem.WGS84;

public class PointTypeIT extends AbstractRestFunctionalTestBase
{
@Test
public void shouldWorkWithPoint2DArrays() throws Exception
{
HTTP.Response response = runQuery( "create (:Node {points: [point({x:1, y:1}), point({x:2, y:2}), point({x: 3.0, y: 3.0})]})" );

assertEquals( 200, response.status() );
assertNoErrors( response );

GraphDatabaseFacade db = server().getDatabase().getGraph();
try ( Transaction tx = db.beginTx() )
{
for ( Node node : db.getAllNodes() )
{
if ( node.hasLabel( label( "Node" ) ) && node.hasProperty( "points" ) )
{
Point[] points = (Point[]) node.getProperty( "points" );

verifyPoint( points[0], Cartesian, 1.0, 1.0 );
verifyPoint( points[1], Cartesian, 2.0, 2.0 );
verifyPoint( points[2], Cartesian, 3.0, 3.0 );
}
}
tx.success();
}
}

@Test
public void shouldReturnPoint2DWithXAndY() throws Exception
{
Expand All @@ -52,9 +85,7 @@ public void shouldReturnPoint2DWithLatitudeAndLongitude() throws Exception

private static void testPoint( String query, double[] expectedCoordinate, CoordinateReferenceSystem expectedCrs ) throws Exception
{
HTTP.Response response = POST( txCommitUri(), quotedJson( "{'statements': [{'statement': '" + query + "'}]}" ) );

System.out.println( response.rawContent() );
HTTP.Response response = runQuery( query );

assertEquals( 200, response.status() );
assertNoErrors( response );
Expand All @@ -65,6 +96,11 @@ private static void testPoint( String query, double[] expectedCoordinate, Coordi
assertCrsEqual( expectedCrs, element );
}

private static HTTP.Response runQuery( String query )
{
return POST( txCommitUri(), quotedJson( "{'statements': [{'statement': '" + query + "'}]}" ) );
}

private static void assertNoErrors( HTTP.Response response ) throws JsonParseException
{
assertEquals( 0, response.get( "errors" ).size() );
Expand Down Expand Up @@ -100,4 +136,10 @@ private static void assertCrsEqual( CoordinateReferenceSystem crs, JsonNode elem
{
assertEquals( crs.getName(), element.get( "crs" ).get( "name" ).asText() );
}

private static void verifyPoint( Point point, CRS expectedCRS, Double... expectedCoordinate )
{
assertEquals( expectedCRS.getCode(), point.getCRS().getCode() );
assertEquals( asList( expectedCoordinate ), point.getCoordinate().getCoordinate() );
}
}

0 comments on commit bd2c9b3

Please sign in to comment.