Skip to content

Commit

Permalink
Natural ordering of Patient State is not consistent with equals - TRU…
Browse files Browse the repository at this point in the history
…NK-3652
  • Loading branch information
k-joseph authored and wluyima committed Aug 16, 2013
1 parent 0ddd198 commit 19e54f4
Show file tree
Hide file tree
Showing 2 changed files with 320 additions and 0 deletions.
5 changes: 5 additions & 0 deletions api/src/main/java/org/openmrs/PatientState.java
Expand Up @@ -194,13 +194,18 @@ public void setId(Integer id) {
* @see java.lang.Comparable#compareTo(java.lang.Object)
* @should return positive if startDates equal and this endDate null
* @should return negative if this startDate null
* @should pass if two states have the same start date, end date and uuid
* @should return positive or negative if two states have the same start date and end date but different uuids
*/
@Override
public int compareTo(PatientState o) {
int result = OpenmrsUtil.compareWithNullAsEarliest(getStartDate(), o.getStartDate());
if (result == 0) {
result = OpenmrsUtil.compareWithNullAsLatest(getEndDate(), o.getEndDate());
}
if (result == 0) {
result = OpenmrsUtil.compareWithNullAsGreatest(getUuid(), o.getUuid());
}
return result;
}
}
315 changes: 315 additions & 0 deletions api/src/test/java/org/openmrs/PatientStateTest.java
@@ -0,0 +1,315 @@
/**
* The contents of this file are subject to the OpenMRS Public License
* Version 1.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://license.openmrs.org
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
* Copyright (C) OpenMRS, LLC. All Rights Reserved.
*/
package org.openmrs;

import java.util.Date;
import java.util.UUID;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openmrs.test.Verifies;

public class PatientStateTest {

private Date leftRange;

private Date inRange;

private Date rightRange;

private Date rightOutOfRange;

private Date leftOutOfRange;

private String uuid2;

private String uuid1;

@Before
public void before() {
inRange = new Date();
leftRange = new Date(inRange.getTime() - 10000);
rightRange = new Date(inRange.getTime() + 10000);
rightOutOfRange = new Date(rightRange.getTime() + 10000);
leftOutOfRange = new Date(leftRange.getTime() - 10000);
}

/**
* @see PatientState#getActive(Date)
* @verifies return false if voided and date in range
*/
@Test
public void getActive_shouldReturnFalseIfVoidedAndDateInRange() throws Exception {
//given
PatientState patientState = new PatientState();
patientState.setStartDate(leftRange);
patientState.setEndDate(rightRange);
patientState.setVoided(true);

//when
boolean active = patientState.getActive(inRange);

//then
Assert.assertFalse(active);
}

/**
* @see PatientState#getActive(Date)
* @verifies return false if voided and date not in range
*/
@Test
public void getActive_shouldReturnFalseIfVoidedAndDateNotInRange() throws Exception {
//given
PatientState patientState = new PatientState();
patientState.setStartDate(leftRange);
patientState.setEndDate(rightRange);
patientState.setVoided(true);

//when
boolean active = patientState.getActive(rightOutOfRange);

//then
Assert.assertFalse(active);
}

/**
* @see PatientState#getActive(Date)
* @verifies return true if not voided and date in range
*/
@Test
public void getActive_shouldReturnTrueIfNotVoidedAndDateInRange() throws Exception {
//given
PatientState patientState = new PatientState();
patientState.setStartDate(leftRange);
patientState.setEndDate(rightRange);
patientState.setVoided(false);

//when
boolean active = patientState.getActive(inRange);

//then
Assert.assertTrue(active);
}

/**
* @see PatientState#getActive(Date)
* @verifies return true if not voided and date in range with null startDate
*/
@Test
public void getActive_shouldReturnTrueIfNotVoidedAndDateInRangeWithNullStartDate() throws Exception {
//given
PatientState patientState = new PatientState();
patientState.setStartDate(null);
patientState.setEndDate(rightRange);
patientState.setVoided(false);

//when
boolean active = patientState.getActive(inRange);

//then
Assert.assertTrue(active);
}

/**
* @see PatientState#getActive(Date)
* @verifies return true if not voided and date in range with null endDate
*/
@Test
public void getActive_shouldReturnTrueIfNotVoidedAndDateInRangeWithNullEndDate() throws Exception {
//given
PatientState patientState = new PatientState();
patientState.setStartDate(leftRange);
patientState.setEndDate(null);
patientState.setVoided(false);

//when
boolean active = patientState.getActive(inRange);

//then
Assert.assertTrue(active);
}

/**
* @see PatientState#getActive(Date)
* @verifies return true if not voided and both startDate and endDate nulled
*/
@Test
public void getActive_shouldReturnTrueIfNotVoidedAndBothStartDateAndEndDateNulled() throws Exception {
//given
PatientState patientState = new PatientState();
patientState.setStartDate(null);
patientState.setEndDate(null);
patientState.setVoided(false);

//when
boolean active = patientState.getActive(inRange);

//then
Assert.assertTrue(active);
}

/**
* @see PatientState#getActive(Date)
* @verifies compare with current date if date null
*/
@Test
public void getActive_shouldCompareWithCurrentDateIfDateNull() throws Exception {
//given
PatientState patientState = new PatientState();
patientState.setStartDate(leftRange);
patientState.setEndDate(leftRange);
patientState.setVoided(false);

//when
boolean active = patientState.getActive(null);

//then
Assert.assertFalse(active);
}

/**
* @see PatientState#getActive(Date)
* @verifies return false if not voided and date earlier than startDate
*/
@Test
public void getActive_shouldReturnFalseIfNotVoidedAndDateEarlierThanStartDate() throws Exception {
//given
PatientState patientState = new PatientState();
patientState.setStartDate(leftRange);
patientState.setEndDate(rightRange);
patientState.setVoided(false);

//when
boolean active = patientState.getActive(leftOutOfRange);

//then
Assert.assertFalse(active);
}

/**
* @see PatientState#getActive(Date)
* @verifies return false if not voided and date later than endDate
*/
@Test
public void getActive_shouldReturnFalseIfNotVoidedAndDateLaterThanEndDate() throws Exception {
//given
PatientState patientState = new PatientState();
patientState.setStartDate(leftRange);
patientState.setEndDate(rightRange);
patientState.setVoided(false);

//when
boolean active = patientState.getActive(rightOutOfRange);

//then
Assert.assertFalse(active);
}

/**
* @see PatientState#compareTo(PatientState)
* @verifies return positive if startDates equal and this endDate null
*/
@Test
public void compareTo_shouldReturnPositiveIfStartDatesEqualAndThisEndDateNull() throws Exception {
//given
PatientState patientState = new PatientState();
patientState.setStartDate(leftRange);
patientState.setEndDate(null);
patientState.setVoided(false);

PatientState patientState2 = new PatientState();
patientState2.setStartDate(leftRange);
patientState2.setEndDate(rightRange);
patientState2.setVoided(false);

//when
int result = patientState.compareTo(patientState2);

//then
Assert.assertTrue(result > 0);
}

/**
* @see PatientState#compareTo(PatientState)
* @verifies return negative if this startDate null
*/
@Test
public void compareTo_shouldReturnNegativeIfThisStartDateNull() throws Exception {
//given
PatientState patientState = new PatientState();
patientState.setStartDate(null);
patientState.setEndDate(rightRange);
patientState.setVoided(false);

PatientState patientState2 = new PatientState();
patientState2.setStartDate(leftRange);
patientState2.setEndDate(rightRange);
patientState2.setVoided(false);

//when
int result = patientState.compareTo(patientState2);

//then
Assert.assertTrue(result < 0);
}

/**
* @see PatientState#compareTo(PatientState)
*/
@Test
@Verifies(value = "pass if two states have the same start date, end date and uuid", method = "compareTo(PatientState)")
public void compareTo_shouldPassIfTwoStatesHaveTheSameStartDateEndDateAndUuid() throws Exception {

PatientState patientState = new PatientState();
patientState.setStartDate(leftRange);
patientState.setEndDate(rightRange);
patientState.setUuid(uuid1);
patientState.setVoided(false);

PatientState patientState2 = new PatientState();
patientState2.setStartDate(leftRange);
patientState2.setEndDate(rightRange);
patientState2.setUuid(uuid1);
patientState2.setVoided(false);

Assert.assertTrue(patientState.compareTo(patientState2) == 0);
}

/**
* @see PatientState#compareTo(PatientState)
*/
@Test
@Verifies(value = "return positive or negative if two states have the same start date and end date but different uuids", method = "compareTo(PatientState)")
public void compareTo_shouldReturnPositiveOrNegativeIfTwoStatesHaveTheSameStartDatesEndDatesAndUuids() throws Exception {
uuid1 = UUID.randomUUID().toString();
uuid2 = UUID.randomUUID().toString();

PatientState patientState = new PatientState();
patientState.setStartDate(leftRange);
patientState.setEndDate(rightRange);
patientState.setUuid(uuid1);
patientState.setVoided(false);

PatientState patientState2 = new PatientState();
patientState2.setStartDate(leftRange);
patientState2.setEndDate(rightRange);
patientState2.setUuid(uuid2);
patientState2.setVoided(false);

int result = (patientState.compareTo(patientState2));

Assert.assertTrue(result <= -1 || result >= 1);
}
}

0 comments on commit 19e54f4

Please sign in to comment.