edu.pitt.dbmi.ccd.anno.CCDAnnotationsTest.java Source code

Java tutorial

Introduction

Here is the source code for edu.pitt.dbmi.ccd.anno.CCDAnnotationsTest.java

Source

/*
 * Copyright (C) 2015 University of Pittsburgh.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301  USA
 */
package edu.pitt.dbmi.ccd.anno;

import javax.servlet.RequestDispatcher;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.restdocs.JUnitRestDocumentation;
import static org.springframework.restdocs.headers.HeaderDocumentation.headerWithName;
import static org.springframework.restdocs.headers.HeaderDocumentation.responseHeaders;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
import org.springframework.restdocs.mockmvc.RestDocumentationResultHandler;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessRequest;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessResponse;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.prettyPrint;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

/**
 *
 * Dec 5, 2016 1:55:33 PM
 *
 * @author Mark Silvis (marksilvis@pitt.edu)
 */
@RunWith(SpringRunner.class)
@SpringBootTest
@WebAppConfiguration
public class CCDAnnotationsTest {

    @Rule
    public JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation("build/generated-snippets");

    @Autowired
    private WebApplicationContext context;

    private MockMvc mockMvc;

    private RestDocumentationResultHandler documentationResultHandler;

    @Before
    public void setUp() {
        this.documentationResultHandler = document("{method-name}", preprocessRequest(prettyPrint()),
                preprocessResponse(prettyPrint()));

        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
                .apply(documentationConfiguration(this.restDocumentation)).alwaysDo(this.documentationResultHandler)
                .build();
    }

    @Test
    public void headerExample() throws Exception {
        this.mockMvc.perform(get("/")).andExpect(status().isOk())
                .andDo(this.documentationResultHandler.document(responseHeaders(headerWithName("Content-Type")
                        .description("The Content-Type of the paylod, e.g. `application/hal+json`"))));
    }

    @Test
    public void errorExample() throws Exception {
        this.mockMvc
                .perform(get("/error").requestAttr(RequestDispatcher.ERROR_STATUS_CODE, 404)
                        .requestAttr(RequestDispatcher.ERROR_REQUEST_URI, "/test")
                        .requestAttr(RequestDispatcher.ERROR_MESSAGE, "Not found"))
                .andExpect(status().isNotFound()).andExpect(jsonPath("timestamp", is(notNullValue())))
                .andExpect(jsonPath("status", is(404))).andExpect(jsonPath("error", is("Not Found")))
                .andExpect(jsonPath("message", is("Not found"))).andExpect(jsonPath("path", is(notNullValue())))
                .andDo(this.documentationResultHandler.document(
                        responseHeaders(headerWithName("Content-Type")
                                .description("The Content-Type of the payload, e.g. `application/hal+json`")),
                        responseFields(
                                fieldWithPath("timestamp").description("The time at which the error occurred"),
                                fieldWithPath("status").description("The HTTP status code"),
                                fieldWithPath("error").description("The HTTP error"),
                                fieldWithPath("message").description("A description of what caused the error"),
                                fieldWithPath("path").description("The path to which the request was made"))));
    }

}