|
| 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 | +} |
0 commit comments