package sample;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.cloud.config.server.test.ConfigServerTestUtils;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.SocketUtils;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class,
// Normally spring.cloud.config.enabled:true is the default but since we have the config
// server on the classpath we need to set it explicitly
	properties = { "spring.cloud.config.enabled:true",
			"management.security.enabled=false", "endpoints.default.web.enabled=true" }, webEnvironment = RANDOM_PORT)
public class ApplicationTests {

	private static int configPort = SocketUtils.findAvailableTcpPort();

	@LocalServerPort
	private int port;

	private static ConfigurableApplicationContext server;

	@BeforeClass
	public static void startConfigServer() throws IOException {
		String baseDir = ConfigServerTestUtils
				.getBaseDirectory("spring-cloud-config-sample");
		String repo = ConfigServerTestUtils.prepareLocalRepo(baseDir, "target/repos",
				"config-repo", "target/config");
		server = SpringApplication.run(
				org.springframework.cloud.config.server.ConfigServerApplication.class,
				"--server.port=" + configPort, "--spring.config.name=server",
				"--spring.cloud.config.server.git.uri=" + repo);
		/*FIXME configPort = ((EmbeddedWebApplicationContext) server)
				.getEmbeddedServletContainer().getPort();*/
		System.setProperty("config.port", "" + configPort);
	}

	@AfterClass
	public static void close() {
		System.clearProperty("config.port");
		if (server != null) {
			server.close();
		}
	}

	@Test
	@SuppressWarnings("unchecked")
	public void contextLoads() {
		Map res = new TestRestTemplate()
				.getForObject("http://localhost:" + port + "/application/env/info.foo", Map.class);
		assertThat(res).containsKey("propertySources");
		List<Map<String, Object>> sources = (List<Map<String,Object>>) res.get("propertySources");
		Optional<Map<String, Object>> properties = sources.stream()
				.map(map -> (Map<String, Object>) map.get("properties"))
				.filter(map -> map.containsKey("info.foo"))
				.map(map -> (Map<String, Object>) map.get("info.foo"))
				.findFirst();
		assertThat(properties.get()).containsEntry("value", "bar");
	}

	public static void main(String[] args) throws IOException {
		configPort = 8888;
		startConfigServer();
		SpringApplication.run(Application.class, args);
	}

}

@@ -2506,21 +2506,16 @@
 es%22);%0A%09%09
-List%3C
 Map%3CStri
@@ -2529,152 +2529,19 @@
 ect%3E
-%3E sources = (List%3CMap%3CString,Object%3E%3E) res.get(%22propertySources%22);%0A%09%09Optional%3CMap%3CString, Object%3E%3E properties = sources.stream()%0A%09%09%09%09.map(map -%3E
+ property =
  (Ma
@@ -2541,33 +2541,32 @@
 y = (Map%3CString,
- 
 Object%3E) map.get
@@ -2554,27 +2554,27 @@
 ng,Object%3E) 
-map
+res
 .get(%22proper
@@ -2578,137 +2578,10 @@
 pert
-ies%22))%0A%09%09%09%09.filter(map -%3E map.containsKey(%22info.foo%22))%0A%09%09%09%09.map(map -%3E (Map%3CString, Object%3E) map.get(%22info.foo%22))%0A%09%09%09%09.findFirst(
+y%22
 );%0A%09
@@ -2603,17 +2603,9 @@
 pert
-ies.get()
+y
 ).co

Update tests for spring boot 2.0 compatibility