Skip to content

Commit 92bda82

Browse files
Add Result.peek support to TestKit back end (neo4j#1110) (neo4j#1133)
Co-authored-by: Robsdedude <[email protected]>
1 parent faa3e21 commit 92bda82

File tree

3 files changed

+93
-1
lines changed

3 files changed

+93
-1
lines changed

testkit-backend/src/main/java/neo4j/org/testkit/backend/messages/requests/GetFeatures.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,15 @@ public class GetFeatures implements TestkitRequest
6767
"Feature:Bolt:3.0",
6868
"Optimization:PullPipelining",
6969
"Feature:API:Result.List",
70+
"Feature:API:Result.Peek",
7071
"Optimization:ResultListFetchAll"
7172
) );
7273

7374
private static final Set<String> ASYNC_FEATURES = new HashSet<>( Arrays.asList(
7475
"Feature:Bolt:3.0",
7576
"Optimization:PullPipelining",
7677
"Feature:API:Result.List",
78+
"Feature:API:Result.Peek",
7779
"Optimization:ResultListFetchAll"
7880
) );
7981

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright (c) "Neo4j"
3+
* Neo4j Sweden AB [http://neo4j.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 neo4j.org.testkit.backend.messages.requests;
20+
21+
import lombok.Getter;
22+
import lombok.Setter;
23+
import neo4j.org.testkit.backend.RxBufferedSubscriber;
24+
import neo4j.org.testkit.backend.TestkitState;
25+
import neo4j.org.testkit.backend.holder.RxResultHolder;
26+
import neo4j.org.testkit.backend.messages.responses.NullRecord;
27+
import neo4j.org.testkit.backend.messages.responses.TestkitResponse;
28+
import org.neo4j.driver.Record;
29+
import org.neo4j.driver.Result;
30+
import org.neo4j.driver.exceptions.NoSuchRecordException;
31+
import reactor.core.publisher.Mono;
32+
33+
import java.util.concurrent.CompletionStage;
34+
35+
@Setter
36+
@Getter
37+
public class ResultPeek implements TestkitRequest
38+
{
39+
private ResultPeekBody data;
40+
41+
@Override
42+
public TestkitResponse process( TestkitState testkitState )
43+
{
44+
try
45+
{
46+
Result result = testkitState.getResultHolder( data.getResultId() ).getResult();
47+
return createResponse( result.peek() );
48+
}
49+
catch ( NoSuchRecordException ignored )
50+
{
51+
return NullRecord.builder().build();
52+
}
53+
}
54+
55+
@Override
56+
public CompletionStage<TestkitResponse> processAsync( TestkitState testkitState )
57+
{
58+
return testkitState.getAsyncResultHolder( data.getResultId() )
59+
.thenCompose( resultCursorHolder -> resultCursorHolder.getResult().peekAsync() )
60+
.thenApply( this::createResponseNullSafe );
61+
}
62+
63+
@Override
64+
public Mono<TestkitResponse> processRx( TestkitState testkitState )
65+
{
66+
throw new UnsupportedOperationException( "Operation not supported" );
67+
}
68+
69+
private TestkitResponse createResponse( Record record )
70+
{
71+
return neo4j.org.testkit.backend.messages.responses.Record.builder()
72+
.data( neo4j.org.testkit.backend.messages.responses.Record.RecordBody.builder()
73+
.values( record )
74+
.build() )
75+
.build();
76+
}
77+
78+
private TestkitResponse createResponseNullSafe( Record record )
79+
{
80+
return record != null ? createResponse( record ) : NullRecord.builder().build();
81+
}
82+
83+
@Setter
84+
@Getter
85+
public static class ResultPeekBody
86+
{
87+
private String resultId;
88+
}
89+
}

testkit-backend/src/main/java/neo4j/org/testkit/backend/messages/requests/TestkitRequest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
@JsonSubTypes.Type( DomainNameResolutionCompleted.class ), @JsonSubTypes.Type( StartTest.class ),
4141
@JsonSubTypes.Type( TransactionRollback.class ), @JsonSubTypes.Type( GetFeatures.class ),
4242
@JsonSubTypes.Type( GetRoutingTable.class ), @JsonSubTypes.Type( TransactionClose.class ),
43-
@JsonSubTypes.Type( ResultList.class ), @JsonSubTypes.Type( GetConnectionPoolMetrics.class )
43+
@JsonSubTypes.Type( ResultList.class ), @JsonSubTypes.Type( GetConnectionPoolMetrics.class ),
44+
@JsonSubTypes.Type( ResultPeek.class )
4445
} )
4546
public interface TestkitRequest
4647
{

0 commit comments

Comments
 (0)