Skip to content

Commit 8bc069e

Browse files
committed
more
1 parent e322812 commit 8bc069e

24 files changed

+7039
-0
lines changed
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
/*
2+
* Copyright (c) "Neo4j"
3+
* Neo4j Sweden AB [https://neo4j.com]
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.neo4j.driver.integration;
18+
19+
import static org.hamcrest.Matchers.startsWith;
20+
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
22+
import static org.junit.jupiter.api.Assertions.assertThrows;
23+
import static org.neo4j.driver.SessionConfig.builder;
24+
import static org.neo4j.driver.internal.InternalBookmark.parse;
25+
import static org.neo4j.driver.internal.util.BookmarkUtil.assertBookmarkContainsSingleValue;
26+
import static org.neo4j.driver.internal.util.BookmarkUtil.assertBookmarkIsEmpty;
27+
import static org.neo4j.driver.internal.util.BookmarkUtil.assertBookmarksContainsSingleUniqueValues;
28+
29+
import org.junit.jupiter.api.BeforeEach;
30+
import org.junit.jupiter.api.Test;
31+
import org.junit.jupiter.api.extension.RegisterExtension;
32+
import org.neo4j.driver.Driver;
33+
import org.neo4j.driver.Session;
34+
import org.neo4j.driver.exceptions.ClientException;
35+
import org.neo4j.driver.internal.util.DisabledOnNeo4jWith;
36+
import org.neo4j.driver.internal.util.EnabledOnNeo4jWith;
37+
import org.neo4j.driver.internal.util.Neo4jFeature;
38+
import org.neo4j.driver.testutil.ParallelizableIT;
39+
import org.neo4j.driver.testutil.SessionExtension;
40+
41+
@ParallelizableIT
42+
class BookmarkIT {
43+
@RegisterExtension
44+
static final SessionExtension sessionRule = new SessionExtension();
45+
46+
private Driver driver;
47+
private Session session;
48+
49+
@BeforeEach
50+
void assumeBookmarkSupport() {
51+
driver = sessionRule.driver();
52+
session = sessionRule;
53+
}
54+
55+
@Test
56+
@DisabledOnNeo4jWith(Neo4jFeature.BOLT_V4)
57+
@SuppressWarnings("deprecation")
58+
void shouldReceiveBookmarkOnSuccessfulCommit() {
59+
// Given
60+
assertBookmarkIsEmpty(session.lastBookmark());
61+
62+
// When
63+
createNodeInTx(session);
64+
65+
// Then
66+
assertBookmarkContainsSingleValue(session.lastBookmark(), startsWith("neo4j:bookmark:v1:tx"));
67+
}
68+
69+
@Test
70+
@EnabledOnNeo4jWith(Neo4jFeature.BOLT_V4)
71+
@SuppressWarnings("deprecation")
72+
void shouldReceiveNewBookmarkOnSuccessfulCommit() {
73+
// Given
74+
var initialBookmark = session.lastBookmark();
75+
assertBookmarkIsEmpty(initialBookmark);
76+
77+
// When
78+
createNodeInTx(session);
79+
80+
// Then
81+
assertBookmarkContainsSingleValue(session.lastBookmark());
82+
assertNotEquals(initialBookmark, session.lastBookmark());
83+
}
84+
85+
@Test
86+
void shouldThrowForInvalidBookmark() {
87+
var invalidBookmark = parse("hi, this is an invalid bookmark");
88+
89+
try (var session =
90+
driver.session(builder().withBookmarks(invalidBookmark).build())) {
91+
assertThrows(ClientException.class, session::beginTransaction);
92+
}
93+
}
94+
95+
@Test
96+
@SuppressWarnings("deprecation")
97+
void bookmarkRemainsAfterRolledBackTx() {
98+
assertBookmarkIsEmpty(session.lastBookmark());
99+
100+
createNodeInTx(session);
101+
102+
var bookmark = session.lastBookmark();
103+
assertBookmarkContainsSingleValue(bookmark);
104+
105+
try (var tx = session.beginTransaction()) {
106+
tx.run("CREATE (a:Person)");
107+
tx.rollback();
108+
}
109+
110+
assertEquals(bookmark, session.lastBookmark());
111+
}
112+
113+
@Test
114+
@SuppressWarnings("deprecation")
115+
void bookmarkRemainsAfterTxFailure() {
116+
assertBookmarkIsEmpty(session.lastBookmark());
117+
118+
createNodeInTx(session);
119+
120+
var bookmark = session.lastBookmark();
121+
assertBookmarkContainsSingleValue(bookmark);
122+
123+
var tx = session.beginTransaction();
124+
125+
assertThrows(ClientException.class, () -> tx.run("RETURN"));
126+
assertEquals(bookmark, session.lastBookmark());
127+
}
128+
129+
@Test
130+
@SuppressWarnings("deprecation")
131+
void bookmarkRemainsAfterSuccessfulSessionRun() {
132+
assertBookmarkIsEmpty(session.lastBookmark());
133+
134+
createNodeInTx(session);
135+
136+
var bookmark = session.lastBookmark();
137+
assertBookmarkContainsSingleValue(bookmark);
138+
139+
session.run("RETURN 1").consume();
140+
141+
assertEquals(bookmark, session.lastBookmark());
142+
}
143+
144+
@Test
145+
@SuppressWarnings("deprecation")
146+
void bookmarkRemainsAfterFailedSessionRun() {
147+
assertBookmarkIsEmpty(session.lastBookmark());
148+
149+
createNodeInTx(session);
150+
151+
var bookmark = session.lastBookmark();
152+
assertBookmarkContainsSingleValue(bookmark);
153+
154+
assertThrows(ClientException.class, () -> session.run("RETURN").consume());
155+
assertEquals(bookmark, session.lastBookmark());
156+
}
157+
158+
@Test
159+
@SuppressWarnings("deprecation")
160+
void bookmarkIsUpdatedOnEveryCommittedTx() {
161+
assertBookmarkIsEmpty(session.lastBookmark());
162+
163+
createNodeInTx(session);
164+
var bookmark1 = session.lastBookmark();
165+
assertBookmarkContainsSingleValue(bookmark1);
166+
167+
createNodeInTx(session);
168+
var bookmark2 = session.lastBookmark();
169+
assertBookmarkContainsSingleValue(bookmark2);
170+
171+
createNodeInTx(session);
172+
var bookmark3 = session.lastBookmark();
173+
assertBookmarkContainsSingleValue(bookmark3);
174+
175+
assertBookmarksContainsSingleUniqueValues(bookmark1, bookmark2, bookmark3);
176+
}
177+
178+
@Test
179+
@SuppressWarnings("deprecation")
180+
void createSessionWithInitialBookmark() {
181+
var bookmark = parse("TheBookmark");
182+
try (var session = driver.session(builder().withBookmarks(bookmark).build())) {
183+
assertEquals(bookmark, session.lastBookmark());
184+
}
185+
}
186+
187+
@Test
188+
@SuppressWarnings("deprecation")
189+
void createSessionWithAccessModeAndInitialBookmark() {
190+
var bookmark = parse("TheBookmark");
191+
try (var session = driver.session(builder().withBookmarks(bookmark).build())) {
192+
assertEquals(bookmark, session.lastBookmark());
193+
}
194+
}
195+
196+
private static void createNodeInTx(Session session) {
197+
try (var tx = session.beginTransaction()) {
198+
tx.run("CREATE (a:Person)");
199+
tx.commit();
200+
}
201+
}
202+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Copyright (c) "Neo4j"
3+
* Neo4j Sweden AB [https://neo4j.com]
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.neo4j.driver.integration;
18+
19+
import static java.util.Collections.singletonMap;
20+
import static org.hamcrest.MatcherAssert.assertThat;
21+
import static org.hamcrest.Matchers.containsString;
22+
import static org.hamcrest.Matchers.equalTo;
23+
import static org.junit.jupiter.api.Assertions.assertThrows;
24+
import static org.neo4j.driver.AuthTokens.basic;
25+
import static org.neo4j.driver.AuthTokens.custom;
26+
import static org.neo4j.driver.internal.logging.DevNullLogging.DEV_NULL_LOGGING;
27+
28+
import java.util.Map;
29+
import org.junit.jupiter.api.Test;
30+
import org.junit.jupiter.api.extension.RegisterExtension;
31+
import org.neo4j.driver.AuthTokens;
32+
import org.neo4j.driver.Config;
33+
import org.neo4j.driver.GraphDatabase;
34+
import org.neo4j.driver.exceptions.AuthenticationException;
35+
import org.neo4j.driver.exceptions.SecurityException;
36+
import org.neo4j.driver.testutil.DatabaseExtension;
37+
import org.neo4j.driver.testutil.ParallelizableIT;
38+
39+
@ParallelizableIT
40+
class CredentialsIT {
41+
@RegisterExtension
42+
static final DatabaseExtension neo4j = new DatabaseExtension();
43+
44+
@Test
45+
void basicCredentialsShouldWork() {
46+
// When & Then
47+
try (var driver = GraphDatabase.driver(neo4j.uri(), basic("neo4j", neo4j.adminPassword()));
48+
var session = driver.session()) {
49+
var single = session.run("RETURN 1").single().get(0);
50+
assertThat(single.asLong(), equalTo(1L));
51+
}
52+
}
53+
54+
@Test
55+
void shouldGetHelpfulErrorOnInvalidCredentials() {
56+
var e = assertThrows(SecurityException.class, () -> {
57+
try (var driver = GraphDatabase.driver(neo4j.uri(), basic("thisisnotthepassword", neo4j.adminPassword()));
58+
var session = driver.session()) {
59+
session.run("RETURN 1");
60+
}
61+
});
62+
assertThat(e.getMessage(), containsString("The client is unauthorized due to authentication failure."));
63+
}
64+
65+
@Test
66+
void shouldBeAbleToProvideRealmWithBasicAuth() {
67+
// When & Then
68+
try (var driver = GraphDatabase.driver(neo4j.uri(), basic("neo4j", neo4j.adminPassword(), "native"));
69+
var session = driver.session()) {
70+
var single = session.run("CREATE () RETURN 1").single().get(0);
71+
assertThat(single.asLong(), equalTo(1L));
72+
}
73+
}
74+
75+
@Test
76+
void shouldBeAbleToConnectWithCustomToken() {
77+
// When & Then
78+
try (var driver = GraphDatabase.driver(neo4j.uri(), custom("neo4j", neo4j.adminPassword(), "native", "basic"));
79+
var session = driver.session()) {
80+
var single = session.run("CREATE () RETURN 1").single().get(0);
81+
assertThat(single.asLong(), equalTo(1L));
82+
}
83+
}
84+
85+
@Test
86+
void shouldBeAbleToConnectWithCustomTokenWithAdditionalParameters() {
87+
Map<String, Object> params = singletonMap("secret", 16);
88+
89+
// When & Then
90+
try (var driver = GraphDatabase.driver(
91+
neo4j.uri(), custom("neo4j", neo4j.adminPassword(), "native", "basic", params));
92+
var session = driver.session()) {
93+
var single = session.run("CREATE () RETURN 1").single().get(0);
94+
assertThat(single.asLong(), equalTo(1L));
95+
}
96+
}
97+
98+
@Test
99+
void directDriverShouldFailEarlyOnWrongCredentials() {
100+
testDriverFailureOnWrongCredentials(neo4j.uri().toString());
101+
}
102+
103+
@Test
104+
void routingDriverShouldFailEarlyOnWrongCredentials() {
105+
testDriverFailureOnWrongCredentials(neo4j.uri().toString());
106+
}
107+
108+
private void testDriverFailureOnWrongCredentials(String uri) {
109+
var config = Config.builder().withLogging(DEV_NULL_LOGGING).build();
110+
var authToken = AuthTokens.basic("neo4j", "wrongSecret");
111+
112+
@SuppressWarnings("resource")
113+
final var driver = GraphDatabase.driver(uri, authToken, config);
114+
assertThrows(AuthenticationException.class, driver::verifyConnectivity);
115+
}
116+
}

0 commit comments

Comments
 (0)