Skip to content

Commit f378b26

Browse files
committed
fix(java-dsl): Add exceptions to the InvokeableFunction classes, allowing easier definition of contracts on methods that have checked exceptions
1 parent 3ee839c commit f378b26

10 files changed

Lines changed: 93 additions & 84 deletions

File tree

packages/dsl-java/src/main/java/io/contract_testing/contractcase/ContractVerifier.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public void close() {
119119
}
120120

121121

122-
public void registerFunction(String functionName, InvokableFunction0 function) {
122+
public <E extends Exception> void registerFunction(String functionName, InvokableFunction0<E> function) {
123123
registerFunctionInternal(functionName, ConnectorInvokableFunctionMapper.fromInvokableFunction(
124124
functionName,
125125
function

packages/dsl-java/src/main/java/io/contract_testing/contractcase/configuration/InvokableFunctions.java

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,57 @@
33
public class InvokableFunctions {
44

55
@FunctionalInterface
6-
public interface InvokableFunction0 {
6+
public interface InvokableFunction0<E extends Exception> {
77

8-
String apply();
8+
String apply() throws E;
99
}
1010

1111
@FunctionalInterface
12-
public interface InvokableFunction1 {
12+
public interface InvokableFunction1<E extends Exception> {
1313

14-
String apply(String a);
14+
String apply(String a) throws E;
1515
}
1616

1717
@FunctionalInterface
18-
public interface InvokableFunction2 {
18+
public interface InvokableFunction2<E extends Exception> {
1919

20-
String apply(String a, String b);
20+
String apply(String a, String b) throws E;
2121
}
2222

2323
@FunctionalInterface
24-
public interface InvokableFunction3 {
24+
public interface InvokableFunction3<E extends Exception> {
2525

26-
String apply(String a, String b, String c);
26+
String apply(String a, String b, String c) throws E;
2727
}
2828

2929
@FunctionalInterface
30-
public interface InvokableFunction4 {
30+
public interface InvokableFunction4<E extends Exception> {
3131

32-
String apply(String a, String b, String c, String d);
32+
String apply(String a, String b, String c, String d) throws E;
3333
}
3434

3535
@FunctionalInterface
36-
public interface InvokableFunction5 {
36+
public interface InvokableFunction5<E extends Exception> {
3737

38-
String apply(String a, String b, String c, String d, String e);
38+
String apply(String a, String b, String c, String d, String e) throws E;
3939
}
4040

4141
@FunctionalInterface
42-
public interface InvokableFunction6 {
42+
public interface InvokableFunction6<E extends Exception> {
4343

44-
String apply(String a, String b, String c, String d, String e, String f);
44+
String apply(String a, String b, String c, String d, String e, String f)
45+
throws E;
4546
}
4647

4748
@FunctionalInterface
48-
public interface InvokableFunction7 {
49+
public interface InvokableFunction7<E extends Exception> {
4950

50-
String apply(String a, String b, String c, String d, String e, String f, String g);
51+
String apply(String a,
52+
String b,
53+
String c,
54+
String d,
55+
String e,
56+
String f,
57+
String g) throws E;
5158
}
5259
}

packages/dsl-java/src/main/java/io/contract_testing/contractcase/internal/client/rpc/AbstractRpcConnector.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ abstract class AbstractRpcConnector<T extends AbstractMessage, B extends Generat
4646
* <p/>
4747
* This should probably be moved somewhere other than the connector.
4848
*/
49-
private final Map<String, ConnectorInvokableFunction> registeredFunctions = new ConcurrentHashMap<>();
49+
private final Map<String, ConnectorInvokableFunction<?>> registeredFunctions = new ConcurrentHashMap<>();
5050

5151
private final ResponseWaiter responseWaiter = new ResponseWaiter();
5252

@@ -277,7 +277,7 @@ public void close() {
277277
* @param functionName The name (ie, handle) of the function that the Core can use as a callback
278278
* @param function The actual function that can be invoked
279279
*/
280-
public <R> void registerFunction(String functionName, ConnectorInvokableFunction function) {
280+
public <E extends Exception> void registerFunction(String functionName, ConnectorInvokableFunction<E> function) {
281281
if (this.registeredFunctions.containsKey(functionName)) {
282282
throw new ContractCaseConfigurationError(
283283
"The function '"

packages/dsl-java/src/main/java/io/contract_testing/contractcase/internal/client/rpc/ConnectorOutgoingMapper.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import com.google.protobuf.InvalidProtocolBufferException;
88
import com.google.protobuf.StringValue;
99
import com.google.protobuf.Struct;
10-
import com.google.protobuf.Value;
1110
import com.google.protobuf.util.JsonFormat;
1211
import io.contract_testing.contractcase.configuration.LogLevel;
1312
import io.contract_testing.contractcase.exceptions.ContractCaseCoreError;
@@ -241,11 +240,6 @@ static ResultResponse mapResultResponse(ConnectorResult result) {
241240
.setResult(mapResult(result)).build();
242241
}
243242

244-
private static Value mapMapToValue(Object payload) {
245-
return Value.newBuilder()
246-
.setStructValue(getStructBuilder(objectMapper.valueToTree(payload)).build())
247-
.build();
248-
}
249243

250244
private static Struct mapMapToStruct(Map<String, Object> payload) {
251245
return getStructBuilder(objectMapper.valueToTree(payload)).build();

packages/dsl-java/src/main/java/io/contract_testing/contractcase/internal/client/rpc/ResponseWaiter.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ String createAwait(String reason) {
4141
* @param result the error to cancel them all with.
4242
*/
4343
void cancelAll(BoundaryResult result) {
44-
this.responseFutures.forEach((key, value) -> {
45-
value.complete(result);
46-
});
44+
this.responseFutures.forEach((key, value) -> value.complete(result));
4745
}
4846

4947
ConnectorResult awaitResponse(String id, int timeoutSeconds) {

packages/dsl-java/src/main/java/io/contract_testing/contractcase/internal/edge/ConnectorInvokableFunctionMapper.java

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
public class ConnectorInvokableFunctionMapper {
2222

2323

24-
public static abstract class ConnectorInvokableFunction {
24+
public static abstract class ConnectorInvokableFunction<E extends Exception> {
2525

2626
private final String functionName;
2727
private final int expectedArgumentCount;
@@ -35,7 +35,7 @@ public static abstract class ConnectorInvokableFunction {
3535
}
3636

3737

38-
protected abstract String invoke(List<String> args) throws JsonProcessingException;
38+
protected abstract String invoke(List<String> args) throws JsonProcessingException, E;
3939

4040
/**
4141
* Called by the core to invoke a user-provided function.
@@ -109,31 +109,31 @@ public ConnectorResult apply(List<String> args) {
109109
}
110110

111111

112-
public static ConnectorInvokableFunction fromInvokableFunction(String functionName,
113-
InvokableFunction0 function) {
114-
return new ConnectorInvokableFunction(functionName, 0) {
112+
public static <E extends Exception> ConnectorInvokableFunction<E> fromInvokableFunction(String functionName,
113+
InvokableFunction0<E> function) {
114+
return new ConnectorInvokableFunction<>(functionName, 0) {
115115
@Override
116-
public String invoke(List<String> args) throws JsonProcessingException {
116+
public String invoke(List<String> args) throws E {
117117
return function.apply();
118118
}
119119
};
120120
}
121121

122-
public static ConnectorInvokableFunction fromInvokableFunction(String functionName,
123-
InvokableFunction1 function) {
124-
return new ConnectorInvokableFunction(functionName, 1) {
122+
public static <E extends Exception> ConnectorInvokableFunction<E> fromInvokableFunction(String functionName,
123+
InvokableFunction1<E> function) {
124+
return new ConnectorInvokableFunction<>(functionName, 1) {
125125
@Override
126-
protected String invoke(List<String> args) {
126+
protected String invoke(List<String> args) throws E {
127127
return function.apply(args.get(0));
128128
}
129129
};
130130
}
131131

132-
public static ConnectorInvokableFunction fromInvokableFunction(String functionName,
133-
InvokableFunction2 function) {
134-
return new ConnectorInvokableFunction(functionName, 2) {
132+
public static <E extends Exception> ConnectorInvokableFunction<E> fromInvokableFunction(String functionName,
133+
InvokableFunction2<E> function) {
134+
return new ConnectorInvokableFunction<>(functionName, 2) {
135135
@Override
136-
protected String invoke(List<String> args) {
136+
protected String invoke(List<String> args) throws E {
137137
return function.apply(
138138
args.get(0),
139139
args.get(1)
@@ -142,11 +142,11 @@ protected String invoke(List<String> args) {
142142
};
143143
}
144144

145-
public static ConnectorInvokableFunction fromInvokableFunction(String functionName,
146-
InvokableFunction3 function) {
147-
return new ConnectorInvokableFunction(functionName, 3) {
145+
public static <E extends Exception> ConnectorInvokableFunction<E> fromInvokableFunction(String functionName,
146+
InvokableFunction3<E> function) {
147+
return new ConnectorInvokableFunction<>(functionName, 3) {
148148
@Override
149-
protected String invoke(List<String> args) {
149+
protected String invoke(List<String> args) throws E {
150150
return function.apply(
151151
args.get(0),
152152
args.get(1),
@@ -156,11 +156,11 @@ protected String invoke(List<String> args) {
156156
};
157157
}
158158

159-
public static ConnectorInvokableFunction fromInvokableFunction(String functionName,
160-
InvokableFunction4 function) {
161-
return new ConnectorInvokableFunction(functionName, 4) {
159+
public static <E extends Exception> ConnectorInvokableFunction<E> fromInvokableFunction(String functionName,
160+
InvokableFunction4<E> function) {
161+
return new ConnectorInvokableFunction<>(functionName, 4) {
162162
@Override
163-
protected String invoke(List<String> args) {
163+
protected String invoke(List<String> args) throws E {
164164
return function.apply(
165165
args.get(0),
166166
args.get(1),
@@ -171,11 +171,11 @@ protected String invoke(List<String> args) {
171171
};
172172
}
173173

174-
public static ConnectorInvokableFunction fromInvokableFunction(String functionName,
175-
InvokableFunction5 function) {
176-
return new ConnectorInvokableFunction(functionName, 5) {
174+
public static <E extends Exception> ConnectorInvokableFunction<E> fromInvokableFunction(String functionName,
175+
InvokableFunction5<E> function) {
176+
return new ConnectorInvokableFunction<>(functionName, 5) {
177177
@Override
178-
protected String invoke(List<String> args) {
178+
protected String invoke(List<String> args) throws E {
179179
return function.apply(
180180
args.get(0),
181181
args.get(1),
@@ -187,11 +187,11 @@ protected String invoke(List<String> args) {
187187
};
188188
}
189189

190-
public static ConnectorInvokableFunction fromInvokableFunction(String functionName,
191-
InvokableFunction6 function) {
192-
return new ConnectorInvokableFunction(functionName, 6) {
190+
public static <E extends Exception> ConnectorInvokableFunction<E> fromInvokableFunction(String functionName,
191+
InvokableFunction6<E> function) {
192+
return new ConnectorInvokableFunction<>(functionName, 6) {
193193
@Override
194-
protected String invoke(List<String> args) {
194+
protected String invoke(List<String> args) throws E {
195195
return function.apply(
196196
args.get(0),
197197
args.get(1),
@@ -201,14 +201,18 @@ protected String invoke(List<String> args) {
201201
args.get(5)
202202
);
203203
}
204-
};
204+
}
205+
206+
;
205207
}
206208

207-
public static ConnectorInvokableFunction fromInvokableFunction(String functionName,
208-
InvokableFunction7 function) {
209-
return new ConnectorInvokableFunction(functionName, 7) {
209+
public static <E extends
210+
Exception> ConnectorInvokableFunction<E> fromInvokableFunction(String functionName,
211+
InvokableFunction7<E> function
212+
) {
213+
return new ConnectorInvokableFunction<>(functionName, 7) {
210214
@Override
211-
protected String invoke(List<String> args) {
215+
protected String invoke(List<String> args) throws E {
212216
return function.apply(
213217
args.get(0),
214218
args.get(1),

packages/dsl-java/src/test/java/io/contract_testing/contractcase/documentation/Verification.java

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

33
import com.fasterxml.jackson.core.JsonProcessingException;
44
import com.fasterxml.jackson.databind.ObjectMapper;
5-
import io.contract_testing.contractcase.configuration.ContractCaseConfig.ContractCaseConfigBuilder;
65
import io.contract_testing.contractcase.ContractVerifier;
6+
import io.contract_testing.contractcase.configuration.ContractCaseConfig.ContractCaseConfigBuilder;
77
import io.contract_testing.contractcase.configuration.InvokableFunctions.InvokableFunction1;
88
import io.contract_testing.contractcase.configuration.PublishType;
99
import io.contract_testing.contractcase.configuration.StateHandler;
@@ -81,7 +81,7 @@ public void testVerify() throws InterruptedException {
8181

8282
}
8383

84-
private static @NotNull <R> InvokableFunction1
84+
private static @NotNull <R> InvokableFunction1<?>
8585
convertJsonIntegerArg(Function<Integer, R> functionUnderTest) {
8686
return (String a) -> {
8787
try {
@@ -94,7 +94,7 @@ public void testVerify() throws InterruptedException {
9494
}
9595

9696
@NotNull
97-
private static <R> InvokableFunction1
97+
private static <R> InvokableFunction1<?>
9898
convertJsonStringArgs(Function<String, R> functionUnderTest) {
9999
return (String a) -> {
100100
try {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package io.contract_testing.contractcase.test.function.verification;
22

3-
public class CustomException extends RuntimeException {
3+
public class CustomException extends Exception {
44

55
public CustomException(String message) {
66
super(message);

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import io.contract_testing.contractcase.ContractVerifier;
66
import io.contract_testing.contractcase.configuration.ContractCaseConfig.ContractCaseConfigBuilder;
77
import io.contract_testing.contractcase.configuration.InvokableFunctions.InvokableFunction1;
8-
import io.contract_testing.contractcase.configuration.LogLevel;
98
import io.contract_testing.contractcase.configuration.PublishType;
109
import io.contract_testing.contractcase.configuration.StateHandler;
1110
import io.contract_testing.contractcase.test.function.FunctionCallerExampleTest.FirstLayer;
@@ -28,7 +27,7 @@ public void testVerifyRunsGlobalStateHandlers() {
2827
ContractCaseConfigBuilder.aContractCaseConfig()
2928
.consumerName("Java Function Caller Example")
3029
.providerName("Java Function Implementer Example")
31-
// .logLevel(LogLevel.MAINTAINER_DEBUG)
30+
// .logLevel(LogLevel.MAINTAINER_DEBUG)
3231
.publish(PublishType.NEVER)
3332
.printResults(true)
3433
.throwOnFail(true)
@@ -63,13 +62,15 @@ public void testVerifyRunsGlobalStateHandlers() {
6362
convertJsonStringArgs((String key) -> mockedStore.get(key))
6463
);
6564

66-
6765
contract.registerFunction(
6866
"throwingFunction",
69-
convertJsonStringArgs((String key) ->{ throw new CustomException();})
67+
convertJsonStringArgs((String key) -> {
68+
throw new CustomException();
69+
})
7070
);
7171

72-
contract.registerFunction("complexReturn",
72+
contract.registerFunction(
73+
"complexReturn",
7374
convertJsonIntegerArg(
7475
(Integer v) ->
7576
new FirstLayer(
@@ -95,8 +96,8 @@ public void testVerifyRunsGlobalStateHandlers() {
9596
}
9697

9798
@NotNull
98-
private static <R> InvokableFunction1
99-
convertJsonStringArgs(Function<String, R> functionUnderTest) {
99+
private static <R, E extends Exception> InvokableFunction1<E>
100+
convertJsonStringArgs(InvokableFunction1<E> functionUnderTest) {
100101
return (String a) -> {
101102
try {
102103
var arg1 = mapper.readValue(a, String.class);

0 commit comments

Comments
 (0)