Skip to content
This repository was archived by the owner on Sep 9, 2024. It is now read-only.

Commit f595d88

Browse files
committed
feat: Implement state handler calls
1 parent edab705 commit f595d88

2 files changed

Lines changed: 54 additions & 14 deletions

File tree

src/main/java/io/contract_testing/contractcase/client/ConfigHandle.java

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import static io.contract_testing.contractcase.client.ConnectorOutgoingMapper.CONTRACT_CASE_TRIGGER_AND_TEST;
44

5-
import io.contract_testing.contractcase.ContractCaseCoreError;
5+
import io.contract_testing.contractcase.ContractCaseConfigurationError;
6+
import io.contract_testing.contractcase.case_boundary.BoundaryStateHandler;
67
import io.contract_testing.contractcase.case_boundary.ContractCaseBoundaryConfig;
78
import io.contract_testing.contractcase.case_boundary.ITriggerFunction;
89
import org.jetbrains.annotations.NotNull;
@@ -20,12 +21,29 @@ public void setBoundaryConfig(ContractCaseBoundaryConfig boundaryConfig) {
2021
this.boundaryConfig = boundaryConfig;
2122
}
2223

24+
BoundaryStateHandler getStateHandler(String stateName) {
25+
if (boundaryConfig.getStateHandlers() == null) {
26+
throw new ContractCaseConfigurationError(
27+
"No state handlers provided, you must provide a state handler that can run '" + stateName
28+
+ "'");
29+
}
30+
var stateHandler = boundaryConfig.getStateHandlers().get(stateName);
31+
32+
if (stateHandler == null) {
33+
throw new ContractCaseConfigurationError(
34+
"The state handler named '" + stateName
35+
+ "' was not provided in the configuration");
36+
}
37+
return stateHandler;
38+
}
39+
2340
@NotNull
2441
ITriggerFunction getTriggerFunction(String handle) {
2542
ITriggerFunction trigger = getTriggerInternal(handle);
2643
if (trigger == null) {
27-
throw new ContractCaseCoreError(
28-
"Unable to trigger the function with handle '" + handle + "', as it is null");
44+
throw new ContractCaseConfigurationError(
45+
"The trigger function named '" + handle
46+
+ "' wasn't provided in the configuration, but is required by this test run");
2947
}
3048
return trigger;
3149
}
@@ -38,10 +56,12 @@ private ITriggerFunction getTriggerInternal(String handle) {
3856

3957
var triggerMap = boundaryConfig.getTriggerAndTests();
4058
if (triggerMap == null) {
41-
throw new ContractCaseCoreError(
42-
"Unable to trigger the function with handle '" + handle
43-
+ "', as the entire trigger map is null");
59+
throw new ContractCaseConfigurationError(
60+
"No trigger functions were provided, unable to run the trigger function '" + handle
61+
+ "'");
4462
}
4563
return triggerMap.get(handle);
4664
}
65+
66+
4767
}

src/main/java/io/contract_testing/contractcase/client/ContractResponseStreamObserver.java

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99
import com.google.protobuf.AbstractMessage;
1010
import com.google.protobuf.GeneratedMessageV3.Builder;
1111
import io.contract_testing.contractcase.ContractCaseCoreError;
12+
import io.contract_testing.contractcase.case_boundary.BoundaryResult;
13+
import io.contract_testing.contractcase.case_boundary.BoundaryStateHandler;
1214
import io.contract_testing.contractcase.case_boundary.ILogPrinter;
1315
import io.contract_testing.contractcase.case_boundary.IResultPrinter;
1416
import io.contract_testing.contractcase.case_boundary.IRunTestCallback;
15-
import io.contract_testing.contractcase.grpc.ContractCaseStream;
1617
import io.contract_testing.contractcase.grpc.ContractCaseStream.ContractResponse;
1718
import io.contract_testing.contractcase.grpc.ContractCaseStream.ResultResponse;
18-
import io.contract_testing.contractcase.grpc.ContractCaseStream.ResultSuccess;
19+
import io.contract_testing.contractcase.grpc.ContractCaseStream.StateHandlerHandle.Stage;
1920
import io.grpc.Status;
2021
import io.grpc.stub.StreamObserver;
2122
import org.jetbrains.annotations.NotNull;
@@ -50,13 +51,20 @@ public void onNext(final ContractResponse note) {
5051
switch (note.getKindCase()) {
5152
case RUN_STATE_HANDLER -> {
5253
final var stateHandlerRunRequest = note.getRunStateHandler();
53-
// TODO Implement this properly
54+
var stateName = stateHandlerRunRequest.getStateHandlerHandle()
55+
.getHandle()
56+
.getValue();
57+
var handle = configHandle.getStateHandler(
58+
stateName);
59+
5460
rpcConnector.sendResponse(
5561
ResultResponse.newBuilder()
56-
.setResult(ContractCaseStream.BoundaryResult.newBuilder()
57-
.setSuccess(ResultSuccess.newBuilder().build())
58-
.build()
59-
).build(),
62+
.setResult(mapResult(runStateHandler(
63+
stateHandlerRunRequest.getStateHandlerHandle()
64+
.getStage(),
65+
stateName,
66+
handle
67+
))).build(),
6068
requestId
6169
);
6270
}
@@ -135,7 +143,6 @@ public void onNext(final ContractResponse note) {
135143
}
136144
case START_TEST_EVENT -> {
137145
var startTestEvent = note.getStartTestEvent();
138-
139146
rpcConnector.sendResponse(
140147
mapResultResponse(
141148
runTestCallback.runTest(
@@ -185,4 +192,17 @@ public void onError(final Throwable t) {
185192
@Override
186193
public void onCompleted() {
187194
}
195+
196+
@NotNull
197+
private static BoundaryResult runStateHandler(Stage stage,
198+
String stateName,
199+
BoundaryStateHandler handle) {
200+
return switch (stage) {
201+
case STAGE_SETUP_UNSPECIFIED -> handle.setup();
202+
case STAGE_TEARDOWN -> handle.teardown();
203+
case UNRECOGNIZED -> throw new ContractCaseCoreError(
204+
"Unrecognised state handler stage while trying to run '" + stateName +
205+
"'");
206+
};
207+
}
188208
}

0 commit comments

Comments
 (0)