-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
PointTypeIT.java
200 lines (172 loc) · 7.5 KB
/
PointTypeIT.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* Copyright (c) 2002-2018 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.server.rest.transactional.integration;
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.values.storable.CoordinateReferenceSystem.Cartesian;
import static org.neo4j.values.storable.CoordinateReferenceSystem.WGS84;
import static org.neo4j.values.storable.Values.pointValue;
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
{
testPoint( "RETURN point({x: 42.05, y: 90.99})", new double[]{42.05, 90.99}, Cartesian, "point" );
}
@Test
public void shouldReturnPoint2DWithLatitudeAndLongitude() throws Exception
{
testPoint( "RETURN point({longitude: 56.7, latitude: 12.78})", new double[]{56.7, 12.78}, WGS84, "point" );
}
@Test
public void shouldHandlePointArrays() throws Exception
{
//Given
GraphDatabaseFacade db = server().getDatabase().getGraph();
try ( Transaction tx = db.beginTx() )
{
Node node = db.createNode( label( "N" ) );
node.setProperty( "coordinates", new Point[]{pointValue( WGS84, 30.655691, 104.081602 )} );
node.setProperty( "location", "Shanghai" );
node.setProperty( "type", "gps" );
tx.success();
}
//When
HTTP.Response response = runQuery( "MATCH (n:N) RETURN n" );
assertEquals( 200, response.status() );
assertNoErrors( response );
//Then
JsonNode row = response.get( "results" ).get( 0 ).get( "data" ).get( 0 ).get( "row" ).get( 0 )
.get( "coordinates" ).get( 0 );
assertGeometryTypeEqual( GeometryType.GEOMETRY_POINT, row );
assertCoordinatesEqual( new double[]{30.655691, 104.081602}, row );
assertCrsEqual( WGS84, row );
}
@Test
public void shouldHandlePointsUsingRestResultDataContent() throws Exception
{
//Given
GraphDatabaseFacade db = server().getDatabase().getGraph();
try ( Transaction tx = db.beginTx() )
{
Node node = db.createNode( label( "N" ) );
node.setProperty( "coordinates", pointValue( WGS84, 30.655691, 104.081602 ) );
node.setProperty( "location", "Shanghai" );
node.setProperty( "type", "gps" );
tx.success();
}
//When
HTTP.Response response = runQuery( "MATCH (n:N) RETURN n", "rest" );
assertEquals( 200, response.status() );
assertNoErrors( response );
//Then
JsonNode row = response.get( "results" ).get( 0 ).get( "data" ).get( 0 ).get( "rest" )
.get( 0 ).get( "data" ).get( "coordinates" );
assertGeometryTypeEqual( GeometryType.GEOMETRY_POINT, row );
assertCoordinatesEqual( new double[]{30.655691, 104.081602}, row );
assertCrsEqual( WGS84, row );
}
private static void testPoint( String query, double[] expectedCoordinate, CoordinateReferenceSystem expectedCrs, String expectedType ) throws Exception
{
HTTP.Response response = runQuery( query );
assertEquals( 200, response.status() );
assertNoErrors( response );
JsonNode element = extractSingleElement( response );
assertGeometryTypeEqual( GeometryType.GEOMETRY_POINT, element );
assertCoordinatesEqual( expectedCoordinate, element );
assertCrsEqual( expectedCrs, element );
assertTypeEqual( expectedType, response );
}
private static void assertTypeEqual( String expectedType, HTTP.Response response ) throws JsonParseException
{
JsonNode data = response.get( "results" ).get( 0 ).get( "data" );
JsonNode meta = data.get( 0 ).get( "meta" );
assertEquals( 1, meta.size() );
assertEquals( expectedType, meta.get( 0 ).get( "type" ).asText() );
}
private static JsonNode extractSingleElement( HTTP.Response response ) throws JsonParseException
{
JsonNode data = response.get( "results" ).get( 0 ).get( "data" );
assertEquals( 1, data.size() );
JsonNode row = data.get( 0 ).get( "row" );
assertEquals( 1, row.size() );
return row.get( 0 );
}
private static void assertGeometryTypeEqual( GeometryType expected, JsonNode element )
{
assertEquals( expected.getName(), element.get( "type" ).asText() );
}
private static void assertCoordinatesEqual( double[] expected, JsonNode element )
{
assertArrayEquals( expected, coordinatesAsArray( element ), 0.000001 );
}
private static double[] coordinatesAsArray( JsonNode element )
{
return Iterables.stream( element.get( "coordinates" ) )
.mapToDouble( JsonNode::asDouble )
.toArray();
}
private static void assertCrsEqual( CoordinateReferenceSystem crs, JsonNode element )
{
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() );
}
}