-
Couldn't load subscription status.
- Fork 33
Add endpoint service exception test util #1366
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dummycode
wants to merge
4
commits into
develop
Choose a base branch
from
hh/endpoint-service-exception-test-util
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| type: improvement | ||
| improvement: | ||
| description: Add endpoint service exception test util | ||
| links: | ||
| - https://github.com/palantir/conjure-java-runtime-api/pull/1366 |
56 changes: 56 additions & 0 deletions
56
test-utils/src/main/java/com/palantir/conjure/java/api/testing/AssertableArgs.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| /* | ||
| * (c) Copyright 2025 Palantir Technologies Inc. All rights reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.palantir.conjure.java.api.testing; | ||
|
|
||
| import com.palantir.logsafe.Arg; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Utility to collect safe and unsafe arguments into separate collections and assert unique entries in each. | ||
| */ | ||
| record AssertableArgs(Map<String, Object> safeArgs, Map<String, Object> unsafeArgs) { | ||
|
|
||
| AssertableArgs(List<Arg<?>> args) { | ||
| this(new HashMap<>(), new HashMap<>()); | ||
|
|
||
| args.forEach(arg -> { | ||
| if (arg.isSafeForLogging()) { | ||
| assertPutSafe(arg); | ||
| } else { | ||
| assertPutUnsafe(arg); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| private void assertPutSafe(Arg<?> arg) { | ||
| assertPut(safeArgs, arg.getName(), arg.getValue(), "safe"); | ||
| } | ||
|
|
||
| private void assertPutUnsafe(Arg<?> arg) { | ||
| assertPut(unsafeArgs, arg.getName(), arg.getValue(), "unsafe"); | ||
| } | ||
|
|
||
| private static void assertPut(Map<String, Object> map, String key, Object value, String name) { | ||
| Object previous = map.put(key, value); | ||
| if (previous != null) { | ||
| throw new AssertionError(String.format( | ||
| "Duplicate %s arg name '%s', first value: %s, second value: %s", name, key, previous, value)); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
...s/src/main/java/com/palantir/conjure/java/api/testing/EndpointServiceExceptionAssert.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| /* | ||
| * (c) Copyright 2025 Palantir Technologies Inc. All rights reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.palantir.conjure.java.api.testing; | ||
|
|
||
| import com.palantir.conjure.java.api.errors.EndpointServiceException; | ||
| import com.palantir.conjure.java.api.errors.ErrorType; | ||
| import com.palantir.logsafe.Arg; | ||
| import java.util.Arrays; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import org.assertj.core.api.AbstractThrowableAssert; | ||
| import org.assertj.core.api.InstanceOfAssertFactory; | ||
| import org.assertj.core.util.Throwables; | ||
|
|
||
| public class EndpointServiceExceptionAssert | ||
| extends AbstractThrowableAssert<EndpointServiceExceptionAssert, EndpointServiceException> { | ||
|
|
||
| private static final InstanceOfAssertFactory<EndpointServiceException, EndpointServiceExceptionAssert> | ||
| INSTANCE_OF_ASSERT_FACTORY = | ||
| new InstanceOfAssertFactory<>(EndpointServiceException.class, EndpointServiceExceptionAssert::new); | ||
|
|
||
| EndpointServiceExceptionAssert(EndpointServiceException actual) { | ||
| super(actual, EndpointServiceExceptionAssert.class); | ||
| } | ||
|
|
||
| public static InstanceOfAssertFactory<EndpointServiceException, EndpointServiceExceptionAssert> | ||
| instanceOfAssertFactory() { | ||
| return INSTANCE_OF_ASSERT_FACTORY; | ||
| } | ||
|
|
||
| public final EndpointServiceExceptionAssert hasCode(ErrorType.Code code) { | ||
| isNotNull(); | ||
| failIfNotEqual( | ||
| "Expected ErrorType.Code to be %s, but found %s", | ||
| code, actual.getErrorType().code()); | ||
| return this; | ||
| } | ||
|
|
||
| public final EndpointServiceExceptionAssert hasType(ErrorType type) { | ||
| isNotNull(); | ||
| failIfNotEqual("Expected ErrorType to be %s, but found %s", type, actual.getErrorType()); | ||
| return this; | ||
| } | ||
|
|
||
| public final EndpointServiceExceptionAssert hasArgs(Arg<?>... args) { | ||
| isNotNull(); | ||
|
|
||
| AssertableArgs actualArgs = new AssertableArgs(actual.getArgs()); | ||
| AssertableArgs expectedArgs = new AssertableArgs(Arrays.asList(args)); | ||
|
|
||
| failIfNotEqual("Expected safe args to be %s, but found %s", expectedArgs.safeArgs(), actualArgs.safeArgs()); | ||
| failIfNotEqual( | ||
| "Expected unsafe args to be %s, but found %s", expectedArgs.unsafeArgs(), actualArgs.unsafeArgs()); | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| public final EndpointServiceExceptionAssert hasNoArgs() { | ||
| isNotNull(); | ||
|
|
||
| AssertableArgs actualArgs = new AssertableArgs(actual.getArgs()); | ||
| if (!actualArgs.safeArgs().isEmpty() || !actualArgs.unsafeArgs().isEmpty()) { | ||
| Map<String, Object> allArgs = new HashMap<>(); | ||
| allArgs.putAll(actualArgs.safeArgs()); | ||
| allArgs.putAll(actualArgs.unsafeArgs()); | ||
| failWithMessage( | ||
| "Expected no args, but found %s; service exception: %s", allArgs, Throwables.getStackTrace(actual)); | ||
| } | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| private <T> void failIfNotEqual(String message, T expectedValue, T actualValue) { | ||
| if (!Objects.equals(expectedValue, actualValue)) { | ||
| failWithMessage( | ||
| message + "; service exception: ", expectedValue, actualValue, Throwables.getStackTrace(actual)); | ||
| } | ||
| } | ||
|
|
||
| public final EndpointServiceExceptionAssert containsArgs(Arg<?>... args) { | ||
| isNotNull(); | ||
|
|
||
| AssertableArgs actualArgs = new AssertableArgs(actual.getArgs()); | ||
| AssertableArgs expectedArgs = new AssertableArgs(Arrays.asList(args)); | ||
|
|
||
| failIfDoesNotContain( | ||
| "Expected safe args to contain %s, but found %s", expectedArgs.safeArgs(), actualArgs.safeArgs()); | ||
| failIfDoesNotContain( | ||
| "Expected unsafe args to contain %s, but found %s", expectedArgs.unsafeArgs(), actualArgs.unsafeArgs()); | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| private void failIfDoesNotContain( | ||
| String message, Map<String, Object> expectedArgs, Map<String, Object> actualArgs) { | ||
| if (!actualArgs.entrySet().containsAll(expectedArgs.entrySet())) { | ||
| failWithMessage( | ||
| message + "; service exception: %s", expectedArgs, actualArgs, Throwables.getStackTrace(actual)); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mutable records is sort of an anti-pattern. I would just make this a normal class.
IMO this class just feels like unnecessary indirection. We have a class that contains two fields, only to ever consume each field independently.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ack, would revert back to normal class. I just extracted this from the existing class, but happy to tweak things.
@mpritham is going to take this over since they implemented the new error type. It was suggested the two exceptions should probably share an interface or ancestor so we can de-duplicate a lot of this logic.