Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Natural ordering of Patient State is not consistent with equals - TRU…
…NK-3652
- Loading branch information
Showing
2 changed files
with
320 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,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); | ||
} | ||
} |