diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirConceptDaoImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirConceptDaoImpl.java index f8a4941bd..2c1b7f565 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirConceptDaoImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirConceptDaoImpl.java @@ -22,7 +22,6 @@ import java.util.Optional; import ca.uhn.fhir.rest.param.StringAndListParam; -import lombok.AccessLevel; import lombok.Setter; import org.hibernate.Criteria; import org.hibernate.transform.DistinctRootEntityResultTransformer; @@ -38,7 +37,7 @@ import org.springframework.stereotype.Component; @Component -@Setter(AccessLevel.PACKAGE) +@Setter public class FhirConceptDaoImpl extends BaseFhirDao implements FhirConceptDao { @Autowired diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/impl/FhirConceptServiceImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/impl/FhirConceptServiceImpl.java index e916afe8c..41ad8751c 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/impl/FhirConceptServiceImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/impl/FhirConceptServiceImpl.java @@ -12,7 +12,6 @@ import java.util.List; import java.util.Optional; -import lombok.AccessLevel; import lombok.Setter; import org.openmrs.Concept; import org.openmrs.ConceptMap; @@ -26,7 +25,7 @@ @Component @Transactional -@Setter(AccessLevel.PACKAGE) +@Setter public class FhirConceptServiceImpl implements FhirConceptService { @Autowired diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/impl/FhirConceptSourceServiceImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/impl/FhirConceptSourceServiceImpl.java index 1dc5c469a..822db0796 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/impl/FhirConceptSourceServiceImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/impl/FhirConceptSourceServiceImpl.java @@ -14,7 +14,6 @@ import java.util.Collection; import java.util.Optional; -import lombok.AccessLevel; import lombok.Setter; import org.openmrs.ConceptSource; import org.openmrs.Duration; @@ -28,7 +27,7 @@ @Component @Transactional -@Setter(AccessLevel.PACKAGE) +@Setter public class FhirConceptSourceServiceImpl implements FhirConceptSourceService { @Autowired diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/ObservationValueTranslatorImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/ObservationValueTranslatorImpl.java index 1088c5bcc..7d6836879 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/ObservationValueTranslatorImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/ObservationValueTranslatorImpl.java @@ -50,8 +50,9 @@ public Type toFhirResource(@Nonnull Obs obs) { // IMPORTANT boolean values are stored as a coded value, so for this to // work, we must check for a boolean value before a general coded value - if (obs.getValueBoolean() != null) { - return new BooleanType(obs.getValueBoolean()); + Boolean valueBoolean = getValueBoolean(obs); + if (valueBoolean != null) { + return new BooleanType(valueBoolean); } else if (obs.getValueCoded() != null) { return conceptTranslator.toFhirResource(obs.getValueCoded()); } else if (obs.getValueDrug() != null) { @@ -99,11 +100,25 @@ public Obs toOpenmrsType(@Nonnull Obs obs, @Nonnull Type resource) { } else if (resource instanceof Quantity) { obs.setValueNumeric(((Quantity) resource).getValue().doubleValue()); } else if (resource instanceof BooleanType) { - obs.setValueBoolean(((BooleanType) resource).getValue()); + setValueBoolean(obs, ((BooleanType) resource).getValue()); } else if (resource instanceof StringType) { obs.setValueText(((StringType) resource).getValue()); } return obs; } + + /** + * @return the valueBoolean of the given obs + */ + protected Boolean getValueBoolean(Obs obs) { + return obs.getValueBoolean(); + } + + /** + * sets the valueBoolean property of the given obs to the given value + */ + protected void setValueBoolean(Obs obs, Boolean valueBoolean) { + obs.setValueBoolean(valueBoolean); + } } diff --git a/api/src/test/java/org/openmrs/module/fhir2/MockedCalendarFactoryConfiguration.java b/api/src/test/java/org/openmrs/module/fhir2/MockedCalendarFactoryConfiguration.java deleted file mode 100644 index 36e2d2b37..000000000 --- a/api/src/test/java/org/openmrs/module/fhir2/MockedCalendarFactoryConfiguration.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * This Source Code Form is subject to the terms of the Mozilla Public License, - * v. 2.0. If a copy of the MPL was not distributed with this file, You can - * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under - * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. - * - * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS - * graphic logo is a trademark of OpenMRS Inc. - */ -package org.openmrs.module.fhir2; - -import org.mockito.Mockito; -import org.openmrs.module.fhir2.api.util.LocalDateTimeFactory; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Primary; - -@Configuration -public class MockedCalendarFactoryConfiguration { - - @Bean - @Primary - public LocalDateTimeFactory getCalendarFactory() { - return Mockito.mock(LocalDateTimeFactory.class); - } - -} diff --git a/api/src/test/java/org/openmrs/module/fhir2/MockedGlobalPropertyServiceConfiguration.java b/api/src/test/java/org/openmrs/module/fhir2/MockedGlobalPropertyServiceConfiguration.java deleted file mode 100644 index 985e9b746..000000000 --- a/api/src/test/java/org/openmrs/module/fhir2/MockedGlobalPropertyServiceConfiguration.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * This Source Code Form is subject to the terms of the Mozilla Public License, - * v. 2.0. If a copy of the MPL was not distributed with this file, You can - * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under - * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. - * - * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS - * graphic logo is a trademark of OpenMRS Inc. - */ -package org.openmrs.module.fhir2; - -import static org.mockito.Mockito.when; - -import org.mockito.Mockito; -import org.openmrs.module.fhir2.api.FhirGlobalPropertyService; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Primary; - -@Configuration -public class MockedGlobalPropertyServiceConfiguration { - - @Bean - @Primary - public FhirGlobalPropertyService getFhirGlobalPropertyService() { - FhirGlobalPropertyService globalPropertyService = Mockito.mock(FhirGlobalPropertyService.class); - when(globalPropertyService.getGlobalProperty("default_locale", "en_GB")).thenReturn("en_GB"); - return globalPropertyService; - } -} diff --git a/api/src/test/java/org/openmrs/module/fhir2/api/dao/impl/FhirAllergyIntoleranceDaoImplTest.java b/api/src/test/java/org/openmrs/module/fhir2/api/dao/impl/FhirAllergyIntoleranceDaoImplTest.java index 31f05aa79..e5db4b87c 100644 --- a/api/src/test/java/org/openmrs/module/fhir2/api/dao/impl/FhirAllergyIntoleranceDaoImplTest.java +++ b/api/src/test/java/org/openmrs/module/fhir2/api/dao/impl/FhirAllergyIntoleranceDaoImplTest.java @@ -14,21 +14,18 @@ import static org.hamcrest.CoreMatchers.nullValue; import static org.hamcrest.MatcherAssert.assertThat; -import org.hibernate.SessionFactory; import org.junit.Before; import org.junit.Test; -import org.mockito.Mock; import org.openmrs.Allergen; import org.openmrs.AllergenType; import org.openmrs.Allergy; import org.openmrs.AllergyReaction; import org.openmrs.Concept; import org.openmrs.module.fhir2.TestFhirSpringConfiguration; -import org.openmrs.module.fhir2.api.FhirGlobalPropertyService; +import org.openmrs.module.fhir2.api.dao.FhirAllergyIntoleranceDao; import org.openmrs.module.fhir2.api.dao.FhirConceptDao; import org.openmrs.test.BaseModuleContextSensitiveTest; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.test.context.ContextConfiguration; @ContextConfiguration(classes = TestFhirSpringConfiguration.class, inheritLocations = false) @@ -43,22 +40,13 @@ public class FhirAllergyIntoleranceDaoImplTest extends BaseModuleContextSensitiv private static final String CODED_REACTION_UUID = "5087AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"; @Autowired - @Qualifier("sessionFactory") - private SessionFactory sessionFactory; - - @Mock - private FhirGlobalPropertyService globalPropertyService; - - private FhirAllergyIntoleranceDaoImpl allergyDao; + private FhirAllergyIntoleranceDao allergyDao; @Autowired private FhirConceptDao conceptDao; @Before public void setup() throws Exception { - allergyDao = new FhirAllergyIntoleranceDaoImpl(); - allergyDao.setSessionFactory(sessionFactory); - allergyDao.setGlobalPropertyService(globalPropertyService); executeDataSet(ALLERGY_INTOLERANCE_INITIAL_DATA_XML); } diff --git a/api/src/test/java/org/openmrs/module/fhir2/api/dao/impl/FhirLocationDaoImplTest.java b/api/src/test/java/org/openmrs/module/fhir2/api/dao/impl/FhirLocationDaoImplTest.java index 6e334353d..e9020a8e9 100644 --- a/api/src/test/java/org/openmrs/module/fhir2/api/dao/impl/FhirLocationDaoImplTest.java +++ b/api/src/test/java/org/openmrs/module/fhir2/api/dao/impl/FhirLocationDaoImplTest.java @@ -13,23 +13,21 @@ import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.nullValue; -import static org.mockito.Mockito.when; import java.util.List; -import org.hibernate.SessionFactory; import org.junit.Before; import org.junit.Test; -import org.mockito.Mock; import org.openmrs.Location; import org.openmrs.LocationAttribute; import org.openmrs.LocationAttributeType; import org.openmrs.LocationTag; import org.openmrs.api.LocationService; +import org.openmrs.customdatatype.datatype.FreeTextDatatype; import org.openmrs.module.fhir2.TestFhirSpringConfiguration; +import org.openmrs.module.fhir2.api.dao.FhirLocationDao; import org.openmrs.test.BaseModuleContextSensitiveTest; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.test.context.ContextConfiguration; @ContextConfiguration(classes = TestFhirSpringConfiguration.class, inheritLocations = false) @@ -47,20 +45,14 @@ public class FhirLocationDaoImplTest extends BaseModuleContextSensitiveTest { private static final String LOCATION_INITIAL_DATA_XML = "org/openmrs/module/fhir2/api/dao/impl/FhirLocationDaoImplTest_initial_data.xml"; - private FhirLocationDaoImpl fhirLocationDao; - @Autowired - @Qualifier("sessionFactory") - private SessionFactory sessionFactory; + private FhirLocationDao fhirLocationDao; - @Mock - private LocationService locationService; + @Autowired + LocationService locationService; @Before public void setup() throws Exception { - fhirLocationDao = new FhirLocationDaoImpl(); - fhirLocationDao.setLocationService(locationService); - fhirLocationDao.setSessionFactory(sessionFactory); executeDataSet(LOCATION_INITIAL_DATA_XML); } @@ -94,8 +86,9 @@ public void getActiveAttributesByLocationAndAttributeTypeUuid_shouldReturnLocati public void getLocationAttributeTypeByUuid_shouldReturnAttributeType() { LocationAttributeType locationAttributeType = new LocationAttributeType(); locationAttributeType.setUuid(LOCATION_ATTRIBUTE_TYPE_UUID); - - when(locationService.getLocationAttributeTypeByUuid(LOCATION_ATTRIBUTE_TYPE_UUID)).thenReturn(locationAttributeType); + locationAttributeType.setName("Some Attribute"); + locationAttributeType.setDatatypeClassname(FreeTextDatatype.class.getName()); + locationService.saveLocationAttributeType(locationAttributeType); LocationAttributeType result = fhirLocationDao.getLocationAttributeTypeByUuid(LOCATION_ATTRIBUTE_TYPE_UUID); @@ -108,8 +101,7 @@ public void saveLocationTag_shouldSaveTag() { LocationTag locationTag = new LocationTag(); locationTag.setUuid(LOCATION_TAG_UUID); locationTag.setName(LOCATION_TAG_NAME); - - when(locationService.saveLocationTag(locationTag)).thenReturn(locationTag); + locationService.saveLocationTag(locationTag); LocationTag result = fhirLocationDao.saveLocationTag(locationTag); @@ -121,8 +113,7 @@ public void getLocationTagByName_shouldGetTag() { LocationTag locationTag = new LocationTag(); locationTag.setUuid(LOCATION_TAG_UUID); locationTag.setName(LOCATION_TAG_NAME); - - when(locationService.getLocationTagByName(LOCATION_TAG_NAME)).thenReturn(locationTag); + locationService.saveLocationTag(locationTag); LocationTag result = fhirLocationDao.getLocationTagByName(LOCATION_TAG_NAME); diff --git a/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirClientServiceImplTest.java b/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirClientServiceImplTest.java index 7f7edd451..198e08df9 100644 --- a/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirClientServiceImplTest.java +++ b/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirClientServiceImplTest.java @@ -18,11 +18,9 @@ import ca.uhn.fhir.rest.client.api.IGenericClient; import org.junit.Before; import org.junit.Test; -import org.mockito.Mock; public class FhirClientServiceImplTest { - @Mock private static final String R3_URL = "https://demo.openmrs.org/openmrs/ws/fhir2/R3"; private static final String R4_URL = "https://demo.openmrs.org/openmrs/ws/fhir2/R4"; diff --git a/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirImmunizationServiceImplTest.java b/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirImmunizationServiceImplTest.java index dc44b33a3..6780386cb 100644 --- a/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirImmunizationServiceImplTest.java +++ b/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirImmunizationServiceImplTest.java @@ -13,7 +13,6 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.hl7.fhir.r4.model.Patient.SP_IDENTIFIER; -import static org.mockito.Mockito.when; import static org.openmrs.module.fhir2.FhirConstants.ENCOUNTER; import static org.openmrs.module.fhir2.FhirConstants.PATIENT; import static org.openmrs.module.fhir2.FhirConstants.PRACTITIONER; @@ -43,13 +42,14 @@ import org.junit.Test; import org.openmrs.Obs; import org.openmrs.Provider; +import org.openmrs.api.AdministrationService; import org.openmrs.api.ObsService; import org.openmrs.module.fhir2.FhirConstants; import org.openmrs.module.fhir2.TestFhirSpringConfiguration; -import org.openmrs.module.fhir2.api.FhirGlobalPropertyService; import org.openmrs.module.fhir2.api.util.ImmunizationObsGroupHelper; import org.openmrs.test.BaseModuleContextSensitiveTest; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.test.context.ContextConfiguration; @ContextConfiguration(classes = TestFhirSpringConfiguration.class, inheritLocations = false) @@ -68,21 +68,21 @@ public class FhirImmunizationServiceImplTest extends BaseModuleContextSensitiveT private ObsService obsService; @Autowired - private FhirGlobalPropertyService globalPropertyService; + @Qualifier("adminService") + private AdministrationService administrationService; @Autowired private ImmunizationObsGroupHelper helper; @Before public void setup() throws Exception { - when(globalPropertyService.getGlobalProperty(FhirConstants.IMMUNIZATIONS_ENCOUNTER_TYPE_PROPERTY)) - .thenReturn("29c02aff-9a93-46c9-bf6f-48b552fcb1fa"); - when(globalPropertyService.getGlobalProperty(FhirConstants.ADMINISTERING_ENCOUNTER_ROLE_PROPERTY)) - .thenReturn("546cce2d-6d58-4097-ba92-206c1a2a0462"); - executeDataSet(IMMUNIZATIONS_METADATA_XML); executeDataSet(IMMUNIZATIONS_INITIAL_DATA_XML); executeDataSet(PRACTITIONER_INITIAL_DATA_XML); + administrationService.setGlobalProperty(FhirConstants.IMMUNIZATIONS_ENCOUNTER_TYPE_PROPERTY, + "29c02aff-9a93-46c9-bf6f-48b552fcb1fa"); + administrationService.setGlobalProperty(FhirConstants.ADMINISTERING_ENCOUNTER_ROLE_PROPERTY, + "546cce2d-6d58-4097-ba92-206c1a2a0462"); } /** diff --git a/api/src/test/java/org/openmrs/module/fhir2/api/search/AllergyIntoleranceSearchQueryTest.java b/api/src/test/java/org/openmrs/module/fhir2/api/search/AllergyIntoleranceSearchQueryTest.java index 36abad6bc..6a27867cf 100644 --- a/api/src/test/java/org/openmrs/module/fhir2/api/search/AllergyIntoleranceSearchQueryTest.java +++ b/api/src/test/java/org/openmrs/module/fhir2/api/search/AllergyIntoleranceSearchQueryTest.java @@ -23,12 +23,9 @@ import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.lessThanOrEqualTo; import static org.hamcrest.Matchers.not; -import static org.mockito.Mockito.when; -import java.util.HashMap; import java.util.HashSet; import java.util.List; -import java.util.Map; import java.util.stream.Collectors; import ca.uhn.fhir.model.api.Include; @@ -48,14 +45,15 @@ import org.junit.Before; import org.junit.Test; import org.openmrs.Allergy; +import org.openmrs.api.AdministrationService; import org.openmrs.module.fhir2.FhirConstants; import org.openmrs.module.fhir2.TestFhirSpringConfiguration; -import org.openmrs.module.fhir2.api.FhirGlobalPropertyService; import org.openmrs.module.fhir2.api.dao.FhirAllergyIntoleranceDao; import org.openmrs.module.fhir2.api.search.param.SearchParameterMap; import org.openmrs.module.fhir2.api.translators.AllergyIntoleranceTranslator; import org.openmrs.test.BaseModuleContextSensitiveTest; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.test.context.ContextConfiguration; @ContextConfiguration(classes = { TestFhirSpringConfiguration.class }, inheritLocations = false) @@ -121,8 +119,6 @@ public class AllergyIntoleranceSearchQueryTest extends BaseModuleContextSensitiv private static final int END_INDEX = 10; - private static final Map SEVERITY_CONCEPT_UUIDS = new HashMap<>(); - @Autowired private FhirAllergyIntoleranceDao allergyDao; @@ -136,27 +132,16 @@ public class AllergyIntoleranceSearchQueryTest extends BaseModuleContextSensitiv private SearchQuery> searchQuery; @Autowired - private FhirGlobalPropertyService globalPropertyService; + @Qualifier("adminService") + private AdministrationService administrationService; @Before public void setup() throws Exception { executeDataSet(ALLERGY_INTOLERANCE_INITIAL_DATA_XML); - } - - @Before - public void setupMocks() { - initSeverityData(); - - when(globalPropertyService.getGlobalProperties(FhirConstants.GLOBAL_PROPERTY_MILD, - FhirConstants.GLOBAL_PROPERTY_MODERATE, FhirConstants.GLOBAL_PROPERTY_SEVERE, - FhirConstants.GLOBAL_PROPERTY_OTHER)).thenReturn(SEVERITY_CONCEPT_UUIDS); - } - - private void initSeverityData() { - SEVERITY_CONCEPT_UUIDS.put(FhirConstants.GLOBAL_PROPERTY_MILD, SEVERITY_MILD_CONCEPT_UUID); - SEVERITY_CONCEPT_UUIDS.put(FhirConstants.GLOBAL_PROPERTY_MODERATE, SEVERITY_MODERATE_CONCEPT_UUID); - SEVERITY_CONCEPT_UUIDS.put(FhirConstants.GLOBAL_PROPERTY_SEVERE, SEVERITY_SEVERE_CONCEPT_UUID); - SEVERITY_CONCEPT_UUIDS.put(FhirConstants.GLOBAL_PROPERTY_OTHER, SEVERITY_NULL_CONCEPT_UUID); + administrationService.setGlobalProperty(FhirConstants.GLOBAL_PROPERTY_MILD, SEVERITY_MILD_CONCEPT_UUID); + administrationService.setGlobalProperty(FhirConstants.GLOBAL_PROPERTY_MODERATE, SEVERITY_MODERATE_CONCEPT_UUID); + administrationService.setGlobalProperty(FhirConstants.GLOBAL_PROPERTY_SEVERE, SEVERITY_SEVERE_CONCEPT_UUID); + administrationService.setGlobalProperty(FhirConstants.GLOBAL_PROPERTY_OTHER, SEVERITY_NULL_CONCEPT_UUID); } private List get(IBundleProvider results) { diff --git a/api/src/test/java/org/openmrs/module/fhir2/api/search/ConditionSearchQueryTest.java b/api/src/test/java/org/openmrs/module/fhir2/api/search/ConditionSearchQueryTest.java index 78dd6219e..8c936dd03 100644 --- a/api/src/test/java/org/openmrs/module/fhir2/api/search/ConditionSearchQueryTest.java +++ b/api/src/test/java/org/openmrs/module/fhir2/api/search/ConditionSearchQueryTest.java @@ -24,12 +24,10 @@ import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.notNullValue; -import static org.mockito.Mockito.when; import static org.openmrs.test.OpenmrsMatchers.hasId; import static org.openmrs.test.OpenmrsMatchers.hasUuid; -import java.time.LocalDateTime; -import java.time.Month; +import java.util.Date; import java.util.HashSet; import java.util.List; @@ -47,18 +45,19 @@ import ca.uhn.fhir.rest.param.TokenAndListParam; import ca.uhn.fhir.rest.param.TokenOrListParam; import ca.uhn.fhir.rest.param.TokenParam; +import org.apache.commons.lang.time.DateUtils; import org.hamcrest.Matchers; import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.r4.model.Patient; import org.junit.Before; import org.junit.Test; import org.openmrs.Condition; +import org.openmrs.api.ConditionService; import org.openmrs.module.fhir2.FhirConstants; import org.openmrs.module.fhir2.TestFhirSpringConfiguration; import org.openmrs.module.fhir2.api.dao.FhirConditionDao; import org.openmrs.module.fhir2.api.search.param.SearchParameterMap; import org.openmrs.module.fhir2.api.translators.ConditionTranslator; -import org.openmrs.module.fhir2.api.util.LocalDateTimeFactory; import org.openmrs.test.BaseModuleContextSensitiveTest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; @@ -143,7 +142,7 @@ public class ConditionSearchQueryTest extends BaseModuleContextSensitiveTest { private SearchQuery, ConditionTranslator, SearchQueryInclude> searchQuery; @Autowired - private LocalDateTimeFactory localDateTimeFactory; + ConditionService conditionService; @Before public void setup() { @@ -649,12 +648,15 @@ public void searchForConditions_shouldReturnConditionByUnboundedOnsetDate() { @Test public void searchForConditions_shouldReturnConditionByOnsetAgeLessThanHour() { + Condition condition = conditionService.getConditionByUuid(CONDITION_UUID); + Date onsetDate = DateUtils.addMinutes(new Date(), -45); + condition.setOnsetDate(onsetDate); + conditionService.saveCondition(condition); + QuantityOrListParam orList = new QuantityOrListParam(); - orList.addOr(new QuantityParam(ParamPrefixEnum.LESSTHAN, 1.5, "", "h")); + orList.addOr(new QuantityParam(ParamPrefixEnum.LESSTHAN, 1, "", "h")); QuantityAndListParam onsetAgeParam = new QuantityAndListParam().addAnd(orList); - when(localDateTimeFactory.now()).thenReturn(LocalDateTime.of(2020, Month.MARCH, 13, 19, 10, 0)); - SearchParameterMap theParams = new SearchParameterMap().addParameter(FhirConstants.QUANTITY_SEARCH_HANDLER, onsetAgeParam); @@ -670,12 +672,15 @@ public void searchForConditions_shouldReturnConditionByOnsetAgeLessThanHour() { @Test public void searchForConditions_shouldReturnConditionByOnsetAgeEqualHour() { + Condition condition = conditionService.getConditionByUuid(CONDITION_UUID); + Date onsetDate = DateUtils.addHours(new Date(), -3); + condition.setOnsetDate(onsetDate); + conditionService.saveCondition(condition); + QuantityOrListParam orList = new QuantityOrListParam(); orList.addOr(new QuantityParam(ParamPrefixEnum.EQUAL, 3, "", "h")); QuantityAndListParam onsetAgeParam = new QuantityAndListParam().addAnd(orList); - when(localDateTimeFactory.now()).thenReturn(LocalDateTime.of(2020, Month.MARCH, 13, 22, 0, 0)); - SearchParameterMap theParams = new SearchParameterMap().addParameter(FhirConstants.QUANTITY_SEARCH_HANDLER, onsetAgeParam); @@ -691,14 +696,17 @@ public void searchForConditions_shouldReturnConditionByOnsetAgeEqualHour() { @Test public void searchForConditions_shouldReturnConditionByOnsetAgeIntervalDay() { + Condition condition = conditionService.getConditionByUuid(CONDITION_UUID); + Date onsetDate = DateUtils.addDays(new Date(), -9); + condition.setOnsetDate(onsetDate); + conditionService.saveCondition(condition); + QuantityOrListParam orListLower = new QuantityOrListParam(); QuantityOrListParam orListUpper = new QuantityOrListParam(); orListLower.addOr(new QuantityParam(ParamPrefixEnum.LESSTHAN, 11, "", "d")); orListUpper.addOr(new QuantityParam(ParamPrefixEnum.GREATERTHAN, 8, "", "d")); QuantityAndListParam onsetAgeParam = new QuantityAndListParam().addAnd(orListLower).addAnd(orListUpper); - when(localDateTimeFactory.now()).thenReturn(LocalDateTime.of(2020, Month.MARCH, 22, 22, 0, 0)); - SearchParameterMap theParams = new SearchParameterMap().addParameter(FhirConstants.QUANTITY_SEARCH_HANDLER, onsetAgeParam); @@ -719,7 +727,7 @@ public void searchForConditions_shouldReturnConditionByOnsetAgeOrWeekMonthYear() orList.addOr(new QuantityParam(ParamPrefixEnum.LESSTHAN, 2, "", "wk")); QuantityAndListParam onsetAgeParam = new QuantityAndListParam().addAnd(orList); - when(localDateTimeFactory.now()).thenReturn(LocalDateTime.of(2020, Month.MARCH, 13, 22, 0, 0)); + //when(localDateTimeFactory.now()).thenReturn(LocalDateTime.of(2020, Month.MARCH, 13, 22, 0, 0)); SearchParameterMap theParams = new SearchParameterMap().addParameter(FhirConstants.QUANTITY_SEARCH_HANDLER, onsetAgeParam); @@ -738,7 +746,7 @@ public void searchForConditions_shouldReturnConditionByOnsetAgeExceptionForWrong orList.addOr(new QuantityParam(ParamPrefixEnum.LESSTHAN, 1.5, "", "WRONG_UNIT")); QuantityAndListParam onsetAgeParam = new QuantityAndListParam().addAnd(orList); - when(localDateTimeFactory.now()).thenReturn(LocalDateTime.of(2020, Month.MARCH, 13, 19, 10, 0)); + //when(localDateTimeFactory.now()).thenReturn(LocalDateTime.of(2020, Month.MARCH, 13, 19, 10, 0)); SearchParameterMap theParams = new SearchParameterMap().addParameter(FhirConstants.QUANTITY_SEARCH_HANDLER, onsetAgeParam); diff --git a/api/src/test/java/org/openmrs/module/fhir2/api/search/PatientSearchQueryTest.java b/api/src/test/java/org/openmrs/module/fhir2/api/search/PatientSearchQueryTest.java index 61fe995e8..688c3fa49 100644 --- a/api/src/test/java/org/openmrs/module/fhir2/api/search/PatientSearchQueryTest.java +++ b/api/src/test/java/org/openmrs/module/fhir2/api/search/PatientSearchQueryTest.java @@ -9,6 +9,33 @@ */ package org.openmrs.module.fhir2.api.search; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.allOf; +import static org.hamcrest.Matchers.anyOf; +import static org.hamcrest.Matchers.empty; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.everyItem; +import static org.hamcrest.Matchers.greaterThan; +import static org.hamcrest.Matchers.greaterThanOrEqualTo; +import static org.hamcrest.Matchers.hasItem; +import static org.hamcrest.Matchers.hasProperty; +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.instanceOf; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.lessThanOrEqualTo; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.startsWith; +import static org.hl7.fhir.r4.model.Patient.SP_FAMILY; +import static org.hl7.fhir.r4.model.Patient.SP_GIVEN; +import static org.openmrs.module.fhir2.matchers.FhirMatchers.isDeceased; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + import ca.uhn.fhir.model.api.Include; import ca.uhn.fhir.rest.api.SortOrderEnum; import ca.uhn.fhir.rest.api.SortSpec; @@ -42,33 +69,6 @@ import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.test.context.ContextConfiguration; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.allOf; -import static org.hamcrest.Matchers.anyOf; -import static org.hamcrest.Matchers.empty; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.everyItem; -import static org.hamcrest.Matchers.greaterThan; -import static org.hamcrest.Matchers.greaterThanOrEqualTo; -import static org.hamcrest.Matchers.hasItem; -import static org.hamcrest.Matchers.hasProperty; -import static org.hamcrest.Matchers.hasSize; -import static org.hamcrest.Matchers.instanceOf; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.lessThanOrEqualTo; -import static org.hamcrest.Matchers.not; -import static org.hamcrest.Matchers.notNullValue; -import static org.hamcrest.Matchers.startsWith; -import static org.hl7.fhir.r4.model.Patient.SP_FAMILY; -import static org.hl7.fhir.r4.model.Patient.SP_GIVEN; -import static org.openmrs.module.fhir2.matchers.FhirMatchers.isDeceased; - @ContextConfiguration(classes = TestFhirSpringConfiguration.class, inheritLocations = false) public class PatientSearchQueryTest extends BaseModuleContextSensitiveTest { @@ -151,7 +151,7 @@ public class PatientSearchQueryTest extends BaseModuleContextSensitiveTest { @Autowired private FhirGlobalPropertyService globalPropertyService; - + @Autowired @Qualifier("adminService") private AdministrationService administrationService; diff --git a/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/ObservationValueTranslatorImplTest.java b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/ObservationValueTranslatorImplTest.java index 335c05dac..bbb1d200b 100644 --- a/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/ObservationValueTranslatorImplTest.java +++ b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/ObservationValueTranslatorImplTest.java @@ -15,11 +15,9 @@ import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.nullValue; -import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import static org.openmrs.module.fhir2.FhirConstants.RX_NORM_SYSTEM_URI; import static org.openmrs.module.fhir2.FhirConstants.UCUM_SYSTEM_URI; -import static org.powermock.api.mockito.PowerMockito.mockStatic; import java.util.Date; @@ -35,37 +33,73 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; import org.openmrs.Concept; -import org.openmrs.ConceptDatatype; import org.openmrs.ConceptNumeric; import org.openmrs.Obs; import org.openmrs.api.ConceptService; -import org.openmrs.api.context.Context; -import org.openmrs.module.fhir2.api.translators.ConceptTranslator; -import org.powermock.core.classloader.annotations.PrepareOnlyThisForTest; -import org.powermock.modules.junit4.PowerMockRunner; +import org.openmrs.module.fhir2.api.dao.FhirConceptSourceDao; +import org.openmrs.module.fhir2.api.dao.impl.FhirConceptDaoImpl; +import org.openmrs.module.fhir2.api.dao.impl.FhirConceptSourceDaoImpl; +import org.openmrs.module.fhir2.api.impl.FhirConceptServiceImpl; +import org.openmrs.module.fhir2.api.impl.FhirConceptSourceServiceImpl; -@RunWith(PowerMockRunner.class) -@PrepareOnlyThisForTest({ Context.class }) +@RunWith(MockitoJUnitRunner.class) public class ObservationValueTranslatorImplTest { private static final String CONCEPT_VALUE_UUID = "12345-abcde-54321"; private static final String OBS_STRING = "An ingenious observation"; - @Mock - private ConceptTranslator conceptTranslator; + private ConceptTranslatorImpl conceptTranslator; @Mock private ObservationQuantityCodingTranslatorImpl quantityCodingTranslator; + @Mock + ConceptService conceptService; + private Obs obs; private ObservationValueTranslatorImpl obsValueTranslator; @Before public void setup() { - obsValueTranslator = new ObservationValueTranslatorImpl(); + FhirConceptDaoImpl fhirConceptDao = new FhirConceptDaoImpl(); + fhirConceptDao.setConceptService(conceptService); + FhirConceptServiceImpl fhirConceptService = new FhirConceptServiceImpl(); + fhirConceptService.setDao(fhirConceptDao); + FhirConceptSourceDao fhirConceptSourceDao = new FhirConceptSourceDaoImpl(); + FhirConceptSourceServiceImpl fhirConceptSourceService = new FhirConceptSourceServiceImpl(); + fhirConceptSourceService.setDao(fhirConceptSourceDao); + conceptTranslator = new ConceptTranslatorImpl(); + conceptTranslator.setConceptService(fhirConceptService); + conceptTranslator.setConceptSourceService(fhirConceptSourceService); + obsValueTranslator = new ObservationValueTranslatorImpl() { + + @Override + protected Boolean getValueBoolean(Obs obs) { + if (obs.getValueCoded() != null) { + if (obs.getValueCoded().equals(conceptService.getTrueConcept())) { + return true; + } else if (obs.getValueCoded().equals(conceptService.getFalseConcept())) { + return false; + } + } + return null; + } + + @Override + protected void setValueBoolean(Obs obs, Boolean valueBoolean) { + if (valueBoolean == Boolean.TRUE) { + obs.setValueCoded(conceptService.getTrueConcept()); + } else if (valueBoolean == Boolean.FALSE) { + obs.setValueCoded(conceptService.getFalseConcept()); + } else { + obs.setValueCoded(null); + } + } + }; obsValueTranslator.setConceptTranslator(conceptTranslator); obsValueTranslator.setQuantityCodingTranslator(quantityCodingTranslator); @@ -78,15 +112,12 @@ public void toFhirResource_shouldConvertObsWithCodedValueToCodeableConcept() { concept.setUuid(CONCEPT_VALUE_UUID); obs.setValueCoded(concept); - CodeableConcept codeableConcept = new CodeableConcept(); - codeableConcept.setId(CONCEPT_VALUE_UUID); - when(conceptTranslator.toFhirResource(concept)).thenReturn(codeableConcept); - Type result = obsValueTranslator.toFhirResource(obs); assertThat(result, notNullValue()); assertThat(result, instanceOf(CodeableConcept.class)); - assertThat(result.getId(), equalTo(CONCEPT_VALUE_UUID)); + CodeableConcept codeableConceptResult = (CodeableConcept) result; + assertThat(codeableConceptResult.getCodingFirstRep().getCode(), equalTo(CONCEPT_VALUE_UUID)); } @Test @@ -171,20 +202,13 @@ public void toFhirResource_shouldConvertObsWithCommonUCUMUnitsToQuantityWithUCUM @Test public void toFhirResource_shouldConvertObsWithBooleanValueToBoolean() { - mockStatic(Context.class); Concept trueConcept = new Concept(); trueConcept.setId(1046); - ConceptService conceptService = mock(ConceptService.class); - when(Context.getConceptService()).thenReturn(conceptService); when(conceptService.getTrueConcept()).thenReturn(trueConcept); - ConceptDatatype booleanDatatype = mock(ConceptDatatype.class); - when(booleanDatatype.isBoolean()).thenReturn(true); - Concept obsConcept = new Concept(); - obsConcept.setDatatype(booleanDatatype); obs.setConcept(obsConcept); - obs.setValueBoolean(true); + obs.setValueCoded(conceptService.getTrueConcept()); Type result = obsValueTranslator.toFhirResource(obs); @@ -214,11 +238,11 @@ public void toFhirResource_shouldReturnNullIfObsIsNull() { @Test public void toOpenmrsType_shouldConvertCodeableConceptToConcept() { CodeableConcept codeableConcept = new CodeableConcept(); - codeableConcept.setId(CONCEPT_VALUE_UUID); + codeableConcept.addCoding(new Coding(null, CONCEPT_VALUE_UUID, CONCEPT_VALUE_UUID)); Concept concept = new Concept(); concept.setUuid(CONCEPT_VALUE_UUID); - when(conceptTranslator.toOpenmrsType(codeableConcept)).thenReturn(concept); + when(conceptService.getConceptByUuid(CONCEPT_VALUE_UUID)).thenReturn(concept); Obs result = obsValueTranslator.toOpenmrsType(obs, codeableConcept); @@ -262,18 +286,11 @@ public void toOpenmrsType_shouldConvertQuantityToNumericValue() { @Test public void toOpenmrsType_shouldConvertBooleanToBooleanValue() { - mockStatic(Context.class); Concept trueConcept = new Concept(); trueConcept.setId(1046); - ConceptService conceptService = mock(ConceptService.class); - when(Context.getConceptService()).thenReturn(conceptService); when(conceptService.getTrueConcept()).thenReturn(trueConcept); - ConceptDatatype booleanDatatype = mock(ConceptDatatype.class); - when(booleanDatatype.isBoolean()).thenReturn(true); Concept obsConcept = new Concept(); - obsConcept.setDatatype(booleanDatatype); - obs.setConcept(obsConcept); BooleanType booleanType = new BooleanType(); @@ -282,7 +299,7 @@ public void toOpenmrsType_shouldConvertBooleanToBooleanValue() { Obs result = obsValueTranslator.toOpenmrsType(obs, booleanType); assertThat(result, notNullValue()); - assertThat(result.getValueBoolean(), is(true)); + assertThat(result.getValueCoded(), is(conceptService.getTrueConcept())); } @Test diff --git a/omod/src/test/java/org/openmrs/module/fhir2/web/servlet/FhirRestServletTest.java b/omod/src/test/java/org/openmrs/module/fhir2/web/servlet/FhirRestServletTest.java index 5d7c01bd1..8629ce6f3 100644 --- a/omod/src/test/java/org/openmrs/module/fhir2/web/servlet/FhirRestServletTest.java +++ b/omod/src/test/java/org/openmrs/module/fhir2/web/servlet/FhirRestServletTest.java @@ -14,6 +14,11 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import javax.servlet.ServletConfig; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + import java.io.IOException; import java.io.PrintWriter; @@ -23,63 +28,59 @@ import org.mockito.MockitoAnnotations; import org.openmrs.util.OpenmrsClassLoader; -import javax.servlet.ServletConfig; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - public class FhirRestServletTest { - - @Mock - private HttpServletRequest mockRequest; - - @Mock - private HttpServletResponse mockResponse; - - @Mock - private ServletConfig mockServletConfig; - - @Mock - private PrintWriter mockWriter; - - private TestableFhirRestServlet servlet; - - @Before - public void setUp() throws ServletException, IOException { - MockitoAnnotations.initMocks(this); - - servlet = new TestableFhirRestServlet(); - - when(mockServletConfig.getServletContext()).thenReturn(mock(javax.servlet.ServletContext.class)); - when(mockResponse.getWriter()).thenReturn(mockWriter); - - servlet.init(mockServletConfig); - } - - @Test - public void testServiceSetsContextClassLoader() throws ServletException, IOException { - // setup - when(mockRequest.getMethod()).thenReturn("GET"); - when(mockRequest.getRequestURI()).thenReturn("/fhir"); - - ClassLoader initialClassLoader = Thread.currentThread().getContextClassLoader(); - - assertNotEquals("ContextClassLoader should not be OpenmrsClassLoader initially", - OpenmrsClassLoader.getInstance().getClass().getName(), initialClassLoader.getClass().getName()); - - // replay - servlet.service(mockRequest, mockResponse); - - // Verify - ClassLoader newClassLoader = Thread.currentThread().getContextClassLoader(); - - assertEquals("ContextClassLoader should be OpenmrsClassLoader after service method", - OpenmrsClassLoader.getInstance().getClass().getName(), newClassLoader.getClass().getName()); - } - - class TestableFhirRestServlet extends FhirRestServlet { - @Override - public void initialize() { - } - } -} \ No newline at end of file + + @Mock + private HttpServletRequest mockRequest; + + @Mock + private HttpServletResponse mockResponse; + + @Mock + private ServletConfig mockServletConfig; + + @Mock + private PrintWriter mockWriter; + + private TestableFhirRestServlet servlet; + + @Before + public void setUp() throws ServletException, IOException { + MockitoAnnotations.initMocks(this); + + servlet = new TestableFhirRestServlet(); + + when(mockServletConfig.getServletContext()).thenReturn(mock(javax.servlet.ServletContext.class)); + when(mockResponse.getWriter()).thenReturn(mockWriter); + + servlet.init(mockServletConfig); + } + + @Test + public void testServiceSetsContextClassLoader() throws ServletException, IOException { + // setup + when(mockRequest.getMethod()).thenReturn("GET"); + when(mockRequest.getRequestURI()).thenReturn("/fhir"); + + ClassLoader initialClassLoader = Thread.currentThread().getContextClassLoader(); + + assertNotEquals("ContextClassLoader should not be OpenmrsClassLoader initially", + OpenmrsClassLoader.getInstance().getClass().getName(), initialClassLoader.getClass().getName()); + + // replay + servlet.service(mockRequest, mockResponse); + + // Verify + ClassLoader newClassLoader = Thread.currentThread().getContextClassLoader(); + + assertEquals("ContextClassLoader should be OpenmrsClassLoader after service method", + OpenmrsClassLoader.getInstance().getClass().getName(), newClassLoader.getClass().getName()); + } + + class TestableFhirRestServlet extends FhirRestServlet { + + @Override + public void initialize() { + } + } +}