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

Commit a01ff31

Browse files
committed
feat: Add builders for config objects
1 parent e109a41 commit a01ff31

4 files changed

Lines changed: 407 additions & 20 deletions

File tree

contractcase/src/main/java/io/contract_testing/contractcase/ContractCaseConfig.java

Lines changed: 140 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,45 @@
22

33
import java.util.Map;
44

5+
/**
6+
* Config object for ContractCase
7+
*/
58
public class ContractCaseConfig {
69

7-
810
/**
911
* The name of the provider for this contract.
1012
*/
11-
public String providerName;
13+
public final String providerName;
1214

1315
/**
1416
* The name of the consumer for this contract.
1517
*/
16-
public String consumerName;
18+
public final String consumerName;
1719

1820

1921
/**
2022
* How much should we log during the test?
2123
*
2224
* @see LogLevel
2325
*/
24-
public LogLevel logLevel;
26+
public final LogLevel logLevel;
2527

2628
/**
2729
* The directory where the contract will be written. If you provide this, ContractCase will
2830
* generate the filename for you (unless `contractFilename` is specified, in which case this
2931
* setting is ignored)
3032
*/
31-
public String contractDir;
33+
public final String contractDir;
3234

3335
/**
3436
* The filename where the contract will be written. If you provide this, `contractDir` is ignored
3537
*/
36-
public String contractFilename;
38+
public final String contractFilename;
3739

3840
/**
3941
* Whether results should be printed on standard out during the test run
4042
*/
41-
public Boolean printResults;
43+
public final Boolean printResults;
4244

4345

4446
/**
@@ -50,49 +52,172 @@ public class ContractCaseConfig {
5052
* <p>
5153
* Default: `true` in contract definition, `false` in contract verification
5254
*/
53-
public Boolean throwOnFail;
55+
public final Boolean throwOnFail;
5456

5557
/**
5658
* Whether to publish contracts or verification results to the broker
5759
*
5860
* @see PublishType
5961
*/
60-
public PublishType publish;
62+
public final PublishType publish;
6163

6264
/**
6365
* The base URL for the contract broker
6466
*/
65-
public String brokerBaseUrl;
67+
public final String brokerBaseUrl;
6668

6769
/**
6870
* The access token to use for the contract broker. Must have CI scope.
6971
* <p>
7072
* If this is specified along with brokerBasicAuth, the basic auth is ignored.
7173
*/
72-
public String brokerCiAccessToken;
74+
public final String brokerCiAccessToken;
7375

7476
/**
7577
* The basic authentication username and password to access the contract broker.
7678
* <p>
7779
* If this is specified along with brokerCiAccessToken, basic auth credentials are ignored.
7880
*/
79-
public BrokerBasicAuthCredentials brokerBasicAuth;
81+
public final BrokerBasicAuthCredentials brokerBasicAuth;
8082

8183
/**
8284
* The base URL for your real server, if you are testing an http server.
8385
*
8486
* @deprecated This will be moved to a config property that allows configuration for arbitrary
8587
* mocks
8688
*/
87-
public String baseUrlUnderTest;
89+
@Deprecated
90+
public final String baseUrlUnderTest;
8891

8992

90-
public TriggerGroups triggers;
93+
public final TriggerGroups triggers;
9194

9295
/**
9396
* State setup and teardown handlers for any states this test requires (see (<a
9497
* href="https://case.contract-testing.io/docs/reference/state-handlers/">writing state
9598
* handlers</a>)) for more details
9699
*/
97-
public Map<String, StateHandler> stateHandlers;
100+
public final Map<String, StateHandler> stateHandlers;
101+
102+
public ContractCaseConfig(String providerName, String consumerName, LogLevel logLevel,
103+
String contractDir, String contractFilename, Boolean printResults, Boolean throwOnFail,
104+
PublishType publish, String brokerBaseUrl, String brokerCiAccessToken,
105+
BrokerBasicAuthCredentials brokerBasicAuth, String baseUrlUnderTest, TriggerGroups triggers,
106+
Map<String, StateHandler> stateHandlers) {
107+
this.providerName = providerName;
108+
this.consumerName = consumerName;
109+
this.logLevel = logLevel;
110+
this.contractDir = contractDir;
111+
this.contractFilename = contractFilename;
112+
this.printResults = printResults;
113+
this.throwOnFail = throwOnFail;
114+
this.publish = publish;
115+
this.brokerBaseUrl = brokerBaseUrl;
116+
this.brokerCiAccessToken = brokerCiAccessToken;
117+
this.brokerBasicAuth = brokerBasicAuth;
118+
this.baseUrlUnderTest = baseUrlUnderTest;
119+
this.triggers = triggers;
120+
this.stateHandlers = stateHandlers;
121+
}
122+
123+
public static final class ContractCaseConfigBuilder {
124+
125+
private String providerName;
126+
private String consumerName;
127+
private LogLevel logLevel;
128+
private String contractDir;
129+
private String contractFilename;
130+
private Boolean printResults;
131+
private Boolean throwOnFail;
132+
private PublishType publish;
133+
private String brokerBaseUrl;
134+
private String brokerCiAccessToken;
135+
private BrokerBasicAuthCredentials brokerBasicAuth;
136+
private String baseUrlUnderTest;
137+
private TriggerGroups triggers;
138+
private Map<String, StateHandler> stateHandlers;
139+
140+
private ContractCaseConfigBuilder() {
141+
}
142+
143+
public static ContractCaseConfigBuilder aContractCaseConfig() {
144+
return new ContractCaseConfigBuilder();
145+
}
146+
147+
public ContractCaseConfigBuilder providerName(String providerName) {
148+
this.providerName = providerName;
149+
return this;
150+
}
151+
152+
public ContractCaseConfigBuilder consumerName(String consumerName) {
153+
this.consumerName = consumerName;
154+
return this;
155+
}
156+
157+
public ContractCaseConfigBuilder logLevel(LogLevel logLevel) {
158+
this.logLevel = logLevel;
159+
return this;
160+
}
161+
162+
public ContractCaseConfigBuilder contractDir(String contractDir) {
163+
this.contractDir = contractDir;
164+
return this;
165+
}
166+
167+
public ContractCaseConfigBuilder contractFilename(String contractFilename) {
168+
this.contractFilename = contractFilename;
169+
return this;
170+
}
171+
172+
public ContractCaseConfigBuilder printResults(Boolean printResults) {
173+
this.printResults = printResults;
174+
return this;
175+
}
176+
177+
public ContractCaseConfigBuilder throwOnFail(Boolean throwOnFail) {
178+
this.throwOnFail = throwOnFail;
179+
return this;
180+
}
181+
182+
public ContractCaseConfigBuilder publish(PublishType publish) {
183+
this.publish = publish;
184+
return this;
185+
}
186+
187+
public ContractCaseConfigBuilder brokerBaseUrl(String brokerBaseUrl) {
188+
this.brokerBaseUrl = brokerBaseUrl;
189+
return this;
190+
}
191+
192+
public ContractCaseConfigBuilder brokerCiAccessToken(String brokerCiAccessToken) {
193+
this.brokerCiAccessToken = brokerCiAccessToken;
194+
return this;
195+
}
196+
197+
public ContractCaseConfigBuilder brokerBasicAuth(BrokerBasicAuthCredentials brokerBasicAuth) {
198+
this.brokerBasicAuth = brokerBasicAuth;
199+
return this;
200+
}
201+
202+
public ContractCaseConfigBuilder baseUrlUnderTest(String baseUrlUnderTest) {
203+
this.baseUrlUnderTest = baseUrlUnderTest;
204+
return this;
205+
}
206+
207+
public ContractCaseConfigBuilder triggers(TriggerGroups triggers) {
208+
this.triggers = triggers;
209+
return this;
210+
}
211+
212+
public ContractCaseConfigBuilder stateHandlers(Map<String, StateHandler> stateHandlers) {
213+
this.stateHandlers = stateHandlers;
214+
return this;
215+
}
216+
217+
public ContractCaseConfig build() {
218+
return new ContractCaseConfig(providerName, consumerName, logLevel, contractDir,
219+
contractFilename, printResults, throwOnFail, publish, brokerBaseUrl, brokerCiAccessToken,
220+
brokerBasicAuth, baseUrlUnderTest, triggers, stateHandlers);
221+
}
222+
}
98223
}
Lines changed: 133 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,139 @@
11
package io.contract_testing.contractcase;
22

3+
import java.util.Map;
4+
35
public class IndividualFailedTestConfig<T> extends ContractCaseConfig {
46

5-
public Trigger<T> trigger;
6-
public TestErrorResponseFunction testErrorResponse;
7+
public final Trigger<T> trigger;
8+
public final TestErrorResponseFunction testErrorResponse;
9+
10+
public IndividualFailedTestConfig(String providerName, String consumerName, LogLevel logLevel,
11+
String contractDir, String contractFilename, Boolean printResults, Boolean throwOnFail,
12+
PublishType publish, String brokerBaseUrl, String brokerCiAccessToken,
13+
BrokerBasicAuthCredentials brokerBasicAuth, String baseUrlUnderTest, TriggerGroups triggers,
14+
Map<String, StateHandler> stateHandlers, Trigger<T> trigger,
15+
TestErrorResponseFunction testErrorResponse) {
16+
super(providerName, consumerName, logLevel, contractDir, contractFilename, printResults,
17+
throwOnFail, publish, brokerBaseUrl, brokerCiAccessToken, brokerBasicAuth, baseUrlUnderTest,
18+
triggers, stateHandlers);
19+
this.trigger = trigger;
20+
this.testErrorResponse = testErrorResponse;
21+
}
22+
23+
public static final class IndividualFailedTestConfigBuilder<T> {
24+
25+
private String providerName;
26+
private String consumerName;
27+
private LogLevel logLevel;
28+
private String contractDir;
29+
private String contractFilename;
30+
private Boolean printResults;
31+
private Boolean throwOnFail;
32+
private PublishType publish;
33+
private String brokerBaseUrl;
34+
private String brokerCiAccessToken;
35+
private BrokerBasicAuthCredentials brokerBasicAuth;
36+
private String baseUrlUnderTest;
37+
private TriggerGroups triggers;
38+
private Map<String, StateHandler> stateHandlers;
39+
private Trigger<T> trigger;
40+
private TestErrorResponseFunction testErrorResponse;
41+
42+
private IndividualFailedTestConfigBuilder() {
43+
}
44+
45+
public static <T> IndividualFailedTestConfigBuilder<T> builder() {
46+
return new IndividualFailedTestConfigBuilder<T>();
47+
}
48+
49+
public IndividualFailedTestConfigBuilder<T> withProviderName(String providerName) {
50+
this.providerName = providerName;
51+
return this;
52+
}
53+
54+
public IndividualFailedTestConfigBuilder<T> withConsumerName(String consumerName) {
55+
this.consumerName = consumerName;
56+
return this;
57+
}
58+
59+
public IndividualFailedTestConfigBuilder<T> withLogLevel(LogLevel logLevel) {
60+
this.logLevel = logLevel;
61+
return this;
62+
}
63+
64+
public IndividualFailedTestConfigBuilder<T> withContractDir(String contractDir) {
65+
this.contractDir = contractDir;
66+
return this;
67+
}
68+
69+
public IndividualFailedTestConfigBuilder<T> withContractFilename(String contractFilename) {
70+
this.contractFilename = contractFilename;
71+
return this;
72+
}
73+
74+
public IndividualFailedTestConfigBuilder<T> withPrintResults(Boolean printResults) {
75+
this.printResults = printResults;
76+
return this;
77+
}
78+
79+
public IndividualFailedTestConfigBuilder<T> withThrowOnFail(Boolean throwOnFail) {
80+
this.throwOnFail = throwOnFail;
81+
return this;
82+
}
83+
84+
public IndividualFailedTestConfigBuilder<T> withPublish(PublishType publish) {
85+
this.publish = publish;
86+
return this;
87+
}
88+
89+
public IndividualFailedTestConfigBuilder<T> withBrokerBaseUrl(String brokerBaseUrl) {
90+
this.brokerBaseUrl = brokerBaseUrl;
91+
return this;
92+
}
93+
94+
public IndividualFailedTestConfigBuilder<T> withBrokerCiAccessToken(
95+
String brokerCiAccessToken) {
96+
this.brokerCiAccessToken = brokerCiAccessToken;
97+
return this;
98+
}
99+
100+
public IndividualFailedTestConfigBuilder<T> withBrokerBasicAuth(
101+
BrokerBasicAuthCredentials brokerBasicAuth) {
102+
this.brokerBasicAuth = brokerBasicAuth;
103+
return this;
104+
}
105+
106+
public IndividualFailedTestConfigBuilder<T> withBaseUrlUnderTest(String baseUrlUnderTest) {
107+
this.baseUrlUnderTest = baseUrlUnderTest;
108+
return this;
109+
}
110+
111+
public IndividualFailedTestConfigBuilder<T> withTriggers(TriggerGroups triggers) {
112+
this.triggers = triggers;
113+
return this;
114+
}
115+
116+
public IndividualFailedTestConfigBuilder<T> withStateHandlers(
117+
Map<String, StateHandler> stateHandlers) {
118+
this.stateHandlers = stateHandlers;
119+
return this;
120+
}
121+
122+
public IndividualFailedTestConfigBuilder<T> withTrigger(Trigger<T> trigger) {
123+
this.trigger = trigger;
124+
return this;
125+
}
126+
127+
public IndividualFailedTestConfigBuilder<T> withTestErrorResponse(
128+
TestErrorResponseFunction testErrorResponse) {
129+
this.testErrorResponse = testErrorResponse;
130+
return this;
131+
}
7132

133+
public IndividualFailedTestConfig<T> build() {
134+
return new IndividualFailedTestConfig<>(providerName, consumerName, logLevel, contractDir,
135+
contractFilename, printResults, throwOnFail, publish, brokerBaseUrl, brokerCiAccessToken,
136+
brokerBasicAuth, baseUrlUnderTest, triggers, stateHandlers, trigger, testErrorResponse);
137+
}
138+
}
8139
}

0 commit comments

Comments
 (0)