/*
 * Copyright 2002-2018 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
 *
 *      https://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 org.springframework.security.config.annotation.web.configuration;

import java.net.URL;
import java.net.URLClassLoader;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import org.springframework.beans.FatalBeanException;
import org.springframework.context.annotation.Bean;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.test.SpringTestContext;
import org.springframework.security.config.test.SpringTestContextExtension;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.Mockito.mock;

/**
 * @author Joe Grandja
 */
@ExtendWith(SpringTestContextExtension.class)
public class Sec2515Tests {

	public final SpringTestContext spring = new SpringTestContext(this);

	// SEC-2515
	@Test
	public void loadConfigWhenAuthenticationManagerNotConfiguredAndRegisterBeanThenThrowFatalBeanException() {
		assertThatExceptionOfType(FatalBeanException.class)
				.isThrownBy(() -> this.spring.register(StackOverflowSecurityConfig.class).autowire());
	}

	@Test
	public void loadConfigWhenAuthenticationManagerNotConfiguredAndRegisterBeanCustomNameThenThrowFatalBeanException() {
		assertThatExceptionOfType(FatalBeanException.class)
				.isThrownBy(() -> this.spring.register(CustomBeanNameStackOverflowSecurityConfig.class).autowire());
	}

	// SEC-2549
	@Test
	public void loadConfigWhenChildClassLoaderSetThenContextLoads() {
		CanLoadWithChildConfig.AUTHENTICATION_MANAGER = mock(AuthenticationManager.class);
		this.spring.register(CanLoadWithChildConfig.class);
		AnnotationConfigWebApplicationContext context = (AnnotationConfigWebApplicationContext) this.spring
				.getContext();
		context.setClassLoader(new URLClassLoader(new URL[0], context.getClassLoader()));
		this.spring.autowire();
		assertThat(this.spring.getContext().getBean(AuthenticationManager.class)).isNotNull();
	} // SEC-2515

	@Test
	public void loadConfigWhenAuthenticationManagerConfiguredAndRegisterBeanThenContextLoads() {
		this.spring.register(SecurityConfig.class).autowire();
	}

	@EnableWebSecurity
	static class StackOverflowSecurityConfig extends WebSecurityConfigurerAdapter {

		@Bean
		@Override
		public AuthenticationManager authenticationManagerBean() throws Exception {
			return super.authenticationManagerBean();
		}

	}

	@EnableWebSecurity
	static class CustomBeanNameStackOverflowSecurityConfig extends WebSecurityConfigurerAdapter {

		@Override
		@Bean(name = "custom")
		public AuthenticationManager authenticationManagerBean() throws Exception {
			return super.authenticationManagerBean();
		}

	}

	@EnableWebSecurity
	static class CanLoadWithChildConfig extends WebSecurityConfigurerAdapter {

		static AuthenticationManager AUTHENTICATION_MANAGER;

		@Override
		@Bean
		public AuthenticationManager authenticationManager() {
			return AUTHENTICATION_MANAGER;
		}

	}

	@EnableWebSecurity
	static class SecurityConfig extends WebSecurityConfigurerAdapter {

		@Bean
		@Override
		public AuthenticationManager authenticationManagerBean() throws Exception {
			return super.authenticationManagerBean();
		}

		@Override
		protected void configure(AuthenticationManagerBuilder auth) throws Exception {
			auth.inMemoryAuthentication();
		}

	}

}
