|
| 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