|
| 1 | +/* |
| 2 | + * Copyright 2020-2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.security.oauth2.server.authorization.aot.hint; |
| 17 | + |
| 18 | +import org.junit.jupiter.api.BeforeEach; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | +import org.junit.jupiter.params.ParameterizedTest; |
| 21 | +import org.junit.jupiter.params.provider.ValueSource; |
| 22 | + |
| 23 | +import org.springframework.beans.factory.aot.BeanRegistrationAotContribution; |
| 24 | +import org.springframework.beans.factory.support.DefaultListableBeanFactory; |
| 25 | +import org.springframework.beans.factory.support.RegisteredBean; |
| 26 | +import org.springframework.beans.factory.support.RootBeanDefinition; |
| 27 | +import org.springframework.jdbc.core.JdbcOperations; |
| 28 | +import org.springframework.security.oauth2.server.authorization.InMemoryOAuth2AuthorizationService; |
| 29 | +import org.springframework.security.oauth2.server.authorization.JdbcOAuth2AuthorizationService; |
| 30 | +import org.springframework.security.oauth2.server.authorization.client.InMemoryRegisteredClientRepository; |
| 31 | +import org.springframework.security.oauth2.server.authorization.client.JdbcRegisteredClientRepository; |
| 32 | +import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository; |
| 33 | + |
| 34 | +import static org.assertj.core.api.Assertions.assertThat; |
| 35 | + |
| 36 | +/** |
| 37 | + * Tests for {@link OAuth2AuthorizationServerBeanRegistrationAotProcessor}. |
| 38 | + * |
| 39 | + * @author William Koch |
| 40 | + */ |
| 41 | +class OAuth2AuthorizationServerBeanRegistrationAotProcessorTests { |
| 42 | + |
| 43 | + private OAuth2AuthorizationServerBeanRegistrationAotProcessor processor; |
| 44 | + |
| 45 | + private DefaultListableBeanFactory defaultListableBeanFactory; |
| 46 | + |
| 47 | + @BeforeEach |
| 48 | + void setUp() { |
| 49 | + this.processor = new OAuth2AuthorizationServerBeanRegistrationAotProcessor(); |
| 50 | + this.defaultListableBeanFactory = new DefaultListableBeanFactory(); |
| 51 | + |
| 52 | + } |
| 53 | + |
| 54 | + @ParameterizedTest |
| 55 | + @ValueSource(classes = { JdbcOAuth2AuthorizationService.class, CustomJdbcOAuth2AuthorizationService.class, |
| 56 | + JdbcRegisteredClientRepository.class, CustomJdbcRegisteredClientRepository.class }) |
| 57 | + void processAheadOfTimeWhenBeanTypeJdbcBasedImplThenReturnContribution(Class<?> beanClass) { |
| 58 | + this.defaultListableBeanFactory.registerBeanDefinition("beanName", new RootBeanDefinition(beanClass)); |
| 59 | + |
| 60 | + BeanRegistrationAotContribution aotContribution = this.processor |
| 61 | + .processAheadOfTime(RegisteredBean.of(this.defaultListableBeanFactory, "beanName")); |
| 62 | + |
| 63 | + assertThat(aotContribution).isNotNull(); |
| 64 | + } |
| 65 | + |
| 66 | + @ParameterizedTest |
| 67 | + @ValueSource(classes = { InMemoryOAuth2AuthorizationService.class, InMemoryRegisteredClientRepository.class, |
| 68 | + Object.class }) |
| 69 | + void processAheadOfTimeWhenBeanTypeNotJdbcBasedImplThenDoesNotReturnContribution(Class<?> beanClass) { |
| 70 | + this.defaultListableBeanFactory.registerBeanDefinition("beanName", new RootBeanDefinition(beanClass)); |
| 71 | + |
| 72 | + BeanRegistrationAotContribution aotContribution = this.processor |
| 73 | + .processAheadOfTime(RegisteredBean.of(this.defaultListableBeanFactory, "beanName")); |
| 74 | + |
| 75 | + assertThat(aotContribution).isNull(); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + void processAheadOfTimeWhenMultipleBeanTypeJdbcBasedImplThenReturnContributionOnce() { |
| 80 | + this.defaultListableBeanFactory.registerBeanDefinition("oauth2AuthorizationService", |
| 81 | + new RootBeanDefinition(JdbcOAuth2AuthorizationService.class)); |
| 82 | + |
| 83 | + this.defaultListableBeanFactory.registerBeanDefinition("registeredClientRepository", |
| 84 | + new RootBeanDefinition(CustomJdbcRegisteredClientRepository.class)); |
| 85 | + |
| 86 | + BeanRegistrationAotContribution firstAotContribution = this.processor |
| 87 | + .processAheadOfTime(RegisteredBean.of(this.defaultListableBeanFactory, "oauth2AuthorizationService")); |
| 88 | + |
| 89 | + BeanRegistrationAotContribution secondAotContribution = this.processor |
| 90 | + .processAheadOfTime(RegisteredBean.of(this.defaultListableBeanFactory, "registeredClientRepository")); |
| 91 | + |
| 92 | + assertThat(firstAotContribution).isNotNull(); |
| 93 | + assertThat(secondAotContribution).isNull(); |
| 94 | + } |
| 95 | + |
| 96 | + static class CustomJdbcOAuth2AuthorizationService extends JdbcOAuth2AuthorizationService { |
| 97 | + |
| 98 | + CustomJdbcOAuth2AuthorizationService(JdbcOperations jdbcOperations, |
| 99 | + RegisteredClientRepository registeredClientRepository) { |
| 100 | + super(jdbcOperations, registeredClientRepository); |
| 101 | + } |
| 102 | + |
| 103 | + } |
| 104 | + |
| 105 | + static class CustomJdbcRegisteredClientRepository extends JdbcRegisteredClientRepository { |
| 106 | + |
| 107 | + CustomJdbcRegisteredClientRepository(JdbcOperations jdbcOperations) { |
| 108 | + super(jdbcOperations); |
| 109 | + } |
| 110 | + |
| 111 | + } |
| 112 | + |
| 113 | +} |
0 commit comments