OAuth2LoadBalancerClientAutoConfigurationTests.java

/*
 * Copyright 2015-present 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.cloud.loadbalancer.security;

import org.apache.catalina.webresources.TomcatURLStreamHandlerFactory;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.test.ClassPathExclusions;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration;

import static org.assertj.core.api.Assertions.assertThat;

/**
 * @author Dave Syer
 *
 */
@ClassPathExclusions("spring-retry-*.jar")
public class OAuth2LoadBalancerClientAutoConfigurationTests {

	private ConfigurableApplicationContext context;

	@BeforeEach
	public void before() {
		// FIXME: why do I need to do this? (fails in maven build without it.
		// https://stackoverflow.com/questions/28911560/tomcat-8-embedded-error-org-apache-catalina-core-containerbase-a-child-con
		// https://github.com/spring-projects/spring-boot/issues/21535
		TomcatURLStreamHandlerFactory.disable();
	}

	@AfterEach
	public void close() {
		if (this.context != null) {
			this.context.close();
		}
	}

	@Test
	@Disabled
	public void userInfoNotLoadBalanced() {
		this.context = new SpringApplicationBuilder(ClientConfiguration.class)
			.properties("spring.config.name=test", "server.port=0",
					"security.oauth2.resource.userInfoUri:https://example.com")
			.run();

		assertThat(this.context.containsBean("loadBalancedUserInfoRestTemplateCustomizer")).isFalse();
		assertThat(this.context.containsBean("retryLoadBalancedUserInfoRestTemplateCustomizer")).isFalse();
	}

	@Test
	@Disabled
	public void userInfoLoadBalancedNoRetry() {
		this.context = new SpringApplicationBuilder(ClientConfiguration.class)
			.properties("spring.config.name=test", "server.port=0",
					"security.oauth2.resource.userInfoUri:https://nosuchservice",
					"spring.cloud.oauth2.load-balanced.enabled=true")
			.run();

		assertThat(this.context.containsBean("loadBalancedUserInfoRestTemplateCustomizer")).isTrue();
		assertThat(this.context.containsBean("retryLoadBalancedUserInfoRestTemplateCustomizer")).isFalse();

		/*
		 * OAuth2RestTemplate template =
		 * this.context.getBean(UserInfoRestTemplateFactory.class).getUserInfoRestTemplate
		 * (); ClientHttpRequest request = template.getRequestFactory().createRequest(new
		 * URI("https://nosuchservice"), HttpMethod.GET);
		 * expected.expectMessage("No instances available for nosuchservice");
		 * request.execute();
		 */
	}

	@EnableAutoConfiguration
	@Configuration(proxyBeanMethods = false)
	// @EnableOAuth2Sso
	protected static class ClientConfiguration {

	}

}