From a5f3ab121b6c98cb05655bf6e3c0f94ee8792476 Mon Sep 17 00:00:00 2001 From: Robsdedude Date: Mon, 24 Jan 2022 19:44:10 +0100 Subject: [PATCH] Add Result.peek support to TestKit back end (#1110) --- .../messages/requests/GetFeatures.java | 2 + .../backend/messages/requests/ResultPeek.java | 89 +++++++++++++++++++ .../messages/requests/TestkitRequest.java | 3 +- 3 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 testkit-backend/src/main/java/neo4j/org/testkit/backend/messages/requests/ResultPeek.java diff --git a/testkit-backend/src/main/java/neo4j/org/testkit/backend/messages/requests/GetFeatures.java b/testkit-backend/src/main/java/neo4j/org/testkit/backend/messages/requests/GetFeatures.java index bd242e27f7..462663f8db 100644 --- a/testkit-backend/src/main/java/neo4j/org/testkit/backend/messages/requests/GetFeatures.java +++ b/testkit-backend/src/main/java/neo4j/org/testkit/backend/messages/requests/GetFeatures.java @@ -67,6 +67,7 @@ public class GetFeatures implements TestkitRequest "Feature:Bolt:3.0", "Optimization:PullPipelining", "Feature:API:Result.List", + "Feature:API:Result.Peek", "Optimization:ResultListFetchAll" ) ); @@ -74,6 +75,7 @@ public class GetFeatures implements TestkitRequest "Feature:Bolt:3.0", "Optimization:PullPipelining", "Feature:API:Result.List", + "Feature:API:Result.Peek", "Optimization:ResultListFetchAll" ) ); diff --git a/testkit-backend/src/main/java/neo4j/org/testkit/backend/messages/requests/ResultPeek.java b/testkit-backend/src/main/java/neo4j/org/testkit/backend/messages/requests/ResultPeek.java new file mode 100644 index 0000000000..4853318449 --- /dev/null +++ b/testkit-backend/src/main/java/neo4j/org/testkit/backend/messages/requests/ResultPeek.java @@ -0,0 +1,89 @@ +/* + * Copyright (c) "Neo4j" + * Neo4j Sweden AB [http://neo4j.com] + * + * This file is part of Neo4j. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package neo4j.org.testkit.backend.messages.requests; + +import lombok.Getter; +import lombok.Setter; +import neo4j.org.testkit.backend.RxBufferedSubscriber; +import neo4j.org.testkit.backend.TestkitState; +import neo4j.org.testkit.backend.holder.RxResultHolder; +import neo4j.org.testkit.backend.messages.responses.NullRecord; +import neo4j.org.testkit.backend.messages.responses.TestkitResponse; +import org.neo4j.driver.Record; +import org.neo4j.driver.Result; +import org.neo4j.driver.exceptions.NoSuchRecordException; +import reactor.core.publisher.Mono; + +import java.util.concurrent.CompletionStage; + +@Setter +@Getter +public class ResultPeek implements TestkitRequest +{ + private ResultPeekBody data; + + @Override + public TestkitResponse process( TestkitState testkitState ) + { + try + { + Result result = testkitState.getResultHolder( data.getResultId() ).getResult(); + return createResponse( result.peek() ); + } + catch ( NoSuchRecordException ignored ) + { + return NullRecord.builder().build(); + } + } + + @Override + public CompletionStage processAsync( TestkitState testkitState ) + { + return testkitState.getAsyncResultHolder( data.getResultId() ) + .thenCompose( resultCursorHolder -> resultCursorHolder.getResult().peekAsync() ) + .thenApply( this::createResponseNullSafe ); + } + + @Override + public Mono processRx( TestkitState testkitState ) + { + throw new UnsupportedOperationException( "Operation not supported" ); + } + + private TestkitResponse createResponse( Record record ) + { + return neo4j.org.testkit.backend.messages.responses.Record.builder() + .data( neo4j.org.testkit.backend.messages.responses.Record.RecordBody.builder() + .values( record ) + .build() ) + .build(); + } + + private TestkitResponse createResponseNullSafe( Record record ) + { + return record != null ? createResponse( record ) : NullRecord.builder().build(); + } + + @Setter + @Getter + public static class ResultPeekBody + { + private String resultId; + } +} diff --git a/testkit-backend/src/main/java/neo4j/org/testkit/backend/messages/requests/TestkitRequest.java b/testkit-backend/src/main/java/neo4j/org/testkit/backend/messages/requests/TestkitRequest.java index 64a3558560..5399ce1350 100644 --- a/testkit-backend/src/main/java/neo4j/org/testkit/backend/messages/requests/TestkitRequest.java +++ b/testkit-backend/src/main/java/neo4j/org/testkit/backend/messages/requests/TestkitRequest.java @@ -40,7 +40,8 @@ @JsonSubTypes.Type( DomainNameResolutionCompleted.class ), @JsonSubTypes.Type( StartTest.class ), @JsonSubTypes.Type( TransactionRollback.class ), @JsonSubTypes.Type( GetFeatures.class ), @JsonSubTypes.Type( GetRoutingTable.class ), @JsonSubTypes.Type( TransactionClose.class ), - @JsonSubTypes.Type( ResultList.class ), @JsonSubTypes.Type( GetConnectionPoolMetrics.class ) + @JsonSubTypes.Type( ResultList.class ), @JsonSubTypes.Type( GetConnectionPoolMetrics.class ), + @JsonSubTypes.Type( ResultPeek.class ) } ) public interface TestkitRequest {