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

Commit d1a9dde

Browse files
committed
feat: Add ContractVerifier
1 parent 66332ac commit d1a9dde

6 files changed

Lines changed: 128 additions & 11 deletions

File tree

contractcase/src/main/java/io/contracttesting/contractcase/BoundaryConfigMapper.java

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,63 @@
22

33
import io.contract_testing.contractcase.case_boundary.ContractCaseBoundaryConfig;
44
import io.contract_testing.contractcase.case_boundary.UserNamePassword;
5+
import org.jetbrains.annotations.NotNull;
56

67
class BoundaryConfigMapper {
78

9+
static <T> ContractCaseBoundaryConfig mapSuccessExample(
10+
final IndividualSuccessTestConfig<T> config,
11+
final String testRunId) {
12+
var builder = makeBuilder(config, testRunId);
13+
14+
if (config.trigger != null) {
15+
if (config.testResponse != null) {
16+
builder.triggerAndTest(BoundaryTriggerMapper.map(config.trigger, config.testResponse));
17+
} else {
18+
throw new ContractCaseConfigurationError(
19+
"Must specify `testResponse` if you are specifying a `trigger`");
20+
}
21+
} else {
22+
if (config.testResponse != null) {
23+
throw new ContractCaseConfigurationError(
24+
"Must specify `trigger` if you are specifying a `testResponse` function");
25+
}
26+
}
27+
28+
return builder.build();
29+
}
30+
31+
public static <T> ContractCaseBoundaryConfig mapFailingExample(
32+
IndividualFailedTestConfig<T> config,
33+
String testRunId) {
34+
var builder = makeBuilder(config, testRunId);
35+
36+
if (config.trigger != null) {
37+
if (config.testErrorResponse != null) {
38+
builder.triggerAndTest(BoundaryTriggerMapper.map(config.trigger, config.testErrorResponse));
39+
} else {
40+
throw new ContractCaseConfigurationError(
41+
"Must specify `testErrorResponse` if you are specifying a `trigger`");
42+
}
43+
} else {
44+
if (config.testErrorResponse != null) {
45+
throw new ContractCaseConfigurationError(
46+
"Must specify `trigger` if you are specifying a `testErrorResponse` function");
47+
}
48+
}
49+
50+
return builder.build();
51+
}
52+
853
static ContractCaseBoundaryConfig map(final ContractCaseConfig config,
954
final String testRunId) {
1055

56+
return makeBuilder(config, testRunId).build();
57+
}
58+
59+
@NotNull
60+
private static ContractCaseBoundaryConfig.Builder makeBuilder(ContractCaseConfig config,
61+
String testRunId) {
1162
var builder = ContractCaseBoundaryConfig.builder().testRunId(testRunId);
1263

1364
if (config.brokerBaseUrl != null) {
@@ -66,9 +117,10 @@ static ContractCaseBoundaryConfig map(final ContractCaseConfig config,
66117
if (config.stateHandlers != null) {
67118
builder.stateHandlers(BoundaryStateHandlerMapper.map(config.stateHandlers));
68119
}
69-
70-
return builder.build();
120+
return builder;
71121
}
122+
123+
72124
}
73125

74126

contractcase/src/main/java/io/contracttesting/contractcase/ContractCaseConfigurationError.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
package io.contracttesting.contractcase;
22

3+
import java.util.Arrays;
4+
import java.util.stream.Collectors;
35
import org.jetbrains.annotations.NotNull;
46

57
public class ContractCaseConfigurationError extends RuntimeException {
68

79
private final String location;
810

11+
public ContractCaseConfigurationError(@NotNull String message) {
12+
super(message);
13+
this.location = Arrays.stream(Thread.currentThread().getStackTrace())
14+
.map(StackTraceElement::toString).collect(
15+
Collectors.joining());
16+
}
17+
918
public ContractCaseConfigurationError(@NotNull String message, @NotNull String location) {
1019
super(message);
1120
this.location = location;

contractcase/src/main/java/io/contracttesting/contractcase/ContractDefiner.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@
55

66
public class ContractDefiner {
77

8-
98
private static final String TEST_RUN_ID = "JAVA";
10-
private final LogPrinter logPrinter;
119
private final BoundaryContractDefiner definer;
1210

11+
1312
public ContractDefiner(final @NotNull ContractCaseConfig config) {
1413

15-
this.logPrinter = new LogPrinter();
14+
LogPrinter logPrinter = new LogPrinter();
1615

1716
// TODO figure out how to get versions
1817
this.definer = new BoundaryContractDefiner(BoundaryConfigMapper.map(config, TEST_RUN_ID),
@@ -21,16 +20,16 @@ public ContractDefiner(final @NotNull ContractCaseConfig config) {
2120
BoundaryVersionGenerator.VERSIONS);
2221
}
2322

24-
public void runExample(ExampleDefinition definition,
25-
final @NotNull ContractCaseConfig additionalConfig) {
23+
public <T> void runExample(ExampleDefinition definition,
24+
final @NotNull IndividualSuccessTestConfig<T> additionalConfig) {
2625
BoundaryResultMapper.map(definer.runExample(BoundaryDefinitionMapper.map(definition),
27-
BoundaryConfigMapper.map(additionalConfig, TEST_RUN_ID)));
26+
BoundaryConfigMapper.mapSuccessExample(additionalConfig, TEST_RUN_ID)));
2827
}
2928

30-
public void runThrowingExample(ExampleDefinition definition,
31-
ContractCaseConfig additionalConfig) {
29+
public <T> void runThrowingExample(ExampleDefinition definition,
30+
IndividualFailedTestConfig<T> additionalConfig) {
3231
BoundaryResultMapper.map(definer.runRejectingExample(BoundaryDefinitionMapper.map(definition),
33-
BoundaryConfigMapper.map(additionalConfig, TEST_RUN_ID)));
32+
BoundaryConfigMapper.mapFailingExample(additionalConfig, TEST_RUN_ID)));
3433
}
3534

3635
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package io.contracttesting.contractcase;
2+
3+
import io.contract_testing.contractcase.case_boundary.BoundaryContractVerifier;
4+
import io.contract_testing.contractcase.case_boundary.BoundaryResult;
5+
import io.contract_testing.contractcase.case_boundary.BoundarySuccess;
6+
import io.contract_testing.contractcase.case_boundary.IInvokeCoreTest;
7+
import io.contract_testing.contractcase.case_boundary.IRunTestCallback;
8+
import org.jetbrains.annotations.NotNull;
9+
10+
public class ContractVerifier {
11+
12+
private final BoundaryContractVerifier verifier;
13+
14+
public ContractVerifier(ContractCaseConfig config) {
15+
LogPrinter logPrinter = new LogPrinter();
16+
this.verifier = new BoundaryContractVerifier(BoundaryConfigMapper.map(config, "VERIFICATION"),
17+
new IRunTestCallback() {
18+
@Override
19+
public @NotNull BoundaryResult runTest(@NotNull String testName,
20+
@NotNull IInvokeCoreTest invoker) {
21+
// TODO replace this with something that knows about JUnit
22+
try {
23+
try {
24+
BoundaryResultMapper.map(invoker.verify());
25+
} catch (Exception e) {
26+
System.err.println(e);
27+
}
28+
return new BoundarySuccess();
29+
} catch (Exception e) {
30+
return BoundaryExceptionMapper.map(e);
31+
}
32+
}
33+
34+
}, logPrinter, logPrinter, BoundaryVersionGenerator.VERSIONS);
35+
}
36+
37+
public void runVerification(ContractCaseConfig configOverrides) {
38+
this.verifier.runVerification(
39+
BoundaryConfigMapper.map(configOverrides, "VERIFICATION"));
40+
}
41+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package io.contracttesting.contractcase;
2+
3+
public class IndividualFailedTestConfig<T> extends ContractCaseConfig {
4+
5+
public Trigger<T> trigger;
6+
public TestErrorResponseFunction testErrorResponse;
7+
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package io.contracttesting.contractcase;
2+
3+
public class IndividualSuccessTestConfig<T> extends ContractCaseConfig {
4+
5+
public Trigger<T> trigger;
6+
public TestResponseFunction<T> testResponse;
7+
8+
}

0 commit comments

Comments
 (0)