@Test public void contextLoads() { assertThat(controller.getMessage()).isNotEqualTo("Hello test"); TestPropertyValues .of("message:Hello test") .applyTo(environment); assertThat(controller.getMessage()).isNotEqualTo("Hello test"); refresher.refresh(); assertThat(controller.getMessage()).isEqualTo("Hello test"); }
/** * Add the properties from the underlying map to the environment. The default property * source used is {@link MapPropertySource}. * @param environment the environment that needs to be modified */ public void applyTo(ConfigurableEnvironment environment) { applyTo(environment, Type.MAP); }
/** * Add the properties from the underlying map to the environment owned by an * {@link ApplicationContext}. * @param context the context with an environment to modify */ public void applyTo(ConfigurableApplicationContext context) { applyTo(context.getEnvironment()); }
/** * Add the properties from the underlying map to the environment using the specified * property source type. * @param environment the environment that needs to be modified * @param type the type of {@link PropertySource} to be added. See {@link Type} */ public void applyTo(ConfigurableEnvironment environment, Type type) { applyTo(environment, type, type.applySuffix("test")); }
@Override public void customizeContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) { TestPropertyValues .of(EnableAutoConfiguration.ENABLED_OVERRIDE_PROPERTY + "=false") .applyTo(context); }
private void setActiveProfiles(ConfigurableEnvironment environment, String[] profiles) { TestPropertyValues .of("spring.profiles.active=" + StringUtils.arrayToCommaDelimitedString(profiles)) .applyTo(environment); }
@Test public void asymmetricJwt() { TestPropertyValues.of("security.oauth2.resource.jwt.keyValue=" + PUBLIC_KEY) .applyTo(this.environment); this.context = new SpringApplicationBuilder(ResourceConfiguration.class) .environment(this.environment).web(WebApplicationType.NONE).run(); DefaultTokenServices services = this.context.getBean(DefaultTokenServices.class); assertThat(services).isNotNull(); }
@Test public void clientCredentialsWithClientId() throws Exception { TestPropertyValues.of("security.oauth2.client.client-id=acme") .applyTo(this.environment); initializeContext(OAuth2RestOperationsConfiguration.class, true); assertThat(this.context.getBean(OAuth2RestOperationsConfiguration.class)) .isNotNull(); assertThat(this.context.getBean(ClientCredentialsResourceDetails.class)) .isNotNull(); }
@Test public void userInfoWithAuthorities() { TestPropertyValues.of("security.oauth2.resource.userInfoUri:http://example.com") .applyTo(this.environment); this.context = new SpringApplicationBuilder(AuthoritiesConfiguration.class) .environment(this.environment).web(WebApplicationType.NONE).run(); UserInfoTokenServices services = this.context .getBean(UserInfoTokenServices.class); assertThat(services).isNotNull(); assertThat(services).extracting("authoritiesExtractor") .containsExactly(this.context.getBean(AuthoritiesExtractor.class)); }
@Test public void configureWhenKeyStoreIsProvidedButNoAliasThenThrowsException() { TestPropertyValues.of("security.oauth2.resource.jwt.key-store=classpath:" + "org/springframework/boot/autoconfigure/security/oauth2/resource/keystore.jks", "security.oauth2.resource.jwt.key-store-password=changeme") .applyTo(this.environment); assertThatCode(() -> new SpringApplicationBuilder(ResourceConfiguration.class) .environment(this.environment).web(WebApplicationType.NONE).run()) .isInstanceOf(UnsatisfiedDependencyException.class); }
@Test public void configureWhenKeyStoreIsProvidedButNoPasswordThenThrowsException() { TestPropertyValues.of("security.oauth2.authorization.jwt.key-store=classpath:" + "org/springframework/boot/autoconfigure/security/oauth2/authserver/keystore.jks", "security.oauth2.authorization.jwt.key-alias=jwt") .applyTo(this.environment); assertThatCode( () -> new SpringApplicationBuilder(AuthorizationServerConfiguration.class) .environment(this.environment).web(WebApplicationType.NONE).run()) .isInstanceOf(UnsatisfiedDependencyException.class); }
@Test public void configureWhenKeyStoreIsProvidedButNoPasswordThenThrowsException() { TestPropertyValues.of("security.oauth2.resource.jwt.key-store=classpath:" + "org/springframework/boot/autoconfigure/security/oauth2/resource/keystore.jks", "security.oauth2.resource.jwt.key-alias=jwt").applyTo(this.environment); assertThatCode(() -> new SpringApplicationBuilder(ResourceConfiguration.class) .environment(this.environment).web(WebApplicationType.NONE).run()); }
@Test public void jwtTokenStoreShouldBeConditionalOnMissingBean() throws Exception { TestPropertyValues.of("security.oauth2.resource.jwt.keyValue=" + PUBLIC_KEY) .applyTo(this.environment); this.context = new SpringApplicationBuilder(JwtTokenStoreConfiguration.class, ResourceConfiguration.class).environment(this.environment) .web(WebApplicationType.NONE).run(); assertThat(this.context.getBeansOfType(JwtTokenStore.class)).hasSize(1); }
@Test public void configureWhenKeyStoreIsProvidedThenExposesJwtAccessTokenConverter() { TestPropertyValues.of("security.oauth2.authorization.jwt.key-store=classpath:" + "org/springframework/boot/autoconfigure/security/oauth2/authserver/keystore.jks", "security.oauth2.authorization.jwt.key-store-password=changeme", "security.oauth2.authorization.jwt.key-alias=jwt") .applyTo(this.environment); this.context = new SpringApplicationBuilder( AuthorizationServerConfiguration.class).environment(this.environment) .web(WebApplicationType.NONE).run(); assertThat(this.context.getBeansOfType(JwtAccessTokenConverter.class)).hasSize(1); }
@Test public void requestScopedWithClientId() throws Exception { TestPropertyValues.of("security.oauth2.client.client-id=acme") .applyTo(this.environment); initializeContext(ConfigForRequestScopedConfiguration.class, false); assertThat(this.context.containsBean("oauth2ClientContext")).isTrue(); }
@Test public void siteSwitcherHandlerInterceptorEnabled() throws Exception { this.context = new AnnotationConfigWebApplicationContext(); TestPropertyValues.of("spring.mobile.siteswitcher.enabled:true") .applyTo(this.context); this.context.register(Config.class); this.context.refresh(); assertThat(this.context.getBean(SiteSwitcherHandlerInterceptor.class)) .isNotNull(); }
@Test public void sitePreferenceHandlerInterceptorEnabled() throws Exception { this.context = new AnnotationConfigWebApplicationContext(); TestPropertyValues.of("spring.mobile.sitepreference.enabled:true") .applyTo(this.context); this.context.register(SitePreferenceAutoConfiguration.class); this.context.refresh(); assertThat(this.context.getBean(SitePreferenceHandlerInterceptor.class)) .isNotNull(); }
@Test public void bindInvalidProperties() { this.context.register(ServiceBrokerPropertiesConfiguration.class); TestPropertyValues.of("spring.cloud.openservicebroker.catalog.services[0].id:service-one-id") .applyTo(this.context); this.thrown.expect(BeanCreationException.class); this.context.refresh(); }
@Test(expected = NoSuchBeanDefinitionException.class) public void siteSwitcherHandlerInterceptorDisabled() { this.context = new AnnotationConfigWebApplicationContext(); TestPropertyValues.of("spring.mobile.siteswitcher.enabled:false") .applyTo(this.context); this.context.register(Config.class); this.context.refresh(); this.context.getBean(SiteSwitcherHandlerInterceptor.class); }
@Test(expected = NoSuchBeanDefinitionException.class) public void sitePreferenceHandlerInterceptorDisabled() { this.context = new AnnotationConfigWebApplicationContext(); TestPropertyValues.of("spring.mobile.sitepreference.enabled:false") .applyTo(this.context); this.context.register(SitePreferenceAutoConfiguration.class); this.context.refresh(); this.context.getBean(SitePreferenceHandlerInterceptor.class); }