Skip to content

Commit 4654158

Browse files
author
Henry Harris
committed
add endpoint service exception test util
1 parent 9c642df commit 4654158

File tree

4 files changed

+144
-6
lines changed

4 files changed

+144
-6
lines changed

test-utils/src/main/java/com/palantir/conjure/java/api/testing/Assertions.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.palantir.conjure.java.api.testing;
1818

19+
import com.palantir.conjure.java.api.errors.EndpointServiceException;
1920
import com.palantir.conjure.java.api.errors.QosException;
2021
import com.palantir.conjure.java.api.errors.RemoteException;
2122
import com.palantir.conjure.java.api.errors.ServiceException;
@@ -32,6 +33,10 @@ public static ServiceExceptionAssert assertThat(ServiceException actual) {
3233
return new ServiceExceptionAssert(actual);
3334
}
3435

36+
public static ServiceExceptionAssert assertThat(EndpointServiceException actual) {
37+
return new ServiceExceptionAssert(actual);
38+
}
39+
3540
public static RemoteExceptionAssert assertThat(RemoteException actual) {
3641
return new RemoteExceptionAssert(actual);
3742
}
@@ -45,6 +50,12 @@ public static ServiceExceptionAssert assertThatServiceExceptionThrownBy(Throwing
4550
return assertThatThrownBy(shouldRaiseThrowable).asInstanceOf(ServiceExceptionAssert.instanceOfAssertFactory());
4651
}
4752

53+
@CanIgnoreReturnValue
54+
public static ServiceExceptionAssert assertThatEndpointServiceExceptionThrownBy(
55+
ThrowingCallable shouldRaiseThrowable) {
56+
return assertThatThrownBy(shouldRaiseThrowable).asInstanceOf(ServiceExceptionAssert.instanceOfAssertFactory());
57+
}
58+
4859
@CanIgnoreReturnValue
4960
public static RemoteExceptionAssert assertThatRemoteExceptionThrownBy(ThrowingCallable shouldRaiseThrowable) {
5061
return assertThatThrownBy(shouldRaiseThrowable).asInstanceOf(RemoteExceptionAssert.instanceOfAssertFactory());

test-utils/src/main/java/com/palantir/conjure/java/api/testing/ServiceExceptionAssert.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
package com.palantir.conjure.java.api.testing;
1818

19+
import com.palantir.conjure.java.api.errors.BaseServiceException;
1920
import com.palantir.conjure.java.api.errors.ErrorType;
20-
import com.palantir.conjure.java.api.errors.ServiceException;
2121
import com.palantir.logsafe.Arg;
2222
import java.util.Arrays;
2323
import java.util.HashMap;
@@ -28,16 +28,17 @@
2828
import org.assertj.core.api.InstanceOfAssertFactory;
2929
import org.assertj.core.util.Throwables;
3030

31-
public class ServiceExceptionAssert extends AbstractThrowableAssert<ServiceExceptionAssert, ServiceException> {
31+
public class ServiceExceptionAssert extends AbstractThrowableAssert<ServiceExceptionAssert, BaseServiceException> {
3232

33-
private static final InstanceOfAssertFactory<ServiceException, ServiceExceptionAssert> INSTANCE_OF_ASSERT_FACTORY =
34-
new InstanceOfAssertFactory<>(ServiceException.class, ServiceExceptionAssert::new);
33+
private static final InstanceOfAssertFactory<BaseServiceException, ServiceExceptionAssert>
34+
INSTANCE_OF_ASSERT_FACTORY =
35+
new InstanceOfAssertFactory<>(BaseServiceException.class, ServiceExceptionAssert::new);
3536

36-
ServiceExceptionAssert(ServiceException actual) {
37+
ServiceExceptionAssert(BaseServiceException actual) {
3738
super(actual, ServiceExceptionAssert.class);
3839
}
3940

40-
public static InstanceOfAssertFactory<ServiceException, ServiceExceptionAssert> instanceOfAssertFactory() {
41+
public static InstanceOfAssertFactory<BaseServiceException, ServiceExceptionAssert> instanceOfAssertFactory() {
4142
return INSTANCE_OF_ASSERT_FACTORY;
4243
}
4344

test-utils/src/test/java/com/palantir/conjure/java/api/testing/AssertionsTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616

1717
package com.palantir.conjure.java.api.testing;
1818

19+
import static com.palantir.conjure.java.api.testing.Assertions.assertThatEndpointServiceExceptionThrownBy;
1920
import static com.palantir.conjure.java.api.testing.Assertions.assertThatQosExceptionThrownBy;
2021
import static com.palantir.conjure.java.api.testing.Assertions.assertThatRemoteExceptionThrownBy;
2122
import static com.palantir.conjure.java.api.testing.Assertions.assertThatServiceExceptionThrownBy;
2223
import static com.palantir.conjure.java.api.testing.Assertions.assertThatThrownBy;
2324

25+
import com.palantir.conjure.java.api.errors.EndpointServiceException;
2426
import com.palantir.conjure.java.api.errors.ErrorType;
2527
import com.palantir.conjure.java.api.errors.QosException;
2628
import com.palantir.conjure.java.api.errors.QosReason;
@@ -58,6 +60,33 @@ public void testAssertThatServiceExceptionThrownBy_catchesServiceException() {
5860
.hasType(ErrorType.INTERNAL);
5961
}
6062

63+
@Test
64+
public void testAssertThatEndpointServiceExceptionThrownBy_failsIfNothingThrown() {
65+
assertThatThrownBy(() -> assertThatEndpointServiceExceptionThrownBy(() -> {
66+
// Not going to throw anything
67+
}))
68+
.hasMessageContaining("Expecting code to raise a throwable.");
69+
}
70+
71+
@Test
72+
public void testAssertThatEndpointServiceExceptionThrownBy_failsIfWrongExceptionThrown() {
73+
assertThatThrownBy(() -> assertThatEndpointServiceExceptionThrownBy(() -> {
74+
throw new RuntimeException("My message");
75+
}))
76+
.hasMessageContaining(
77+
"com.palantir.conjure.java.api.errors.EndpointServiceException",
78+
"java.lang.RuntimeException",
79+
"My message");
80+
}
81+
82+
@Test
83+
public void testAssertThatEndpointServiceExceptionThrownBy_catchesEndpointServiceException() {
84+
assertThatEndpointServiceExceptionThrownBy(() -> {
85+
throw new EndpointServiceException(ErrorType.INTERNAL) {};
86+
})
87+
.hasType(ErrorType.INTERNAL);
88+
}
89+
6190
@Test
6291
public void testAssertThatRemoteExceptionThrownBy_failsIfNothingThrown() {
6392
assertThatThrownBy(() -> assertThatRemoteExceptionThrownBy(() -> {
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* (c) Copyright 2017 Palantir Technologies Inc. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.palantir.conjure.java.api.testing;
18+
19+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
20+
21+
import com.palantir.conjure.java.api.errors.EndpointServiceException;
22+
import com.palantir.conjure.java.api.errors.ErrorType;
23+
import com.palantir.logsafe.SafeArg;
24+
import com.palantir.logsafe.UnsafeArg;
25+
import org.junit.jupiter.api.Test;
26+
27+
public class EndpointServiceExceptionAssertTest {
28+
29+
@Test
30+
public void testSanity() {
31+
ErrorType actualType = ErrorType.FAILED_PRECONDITION;
32+
33+
Assertions.assertThat(new EndpointServiceException(actualType, SafeArg.of("a", "b"), UnsafeArg.of("c", "d")))
34+
.hasCode(actualType.code())
35+
.hasType(actualType)
36+
.hasArgs(SafeArg.of("a", "b"), UnsafeArg.of("c", "d"));
37+
38+
Assertions.assertThat(new EndpointServiceException(actualType, SafeArg.of("a", "b"), UnsafeArg.of("c", "d")))
39+
.hasCode(actualType.code())
40+
.hasType(actualType)
41+
.hasArgs(UnsafeArg.of("c", "d"), SafeArg.of("a", "b")); // Order doesn't matter
42+
43+
Assertions.assertThat(new EndpointServiceException(actualType)).hasNoArgs();
44+
45+
assertThatThrownBy(() ->
46+
Assertions.assertThat(new EndpointServiceException(actualType)).hasCode(ErrorType.Code.INTERNAL))
47+
.isInstanceOf(AssertionError.class)
48+
.hasMessageContaining(
49+
"Expected ErrorType.Code to be %s, but found %s", ErrorType.Code.INTERNAL, actualType.code());
50+
51+
assertThatThrownBy(() ->
52+
Assertions.assertThat(new EndpointServiceException(actualType)).hasType(ErrorType.INTERNAL))
53+
.isInstanceOf(AssertionError.class)
54+
.hasMessageContaining("Expected ErrorType to be %s, but found %s", ErrorType.INTERNAL, actualType);
55+
56+
assertThatThrownBy(() -> Assertions.assertThat(new EndpointServiceException(actualType, SafeArg.of("a", "b")))
57+
.hasArgs(SafeArg.of("c", "d")))
58+
.isInstanceOf(AssertionError.class)
59+
.hasMessageContaining("Expected safe args to be {c=d}, but found {a=b}");
60+
61+
assertThatThrownBy(() -> Assertions.assertThat(new EndpointServiceException(actualType, UnsafeArg.of("a", "b")))
62+
.hasArgs(UnsafeArg.of("c", "d")))
63+
.isInstanceOf(AssertionError.class)
64+
.hasMessageContaining("Expected unsafe args to be {c=d}, but found {a=b}");
65+
66+
assertThatThrownBy(() -> Assertions.assertThat(new EndpointServiceException(actualType, SafeArg.of("a", "b")))
67+
.hasNoArgs())
68+
.isInstanceOf(AssertionError.class)
69+
.hasMessageContaining("Expected no args, but found {a=b}");
70+
71+
assertThatThrownBy(() -> Assertions.assertThat(
72+
new EndpointServiceException(actualType, SafeArg.of("a", "b"), UnsafeArg.of("c", "d")))
73+
.hasNoArgs())
74+
.isInstanceOf(AssertionError.class)
75+
.hasMessageContaining("Expected no args, but found {a=b, c=d}");
76+
77+
Assertions.assertThat(new EndpointServiceException(actualType, UnsafeArg.of("a", "b"), UnsafeArg.of("c", "d")))
78+
.containsArgs(UnsafeArg.of("a", "b"));
79+
80+
// Safety matters
81+
assertThatThrownBy(() -> Assertions.assertThat(
82+
new EndpointServiceException(actualType, SafeArg.of("a", "b"), UnsafeArg.of("c", "d")))
83+
.containsArgs(UnsafeArg.of("a", "b")))
84+
.isInstanceOf(AssertionError.class)
85+
.hasMessageContaining("Expected unsafe args to contain {a=b}, but found {c=d}");
86+
87+
assertThatThrownBy(() -> Assertions.assertThat(new EndpointServiceException(actualType, SafeArg.of("a", "b")))
88+
.containsArgs(SafeArg.of("c", "d")))
89+
.isInstanceOf(AssertionError.class)
90+
.hasMessageContaining("Expected safe args to contain {c=d}, but found {a=b}");
91+
92+
assertThatThrownBy(() -> Assertions.assertThat(new EndpointServiceException(actualType, UnsafeArg.of("a", "b")))
93+
.containsArgs(UnsafeArg.of("c", "d")))
94+
.isInstanceOf(AssertionError.class)
95+
.hasMessageContaining("Expected unsafe args to contain {c=d}, but found {a=b}");
96+
}
97+
}

0 commit comments

Comments
 (0)