-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Neo4jPackV2Test.java
203 lines (177 loc) · 6.68 KB
/
Neo4jPackV2Test.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
201
202
203
/*
* Copyright (c) 2002-2018 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.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.bolt.v2.messaging;
import org.junit.Test;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.IntStream;
import org.neo4j.bolt.v1.messaging.Neo4jPack;
import org.neo4j.bolt.v1.packstream.PackedInputArray;
import org.neo4j.bolt.v1.packstream.PackedOutputArray;
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;
import static org.junit.Assert.fail;
import static org.neo4j.values.storable.CoordinateReferenceSystem.Cartesian;
import static org.neo4j.values.storable.CoordinateReferenceSystem.WGS84;
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
{
@Test
public void shouldFailToPackPointWithIllegalDimensions() throws IOException
{
testPackingPointsWithWrongDimensions( 0 );
testPackingPointsWithWrongDimensions( 1 );
testPackingPointsWithWrongDimensions( 4 );
testPackingPointsWithWrongDimensions( 100 );
}
@Test
public void shouldFailToUnpack2DPointWithIncorrectCoordinate() throws IOException
{
Neo4jPackV2 neo4jPack = new Neo4jPackV2();
PackedOutputArray output = new PackedOutputArray();
Neo4jPack.Packer packer = neo4jPack.newPacker( output );
packer.packStructHeader( 3, Neo4jPackV2.POINT_2D );
packer.pack( intValue( WGS84.getCode() ) );
packer.pack( doubleValue( 42.42 ) );
try
{
unpack( output );
fail( "Exception expected" );
}
catch ( IOException ignore )
{
}
}
@Test
public void shouldFailToUnpack3DPointWithIncorrectCoordinate() throws IOException
{
Neo4jPackV2 neo4jPack = new Neo4jPackV2();
PackedOutputArray output = new PackedOutputArray();
Neo4jPack.Packer packer = neo4jPack.newPacker( output );
packer.packStructHeader( 4, Neo4jPackV2.POINT_3D );
packer.pack( intValue( Cartesian.getCode() ) );
packer.pack( doubleValue( 1.0 ) );
packer.pack( doubleValue( 100.1 ) );
try
{
unpack( output );
fail( "Exception expected" );
}
catch ( IOException ignore )
{
}
}
@Test
public void shouldPackAndUnpack2DPoints() throws IOException
{
testPackingAndUnpackingOfPoints( 2 );
}
@Test
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 )
.mapToObj( index -> randomPoint( index, dimension ) )
.collect( toList() );
for ( PointValue original : points )
{
PointValue unpacked = packAndUnpack( original );
assertEquals( "Failed on " + original, original, unpacked );
}
}
private static void testPackingPointsWithWrongDimensions( int dimensions ) throws IOException
{
PointValue point = randomPoint( 0, dimensions );
try
{
pack( point );
fail( "Exception expected" );
}
catch ( IllegalArgumentException ignore )
{
}
}
private static <T extends AnyValue> T packAndUnpack( T value ) throws IOException
{
return unpack( pack( value ) );
}
private static PackedOutputArray pack( AnyValue value ) throws IOException
{
Neo4jPackV2 neo4jPack = new Neo4jPackV2();
PackedOutputArray output = new PackedOutputArray();
Neo4jPack.Packer packer = neo4jPack.newPacker( output );
packer.pack( value );
return output;
}
@SuppressWarnings( "unchecked" )
private static <T extends AnyValue> T unpack( PackedOutputArray output ) throws IOException
{
Neo4jPackV2 neo4jPack = new Neo4jPackV2();
PackedInputArray input = new PackedInputArray( output.bytes() );
Neo4jPack.Unpacker unpacker = neo4jPack.newUnpacker( input );
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 )
{
CoordinateReferenceSystem crs = index % 2 == 0 ? WGS84 : Cartesian;
return pointValue( crs, ThreadLocalRandom.current().doubles( dimension, Double.MIN_VALUE, Double.MAX_VALUE ).toArray() );
}
}