diff --git a/api/pom.xml b/api/pom.xml
index e96b8fad8..13343bae3 100644
--- a/api/pom.xml
+++ b/api/pom.xml
@@ -59,7 +59,13 @@
test-jar
test
-
+
+ org.springframework
+ spring-context
+ 4.1.4.RELEASE
+ compile
+
+
diff --git a/api/src/main/java/org/openmrs/module/fhir2/FhirConstants.java b/api/src/main/java/org/openmrs/module/fhir2/FhirConstants.java
index c5d976f45..491b9eee6 100644
--- a/api/src/main/java/org/openmrs/module/fhir2/FhirConstants.java
+++ b/api/src/main/java/org/openmrs/module/fhir2/FhirConstants.java
@@ -69,6 +69,8 @@ private FhirConstants() {
public static final String ENCOUNTER_CLASS_VALUE_SET_URI = HL7_FHIR_CODE_SYSTEM_PREFIX + "/v3-ActCode";
+ public static final String MEDIA_CREATED_DATE_TIME = "media.created.date.time";
+
@Value("${project.version}")
public static String OPENMRS_FHIR_SERVER_VERSION;
@@ -301,4 +303,25 @@ private FhirConstants() {
public static final String REVERSE_INCLUDE_SEARCH_HANDLER = "_revinclude.search.handler";
public static final String CONDITION_OBSERVATION_CONCEPT_UUID = "1284AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
+
+ public static final String MEDIA = "media";
+
+ public static final String MEDIA_STATUS = "media.status";
+
+ public static final String MEDIA_TYPE = "media.type";
+
+ public static final String MEDIA_SUBJECT = "media.subject";
+
+ public static final String MEDIA_ENCOUNTER_REFERENCE = "media.encounter.reference";
+
+ public static final String MEDIA_CONTENT_TYPE = "media.content.type";
+
+ public static final String CONTENT_DATA = "content.data";
+
+ public static final String CONTENT_TITLE = "content.title";
+
+ public static final String CONTENT_DATE_OF_CREATION = "content.creation";
+
+ public static final String SAVED_SUCCESSFULLY = "Saved successfully";
+
}
diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/FhirMediaService.java b/api/src/main/java/org/openmrs/module/fhir2/api/FhirMediaService.java
new file mode 100644
index 000000000..2b2778558
--- /dev/null
+++ b/api/src/main/java/org/openmrs/module/fhir2/api/FhirMediaService.java
@@ -0,0 +1,30 @@
+/*
+ * 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.api;
+
+import java.util.HashSet;
+
+import ca.uhn.fhir.model.api.Include;
+import ca.uhn.fhir.rest.api.SortSpec;
+import ca.uhn.fhir.rest.api.server.IBundleProvider;
+import ca.uhn.fhir.rest.param.DateRangeParam;
+import ca.uhn.fhir.rest.param.ReferenceAndListParam;
+import ca.uhn.fhir.rest.param.StringAndListParam;
+import ca.uhn.fhir.rest.param.TokenAndListParam;
+import org.hl7.fhir.r4.model.Media;
+
+public interface FhirMediaService extends FhirService {
+
+ IBundleProvider searchForMedia(TokenAndListParam status, TokenAndListParam type, ReferenceAndListParam subject,
+ ReferenceAndListParam encounterReference, DateRangeParam createdDateTime, TokenAndListParam contentType,
+ TokenAndListParam id, StringAndListParam contentDataType, StringAndListParam contentTitle,
+ DateRangeParam contentCreated, DateRangeParam lastUpdated, HashSet includes,
+ HashSet revIncludes, SortSpec sort);
+}
diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/dao/FhirMediaDao.java b/api/src/main/java/org/openmrs/module/fhir2/api/dao/FhirMediaDao.java
new file mode 100644
index 000000000..cc5bc5b7f
--- /dev/null
+++ b/api/src/main/java/org/openmrs/module/fhir2/api/dao/FhirMediaDao.java
@@ -0,0 +1,39 @@
+/*
+ * 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.api.dao;
+
+import javax.annotation.Nonnull;
+
+import java.util.List;
+
+import org.openmrs.Obs;
+import org.openmrs.annotation.Authorized;
+import org.openmrs.module.fhir2.api.search.param.SearchParameterMap;
+import org.openmrs.util.PrivilegeConstants;
+
+public interface FhirMediaDao extends FhirDao {
+
+ @Override
+ @Authorized(PrivilegeConstants.GET_OBS)
+ Obs get(@Nonnull String uuid);
+
+ @Override
+ @Authorized(PrivilegeConstants.ADD_OBS)
+ Obs createOrUpdate(@Nonnull Obs newEntry);
+
+ @Override
+ @Authorized(PrivilegeConstants.DELETE_OBS)
+ Obs delete(@Nonnull String uuid);
+
+ @Override
+ @Authorized(PrivilegeConstants.GET_OBS)
+ List getSearchResultUuids(@Nonnull SearchParameterMap theParams);
+
+}
diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/BaseDao.java b/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/BaseDao.java
index 9d64ccfac..cee90108f 100644
--- a/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/BaseDao.java
+++ b/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/BaseDao.java
@@ -156,7 +156,7 @@
* AND crt.code in (?, ?, ?)
* ) OR (
* crt.concept_source_id = (select concept_source_id from fhir_concept_source where url = ?)
- * AND crt.code = ?
+ * AND crt.code = ?fhir_concept_source
* )) AND (
* crt.concept_source_id = (select concept_source_id from fhir_concept_source where url = ?)
* AND crt.code in (?, ?, ?)
diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirMediaDaoImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirMediaDaoImpl.java
new file mode 100644
index 000000000..6b92324c2
--- /dev/null
+++ b/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirMediaDaoImpl.java
@@ -0,0 +1,119 @@
+/*
+ * 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.api.dao.impl;
+
+import static org.hibernate.criterion.Restrictions.eq;
+
+import javax.annotation.Nonnull;
+
+import java.util.List;
+import java.util.Optional;
+import java.util.stream.Collectors;
+
+import ca.uhn.fhir.rest.param.DateParam;
+import ca.uhn.fhir.rest.param.ReferenceAndListParam;
+import ca.uhn.fhir.rest.param.TokenAndListParam;
+import lombok.AccessLevel;
+import lombok.Setter;
+import org.hibernate.Criteria;
+import org.openmrs.Obs;
+import org.openmrs.api.ObsService;
+import org.openmrs.api.context.Context;
+import org.openmrs.module.fhir2.FhirConstants;
+import org.openmrs.module.fhir2.api.dao.FhirMediaDao;
+import org.openmrs.module.fhir2.api.search.param.SearchParameterMap;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+@Component
+@Setter(AccessLevel.PACKAGE)
+public class FhirMediaDaoImpl extends BaseFhirDao implements FhirMediaDao{
+
+ private ObsService obsService = Context.getObsService();
+
+ @Override
+ public Obs get(@Nonnull String uuid) {
+ return obsService.getObsByUuid(uuid);
+ }
+
+ @Override
+ public Obs createOrUpdate(@Nonnull Obs newEntry) {
+ return newEntry;
+ }
+
+ @Override
+ public Obs delete(@Nonnull String uuid) {
+ return obsService.getObsByUuid(uuid);
+ }
+
+ @Override
+ public List getSearchResults(@Nonnull SearchParameterMap theParams, @Nonnull List resourceUuids) {
+ return resourceUuids.stream().map(obsService::getObsByUuid).collect(Collectors.toList());
+ }
+
+ @Override
+ protected void setupSearchParams(Criteria criteria, SearchParameterMap theParams) {
+ theParams.getParameters().forEach(entry -> {
+ switch (entry.getKey()) {
+ case FhirConstants.MEDIA_STATUS:
+ entry.getValue().forEach(status -> handleStatus(criteria, (TokenAndListParam) status.getParam()));
+ break;
+ case FhirConstants.MEDIA_TYPE:
+ entry.getValue().forEach(type -> handleMediaType(criteria, (TokenAndListParam) type.getParam()));
+ break;
+ case FhirConstants.MEDIA_SUBJECT:
+ entry.getValue()
+ .forEach(subject -> handleMediaSubject(criteria, (ReferenceAndListParam) subject.getParam()));
+ break;
+ case FhirConstants.MEDIA_ENCOUNTER_REFERENCE:
+ break;
+ case FhirConstants.MEDIA_CREATED_DATE_TIME:
+ entry.getValue().forEach(
+ createdTime -> handleDate(createdTime.getPropertyName(), (DateParam) createdTime.getParam())
+ .ifPresent(criteria::add));
+ break;
+ case FhirConstants.MEDIA_CONTENT_TYPE:
+ break;
+ case FhirConstants.CONTENT_DATA:
+ break;
+ case FhirConstants.CONTENT_TITLE:
+ break;
+ case FhirConstants.CONTENT_DATE_OF_CREATION:
+ entry.getValue().forEach(contentDateOfCreation -> handleDate(contentDateOfCreation.getPropertyName(),
+ (DateParam) contentDateOfCreation.getParam()));
+ break;
+ }
+ });
+ }
+
+ private void handleStatus(Criteria criteria, TokenAndListParam status) {
+ // handleAndListParam(status, (data) -> propertyLike("status", status)).ifPresent(criteria::add);
+ }
+
+ private void handleMediaType(Criteria criteria, TokenAndListParam mediaType) {
+ if (mediaType != null) {
+ if (lacksAlias(criteria, "mt")) {
+ criteria.createAlias("mediaType", "mt");
+ handleAndListParam(mediaType, (tag) -> Optional.of(eq("mt.type", tag.getValue()))).ifPresent(criteria::add);
+ }
+ }
+ }
+
+ private void handleMediaSubject(Criteria criteria, ReferenceAndListParam mediaSubject) {
+ if (mediaSubject != null) {
+ if (lacksAlias(criteria, "ms")) {
+ criteria.createAlias("mediaSubject", "ms");
+ handleAndListParam(mediaSubject, (tag) -> Optional.of(eq("ms.subject", tag.getValue())))
+ .ifPresent(criteria::add);
+
+ }
+ }
+ }
+}
diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/impl/FhirMediaServiceImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/impl/FhirMediaServiceImpl.java
new file mode 100644
index 000000000..b78204d4f
--- /dev/null
+++ b/api/src/main/java/org/openmrs/module/fhir2/api/impl/FhirMediaServiceImpl.java
@@ -0,0 +1,77 @@
+/*
+ * 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.api.impl;
+
+import java.util.HashSet;
+
+import ca.uhn.fhir.model.api.Include;
+import ca.uhn.fhir.rest.api.SortSpec;
+import ca.uhn.fhir.rest.api.server.IBundleProvider;
+import ca.uhn.fhir.rest.param.DateRangeParam;
+import ca.uhn.fhir.rest.param.ReferenceAndListParam;
+import ca.uhn.fhir.rest.param.StringAndListParam;
+import ca.uhn.fhir.rest.param.TokenAndListParam;
+import lombok.AccessLevel;
+import lombok.Getter;
+import lombok.Setter;
+import org.hl7.fhir.r4.model.Media;
+import org.openmrs.Obs;
+import org.openmrs.module.fhir2.FhirConstants;
+import org.openmrs.module.fhir2.api.FhirMediaService;
+import org.openmrs.module.fhir2.api.dao.FhirMediaDao;
+import org.openmrs.module.fhir2.api.search.SearchQuery;
+import org.openmrs.module.fhir2.api.search.SearchQueryInclude;
+import org.openmrs.module.fhir2.api.search.param.SearchParameterMap;
+import org.openmrs.module.fhir2.api.translators.MediaTranslator;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+import org.springframework.transaction.annotation.Transactional;
+
+@Component
+@Transactional
+@Getter(AccessLevel.PROTECTED)
+@Setter(AccessLevel.MODULE)
+public class FhirMediaServiceImpl extends BaseFhirService implements FhirMediaService {
+
+ @Autowired
+ private FhirMediaDao dao;
+
+ @Autowired
+ private MediaTranslator translator;
+
+ @Autowired
+ private SearchQuery> searchQuery;
+
+ @Autowired
+ private SearchQueryInclude searchQueryInclude;
+
+ @Override
+ public IBundleProvider searchForMedia(TokenAndListParam status, TokenAndListParam type, ReferenceAndListParam subject,
+ ReferenceAndListParam encounterReference, DateRangeParam createdDateTime, TokenAndListParam contentType,
+ TokenAndListParam id, StringAndListParam contentDataType, StringAndListParam contentTitle,
+ DateRangeParam contentCreated, DateRangeParam lastUpdated, HashSet includes,
+ HashSet revIncludes, SortSpec sort) {
+
+ SearchParameterMap theParams = new SearchParameterMap().addParameter(FhirConstants.MEDIA_STATUS, status)
+ .addParameter(FhirConstants.MEDIA_TYPE, type).addParameter(FhirConstants.MEDIA_SUBJECT, subject)
+ .addParameter(FhirConstants.MEDIA_ENCOUNTER_REFERENCE, encounterReference)
+ .addParameter(FhirConstants.MEDIA_CREATED_DATE_TIME, createdDateTime)
+ .addParameter(FhirConstants.MEDIA_CONTENT_TYPE, contentType)
+ .addParameter(FhirConstants.CONTENT_DATA, contentDataType)
+ .addParameter(FhirConstants.CONTENT_TITLE, contentTitle)
+ .addParameter(FhirConstants.CONTENT_DATE_OF_CREATION, contentCreated)
+ .addParameter(FhirConstants.COMMON_SEARCH_HANDLER, FhirConstants.ID_PROPERTY, id)
+ .addParameter(FhirConstants.COMMON_SEARCH_HANDLER, FhirConstants.LAST_UPDATED_PROPERTY, lastUpdated)
+ .addParameter(FhirConstants.COMMON_SEARCH_HANDLER, includes)
+ .addParameter(FhirConstants.COMMON_SEARCH_HANDLER, revIncludes).setSortSpec(sort);
+
+ return searchQuery.getQueryResults(theParams, dao, translator, searchQueryInclude);
+ }
+}
diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/search/SearchQueryInclude.java b/api/src/main/java/org/openmrs/module/fhir2/api/search/SearchQueryInclude.java
index d4afa5dde..df0719fcd 100644
--- a/api/src/main/java/org/openmrs/module/fhir2/api/search/SearchQueryInclude.java
+++ b/api/src/main/java/org/openmrs/module/fhir2/api/search/SearchQueryInclude.java
@@ -24,17 +24,7 @@
import lombok.NoArgsConstructor;
import org.apache.commons.collections.CollectionUtils;
import org.hl7.fhir.instance.model.api.IBaseResource;
-import org.hl7.fhir.r4.model.AllergyIntolerance;
-import org.hl7.fhir.r4.model.Condition;
-import org.hl7.fhir.r4.model.DiagnosticReport;
-import org.hl7.fhir.r4.model.Encounter;
-import org.hl7.fhir.r4.model.Location;
-import org.hl7.fhir.r4.model.MedicationRequest;
-import org.hl7.fhir.r4.model.Observation;
-import org.hl7.fhir.r4.model.Person;
-import org.hl7.fhir.r4.model.Reference;
-import org.hl7.fhir.r4.model.RelatedPerson;
-import org.hl7.fhir.r4.model.ServiceRequest;
+import org.hl7.fhir.r4.model.*;
import org.openmrs.module.fhir2.FhirConstants;
import org.openmrs.module.fhir2.api.FhirAllergyIntoleranceService;
import org.openmrs.module.fhir2.api.FhirDiagnosticReportService;
diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/translators/MediaContentTranslator.java b/api/src/main/java/org/openmrs/module/fhir2/api/translators/MediaContentTranslator.java
new file mode 100644
index 000000000..e3d41446e
--- /dev/null
+++ b/api/src/main/java/org/openmrs/module/fhir2/api/translators/MediaContentTranslator.java
@@ -0,0 +1,37 @@
+/*
+ * 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.api.translators;
+
+import javax.annotation.Nonnull;
+
+import org.hl7.fhir.r4.model.Media;
+import org.openmrs.Obs;
+
+public interface MediaContentTranslator extends OpenmrsFhirUpdatableTranslator {
+
+ /**
+ * Maps an {@link org.openmrs.Obs} to a corresponding {@link org.hl7.fhir.r4.model.Type}
+ *
+ * @param data the obs data to translate
+ * @return the corresponding FHIR base64 encoded version of the data
+ */
+ @Override
+ Media toFhirResource(@Nonnull Obs data);
+
+ /**
+ * Maps an {@link org.hl7.fhir.r4.model.Type} to a existing {@link org.openmrs.Obs}
+ *
+ * @param existingObject the obs data to update
+ * @param resource the FHIR base64 encoded version of the data to map
+ * @return an updated version of the obs data
+ */
+ @Override
+ Obs toOpenmrsType(@Nonnull Obs existingObject, @Nonnull Media resource);
+}
diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/translators/MediaStatusTranslator.java b/api/src/main/java/org/openmrs/module/fhir2/api/translators/MediaStatusTranslator.java
new file mode 100644
index 000000000..1e9ec8cca
--- /dev/null
+++ b/api/src/main/java/org/openmrs/module/fhir2/api/translators/MediaStatusTranslator.java
@@ -0,0 +1,37 @@
+/*
+ * 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.api.translators;
+
+import javax.annotation.Nonnull;
+
+import org.hl7.fhir.r4.model.Media;
+import org.openmrs.Obs;
+
+public interface MediaStatusTranslator extends OpenmrsFhirUpdatableTranslator {
+
+ /**
+ * Maps an {@link Obs} to an {@link org.hl7.fhir.r4.model.Media.MediaStatus}
+ *
+ * @param data the complex obs to translate
+ * @return the corresponding media resource
+ */
+ @Override
+ Media.MediaStatus toFhirResource(@Nonnull Obs data);
+
+ /**
+ * Maps an {@link org.hl7.fhir.r4.model.Media.MediaStatus} to an {@link Obs}
+ *
+ * @param existingObject the observation to update
+ * @param resource the media status
+ * @return the corresponding media resource status
+ */
+ @Override
+ Obs toOpenmrsType(@Nonnull Obs existingObject, @Nonnull Media.MediaStatus resource);
+}
diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/translators/MediaTranslator.java b/api/src/main/java/org/openmrs/module/fhir2/api/translators/MediaTranslator.java
new file mode 100644
index 000000000..267f097a0
--- /dev/null
+++ b/api/src/main/java/org/openmrs/module/fhir2/api/translators/MediaTranslator.java
@@ -0,0 +1,46 @@
+/*
+ * 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.api.translators;
+
+import javax.annotation.Nonnull;
+
+import org.hl7.fhir.r4.model.Media;
+import org.openmrs.Obs;
+
+public interface MediaTranslator extends OpenmrsFhirTranslator, OpenmrsFhirUpdatableTranslator {
+
+ /**
+ * Maps an {@link org.openmrs.Obs} to a {@link org.hl7.fhir.r4.model.Media}
+ *
+ * @param data the FHIR Media to translate
+ * @return the corresponding FHIR media resource
+ */
+ @Override
+ Media toFhirResource(@Nonnull Obs data);
+
+ /**
+ * Maps a {@link org.hl7.fhir.r4.model.Media} to an {@link org.openmrs.Obs}
+ *
+ * @param resource the FHIR Media resource to translate
+ * @return the corresponding OpenMRS observation resource
+ */
+ @Override
+ Obs toOpenmrsType(@Nonnull Media resource);
+
+ /**
+ * Maps a {@link Media} to an existing {@link org.openmrs.Obs}
+ *
+ * @param existingObject the observation to update
+ * @param resource the FHIR complex object to map
+ * @return the updated OpenMRS observation
+ */
+ @Override
+ Obs toOpenmrsType(@Nonnull Obs existingObject, @Nonnull Media resource);
+}
diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/LocationTranslatorImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/LocationTranslatorImpl.java
index e2afc97f8..4e904eafb 100644
--- a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/LocationTranslatorImpl.java
+++ b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/LocationTranslatorImpl.java
@@ -19,7 +19,7 @@
import lombok.AccessLevel;
import lombok.Setter;
-import org.apache.commons.lang.math.NumberUtils;
+import org.apache.commons.lang3.math.NumberUtils;
import org.hl7.fhir.r4.model.Coding;
import org.hl7.fhir.r4.model.ContactPoint;
import org.hl7.fhir.r4.model.Location;
diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/MediaContentTranslatorImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/MediaContentTranslatorImpl.java
new file mode 100644
index 000000000..9880b6350
--- /dev/null
+++ b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/MediaContentTranslatorImpl.java
@@ -0,0 +1,55 @@
+/*
+ * 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.api.translators.impl;
+
+import javax.annotation.Nonnull;
+
+import lombok.AccessLevel;
+import lombok.Setter;
+import org.hl7.fhir.r4.model.Attachment;
+import org.hl7.fhir.r4.model.Base64BinaryType;
+import org.hl7.fhir.r4.model.Media;
+import org.openmrs.Obs;
+import org.openmrs.module.fhir2.api.translators.MediaContentTranslator;
+import org.springframework.stereotype.Component;
+
+@Component
+@Setter(AccessLevel.PACKAGE)
+public class MediaContentTranslatorImpl extends BaseReferenceHandlingTranslator implements MediaContentTranslator {
+
+ @Override
+ public Media toFhirResource(@Nonnull Obs data) {
+ if (data == null) {
+ return null;
+ }
+
+ Media mediaContent = new Media();
+ mediaContent.setContent(new Attachment().setContentType(data.getValueText()));
+ mediaContent.setContent(new Attachment()
+ .setDataElement(new Base64BinaryType().setValue(data.getComplexData().getData().toString().getBytes())));
+ mediaContent.setContent(new Attachment().setTitle(data.getComment()));
+ mediaContent.setContent(new Attachment().setCreation(data.getDateCreated()));
+ return mediaContent;
+ }
+
+ @Override
+ public Obs toOpenmrsType(@Nonnull Obs existingObject, @Nonnull Media resource) {
+ existingObject.setValueText(resource.getContent().getContentType());
+ existingObject.setValueComplex(resource.getContent().getDataElement().getValueAsString());
+ existingObject.setComment(resource.getContent().getTitle());
+ existingObject.setDateCreated(resource.getCreatedDateTimeType().getValue());
+ return existingObject;
+ }
+
+ @Override
+ public Obs toOpenmrsType(@Nonnull Media resource) {
+ return toOpenmrsType(new Obs(), resource);
+ }
+}
diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/MediaStatusTranslatorImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/MediaStatusTranslatorImpl.java
new file mode 100644
index 000000000..3606840fc
--- /dev/null
+++ b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/MediaStatusTranslatorImpl.java
@@ -0,0 +1,45 @@
+/*
+ * 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.api.translators.impl;
+
+import static org.apache.commons.lang3.Validate.notNull;
+
+import javax.annotation.Nonnull;
+
+import lombok.AccessLevel;
+import lombok.Setter;
+import org.hl7.fhir.r4.model.Media;
+import org.openmrs.Obs;
+import org.openmrs.module.fhir2.api.translators.MediaStatusTranslator;
+import org.springframework.stereotype.Component;
+
+@Component
+@Setter(AccessLevel.PACKAGE)
+public class MediaStatusTranslatorImpl implements MediaStatusTranslator {
+
+ @Override
+ public Media.MediaStatus toFhirResource(@Nonnull Obs data) {
+ return Media.MediaStatus.UNKNOWN;
+ }
+
+ @Override
+ public Obs toOpenmrsType(@Nonnull Obs existingObject, @Nonnull Media.MediaStatus resource) {
+ notNull(existingObject, "The existing Obs object should not be null");
+ notNull(resource, "The Media object should not be null");
+
+ return existingObject;
+ }
+
+ @Override
+ public Obs toOpenmrsType(@Nonnull Media.MediaStatus resource) {
+ notNull(resource, "The Media object should not be null");
+ return null;
+ }
+}
diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/MediaTranslatorImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/MediaTranslatorImpl.java
new file mode 100644
index 000000000..6bb09ee67
--- /dev/null
+++ b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/MediaTranslatorImpl.java
@@ -0,0 +1,77 @@
+/*
+ * 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.api.translators.impl;
+
+import static org.apache.commons.lang3.Validate.notNull;
+
+import javax.annotation.Nonnull;
+
+import lombok.AccessLevel;
+import lombok.Setter;
+import org.hibernate.proxy.HibernateProxy;
+import org.hl7.fhir.r4.model.CodeableConcept;
+import org.hl7.fhir.r4.model.DateType;
+import org.hl7.fhir.r4.model.Media;
+import org.hl7.fhir.r4.model.Reference;
+import org.openmrs.Concept;
+import org.openmrs.Encounter;
+import org.openmrs.Obs;
+import org.openmrs.Patient;
+import org.openmrs.Person;
+import org.openmrs.api.db.hibernate.HibernateUtil;
+import org.openmrs.module.fhir2.api.translators.MediaTranslator;
+import org.springframework.stereotype.Component;
+
+@Component
+@Setter(AccessLevel.PACKAGE)
+public class MediaTranslatorImpl extends BaseReferenceHandlingTranslator implements MediaTranslator {
+
+ @Override
+ public Media toFhirResource(@Nonnull Obs data) {
+ notNull(data, "The Openmrs Complex obs object should not be null");
+ Person obsPerson = data.getPerson();
+ Encounter encounter = data.getEncounter();
+ Concept concept = data.getValueCoded();
+ encounter.setLocation(data.getLocation());
+ encounter.setDateCreated(data.getDateCreated());
+
+ if (obsPerson != null) {
+ if (obsPerson instanceof HibernateProxy) {
+ obsPerson = HibernateUtil.getRealObjectFromProxy(obsPerson);
+ }
+ if (obsPerson instanceof Patient) {
+ data.setPerson(obsPerson);
+ }
+ }
+ data.setEncounter(encounter);
+ data.setValueCoded(data.getValueCoded());
+
+ Media media = new Media();
+ media.setType(new CodeableConcept());
+ media.setEncounter(null);
+ media.setSubject(new Reference());
+ media.setCreated(new DateType());
+
+ return media;
+ }
+
+ @Override
+ public Obs toOpenmrsType(@Nonnull Media resource) {
+ notNull(resource, "The media resource should not be null");
+ return toOpenmrsType(new Obs(), resource);
+ }
+
+ @Override
+ public Obs toOpenmrsType(@Nonnull Obs existingObject, @Nonnull Media resource) {
+ notNull(existingObject, "The existing object should not be null");
+ notNull(resource, "The observation object should not be null");
+ return existingObject;
+ }
+}
diff --git a/api/src/main/java/org/openmrs/module/fhir2/providers/r3/MediaFhirResourceProvider.java b/api/src/main/java/org/openmrs/module/fhir2/providers/r3/MediaFhirResourceProvider.java
new file mode 100644
index 000000000..e32ca8670
--- /dev/null
+++ b/api/src/main/java/org/openmrs/module/fhir2/providers/r3/MediaFhirResourceProvider.java
@@ -0,0 +1,28 @@
+/*
+ * 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.providers.r3;
+
+import ca.uhn.fhir.rest.server.IResourceProvider;
+import lombok.AccessLevel;
+import lombok.Setter;
+import org.hl7.fhir.instance.model.api.IBaseResource;
+import org.openmrs.module.fhir2.api.annotations.R3Provider;
+import org.springframework.stereotype.Component;
+
+@Component("mediaFhirR3ResourceProvider")
+@R3Provider
+@Setter(AccessLevel.PACKAGE)
+public class MediaFhirResourceProvider implements IResourceProvider {
+
+ @Override
+ public Class extends IBaseResource> getResourceType() {
+ return null;
+ }
+}
diff --git a/api/src/main/java/org/openmrs/module/fhir2/providers/r4/MediaFhirResourceProvider.java b/api/src/main/java/org/openmrs/module/fhir2/providers/r4/MediaFhirResourceProvider.java
new file mode 100644
index 000000000..e86c42aae
--- /dev/null
+++ b/api/src/main/java/org/openmrs/module/fhir2/providers/r4/MediaFhirResourceProvider.java
@@ -0,0 +1,34 @@
+/*
+ * 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.providers.r4;
+
+import ca.uhn.fhir.rest.server.IResourceProvider;
+import lombok.AccessLevel;
+import lombok.Setter;
+import org.hl7.fhir.instance.model.api.IBaseResource;
+import org.hl7.fhir.r4.model.Media;
+import org.openmrs.module.fhir2.api.FhirMediaService;
+import org.openmrs.module.fhir2.api.annotations.R4Provider;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+@Component("mediaFhirR4ResourceProvider")
+@R4Provider
+@Setter(AccessLevel.PACKAGE)
+public class MediaFhirResourceProvider implements IResourceProvider {
+
+ @Autowired
+ private FhirMediaService fhirMediaService;
+
+ @Override
+ public Class extends IBaseResource> getResourceType() {
+ return Media.class;
+ }
+}
diff --git a/api/src/test/java/org/openmrs/module/fhir2/api/dao/impl/FhirMediaDaoImplTest.java b/api/src/test/java/org/openmrs/module/fhir2/api/dao/impl/FhirMediaDaoImplTest.java
new file mode 100644
index 000000000..7ad2fba2b
--- /dev/null
+++ b/api/src/test/java/org/openmrs/module/fhir2/api/dao/impl/FhirMediaDaoImplTest.java
@@ -0,0 +1,186 @@
+/*
+ * 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.api.dao.impl;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.hamcrest.Matchers.nullValue;
+import static org.junit.Assert.assertNull;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Date;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import ca.uhn.fhir.rest.param.StringAndListParam;
+import ca.uhn.fhir.rest.param.StringParam;
+import org.apache.commons.lang3.StringUtils;
+import org.hibernate.SessionFactory;
+import org.junit.Before;
+import org.junit.Test;
+import org.openmrs.Concept;
+import org.openmrs.ConceptDatatype;
+import org.openmrs.Obs;
+import org.openmrs.Person;
+import org.openmrs.PersonName;
+import org.openmrs.User;
+import org.openmrs.api.ObsService;
+import org.openmrs.api.context.Context;
+import org.openmrs.module.fhir2.FhirConstants;
+import org.openmrs.module.fhir2.TestFhirSpringConfiguration;
+import org.openmrs.module.fhir2.api.dao.FhirMediaDao;
+import org.openmrs.module.fhir2.api.search.param.SearchParameterMap;
+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)
+public class FhirMediaDaoImplTest extends BaseModuleContextSensitiveTest {
+
+ private static final String OBS_DATA_XML = "org/openmrs/module/fhir2/api/dao/impl/FhirObsServiceTest-complex.xml";
+
+ private static final String OBS_UUID = "32a8dde4-c159-11eb-8529-0242ac130003";
+
+ private static final String OBS_CONCEPT_ID = "5242";
+
+ private FhirMediaDaoImpl dao;
+
+ @Autowired
+ private SessionFactory sessionFactory;
+
+
+ @Before
+ public void setup() throws Exception {
+ executeDataSet(OBS_DATA_XML);
+ dao = new FhirMediaDaoImpl();
+ dao.setSessionFactory(sessionFactory);
+ }
+
+ @Test
+ public void get_shouldGetComplexObsByUuid() {
+ assertThat(dao.get(OBS_UUID), notNullValue());
+ }
+
+ @Test
+ public void get_shouldReturnNullIfObsNotFoundByUuid() {
+ Obs obs = dao.get(OBS_UUID);
+
+ assertThat(obs, notNullValue());
+ assertThat(obs.getUuid(), equalTo(OBS_UUID));
+ assertThat(dao.get(OBS_UUID).getValueComplex(), equalTo("txt image |sometext.txt"));
+ }
+
+ @Test
+ public void createOrUpdate_shouldSaveNewObs() {
+ Obs obs = new Obs();
+ Concept concept = new Concept();
+ Person person = new Person();
+ Set names = new HashSet<>();
+ PersonName name = new PersonName();
+ name.setFamilyName("Mpanda");
+ name.setGivenName("Ssekitto");
+ names.add(name);
+ person.setNames(names);
+ person.setBirthdate(new Date());
+ person.setId(2);
+ person.setDateCreated(new Date());
+ person.setGender("Male");
+ concept.setConceptId(1);
+ concept.setCreator(new User());
+ concept.setDatatype(new ConceptDatatype());
+ obs.setUuid(OBS_UUID);
+ obs.setObsId(21);
+ obs.setDateCreated(new Date());
+ obs.setPerson(person);
+ obs.setObsDatetime(new Date());
+ obs.setConcept(concept);
+ Obs result = dao.createOrUpdate(obs);
+ assertThat(result.getUuid(), equalTo(OBS_UUID));
+ }
+
+// @Test
+ public void createOrUpdate_shouldUpdateExistingObs() {
+
+ Obs obs = new Obs();
+ Concept concept = new Concept();
+ Person person = new Person();
+ Set names = new HashSet<>();
+ PersonName name = new PersonName();
+ name.setFamilyName("Mpanda");
+ name.setGivenName("Ssekitto");
+ names.add(name);
+ person.setNames(names);
+ person.setBirthdate(new Date());
+ person.setId(2);
+ person.setDateCreated(new Date());
+ person.setGender("Male");
+ concept.setConceptId(1);
+ concept.setCreator(new User());
+ concept.setDatatype(new ConceptDatatype());
+ obs.setUuid(OBS_UUID);
+ obs.setObsId(21);
+ obs.setDateCreated(new Date());
+ obs.setPerson(person);
+ obs.setObsDatetime(new Date());
+ obs.setConcept(concept);
+ Obs result = dao.createOrUpdate(obs);
+ assertThat(result.getUuid(), equalTo(OBS_UUID));
+ }
+
+ @Test
+ public void delete_shouldDeleteObs(){
+ Obs obs = dao.get(OBS_UUID);
+ assertThat(obs, notNullValue());
+
+ Obs result = dao.delete(OBS_UUID);
+ assertThat(result, notNullValue());
+
+ }
+
+ @Test
+ public void delete_shouldReturnNullIfObsToDeleteDoesNotExist(){
+ Obs result = dao.delete("32a8dde4-c159-11eb-0000-0242ac1311111");
+
+ assertThat(result, nullValue());
+ }
+
+ @Test
+ public void getSearchResults_shouldReturnAListOfObs(){
+ SearchParameterMap searchParameterMap = new SearchParameterMap();
+ searchParameterMap.addParameter("", null);
+ searchParameterMap.addParameter("", null);
+
+ List obsUUidsList = new ArrayList<>();
+ obsUUidsList.add("32a8dd30-c159-11eb-8529-0242ac130003");
+ obsUUidsList.add("32a8dde4-c159-11eb-8529-0242ac130003");
+
+ assertThat(dao.getSearchResults(searchParameterMap, obsUUidsList).size(), equalTo(2));
+ }
+ @Test
+ public void getSearchResults_ShouldReturnSearchQuery() {
+ StringAndListParam status = new StringAndListParam();
+ StringParam codingToken = new StringParam();
+ codingToken.setValue(OBS_CONCEPT_ID);
+ status.addAnd(codingToken);
+
+ SearchParameterMap theParams = new SearchParameterMap();
+ theParams.addParameter(FhirConstants.MEDIA_CONTENT_TYPE, status);
+
+ List matchingResourceUuids = dao.getSearchResultUuids(theParams);
+ Collection obs = dao.getSearchResults(theParams, matchingResourceUuids);
+
+ assertThat(obs, notNullValue());
+ }
+}
diff --git a/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirMediaServiceImplTest.java b/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirMediaServiceImplTest.java
new file mode 100644
index 000000000..505390afa
--- /dev/null
+++ b/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirMediaServiceImplTest.java
@@ -0,0 +1,59 @@
+/*
+ * 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.api.impl;
+
+import org.hl7.fhir.r4.model.Media;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.openmrs.Obs;
+import org.openmrs.module.fhir2.api.dao.FhirMediaDao;
+import org.openmrs.module.fhir2.api.search.SearchQueryInclude;
+import org.openmrs.module.fhir2.api.translators.MediaTranslator;
+
+import static org.mockito.Mockito.lenient;
+import static org.mockito.Mockito.when;
+
+@RunWith(MockitoJUnitRunner.class)
+public class FhirMediaServiceImplTest {
+
+ private static String OBS_UUID = "d085336f-2ddf-40cb-a67f-afd968ab9fa9";
+
+ @Mock
+ private FhirMediaDao dao;
+
+ @Mock
+ private SearchQueryInclude searchQueryInclude;
+
+ @Mock
+ private MediaTranslator mediaTranslator;
+
+ private FhirMediaServiceImpl fhirMediaService;
+
+ @Before
+ public void setUp() {
+ fhirMediaService = new FhirMediaServiceImpl();
+ fhirMediaService.setDao(dao);
+ fhirMediaService.setTranslator(mediaTranslator);
+ fhirMediaService.setSearchQueryInclude(searchQueryInclude);
+ }
+
+ @Test
+ public void get_shouldGetComplexObsByUuid(){
+ Obs obs = new Obs();
+ obs.setUuid(OBS_UUID);
+ Media media = new Media();
+ System.out.println(dao.get(OBS_UUID));
+ lenient().when(dao.get(OBS_UUID)).thenReturn(obs);
+ lenient().when(mediaTranslator.toFhirResource(obs)).thenReturn(media);
+ }
+}
diff --git a/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/MediaContentTranslatorImplTest.java b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/MediaContentTranslatorImplTest.java
new file mode 100644
index 000000000..8a367df53
--- /dev/null
+++ b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/MediaContentTranslatorImplTest.java
@@ -0,0 +1,66 @@
+/*
+ * 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.api.translators.impl;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.junit.Assert.assertNotNull;
+
+import java.util.Date;
+
+import org.hl7.fhir.r4.model.Attachment;
+import org.hl7.fhir.r4.model.Base64BinaryType;
+import org.hl7.fhir.r4.model.Media;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.openmrs.Obs;
+import org.openmrs.module.fhir2.api.translators.MediaContentTranslator;
+import org.springframework.util.Base64Utils;
+
+@RunWith(MockitoJUnitRunner.class)
+public class MediaContentTranslatorImplTest {
+
+ @Mock
+ private MediaContentTranslator mediaContentTranslator;
+
+ @Before
+ public void setUp() {
+ mediaContentTranslator = new MediaContentTranslatorImpl();
+ }
+
+ @Test
+ public void shouldTranslateObsToMediaContent() {
+ Obs obs = new Obs();
+ obs.setObsId(2);
+ obs.setValueComplex("");
+ obs.setValueText("image/gif");
+ obs.setComment("CT Scan");
+ obs.setDateCreated(new Date());
+
+ Media mediaContent = mediaContentTranslator.toFhirResource(obs);
+ assertThat(mediaContent, notNullValue());
+ }
+
+ @Test
+ public void toOpenmrsType_shouldConvertMediaContentToOpenmrsObs() {
+ Media mediaContent = new Media();
+ mediaContent.setContent(new Attachment()
+ .setDataElement(new Base64BinaryType().setValue(Base64Utils.decode("VGVzdCBFbmNvZGVyCgo".getBytes()))));
+ mediaContent.setContent(new Attachment().setContentType("image/gif"));
+ mediaContent.setContent(new Attachment().setCreation(new Date()));
+ mediaContent.setContent(new Attachment().setTitle("Brain CT-Scan "));
+ Obs obsContent = mediaContentTranslator.toOpenmrsType(mediaContent);
+
+ assertNotNull(obsContent);
+ }
+}
diff --git a/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/MediaStatusTranslatorImplTest.java b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/MediaStatusTranslatorImplTest.java
new file mode 100644
index 000000000..1858820f8
--- /dev/null
+++ b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/MediaStatusTranslatorImplTest.java
@@ -0,0 +1,53 @@
+/*
+ * 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.api.translators.impl;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+
+import org.hl7.fhir.r4.model.Media;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.openmrs.Obs;
+import org.openmrs.module.fhir2.api.translators.MediaStatusTranslator;
+
+@RunWith(MockitoJUnitRunner.class)
+public class MediaStatusTranslatorImplTest {
+
+ private MediaStatusTranslator mediaStatusTranslator;
+
+ @Before
+ public void setUp() {
+ mediaStatusTranslator = new MediaStatusTranslatorImpl();
+ }
+
+ @Test
+ public void shouldMapMediaStatusToObsStatus() {
+ Obs obs = new Obs();
+
+ Media.MediaStatus status = mediaStatusTranslator.toFhirResource(obs);
+
+ assertThat(status, is(Media.MediaStatus.UNKNOWN));
+ }
+
+ @Test
+ public void shouldMapFhirRepresentationToCompleted() {
+ Obs obs = new Obs();
+ Media.MediaStatus mediaStatus = Media.MediaStatus.COMPLETED;
+
+ Obs result = mediaStatusTranslator.toOpenmrsType(obs, mediaStatus);
+ assertThat(result, notNullValue());
+
+ }
+
+}
diff --git a/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/MediaTranslatorImplTest.java b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/MediaTranslatorImplTest.java
new file mode 100644
index 000000000..224b9c524
--- /dev/null
+++ b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/MediaTranslatorImplTest.java
@@ -0,0 +1,54 @@
+/*
+ * 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.api.translators.impl;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.notNullValue;
+
+import java.util.Date;
+
+import org.hl7.fhir.r4.model.Media;
+import org.junit.Before;
+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.Obs;
+import org.openmrs.Patient;
+
+@RunWith(MockitoJUnitRunner.class)
+public class MediaTranslatorImplTest {
+
+ private static String OBS_UUID = "96c695c9-148b-4788-ac8e-ff2594381ebf";
+
+ private static String MEDIA_STATUS = "COMPLETED";
+
+ @Mock
+ MediaTranslatorImpl mediaTranslator;
+
+ @Before
+ public void setUp() {
+ mediaTranslator = new MediaTranslatorImpl();
+ }
+
+ @Test
+ public void toFhir_shouldConvertObsToMedia() {
+ Concept concept = new Concept();
+ // concept.set
+ Obs obs = new Obs();
+ obs.setDateCreated(new Date());
+ // obs.setValueCoded(new Concept().set);
+ obs.setPerson(new Patient());
+
+ Media result = mediaTranslator.toFhirResource(obs);
+ assertThat(result, notNullValue());
+ }
+}
diff --git a/api/src/test/java/org/openmrs/module/fhir2/providers/r4/MediaFhirResourceProviderTest.java b/api/src/test/java/org/openmrs/module/fhir2/providers/r4/MediaFhirResourceProviderTest.java
new file mode 100644
index 000000000..43aad7fcf
--- /dev/null
+++ b/api/src/test/java/org/openmrs/module/fhir2/providers/r4/MediaFhirResourceProviderTest.java
@@ -0,0 +1,16 @@
+/*
+ * 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.providers.r4;
+
+import org.junit.runner.RunWith;
+import org.mockito.junit.MockitoJUnitRunner;
+
+@RunWith(MockitoJUnitRunner.class)
+public class MediaFhirResourceProviderTest {}
diff --git a/test-data/src/test/resources/org/openmrs/module/fhir2/api/dao/impl/FhirObsServiceTest-complex.xml b/test-data/src/test/resources/org/openmrs/module/fhir2/api/dao/impl/FhirObsServiceTest-complex.xml
new file mode 100644
index 000000000..21e11e843
--- /dev/null
+++ b/test-data/src/test/resources/org/openmrs/module/fhir2/api/dao/impl/FhirObsServiceTest-complex.xml
@@ -0,0 +1,22 @@
+
+
+
+
+
+
+
+
+
+
+
+45
+
+
+
\ No newline at end of file