-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
BoltResponseMessageTest.java
266 lines (241 loc) · 14 KB
/
BoltResponseMessageTest.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
* 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.bolt.v1.messaging;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import org.neo4j.bolt.messaging.Neo4jPack;
import org.neo4j.bolt.messaging.ResponseMessage;
import org.neo4j.bolt.v1.messaging.response.FailureMessage;
import org.neo4j.bolt.v1.messaging.response.RecordMessage;
import org.neo4j.bolt.v1.messaging.response.SuccessMessage;
import org.neo4j.bolt.v1.packstream.BufferedChannelInput;
import org.neo4j.bolt.v1.packstream.BufferedChannelOutput;
import org.neo4j.kernel.api.exceptions.Status;
import org.neo4j.kernel.impl.util.HexPrinter;
import org.neo4j.kernel.impl.util.ValueUtils;
import org.neo4j.logging.internal.NullLogService;
import org.neo4j.values.AnyValue;
import org.neo4j.values.virtual.NodeValue;
import org.neo4j.values.virtual.RelationshipValue;
import org.neo4j.values.virtual.VirtualValues;
import static java.lang.System.lineSeparator;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.neo4j.bolt.v1.messaging.example.Paths.PATH_WITH_LENGTH_ONE;
import static org.neo4j.bolt.v1.messaging.example.Paths.PATH_WITH_LENGTH_TWO;
import static org.neo4j.bolt.v1.messaging.example.Paths.PATH_WITH_LENGTH_ZERO;
import static org.neo4j.bolt.v1.messaging.example.Paths.PATH_WITH_LOOP;
import static org.neo4j.bolt.v1.messaging.example.Paths.PATH_WITH_NODES_VISITED_MULTIPLE_TIMES;
import static org.neo4j.bolt.v1.messaging.example.Paths.PATH_WITH_RELATIONSHIP_TRAVERSED_AGAINST_ITS_DIRECTION;
import static org.neo4j.bolt.v1.messaging.example.Paths.PATH_WITH_RELATIONSHIP_TRAVERSED_MULTIPLE_TIMES_IN_SAME_DIRECTION;
import static org.neo4j.bolt.v1.messaging.response.IgnoredMessage.IGNORED_MESSAGE;
import static org.neo4j.bolt.v1.messaging.util.MessageMatchers.serialize;
import static org.neo4j.bolt.v1.runtime.spi.Records.record;
import static org.neo4j.helpers.collection.MapUtil.map;
import static org.neo4j.values.storable.Values.intValue;
import static org.neo4j.values.storable.Values.longValue;
import static org.neo4j.values.storable.Values.stringArray;
import static org.neo4j.values.storable.Values.stringValue;
import static org.neo4j.values.virtual.VirtualValues.nodeValue;
import static org.neo4j.values.virtual.VirtualValues.relationshipValue;
public class BoltResponseMessageTest
{
@Rule
public ExpectedException exception = ExpectedException.none();
private final Neo4jPack neo4jPack = new Neo4jPackV1();
@Test
public void shouldHandleCommonMessages() throws Throwable
{
assertSerializes( new RecordMessage( record( longValue( 1L ), stringValue( "b" ), longValue( 2L ) ) ) );
assertSerializes( new SuccessMessage( VirtualValues.EMPTY_MAP ) );
assertSerializes( new FailureMessage( Status.General.UnknownError, "Err" ) );
assertSerializes( IGNORED_MESSAGE );
}
@Test
public void shouldSerializeBasicTypes() throws Throwable
{
assertSerializesNeoValue( null );
assertSerializesNeoValue( true );
assertSerializesNeoValue( false );
assertSerializesNeoValue( Long.MAX_VALUE );
assertSerializesNeoValue( 1337L );
assertSerializesNeoValue( Long.MIN_VALUE );
assertSerializesNeoValue( Double.MIN_VALUE );
assertSerializesNeoValue( 13.37d );
assertSerializesNeoValue( Double.MAX_VALUE );
assertSerializesNeoValue( "" );
assertSerializesNeoValue( "A basic piece of text" );
assertSerializesNeoValue( new String( new byte[16000], StandardCharsets.UTF_8 ) );
assertSerializesNeoValue( emptyList() );
assertSerializesNeoValue( asList( null, null ) );
assertSerializesNeoValue( asList( true, false ) );
assertSerializesNeoValue( asList( "one", "", "three" ) );
assertSerializesNeoValue( asList( 12.4d, 0.0d ) );
assertSerializesNeoValue( map( "k", null ) );
assertSerializesNeoValue( map( "k", true ) );
assertSerializesNeoValue( map( "k", false ) );
assertSerializesNeoValue( map( "k", 1337L ) );
assertSerializesNeoValue( map( "k", 133.7d ) );
assertSerializesNeoValue( map( "k", "Hello" ) );
assertSerializesNeoValue( map( "k", asList( "one", "", "three" ) ) );
}
@Test
public void shouldSerializeNode() throws Throwable
{
NodeValue nodeValue = nodeValue( 12L, stringArray( "User", "Banana" ), VirtualValues
.map( new String[]{"name", "age"},
new AnyValue[]{stringValue( "Bob" ), intValue( 14 )} ) );
assertThat( serialized( nodeValue ),
equalTo( "B1 71 91 B3 4E 0C 92 84 55 73 65 72 86 42 61 6E" + lineSeparator() +
"61 6E 61 A2 84 6E 61 6D 65 83 42 6F 62 83 61 67" + lineSeparator() +
"65 0E" ) );
}
@Test
public void shouldSerializeRelationship() throws Throwable
{
RelationshipValue rel = relationshipValue( 12L,
nodeValue( 1L, stringArray(), VirtualValues.EMPTY_MAP ),
nodeValue( 2L, stringArray(), VirtualValues.EMPTY_MAP ),
stringValue( "KNOWS" ), VirtualValues.map( new String[]{"name", "age"},
new AnyValue[]{stringValue( "Bob" ), intValue( 14 )} ) );
assertThat( serialized( rel ),
equalTo( "B1 71 91 B5 52 0C 01 02 85 4B 4E 4F 57 53 A2 84" + lineSeparator() +
"6E 61 6D 65 83 42 6F 62 83 61 67 65 0E" ) );
}
@Test
public void shouldSerializePaths() throws Throwable
{
assertThat( serialized( PATH_WITH_LENGTH_ZERO ),
equalTo( "B1 71 91 B3 50 91 B3 4E C9 03 E9 92 86 50 65 72" + lineSeparator() +
"73 6F 6E 88 45 6D 70 6C 6F 79 65 65 A2 84 6E 61" + lineSeparator() +
"6D 65 85 41 6C 69 63 65 83 61 67 65 21 90 90" ) );
assertThat( serialized( PATH_WITH_LENGTH_ONE ),
equalTo( "B1 71 91 B3 50 92 B3 4E C9 03 E9 92 86 50 65 72" + lineSeparator() +
"73 6F 6E 88 45 6D 70 6C 6F 79 65 65 A2 84 6E 61" + lineSeparator() +
"6D 65 85 41 6C 69 63 65 83 61 67 65 21 B3 4E C9" + lineSeparator() +
"03 EA 92 86 50 65 72 73 6F 6E 88 45 6D 70 6C 6F" + lineSeparator() +
"79 65 65 A2 84 6E 61 6D 65 83 42 6F 62 83 61 67" + lineSeparator() +
"65 2C 91 B3 72 0C 85 4B 4E 4F 57 53 A1 85 73 69" + lineSeparator() +
"6E 63 65 C9 07 CF 92 01 01"
) );
assertThat( serialized( PATH_WITH_LENGTH_TWO ),
equalTo( "B1 71 91 B3 50 93 B3 4E C9 03 E9 92 86 50 65 72" + lineSeparator() +
"73 6F 6E 88 45 6D 70 6C 6F 79 65 65 A2 84 6E 61" + lineSeparator() +
"6D 65 85 41 6C 69 63 65 83 61 67 65 21 B3 4E C9" + lineSeparator() +
"03 EB 91 86 50 65 72 73 6F 6E A1 84 6E 61 6D 65" + lineSeparator() +
"85 43 61 72 6F 6C B3 4E C9 03 EC 90 A1 84 6E 61" + lineSeparator() +
"6D 65 84 44 61 76 65 92 B3 72 0D 85 4C 49 4B 45" + lineSeparator() +
"53 A0 B3 72 22 8A 4D 41 52 52 49 45 44 5F 54 4F" + lineSeparator() +
"A0 94 01 01 02 02" ) );
assertThat( serialized( PATH_WITH_RELATIONSHIP_TRAVERSED_AGAINST_ITS_DIRECTION ),
equalTo( "B1 71 91 B3 50 94 B3 4E C9 03 E9 92 86 50 65 72" + lineSeparator() +
"73 6F 6E 88 45 6D 70 6C 6F 79 65 65 A2 84 6E 61" + lineSeparator() +
"6D 65 85 41 6C 69 63 65 83 61 67 65 21 B3 4E C9" + lineSeparator() +
"03 EA 92 86 50 65 72 73 6F 6E 88 45 6D 70 6C 6F" + lineSeparator() +
"79 65 65 A2 84 6E 61 6D 65 83 42 6F 62 83 61 67" + lineSeparator() +
"65 2C B3 4E C9 03 EB 91 86 50 65 72 73 6F 6E A1" + lineSeparator() +
"84 6E 61 6D 65 85 43 61 72 6F 6C B3 4E C9 03 EC" + lineSeparator() +
"90 A1 84 6E 61 6D 65 84 44 61 76 65 93 B3 72 0C" + lineSeparator() +
"85 4B 4E 4F 57 53 A1 85 73 69 6E 63 65 C9 07 CF" + lineSeparator() +
"B3 72 20 88 44 49 53 4C 49 4B 45 53 A0 B3 72 22" + lineSeparator() +
"8A 4D 41 52 52 49 45 44 5F 54 4F A0 96 01 01 FE" + lineSeparator() +
"02 03 03" ) );
assertThat( serialized( PATH_WITH_NODES_VISITED_MULTIPLE_TIMES ),
equalTo( "B1 71 91 B3 50 93 B3 4E C9 03 E9 92 86 50 65 72" + lineSeparator() +
"73 6F 6E 88 45 6D 70 6C 6F 79 65 65 A2 84 6E 61" + lineSeparator() +
"6D 65 85 41 6C 69 63 65 83 61 67 65 21 B3 4E C9" + lineSeparator() +
"03 EA 92 86 50 65 72 73 6F 6E 88 45 6D 70 6C 6F" + lineSeparator() +
"79 65 65 A2 84 6E 61 6D 65 83 42 6F 62 83 61 67" + lineSeparator() +
"65 2C B3 4E C9 03 EB 91 86 50 65 72 73 6F 6E A1" + lineSeparator() +
"84 6E 61 6D 65 85 43 61 72 6F 6C 93 B3 72 0C 85" + lineSeparator() +
"4B 4E 4F 57 53 A1 85 73 69 6E 63 65 C9 07 CF B3" + lineSeparator() +
"72 0D 85 4C 49 4B 45 53 A0 B3 72 20 88 44 49 53" + lineSeparator() +
"4C 49 4B 45 53 A0 9A 01 01 FF 00 02 02 03 01 FD" + lineSeparator() +
"02" ) );
assertThat( serialized( PATH_WITH_RELATIONSHIP_TRAVERSED_MULTIPLE_TIMES_IN_SAME_DIRECTION ),
equalTo( "B1 71 91 B3 50 94 B3 4E C9 03 E9 92 86 50 65 72" + lineSeparator() +
"73 6F 6E 88 45 6D 70 6C 6F 79 65 65 A2 84 6E 61" + lineSeparator() +
"6D 65 85 41 6C 69 63 65 83 61 67 65 21 B3 4E C9" + lineSeparator() +
"03 EB 91 86 50 65 72 73 6F 6E A1 84 6E 61 6D 65" + lineSeparator() +
"85 43 61 72 6F 6C B3 4E C9 03 EA 92 86 50 65 72" + lineSeparator() +
"73 6F 6E 88 45 6D 70 6C 6F 79 65 65 A2 84 6E 61" + lineSeparator() +
"6D 65 83 42 6F 62 83 61 67 65 2C B3 4E C9 03 EC" + lineSeparator() +
"90 A1 84 6E 61 6D 65 84 44 61 76 65 94 B3 72 0D" + lineSeparator() +
"85 4C 49 4B 45 53 A0 B3 72 20 88 44 49 53 4C 49" + lineSeparator() +
"4B 45 53 A0 B3 72 0C 85 4B 4E 4F 57 53 A1 85 73" + lineSeparator() +
"69 6E 63 65 C9 07 CF B3 72 22 8A 4D 41 52 52 49" + lineSeparator() +
"45 44 5F 54 4F A0 9A 01 01 02 02 FD 00 01 01 04" + lineSeparator() +
"03" ) );
assertThat( serialized( PATH_WITH_LOOP ),
equalTo( "B1 71 91 B3 50 92 B3 4E C9 03 EB 91 86 50 65 72" + lineSeparator() +
"73 6F 6E A1 84 6E 61 6D 65 85 43 61 72 6F 6C B3" + lineSeparator() +
"4E C9 03 EC 90 A1 84 6E 61 6D 65 84 44 61 76 65" + lineSeparator() +
"92 B3 72 22 8A 4D 41 52 52 49 45 44 5F 54 4F A0" + lineSeparator() +
"B3 72 2C 89 57 4F 52 4B 53 5F 46 4F 52 A0 94 01" + lineSeparator() +
"01 02 01" ) );
}
private String serialized( AnyValue object ) throws IOException
{
RecordMessage message = new RecordMessage( record( object ) );
return HexPrinter.hex( serialize( neo4jPack, message ), 4, " " );
}
private void assertSerializes( ResponseMessage msg ) throws IOException
{
assertThat( serializeAndDeserialize( msg ), equalTo( msg ) );
}
private <T extends ResponseMessage> T serializeAndDeserialize( T msg ) throws IOException
{
RecordingByteChannel channel = new RecordingByteChannel();
BoltResponseMessageReader reader = new BoltResponseMessageReader(
neo4jPack.newUnpacker( new BufferedChannelInput( 16 ).reset( channel ) ) );
BufferedChannelOutput output = new BufferedChannelOutput( channel );
BoltResponseMessageWriterV1 writer = new BoltResponseMessageWriterV1( neo4jPack::newPacker, output, NullLogService.getInstance() );
writer.write( msg );
writer.flush();
channel.eof();
return unpack( reader, channel );
}
@SuppressWarnings( "unchecked" )
private <T extends ResponseMessage> T unpack( BoltResponseMessageReader reader, RecordingByteChannel channel )
{
// Unpack
String serialized = HexPrinter.hex( channel.getBytes() );
BoltResponseMessageRecorder messages = new BoltResponseMessageRecorder();
try
{
reader.read( messages );
}
catch ( Throwable e )
{
throw new AssertionError( "Failed to unpack message, wire data was:\n" + serialized + "[" + channel
.getBytes().length + "b]", e );
}
return (T) messages.asList().get( 0 );
}
private void assertSerializesNeoValue( Object val ) throws IOException
{
assertSerializes( new RecordMessage( record( ValueUtils.of( val ) ) ) );
}
}