Skip to content

Commit a8fe2b4

Browse files
committed
Dedicated class for parsing of SUCCESS metadata
Previously parsing of SUCCESS message metadata was done in `PullAllResponseHandler` which violates single responsibility principle. This commit extracts it in a dedicated helper class and adds unit tests.
1 parent 0c57215 commit a8fe2b4

File tree

9 files changed

+500
-112
lines changed

9 files changed

+500
-112
lines changed

driver/src/main/java/org/neo4j/driver/internal/handlers/PullAllResponseHandler.java

Lines changed: 3 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
*/
1919
package org.neo4j.driver.internal.handlers;
2020

21-
import java.util.Collections;
2221
import java.util.LinkedList;
23-
import java.util.List;
2422
import java.util.Map;
2523
import java.util.Queue;
2624
import java.util.concurrent.CompletableFuture;
@@ -29,20 +27,11 @@
2927
import org.neo4j.driver.internal.InternalRecord;
3028
import org.neo4j.driver.internal.spi.Connection;
3129
import org.neo4j.driver.internal.spi.ResponseHandler;
32-
import org.neo4j.driver.internal.summary.InternalNotification;
33-
import org.neo4j.driver.internal.summary.InternalPlan;
34-
import org.neo4j.driver.internal.summary.InternalProfiledPlan;
35-
import org.neo4j.driver.internal.summary.InternalResultSummary;
36-
import org.neo4j.driver.internal.summary.InternalServerInfo;
37-
import org.neo4j.driver.internal.summary.InternalSummaryCounters;
30+
import org.neo4j.driver.internal.summary.ResultSummaryCreator;
3831
import org.neo4j.driver.v1.Record;
3932
import org.neo4j.driver.v1.Statement;
4033
import org.neo4j.driver.v1.Value;
41-
import org.neo4j.driver.v1.summary.Notification;
42-
import org.neo4j.driver.v1.summary.Plan;
43-
import org.neo4j.driver.v1.summary.ProfiledPlan;
4434
import org.neo4j.driver.v1.summary.ResultSummary;
45-
import org.neo4j.driver.v1.summary.StatementType;
4635

4736
import static java.util.Collections.emptyMap;
4837
import static java.util.Objects.requireNonNull;
@@ -302,89 +291,7 @@ private boolean completeFailureFuture( Throwable error )
302291

303292
private ResultSummary extractResultSummary( Map<String,Value> metadata )
304293
{
305-
InternalServerInfo serverInfo = new InternalServerInfo( connection.serverAddress(),
306-
connection.serverVersion() );
307-
return new InternalResultSummary( statement, serverInfo, extractStatementType( metadata ),
308-
extractCounters( metadata ), extractPlan( metadata ), extractProfiledPlan( metadata ),
309-
extractNotifications( metadata ), runResponseHandler.resultAvailableAfter(),
310-
extractResultConsumedAfter( metadata ) );
311-
}
312-
313-
private static StatementType extractStatementType( Map<String,Value> metadata )
314-
{
315-
Value typeValue = metadata.get( "type" );
316-
if ( typeValue != null )
317-
{
318-
return StatementType.fromCode( typeValue.asString() );
319-
}
320-
return null;
321-
}
322-
323-
private static InternalSummaryCounters extractCounters( Map<String,Value> metadata )
324-
{
325-
Value countersValue = metadata.get( "stats" );
326-
if ( countersValue != null )
327-
{
328-
return new InternalSummaryCounters(
329-
counterValue( countersValue, "nodes-created" ),
330-
counterValue( countersValue, "nodes-deleted" ),
331-
counterValue( countersValue, "relationships-created" ),
332-
counterValue( countersValue, "relationships-deleted" ),
333-
counterValue( countersValue, "properties-set" ),
334-
counterValue( countersValue, "labels-added" ),
335-
counterValue( countersValue, "labels-removed" ),
336-
counterValue( countersValue, "indexes-added" ),
337-
counterValue( countersValue, "indexes-removed" ),
338-
counterValue( countersValue, "constraints-added" ),
339-
counterValue( countersValue, "constraints-removed" )
340-
);
341-
}
342-
return null;
343-
}
344-
345-
private static int counterValue( Value countersValue, String name )
346-
{
347-
Value value = countersValue.get( name );
348-
return value.isNull() ? 0 : value.asInt();
349-
}
350-
351-
private static Plan extractPlan( Map<String,Value> metadata )
352-
{
353-
Value planValue = metadata.get( "plan" );
354-
if ( planValue != null )
355-
{
356-
return InternalPlan.EXPLAIN_PLAN_FROM_VALUE.apply( planValue );
357-
}
358-
return null;
359-
}
360-
361-
private static ProfiledPlan extractProfiledPlan( Map<String,Value> metadata )
362-
{
363-
Value profiledPlanValue = metadata.get( "profile" );
364-
if ( profiledPlanValue != null )
365-
{
366-
return InternalProfiledPlan.PROFILED_PLAN_FROM_VALUE.apply( profiledPlanValue );
367-
}
368-
return null;
369-
}
370-
371-
private static List<Notification> extractNotifications( Map<String,Value> metadata )
372-
{
373-
Value notificationsValue = metadata.get( "notifications" );
374-
if ( notificationsValue != null )
375-
{
376-
return notificationsValue.asList( InternalNotification.VALUE_TO_NOTIFICATION );
377-
}
378-
return Collections.emptyList();
379-
}
380-
381-
private static long extractResultConsumedAfter( Map<String,Value> metadata )
382-
{
383-
Value resultConsumedAfterValue = metadata.get( "result_consumed_after" );
384-
if ( resultConsumedAfterValue != null )
385-
{
386-
return resultConsumedAfterValue.asLong();
387-
}
388-
return -1;
294+
long resultAvailableAfter = runResponseHandler.resultAvailableAfter();
295+
return ResultSummaryCreator.create( statement, connection, resultAvailableAfter, metadata );
389296
}
390297
}

driver/src/main/java/org/neo4j/driver/internal/summary/InternalInputPosition.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
*/
1919
package org.neo4j.driver.internal.summary;
2020

21+
import java.util.Objects;
22+
2123
import org.neo4j.driver.v1.summary.InputPosition;
2224

2325
/**
@@ -61,6 +63,29 @@ public int column()
6163
return column;
6264
}
6365

66+
@Override
67+
public boolean equals( Object o )
68+
{
69+
if ( this == o )
70+
{
71+
return true;
72+
}
73+
if ( o == null || getClass() != o.getClass() )
74+
{
75+
return false;
76+
}
77+
InternalInputPosition that = (InternalInputPosition) o;
78+
return offset == that.offset &&
79+
line == that.line &&
80+
column == that.column;
81+
}
82+
83+
@Override
84+
public int hashCode()
85+
{
86+
return Objects.hash( offset, line, column );
87+
}
88+
6489
@Override
6590
public String toString()
6691
{

driver/src/main/java/org/neo4j/driver/internal/summary/InternalResultSummary.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,15 @@ public List<Notification> notifications()
110110
@Override
111111
public long resultAvailableAfter( TimeUnit unit )
112112
{
113-
return unit.convert( resultAvailableAfter, TimeUnit.MILLISECONDS );
113+
return resultAvailableAfter == -1 ? resultAvailableAfter
114+
: unit.convert( resultAvailableAfter, TimeUnit.MILLISECONDS );
114115
}
115116

116117
@Override
117118
public long resultConsumedAfter( TimeUnit unit )
118119
{
119-
return unit.convert( resultConsumedAfter, TimeUnit.MILLISECONDS );
120+
return resultConsumedAfter == -1 ? resultConsumedAfter
121+
: unit.convert( resultConsumedAfter, TimeUnit.MILLISECONDS );
120122
}
121123

122124
@Override

driver/src/main/java/org/neo4j/driver/internal/summary/InternalServerInfo.java

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,19 @@
2525

2626
public class InternalServerInfo implements ServerInfo
2727
{
28-
private final BoltServerAddress address;
28+
private final String address;
2929
private final String version;
3030

3131
public InternalServerInfo( BoltServerAddress address, ServerVersion version )
3232
{
33-
this( address, version.toString() );
34-
}
35-
36-
public InternalServerInfo( BoltServerAddress address, String version )
37-
{
38-
this.address = address;
39-
this.version = version;
40-
}
41-
42-
public BoltServerAddress boltServerAddress()
43-
{
44-
return this.address;
33+
this.address = address.toString();
34+
this.version = version.toString();
4535
}
4636

4737
@Override
4838
public String address()
4939
{
50-
return this.address.toString();
40+
return address;
5141
}
5242

5343
@Override
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
* Copyright (c) 2002-2017 "Neo Technology,"
3+
* Network Engine for Objects in Lund AB [http://neotechnology.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.neo4j.driver.internal.summary;
20+
21+
import java.util.Collections;
22+
import java.util.List;
23+
import java.util.Map;
24+
25+
import org.neo4j.driver.internal.spi.Connection;
26+
import org.neo4j.driver.v1.Statement;
27+
import org.neo4j.driver.v1.Value;
28+
import org.neo4j.driver.v1.summary.Notification;
29+
import org.neo4j.driver.v1.summary.Plan;
30+
import org.neo4j.driver.v1.summary.ProfiledPlan;
31+
import org.neo4j.driver.v1.summary.ResultSummary;
32+
import org.neo4j.driver.v1.summary.ServerInfo;
33+
import org.neo4j.driver.v1.summary.StatementType;
34+
35+
public final class ResultSummaryCreator
36+
{
37+
private ResultSummaryCreator()
38+
{
39+
}
40+
41+
public static ResultSummary create( Statement statement, Connection connection, long resultAvailableAfter,
42+
Map<String,Value> metadata )
43+
{
44+
ServerInfo serverInfo = new InternalServerInfo( connection.serverAddress(), connection.serverVersion() );
45+
return new InternalResultSummary( statement, serverInfo, extractStatementType( metadata ),
46+
extractCounters( metadata ), extractPlan( metadata ), extractProfiledPlan( metadata ),
47+
extractNotifications( metadata ), resultAvailableAfter, extractResultConsumedAfter( metadata ) );
48+
}
49+
50+
private static StatementType extractStatementType( Map<String,Value> metadata )
51+
{
52+
Value typeValue = metadata.get( "type" );
53+
if ( typeValue != null )
54+
{
55+
return StatementType.fromCode( typeValue.asString() );
56+
}
57+
return null;
58+
}
59+
60+
private static InternalSummaryCounters extractCounters( Map<String,Value> metadata )
61+
{
62+
Value countersValue = metadata.get( "stats" );
63+
if ( countersValue != null )
64+
{
65+
return new InternalSummaryCounters(
66+
counterValue( countersValue, "nodes-created" ),
67+
counterValue( countersValue, "nodes-deleted" ),
68+
counterValue( countersValue, "relationships-created" ),
69+
counterValue( countersValue, "relationships-deleted" ),
70+
counterValue( countersValue, "properties-set" ),
71+
counterValue( countersValue, "labels-added" ),
72+
counterValue( countersValue, "labels-removed" ),
73+
counterValue( countersValue, "indexes-added" ),
74+
counterValue( countersValue, "indexes-removed" ),
75+
counterValue( countersValue, "constraints-added" ),
76+
counterValue( countersValue, "constraints-removed" )
77+
);
78+
}
79+
return null;
80+
}
81+
82+
private static int counterValue( Value countersValue, String name )
83+
{
84+
Value value = countersValue.get( name );
85+
return value.isNull() ? 0 : value.asInt();
86+
}
87+
88+
private static Plan extractPlan( Map<String,Value> metadata )
89+
{
90+
Value planValue = metadata.get( "plan" );
91+
if ( planValue != null )
92+
{
93+
return InternalPlan.EXPLAIN_PLAN_FROM_VALUE.apply( planValue );
94+
}
95+
return null;
96+
}
97+
98+
private static ProfiledPlan extractProfiledPlan( Map<String,Value> metadata )
99+
{
100+
Value profiledPlanValue = metadata.get( "profile" );
101+
if ( profiledPlanValue != null )
102+
{
103+
return InternalProfiledPlan.PROFILED_PLAN_FROM_VALUE.apply( profiledPlanValue );
104+
}
105+
return null;
106+
}
107+
108+
private static List<Notification> extractNotifications( Map<String,Value> metadata )
109+
{
110+
Value notificationsValue = metadata.get( "notifications" );
111+
if ( notificationsValue != null )
112+
{
113+
return notificationsValue.asList( InternalNotification.VALUE_TO_NOTIFICATION );
114+
}
115+
return Collections.emptyList();
116+
}
117+
118+
private static long extractResultConsumedAfter( Map<String,Value> metadata )
119+
{
120+
Value resultConsumedAfterValue = metadata.get( "result_consumed_after" );
121+
if ( resultConsumedAfterValue != null )
122+
{
123+
return resultConsumedAfterValue.asLong();
124+
}
125+
return -1;
126+
}
127+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ public void setUp()
9090
{
9191
connection = connectionMock();
9292
when( connection.release() ).thenReturn( completedFuture( null ) );
93+
when( connection.serverAddress() ).thenReturn( BoltServerAddress.LOCAL_DEFAULT );
9394
when( connection.serverVersion() ).thenReturn( ServerVersion.v3_2_0 );
9495
connectionProvider = mock( ConnectionProvider.class );
9596
when( connectionProvider.acquireConnection( any( AccessMode.class ) ) )

driver/src/test/java/org/neo4j/driver/internal/handlers/SessionPullAllResponseHandlerTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import java.util.concurrent.CompletableFuture;
2424

25+
import org.neo4j.driver.internal.BoltServerAddress;
2526
import org.neo4j.driver.internal.spi.Connection;
2627
import org.neo4j.driver.internal.util.ServerVersion;
2728
import org.neo4j.driver.v1.Statement;
@@ -64,6 +65,7 @@ private SessionPullAllResponseHandler newHandler( Connection connection )
6465
private static Connection newConnectionMock()
6566
{
6667
Connection connection = mock( Connection.class );
68+
when( connection.serverAddress() ).thenReturn( BoltServerAddress.LOCAL_DEFAULT );
6769
when( connection.serverVersion() ).thenReturn( ServerVersion.v3_2_0 );
6870
return connection;
6971
}

driver/src/test/java/org/neo4j/driver/internal/handlers/TransactionPullAllResponseHandlerTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import java.util.concurrent.CompletableFuture;
2424

25+
import org.neo4j.driver.internal.BoltServerAddress;
2526
import org.neo4j.driver.internal.ExplicitTransaction;
2627
import org.neo4j.driver.internal.spi.Connection;
2728
import org.neo4j.driver.internal.util.ServerVersion;
@@ -37,6 +38,7 @@ public class TransactionPullAllResponseHandlerTest
3738
public void shouldMarkTransactionAsFailedOnFailure()
3839
{
3940
Connection connection = mock( Connection.class );
41+
when( connection.serverAddress() ).thenReturn( BoltServerAddress.LOCAL_DEFAULT );
4042
when( connection.serverVersion() ).thenReturn( ServerVersion.v3_2_0 );
4143
ExplicitTransaction tx = mock( ExplicitTransaction.class );
4244
TransactionPullAllResponseHandler handler = new TransactionPullAllResponseHandler( new Statement( "RETURN 1" ),

0 commit comments

Comments
 (0)