Skip to content

Commit

Permalink
- added guava-testlib as test dependency to modules hapi-fhir-structu…
Browse files Browse the repository at this point in the history
…res-dstu2 and hapi-fhir-structures-dstu3

- added DateRangeParam.equals(Object) and DateRangeParam.hashCode()
- fixed broken DateParamTest
  • Loading branch information
Gaetano Gallo committed Mar 2, 2018
1 parent 1caf768 commit 3009ec7
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 30 deletions.
Expand Up @@ -229,6 +229,9 @@ public void setValuesAsQueryTokens(FhirContext theContext, String theParamName,

@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof DateParam)) {
return false;
}
Expand Down
Expand Up @@ -413,6 +413,24 @@ public void setValuesAsQueryTokens(FhirContext theContext, String theParamName,

}

@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();
Expand Down Expand Up @@ -484,7 +502,5 @@ private void validateAndThrowDataFormatExceptionIfInvalid() {
throw new DataFormatException("Upper bound comparator must be < or <=, can not be " + myUpperBound.getPrefix().getValue());
}
}

}

}
6 changes: 5 additions & 1 deletion hapi-fhir-structures-dstu2/pom.xml
Expand Up @@ -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>
Expand Down
Expand Up @@ -2,21 +2,19 @@

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.List;
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 com.google.common.collect.Lists.newArrayList;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.startsWith;
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.assertThat;

Expand Down Expand Up @@ -115,24 +113,18 @@ public void testParseLegacyPrefixes() {

@Test()
public void testEqualsAndHashCode() {
List<DateParam> uniqueSamples = newArrayList(
new DateParam(),
new DateParam(NOT_EQUAL, new Date()),
new DateParam(EQUAL, new Date().getTime()),
new DateParam(APPROXIMATE, "2017-10-17T11:11:11.111-11:11"));
DateParam previous = null;
for (DateParam current : uniqueSamples) {
ourLog.info("Equals test samples: [previous=" + previous + ", current=" + current + "]");
assertThat(current, is(equalTo(copyOf(current))));
assertThat(current.hashCode(), is(equalTo(copyOf(current).hashCode())));

assertThat(current, is(not(equalTo(previous))));
assertThat(current.hashCode(), is(not(equalTo(previous != null ? previous.hashCode() : null))));
previous = current;
}
}

private DateParam copyOf(DateParam param) {
return new DateParam(param.getPrefix(), param.getValue());
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();
}
}
6 changes: 5 additions & 1 deletion hapi-fhir-structures-dstu3/pom.xml
Expand Up @@ -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>
Expand Down
@@ -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;

Expand Down Expand Up @@ -183,7 +199,26 @@ public void testYear() throws Exception {
public static void afterClassClearContext() {
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>();
Expand All @@ -202,5 +237,4 @@ public static Date parse(String theString) throws ParseException {
public static Date parseM1(String theString) throws ParseException {
return new Date(ourFmt.parse(theString).getTime() - 1L);
}

}
5 changes: 5 additions & 0 deletions pom.xml
Expand Up @@ -510,6 +510,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>
Expand Down

0 comments on commit 3009ec7

Please sign in to comment.