Skip to content

Commit b1bf3fa

Browse files
authored
Implement result single in sync and async Testkit backends (#1210)
This update enables Testkit tests with `Feature:API:Result.Single` flag.
1 parent 8b2d872 commit b1bf3fa

File tree

4 files changed

+88
-4
lines changed

4 files changed

+88
-4
lines changed

testkit-backend/src/main/java/neo4j/org/testkit/backend/channel/handler/TestkitRequestProcessorHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.util.function.BiFunction;
3838

3939
import org.neo4j.driver.exceptions.Neo4jException;
40+
import org.neo4j.driver.exceptions.NoSuchRecordException;
4041
import org.neo4j.driver.exceptions.UntrustedServerException;
4142
import org.neo4j.driver.internal.spi.ConnectionPool;
4243

@@ -138,7 +139,7 @@ private TestkitResponse createErrorResponse( Throwable throwable )
138139
.build() )
139140
.build();
140141
}
141-
else if ( isConnectionPoolClosedException( throwable ) || throwable instanceof UntrustedServerException )
142+
else if ( isConnectionPoolClosedException( throwable ) || throwable instanceof UntrustedServerException || throwable instanceof NoSuchRecordException )
142143
{
143144
String id = testkitState.newId();
144145
return DriverError.builder()

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,17 @@ public class GetFeatures implements TestkitRequest
6666
"Optimization:PullPipelining",
6767
"Feature:API:Result.List",
6868
"Feature:API:Result.Peek",
69-
"Optimization:ResultListFetchAll"
69+
"Optimization:ResultListFetchAll",
70+
"Feature:API:Result.Single"
7071
) );
7172

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

8082
@Override
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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.TestkitState;
24+
import neo4j.org.testkit.backend.holder.ResultCursorHolder;
25+
import neo4j.org.testkit.backend.messages.responses.TestkitResponse;
26+
import reactor.core.publisher.Mono;
27+
28+
import java.util.concurrent.CompletionStage;
29+
30+
import org.neo4j.driver.Record;
31+
import org.neo4j.driver.async.ResultCursor;
32+
33+
@Setter
34+
@Getter
35+
public class ResultSingle implements TestkitRequest
36+
{
37+
private ResultSingleBody data;
38+
39+
@Override
40+
public TestkitResponse process( TestkitState testkitState )
41+
{
42+
return createResponse( testkitState.getResultHolder( data.getResultId() ).getResult().single() );
43+
}
44+
45+
@Override
46+
public CompletionStage<TestkitResponse> processAsync( TestkitState testkitState )
47+
{
48+
return testkitState.getAsyncResultHolder( data.getResultId() )
49+
.thenApply( ResultCursorHolder::getResult )
50+
.thenCompose( ResultCursor::singleAsync )
51+
.thenApply( this::createResponse );
52+
}
53+
54+
@Override
55+
public Mono<TestkitResponse> processRx( TestkitState testkitState )
56+
{
57+
throw new UnsupportedOperationException( "Single method is not supported by reactive API" );
58+
}
59+
60+
@Override
61+
public Mono<TestkitResponse> processReactive( TestkitState testkitState )
62+
{
63+
throw new UnsupportedOperationException( "Single method is not supported by reactive API" );
64+
}
65+
66+
private neo4j.org.testkit.backend.messages.responses.TestkitResponse createResponse( Record record )
67+
{
68+
return neo4j.org.testkit.backend.messages.responses.Record.builder()
69+
.data( neo4j.org.testkit.backend.messages.responses.Record.RecordBody.builder()
70+
.values( record )
71+
.build() )
72+
.build();
73+
}
74+
75+
@Setter
76+
@Getter
77+
public static class ResultSingleBody
78+
{
79+
private String resultId;
80+
}
81+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
@JsonSubTypes.Type( GetRoutingTable.class ), @JsonSubTypes.Type( TransactionClose.class ),
4343
@JsonSubTypes.Type( ResultList.class ), @JsonSubTypes.Type( GetConnectionPoolMetrics.class ),
4444
@JsonSubTypes.Type( ResultPeek.class ), @JsonSubTypes.Type( CheckDriverIsEncrypted.class ),
45-
@JsonSubTypes.Type( CypherTypeField.class )
45+
@JsonSubTypes.Type( CypherTypeField.class ), @JsonSubTypes.Type( ResultSingle.class )
4646
} )
4747
public interface TestkitRequest
4848
{

0 commit comments

Comments
 (0)