Skip to content

Commit 3f8a372

Browse files
committed
Refactoring
1 parent d56407b commit 3f8a372

File tree

6 files changed

+109
-159
lines changed

6 files changed

+109
-159
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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 com.fasterxml.jackson.databind.annotation.JsonDeserialize;
22+
import java.time.Duration;
23+
import java.util.Map;
24+
import java.util.Optional;
25+
import lombok.Getter;
26+
import lombok.Setter;
27+
import neo4j.org.testkit.backend.CustomDriverError;
28+
import neo4j.org.testkit.backend.messages.requests.deserializer.TestkitCypherParamDeserializer;
29+
import org.neo4j.driver.TransactionConfig;
30+
31+
@Setter
32+
@Getter
33+
abstract class AbstractTestkitRequestWithTransactionConfig<
34+
T extends AbstractTestkitRequestWithTransactionConfig.TransactionConfigBody>
35+
implements TestkitRequest {
36+
protected T data;
37+
38+
protected TransactionConfig buildTxConfig() {
39+
return configureTx(TransactionConfig.builder()).build();
40+
}
41+
42+
private TransactionConfig.Builder configureTx(TransactionConfig.Builder builder) {
43+
return configureTxMetadata(configureTxTimeout(builder));
44+
}
45+
46+
private TransactionConfig.Builder configureTxMetadata(TransactionConfig.Builder builder) {
47+
data.getTxMeta().ifPresent(builder::withMetadata);
48+
return builder;
49+
}
50+
51+
private TransactionConfig.Builder configureTxTimeout(TransactionConfig.Builder builder) {
52+
try {
53+
data.getTimeout().ifPresent((timeout) -> builder.withTimeout(Duration.ofMillis(timeout)));
54+
} catch (IllegalArgumentException e) {
55+
throw new CustomDriverError(e);
56+
}
57+
return builder;
58+
}
59+
60+
@Setter
61+
abstract static class TransactionConfigBody {
62+
protected Integer timeout;
63+
64+
@JsonDeserialize(using = TestkitCypherParamDeserializer.class)
65+
protected Map<String, Object> txMeta;
66+
67+
Optional<Integer> getTimeout() {
68+
return Optional.ofNullable(timeout);
69+
}
70+
71+
Optional<Map<String, Object>> getTxMeta() {
72+
return Optional.ofNullable(txMeta);
73+
}
74+
}
75+
}

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

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020

2121
import static reactor.adapter.JdkFlowAdapter.flowPublisherToFlux;
2222

23-
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
24-
import java.util.Map;
2523
import java.util.concurrent.CompletionStage;
2624
import lombok.Getter;
2725
import lombok.Setter;
@@ -32,7 +30,6 @@
3230
import neo4j.org.testkit.backend.holder.RxTransactionHolder;
3331
import neo4j.org.testkit.backend.holder.SessionHolder;
3432
import neo4j.org.testkit.backend.holder.TransactionHolder;
35-
import neo4j.org.testkit.backend.messages.requests.deserializer.TestkitCypherParamDeserializer;
3633
import neo4j.org.testkit.backend.messages.responses.TestkitResponse;
3734
import neo4j.org.testkit.backend.messages.responses.Transaction;
3835
import org.neo4j.driver.Session;
@@ -42,17 +39,14 @@
4239
import org.neo4j.driver.reactive.RxSession;
4340
import reactor.core.publisher.Mono;
4441

45-
@Setter
46-
@Getter
47-
public class SessionBeginTransaction extends WithTxConfig {
48-
private SessionBeginTransactionBody data;
49-
42+
public class SessionBeginTransaction
43+
extends AbstractTestkitRequestWithTransactionConfig<SessionBeginTransaction.SessionBeginTransactionBody> {
5044
@Override
5145
public TestkitResponse process(TestkitState testkitState) {
5246
SessionHolder sessionHolder = testkitState.getSessionHolder(data.getSessionId());
5347
Session session = sessionHolder.getSession();
5448

55-
org.neo4j.driver.Transaction transaction = session.beginTransaction(getTxConfig());
49+
org.neo4j.driver.Transaction transaction = session.beginTransaction(buildTxConfig());
5650
return transaction(testkitState.addTransactionHolder(new TransactionHolder(sessionHolder, transaction)));
5751
}
5852

@@ -62,7 +56,7 @@ public CompletionStage<TestkitResponse> processAsync(TestkitState testkitState)
6256
AsyncSession session = sessionHolder.getSession();
6357
TransactionConfig.Builder builder = TransactionConfig.builder();
6458

65-
return session.beginTransactionAsync(getTxConfig())
59+
return session.beginTransactionAsync(buildTxConfig())
6660
.thenApply(tx -> transaction(
6761
testkitState.addAsyncTransactionHolder(new AsyncTransactionHolder(sessionHolder, tx))));
6862
});
@@ -75,7 +69,7 @@ public Mono<TestkitResponse> processRx(TestkitState testkitState) {
7569
RxSession session = sessionHolder.getSession();
7670
TransactionConfig.Builder builder = TransactionConfig.builder();
7771

78-
return Mono.fromDirect(session.beginTransaction(getTxConfig()))
72+
return Mono.fromDirect(session.beginTransaction(buildTxConfig()))
7973
.map(tx -> transaction(
8074
testkitState.addRxTransactionHolder(new RxTransactionHolder(sessionHolder, tx))));
8175
});
@@ -87,7 +81,7 @@ public Mono<TestkitResponse> processReactive(TestkitState testkitState) {
8781
ReactiveSession session = sessionHolder.getSession();
8882
TransactionConfig.Builder builder = TransactionConfig.builder();
8983

90-
return Mono.fromDirect(flowPublisherToFlux(session.beginTransaction(getTxConfig())))
84+
return Mono.fromDirect(flowPublisherToFlux(session.beginTransaction(buildTxConfig())))
9185
.map(tx -> transaction(testkitState.addReactiveTransactionHolder(
9286
new ReactiveTransactionHolder(sessionHolder, tx))));
9387
});
@@ -99,7 +93,7 @@ public Mono<TestkitResponse> processReactiveStreams(TestkitState testkitState) {
9993
var session = sessionHolder.getSession();
10094
TransactionConfig.Builder builder = TransactionConfig.builder();
10195

102-
return Mono.fromDirect(session.beginTransaction(getTxConfig()))
96+
return Mono.fromDirect(session.beginTransaction(buildTxConfig()))
10397
.map(tx -> transaction(testkitState.addReactiveTransactionStreamsHolder(
10498
new ReactiveTransactionStreamsHolder(sessionHolder, tx))));
10599
});
@@ -113,18 +107,8 @@ private Transaction transaction(String txId) {
113107

114108
@Getter
115109
@Setter
116-
public static class SessionBeginTransactionBody implements WithTxConfig.ITxConfigBody {
110+
public static class SessionBeginTransactionBody
111+
extends AbstractTestkitRequestWithTransactionConfig.TransactionConfigBody {
117112
private String sessionId;
118-
119-
@JsonDeserialize(using = TestkitCypherParamDeserializer.class)
120-
private Map<String, Object> txMeta;
121-
122-
private Integer timeout;
123-
private Boolean timeoutPresent = false;
124-
125-
public void setTimeout(Integer timeout) {
126-
this.timeout = timeout;
127-
timeoutPresent = true;
128-
}
129113
}
130114
}

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

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
import static reactor.adapter.JdkFlowAdapter.flowPublisherToFlux;
2222
import static reactor.adapter.JdkFlowAdapter.publisherToFlowPublisher;
2323

24-
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
25-
import java.util.Map;
2624
import java.util.concurrent.CompletableFuture;
2725
import java.util.concurrent.CompletionStage;
2826
import java.util.concurrent.ExecutionException;
@@ -37,7 +35,6 @@
3735
import neo4j.org.testkit.backend.holder.RxTransactionHolder;
3836
import neo4j.org.testkit.backend.holder.SessionHolder;
3937
import neo4j.org.testkit.backend.holder.TransactionHolder;
40-
import neo4j.org.testkit.backend.messages.requests.deserializer.TestkitCypherParamDeserializer;
4138
import neo4j.org.testkit.backend.messages.responses.RetryableDone;
4239
import neo4j.org.testkit.backend.messages.responses.RetryableTry;
4340
import neo4j.org.testkit.backend.messages.responses.TestkitResponse;
@@ -50,17 +47,14 @@
5047
import org.reactivestreams.Publisher;
5148
import reactor.core.publisher.Mono;
5249

53-
@Setter
54-
@Getter
55-
public class SessionReadTransaction extends WithTxConfig {
56-
private SessionReadTransactionBody data;
57-
50+
public class SessionReadTransaction
51+
extends AbstractTestkitRequestWithTransactionConfig<SessionReadTransaction.SessionReadTransactionBody> {
5852
@Override
5953
@SuppressWarnings("deprecation")
6054
public TestkitResponse process(TestkitState testkitState) {
6155
SessionHolder sessionHolder = testkitState.getSessionHolder(data.getSessionId());
6256
Session session = sessionHolder.getSession();
63-
session.readTransaction(handle(testkitState, sessionHolder), getTxConfig());
57+
session.readTransaction(handle(testkitState, sessionHolder), buildTxConfig());
6458
return retryableDone();
6559
}
6660

@@ -81,7 +75,7 @@ public CompletionStage<TestkitResponse> processAsync(TestkitState testkitState)
8175
return txWorkFuture;
8276
};
8377

84-
return session.readTransactionAsync(workWrapper, getTxConfig());
78+
return session.readTransactionAsync(workWrapper, buildTxConfig());
8579
})
8680
.thenApply(nothing -> retryableDone());
8781
}
@@ -100,7 +94,7 @@ public Mono<TestkitResponse> processRx(TestkitState testkitState) {
10094
return Mono.fromCompletionStage(tryResult);
10195
};
10296

103-
return Mono.fromDirect(sessionHolder.getSession().readTransaction(workWrapper, getTxConfig()));
97+
return Mono.fromDirect(sessionHolder.getSession().readTransaction(workWrapper, buildTxConfig()));
10498
})
10599
.then(Mono.just(retryableDone()));
106100
}
@@ -120,7 +114,7 @@ public Mono<TestkitResponse> processReactive(TestkitState testkitState) {
120114
};
121115

122116
return Mono.fromDirect(
123-
flowPublisherToFlux(sessionHolder.getSession().executeRead(workWrapper, getTxConfig())));
117+
flowPublisherToFlux(sessionHolder.getSession().executeRead(workWrapper, buildTxConfig())));
124118
})
125119
.then(Mono.just(retryableDone()));
126120
}
@@ -140,7 +134,7 @@ public Mono<TestkitResponse> processReactiveStreams(TestkitState testkitState) {
140134
return Mono.fromCompletionStage(tryResult);
141135
};
142136

143-
return Mono.fromDirect(sessionHolder.getSession().executeRead(workWrapper, getTxConfig()));
137+
return Mono.fromDirect(sessionHolder.getSession().executeRead(workWrapper, buildTxConfig()));
144138
})
145139
.then(Mono.just(retryableDone()));
146140
}
@@ -180,18 +174,8 @@ private RetryableDone retryableDone() {
180174

181175
@Setter
182176
@Getter
183-
public static class SessionReadTransactionBody implements WithTxConfig.ITxConfigBody {
177+
public static class SessionReadTransactionBody
178+
extends AbstractTestkitRequestWithTransactionConfig.TransactionConfigBody {
184179
private String sessionId;
185-
186-
@JsonDeserialize(using = TestkitCypherParamDeserializer.class)
187-
private Map<String, Object> txMeta;
188-
189-
private Integer timeout;
190-
private Boolean timeoutPresent = false;
191-
192-
public void setTimeout(Integer timeout) {
193-
this.timeout = timeout;
194-
timeoutPresent = true;
195-
}
196180
}
197181
}

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

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,15 @@
4545
import org.neo4j.driver.reactive.RxSession;
4646
import reactor.core.publisher.Mono;
4747

48-
@Setter
49-
@Getter
50-
public class SessionRun extends WithTxConfig {
51-
private SessionRunBody data;
52-
48+
public class SessionRun extends AbstractTestkitRequestWithTransactionConfig<SessionRun.SessionRunBody> {
5349
@Override
5450
public TestkitResponse process(TestkitState testkitState) {
5551
SessionHolder sessionHolder = testkitState.getSessionHolder(data.getSessionId());
5652
Session session = sessionHolder.getSession();
5753
Query query = Optional.ofNullable(data.params)
5854
.map(params -> new Query(data.cypher, data.params))
5955
.orElseGet(() -> new Query(data.cypher));
60-
org.neo4j.driver.Result result = session.run(query, getTxConfig());
56+
org.neo4j.driver.Result result = session.run(query, buildTxConfig());
6157
String id = testkitState.addResultHolder(new ResultHolder(sessionHolder, result));
6258

6359
return createResponse(id, result.keys());
@@ -71,7 +67,7 @@ public CompletionStage<TestkitResponse> processAsync(TestkitState testkitState)
7167
.map(params -> new Query(data.cypher, data.params))
7268
.orElseGet(() -> new Query(data.cypher));
7369

74-
return session.runAsync(query, getTxConfig()).thenApply(resultCursor -> {
70+
return session.runAsync(query, buildTxConfig()).thenApply(resultCursor -> {
7571
String id = testkitState.addAsyncResultHolder(new ResultCursorHolder(sessionHolder, resultCursor));
7672
return createResponse(id, resultCursor.keys());
7773
});
@@ -87,7 +83,7 @@ public Mono<TestkitResponse> processRx(TestkitState testkitState) {
8783
.map(params -> new Query(data.cypher, data.params))
8884
.orElseGet(() -> new Query(data.cypher));
8985

90-
RxResult result = session.run(query, getTxConfig());
86+
RxResult result = session.run(query, buildTxConfig());
9187
String id = testkitState.addRxResultHolder(new RxResultHolder(sessionHolder, result));
9288

9389
// The keys() method causes RUN message exchange.
@@ -104,7 +100,7 @@ public Mono<TestkitResponse> processReactive(TestkitState testkitState) {
104100
.map(params -> new Query(data.cypher, data.params))
105101
.orElseGet(() -> new Query(data.cypher));
106102

107-
return Mono.fromDirect(flowPublisherToFlux(session.run(query, getTxConfig())))
103+
return Mono.fromDirect(flowPublisherToFlux(session.run(query, buildTxConfig())))
108104
.map(result -> {
109105
String id =
110106
testkitState.addReactiveResultHolder(new ReactiveResultHolder(sessionHolder, result));
@@ -121,7 +117,7 @@ public Mono<TestkitResponse> processReactiveStreams(TestkitState testkitState) {
121117
.map(params -> new Query(data.cypher, data.params))
122118
.orElseGet(() -> new Query(data.cypher));
123119

124-
return Mono.fromDirect(session.run(query, getTxConfig())).map(result -> {
120+
return Mono.fromDirect(session.run(query, buildTxConfig())).map(result -> {
125121
String id = testkitState.addReactiveResultStreamsHolder(
126122
new ReactiveResultStreamsHolder(sessionHolder, result));
127123
return createResponse(id, result.keys());
@@ -137,22 +133,11 @@ private Result createResponse(String resultId, List<String> keys) {
137133

138134
@Setter
139135
@Getter
140-
public static class SessionRunBody implements ITxConfigBody {
136+
public static class SessionRunBody extends AbstractTestkitRequestWithTransactionConfig.TransactionConfigBody {
141137
@JsonDeserialize(using = TestkitCypherParamDeserializer.class)
142138
private Map<String, Object> params;
143139

144140
private String sessionId;
145141
private String cypher;
146-
147-
@JsonDeserialize(using = TestkitCypherParamDeserializer.class)
148-
private Map<String, Object> txMeta;
149-
150-
private Integer timeout;
151-
private Boolean timeoutPresent = false;
152-
153-
public void setTimeout(Integer timeout) {
154-
this.timeout = timeout;
155-
timeoutPresent = true;
156-
}
157142
}
158143
}

0 commit comments

Comments
 (0)