org.openmrs.module.webservices.rest.web.v1_0.controller.BaseCrudControllerTest.java Source code

Java tutorial

Introduction

Here is the source code for org.openmrs.module.webservices.rest.web.v1_0.controller.BaseCrudControllerTest.java

Source

/**
 * The contents of this file are subject to the OpenMRS Public License
 * Version 1.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://license.openmrs.org
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * Copyright (C) OpenMRS, LLC.  All Rights Reserved.
 */
package org.openmrs.module.webservices.rest.web.v1_0.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.PropertyUtils;
import org.codehaus.jackson.map.ObjectMapper;
import org.junit.Assert;
import org.junit.Test;
import org.openmrs.module.webservices.rest.SimpleObject;
import org.openmrs.module.webservices.rest.test.Util;
import org.openmrs.module.webservices.rest.web.RestConstants;
import org.openmrs.web.test.BaseModuleWebContextSensitiveTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.HandlerExecutionChain;
import org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter;
import org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping;

/**
 * Facilitates testing controllers.
 */
public abstract class BaseCrudControllerTest extends BaseModuleWebContextSensitiveTest {

    @Autowired
    private AnnotationMethodHandlerAdapter handlerAdapter;

    @Autowired
    private List<DefaultAnnotationHandlerMapping> handlerMappings;

    /**
     * Creates a request from the given parameters.
     * <p>
     * The requestURI is automatically preceded with "/rest/" + RestConstants.VERSION_1.
     * 
     * @param method
     * @param requestURI
     * @return
     */
    public MockHttpServletRequest request(RequestMethod method, String requestURI) {
        MockHttpServletRequest request = new MockHttpServletRequest(method.toString(),
                "/rest/" + RestConstants.VERSION_1 + "/" + requestURI);
        request.addHeader("content-type", "application/json");
        return request;
    }

    /**
     * Passes the given request to a proper controller.
     * 
     * @param request
     * @return
     * @throws Exception
     */
    public MockHttpServletResponse handle(HttpServletRequest request) throws Exception {
        MockHttpServletResponse response = new MockHttpServletResponse();

        HandlerExecutionChain handlerExecutionChain = null;
        for (DefaultAnnotationHandlerMapping handlerMapping : handlerMappings) {
            handlerExecutionChain = handlerMapping.getHandler(request);
            if (handlerExecutionChain != null) {
                break;
            }
        }
        Assert.assertNotNull("The request URI does not exist", handlerExecutionChain);

        handlerAdapter.handle(request, response, handlerExecutionChain.getHandler());

        return response;
    }

    /**
     * Deserializes the JSON response.
     * 
     * @param response
     * @return
     * @throws Exception
     */
    public SimpleObject deserialize(MockHttpServletResponse response) throws Exception {
        return new ObjectMapper().readValue(response.getContentAsString(), SimpleObject.class);
    }

    @Test
    public void shouldGetDefaultByUuid() throws Exception {
        SimpleObject result = deserialize(handle(request(RequestMethod.GET, getURI() + "/" + getUuid())));

        Assert.assertNotNull(result);
        Assert.assertEquals(getUuid(), PropertyUtils.getProperty(result, "uuid"));
    }

    @Test
    public void shouldGetRefByUuid() throws Exception {
        MockHttpServletRequest request = request(RequestMethod.GET, getURI() + "/" + getUuid());
        request.addParameter("v", "ref");
        SimpleObject result = deserialize(handle(request));

        Assert.assertNotNull(result);
        Assert.assertEquals(getUuid(), PropertyUtils.getProperty(result, "uuid"));
    }

    @Test
    public void shouldGetFullByUuid() throws Exception {
        MockHttpServletRequest request = request(RequestMethod.GET, getURI() + "/" + getUuid());
        request.addParameter("v", "full");
        SimpleObject result = deserialize(handle(request));

        Assert.assertNotNull(result);
        Assert.assertEquals(getUuid(), PropertyUtils.getProperty(result, "uuid"));
    }

    @Test
    public void shouldGetAll() throws Exception {
        SimpleObject result = deserialize(handle(request(RequestMethod.GET, getURI())));

        Assert.assertNotNull(result);
        Assert.assertEquals(getAllCount(), Util.getResultsSize(result));
    }

    /**
     * @return the URI of the resource
     */
    public abstract String getURI();

    /**
     * @return the uuid of an existing object
     */
    public abstract String getUuid();

    /**
     * @return the count of all not retired/voided objects
     */
    public abstract long getAllCount();
}