1818
1919import static org .assertj .core .api .Assertions .assertThatThrownBy ;
2020
21+ import com .palantir .conjure .java .api .errors .EndpointServiceException ;
2122import com .palantir .conjure .java .api .errors .ErrorType ;
2223import com .palantir .conjure .java .api .errors .ServiceException ;
24+ import com .palantir .logsafe .Arg ;
2325import com .palantir .logsafe .SafeArg ;
2426import com .palantir .logsafe .UnsafeArg ;
2527import org .junit .jupiter .api .Test ;
2628
2729public class ServiceExceptionAssertTest {
2830
31+ private static class TestEndpointServiceException extends EndpointServiceException {
32+ TestEndpointServiceException (ErrorType errorType , Arg <?>... safeArgs ) {
33+ super (errorType , safeArgs );
34+ }
35+ }
36+
2937 @ Test
3038 public void testSanity () {
3139 ErrorType actualType = ErrorType .FAILED_PRECONDITION ;
@@ -35,63 +43,123 @@ public void testSanity() {
3543 .hasType (actualType )
3644 .hasArgs (SafeArg .of ("a" , "b" ), UnsafeArg .of ("c" , "d" ));
3745
46+ Assertions .assertThat (
47+ new TestEndpointServiceException (actualType , SafeArg .of ("a" , "b" ), UnsafeArg .of ("c" , "d" )))
48+ .hasCode (actualType .code ())
49+ .hasType (actualType )
50+ .hasArgs (SafeArg .of ("a" , "b" ), UnsafeArg .of ("c" , "d" ));
51+
3852 Assertions .assertThat (new ServiceException (actualType , SafeArg .of ("a" , "b" ), UnsafeArg .of ("c" , "d" )))
3953 .hasCode (actualType .code ())
4054 .hasType (actualType )
4155 .hasArgs (UnsafeArg .of ("c" , "d" ), SafeArg .of ("a" , "b" )); // Order doesn't matter
4256
57+ Assertions .assertThat (
58+ new TestEndpointServiceException (actualType , SafeArg .of ("a" , "b" ), UnsafeArg .of ("c" , "d" )))
59+ .hasCode (actualType .code ())
60+ .hasType (actualType )
61+ .hasArgs (UnsafeArg .of ("c" , "d" ), SafeArg .of ("a" , "b" )); // Order doesn't matter
62+
4363 Assertions .assertThat (new ServiceException (actualType )).hasNoArgs ();
64+ Assertions .assertThat (new TestEndpointServiceException (actualType )).hasNoArgs ();
4465
4566 assertThatThrownBy (() ->
4667 Assertions .assertThat (new ServiceException (actualType )).hasCode (ErrorType .Code .INTERNAL ))
4768 .isInstanceOf (AssertionError .class )
4869 .hasMessageContaining (
4970 "Expected ErrorType.Code to be %s, but found %s" , ErrorType .Code .INTERNAL , actualType .code ());
71+ assertThatThrownBy (() -> Assertions .assertThat (new TestEndpointServiceException (actualType ))
72+ .hasCode (ErrorType .Code .INTERNAL ))
73+ .isInstanceOf (AssertionError .class )
74+ .hasMessageContaining (
75+ "Expected ErrorType.Code to be %s, but found %s" , ErrorType .Code .INTERNAL , actualType .code ());
5076
5177 assertThatThrownBy (() ->
5278 Assertions .assertThat (new ServiceException (actualType )).hasType (ErrorType .INTERNAL ))
5379 .isInstanceOf (AssertionError .class )
5480 .hasMessageContaining ("Expected ErrorType to be %s, but found %s" , ErrorType .INTERNAL , actualType );
81+ assertThatThrownBy (() -> Assertions .assertThat (new TestEndpointServiceException (actualType ))
82+ .hasType (ErrorType .INTERNAL ))
83+ .isInstanceOf (AssertionError .class )
84+ .hasMessageContaining ("Expected ErrorType to be %s, but found %s" , ErrorType .INTERNAL , actualType );
5585
5686 assertThatThrownBy (() -> Assertions .assertThat (new ServiceException (actualType , SafeArg .of ("a" , "b" )))
5787 .hasArgs (SafeArg .of ("c" , "d" )))
5888 .isInstanceOf (AssertionError .class )
5989 .hasMessageContaining ("Expected safe args to be {c=d}, but found {a=b}" );
90+ assertThatThrownBy (
91+ () -> Assertions .assertThat (new TestEndpointServiceException (actualType , SafeArg .of ("a" , "b" )))
92+ .hasArgs (SafeArg .of ("c" , "d" )))
93+ .isInstanceOf (AssertionError .class )
94+ .hasMessageContaining ("Expected safe args to be {c=d}, but found {a=b}" );
6095
6196 assertThatThrownBy (() -> Assertions .assertThat (new ServiceException (actualType , UnsafeArg .of ("a" , "b" )))
6297 .hasArgs (UnsafeArg .of ("c" , "d" )))
6398 .isInstanceOf (AssertionError .class )
6499 .hasMessageContaining ("Expected unsafe args to be {c=d}, but found {a=b}" );
100+ assertThatThrownBy (() -> Assertions .assertThat (
101+ new TestEndpointServiceException (actualType , UnsafeArg .of ("a" , "b" )))
102+ .hasArgs (UnsafeArg .of ("c" , "d" )))
103+ .isInstanceOf (AssertionError .class )
104+ .hasMessageContaining ("Expected unsafe args to be {c=d}, but found {a=b}" );
65105
66106 assertThatThrownBy (() -> Assertions .assertThat (new ServiceException (actualType , SafeArg .of ("a" , "b" )))
67107 .hasNoArgs ())
68108 .isInstanceOf (AssertionError .class )
69109 .hasMessageContaining ("Expected no args, but found {a=b}" );
110+ assertThatThrownBy (
111+ () -> Assertions .assertThat (new TestEndpointServiceException (actualType , SafeArg .of ("a" , "b" )))
112+ .hasNoArgs ())
113+ .isInstanceOf (AssertionError .class )
114+ .hasMessageContaining ("Expected no args, but found {a=b}" );
70115
71116 assertThatThrownBy (() -> Assertions .assertThat (
72117 new ServiceException (actualType , SafeArg .of ("a" , "b" ), UnsafeArg .of ("c" , "d" )))
73118 .hasNoArgs ())
74119 .isInstanceOf (AssertionError .class )
75120 .hasMessageContaining ("Expected no args, but found {a=b, c=d}" );
121+ assertThatThrownBy (() -> Assertions .assertThat (new TestEndpointServiceException (
122+ actualType , SafeArg .of ("a" , "b" ), UnsafeArg .of ("c" , "d" )))
123+ .hasNoArgs ())
124+ .isInstanceOf (AssertionError .class )
125+ .hasMessageContaining ("Expected no args, but found {a=b, c=d}" );
76126
77127 Assertions .assertThat (new ServiceException (actualType , UnsafeArg .of ("a" , "b" ), UnsafeArg .of ("c" , "d" )))
78128 .containsArgs (UnsafeArg .of ("a" , "b" ));
129+ Assertions .assertThat (
130+ new TestEndpointServiceException (actualType , UnsafeArg .of ("a" , "b" ), UnsafeArg .of ("c" , "d" )))
131+ .containsArgs (UnsafeArg .of ("a" , "b" ));
79132
80133 // Safety matters
81134 assertThatThrownBy (() -> Assertions .assertThat (
82135 new ServiceException (actualType , SafeArg .of ("a" , "b" ), UnsafeArg .of ("c" , "d" )))
83136 .containsArgs (UnsafeArg .of ("a" , "b" )))
84137 .isInstanceOf (AssertionError .class )
85138 .hasMessageContaining ("Expected unsafe args to contain {a=b}, but found {c=d}" );
139+ assertThatThrownBy (() -> Assertions .assertThat (new TestEndpointServiceException (
140+ actualType , SafeArg .of ("a" , "b" ), UnsafeArg .of ("c" , "d" )))
141+ .containsArgs (UnsafeArg .of ("a" , "b" )))
142+ .isInstanceOf (AssertionError .class )
143+ .hasMessageContaining ("Expected unsafe args to contain {a=b}, but found {c=d}" );
86144
87145 assertThatThrownBy (() -> Assertions .assertThat (new ServiceException (actualType , SafeArg .of ("a" , "b" )))
88146 .containsArgs (SafeArg .of ("c" , "d" )))
89147 .isInstanceOf (AssertionError .class )
90148 .hasMessageContaining ("Expected safe args to contain {c=d}, but found {a=b}" );
149+ assertThatThrownBy (
150+ () -> Assertions .assertThat (new TestEndpointServiceException (actualType , SafeArg .of ("a" , "b" )))
151+ .containsArgs (SafeArg .of ("c" , "d" )))
152+ .isInstanceOf (AssertionError .class )
153+ .hasMessageContaining ("Expected safe args to contain {c=d}, but found {a=b}" );
91154
92155 assertThatThrownBy (() -> Assertions .assertThat (new ServiceException (actualType , UnsafeArg .of ("a" , "b" )))
93156 .containsArgs (UnsafeArg .of ("c" , "d" )))
94157 .isInstanceOf (AssertionError .class )
95158 .hasMessageContaining ("Expected unsafe args to contain {c=d}, but found {a=b}" );
159+ assertThatThrownBy (() -> Assertions .assertThat (
160+ new TestEndpointServiceException (actualType , UnsafeArg .of ("a" , "b" )))
161+ .containsArgs (UnsafeArg .of ("c" , "d" )))
162+ .isInstanceOf (AssertionError .class )
163+ .hasMessageContaining ("Expected unsafe args to contain {c=d}, but found {a=b}" );
96164 }
97165}
0 commit comments