Test Uploading a file Using Rest Failing

This is related to this issue:

https://issues.openmrs.org/browse/RESTWS-523

I created simple controller test as shown below.

    @Test
    public void create_shouldAcceptAndStoreClobDataViaPost() throws Exception{
        long before = getAllCount();
        System.out.println(before);
        File file = new File("omod-1.9/src/test/resources/formResourcefile.txt");
        Assert.assertTrue(file.exists());

        MockMultipartFile toUpload = new MockMultipartFile("value", "formresource.txt", "text/plain",
                OpenmrsUtil.getFileAsBytes(file));

        MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
        request.setRequestURI(getBaseRestURI()+getURI());
        request.setMethod(RequestMethod.POST.name());
        request.setContentType("multipart/form-data");
        request.addHeader("Content-Type", "multipart/form-data");

        request.addFile(toUpload);    
        MockHttpServletResponse response = handle(request);
    }

Below is the controller handling the request.

@Controller
@RequestMapping(value = "/rest/" + RestConstants.VERSION_1 + "/clobdata")
public class ClobDatatypeStorageController extends BaseRestController{

    @Autowired
    private RestService restService;

    @RequestMapping(method = RequestMethod.POST, headers = {"Content-Type=multipart/form-data"})
    @ResponseBody
    public Object create(@RequestBody MultipartFile file, HttpServletRequest request, HttpServletResponse response)
    throws IOException{
        DatatypeService ds = Context.getService(DatatypeService.class);
        RequestContext context = RestUtil.getRequestContext(request, response);
        DelegatingCrudResource<ClobDatatypeStorage> res =
                (DelegatingCrudResource) restService.getResourceByName(buildResourceName("clobdata"));
        Object created = res.create(file, context);
        return RestUtil.created(response, created);
    }

}

When I run the test I get an error org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'multipart/form-data' not supported. See the complete stack trace [here][2]. It seems the code does not even get to the point of calling the associated controller before throwing the error. On debugging I saw something more interesting. Although the error says ‘multipart/form-data’ not supported, the list of supported media includes it!! (See a screenshot of debugger details below)

I noticed rest module has not been designed to handle files and may be this could be the problem. Otherwise what am I doing wrong? Is there a better way to implement this ticket?

[1]: [2]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'multip - Pastebin.com

I’m not sure when we added MultipartResolver to openmrs-servlet.xml in openmrs-core, but it is possible that webservices.rest declares a dependency on a version of core that does not have it. Please try adding

<bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />

to webModuleApplicationContext.xml in webservices.rest.

The bean definition is already included!

Could you please create a pull request with all your changes so we can have a better look?

I just pushed the branch I am working on. You can find it here. If you still need a pull request let me know.

Thanks a lot.

I have created a pull request and added comments about thingsm which you probably need to fix to make it work.

1 Like