fr.keemto.web.ControllerTestCase.java Source code

Java tutorial

Introduction

Here is the source code for fr.keemto.web.ControllerTestCase.java

Source

/*
 * Copyright (C) 2010 Benoit Guerout <bguerout at gmail dot com>
 *
 * Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package fr.keemto.web;

import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.map.ObjectMapper;
import org.junit.Before;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.mockito.MockitoAnnotations.initMocks;

public abstract class ControllerTestCase {

    protected AnnotationMethodHandlerAdapter handlerAdapter;
    protected MockHttpServletRequest request;
    protected MockHttpServletResponse response;

    @Before
    public void setUp() throws Exception {
        initMocks(this);
        request = new MockHttpServletRequest();
        response = new MockHttpServletResponse();
        handlerAdapter = new AnnotationMethodHandlerAdapter();

        addJackonMessageConverter();

        request.setAttribute(HandlerMapping.INTROSPECT_TYPE_LEVEL_MAPPING, true);
    }

    private void addJackonMessageConverter() {
        List<HttpMessageConverter<?>> httpMessageConverters = new ArrayList<HttpMessageConverter<?>>(
                Arrays.asList(handlerAdapter.getMessageConverters()));
        httpMessageConverters.add(new MappingJacksonHttpMessageConverter());

        HttpMessageConverter[] converters = httpMessageConverters
                .toArray(new HttpMessageConverter[httpMessageConverters.size()]);
        handlerAdapter.setMessageConverters(converters);
    }

    protected JsonNode toJsonNode(String json) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        JsonFactory jsonFactory = mapper.getJsonFactory();
        JsonParser jp = jsonFactory.createJsonParser(json);
        return jp.readValueAsTree();
    }

    protected JsonNode toJsonNode(File jsonFile) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        JsonFactory jsonFactory = mapper.getJsonFactory();
        JsonParser jp = jsonFactory.createJsonParser(jsonFile);
        return jp.readValueAsTree();
    }

    protected File getJsonFile(String fileName) {
        return new File("src/test/resources/web/" + fileName);
    }

    protected String getJsonFileAsString(String fileName) throws IOException {
        return toJsonNode(getJsonFile(fileName)).toString();
    }
}