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 d703738 commit d063290
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import java.util.Date;

import junit.framework.TestCase;
import org.hl7.fhir.r4.model.Period;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -26,7 +25,7 @@
import org.openmrs.module.fhir2.api.translators.EncounterPeriodTranslator;

@RunWith(MockitoJUnitRunner.class)
public class EncounterPeriodTranslatorImplTest extends TestCase {
public class EncounterPeriodTranslatorImplTest {

private EncounterPeriodTranslator<Encounter> encounterPeriodTranslator;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;

import org.hibernate.SessionFactory;
import org.hl7.fhir.r4.model.CodeableConcept;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.openmrs.Concept;
import org.openmrs.ConceptClass;
import org.openmrs.module.fhir2.TestFhirSpringConfiguration;
import org.openmrs.module.fhir2.api.mappings.ObservationCategoryMap;
import org.openmrs.module.fhir2.api.translators.ObservationCategoryTranslator;
import org.openmrs.test.BaseModuleContextSensitiveTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
Expand All @@ -37,27 +35,17 @@ public class ObservationCategoryTranslatorImplTest extends BaseModuleContextSens

private final String EXAM_CONCEPT_CLASS_UUID = "8d491a9a-c2cc-11de-8d13-0010c6dffd0f";

@Mock
private ObservationCategoryMap categoryMap;

@Autowired
private SessionFactory sessionFactory;

private Concept concept;

private CodeableConcept codeableConcept;

private ObservationCategoryTranslatorImpl observationCategoryTranslator;
@Autowired
ObservationCategoryTranslator observationCategoryTranslator;

@Before
public void setup() throws Exception {
observationCategoryTranslator = new ObservationCategoryTranslatorImpl();
categoryMap = new ObservationCategoryMap();
concept = new Concept();
categoryMap.setSessionFactory(sessionFactory);
observationCategoryTranslator.setCategoryMap(categoryMap);

executeDataSet(OBSERVATION_CATEGORY_CONCEPT_CLASS_DATA);
concept = new Concept();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,17 @@
import org.hl7.fhir.r4.model.StringType;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
import org.openmrs.PersonName;
import org.openmrs.api.AdministrationService;
import org.openmrs.api.context.ServiceContext;
import org.openmrs.layout.name.NameSupport;
import org.openmrs.layout.name.NameTemplate;
import org.openmrs.module.fhir2.FhirConstants;
import org.openmrs.serialization.SerializationException;
import org.openmrs.serialization.SimpleXStreamSerializer;

@RunWith(MockitoJUnitRunner.class)
public class PersonNameTranslatorImplTest {

private static final String PERSON_NAME_UUID = "123456-abcdef-123456";
Expand Down Expand Up @@ -166,7 +168,6 @@ public void shouldOnlyCreateOneExtensionForExtensibleAttributes() {
@Test
public void shouldUseDefaultNameTemplateToSetNameText() throws SerializationException {
AdministrationService administrationService = mock(AdministrationService.class);
ServiceContext.getInstance().setAdministrationService(administrationService);
when(administrationService.getGlobalProperty("layout.name.format")).thenReturn("test");

NameSupport nameSupportInstance = new NameSupport();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Optional;

import org.hl7.fhir.r4.model.ValueSet;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
import org.openmrs.Concept;
import org.openmrs.ConceptDescription;
import org.openmrs.ConceptMap;
Expand All @@ -33,18 +34,15 @@
import org.openmrs.ConceptReferenceTerm;
import org.openmrs.ConceptSet;
import org.openmrs.ConceptSource;
import org.openmrs.api.ConceptService;
import org.openmrs.api.context.ServiceContext;
import org.openmrs.module.fhir2.FhirTestConstants;
import org.openmrs.module.fhir2.api.FhirConceptSourceService;
import org.openmrs.module.fhir2.model.FhirConceptSource;

@RunWith(MockitoJUnitRunner.class)
public class ValueSetTranslatorImplTest {

private static final String CONCEPT_UUID = "0f97e14e-cdc2-49ac-9255-b5126f8a5147";

private ConceptService conceptService;

private FhirConceptSourceService conceptSourceService;

private Concept concept;
Expand All @@ -53,17 +51,13 @@ public class ValueSetTranslatorImplTest {

@Before
public void setup() {
conceptService = mock(ConceptService.class);
conceptSourceService = mock(FhirConceptSourceService.class);
concept = mock(Concept.class);
valueSetTranslator.setConceptSourceService(conceptSourceService);
}

@Test
public void shouldTranslateConceptSetToValueSet() {
when(conceptService.getDefaultConceptMapType()).thenReturn(new ConceptMapType());
ServiceContext.getInstance().setConceptService(conceptService);

ConceptName conceptName = new ConceptName();
conceptName.setName("test");

Expand All @@ -84,16 +78,13 @@ public void shouldTranslateConceptSetToValueSet() {
when(conceptMap.getConceptReferenceTerm()).thenReturn(conceptReferenceTerm);
when(conceptMap.getConceptMapType()).thenReturn(conceptMapType);
when(conceptReferenceTerm.getConceptSource()).thenReturn(conceptSource);
when(conceptReferenceTerm.getCode()).thenReturn("1000-1");
when(conceptSource.getName()).thenReturn("LOINC");
concept1.addConceptMapping(conceptMap);

FhirConceptSource loinc = new FhirConceptSource();
ConceptSource loincConceptSource = new ConceptSource();
loincConceptSource.setName("LOINC");
loinc.setConceptSource(loincConceptSource);
loinc.setUrl(FhirTestConstants.LOINC_SYSTEM_URL);
when(conceptSourceService.getFhirConceptSource(loincConceptSource)).thenReturn(Optional.of(loinc));

ConceptSet conceptSet = new ConceptSet();
conceptSet.setConceptSet(concept);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,17 @@
*/
package org.openmrs.module.fhir2.providers;

import static org.mockito.Mockito.when;

import java.util.Arrays;
import java.util.Date;

import org.hl7.fhir.r4.model.CodeableConcept;
import org.hl7.fhir.r4.model.Coding;
import org.hl7.fhir.r4.model.DomainResource;
import org.hl7.fhir.r4.model.Practitioner;
import org.hl7.fhir.r4.model.Provenance;
import org.hl7.fhir.r4.model.Reference;
import org.mockito.Mock;
import org.openmrs.User;
import org.openmrs.module.fhir2.FhirConstants;
import org.openmrs.module.fhir2.api.translators.PractitionerReferenceTranslator;

public abstract class BaseFhirProvenanceResourceTest<T extends DomainResource> {

private static final String PRACTITIONER_UUID = "2ffb1a5f-bcd3-4243-8f40-78edc2642789";

private static final String AGENT_TYPE_CODE = "author";

private static final String AGENT_TYPE_DISPLAY = "Author";

private static final String AGENT_ROLE_CODE = "AUT";

private static final String AGENT_ROLE_DISPLAY = "author";

private static final String UPDATE = "Update";

private static final String REVISE = "revise";
Expand All @@ -45,9 +28,6 @@ public abstract class BaseFhirProvenanceResourceTest<T extends DomainResource> {

private static final String CREATE_DISPLAY = "create";

@Mock
private PractitionerReferenceTranslator<User> practitionerReferenceTranslator;

public void setProvenanceResources(T resource) {
resource.setContained(Arrays.asList(onCreateDataOperation(), onUpdateDataOperation()));
}
Expand All @@ -58,8 +38,7 @@ private Provenance onUpdateDataOperation() {
coding.setDisplay(REVISE);
coding.setSystem(FhirConstants.FHIR_TERMINOLOGY_DATA_OPERATION);

return new Provenance().setActivity(new CodeableConcept().addCoding(coding)).setRecorded(new Date())
.addAgent(addAgent());
return new Provenance().setActivity(new CodeableConcept().addCoding(coding)).setRecorded(new Date());
}

private Provenance onCreateDataOperation() {
Expand All @@ -68,32 +47,7 @@ private Provenance onCreateDataOperation() {
coding.setDisplay(CREATE_DISPLAY);
coding.setSystem(FhirConstants.FHIR_TERMINOLOGY_DATA_OPERATION);

return new Provenance().setActivity(new CodeableConcept().addCoding(coding)).setRecorded(new Date())
.addAgent(addAgent());
}

private Provenance.ProvenanceAgentComponent addAgent() {
User user = new User();
user.setUuid(PRACTITIONER_UUID);
Practitioner practitioner = new Practitioner();
practitioner.setId(PRACTITIONER_UUID);
when(practitionerReferenceTranslator.toFhirResource(user)).thenReturn(createPractitionerReference());
return new Provenance.ProvenanceAgentComponent().setType(addAgentType()).addRole(addAgentRole())
.setWho(practitionerReferenceTranslator.toFhirResource(user));
}

private CodeableConcept addAgentType() {
return new CodeableConcept().addCoding(new Coding().setCode(AGENT_TYPE_CODE)
.setSystem(FhirConstants.FHIR_TERMINOLOGY_PROVENANCE_PARTICIPANT_TYPE).setDisplay(AGENT_TYPE_DISPLAY));
}

private CodeableConcept addAgentRole() {
return new CodeableConcept().addCoding(new Coding().setCode(AGENT_ROLE_CODE)
.setSystem(FhirConstants.FHIR_TERMINOLOGY_PARTICIPATION_TYPE).setDisplay(AGENT_ROLE_DISPLAY));
}

private Reference createPractitionerReference() {
return new Reference().setReference(FhirConstants.PRACTITIONER + "/" + PRACTITIONER_UUID);
return new Provenance().setActivity(new CodeableConcept().addCoding(coding)).setRecorded(new Date());
}

}

0 comments on commit d063290

Please sign in to comment.