Merge pull request #868 from Tastelezz/wip-DateParam

equals() and hashCode() for DateRangeParam and DateParam
This commit is contained in:
James Agnew 2018-03-02 08:58:07 -05:00 committed by GitHub
commit fe140a55dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 131 additions and 30 deletions

View File

@ -107,5 +107,4 @@ public abstract class BaseParamWithPrefix<T extends BaseParam> extends BaseParam
myPrefix = thePrefix;
return (T) this;
}
}

View File

@ -20,21 +20,26 @@ package ca.uhn.fhir.rest.param;
* #L%
*/
import static org.apache.commons.lang3.StringUtils.isNotBlank;
import java.util.*;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.model.api.IQueryParameterOr;
import ca.uhn.fhir.model.api.TemporalPrecisionEnum;
import ca.uhn.fhir.model.primitive.BaseDateTimeDt;
import ca.uhn.fhir.model.primitive.DateDt;
import ca.uhn.fhir.model.primitive.DateTimeDt;
import ca.uhn.fhir.model.primitive.InstantDt;
import ca.uhn.fhir.rest.api.QualifiedParamList;
import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
import ca.uhn.fhir.util.ValidateUtil;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.hl7.fhir.instance.model.api.IPrimitiveType;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.model.api.IQueryParameterOr;
import ca.uhn.fhir.model.api.TemporalPrecisionEnum;
import ca.uhn.fhir.model.primitive.*;
import ca.uhn.fhir.rest.api.QualifiedParamList;
import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
import ca.uhn.fhir.util.ValidateUtil;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
public class DateParam extends BaseParamWithPrefix<DateParam> implements /*IQueryParameterType , */IQueryParameterOr<DateParam> {
@ -222,6 +227,23 @@ public class DateParam extends BaseParamWithPrefix<DateParam> implements /*IQuer
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof DateParam)) {
return false;
}
DateParam other = (DateParam) obj;
return Objects.equals(getValue(), other.getValue()) &&
Objects.equals(getPrefix(), other.getPrefix());
}
@Override
public int hashCode() {
return Objects.hash(getValue(), getPrefix());
}
@Override
public String toString() {
@ -241,8 +263,5 @@ public class DateParam extends BaseParamWithPrefix<DateParam> implements /*IQuer
protected boolean isPrecisionAllowed(TemporalPrecisionEnum thePrecision) {
return true;
}
}
}

View File

@ -413,6 +413,24 @@ public class DateRangeParam implements IQueryParameterAnd<DateParam> {
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof DateRangeParam)) {
return false;
}
DateRangeParam other = (DateRangeParam) obj;
return Objects.equals(myLowerBound, other.myLowerBound) &&
Objects.equals(myUpperBound, other.myUpperBound);
}
@Override
public int hashCode() {
return Objects.hash(myLowerBound, myUpperBound);
}
@Override
public String toString() {
StringBuilder b = new StringBuilder();
@ -484,7 +502,5 @@ public class DateRangeParam implements IQueryParameterAnd<DateParam> {
throw new DataFormatException("Upper bound comparator must be < or <=, can not be " + myUpperBound.getPrefix().getValue());
}
}
}
}

View File

@ -172,7 +172,11 @@
<artifactId>guava</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava-testlib</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>

View File

@ -1,18 +1,23 @@
package ca.uhn.fhir.rest.param;
import ca.uhn.fhir.model.primitive.DateTimeDt;
import ca.uhn.fhir.model.primitive.InstantDt;
import com.google.common.testing.EqualsTester;
import org.junit.Test;
import java.util.Date;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
import static ca.uhn.fhir.rest.param.ParamPrefixEnum.APPROXIMATE;
import static ca.uhn.fhir.rest.param.ParamPrefixEnum.EQUAL;
import static ca.uhn.fhir.rest.param.ParamPrefixEnum.NOT_EQUAL;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import java.util.Date;
import java.util.TimeZone;
import org.junit.Test;
import ca.uhn.fhir.model.primitive.DateTimeDt;
import ca.uhn.fhir.model.primitive.InstantDt;
public class DateParamTest {
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(DateParamTest.class);
@ -106,5 +111,20 @@ public class DateParamTest {
assertEquals(ParamPrefixEnum.LESSTHAN_OR_EQUALS, new DateParam("le2012").getPrefix());
}
@Test()
public void testEqualsAndHashCode() {
Date now = new Date();
Date later = new Date(now.getTime() + SECONDS.toMillis(666));
new EqualsTester()
.addEqualityGroup(new DateParam(),
new DateParam(null))
.addEqualityGroup(new DateParam(NOT_EQUAL, now),
new DateParam(NOT_EQUAL, now.getTime()))
.addEqualityGroup(new DateParam(EQUAL, now),
new DateParam(EQUAL, now.getTime()))
.addEqualityGroup(new DateParam(EQUAL, later),
new DateParam(EQUAL, later.getTime()))
.addEqualityGroup(new DateParam(APPROXIMATE, "2011-11-11T11:11:11.111-11:11"))
.testEquals();
}
}

View File

@ -261,7 +261,11 @@
<artifactId>guava</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava-testlib</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>

View File

@ -1,12 +1,28 @@
package ca.uhn.fhir.rest.param;
import static ca.uhn.fhir.rest.param.ParamPrefixEnum.APPROXIMATE;
import static ca.uhn.fhir.rest.param.ParamPrefixEnum.EQUAL;
import static ca.uhn.fhir.rest.param.ParamPrefixEnum.GREATERTHAN;
import static ca.uhn.fhir.rest.param.ParamPrefixEnum.GREATERTHAN_OR_EQUALS;
import static ca.uhn.fhir.rest.param.ParamPrefixEnum.LESSTHAN;
import static ca.uhn.fhir.rest.param.ParamPrefixEnum.LESSTHAN_OR_EQUALS;
import static ca.uhn.fhir.rest.param.ParamPrefixEnum.NOT_EQUAL;
import static com.google.common.collect.Lists.newArrayList;
import static java.lang.System.currentTimeMillis;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.hamcrest.core.IsNot.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;
import com.google.common.testing.EqualsTester;
import org.junit.AfterClass;
import org.junit.Test;
@ -184,6 +200,25 @@ public class DateRangeParamTest {
TestUtil.clearAllStaticFieldsForUnitTest();
}
@Test()
public void testEqualsAndHashCode() {
Date lowerBound = new Date(currentTimeMillis());
Date upperBound = new Date(lowerBound.getTime() + SECONDS.toMillis(1));
new EqualsTester()
.addEqualityGroup(new DateRangeParam(),
new DateRangeParam((Date) null, (Date) null))
.addEqualityGroup(new DateRangeParam(lowerBound, upperBound),
new DateRangeParam(new DateParam(GREATERTHAN_OR_EQUALS, lowerBound), new DateParam(LESSTHAN_OR_EQUALS, upperBound)))
.addEqualityGroup(new DateRangeParam(new DateParam(EQUAL, lowerBound)),
new DateRangeParam(new DateParam(null, lowerBound)),
new DateRangeParam(new DateParam(EQUAL, lowerBound), new DateParam(EQUAL, lowerBound)))
.addEqualityGroup(new DateRangeParam(lowerBound, null),
new DateRangeParam(new DateParam(GREATERTHAN_OR_EQUALS, lowerBound), null))
.addEqualityGroup(new DateRangeParam(null, upperBound),
new DateRangeParam(null, new DateParam(LESSTHAN_OR_EQUALS, upperBound)))
.testEquals();
}
private static DateRangeParam create(String theLower, String theUpper) throws InvalidRequestException {
DateRangeParam p = new DateRangeParam();
List<QualifiedParamList> tokens = new ArrayList<QualifiedParamList>();
@ -202,5 +237,4 @@ public class DateRangeParamTest {
public static Date parseM1(String theString) throws ParseException {
return new Date(ourFmt.parse(theString).getTime() - 1L);
}
}

View File

@ -514,6 +514,11 @@
<artifactId>guava</artifactId>
<version>23.0</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava-testlib</artifactId>
<version>23.0</version>
</dependency>
<dependency>
<groupId>com.phloc</groupId>
<artifactId>phloc-schematron</artifactId>