Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ATT-38: AttachmentRestController2_0Test to test AttachmentBytesResource with 'view' param. #1

Merged
merged 9 commits into from May 5, 2020
Expand Up @@ -22,8 +22,9 @@ public AttachmentComplexData2_0(String instructions, String title, Object data,
} else {
this.setMimeType(AttachmentsConstants.UNKNOWN_MIME_TYPE);
}
if (!StringUtils.isEmpty(instructions))
if (!StringUtils.isEmpty(instructions)) {
this.instructions = instructions;
}
}

public AttachmentComplexData2_0(String title, Object data) {
Expand Down
Expand Up @@ -2,21 +2,27 @@

import org.openmrs.Obs;
import org.openmrs.annotation.OpenmrsProfile;
import org.openmrs.api.ObsService;
import org.openmrs.module.attachments.AttachmentsConstants;
import org.openmrs.obs.ComplexObsHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component(AttachmentsConstants.COMPONENT_COMPLEXVIEW_HELPER)
@OpenmrsProfile(openmrsPlatformVersion = "2.0.0")
public class ComplexViewHelper2_0 implements ComplexViewHelper {

@Autowired
private ObsService obsService;

@Override
public String getView(Obs obs, String view) {
// TODO: Obs obs will help the 2.x implementation support fetching the
// supported views for that obs,
// See:
// https://issues.openmrs.org/browse/ATT-34

if (obsService.getHandler(obs).supportsView(view)) {
return view;
}

return ComplexObsHandler.RAW_VIEW;

}
}
@@ -1,28 +1,36 @@
package org.openmrs.module.attachments.obs;

import static org.openmrs.module.attachments.AttachmentsConstants.ATT_VIEW_ORIGINAL;
import static org.openmrs.module.attachments.AttachmentsConstants.ATT_VIEW_THUMBNAIL;
import static org.openmrs.module.attachments.obs.ImageAttachmentHandler.THUMBNAIL_MAX_HEIGHT;
import static org.openmrs.module.attachments.obs.ImageAttachmentHandler.THUMBNAIL_MAX_WIDTH;

import java.io.File;
import java.io.IOException;
import net.coobird.thumbnailator.Thumbnails;
import java.util.Arrays;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openmrs.Obs;
import org.openmrs.api.APIException;
import org.openmrs.module.attachments.AttachmentsConstants;
import static org.openmrs.module.attachments.obs.ImageAttachmentHandler.THUMBNAIL_MAX_HEIGHT;
import static org.openmrs.module.attachments.obs.ImageAttachmentHandler.THUMBNAIL_MAX_WIDTH;
import org.openmrs.obs.ComplexData;
import org.openmrs.obs.ComplexObsHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import net.coobird.thumbnailator.Thumbnails;

/**
* Double inheritance class. The actual implementation parent must be set through
* {@link #setParentComplexObsHandler()}.
*/
public abstract class AbstractAttachmentHandler implements ComplexObsHandler {

private static final String[] supportedViews = { ATT_VIEW_ORIGINAL, ATT_VIEW_THUMBNAIL };

public final static String NO_THUMBNAIL_SUFFIX = "___nothumb__";

public final static String THUMBNAIL_SUFFIX = "_thumb";
Expand Down Expand Up @@ -64,6 +72,14 @@ protected ComplexDataHelper getComplexDataHelper() {
*/
abstract protected ValueComplex saveComplexData(Obs obs, AttachmentComplexData complexData);

public String[] getSupportedViews() {
return supportedViews;
}

public boolean supportsView(String view) {
return Arrays.asList(getSupportedViews()).contains(view);
}

protected void setParent(ComplexObsHandler complexObsHandler) {
this.parent = complexObsHandler;
}
Expand Down Expand Up @@ -167,8 +183,8 @@ final public Obs getObs(Obs obs, String view) {
view = AttachmentsConstants.ATT_VIEW_ORIGINAL;
}

ComplexData docData = readComplexData(obs, valueComplex, view);
obs.setComplexData(docData);
ComplexData attData = readComplexData(obs, valueComplex, view);
obs.setComplexData(attData);
return obs;
}

Expand Down
Expand Up @@ -43,6 +43,8 @@ public static byte[] getByteArray(ComplexData complexData) {
}
if (data instanceof byte[]) {
return (byte[]) data;
} else if (data instanceof String) {
return ((String) data).getBytes();
} else if (RenderedImage.class.isAssignableFrom(data.getClass())) {
RenderedImage image = (RenderedImage) data;

Expand Down
4 changes: 3 additions & 1 deletion api/src/main/resources/messages.properties
Expand Up @@ -27,4 +27,6 @@ ${project.parent.artifactId}.thumbail.save.error=An error occurred and the attac
${project.parent.artifactId}.thumbail.delete.success=The attachment was successfully deleted.
${project.parent.artifactId}.thumbail.delete.error=An error occurred and the attachment could not be deleted.

${project.parent.artifactId}.misc.label.enterCaption=Enter a caption
${project.parent.artifactId}.misc.label.enterCaption=Enter a caption

${project.parent.artifactId}.noActiveVisit=The file upload is disabled while there is no active visit.
24 changes: 24 additions & 0 deletions api/src/test/resources/testdata/test-dataset.xml
@@ -0,0 +1,24 @@
<?xml version='1.0' encoding='UTF-8'?>
<!--

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.

-->
<dataset>
<concept concept_id="84069" retired="false" datatype_id="13" class_id="5" is_set="false" creator="1" date_created="2005-01-01 00:00:00.0" uuid="3fe44002-35b8-445e-9d46-692c56e6bdde"/>
<concept_description concept_description_id="84069" concept_id="84069" description="TestBytesViewHandler Concept Complex" locale="en_GB" creator="1" date_created="2004-08-12 00:00:00.0" uuid="3a951971-34e9-4c88-954c-68d188f2c4de"/>

<concept_complex concept_id="84069" handler="TestAttachmentBytesViewHandler" />

<concept_name concept_id="84069" name="TestAttachmentBytesViewHandler Concept Complex" locale="en_GB" creator="1" date_created="2008-08-15 13:52:53.0" concept_name_id="8007" concept_name_type="FULLY_SPECIFIED" locale_preferred="0" voided="false" uuid="86f4f6ff-2bfb-4a17-a96b-ca633a96dd13"/>

<!-- This obs just exists so that there is a reference to an attachment associated with TestAttachmentBytesViewHandler to fetch it via REST -->
<obs obs_id="84044" person_id="2" concept_id="84069" obs_datetime="2008-02-09 00:00:00.0" location_id="1" value_complex="m3ks | instructions.none" creator="1" date_created="2006-02-10 15:57:35.0" voided="false" uuid="bbec1b32-dbf1-4b7a-bd2b-8898543c1367"/>

</dataset>
Expand Up @@ -26,6 +26,7 @@
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping(value = "/rest/" + RestConstants.VERSION_1 + "/" + AttachmentsConstants.ATTACHMENT_URI)
Expand All @@ -37,7 +38,8 @@ public class AttachmentBytesResource1_10 extends BaseRestController {
protected final Log log = LogFactory.getLog(getClass());

@RequestMapping(value = AttachmentsConstants.ATTACHMENT_BYTES_URI, method = RequestMethod.GET)
public void getFile(@PathVariable("uuid") String uuid, HttpServletResponse response) throws ResponseException {
public void getFile(@PathVariable("uuid") String uuid, @RequestParam(required = false, value = "view") String view,
HttpServletResponse response) throws ResponseException {
// Getting the Core/Platform complex data object
Obs obs = context.getObsService().getObsByUuid(uuid);

Expand All @@ -48,7 +50,7 @@ public void getFile(@PathVariable("uuid") String uuid, HttpServletResponse respo

ComplexViewHelper viewHelper = context.getComplexViewHelper();

Obs complexObs = Context.getObsService().getComplexObs(obs.getObsId(), viewHelper.getView(obs, null));
Obs complexObs = Context.getObsService().getComplexObs(obs.getObsId(), viewHelper.getView(obs, view));
ComplexData complexData = complexObs.getComplexData();

// Switching to our complex data object
Expand Down
Expand Up @@ -34,7 +34,7 @@
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.mock.web.MockMultipartHttpServletRequest;

public class AttachmentController1_10Test extends MainResourceControllerTest {
public class AttachmentRestController1_10Test extends MainResourceControllerTest {

@Autowired
protected ObsService obsService;
Expand Down
3 changes: 3 additions & 0 deletions omod-2.0/pom.xml
Expand Up @@ -205,6 +205,9 @@
</resources>

<testResources>
<testResource>
<directory>../api/src/test/resources</directory>
</testResource>
<testResource>
<directory>src/test/resources</directory>
<includes>
Expand Down
@@ -0,0 +1,51 @@
package org.openmrs.module.attachments.obs;

import static org.openmrs.module.attachments.obs.ValueComplex.INSTRUCTIONS_DEFAULT;

import org.openmrs.Obs;
import org.openmrs.obs.ComplexData;
import org.openmrs.obs.handler.BinaryDataHandler;

public class TestAttachmentBytesViewHandler extends AbstractAttachmentHandler {

public static final String DEFAULT_VIEW = "VIEW_1";

public static final String DEFAULT_VIEW_DATA = "This is a string content for VIEW_1.";

public static final String ALTERNATE_VIEW = "VIEW_2";

public static final String ALTERNATE_VIEW_DATA = "This is a string content for VIEW_2.";

private static final String[] supportedViews = { DEFAULT_VIEW, ALTERNATE_VIEW };

@Override
protected void setParentComplexObsHandler() {
setParent(new BinaryDataHandler());
}

@Override
protected ComplexData readComplexData(Obs obs, ValueComplex valueComplex, String view) {

if (ALTERNATE_VIEW.equals(view)) {
return new AttachmentComplexData2_0(INSTRUCTIONS_DEFAULT, "extra_view", ALTERNATE_VIEW_DATA, "text/plain");
}

return new AttachmentComplexData2_0(INSTRUCTIONS_DEFAULT, "default_view", DEFAULT_VIEW_DATA, "text/plain");

}

@Override
protected boolean deleteComplexData(Obs obs, AttachmentComplexData complexData) {
return false;
}

@Override
protected ValueComplex saveComplexData(Obs obs, AttachmentComplexData complexData) {
return null;
}

@Override
public String[] getSupportedViews() {
return supportedViews;
}
}

This file was deleted.