Skip to content

Commit 581d53e

Browse files
committed
chore: Add test for complex return type in java
1 parent 221596b commit 581d53e

4 files changed

Lines changed: 96 additions & 18 deletions

File tree

packages/dsl-java/case-contracts/java-function-implementer-example/java-function-caller-example-main.case.json

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,32 @@
5050
"arguments": [],
5151
"functionName": "NoArgFunction"
5252
},
53+
"matcher:returning an object shaped like {a: an object shaped like {b: <any integer>},c: \"d\"}": {
54+
"_case:matcher:type": "_case:FunctionResultMatcher",
55+
"success": {
56+
"a": {
57+
"b": {
58+
"_case:context:matchBy": "type",
59+
"_case:matcher:example": 2,
60+
"_case:matcher:resolvesTo": "number",
61+
"_case:matcher:type": "_case:Integer"
62+
}
63+
},
64+
"c": "d"
65+
}
66+
},
67+
"matcher:An invocation of complexReturn( <any integer> )": {
68+
"_case:matcher:type": "_case:FunctionArgumentsMatcher",
69+
"arguments": [
70+
{
71+
"_case:context:matchBy": "type",
72+
"_case:matcher:example": 2,
73+
"_case:matcher:resolvesTo": "number",
74+
"_case:matcher:type": "_case:Integer"
75+
}
76+
],
77+
"functionName": "complexReturn"
78+
},
5379
"matcher:returning \"bar\"": {
5480
"_case:matcher:type": "_case:FunctionResultMatcher",
5581
"success": "bar"
@@ -184,10 +210,10 @@
184210
"type": "_case:MockFunctionExecution"
185211
}
186212
},
187-
"functionName": "PageNumbers",
213+
"functionName": "complexReturn",
188214
"request": {
189215
"_case:matcher:type": "_case:Lookup",
190-
"_case:matcher:uniqueName": "An invocation of PageNumbers( <any integer> )",
216+
"_case:matcher:uniqueName": "An invocation of complexReturn( <any integer> )",
191217
"_case:matcher:child": {
192218
"_case:matcher:type": "_case:FunctionArgumentsMatcher",
193219
"arguments": [
@@ -198,15 +224,25 @@
198224
"_case:matcher:type": "_case:Integer"
199225
}
200226
],
201-
"functionName": "PageNumbers"
227+
"functionName": "complexReturn"
202228
}
203229
},
204230
"response": {
205231
"_case:matcher:type": "_case:Lookup",
206-
"_case:matcher:uniqueName": "returning \"2 pages\"",
232+
"_case:matcher:uniqueName": "returning an object shaped like {a: an object shaped like {b: <any integer>},c: \"d\"}",
207233
"_case:matcher:child": {
208234
"_case:matcher:type": "_case:FunctionResultMatcher",
209-
"success": "2 pages"
235+
"success": {
236+
"a": {
237+
"b": {
238+
"_case:context:matchBy": "type",
239+
"_case:matcher:example": 2,
240+
"_case:matcher:resolvesTo": "number",
241+
"_case:matcher:type": "_case:Integer"
242+
}
243+
},
244+
"c": "d"
245+
}
210246
}
211247
}
212248
},

packages/dsl-java/src/test/java/io/contract_testing/contractcase/test/function/FunctionCallerExampleTest.java

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import io.contract_testing.contractcase.configuration.ChangedContractsBehaviour;
1010
import io.contract_testing.contractcase.configuration.ContractCaseConfig.ContractCaseConfigBuilder;
1111
import io.contract_testing.contractcase.configuration.IndividualSuccessTestConfig.IndividualSuccessTestConfigBuilder;
12-
import io.contract_testing.contractcase.configuration.LogLevel;
1312
import io.contract_testing.contractcase.configuration.PublishType;
1413
import io.contract_testing.contractcase.definitions.interactions.functions.FunctionExecutionExample;
1514
import io.contract_testing.contractcase.definitions.interactions.functions.WillCallFunction;
@@ -34,9 +33,12 @@ static void before() {
3433
.consumerName("Java Function Caller Example")
3534
.providerName("Java Function Implementer Example")
3635
.publish(PublishType.NEVER)
37-
// .logLevel(LogLevel.MAINTAINER_DEBUG)
38-
// .changedContracts(ChangedContractsBehaviour.OVERWRITE)
39-
.adviceOverrides(Map.of("OVERWRITE_CONTRACTS_NEEDED", "Please re-run this test, but:\nFirst uncomment the changedContracts line in this unit test"))
36+
// .logLevel(LogLevel.MAINTAINER_DEBUG)
37+
.changedContracts(ChangedContractsBehaviour.OVERWRITE)
38+
.adviceOverrides(Map.of(
39+
"OVERWRITE_CONTRACTS_NEEDED",
40+
"Please re-run this test, but:\nFirst uncomment the changedContracts line in this unit test"
41+
))
4042
.build());
4143
}
4244

@@ -91,31 +93,51 @@ public void testOneArgFunction() {
9193

9294
private String parse(String json) {
9395
try {
94-
return new ObjectMapper().readValue(json,String.class);
96+
return new ObjectMapper().readValue(json, String.class);
9597
} catch (JsonProcessingException e) {
9698
throw new RuntimeException(e);
9799
}
98100
}
99101

102+
public record SecondLayer(int b) {
103+
104+
}
105+
106+
public record FirstLayer(SecondLayer a, String c) {
107+
108+
}
109+
110+
private FirstLayer parseComplex(String json) {
111+
try {
112+
return new ObjectMapper().readValue(json, FirstLayer.class);
113+
} catch (JsonProcessingException e) {
114+
throw new RuntimeException(e);
115+
}
116+
}
100117

101118
@Test
102-
public void testThrowingFunction() {
119+
public void testComplexReturn() {
103120

104121
contract.runInteraction(
105122
new InteractionDefinition<>(
106123
List.of(new InState("The map is null")),
107124
new WillCallFunction(FunctionExecutionExample.builder()
108125
.arguments(List.of(new AnyInteger(2)))
109-
.returnValue("2 pages")
110-
.functionName("PageNumbers")
126+
.returnValue(
127+
Map.of(
128+
"a", Map.of("b", new AnyInteger(2)),
129+
"c", "d"
130+
))
131+
.functionName("complexReturn")
111132
.build())
112133
),
113-
IndividualSuccessTestConfigBuilder.<String>builder()
134+
IndividualSuccessTestConfigBuilder.<FirstLayer>builder()
114135
.withTrigger((setupInfo) ->
115-
parse(setupInfo.getFunction(setupInfo.getMockSetup("functionHandle"))
136+
parseComplex(setupInfo.getFunction(setupInfo.getMockSetup("functionHandle"))
116137
.apply(List.of("2"))))
117138
.withTestResponse((result, setupInfo) -> {
118-
assertThat(result).isEqualTo("2 pages");
139+
assertThat(result.c).isEqualTo("d");
140+
assertThat(result.a).isEqualTo(new SecondLayer(2));
119141
})
120142
);
121143

@@ -138,7 +160,7 @@ public void testStatefulFunction() {
138160
),
139161
IndividualSuccessTestConfigBuilder.<String>builder()
140162
.withTrigger((setupInfo) ->
141-
parse(setupInfo.getFunction(setupInfo.getMockSetup("functionHandle"))
163+
parse(setupInfo.getFunction(setupInfo.getMockSetup("functionHandle"))
142164
.apply(List.of("\"foo\""))))
143165
.withTestResponse((result, setupInfo) -> {
144166
assertThat(result).isEqualTo("bar");

packages/dsl-java/src/test/java/io/contract_testing/contractcase/test/function/verification/FunctionCallerVerificationEmptyConfigTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import io.contract_testing.contractcase.configuration.LogLevel;
99
import io.contract_testing.contractcase.configuration.PublishType;
1010
import io.contract_testing.contractcase.configuration.StateHandler;
11+
import io.contract_testing.contractcase.test.function.FunctionCallerExampleTest.FirstLayer;
12+
import io.contract_testing.contractcase.test.function.FunctionCallerExampleTest.SecondLayer;
1113
import java.util.HashMap;
1214
import java.util.function.Function;
1315
import org.jetbrains.annotations.NotNull;
@@ -61,6 +63,14 @@ public void testVerifyRunsGlobalStateHandlers() {
6163
convertJsonStringArgs((String key) -> mockedStore.get(key))
6264
);
6365

66+
contract.registerFunction("complexReturn",
67+
convertJsonIntegerArg(
68+
(Integer v) ->
69+
new FirstLayer(
70+
new SecondLayer(10), "d")
71+
)
72+
);
73+
6474
contract.runVerification();
6575
}
6676

packages/dsl-java/src/test/java/io/contract_testing/contractcase/test/function/verification/FunctionCallerVerificationTest.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
import com.fasterxml.jackson.core.JsonProcessingException;
44
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import io.contract_testing.contractcase.ContractVerifier;
56
import io.contract_testing.contractcase.configuration.ContractCaseConfig;
67
import io.contract_testing.contractcase.configuration.ContractCaseConfig.ContractCaseConfigBuilder;
7-
import io.contract_testing.contractcase.ContractVerifier;
88
import io.contract_testing.contractcase.configuration.InvokableFunctions.InvokableFunction1;
99
import io.contract_testing.contractcase.configuration.PublishType;
1010
import io.contract_testing.contractcase.configuration.StateHandler;
11+
import io.contract_testing.contractcase.test.function.FunctionCallerExampleTest.FirstLayer;
12+
import io.contract_testing.contractcase.test.function.FunctionCallerExampleTest.SecondLayer;
1113
import java.util.HashMap;
1214
import java.util.function.Function;
1315
import org.jetbrains.annotations.NotNull;
@@ -33,6 +35,14 @@ public void testVerify() throws InterruptedException {
3335
return null;
3436
});
3537

38+
contract.registerFunction("complexReturn",
39+
convertJsonIntegerArg(
40+
(Integer v) ->
41+
new FirstLayer(
42+
new SecondLayer(10), "d")
43+
)
44+
);
45+
3646
contract.registerFunction(
3747
"PageNumbers",
3848
convertJsonIntegerArg((Integer num) -> num + " pages")

0 commit comments

Comments
 (0)