diff --git a/community/bolt/src/test/java/org/neo4j/bolt/v2/messaging/Neo4jPackV2Test.java b/community/bolt/src/test/java/org/neo4j/bolt/v2/messaging/Neo4jPackV2Test.java index 19a99b38ca768..07f63eaab5b24 100644 --- a/community/bolt/src/test/java/org/neo4j/bolt/v2/messaging/Neo4jPackV2Test.java +++ b/community/bolt/src/test/java/org/neo4j/bolt/v2/messaging/Neo4jPackV2Test.java @@ -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; @@ -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 { @@ -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 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 points = IntStream.range( 0, 1000 ) @@ -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 ); } } @@ -157,7 +181,18 @@ private static 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 ) diff --git a/community/server/src/test/java/org/neo4j/server/rest/transactional/integration/PointTypeIT.java b/community/server/src/test/java/org/neo4j/server/rest/transactional/integration/PointTypeIT.java index c9831aa14dec2..b53077eb3da49 100644 --- a/community/server/src/test/java/org/neo4j/server/rest/transactional/integration/PointTypeIT.java +++ b/community/server/src/test/java/org/neo4j/server/rest/transactional/integration/PointTypeIT.java @@ -22,15 +22,22 @@ 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; @@ -38,6 +45,32 @@ 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 { @@ -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 ); @@ -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() ); @@ -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() ); + } }