-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
ConnectedStateIT.java
112 lines (98 loc) · 4.67 KB
/
ConnectedStateIT.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
/*
* 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.v3.runtime.integration;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import java.util.stream.Stream;
import org.neo4j.bolt.messaging.BoltIOException;
import org.neo4j.bolt.messaging.RequestMessage;
import org.neo4j.bolt.testing.BoltResponseRecorder;
import org.neo4j.bolt.testing.RecordedBoltResponse;
import org.neo4j.bolt.v1.messaging.request.DiscardAllMessage;
import org.neo4j.bolt.v1.messaging.request.InitMessage;
import org.neo4j.bolt.v1.messaging.request.PullAllMessage;
import org.neo4j.bolt.v1.messaging.request.ResetMessage;
import org.neo4j.bolt.v1.runtime.ConnectedState;
import org.neo4j.bolt.v3.BoltStateMachineV3;
import org.neo4j.bolt.v3.messaging.request.BeginMessage;
import org.neo4j.bolt.v3.messaging.request.RunMessage;
import org.neo4j.bolt.v3.runtime.ReadyState;
import org.neo4j.kernel.api.exceptions.Status;
import org.neo4j.kernel.internal.Version;
import static java.util.Collections.emptyMap;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.neo4j.bolt.testing.BoltMatchers.failedWithStatus;
import static org.neo4j.bolt.testing.BoltMatchers.succeededWithMetadata;
import static org.neo4j.bolt.testing.BoltMatchers.verifyKillsConnection;
import static org.neo4j.bolt.v1.runtime.BoltStateMachineV1SPI.BOLT_SERVER_VERSION_PREFIX;
import static org.neo4j.bolt.v3.messaging.request.CommitMessage.COMMIT_MESSAGE;
import static org.neo4j.bolt.v3.messaging.request.RollbackMessage.ROLLBACK_MESSAGE;
class ConnectedStateIT extends BoltStateMachineStateTestBase
{
@Test
void shouldHandleHelloMessage() throws Throwable
{
// Given
BoltStateMachineV3 machine = newStateMachine();
BoltResponseRecorder recorder = new BoltResponseRecorder();
// When
machine.process( newHelloMessage(), recorder );
// Then
RecordedBoltResponse response = recorder.nextResponse();
assertThat( response, succeededWithMetadata( "server", BOLT_SERVER_VERSION_PREFIX + Version.getNeo4jVersion() ) );
assertThat( response, succeededWithMetadata( "routing_table", "dbms.cluster.routing.getRoutingTable" ) );
assertThat( response, succeededWithMetadata( "connection_id", "conn-v3-test-boltchannel-id" ) );
assertThat( machine.state(), instanceOf( ReadyState.class ) );
}
@ParameterizedTest
@MethodSource( "illegalV3Messages" )
void shouldCloseConnectionOnIllegalV3Messages( RequestMessage message ) throws Throwable
{
shouldCloseConnectionOnIllegalMessages( message );
}
@ParameterizedTest
@MethodSource( "illegalV2Messages" )
void shouldCloseConnectionOnIllegalV2Messages( RequestMessage message ) throws Throwable
{
shouldCloseConnectionOnIllegalMessages( message );
}
private void shouldCloseConnectionOnIllegalMessages( RequestMessage message ) throws InterruptedException
{
// Given
BoltStateMachineV3 machine = newStateMachine();
// when
BoltResponseRecorder recorder = new BoltResponseRecorder();
verifyKillsConnection( () -> machine.process( message, recorder ) );
// then
assertThat( recorder.nextResponse(), failedWithStatus( Status.Request.Invalid ) );
assertThat( machine.state(), instanceOf( ConnectedState.class ) );
}
private static Stream<RequestMessage> illegalV3Messages() throws BoltIOException
{
return Stream.of( new RunMessage( "RETURN 1", EMPTY_PARAMS, EMPTY_PARAMS ), DiscardAllMessage.INSTANCE, PullAllMessage.INSTANCE, new BeginMessage(),
COMMIT_MESSAGE, ROLLBACK_MESSAGE, ResetMessage.INSTANCE );
}
private static Stream<RequestMessage> illegalV2Messages()
{
return Stream.of( new org.neo4j.bolt.v1.messaging.request.RunMessage( "RETURN 1", EMPTY_PARAMS ), new InitMessage( USER_AGENT, emptyMap() ) );
}
}