From c2c35521df5fad95159478c6b8391d2c45bb346f Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Sat, 20 May 2017 13:39:53 +0200 Subject: [PATCH] Polishing. Extend tests for GenericSecretBackendMetadata. --- ...GenericSecretBackendMetadataUnitTests.java | 108 ++++++++++++++++++ .../VaultPropertySourceLocatorUnitTests.java | 77 ++++++------- 2 files changed, 142 insertions(+), 43 deletions(-) create mode 100644 spring-cloud-vault-config/src/test/java/org/springframework/cloud/vault/config/GenericSecretBackendMetadataUnitTests.java diff --git a/spring-cloud-vault-config/src/test/java/org/springframework/cloud/vault/config/GenericSecretBackendMetadataUnitTests.java b/spring-cloud-vault-config/src/test/java/org/springframework/cloud/vault/config/GenericSecretBackendMetadataUnitTests.java new file mode 100644 index 00000000..c28d976e --- /dev/null +++ b/spring-cloud-vault-config/src/test/java/org/springframework/cloud/vault/config/GenericSecretBackendMetadataUnitTests.java @@ -0,0 +1,108 @@ +/* + * Copyright 2017 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 org.springframework.cloud.vault.config; + +import java.util.List; + +import org.junit.Test; + +import org.springframework.mock.env.MockEnvironment; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Unit tests for {@link GenericSecretBackendMetadata}. + * + * @author Mark Paluch + */ +public class GenericSecretBackendMetadataUnitTests { + + MockEnvironment environment = new MockEnvironment(); + VaultGenericBackendProperties properties = new VaultGenericBackendProperties(); + + @Test + public void shouldCreateDefaultContexts() { + + List contexts = GenericSecretBackendMetadata.buildContexts(properties, + environment); + + assertThat(contexts).hasSize(1).contains("application"); + } + + @Test + public void shouldCreateDefaultForAppNameAndDefaultContext() { + + properties.setApplicationName("my-app"); + + List contexts = GenericSecretBackendMetadata.buildContexts(properties, + environment); + + assertThat(contexts).hasSize(2).containsSequence("my-app", "application"); + } + + @Test + public void shouldCreateDefaultForAppNameAndDefaultContextWithProfiles() { + + properties.setApplicationName("my-app"); + environment.addActiveProfile("cloud"); + environment.addActiveProfile("local"); + + List contexts = GenericSecretBackendMetadata.buildContexts(properties, + environment); + + assertThat(contexts).hasSize(6).containsSequence("my-app/local", "my-app/cloud", + "my-app", "application/local", "application/cloud", "application"); + } + + @Test + public void shouldCreateAppNameContextIfDefaultIsDisabled() { + + properties.setApplicationName("my-app"); + properties.setDefaultContext(""); + + List contexts = GenericSecretBackendMetadata.buildContexts(properties, + environment); + + assertThat(contexts).hasSize(1).containsSequence("my-app"); + } + + @Test + public void shouldCreateContextsForCommaSeparatedAppName() { + + properties.setApplicationName("foo,bar"); + + List contexts = GenericSecretBackendMetadata.buildContexts(properties, + environment); + + assertThat(contexts).hasSize(3).containsSequence("bar", "foo", "application"); + } + + @Test + public void shouldCreateContextsWithProfile() { + + environment.addActiveProfile("cloud"); + environment.addActiveProfile("local"); + + properties.setApplicationName("foo,bar"); + + List contexts = GenericSecretBackendMetadata.buildContexts(properties, + environment); + + assertThat(contexts).hasSize(9).containsSequence("bar/local", "bar/cloud", "bar", + "foo/local", "foo/cloud", "foo", "application/local", "application/cloud", + "application"); + } +} diff --git a/spring-cloud-vault-config/src/test/java/org/springframework/cloud/vault/config/VaultPropertySourceLocatorUnitTests.java b/spring-cloud-vault-config/src/test/java/org/springframework/cloud/vault/config/VaultPropertySourceLocatorUnitTests.java index d5dd85d8..cf78ad22 100644 --- a/spring-cloud-vault-config/src/test/java/org/springframework/cloud/vault/config/VaultPropertySourceLocatorUnitTests.java +++ b/spring-cloud-vault-config/src/test/java/org/springframework/cloud/vault/config/VaultPropertySourceLocatorUnitTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 the original author or authors. + * Copyright 2016-2017 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. @@ -15,10 +15,6 @@ */ package org.springframework.cloud.vault.config; -import static org.assertj.core.api.Assertions.*; -import static org.mockito.Mockito.*; - -import java.util.Arrays; import java.util.Collections; import org.junit.Before; @@ -31,10 +27,14 @@ import org.springframework.core.env.CompositePropertySource; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.PropertySource; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.when; + /** * Unit tests for {@link VaultPropertySourceLocator}. * * @author Ryan Hoegg + * @author Mark Paluch */ @RunWith(MockitoJUnitRunner.class) public class VaultPropertySourceLocatorUnitTests { @@ -53,8 +53,8 @@ public class VaultPropertySourceLocatorUnitTests { @Before public void before() { propertySourceLocator = new VaultPropertySourceLocator(operations, - new VaultProperties(), new VaultGenericBackendProperties(), - Collections.emptyList()); + new VaultProperties(), new VaultGenericBackendProperties(), + Collections.emptyList()); } @Test @@ -64,8 +64,8 @@ public class VaultPropertySourceLocatorUnitTests { vaultProperties.getConfig().setOrder(42); propertySourceLocator = new VaultPropertySourceLocator(operations, - vaultProperties, new VaultGenericBackendProperties(), - Collections.emptyList()); + vaultProperties, new VaultGenericBackendProperties(), + Collections.emptyList()); assertThat(propertySourceLocator.getOrder()).isEqualTo(42); } @@ -76,7 +76,7 @@ public class VaultPropertySourceLocatorUnitTests { when(configurableEnvironment.getActiveProfiles()).thenReturn(new String[0]); PropertySource propertySource = propertySourceLocator - .locate(configurableEnvironment); + .locate(configurableEnvironment); assertThat(propertySource).isInstanceOf(CompositePropertySource.class); @@ -88,19 +88,16 @@ public class VaultPropertySourceLocatorUnitTests { public void shouldLocatePropertySourcesForActiveProfilesInDefaultContext() { when(configurableEnvironment.getActiveProfiles()) - .thenReturn(new String[] { "vermillion", "periwinkle" }); + .thenReturn(new String[] { "vermillion", "periwinkle" }); PropertySource propertySource = propertySourceLocator - .locate(configurableEnvironment); + .locate(configurableEnvironment); assertThat(propertySource).isInstanceOf(CompositePropertySource.class); CompositePropertySource composite = (CompositePropertySource) propertySource; - assertThat(composite.getPropertySources()) - .extracting("name") - .containsAll(Arrays.asList(new String[] { - "secret/application/vermillion", - "secret/application/periwinkle" })); + assertThat(composite.getPropertySources()).extracting("name").containsSequence( + "secret/application/periwinkle", "secret/application/vermillion"); } @Test @@ -108,51 +105,45 @@ public class VaultPropertySourceLocatorUnitTests { final VaultGenericBackendProperties backendProperties = new VaultGenericBackendProperties(); backendProperties.setApplicationName("wintermute"); propertySourceLocator = new VaultPropertySourceLocator(operations, - new VaultProperties(), backendProperties, Collections.emptyList()); + new VaultProperties(), backendProperties, + Collections.emptyList()); when(configurableEnvironment.getActiveProfiles()) - .thenReturn(new String[] { "vermillion", "periwinkle" }); + .thenReturn(new String[] { "vermillion", "periwinkle" }); PropertySource propertySource = propertySourceLocator - .locate(configurableEnvironment); + .locate(configurableEnvironment); assertThat(propertySource).isInstanceOf(CompositePropertySource.class); CompositePropertySource composite = (CompositePropertySource) propertySource; - assertThat(composite.getPropertySources()).extracting("name") - .containsAll(Arrays.asList(new String[] { - "secret/wintermute", - "secret/wintermute/vermillion", - "secret/wintermute/periwinkle" })); + assertThat(composite.getPropertySources()).extracting("name").containsSequence( + "secret/wintermute/periwinkle", "secret/wintermute/vermillion", + "secret/wintermute"); } @Test public void shouldLocatePropertySourcesInEachPathSpecifiedWhenApplicationNameContainsSeveral() { final VaultGenericBackendProperties backendProperties = new VaultGenericBackendProperties(); backendProperties.setApplicationName("wintermute,straylight,icebreaker/armitage"); - propertySourceLocator = new VaultPropertySourceLocator( - operations, new VaultProperties(), backendProperties, Collections.emptyList()); + propertySourceLocator = new VaultPropertySourceLocator(operations, + new VaultProperties(), backendProperties, + Collections.emptyList()); when(configurableEnvironment.getActiveProfiles()) - .thenReturn(new String[] { "vermillion", "periwinkle" }); + .thenReturn(new String[] { "vermillion", "periwinkle" }); - PropertySource propertySource = - propertySourceLocator.locate(configurableEnvironment); + PropertySource propertySource = propertySourceLocator + .locate(configurableEnvironment); assertThat(propertySource).isInstanceOf(CompositePropertySource.class); CompositePropertySource composite = (CompositePropertySource) propertySource; - assertThat(composite.getPropertySources()) - .extracting("name") - .containsAll(Arrays.asList( - new String[] { "secret/wintermute", - "secret/straylight", - "secret/icebreaker/armitage", - "secret/wintermute/vermillion", - "secret/wintermute/periwinkle", - "secret/straylight/vermillion", - "secret/straylight/periwinkle", - "secret/icebreaker/armitage/vermillion", - "secret/icebreaker/armitage/periwinkle" })); + assertThat(composite.getPropertySources()).extracting("name").contains( + "secret/wintermute", "secret/straylight", "secret/icebreaker/armitage", + "secret/wintermute/vermillion", "secret/wintermute/periwinkle", + "secret/straylight/vermillion", "secret/straylight/periwinkle", + "secret/icebreaker/armitage/vermillion", + "secret/icebreaker/armitage/periwinkle"); } -} \ No newline at end of file +}