Skip to content
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

Add Equals and Hashcode to ApiErrorBase / ApiErrorWithMetadata #26

Merged
merged 1 commit into from
Oct 25, 2017

Conversation

rabeyta
Copy link
Contributor

@rabeyta rabeyta commented Oct 24, 2017

Add equals and hashcode methods to ApiErrorBase and ApiErrorWithMetadata. Modify ApiErrorComparator to use new comparison and sort by ErrorCode after Name.

@codecov-io
Copy link

codecov-io commented Oct 24, 2017

Codecov Report

Merging #26 into master will increase coverage by 0.13%.
The diff coverage is 100%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master      #26      +/-   ##
============================================
+ Coverage     95.14%   95.27%   +0.13%     
- Complexity      722      739      +17     
============================================
  Files            70       71       +1     
  Lines          2018     2033      +15     
  Branches        324      331       +7     
============================================
+ Hits           1920     1937      +17     
+ Misses           60       59       -1     
+ Partials         38       37       -1
Impacted Files Coverage Δ Complexity Δ
.../nike/backstopper/apierror/ApiErrorComparator.java 100% <100%> (ø) 7 <6> (ø) ⬇️
...n/java/com/nike/backstopper/util/ApiErrorUtil.java 100% <100%> (ø) 12 <12> (?)
...va/com/nike/backstopper/apierror/ApiErrorBase.java 100% <100%> (+6.66%) 17 <2> (+3) ⬆️
...ike/backstopper/apierror/ApiErrorWithMetadata.java 100% <100%> (ø) 16 <2> (+2) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b0f21db...1517d94. Read the comment docs.

@Test
@DataProvider(value = {
"false | false | false | false | false | true ",
"false | true | false | false | false | false ",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing the case where the first arg is true and everything else is false.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good eyes.added!

doReturn(name).when(mockApiError1).getName();
doReturn(name).when(mockApiError2).getName();

String errorCode1 = "1";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"1" vs "2" isn't definitive enough I think. I'd just make errorCode1 and errorCode2 some random UUIDs. Should give higher confidence that the test is verifying what we think it's verifying. You also might want to include some verify(mockApiError1).getErrorCode() to verify that getErrorCode() is being called on both ApiErrors.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made those suggestions.


/**
* A comparator that knows how to compare {@link ApiError} instances by {@link ApiError#getName()} first, and then by
* {@link ApiError#getMetadata()} to allow for multiple same-named errors that have different metadata.
* {@link ApiError#getErrorCode()} ()} to allow for multiple same-named errors that have different metadata.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Semi-incomplete javadoc update. I'd recommend:

A comparator that knows how to compare {@link ApiError} instances by {@link ApiError#getName()} first, 
then by {@link ApiError#getErrorCode()}, and finally by everything else by comparing hashcodes using 
{@link ApiErrorUtil#generateApiErrorHashCode(ApiError)}. 

<p>Note that this means two {@link ApiError}s that are identical other than metadata will be considered
different by this comparator.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

import java.util.Objects;

/**
* Utility class to enable sharing code between {@link ApiError} implementations. This probably won't be used outside
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd recommend a slightly different javadoc:

Utility class to enable sharing code between {@link ApiError} implementations. Most Backstopper end-users
won't need to use this class unless you're creating custom {@link ApiError} implementations (which is not 
common).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

new ApiErrorBase(null, 42, "some error", 400);
}

@Test(expected = IllegalArgumentException.class)
public void constructor_should_throw_IllegalArgumentException_null_error_code() {
new ApiErrorBase(null, null, "some error", 400, null);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The IllegalArgumentException being thrown here is likely due to the null name, not the null error code you're trying to test. I'd recommend switching from @Test(expected = ...) to AssertJ's catchThrowable and verify the message so you know the exception was thrown for the desired reason.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i've changed this test and the other in the class to use catchThrowable

@Test
@DataProvider(value = {
"false | false | false | false | false | true | true ",
"false | true | false | false | false | false | false ",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same missing data provider row.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good eyes. added.


@Test
public void generate_api_error_hashcode_generates_expected_hashcodes() {
Map<String, Object> metadata = MapBuilder.<String, Object>builder().put("foo", UUID.randomUUID().toString()).build();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would duplicate the Objects.hash(...) here in the test and verify that the hashcode returned by the method matches the expected Objects.hash(...) value. It's a bit of a tautology, but works as a sanity check and can catch if the method gets updated in the future to make sure we take a step back and ask if it's a change we really want.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good call. i've updated

@Test
@DataProvider(value = {
"false | false | false | false | false | true ",
"false | true | false | false | false | false ",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same missing data provider row.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good eyes. fixed.

@rabeyta rabeyta force-pushed the apiErrorEquals branch 2 times, most recently from 1e1f22a to af5fcc0 Compare October 25, 2017 19:15
…ata. Modify ApiErrorComparator to use new comparison and sort by ErrorCode after Name
@nicmunroe
Copy link
Member

Fantastic - thank you @rabeyta!

@nicmunroe nicmunroe merged commit 719aa32 into Nike-Inc:master Oct 25, 2017
@rabeyta rabeyta deleted the apiErrorEquals branch October 25, 2017 19:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants