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

Trunk 5452 - Adaption of equals for CohortMembership #2791

Merged
merged 1 commit into from
Dec 26, 2018

Conversation

fruether
Copy link
Contributor

Description of what I changed

In this function I added the equal method to CohortMembership accordingly to the ticket description (https://issues.openmrs.org/browse/TRUNK-5452). In addition, I added several test cases to CohortMembershipTest to prove that the method behaves in the intended way. A related talk topic can be found here: Talk Thread

Issue I worked on

see https://issues.openmrs.org/browse/TRUNK-5452

Checklist: I completed these to help reviewers :)

  • My pull request only contains ONE single commit
    (the number above, next to the 'Commits' tab is 1).

    No? -> read here on how to squash multiple commits into one

  • [x ] My IDE is configured to follow the code style of this project.

    No? Unsure? -> configure your IDE, format the code and add the changes with git add . && git commit --amend

  • [ x] I have added tests to cover my changes. (If you refactored
    existing code that was well tested you do not have to add tests)

    No? -> write tests and add them to this commit git add . && git commit --amend

  • [ x] I ran mvn clean package right before creating this pull request and
    added all formatting changes to my commit.

    No? -> execute above command

  • [ x] All new and existing tests passed.

    No? -> figure out why and add the fix to your commit. It is your responsibility to make sure your code works.

  • [x ] My pull request is based on the latest changes of the master branch.

    No? Unsure? -> execute command git pull --rebase upstream master

@coveralls
Copy link

coveralls commented Dec 18, 2018

Coverage Status

Coverage increased (+0.02%) to 59.368% when pulling cc7e9d6 on fruether:TRUNK-5452 into eb6c31c on openmrs:master.

@dkayiwa
Copy link
Member

dkayiwa commented Dec 18, 2018

Do you mind making the commit message better than simply "resolving ticket id"? In other words, from the commit message, i should tell what the commit is about, without having to first look at the ticket.

@fruether
Copy link
Contributor Author

@dkayiwa I adapted the commit message. In the past I always did it like this. THat's why I did not do it in the first place.

@dkayiwa
Copy link
Member

dkayiwa commented Dec 18, 2018

Can you have the commit message include the ticket id as advised at? https://wiki.openmrs.org/display/docs/Pull+Request+Tips

@fruether
Copy link
Contributor Author

@dkayiwa added message and issue number

public void equal_shouldReturnTrueIfObjectReferenceAreTheSame() {
CohortMembership cohortMembership = new CohortMembership(12);

boolean result = cohortMembership.equals(cohortMembership);
Copy link
Member

Choose a reason for hiding this comment

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

I guess we could just assert without the temporary variable.

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 thought it is more OpenMRS agree on style to first execute the function under test and then perform the assertion? Anyways I changed it

// A null argument to equal should not lead to an exception
CohortMembership cohortMembership = new CohortMembership(12);

boolean result = cohortMembership.equals(null);
Copy link
Member

Choose a reason for hiding this comment

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

I guess we could just assert without the temporary result variable.


/**
* Indicates if a given cohortMembership object is equal to this one
*
Copy link
Member

Choose a reason for hiding this comment

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

Did you just forget to add the @SInCE annotation?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. I just added it

Date startDate1 = new Date(1545140935);
Date endDate1 = new Date(1577836800);

Date startDate2 = new Date(1545140935);
Copy link
Member

Choose a reason for hiding this comment

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

I guess we can avoid duplicating the date value.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Are you sure? Because the issue is that then we may do not see if equal() of Date works as aspected

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Anyways I adapted it

Copy link
Member

Choose a reason for hiding this comment

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

i mean duplicating 1545140935

cohortMembershipTwo.setStartDate(startDate2);
cohortMembershipTwo.setEndDate(endDate2);

boolean result = cohortMembershipOne.equals(cohortMembershipTwo);
Copy link
Member

Choose a reason for hiding this comment

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

I guess we could just directly assert

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 did so in the new commit

cohortMembershipTwo.setStartDate(startDate2);
cohortMembershipTwo.setEndDate(endDate2);

boolean result = cohortMembershipOne.equals(cohortMembershipTwo);
Copy link
Member

Choose a reason for hiding this comment

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

I guess we could just directly assert

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 did so in the new commit

@fruether fruether force-pushed the TRUNK-5452 branch 2 times, most recently from 859a803 to 9293850 Compare December 20, 2018 09:16
public boolean equals(CohortMembership otherCohortMembership) {
if(this == otherCohortMembership) return true;

return otherCohortMembership != null && this.endDate.equals(otherCohortMembership.getEndDate()) && this.startDate.equals(otherCohortMembership.getStartDate())
Copy link
Member

Choose a reason for hiding this comment

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

What happens if any of these properties is null?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are right! I did not took this into account. I changed the code and added two new tests. Are two tests alright or should I add further test cases dealing with null values?

Copy link
Member

Choose a reason for hiding this comment

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

Those tests would not hurt. 😊

Copy link
Contributor Author

Choose a reason for hiding this comment

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

All possible cases should be tested now!

@fruether fruether force-pushed the TRUNK-5452 branch 3 times, most recently from b569d7c to 3464633 Compare December 20, 2018 11:46
if(this == otherCohortMembership) return true;

return otherCohortMembership != null &&
((endDate != null ) ? this.endDate.equals(otherCohortMembership.getEndDate()) : otherCohortMembership.getEndDate() == null)
Copy link
Member

Choose a reason for hiding this comment

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

Any reason why you sometimes use endDate and others you use this.endDate?
At first appearance, it looked as if endDate is a local variable different from this.endDate

Copy link
Contributor Author

Choose a reason for hiding this comment

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

"this." is not necessary since only the class attributes exist with this name (no local variable).
At first I thought it would make it more obvious that "this" class member is checked for equality with the attribute. But since the statement is already quite long to avoid NullPointerExceptions I think the better way to go is to remove the "this" in this method.

This was done in the new commit

@dkayiwa
Copy link
Member

dkayiwa commented Dec 20, 2018

@fruether
Copy link
Contributor Author

fruether commented Dec 20, 2018

@dkayiwa

@fruether is this of any help? http://www.javapractices.com/topic/TopicAction.do?Id=17

I am not sure if I got correctly what the intension of this link is.
Do you want to point out that hashCode() has to be implemented as well? If so I did not do this so far since the issue just said equal and I am not sure what the best way would be to implement hashCode so that is will work

Regarding the comparrission guideline: "possibly-null object fields : use both == and equals" since -> I used equals() but not explicitly ==. But this should not be a problem since equals() should check if both references are equal and the null object case is also taken care of

}

@Test
public void equal_shouldReturnTrueIfBothPatientIdIsNull() {
Copy link
Member

Choose a reason for hiding this comment

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

Should return true or 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.

Fixed it

}

@Test
public void equal_shouldReturnFalseIfEndDateIsNull() {
Copy link
Member

@dkayiwa dkayiwa Dec 21, 2018

Choose a reason for hiding this comment

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

The method name seems to suggest that this would return true if the end date was not null

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed it

}

@Test
public void equal_shouldReturnFalseIfStartDateIsNull() {
Copy link
Member

Choose a reason for hiding this comment

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

The method name seems to suggest that this would return true if start date was not null

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed it

}

@Test
public void equal_shouldReturnTrueIfObjectsAreTheSameIncludingNullValues() {
Copy link
Member

Choose a reason for hiding this comment

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

What does "including null values" mean?

Copy link
Contributor Author

@fruether fruether Dec 22, 2018

Choose a reason for hiding this comment

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

The thing is I want to distinguish the equal tests that contain Null values in their objects and that have all attributes set properly.

That is why I wanted to add a phrase to point out that the test actually tests if equal works when some attributes are null in both objects.

Which name do you think would make the most sense:

  1. equal_shouldWorkWhenNullValuesExistInObject()
  2. equal_shouldReturnTrueWhenBothPatientIdsAreNull()

Copy link
Member

Choose a reason for hiding this comment

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

The second one is more precise.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

}

@Test
public void equal_shouldReturnTrueWhenBothPatientIdsAreNull() {
Copy link
Member

Choose a reason for hiding this comment

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

Does this mean it will return true if both patient ids are null even if the other fields are different?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dkayiwa I corrected the name

}

@Test
public void equal_shouldReturnTrueIfAllDatesAreNull() {
Copy link
Member

Choose a reason for hiding this comment

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

Should return true if all dates are null even if the patients are different?

Copy link
Member

Choose a reason for hiding this comment

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

When you say "all dates" does it also include dateCreated, dateChanged and dateVoided?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dkayiwa I corrected the name

@openmrs openmrs deleted a comment Dec 23, 2018
CohortMembership cohortMembershipOne = new CohortMembership(13);
CohortMembership cohortMembershipTwo = new CohortMembership(12);

Date startDate1 = new Date(1577836801);
Copy link
Member

Choose a reason for hiding this comment

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

Should we duplicate 1577836801?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

}

@Test
public void equal_shouldReturnFalseIfOnlyOneStartDateIsNull() {
Copy link
Member

Choose a reason for hiding this comment

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

Should return false when one start date is null, even if all the other fields are the same? Or did you want to mean something else?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

equal_shouldReturnFalseIfOnlyStartDateOfOneObjectIsNull

}

@Test
public void equal_shouldReturnFalseIfOnlyOneEndDateIsNull() {
Copy link
Member

Choose a reason for hiding this comment

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

Should return false when end date is null, even if all the other fields are the same? Or did you want to mean something else?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Pointed out now that it is only False if end Date is null for one object:

equal_shouldReturnFalseIfOnlyEndDateOfOneObjectIsNull

@fruether fruether force-pushed the TRUNK-5452 branch 3 times, most recently from 767dcfe to 5cab65c Compare December 25, 2018 18:36
@openmrs openmrs deleted a comment Dec 25, 2018
CohortMembership cohortMembershipOne = new CohortMembership();
CohortMembership cohortMembershipTwo = new CohortMembership();

Date startDate1 = new Date(1545140935);
Copy link
Member

Choose a reason for hiding this comment

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

Do wee need to duplicate 1545140935?

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 will make a variable out of it. And yeah I think it is helpful for testing purposes to duplicate it

Copy link
Member

Choose a reason for hiding this comment

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

how would duplicating it be helpful?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The alternative would be to say: startDate2 = startDate1 correct?
But if I am doing so the objects point to the same area and the reference is already equal. I want to test/make sure that the equal() function of the Date type is executed. Hence to verify, if both dates have the same content. That is why I need to duplicate the value to create a new object which is equal but not the same

Copy link
Member

Choose a reason for hiding this comment

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

Assigning that value to a constant always helps to avoid the duplication 😊

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What do you mean? I can't follow your right now :(

Copy link
Member

Choose a reason for hiding this comment

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

If you wanted to change that value from 1545140935 to 1545140936, would also change it for the second date?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. it should change the value of both startDate's. The one of object1 and object2. Because I want to test if it fails, when only the endDate is different in both objects

Copy link
Member

Choose a reason for hiding this comment

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

The fact that you have to change the same value in more than one place is a design smell.

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 fixed it for patientId now as well. Now only values are hardcoded that are only used for the creation of one objects. Thanks for the clarification

}

@Test
public void equal_shouldReturnFalseIfOnlyEndDateOfOneObjectIsNull() {
Copy link
Member

Choose a reason for hiding this comment

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

end date of one object or two objects?

Copy link
Member

Choose a reason for hiding this comment

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

Secondly, would it return true of the patient ids were the same?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

(1) Only EndDate of one object is null. That is why both objects are not equal since the EndDate is different
(2) I fixed it. It was actually a mistake of me

}

@Test
public void equal_shouldReturnFalseIfOnlyStartDateOfOneObjectIsNull() {
Copy link
Member

Choose a reason for hiding this comment

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

Only start date is null or even end date of one object?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixxd it

CohortMembership cohortMembershipOne = new CohortMembership();
CohortMembership cohortMembershipTwo = new CohortMembership();

long timestampStart = 1545140935;
Copy link
Member

Choose a reason for hiding this comment

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

Did you get a chance to look at our naming convention for constants? https://wiki.openmrs.org/display/docs/Java+Conventions

Copy link
Member

Choose a reason for hiding this comment

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

Secondly, would you like this to be later on assigned to another value?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

"Any constants should be written in uppercase letters." I made it upper case and new words seperated by _

public void equal_shouldReturnTrueIfPatientIdIsEqualAndStarDateAndEndDateAreBothNull() {

// Creating two logical identical CohortMemberships to test equal method
int patientId = 12;
Copy link
Member

Choose a reason for hiding this comment

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

Same questions as below

Copy link
Contributor Author

Choose a reason for hiding this comment

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

made it final

@openmrs openmrs deleted a comment Dec 25, 2018
@dkayiwa dkayiwa merged commit 60d92bd into openmrs:master Dec 26, 2018
Andu033 pushed a commit to Andu033/openmrs-core that referenced this pull request Feb 19, 2019
…mrs#2790)

TRUNK-5452 - Adding equal method to CohoerMembership class (openmrs#2791)

TRUNK-5266: Remove unecessary  keywords (openmrs#2795)

TRUNK-5467 Use diamond operator where possible (openmrs#2799)

TRUNK-5469 Use simpler call to get a String's substring (openmrs#2801)

TRUNK-5470 Simplify detection of global error (openmrs#2802)
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