All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jayway.restassured.module.mockmvc.config.MockMvcConfig Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2015 the original author or authors.
 *
 * 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 com.jayway.restassured.module.mockmvc.config;

import com.jayway.restassured.config.Config;
import org.springframework.test.web.servlet.MockMvcBuilder;
import org.springframework.test.web.servlet.setup.MockMvcConfigurer;
import org.springframework.web.context.WebApplicationContext;

/**
 * General configuration of the Spring Mock MVC module
 */
public class MockMvcConfig implements Config {

    private final boolean userConfigured;
    private final boolean automaticallyApplySpringSecurityMockMvcConfigurer;
    private final boolean automaticallyApplySpringRestDocsMockMvcSupport;

    /**
     * Creates a default {@link MockMvcConfig} that automatically applies the SecurityMockMvcConfigurer if available in classpath.
     */
    public MockMvcConfig() {
        this(true, true, false);
    }

    private MockMvcConfig(boolean shouldAutomaticallyApplySpringSecurityMockMvcConfigurer, boolean automaticallyApplySpringRestDocsMockMvcSupport,
                          boolean isUserConfigured) {
        this.automaticallyApplySpringSecurityMockMvcConfigurer = shouldAutomaticallyApplySpringSecurityMockMvcConfigurer;
        this.automaticallyApplySpringRestDocsMockMvcSupport = automaticallyApplySpringRestDocsMockMvcSupport;
        this.userConfigured = isUserConfigured;
    }

    /**
     * Instruct REST Assured Mock Mvc not to automatically apply the SpringSecurityMockMvcConfigurer even if it's available in the classpath.
     */
    public MockMvcConfig dontAutomaticallyApplySpringSecurityMockMvcConfigurer() {
        return new MockMvcConfig(false, automaticallyApplySpringRestDocsMockMvcSupport, true);
    }

    /**
     * Instruct REST Assured Mock Mvc not to automatically apply support for spring-restdocs-mockmvc even if it's available in the classpath.
     */
    public MockMvcConfig dontAutomaticallyApplySpringRestDocsMockMvcSupport() {
        return new MockMvcConfig(automaticallyApplySpringSecurityMockMvcConfigurer, false, true);
    }

    /**
     * Instructs REST Assured to automatically apply the SecurityMockMvcConfigurer that allows for Spring Security testing. This only works if
     * spring-security-test is in the classpath and you've configured Rest Assured Mock MVC an instance of {@link org.springframework.test.web.servlet.setup.AbstractMockMvcBuilder}.
     * For example by using {@link com.jayway.restassured.module.mockmvc.specification.MockMvcRequestSpecification#webAppContextSetup(WebApplicationContext, MockMvcConfigurer...)} or
     * {@link com.jayway.restassured.module.mockmvc.specification.MockMvcRequestSpecification#standaloneSetup(MockMvcBuilder)}.
     *
     * @param shouldAutomaticallyApplySpringSecurityMockMvcConfigurer true if SecurityMockMvcConfigurer should be automatically applied if available in classpath, false otherwise.
     * @return a new instance of {@link MockMvcConfig}.
     */
    public MockMvcConfig automaticallyApplySpringSecurityMockMvcConfigurer(boolean shouldAutomaticallyApplySpringSecurityMockMvcConfigurer) {
        return new MockMvcConfig(shouldAutomaticallyApplySpringSecurityMockMvcConfigurer, automaticallyApplySpringRestDocsMockMvcSupport, true);
    }

    /**
     * Instructs REST Assured whether or not to automatically support spring-restdocs-mockmvc if available in classpath.
     *
     * @param automaticallyApplySpringRestDocsMockMvcSupport true if Spring Rest Docs MockMvc support should be automatically applied if available in classpath, false otherwise.
     * @return a new instance of {@link MockMvcConfig}.
     */
    public MockMvcConfig automaticallyApplySpringRestDocsMockMvcSupport(boolean automaticallyApplySpringRestDocsMockMvcSupport) {
        return new MockMvcConfig(automaticallyApplySpringSecurityMockMvcConfigurer, automaticallyApplySpringRestDocsMockMvcSupport, true);
    }

    /**
     * Instruct REST Assured Mock Mvc to automatically apply the SpringSecurityMockMvcConfigurer if it's available in the classpath.
     *
     * @return a new instance of {@link MockMvcConfig}.
     * @see #automaticallyApplySpringSecurityMockMvcConfigurer(boolean)
     */
    public MockMvcConfig automaticallyApplySpringSecurityMockMvcConfigurer() {
        return new MockMvcConfig(true, automaticallyApplySpringRestDocsMockMvcSupport, true);
    }

    /**
     * Instruct REST Assured Mock Mvc to automatically apply support for spring-restdocs-mockmvc if it's available in the classpath.
     *
     * @return a new instance of {@link MockMvcConfig}.
     * @see #automaticallyApplySpringRestDocsMockMvcSupport(boolean)
     */
    public MockMvcConfig automaticallyApplySpringRestDocsMockMvcSupport() {
        return new MockMvcConfig(automaticallyApplySpringSecurityMockMvcConfigurer, true, true);
    }

    /**
     * @return whether or not REST Assured should automatically try to apply support for Spring Security test if it's available in classpath
     */
    public boolean shouldAutomaticallyApplySpringSecurityMockMvcConfigurer() {
        return automaticallyApplySpringSecurityMockMvcConfigurer;
    }

    /**
     * @return whether or not REST Assured should automatically try to apply support for spring-restdocs-mockmvc if it's available in classpath
     */
    public boolean shouldAutomaticallyApplySpringRestDocsMockMvcSupport() {
        return automaticallyApplySpringRestDocsMockMvcSupport;
    }

    public boolean isUserConfigured() {
        return userConfigured;
    }

    /**
     * Just syntactic sugar to make the DSL more english like.
     */
    public MockMvcConfig with() {
        return this;
    }

    /**
     * Just syntactic sugar.
     *
     * @return A new instance of {@link MockMvcConfig}.
     */
    public static MockMvcConfig mockMvcConfig() {
        return new MockMvcConfig();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy