Skip to content

Commit 86d9d36

Browse files
committed
More tests for transaction timeout and metadata
1 parent 720aa93 commit 86d9d36

File tree

7 files changed

+760
-39
lines changed

7 files changed

+760
-39
lines changed

driver/src/test/java/org/neo4j/driver/internal/ExtractTest.java

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.junit.jupiter.api.Test;
2222

23+
import java.time.LocalDate;
2324
import java.util.Arrays;
2425
import java.util.Collection;
2526
import java.util.Collections;
@@ -31,16 +32,19 @@
3132
import org.neo4j.driver.internal.util.Extract;
3233
import org.neo4j.driver.internal.util.Iterables;
3334
import org.neo4j.driver.v1.Value;
34-
import org.neo4j.driver.v1.util.Function;
35+
import org.neo4j.driver.v1.exceptions.ClientException;
3536
import org.neo4j.driver.v1.util.Pair;
3637

3738
import static java.util.Arrays.asList;
39+
import static java.util.Collections.emptyMap;
3840
import static java.util.Collections.singletonList;
41+
import static java.util.Collections.singletonMap;
3942
import static org.hamcrest.CoreMatchers.equalTo;
4043
import static org.hamcrest.Matchers.contains;
4144
import static org.hamcrest.Matchers.containsInAnyOrder;
4245
import static org.hamcrest.Matchers.empty;
4346
import static org.hamcrest.junit.MatcherAssert.assertThat;
47+
import static org.junit.jupiter.api.Assertions.assertEquals;
4448
import static org.junit.jupiter.api.Assertions.assertFalse;
4549
import static org.junit.jupiter.api.Assertions.assertThrows;
4650
import static org.neo4j.driver.v1.Values.value;
@@ -77,7 +81,7 @@ void extractMultipleShouldNotBeModifiable()
7781
@Test
7882
void testMapOverList()
7983
{
80-
List<Integer> mapped = Extract.list( new Value[]{value( 42 ), value( 43 )}, integerExtractor() );
84+
List<Integer> mapped = Extract.list( new Value[]{value( 42 ), value( 43 )}, Value::asInt );
8185

8286
assertThat( mapped, equalTo( Arrays.asList( 42, 43 ) ) );
8387
}
@@ -91,7 +95,7 @@ void testMapValues()
9195
map.put( "k2", value( 42 ) );
9296

9397
// WHEN
94-
Map<String,Integer> mappedMap = Extract.map( map, integerExtractor() );
98+
Map<String,Integer> mappedMap = Extract.map( map, Value::asInt );
9599

96100
// THEN
97101
Collection<Integer> values = mappedMap.values();
@@ -108,7 +112,7 @@ void testShouldPreserveMapOrderMapValues()
108112
map.put( "k1", value( 42 ) );
109113

110114
// WHEN
111-
Map<String,Integer> mappedMap = Extract.map( map, integerExtractor() );
115+
Map<String,Integer> mappedMap = Extract.map( map, Value::asInt );
112116

113117
// THEN
114118
Collection<Integer> values = mappedMap.values();
@@ -126,7 +130,7 @@ void testProperties()
126130
InternalNode node = new InternalNode( 42L, Collections.singletonList( "L" ), props );
127131

128132
// WHEN
129-
Iterable<Pair<String, Integer>> properties = Extract.properties( node, integerExtractor() );
133+
Iterable<Pair<String,Integer>> properties = Extract.properties( node, Value::asInt );
130134

131135
// THEN
132136
Iterator<Pair<String, Integer>> iterator = properties.iterator();
@@ -141,24 +145,43 @@ void testFields()
141145
// GIVEN
142146
InternalRecord record = new InternalRecord( Arrays.asList( "k1" ), new Value[]{value( 42 )} );
143147
// WHEN
144-
List<Pair<String, Integer>> fields = Extract.fields( record, integerExtractor() );
148+
List<Pair<String,Integer>> fields = Extract.fields( record, Value::asInt );
145149

146150

147151
// THEN
148152
assertThat( fields, equalTo( Collections.singletonList( InternalPair.of( "k1", 42 ) ) ) );
149153
}
150154

151-
private Function<Value,Integer> integerExtractor()
155+
@Test
156+
void shouldExtractMapOfValuesFromNullOrEmptyMap()
157+
{
158+
assertEquals( emptyMap(), Extract.mapOfValues( null ) );
159+
assertEquals( emptyMap(), Extract.mapOfValues( emptyMap() ) );
160+
}
161+
162+
@Test
163+
void shouldExtractMapOfValues()
152164
{
153-
return new Function<Value,Integer>()
154-
{
155-
156-
@Override
157-
public Integer apply( Value value )
158-
{
159-
return value.asInt();
160-
}
161-
};
165+
Map<String,Object> map = new HashMap<>();
166+
map.put( "key1", "value1" );
167+
map.put( "key2", 42L );
168+
map.put( "key3", LocalDate.now() );
169+
map.put( "key4", new byte[]{1, 2, 3} );
170+
171+
Map<String,Value> mapOfValues = Extract.mapOfValues( map );
172+
173+
assertEquals( 4, map.size() );
174+
assertEquals( value( "value1" ), mapOfValues.get( "key1" ) );
175+
assertEquals( value( 42L ), mapOfValues.get( "key2" ) );
176+
assertEquals( value( LocalDate.now() ), mapOfValues.get( "key3" ) );
177+
assertEquals( value( new byte[]{1, 2, 3} ), mapOfValues.get( "key4" ) );
162178
}
163179

180+
@Test
181+
void shouldFailToExtractMapOfValuesFromUnsupportedValues()
182+
{
183+
assertThrows( ClientException.class, () -> Extract.mapOfValues( singletonMap( "key", new InternalNode( 1 ) ) ) );
184+
assertThrows( ClientException.class, () -> Extract.mapOfValues( singletonMap( "key", new InternalRelationship( 1, 1, 1, "HI" ) ) ) );
185+
assertThrows( ClientException.class, () -> Extract.mapOfValues( singletonMap( "key", new InternalPath( new InternalNode( 1 ) ) ) ) );
186+
}
164187
}

driver/src/test/java/org/neo4j/driver/internal/messaging/v1/BoltProtocolV1Test.java

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.junit.jupiter.api.Test;
2626
import org.mockito.ArgumentCaptor;
2727

28+
import java.time.Duration;
2829
import java.util.HashMap;
2930
import java.util.Map;
3031
import java.util.concurrent.CompletableFuture;
@@ -54,23 +55,27 @@
5455
import org.neo4j.driver.v1.Statement;
5556
import org.neo4j.driver.v1.TransactionConfig;
5657
import org.neo4j.driver.v1.Value;
58+
import org.neo4j.driver.v1.exceptions.ClientException;
5759

5860
import static java.util.Collections.emptyMap;
5961
import static java.util.Collections.singletonMap;
6062
import static org.hamcrest.Matchers.hasSize;
6163
import static org.hamcrest.Matchers.instanceOf;
64+
import static org.hamcrest.Matchers.startsWith;
6265
import static org.hamcrest.junit.MatcherAssert.assertThat;
6366
import static org.junit.jupiter.api.Assertions.assertEquals;
6467
import static org.junit.jupiter.api.Assertions.assertFalse;
6568
import static org.junit.jupiter.api.Assertions.assertNotNull;
6669
import static org.junit.jupiter.api.Assertions.assertNull;
70+
import static org.junit.jupiter.api.Assertions.assertThrows;
6771
import static org.junit.jupiter.api.Assertions.assertTrue;
6872
import static org.mockito.ArgumentMatchers.any;
6973
import static org.mockito.ArgumentMatchers.eq;
7074
import static org.mockito.Mockito.mock;
7175
import static org.mockito.Mockito.verify;
7276
import static org.neo4j.driver.internal.util.Futures.blockingGet;
7377
import static org.neo4j.driver.v1.Values.value;
78+
import static org.neo4j.driver.v1.util.TestUtil.await;
7479
import static org.neo4j.driver.v1.util.TestUtil.connectionMock;
7580

7681
public class BoltProtocolV1Test
@@ -152,7 +157,7 @@ void shouldBeginTransactionWithoutBookmark()
152157
}
153158

154159
@Test
155-
void shouldBeginTransactionWithBookmark()
160+
void shouldBeginTransactionWithBookmarks()
156161
{
157162
Connection connection = connectionMock();
158163
Bookmarks bookmarks = Bookmarks.from( "neo4j:bookmark:v1:tx100" );
@@ -197,37 +202,66 @@ void shouldRollbackTransaction()
197202
@Test
198203
void shouldRunInAutoCommitTransactionWithoutWaitingForRunResponse() throws Exception
199204
{
200-
testNotWaitingForRunResponse( true );
205+
testRunWithoutWaitingForRunResponse( true );
201206
}
202207

203208
@Test
204209
void shouldRunInAutoCommitTransactionAndWaitForSuccessRunResponse() throws Exception
205210
{
206-
testWaitingForRunResponse( true, true );
211+
testRunWithWaitingForResponse( true, true );
207212
}
208213

209214
@Test
210215
void shouldRunInAutoCommitTransactionAndWaitForFailureRunResponse() throws Exception
211216
{
212-
testWaitingForRunResponse( false, true );
217+
testRunWithWaitingForResponse( false, true );
213218
}
214219

215220
@Test
216221
void shouldRunInTransactionWithoutWaitingForRunResponse() throws Exception
217222
{
218-
testNotWaitingForRunResponse( false );
223+
testRunWithoutWaitingForRunResponse( false );
219224
}
220225

221226
@Test
222227
void shouldRunInTransactionAndWaitForSuccessRunResponse() throws Exception
223228
{
224-
testWaitingForRunResponse( true, false );
229+
testRunWithWaitingForResponse( true, false );
225230
}
226231

227232
@Test
228233
void shouldRunInTransactionAndWaitForFailureRunResponse() throws Exception
229234
{
230-
testWaitingForRunResponse( false, false );
235+
testRunWithWaitingForResponse( false, false );
236+
}
237+
238+
@Test
239+
void shouldNotSupportTransactionConfigInBeginTransaction()
240+
{
241+
TransactionConfig config = TransactionConfig.builder()
242+
.withTimeout( Duration.ofSeconds( 5 ) )
243+
.withMetadata( singletonMap( "key", "value" ) )
244+
.build();
245+
246+
CompletionStage<Void> txStage = protocol.beginTransaction( connectionMock(), Bookmarks.empty(), config );
247+
248+
ClientException e = assertThrows( ClientException.class, () -> await( txStage ) );
249+
assertThat( e.getMessage(), startsWith( "Driver is connected to the database that does not support transaction configuration" ) );
250+
}
251+
252+
@Test
253+
void shouldNotSupportTransactionConfigForAutoCommitTransactions()
254+
{
255+
TransactionConfig config = TransactionConfig.builder()
256+
.withTimeout( Duration.ofSeconds( 42 ) )
257+
.withMetadata( singletonMap( "hello", "world" ) )
258+
.build();
259+
260+
CompletionStage<InternalStatementResultCursor> cursorFuture = protocol.runInAutoCommitTransaction( connectionMock(), new Statement( "RETURN 1" ),
261+
Bookmarks.empty(), config, true );
262+
263+
ClientException e = assertThrows( ClientException.class, () -> await( cursorFuture ) );
264+
assertThat( e.getMessage(), startsWith( "Driver is connected to the database that does not support transaction configuration" ) );
231265
}
232266

233267
protected BoltProtocol createProtocol()
@@ -240,7 +274,7 @@ protected Class<? extends MessageFormat> expectedMessageFormatType()
240274
return MessageFormatV1.class;
241275
}
242276

243-
private void testNotWaitingForRunResponse( boolean autoCommitTx ) throws Exception
277+
private void testRunWithoutWaitingForRunResponse( boolean autoCommitTx ) throws Exception
244278
{
245279
Connection connection = mock( Connection.class );
246280

@@ -261,7 +295,7 @@ private void testNotWaitingForRunResponse( boolean autoCommitTx ) throws Excepti
261295
verifyRunInvoked( connection, autoCommitTx );
262296
}
263297

264-
private void testWaitingForRunResponse( boolean success, boolean session ) throws Exception
298+
private void testRunWithWaitingForResponse( boolean success, boolean session ) throws Exception
265299
{
266300
Connection connection = mock( Connection.class );
267301

0 commit comments

Comments
 (0)