Skip to content

Commit

Permalink
Additional fixes to mocked context sensitive tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mseaton committed Dec 20, 2024
1 parent 01ba800 commit d703738
Show file tree
Hide file tree
Showing 15 changed files with 211 additions and 268 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -38,7 +37,7 @@
import org.springframework.stereotype.Component;

@Component
@Setter(AccessLevel.PACKAGE)
@Setter
public class FhirConceptDaoImpl extends BaseFhirDao<Concept> implements FhirConceptDao {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,7 +25,7 @@

@Component
@Transactional
@Setter(AccessLevel.PACKAGE)
@Setter
public class FhirConceptServiceImpl implements FhirConceptService {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,7 +27,7 @@

@Component
@Transactional
@Setter(AccessLevel.PACKAGE)
@Setter
public class FhirConceptSourceServiceImpl implements FhirConceptSourceService {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
}

Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand All @@ -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");
}

/**
Expand Down
Loading

0 comments on commit d703738

Please sign in to comment.