package org.springframework.cloud.vault.config;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.vault.util.IntegrationTestSupport;
import org.springframework.cloud.vault.util.VaultRule;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.Collections;

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

/**
 * Integration test incorporating loading secrets using {@code spring.cloud.vault.applicationName}
 * and active profiles
 *
 * @author Ryan Hoegg
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = VaultPropertySourceLocatorIntegrationTests.TestApplication.class)
public class VaultPropertySourceLocatorIntegrationTests extends IntegrationTestSupport {

	@BeforeClass
	public static void beforeClass() throws Exception {
		System.setProperty("spring.application.name", "wintermute");
		System.setProperty("spring.cloud.vault.application-name", "neuromancer");
		System.setProperty("spring.cloud.vault.generic.application-name", "neuromancer,icebreaker");
		System.setProperty("spring.profiles.active", "integrationtest");
		VaultRule vaultRule = new VaultRule();
		vaultRule.before();

		vaultRule.prepare().getVaultOperations().write("secret/wintermute",
			Collections.singletonMap("vault.value", "spring.application.name value"));
		vaultRule.prepare().getVaultOperations()
			.write("secret/wintermute/integrationtest",
				Collections.singletonMap("vault.value",
					"spring.application.name:integrationtest value"));
		vaultRule.prepare().getVaultOperations().write("secret/neuromancer",
			Collections.singletonMap("vault.value",	"spring.cloud.vault.applicationName value"));
		vaultRule.prepare().getVaultOperations()
			.write("secret/neuromancer/integrationtest",
				Collections.singletonMap("vault.value",
					"spring.cloud.vault.applicationName:integrationtest value"));
		vaultRule.prepare().getVaultOperations()
			.write("secret/icebreaker",
				Collections.singletonMap("icebreaker.value",
					"additional context value"));
		vaultRule.prepare().getVaultOperations()
			.write("secret/icebreaker/integrationtest",
				Collections.singletonMap("icebreaker.value",
					"additional context:integrationtest value"));
	}

	@Value("${vault.value}")
	String configValue;

	@Value("${icebreaker.value}")
	String additionalValue;

	@Test
	public void getsSecretFromVaultUsingVaultApplicationName() {
		assertThat(configValue)
			.isEqualTo("spring.cloud.vault.applicationName:integrationtest value");
	}

	@Test
	public void getsSecretFromVaultUsingAdditionalContext() {
		assertThat(additionalValue)
			.isEqualTo("additional context:integrationtest value");
	}

	@SpringBootApplication
	public static class TestApplication {

		public static void main(String[] args) {
			SpringApplication
				.run(VaultConfigWithContextTests.TestApplication.class, args);
		}
	}

}

@@ -516,16 +516,72 @@
 ltRule;%0A
+import org.springframework.test.context.ActiveProfiles;%0A
 import o
@@ -1015,188 +1015,26 @@
 lass
-)%0Apublic class VaultPropertySourceLocatorIntegrationTests extends IntegrationTestSupport %7B%0A%0A%09@BeforeClass%0A%09public static void beforeClass() throws Exception %7B%0A%09%09System.setProperty(
+, properties = %7B%0A%09
 %22spr
@@ -1053,20 +1053,17 @@
 ion.name
-%22, %22
+=
 wintermu
@@ -1069,32 +1069,11 @@
 ute%22
-);%0A%09%09System.setProperty(
+,%0A%09
 %22spr
@@ -1096,36 +1096,33 @@
 application-name
-%22, %22
+=
 neuromancer%22);%0A%09
@@ -1121,32 +1121,11 @@
 cer%22
-);%0A%09%09System.setProperty(
+,%0A%09
 %22spr
@@ -1164,20 +1164,17 @@
 ion-name
-%22, %22
+=
 neuroman
@@ -1192,77 +1192,205 @@
 ker%22
-);%0A%09%09System.setProperty(%22spring.profiles.active%22, %22integrationtest%22);
+%0A%7D)%0A@ActiveProfiles(%7B%22integrationtest%22%7D)%0Apublic class VaultPropertySourceLocatorIntegrationTests extends IntegrationTestSupport %7B%0A%0A%09@BeforeClass%0A%09public static void beforeClass() throws Exception %7B
 %0A%09%09V

Replace System.setProperty calls with annotation configuration to fix test isolation problems