Skip to content

Commit a5371d1

Browse files
committed
chore: Update DSL usage
1 parent 46673ea commit a5371d1

2 files changed

Lines changed: 42 additions & 38 deletions

File tree

src/test/java/io/contract_testing/contractcase/example/HttpApiExampleTest.java

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import io.contract_testing.contractcase.ExampleDefinition;
1010
import io.contract_testing.contractcase.IndividualFailedTestConfig.IndividualFailedTestConfigBuilder;
1111
import io.contract_testing.contractcase.IndividualSuccessTestConfig.IndividualSuccessTestConfigBuilder;
12+
import io.contract_testing.contractcase.LogLevel;
1213
import io.contract_testing.contractcase.PublishType;
1314
import io.contract_testing.contractcase.Trigger;
1415
import io.contract_testing.contractcase.case_example_mock_types.mocks.http.HttpExample;
@@ -39,9 +40,9 @@ public class HttpApiExampleTest {
3940
.contractDir("temp-contracts")
4041
.build());
4142

42-
Trigger<String> getHealth = (Map<String, Object> config) -> {
43+
Trigger<String> getHealth = (setupInfo) -> {
4344
try {
44-
return new ApiClient((String) config.get("baseUrl")).getHealth();
45+
return new ApiClient(setupInfo.getInfo("baseUrl")).getHealth();
4546
} catch (IOException e) {
4647
throw new RuntimeException(e);
4748
}
@@ -86,7 +87,7 @@ public void testHealthUp() {
8687
IndividualSuccessTestConfigBuilder.<String>builder()
8788
.withProviderName("Java Example HTTP Server")
8889
.withTrigger(getHealth)
89-
.withTestResponse(status -> {
90+
.withTestResponse((status, setupInfo) -> {
9091
assertThat(status, is("up"));
9192
})
9293
);
@@ -108,7 +109,7 @@ public void testHealthDown() {
108109
IndividualSuccessTestConfigBuilder.<String>builder()
109110
.withProviderName("Java Example HTTP Server")
110111
.withTrigger(getHealth)
111-
.withTestResponse(status -> {
112+
.withTestResponse((status, setupInfo) -> {
112113
assertThat(status, is("down"));
113114
})
114115
);
@@ -127,7 +128,7 @@ public void testHealthUnavailable() {
127128
IndividualFailedTestConfigBuilder.<String>builder()
128129
.withProviderName("Java Example HTTP Server")
129130
.withTrigger(getHealth)
130-
.withTestErrorResponse(exception -> {
131+
.withTestErrorResponse((exception, setupInfo) -> {
131132
assertThat(exception.getMessage(), is("The server is not ready"));
132133
})
133134
);
@@ -156,16 +157,16 @@ public void testGetUserWithPathVariable() {
156157
),
157158
IndividualSuccessTestConfigBuilder.<User>builder()
158159
.withProviderName("Java Example HTTP Server")
159-
.withTrigger((config) -> {
160+
.withTrigger((setupInfo) -> {
160161
try {
161-
return new ApiClient((String) config.get("baseUrl"))
162-
.getUser(((Map<String, String>) config.get("variables")).get("userId"));
162+
return new ApiClient(setupInfo.getInfo("baseUrl"))
163+
.getUser(setupInfo.getStateVariable("userId"));
163164
} catch (IOException e) {
164165
throw new RuntimeException(e);
165166
}
166167
})
167-
.withTestResponse(user -> {
168-
assertThat(user.userId(), is("123"));
168+
.withTestResponse((user, setupInfo) -> {
169+
assertThat(user.userId(), is(setupInfo.getStateVariable("userId")));
169170
})
170171
);
171172
}
@@ -193,16 +194,16 @@ public void testGetUserWithQueryVariable() {
193194
),
194195
IndividualSuccessTestConfigBuilder.<User>builder()
195196
.withProviderName("Java Example HTTP Server")
196-
.withTrigger((config) -> {
197+
.withTrigger((setupInfo) -> {
197198
try {
198-
return new ApiClient((String) config.get("baseUrl"))
199-
.getUserQuery(((Map<String, String>) config.get("variables")).get("userId"));
199+
return new ApiClient(setupInfo.getInfo("baseUrl"))
200+
.getUserQuery(setupInfo.getStateVariable("userId"));
200201
} catch (IOException e) {
201202
throw new RuntimeException(e);
202203
}
203204
})
204-
.withTestResponse(user -> {
205-
assertThat(user.userId(), is("123"));
205+
.withTestResponse((user, setupInfo) -> {
206+
assertThat(user.userId(), is(setupInfo.getStateVariable("userId")));
206207
})
207208
);
208209
}
@@ -224,15 +225,15 @@ public void testGetUserWithQueryVariableWhenUserNotExist() {
224225
),
225226
IndividualFailedTestConfigBuilder.<User>builder()
226227
.withProviderName("Java Example HTTP Server")
227-
.withTrigger((config) -> {
228+
.withTrigger((setupInfo) -> {
228229
try {
229-
return new ApiClient((String) config.get("baseUrl"))
230-
.getUserQuery(((Map<String, String>) config.get("variables")).get("userId"));
230+
return new ApiClient(setupInfo.getInfo("baseUrl"))
231+
.getUserQuery(setupInfo.getStateVariable("userId"));
231232
} catch (IOException e) {
232233
throw new RuntimeException(e);
233234
}
234235
})
235-
.withTestErrorResponse(exception -> {
236+
.withTestErrorResponse((exception, setupInfo) -> {
236237
assertThat(exception.getClass(), is(UserNotFoundException.class));
237238
})
238239
);

src/test/java/io/contract_testing/contractcase/example/HttpApiExampleVerifyTest.java

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import io.contract_testing.contractcase.ContractCaseConfig;
88
import io.contract_testing.contractcase.ContractVerifier;
99
import io.contract_testing.contractcase.PublishType;
10+
import io.contract_testing.contractcase.SetupInfo;
1011
import io.contract_testing.contractcase.TestErrorResponseFunction;
1112
import io.contract_testing.contractcase.TestResponseFunction;
1213
import io.contract_testing.contractcase.Trigger;
@@ -22,54 +23,53 @@ public class HttpApiExampleVerifyTest {
2223
private static final ContractVerifier contract = new ContractVerifier(ContractCaseConfig.ContractCaseConfigBuilder.aContractCaseConfig()
2324
.providerName("http request provider")
2425
.publish(PublishType.NEVER)
25-
.contractDir("./packages/contract-case-jest/case-contracts")
26+
.contractDir("./case-contracts")
2627
.build());
2728

28-
Trigger<String> getHealth = (Map<String, Object> config) -> {
29+
Trigger<String> getHealth = (setupInfo) -> {
2930
try {
30-
return new ApiClient((String) config.get("baseUrl")).getHealth();
31+
return new ApiClient(setupInfo.getInfo("baseUrl")).getHealth();
3132
} catch (IOException e) {
3233
throw new RuntimeException(e);
3334
}
3435
};
3536

36-
Trigger<User> getUserFromConfig = (config) -> {
37+
Trigger<User> getUserFromConfig = (setupInfo) -> {
3738
try {
38-
return new ApiClient((String) config.get("baseUrl"))
39-
.getUser(((Map<String, String>) config.get("variables")).get("userId"));
39+
return new ApiClient(setupInfo.getInfo("baseUrl"))
40+
.getUser(setupInfo.getStateVariable("userId"));
4041
} catch (IOException e) {
4142
throw new RuntimeException(e);
4243
}
4344
};
4445

45-
Trigger<User> getUserByQuery = (config) -> {
46+
Trigger<User> getUserByQuery = (setupInfo) -> {
4647
try {
47-
return new ApiClient((String) config.get("baseUrl"))
48-
.getUserQuery(((Map<String, String>) config.get("variables")).get("userId"));
48+
return new ApiClient(setupInfo.getInfo("baseUrl"))
49+
.getUserQuery(setupInfo.getStateVariable("userId"));
4950
} catch (IOException e) {
5051
throw new RuntimeException(e);
5152
}
5253
};
5354

54-
Trigger<User> getUser123 = (config) -> {
55+
Trigger<User> getUser123 = (setupInfo) -> {
5556
try {
56-
return new ApiClient((String) config.get("baseUrl"))
57+
return new ApiClient(setupInfo.getInfo("baseUrl"))
5758
.getUser("123");
5859
} catch (IOException e) {
5960
throw new RuntimeException(e);
6061
}
6162
};
6263
private final Map<String, TestResponseFunction<User>> userSuccessTests = Map.of(
6364
"a (200) response with body an object shaped like {userId: {{userId}}}",
64-
(s) -> {
65-
// TODO: get this from config
66-
assertThat(s.userId(), is("123"));
65+
(s, setupInfo) -> {
66+
assertThat(s.userId(), is(setupInfo.getStateVariable("userId")));
6767
}
6868
);
6969
;
7070
private final Map<String, TestErrorResponseFunction> userErrorTests = Map.of(
7171
"a (404) response without a body",
72-
(e) -> {
72+
(e, setupInfo) -> {
7373
assertThat(e.getMessage(), is("User not found"));
7474
}
7575
);
@@ -88,7 +88,7 @@ public void testVerify() throws InterruptedException {
8888
,
8989
Map.of(
9090
"a (200) response with body an object shaped like {status: \"up\"}",
91-
(String result) -> {
91+
(String result, SetupInfo setupInfo) -> {
9292
assertThat(result, is("up"));
9393
}
9494
),
@@ -100,15 +100,17 @@ public void testVerify() throws InterruptedException {
100100
getHealth,
101101
Map.of(
102102
"a (200) response with body an object shaped like {status: <any string>}",
103-
(String result) -> {
103+
(result, setupInfo) -> {
104104
assertThat(result, isA(String.class));
105105
}
106106
),
107-
Map.of("a (httpStatus 4XX | 5XX) response without a body", (e) -> {
107+
Map.of(
108+
"a (httpStatus 4XX | 5XX) response without a body",
109+
(e, setupInfo) -> {
108110
assertThat(e.getMessage(), is("The server is not ready"));
109111
},
110112
"a (503) response with body an object shaped like {status: \"down\"}",
111-
(e) -> {
113+
(e, setupInfo) -> {
112114
assertThat(e.getMessage(), is("The server is not ready"));
113115
}
114116
)
@@ -131,6 +133,7 @@ public void testVerify() throws InterruptedException {
131133
userErrorTests
132134
)))
133135
.build());
136+
contract.close();
134137
}
135138

136139
}

0 commit comments

Comments
 (0)