From dbe52b429c917a8678a60fbf98115345c0d5bce5 Mon Sep 17 00:00:00 2001 From: jdar8 <69840459+jdar8@users.noreply.github.com> Date: Wed, 21 Sep 2022 08:54:27 -0700 Subject: [PATCH] fix-matchers-not-contains-in-asserts (#3913) * test * replace not(contains()) * make a util class to replace not(hasItems()) and not(containsInAnyOrder()) * replace not(hasItems()) and not(containsInAnyOrder()) * add javadocs to new util class * test for util class * Addressing comments from code review. * Adding static import. Co-authored-by: Justin_Dar Co-authored-by: peartree --- .../FhirResourceDaoDstu2SearchFtTest.java | 3 +- .../FhirResourceDaoDstu2SearchNoFtTest.java | 12 +++--- .../provider/ResourceProviderDstu2Test.java | 8 ++-- .../FhirResourceDaoDstu3SearchFtTest.java | 6 ++- .../FhirResourceDaoDstu3SearchNoFtTest.java | 20 +++++---- .../dstu3/ResourceProviderDstu3Test.java | 11 ++--- .../dao/r4/FhirResourceDaoR4SearchFtTest.java | 5 ++- .../r4/FhirResourceDaoR4SearchNoFtTest.java | 24 +++++------ .../FhirResourceDaoR4SearchNoHashesTest.java | 19 +++++---- .../provider/r4/ResourceProviderR4Test.java | 41 +++++++++++++------ .../jpa/migrate/taskdef/DropTableTest.java | 9 ++-- .../test/utilities/CustomMatchersUtil.java | 21 ++++++++++ .../utilities/CustomMatchersUtilTest.java | 31 ++++++++++++++ 13 files changed, 148 insertions(+), 62 deletions(-) create mode 100644 hapi-fhir-test-utilities/src/main/java/ca/uhn/fhir/test/utilities/CustomMatchersUtil.java create mode 100644 hapi-fhir-test-utilities/src/test/java/ca/uhn/fhir/test/utilities/CustomMatchersUtilTest.java diff --git a/hapi-fhir-jpaserver-test-dstu2/src/test/java/ca/uhn/fhir/jpa/dao/dstu2/FhirResourceDaoDstu2SearchFtTest.java b/hapi-fhir-jpaserver-test-dstu2/src/test/java/ca/uhn/fhir/jpa/dao/dstu2/FhirResourceDaoDstu2SearchFtTest.java index 23e2013d95a..a8b72629ac5 100644 --- a/hapi-fhir-jpaserver-test-dstu2/src/test/java/ca/uhn/fhir/jpa/dao/dstu2/FhirResourceDaoDstu2SearchFtTest.java +++ b/hapi-fhir-jpaserver-test-dstu2/src/test/java/ca/uhn/fhir/jpa/dao/dstu2/FhirResourceDaoDstu2SearchFtTest.java @@ -24,6 +24,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.empty; +import static org.hamcrest.Matchers.hasItem; import static org.hamcrest.Matchers.not; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.mockito.Mockito.mock; @@ -325,7 +326,7 @@ public class FhirResourceDaoDstu2SearchFtTest extends BaseJpaDstu2Test { assertThat(toUnqualifiedVersionlessIds(myPatientDao.search(map)), contains(pId1)); map = new SearchParameterMap(); map.add(Constants.PARAM_CONTENT, new StringParam("NAMEBBB")); - assertThat(toUnqualifiedVersionlessIds(myPatientDao.search(map)), not(contains(pId1))); + assertThat(toUnqualifiedVersionlessIds(myPatientDao.search(map)), not(hasItem(pId1))); myPatientDao.update(patient, null, true, mySrd); diff --git a/hapi-fhir-jpaserver-test-dstu2/src/test/java/ca/uhn/fhir/jpa/dao/dstu2/FhirResourceDaoDstu2SearchNoFtTest.java b/hapi-fhir-jpaserver-test-dstu2/src/test/java/ca/uhn/fhir/jpa/dao/dstu2/FhirResourceDaoDstu2SearchNoFtTest.java index 2c5c426a5b9..83866421663 100644 --- a/hapi-fhir-jpaserver-test-dstu2/src/test/java/ca/uhn/fhir/jpa/dao/dstu2/FhirResourceDaoDstu2SearchNoFtTest.java +++ b/hapi-fhir-jpaserver-test-dstu2/src/test/java/ca/uhn/fhir/jpa/dao/dstu2/FhirResourceDaoDstu2SearchNoFtTest.java @@ -72,6 +72,7 @@ import ca.uhn.fhir.rest.param.TokenOrListParam; import ca.uhn.fhir.rest.param.TokenParam; import ca.uhn.fhir.rest.param.UriParam; import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; +import ca.uhn.fhir.test.utilities.CustomMatchersUtil; import org.apache.commons.lang3.StringUtils; import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.instance.model.api.IIdType; @@ -93,6 +94,7 @@ import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.containsInRelativeOrder; import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.endsWith; +import static org.hamcrest.Matchers.hasItem; import static org.hamcrest.Matchers.hasItems; import static org.hamcrest.Matchers.not; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -852,14 +854,14 @@ public class FhirResourceDaoDstu2SearchNoFtTest extends BaseJpaDstu2Test { params.setLastUpdated(new DateRangeParam(beforeR2, null)); List patients = toUnqualifiedVersionlessIds(myPatientDao.search(params)); assertThat(patients, containsInAnyOrder(id2)); - assertThat(patients, not(hasItems(id1a, id1b))); + CustomMatchersUtil.assertDoesNotContainAnyOf(patients, List.of(id1a, id1b)); } { SearchParameterMap params = new SearchParameterMap(); params.setLoadSynchronous(true); params.setLastUpdated(new DateRangeParam(beforeAny, beforeR2)); List patients = toUnqualifiedVersionlessIds(myPatientDao.search(params)); - assertThat(patients.toString(), patients, not(hasItems(id2))); + assertThat(patients.toString(), patients, not(hasItem(id2))); assertThat(patients.toString(), patients, (containsInAnyOrder(id1a, id1b))); } { @@ -868,7 +870,7 @@ public class FhirResourceDaoDstu2SearchNoFtTest extends BaseJpaDstu2Test { params.setLastUpdated(new DateRangeParam(null, beforeR2)); List patients = toUnqualifiedVersionlessIds(myPatientDao.search(params)); assertThat(patients, (containsInAnyOrder(id1a, id1b))); - assertThat(patients, not(hasItems(id2))); + assertThat(patients, not(hasItem(id2))); } } @@ -1088,7 +1090,7 @@ public class FhirResourceDaoDstu2SearchNoFtTest extends BaseJpaDstu2Test { params.setLoadSynchronous(true); params.add(Patient.SP_FAMILY, new StringDt(name)); patients = toUnqualifiedVersionlessIds(myPatientDao.search(params)); - assertThat(patients, not(contains(id))); + assertThat(patients, not(hasItem(id))); } @@ -1438,7 +1440,7 @@ public class FhirResourceDaoDstu2SearchNoFtTest extends BaseJpaDstu2Test { IBundleProvider found = myPatientDao.search(params); assertEquals(1, toList(found).size()); assertThat(toUnqualifiedVersionlessIds(found), contains(longId)); - assertThat(toUnqualifiedVersionlessIds(found), not(contains(shortId))); + assertThat(toUnqualifiedVersionlessIds(found), not(hasItem(shortId))); } diff --git a/hapi-fhir-jpaserver-test-dstu2/src/test/java/ca/uhn/fhir/jpa/provider/ResourceProviderDstu2Test.java b/hapi-fhir-jpaserver-test-dstu2/src/test/java/ca/uhn/fhir/jpa/provider/ResourceProviderDstu2Test.java index ed8b176808e..4edd711f638 100644 --- a/hapi-fhir-jpaserver-test-dstu2/src/test/java/ca/uhn/fhir/jpa/provider/ResourceProviderDstu2Test.java +++ b/hapi-fhir-jpaserver-test-dstu2/src/test/java/ca/uhn/fhir/jpa/provider/ResourceProviderDstu2Test.java @@ -110,6 +110,7 @@ import java.util.HashSet; import java.util.List; import java.util.Set; +import static ca.uhn.fhir.test.utilities.CustomMatchersUtil.*; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.containsInAnyOrder; @@ -119,6 +120,7 @@ import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.emptyString; import static org.hamcrest.Matchers.endsWith; import static org.hamcrest.Matchers.greaterThan; +import static org.hamcrest.Matchers.hasItem; import static org.hamcrest.Matchers.hasItems; import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.startsWith; @@ -2040,7 +2042,7 @@ public class ResourceProviderDstu2Test extends BaseResourceProviderDstu2Test { .execute(); List patients = toIdListUnqualifiedVersionless(found); assertThat(patients, hasItems(id2)); - assertThat(patients, not(hasItems(id1a, id1b))); + assertDoesNotContainAnyOf(patients, List.of(id1a, id1b)); } { Bundle found = ourClient.search() @@ -2051,7 +2053,7 @@ public class ResourceProviderDstu2Test extends BaseResourceProviderDstu2Test { .execute(); List patients = toIdListUnqualifiedVersionless(found); - assertThat(patients.toString(), patients, not(hasItems(id2))); + assertThat(patients.toString(), patients, not(hasItem(id2))); assertThat(patients.toString(), patients, (hasItems(id1a, id1b))); } { @@ -2064,7 +2066,7 @@ public class ResourceProviderDstu2Test extends BaseResourceProviderDstu2Test { List patients = toIdListUnqualifiedVersionless(found); assertThat(patients, (hasItems(id1a, id1b))); - assertThat(patients, not(hasItems(id2))); + assertThat(patients, not(hasItem(id2))); } } diff --git a/hapi-fhir-jpaserver-test-dstu3/src/test/java/ca/uhn/fhir/jpa/dao/dstu3/FhirResourceDaoDstu3SearchFtTest.java b/hapi-fhir-jpaserver-test-dstu3/src/test/java/ca/uhn/fhir/jpa/dao/dstu3/FhirResourceDaoDstu3SearchFtTest.java index d16277eec13..08c7646caef 100644 --- a/hapi-fhir-jpaserver-test-dstu3/src/test/java/ca/uhn/fhir/jpa/dao/dstu3/FhirResourceDaoDstu3SearchFtTest.java +++ b/hapi-fhir-jpaserver-test-dstu3/src/test/java/ca/uhn/fhir/jpa/dao/dstu3/FhirResourceDaoDstu3SearchFtTest.java @@ -23,12 +23,16 @@ import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import javax.servlet.http.HttpServletRequest; +import java.util.Arrays; import java.util.List; +import static ca.uhn.fhir.test.utilities.CustomMatchersUtil.*; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.empty; +import static org.hamcrest.Matchers.hasItem; +import static org.hamcrest.Matchers.hasItems; import static org.hamcrest.Matchers.not; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.mockito.Mockito.mock; @@ -418,7 +422,7 @@ public class FhirResourceDaoDstu3SearchFtTest extends BaseJpaDstu3Test { assertThat(toUnqualifiedVersionlessIdValues(myPatientDao.search(map)), contains(idArray)); map = new SearchParameterMap(); map.add(Constants.PARAM_CONTENT, new StringParam("NAMEBBB")); - assertThat(toUnqualifiedVersionlessIdValues(myPatientDao.search(map)), not(contains(idArray))); + assertDoesNotContainAnyOf(toUnqualifiedVersionlessIdValues(myPatientDao.search(map)), Arrays.asList(idArray)); myPatientDao.update(patient, null, true, mockSrd()); diff --git a/hapi-fhir-jpaserver-test-dstu3/src/test/java/ca/uhn/fhir/jpa/dao/dstu3/FhirResourceDaoDstu3SearchNoFtTest.java b/hapi-fhir-jpaserver-test-dstu3/src/test/java/ca/uhn/fhir/jpa/dao/dstu3/FhirResourceDaoDstu3SearchNoFtTest.java index 6f82166bfed..6c4e826933b 100644 --- a/hapi-fhir-jpaserver-test-dstu3/src/test/java/ca/uhn/fhir/jpa/dao/dstu3/FhirResourceDaoDstu3SearchNoFtTest.java +++ b/hapi-fhir-jpaserver-test-dstu3/src/test/java/ca/uhn/fhir/jpa/dao/dstu3/FhirResourceDaoDstu3SearchNoFtTest.java @@ -45,6 +45,7 @@ import ca.uhn.fhir.rest.param.TokenParamModifier; import ca.uhn.fhir.rest.param.UriParam; import ca.uhn.fhir.rest.param.UriParamQualifierEnum; import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; +import ca.uhn.fhir.test.utilities.CustomMatchersUtil; import ca.uhn.fhir.util.UrlUtil; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; @@ -114,6 +115,7 @@ import java.util.List; import java.util.Set; import java.util.TreeSet; +import static ca.uhn.fhir.test.utilities.CustomMatchersUtil.*; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.containsInAnyOrder; @@ -1291,13 +1293,13 @@ public class FhirResourceDaoDstu3SearchNoFtTest extends BaseJpaDstu3Test { List patients = toUnqualifiedVersionlessIds(myPatientDao.search(params)); myCaptureQueriesListener.logSelectQueriesForCurrentThread(0); assertThat(patients, hasItems(id2)); - assertThat(patients, not(hasItems(id1a, id1b))); + assertDoesNotContainAnyOf(patients, List.of(id1a, id1b)); } { SearchParameterMap params = new SearchParameterMap(); params.setLastUpdated(new DateRangeParam(beforeAny, beforeR2)); List patients = toUnqualifiedVersionlessIds(myPatientDao.search(params)); - assertThat(patients.toString(), patients, not(hasItems(id2))); + assertThat(patients.toString(), patients, not(hasItem(id2))); assertThat(patients.toString(), patients, (hasItems(id1a, id1b))); } { @@ -1305,7 +1307,7 @@ public class FhirResourceDaoDstu3SearchNoFtTest extends BaseJpaDstu3Test { params.setLastUpdated(new DateRangeParam(null, beforeR2)); List patients = toUnqualifiedVersionlessIds(myPatientDao.search(params)); assertThat(patients, (hasItems(id1a, id1b))); - assertThat(patients, not(hasItems(id2))); + assertThat(patients, not(hasItem(id2))); } @@ -1313,7 +1315,7 @@ public class FhirResourceDaoDstu3SearchNoFtTest extends BaseJpaDstu3Test { SearchParameterMap params = new SearchParameterMap(); params.setLastUpdated(new DateRangeParam(new DateParam(ParamPrefixEnum.GREATERTHAN_OR_EQUALS, beforeR2))); List patients = toUnqualifiedVersionlessIds(myPatientDao.search(params)); - assertThat(patients, not(hasItems(id1a, id1b))); + CustomMatchersUtil.assertDoesNotContainAnyOf(patients, List.of(id1a, id1b)); assertThat(patients, (hasItems(id2))); } { @@ -1321,7 +1323,7 @@ public class FhirResourceDaoDstu3SearchNoFtTest extends BaseJpaDstu3Test { params.setLastUpdated(new DateRangeParam(new DateParam(ParamPrefixEnum.LESSTHAN_OR_EQUALS, beforeR2))); List patients = toUnqualifiedVersionlessIds(myPatientDao.search(params)); assertThat(patients, (hasItems(id1a, id1b))); - assertThat(patients, not(hasItems(id2))); + assertThat(patients, not(hasItem(id2))); } } @@ -1558,7 +1560,7 @@ public class FhirResourceDaoDstu3SearchNoFtTest extends BaseJpaDstu3Test { params.setLoadSynchronous(true); params.add(Patient.SP_FAMILY, new StringParam(name)); patients = toUnqualifiedVersionlessIds(myPatientDao.search(params)); - assertThat(patients, not(contains(id))); + assertThat(patients, not(hasItem(id))); } @@ -1961,7 +1963,7 @@ public class FhirResourceDaoDstu3SearchNoFtTest extends BaseJpaDstu3Test { params.add(Patient.SP_FAMILY, new StringParam("HELLO")); patients = toUnqualifiedVersionlessIds(myPatientDao.search(params)); assertThat(patients, containsInAnyOrder(pid1)); - assertThat(patients, not(containsInAnyOrder(pid2))); + assertThat(patients, not(hasItem(pid2))); } @Test @@ -2053,7 +2055,7 @@ public class FhirResourceDaoDstu3SearchNoFtTest extends BaseJpaDstu3Test { IBundleProvider found = myPatientDao.search(params); assertEquals(1, toList(found).size()); assertThat(toUnqualifiedVersionlessIds(found), contains(longId)); - assertThat(toUnqualifiedVersionlessIds(found), not(contains(shortId))); + assertThat(toUnqualifiedVersionlessIds(found), not(hasItem(shortId))); } @@ -3108,7 +3110,7 @@ public class FhirResourceDaoDstu3SearchNoFtTest extends BaseJpaDstu3Test { params.add("_tag", new TokenParam("urn:taglist", methodName + "1a").setModifier(TokenParamModifier.NOT)); List patients = toUnqualifiedVersionlessIds(myOrganizationDao.search(params)); assertThat(patients, containsInAnyOrder(tag2id)); - assertThat(patients, not(containsInAnyOrder(tag1id))); + assertThat(patients, not(hasItem(tag1id))); } { // Non existant tag diff --git a/hapi-fhir-jpaserver-test-dstu3/src/test/java/ca/uhn/fhir/jpa/provider/dstu3/ResourceProviderDstu3Test.java b/hapi-fhir-jpaserver-test-dstu3/src/test/java/ca/uhn/fhir/jpa/provider/dstu3/ResourceProviderDstu3Test.java index 09dd39edb07..da7b89bd180 100644 --- a/hapi-fhir-jpaserver-test-dstu3/src/test/java/ca/uhn/fhir/jpa/provider/dstu3/ResourceProviderDstu3Test.java +++ b/hapi-fhir-jpaserver-test-dstu3/src/test/java/ca/uhn/fhir/jpa/provider/dstu3/ResourceProviderDstu3Test.java @@ -151,6 +151,7 @@ import java.util.TreeSet; import java.util.UUID; import java.util.stream.Collectors; +import static ca.uhn.fhir.test.utilities.CustomMatchersUtil.assertDoesNotContainAnyOf; import static org.apache.commons.lang3.StringUtils.isNotBlank; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.contains; @@ -3173,7 +3174,7 @@ public class ResourceProviderDstu3Test extends BaseResourceProviderDstu3Test { //@formatter:on List patients = toUnqualifiedVersionlessIds(found); assertThat(patients, hasItems(id2)); - assertThat(patients, not(hasItems(id1a, id1b))); + assertDoesNotContainAnyOf(patients, List.of(id1a, id1b)); } { //@formatter:off @@ -3185,7 +3186,7 @@ public class ResourceProviderDstu3Test extends BaseResourceProviderDstu3Test { .execute(); //@formatter:on List patients = toUnqualifiedVersionlessIds(found); - assertThat(patients.toString(), patients, not(hasItems(id2))); + assertThat(patients.toString(), patients, not(hasItem(id2))); assertThat(patients.toString(), patients, (hasItems(id1a, id1b))); } { @@ -3199,7 +3200,7 @@ public class ResourceProviderDstu3Test extends BaseResourceProviderDstu3Test { //@formatter:on List patients = toUnqualifiedVersionlessIds(found); assertThat(patients, (hasItems(id1a, id1b))); - assertThat(patients, not(hasItems(id2))); + assertThat(patients, not(hasItem(id2))); } } @@ -3696,7 +3697,7 @@ public class ResourceProviderDstu3Test extends BaseResourceProviderDstu3Test { List ids = toUnqualifiedVersionlessIdValues(bundle); assertThat(ids, contains(oid1)); - assertThat(ids, not(contains(oid2))); + assertThat(ids, not(hasItem(oid2))); } finally { IOUtils.closeQuietly(resp); } @@ -3883,7 +3884,7 @@ public class ResourceProviderDstu3Test extends BaseResourceProviderDstu3Test { List ids = toUnqualifiedVersionlessIdValues(bundle); assertThat(ids, contains(id1.getValue())); - assertThat(ids, not(contains(id2.getValue()))); + assertThat(ids, not(hasItem(id2.getValue()))); } finally { IOUtils.closeQuietly(resp); } diff --git a/hapi-fhir-jpaserver-test-r4/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoR4SearchFtTest.java b/hapi-fhir-jpaserver-test-r4/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoR4SearchFtTest.java index 71e4be0a765..746af3fe500 100644 --- a/hapi-fhir-jpaserver-test-r4/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoR4SearchFtTest.java +++ b/hapi-fhir-jpaserver-test-r4/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoR4SearchFtTest.java @@ -25,12 +25,15 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import javax.servlet.http.HttpServletRequest; +import java.util.Arrays; import java.util.List; +import static ca.uhn.fhir.test.utilities.CustomMatchersUtil.*; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.empty; +import static org.hamcrest.Matchers.hasItems; import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.startsWith; import static org.hl7.fhir.r4.model.Observation.SP_VALUE_QUANTITY; @@ -432,7 +435,7 @@ public class FhirResourceDaoR4SearchFtTest extends BaseJpaR4Test { assertThat(toUnqualifiedVersionlessIdValues(myPatientDao.search(map)), contains(pidTypeArray)); map = new SearchParameterMap(); map.add(Constants.PARAM_CONTENT, new StringParam("NAMEBBB")); - assertThat(toUnqualifiedVersionlessIdValues(myPatientDao.search(map)), not(contains(pidTypeArray))); + assertDoesNotContainAnyOf(toUnqualifiedVersionlessIdValues(myPatientDao.search(map)), Arrays.asList(pidTypeArray)); myPatientDao.update(patient, null, true, mockSrd()); diff --git a/hapi-fhir-jpaserver-test-r4/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoR4SearchNoFtTest.java b/hapi-fhir-jpaserver-test-r4/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoR4SearchNoFtTest.java index 6458ce15310..d9ec18f17de 100644 --- a/hapi-fhir-jpaserver-test-r4/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoR4SearchNoFtTest.java +++ b/hapi-fhir-jpaserver-test-r4/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoR4SearchNoFtTest.java @@ -173,6 +173,7 @@ 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 ca.uhn.fhir.test.utilities.CustomMatchersUtil.*; import static org.apache.commons.lang3.StringUtils.countMatches; import static org.apache.commons.lang3.StringUtils.leftPad; import static org.hamcrest.CoreMatchers.is; @@ -2652,24 +2653,23 @@ public class FhirResourceDaoR4SearchNoFtTest extends BaseJpaR4Test { result = performSearchLastUpdatedAndReturnIds(new DateRangeParam(beforeR2, null)); assertThat(result, hasItems(id2)); - assertThat(result, not(hasItems(id1a, id1b))); - + assertDoesNotContainAnyOf(result, List.of(id1a, id1b)); result = performSearchLastUpdatedAndReturnIds(new DateRangeParam(beforeAny, beforeR2)); - assertThat(result.toString(), result, not(hasItems(id2))); + assertThat(result.toString(), result, not(hasItem(id2))); assertThat(result.toString(), result, (hasItems(id1a, id1b))); result = performSearchLastUpdatedAndReturnIds(new DateRangeParam(null, beforeR2)); assertThat(result, (hasItems(id1a, id1b))); - assertThat(result, not(hasItems(id2))); + assertThat(result, not(hasItem(id2))); result = performSearchLastUpdatedAndReturnIds(new DateRangeParam(new DateParam(GREATERTHAN_OR_EQUALS, beforeR2))); - assertThat(result, not(hasItems(id1a, id1b))); + assertDoesNotContainAnyOf(result, List.of(id1a, id1b)); assertThat(result, (hasItems(id2))); result = performSearchLastUpdatedAndReturnIds(new DateRangeParam(new DateParam(LESSTHAN_OR_EQUALS, beforeR2))); assertThat(result, (hasItems(id1a, id1b))); - assertThat(result, not(hasItems(id2))); + assertThat(result, not(hasItem(id2))); } @Test @@ -2741,7 +2741,7 @@ public class FhirResourceDaoR4SearchNoFtTest extends BaseJpaR4Test { dateRange = new DateRangeParam(new DateParam(EQUAL, p0LastUpdated), new DateParam(EQUAL, p0LastUpdated)); result = performSearchLastUpdatedAndReturnIds(dateRange); assertThat(result, containsInAnyOrder(id0)); - assertThat(result, not(containsInAnyOrder(id1a, id1b))); + assertDoesNotContainAnyOf(result, List.of(id1a, id1b)); DateTimeType p0LastUpdatedDay = new DateTimeType(p0LastUpdated.getValue(), TemporalPrecisionEnum.DAY); dateRange = new DateRangeParam(new DateParam(EQUAL, p0LastUpdatedDay), new DateParam(EQUAL, p0LastUpdatedDay)); @@ -2751,7 +2751,7 @@ public class FhirResourceDaoR4SearchNoFtTest extends BaseJpaR4Test { dateRange = new DateRangeParam(new DateParam(NOT_EQUAL, p0LastUpdated), new DateParam(NOT_EQUAL, p0LastUpdated)); result = performSearchLastUpdatedAndReturnIds(dateRange); assertThat(result, containsInAnyOrder(id1a, id1b)); - assertThat(result, not(containsInAnyOrder(id0))); + assertThat(result, not(hasItem(id0))); dateRange = new DateRangeParam(new DateParam(NOT_EQUAL, p0LastUpdatedDay), new DateParam(NOT_EQUAL, p0LastUpdatedDay)); result = performSearchLastUpdatedAndReturnIds(dateRange); @@ -3077,7 +3077,7 @@ public class FhirResourceDaoR4SearchNoFtTest extends BaseJpaR4Test { params.setLoadSynchronous(true); params.add(Patient.SP_FAMILY, new StringParam(name)); patients = toUnqualifiedVersionlessIds(myPatientDao.search(params)); - assertThat(patients, not(contains(id))); + assertThat(patients, not(hasItem(id))); } @@ -3594,7 +3594,7 @@ public class FhirResourceDaoR4SearchNoFtTest extends BaseJpaR4Test { params.add(Patient.SP_FAMILY, new StringParam("HELLO")); patients = toUnqualifiedVersionlessIds(myPatientDao.search(params)); assertThat(patients, containsInAnyOrder(pid1)); - assertThat(patients, not(containsInAnyOrder(pid2))); + assertThat(patients, not(hasItem(pid2))); } @Test @@ -3626,7 +3626,7 @@ public class FhirResourceDaoR4SearchNoFtTest extends BaseJpaR4Test { IBundleProvider found = myPatientDao.search(params); assertEquals(1, toList(found).size()); assertThat(toUnqualifiedVersionlessIds(found), contains(longId)); - assertThat(toUnqualifiedVersionlessIds(found), not(contains(shortId))); + assertThat(toUnqualifiedVersionlessIds(found), not(hasItem(shortId))); } @@ -5033,7 +5033,7 @@ public class FhirResourceDaoR4SearchNoFtTest extends BaseJpaR4Test { List patients = toUnqualifiedVersionlessIds(myOrganizationDao.search(params)); myCaptureQueriesListener.logSelectQueriesForCurrentThread(0); assertThat(patients, containsInAnyOrder(tag2id)); - assertThat(patients, not(containsInAnyOrder(tag1id))); + assertThat(patients, not(hasItem(tag1id))); } { // Non existant tag diff --git a/hapi-fhir-jpaserver-test-r4/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoR4SearchNoHashesTest.java b/hapi-fhir-jpaserver-test-r4/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoR4SearchNoHashesTest.java index e66785dbec5..90f9bd6f2ca 100644 --- a/hapi-fhir-jpaserver-test-r4/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoR4SearchNoHashesTest.java +++ b/hapi-fhir-jpaserver-test-r4/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoR4SearchNoHashesTest.java @@ -120,6 +120,7 @@ import java.util.Set; import java.util.TreeSet; import java.util.stream.Collectors; +import static ca.uhn.fhir.test.utilities.CustomMatchersUtil.*; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.containsInAnyOrder; @@ -1485,13 +1486,13 @@ public class FhirResourceDaoR4SearchNoHashesTest extends BaseJpaR4Test { params.setLastUpdated(new DateRangeParam(beforeR2, null)); List patients = toUnqualifiedVersionlessIds(myPatientDao.search(params)); assertThat(patients, hasItems(id2)); - assertThat(patients, not(hasItems(id1a, id1b))); + assertDoesNotContainAnyOf(patients, List.of(id1a, id1b)); } { SearchParameterMap params = new SearchParameterMap(); params.setLastUpdated(new DateRangeParam(beforeAny, beforeR2)); List patients = toUnqualifiedVersionlessIds(myPatientDao.search(params)); - assertThat(patients.toString(), patients, not(hasItems(id2))); + assertThat(patients.toString(), patients, not(hasItem(id2))); assertThat(patients.toString(), patients, (hasItems(id1a, id1b))); } { @@ -1499,7 +1500,7 @@ public class FhirResourceDaoR4SearchNoHashesTest extends BaseJpaR4Test { params.setLastUpdated(new DateRangeParam(null, beforeR2)); List patients = toUnqualifiedVersionlessIds(myPatientDao.search(params)); assertThat(patients, (hasItems(id1a, id1b))); - assertThat(patients, not(hasItems(id2))); + assertThat(patients, not(hasItem(id2))); } @@ -1507,7 +1508,7 @@ public class FhirResourceDaoR4SearchNoHashesTest extends BaseJpaR4Test { SearchParameterMap params = new SearchParameterMap(); params.setLastUpdated(new DateRangeParam(new DateParam(ParamPrefixEnum.GREATERTHAN_OR_EQUALS, beforeR2))); List patients = toUnqualifiedVersionlessIds(myPatientDao.search(params)); - assertThat(patients, not(hasItems(id1a, id1b))); + assertDoesNotContainAnyOf(patients, List.of(id1a, id1b)); assertThat(patients, (hasItems(id2))); } { @@ -1515,7 +1516,7 @@ public class FhirResourceDaoR4SearchNoHashesTest extends BaseJpaR4Test { params.setLastUpdated(new DateRangeParam(new DateParam(ParamPrefixEnum.LESSTHAN_OR_EQUALS, beforeR2))); List patients = toUnqualifiedVersionlessIds(myPatientDao.search(params)); assertThat(patients, (hasItems(id1a, id1b))); - assertThat(patients, not(hasItems(id2))); + assertThat(patients, not(hasItem(id2))); } } @@ -1758,7 +1759,7 @@ public class FhirResourceDaoR4SearchNoHashesTest extends BaseJpaR4Test { params.setLoadSynchronous(true); params.add(Patient.SP_FAMILY, new StringParam(name)); patients = toUnqualifiedVersionlessIds(myPatientDao.search(params)); - assertThat(patients, not(contains(id))); + assertThat(patients, not(hasItem(id))); } @@ -2153,7 +2154,7 @@ public class FhirResourceDaoR4SearchNoHashesTest extends BaseJpaR4Test { params.add(Patient.SP_FAMILY, new StringParam("HELLO")); patients = toUnqualifiedVersionlessIds(myPatientDao.search(params)); assertThat(patients, containsInAnyOrder(pid1)); - assertThat(patients, not(containsInAnyOrder(pid2))); + assertThat(patients, not(hasItem(pid2))); } @Test @@ -2185,7 +2186,7 @@ public class FhirResourceDaoR4SearchNoHashesTest extends BaseJpaR4Test { IBundleProvider found = myPatientDao.search(params); assertEquals(1, toList(found).size()); assertThat(toUnqualifiedVersionlessIds(found), contains(longId)); - assertThat(toUnqualifiedVersionlessIds(found), not(contains(shortId))); + assertThat(toUnqualifiedVersionlessIds(found), not(hasItem(shortId))); } @@ -3149,7 +3150,7 @@ public class FhirResourceDaoR4SearchNoHashesTest extends BaseJpaR4Test { params.add("_tag", new TokenParam("urn:taglist", methodName + "1a").setModifier(TokenParamModifier.NOT)); List patients = toUnqualifiedVersionlessIds(myOrganizationDao.search(params)); assertThat(patients, containsInAnyOrder(tag2id)); - assertThat(patients, not(containsInAnyOrder(tag1id))); + assertThat(patients, not(hasItem(tag1id))); } { // Non existant tag diff --git a/hapi-fhir-jpaserver-test-r4/src/test/java/ca/uhn/fhir/jpa/provider/r4/ResourceProviderR4Test.java b/hapi-fhir-jpaserver-test-r4/src/test/java/ca/uhn/fhir/jpa/provider/r4/ResourceProviderR4Test.java index 15acc7292cb..9ed65e15945 100644 --- a/hapi-fhir-jpaserver-test-r4/src/test/java/ca/uhn/fhir/jpa/provider/r4/ResourceProviderR4Test.java +++ b/hapi-fhir-jpaserver-test-r4/src/test/java/ca/uhn/fhir/jpa/provider/r4/ResourceProviderR4Test.java @@ -162,7 +162,6 @@ import org.springframework.transaction.support.TransactionCallbackWithoutResult; import org.springframework.transaction.support.TransactionTemplate; import javax.annotation.Nonnull; -import javax.sql.DataSource; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; @@ -186,6 +185,7 @@ import java.util.stream.Stream; import static ca.uhn.fhir.jpa.config.r4.FhirContextR4Config.DEFAULT_PRESERVE_VERSION_REFS; import static ca.uhn.fhir.jpa.util.TestUtil.sleepOneClick; import static ca.uhn.fhir.rest.param.BaseParamWithPrefix.MSG_PREFIX_INVALID_FORMAT; +import static ca.uhn.fhir.test.utilities.CustomMatchersUtil.assertDoesNotContainAnyOf; import static ca.uhn.fhir.util.TestUtil.sleepAtLeast; import static org.apache.commons.lang3.StringUtils.isNotBlank; import static org.hamcrest.MatcherAssert.assertThat; @@ -2282,8 +2282,8 @@ public class ResourceProviderR4Test extends BaseResourceProviderR4Test { assertThat(ids, containsInAnyOrder(o1Id, p1Id, c1Id)); assertThat(ids, not((o2Id))); - assertThat(ids, not(contains(c2Id))); - assertThat(ids, not(contains(p2Id))); + assertThat(ids, not(hasItem(c2Id))); + assertThat(ids, not(hasItem(p2Id))); } { @@ -2315,7 +2315,7 @@ public class ResourceProviderR4Test extends BaseResourceProviderR4Test { List ids = toUnqualifiedVersionlessIds(b); assertThat(ids, containsInAnyOrder(o1Id, p1Id, c1Id, o2Id, c2Id, p2Id)); - assertThat(ids, not(contains(c5Id))); + assertThat(ids, not(hasItem(c5Id))); } { @@ -2333,7 +2333,7 @@ public class ResourceProviderR4Test extends BaseResourceProviderR4Test { List ids = toUnqualifiedVersionlessIds(b); assertThat(ids, containsInAnyOrder(o1Id, p1Id, c1Id, o2Id, c2Id, p2Id, p3Id, o3Id, c3Id, p4Id, c4Id, o4Id)); - assertThat(ids, not(contains(c5Id))); + assertThat(ids, not(hasItem(c5Id))); } { @@ -2361,9 +2361,26 @@ public class ResourceProviderR4Test extends BaseResourceProviderR4Test { allresults.addAll(secondBundle); assertThat(allresults, containsInAnyOrder(o1Id, p1Id, c1Id, o2Id, c2Id, p2Id, p3Id, o3Id, c3Id, p4Id, c4Id, o4Id)); - assertThat(allresults, not(contains(c5Id))); + assertThat(allresults, not(hasItem(c5Id))); } } + @Test + public void testContains(){ + List test = List.of("a", "b", "c"); + String testString = "testAString"; + + //examined iterable must be of the same length as the specified collection of matchers + assertThat(test, not(contains("b"))); //replace with not(hasItem()) + + //examined Iterable yield at least one item that is matched + //it can contain "a", but it doesn't contain "d" so this passes + //really does "do not have one of these" + assertThat(test, not(hasItems("a", "d"))); //replace with individual calls to not(hasItem()) + //MatchersUtil.assertDoesNotContainAnyOf(test, List.of("a", "d")); + + //examined iterable must be of the same length as the specified collection of matchers + assertThat(test, not(containsInAnyOrder("a", "b"))); //replace with indiv calls to not(hasItem()) + } @Test public void testEverythingPatientInstanceWithTypeParameter() { @@ -2503,7 +2520,7 @@ public class ResourceProviderR4Test extends BaseResourceProviderR4Test { assertEquals(BundleType.SEARCHSET, b.getType()); List ids = toUnqualifiedVersionlessIds(b); assertThat(ids, containsInAnyOrder(o1Id, pabcId, c1Id, pdefId, o2Id, c2Id)); - assertThat(ids, not(contains(c3Id))); + assertThat(ids, not(hasItem(c3Id))); } @@ -4579,7 +4596,7 @@ public class ResourceProviderR4Test extends BaseResourceProviderR4Test { //@formatter:on List patients = toUnqualifiedVersionlessIds(found); assertThat(patients, hasItems(id2)); - assertThat(patients, not(hasItems(id1a, id1b))); + assertDoesNotContainAnyOf(patients, List.of(id1a, id1b)); } { //@formatter:off @@ -4591,7 +4608,7 @@ public class ResourceProviderR4Test extends BaseResourceProviderR4Test { .execute(); //@formatter:on List patients = toUnqualifiedVersionlessIds(found); - assertThat(patients.toString(), patients, not(hasItems(id2))); + assertThat(patients.toString(), patients, not(hasItem(id2))); assertThat(patients.toString(), patients, (hasItems(id1a, id1b))); } { @@ -4605,7 +4622,7 @@ public class ResourceProviderR4Test extends BaseResourceProviderR4Test { //@formatter:on List patients = toUnqualifiedVersionlessIds(found); assertThat(patients, (hasItems(id1a, id1b))); - assertThat(patients, not(hasItems(id2))); + assertThat(patients, not(hasItem(id2))); } } @@ -4649,7 +4666,7 @@ public class ResourceProviderR4Test extends BaseResourceProviderR4Test { List ids = toUnqualifiedVersionlessIdValues(bundle); assertThat(ids, contains(oid1)); - assertThat(ids, not(contains(oid2))); + assertThat(ids, not(hasItem(oid2))); } } @@ -5754,7 +5771,7 @@ public class ResourceProviderR4Test extends BaseResourceProviderR4Test { List ids = toUnqualifiedVersionlessIdValues(bundle); assertThat(ids, contains(id1.getValue())); - assertThat(ids, not(contains(id2.getValue()))); + assertThat(ids, not(hasItem(id2.getValue()))); } } diff --git a/hapi-fhir-sql-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/taskdef/DropTableTest.java b/hapi-fhir-sql-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/taskdef/DropTableTest.java index c8520d87892..6f7a45224d6 100644 --- a/hapi-fhir-sql-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/taskdef/DropTableTest.java +++ b/hapi-fhir-sql-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/taskdef/DropTableTest.java @@ -10,6 +10,7 @@ import java.util.function.Supplier; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.greaterThan; +import static org.hamcrest.Matchers.hasItem; import static org.hamcrest.Matchers.hasItems; import static org.hamcrest.core.IsNot.not; @@ -33,7 +34,7 @@ public class DropTableTest extends BaseTest { getMigrator().migrate(); - assertThat(JdbcUtils.getTableNames(getConnectionProperties()), not(hasItems("SOMETABLE"))); + assertThat(JdbcUtils.getTableNames(getConnectionProperties()), not(hasItem("SOMETABLE"))); } @ParameterizedTest(name = "{index}: {0}") @@ -53,7 +54,7 @@ public class DropTableTest extends BaseTest { getMigrator().migrate(); - assertThat(JdbcUtils.getTableNames(getConnectionProperties()), not(hasItems("SOMETABLE"))); + assertThat(JdbcUtils.getTableNames(getConnectionProperties()), not(hasItem("SOMETABLE"))); } @ParameterizedTest(name = "{index}: {0}") @@ -67,7 +68,7 @@ public class DropTableTest extends BaseTest { getMigrator().migrate(); - assertThat(JdbcUtils.getTableNames(getConnectionProperties()), not(hasItems("SOMETABLE"))); + assertThat(JdbcUtils.getTableNames(getConnectionProperties()), not(hasItem("SOMETABLE"))); } @ParameterizedTest(name = "{index}: {0}") @@ -87,7 +88,7 @@ public class DropTableTest extends BaseTest { getMigrator().migrate(); assertThat(getMigrator().getMigrationInfo().get().pending().length, equalTo(0)); - assertThat(JdbcUtils.getTableNames(getConnectionProperties()), not(hasItems("SOMETABLE"))); + assertThat(JdbcUtils.getTableNames(getConnectionProperties()), not(hasItem("SOMETABLE"))); } diff --git a/hapi-fhir-test-utilities/src/main/java/ca/uhn/fhir/test/utilities/CustomMatchersUtil.java b/hapi-fhir-test-utilities/src/main/java/ca/uhn/fhir/test/utilities/CustomMatchersUtil.java new file mode 100644 index 00000000000..1a4919fe08a --- /dev/null +++ b/hapi-fhir-test-utilities/src/main/java/ca/uhn/fhir/test/utilities/CustomMatchersUtil.java @@ -0,0 +1,21 @@ +package ca.uhn.fhir.test.utilities; + +import java.util.List; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.hasItem; +import static org.hamcrest.Matchers.not; + +public class CustomMatchersUtil { + + /** + * Asserts that none of the items in theShouldNotContain are in theActual + * @param theActual the actual results + * @param theShouldNotContain the items that should not be in theActual + */ + public static void assertDoesNotContainAnyOf(List theActual, List theShouldNotContain) { + for (T item : theShouldNotContain) { + assertThat(theActual, not(hasItem(item))); + } + } +} diff --git a/hapi-fhir-test-utilities/src/test/java/ca/uhn/fhir/test/utilities/CustomMatchersUtilTest.java b/hapi-fhir-test-utilities/src/test/java/ca/uhn/fhir/test/utilities/CustomMatchersUtilTest.java new file mode 100644 index 00000000000..72b3767fccd --- /dev/null +++ b/hapi-fhir-test-utilities/src/test/java/ca/uhn/fhir/test/utilities/CustomMatchersUtilTest.java @@ -0,0 +1,31 @@ +package ca.uhn.fhir.test.utilities; + +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static ca.uhn.fhir.test.utilities.CustomMatchersUtil.assertDoesNotContainAnyOf; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class CustomMatchersUtilTest { + private List data = List.of("A", "B", "C"); + + @Test + public void testAssertDoesNotContainAllOf_withItemsNotInData() { + assertDoesNotContainAnyOf(data, List.of("D", "E")); + } + + @Test + public void testAssertDoesNotContainAllOf_withItemsInData() { + assertThrows(AssertionError.class, () -> { + assertDoesNotContainAnyOf(data, List.of("A", "B")); + }); + } + + @Test + public void testAssertDoesNotContainAllOf_withSomeItemsInData() { + assertThrows(AssertionError.class, () -> { + assertDoesNotContainAnyOf(data, List.of("A", "E")); + }); + } +}