package org.springframework.boot.gradle.docs;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Collections;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
import java.util.zip.ZipEntry;

import org.gradle.testkit.runner.BuildResult;
import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;

import org.springframework.boot.gradle.junit.GradleMultiDslExtension;
import org.springframework.boot.testsupport.gradle.testkit.GradleBuild;
import org.springframework.util.FileCopyUtils;

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

/**
 * Tests for the packaging documentation.
 *
 * @author Andy Wilkinson
 * @author Jean-Baptiste Nizet
 * @author Scott Frederick
 */
@ExtendWith(GradleMultiDslExtension.class)
class PackagingDocumentationTests {

	GradleBuild gradleBuild;

	@TestTemplate
	void warContainerDependencyEvaluatesSuccessfully() {
		this.gradleBuild.script("src/docs/gradle/packaging/war-container-dependency").build();
	}

	@TestTemplate
	void bootJarMainClass() throws IOException {
		this.gradleBuild.script("src/docs/gradle/packaging/boot-jar-main-class").build("bootJar");
		File file = new File(this.gradleBuild.getProjectDir(),
				"build/libs/" + this.gradleBuild.getProjectDir().getName() + ".jar");
		assertThat(file).isFile();
		try (JarFile jar = new JarFile(file)) {
			assertThat(jar.getManifest().getMainAttributes().getValue("Start-Class"))
					.isEqualTo("com.example.ExampleApplication");
		}
	}

	@TestTemplate
	void bootJarManifestMainClass() throws IOException {
		this.gradleBuild.script("src/docs/gradle/packaging/boot-jar-manifest-main-class").build("bootJar");
		File file = new File(this.gradleBuild.getProjectDir(),
				"build/libs/" + this.gradleBuild.getProjectDir().getName() + ".jar");
		assertThat(file).isFile();
		try (JarFile jar = new JarFile(file)) {
			assertThat(jar.getManifest().getMainAttributes().getValue("Start-Class"))
					.isEqualTo("com.example.ExampleApplication");
		}
	}

	@TestTemplate
	void applicationPluginMainClass() throws IOException {
		this.gradleBuild.script("src/docs/gradle/packaging/application-plugin-main-class").build("bootJar");
		File file = new File(this.gradleBuild.getProjectDir(),
				"build/libs/" + this.gradleBuild.getProjectDir().getName() + ".jar");
		assertThat(file).isFile();
		try (JarFile jar = new JarFile(file)) {
			assertThat(jar.getManifest().getMainAttributes().getValue("Start-Class"))
					.isEqualTo("com.example.ExampleApplication");
		}
	}

	@TestTemplate
	void springBootDslMainClass() throws IOException {
		this.gradleBuild.script("src/docs/gradle/packaging/spring-boot-dsl-main-class").build("bootJar");
		File file = new File(this.gradleBuild.getProjectDir(),
				"build/libs/" + this.gradleBuild.getProjectDir().getName() + ".jar");
		assertThat(file).isFile();
		try (JarFile jar = new JarFile(file)) {
			assertThat(jar.getManifest().getMainAttributes().getValue("Start-Class"))
					.isEqualTo("com.example.ExampleApplication");
		}
	}

	@TestTemplate
	void bootWarIncludeDevtools() throws IOException {
		jarFile(new File(this.gradleBuild.getProjectDir(), "spring-boot-devtools-1.2.3.RELEASE.jar"));
		this.gradleBuild.script("src/docs/gradle/packaging/boot-war-include-devtools").build("bootWar");
		File file = new File(this.gradleBuild.getProjectDir(),
				"build/libs/" + this.gradleBuild.getProjectDir().getName() + ".war");
		assertThat(file).isFile();
		try (JarFile jar = new JarFile(file)) {
			assertThat(jar.getEntry("WEB-INF/lib/spring-boot-devtools-1.2.3.RELEASE.jar")).isNotNull();
		}
	}

	@TestTemplate
	void bootJarRequiresUnpack() throws IOException {
		this.gradleBuild.script("src/docs/gradle/packaging/boot-jar-requires-unpack").build("bootJar");
		File file = new File(this.gradleBuild.getProjectDir(),
				"build/libs/" + this.gradleBuild.getProjectDir().getName() + ".jar");
		assertThat(file).isFile();
		try (JarFile jar = new JarFile(file)) {
			JarEntry entry = jar.getJarEntry("BOOT-INF/lib/jruby-complete-1.7.25.jar");
			assertThat(entry).isNotNull();
			assertThat(entry.getComment()).startsWith("UNPACK:");
		}
	}

	@TestTemplate
	void bootJarIncludeLaunchScript() throws IOException {
		this.gradleBuild.script("src/docs/gradle/packaging/boot-jar-include-launch-script").build("bootJar");
		File file = new File(this.gradleBuild.getProjectDir(),
				"build/libs/" + this.gradleBuild.getProjectDir().getName() + ".jar");
		assertThat(file).isFile();
		assertThat(FileCopyUtils.copyToString(new FileReader(file))).startsWith("#!/bin/bash");
	}

	@TestTemplate
	void bootJarLaunchScriptProperties() throws IOException {
		this.gradleBuild.script("src/docs/gradle/packaging/boot-jar-launch-script-properties").build("bootJar");
		File file = new File(this.gradleBuild.getProjectDir(),
				"build/libs/" + this.gradleBuild.getProjectDir().getName() + ".jar");
		assertThat(file).isFile();
		assertThat(FileCopyUtils.copyToString(new FileReader(file))).contains("example-app.log");
	}

	@TestTemplate
	void bootJarCustomLaunchScript() throws IOException {
		File customScriptFile = new File(this.gradleBuild.getProjectDir(), "src/custom.script");
		customScriptFile.getParentFile().mkdirs();
		FileCopyUtils.copy("custom", new FileWriter(customScriptFile));
		this.gradleBuild.script("src/docs/gradle/packaging/boot-jar-custom-launch-script").build("bootJar");
		File file = new File(this.gradleBuild.getProjectDir(),
				"build/libs/" + this.gradleBuild.getProjectDir().getName() + ".jar");
		assertThat(file).isFile();
		assertThat(FileCopyUtils.copyToString(new FileReader(file))).startsWith("custom");
	}

	@TestTemplate
	void bootWarPropertiesLauncher() throws IOException {
		this.gradleBuild.script("src/docs/gradle/packaging/boot-war-properties-launcher").build("bootWar");
		File file = new File(this.gradleBuild.getProjectDir(),
				"build/libs/" + this.gradleBuild.getProjectDir().getName() + ".war");
		assertThat(file).isFile();
		try (JarFile jar = new JarFile(file)) {
			assertThat(jar.getManifest().getMainAttributes().getValue("Main-Class"))
					.isEqualTo("org.springframework.boot.loader.PropertiesLauncher");
		}
	}

	@TestTemplate
	void onlyBootJar() throws IOException {
		this.gradleBuild.script("src/docs/gradle/packaging/only-boot-jar").build("assemble");
		File plainJar = new File(this.gradleBuild.getProjectDir(),
				"build/libs/" + this.gradleBuild.getProjectDir().getName() + "-plain.jar");
		assertThat(plainJar).doesNotExist();
		File bootJar = new File(this.gradleBuild.getProjectDir(),
				"build/libs/" + this.gradleBuild.getProjectDir().getName() + ".jar");
		assertThat(bootJar).isFile();
		try (JarFile jar = new JarFile(bootJar)) {
			assertThat(jar.getEntry("BOOT-INF/")).isNotNull();
		}
	}

	@TestTemplate
	void classifiedBootJar() throws IOException {
		this.gradleBuild.script("src/docs/gradle/packaging/boot-jar-and-jar-classifiers").build("assemble");
		File plainJar = new File(this.gradleBuild.getProjectDir(),
				"build/libs/" + this.gradleBuild.getProjectDir().getName() + ".jar");
		assertThat(plainJar).isFile();
		try (JarFile jar = new JarFile(plainJar)) {
			assertThat(jar.getEntry("BOOT-INF/")).isNull();
		}
		File bootJar = new File(this.gradleBuild.getProjectDir(),
				"build/libs/" + this.gradleBuild.getProjectDir().getName() + "-boot.jar");
		assertThat(bootJar).isFile();
		try (JarFile jar = new JarFile(bootJar)) {
			assertThat(jar.getEntry("BOOT-INF/")).isNotNull();
		}
	}

	@TestTemplate
	void bootJarLayeredDisabled() throws IOException {
		this.gradleBuild.script("src/docs/gradle/packaging/boot-jar-layered-disabled").build("bootJar");
		File file = new File(this.gradleBuild.getProjectDir(),
				"build/libs/" + this.gradleBuild.getProjectDir().getName() + ".jar");
		assertThat(file).isFile();
		try (JarFile jar = new JarFile(file)) {
			JarEntry entry = jar.getJarEntry("BOOT-INF/layers.idx");
			assertThat(entry).isNull();
		}
	}

	@TestTemplate
	void bootJarLayeredCustom() throws IOException {
		this.gradleBuild.script("src/docs/gradle/packaging/boot-jar-layered-custom").build("bootJar");
		File file = new File(this.gradleBuild.getProjectDir(),
				"build/libs/" + this.gradleBuild.getProjectDir().getName() + ".jar");
		assertThat(file).isFile();
		try (JarFile jar = new JarFile(file)) {
			JarEntry entry = jar.getJarEntry("BOOT-INF/layers.idx");
			assertThat(entry).isNotNull();
			assertThat(Collections.list(jar.entries()).stream().map(JarEntry::getName)
					.filter((name) -> name.startsWith("BOOT-INF/lib/spring-boot"))).isNotEmpty();
		}
	}

	@TestTemplate
	void bootJarLayeredExcludeTools() throws IOException {
		this.gradleBuild.script("src/docs/gradle/packaging/boot-jar-layered-exclude-tools").build("bootJar");
		File file = new File(this.gradleBuild.getProjectDir(),
				"build/libs/" + this.gradleBuild.getProjectDir().getName() + ".jar");
		assertThat(file).isFile();
		try (JarFile jar = new JarFile(file)) {
			JarEntry entry = jar.getJarEntry("BOOT-INF/layers.idx");
			assertThat(entry).isNotNull();
			assertThat(Collections.list(jar.entries()).stream().map(JarEntry::getName)
					.filter((name) -> name.startsWith("BOOT-INF/lib/spring-boot"))).isEmpty();
		}
	}

	@TestTemplate
	void bootBuildImageWithBuilder() {
		BuildResult result = this.gradleBuild.script("src/docs/gradle/packaging/boot-build-image-builder")
				.build("bootBuildImageBuilder");
		assertThat(result.getOutput()).contains("builder=mine/java-cnb-builder").contains("runImage=mine/java-cnb-run");
	}

	@TestTemplate
	void bootBuildImageWithCustomBuildpackJvmVersion() {
		BuildResult result = this.gradleBuild.script("src/docs/gradle/packaging/boot-build-image-env")
				.build("bootBuildImageEnvironment");
		assertThat(result.getOutput()).contains("BP_JVM_VERSION=17");
	}

	@TestTemplate
	void bootBuildImageWithCustomProxySettings() {
		BuildResult result = this.gradleBuild.script("src/docs/gradle/packaging/boot-build-image-env-proxy")
				.build("bootBuildImageEnvironment");
		assertThat(result.getOutput()).contains("HTTP_PROXY=http://proxy.example.com")
				.contains("HTTPS_PROXY=https://proxy.example.com");
	}

	@TestTemplate
	void bootBuildImageWithCustomRuntimeConfiguration() {
		BuildResult result = this.gradleBuild.script("src/docs/gradle/packaging/boot-build-image-env-runtime")
				.build("bootBuildImageEnvironment");
		assertThat(result.getOutput()).contains("BPE_DELIM_JAVA_TOOL_OPTIONS= ")
				.contains("BPE_APPEND_JAVA_TOOL_OPTIONS=-XX:+HeapDumpOnOutOfMemoryError");
	}

	@TestTemplate
	void bootBuildImageWithCustomImageName() {
		BuildResult result = this.gradleBuild.script("src/docs/gradle/packaging/boot-build-image-name")
				.build("bootBuildImageName");
		assertThat(result.getOutput()).contains("example.com/library/" + this.gradleBuild.getProjectDir().getName());
	}

	@TestTemplate
	void bootBuildImageWithDockerHostMinikube() {
		BuildResult result = this.gradleBuild.script("src/docs/gradle/packaging/boot-build-image-docker-host")
				.build("bootBuildImageDocker");
		assertThat(result.getOutput()).contains("host=tcp://192.168.99.100:2376").contains("tlsVerify=true")
				.contains("certPath=/home/user/.minikube/certs");
	}

	@TestTemplate
	void bootBuildImageWithDockerHostPodman() {
		BuildResult result = this.gradleBuild.script("src/docs/gradle/packaging/boot-build-image-docker-host-podman")
				.build("bootBuildImageDocker");
		assertThat(result.getOutput()).contains("host=unix:///run/user/1000/podman/podman.sock")
				.contains("bindHostToBuilder=true");
	}

	@TestTemplate
	void bootBuildImageWithDockerUserAuth() {
		BuildResult result = this.gradleBuild.script("src/docs/gradle/packaging/boot-build-image-docker-auth-user")
				.build("bootBuildImageDocker");
		assertThat(result.getOutput()).contains("username=user").contains("password=secret")
				.contains("url=https://docker.example.com/v1/").contains("email=user@example.com");
	}

	@TestTemplate
	void bootBuildImageWithDockerTokenAuth() {
		BuildResult result = this.gradleBuild.script("src/docs/gradle/packaging/boot-build-image-docker-auth-token")
				.build("bootBuildImageDocker");
		assertThat(result.getOutput()).contains("token=9cbaf023786cd7...");
	}

	@TestTemplate
	void bootBuildImagePublish() {
		BuildResult result = this.gradleBuild.script("src/docs/gradle/packaging/boot-build-image-publish")
				.build("bootBuildImagePublish");
		assertThat(result.getOutput()).contains("true");
	}

	@TestTemplate
	void bootBuildImageWithBuildpacks() {
		BuildResult result = this.gradleBuild.script("src/docs/gradle/packaging/boot-build-image-buildpacks")
				.build("bootBuildImageBuildpacks");
		assertThat(result.getOutput()).contains("file:///path/to/example-buildpack.tgz")
				.contains("urn:cnb:builder:paketo-buildpacks/java");
	}

	@TestTemplate
	void bootBuildImageWithCaches() {
		BuildResult result = this.gradleBuild.script("src/docs/gradle/packaging/boot-build-image-caches")
				.build("bootBuildImageCaches");
		assertThat(result.getOutput()).containsPattern("buildCache=cache-gradle-[\\d]+.build")
				.containsPattern("launchCache=cache-gradle-[\\d]+.launch");
	}

	protected void jarFile(File file) throws IOException {
		try (JarOutputStream jar = new JarOutputStream(new FileOutputStream(file))) {
			jar.putNextEntry(new ZipEntry("META-INF/MANIFEST.MF"));
			new Manifest().write(jar);
			jar.closeEntry();
		}
	}

}

package org.springframework.boot.actuate.autoconfigure.observation.web.reactive;

import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.Tags;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.test.MetricsRun;
import org.springframework.boot.actuate.autoconfigure.metrics.web.TestController;
import org.springframework.boot.actuate.autoconfigure.observation.ObservationAutoConfiguration;
import org.springframework.boot.actuate.metrics.web.reactive.server.DefaultWebFluxTagsProvider;
import org.springframework.boot.actuate.metrics.web.reactive.server.WebFluxTagsContributor;
import org.springframework.boot.actuate.metrics.web.reactive.server.WebFluxTagsProvider;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration;
import org.springframework.boot.test.context.assertj.AssertableReactiveWebApplicationContext;
import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner;
import org.springframework.boot.test.system.CapturedOutput;
import org.springframework.boot.test.system.OutputCaptureExtension;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.web.filter.reactive.ServerHttpObservationFilter;
import org.springframework.web.server.ServerWebExchange;

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

/**
 * Tests for {@link WebFluxObservationAutoConfiguration}
 *
 * @author Brian Clozel
 * @author Dmytro Nosan
 * @author Madhura Bhave
 */
@ExtendWith(OutputCaptureExtension.class)
@SuppressWarnings("removal")
class WebFluxObservationAutoConfigurationTests {

	private final ReactiveWebApplicationContextRunner contextRunner = new ReactiveWebApplicationContextRunner()
			.with(MetricsRun.simple()).withConfiguration(AutoConfigurations.of(ObservationAutoConfiguration.class,
					WebFluxObservationAutoConfiguration.class));

	@Test
	void shouldProvideWebFluxObservationFilter() {
		this.contextRunner.run((context) -> assertThat(context).hasSingleBean(ServerHttpObservationFilter.class));
	}

	@Test
	void shouldUseConventionAdapterWhenCustomTagsProvider() {
		this.contextRunner.withUserConfiguration(CustomTagsProviderConfiguration.class).run((context) -> {
			assertThat(context).hasSingleBean(ServerHttpObservationFilter.class);
			assertThat(context).hasSingleBean(WebFluxTagsProvider.class);
			assertThat(context).getBean(ServerHttpObservationFilter.class).extracting("observationConvention")
					.isInstanceOf(ServerRequestObservationConventionAdapter.class);
		});
	}

	@Test
	void shouldUseConventionAdapterWhenCustomTagsContributor() {
		this.contextRunner.withUserConfiguration(CustomTagsContributorConfiguration.class).run((context) -> {
			assertThat(context).hasSingleBean(ServerHttpObservationFilter.class);
			assertThat(context).hasSingleBean(WebFluxTagsContributor.class);
			assertThat(context).getBean(ServerHttpObservationFilter.class).extracting("observationConvention")
					.isInstanceOf(ServerRequestObservationConventionAdapter.class);
		});
	}

	@Test
	void afterMaxUrisReachedFurtherUrisAreDenied(CapturedOutput output) {
		this.contextRunner.withUserConfiguration(TestController.class)
				.withConfiguration(AutoConfigurations.of(MetricsAutoConfiguration.class,
						ObservationAutoConfiguration.class, WebFluxAutoConfiguration.class))
				.withPropertyValues("management.metrics.web.server.max-uri-tags=2").run((context) -> {
					MeterRegistry registry = getInitializedMeterRegistry(context);
					assertThat(registry.get("http.server.requests").meters().size()).isLessThanOrEqualTo(2);
					assertThat(output).contains("Reached the maximum number of URI tags for 'http.server.requests'");
				});
	}

	@Test
	@Deprecated(since = "3.0.0", forRemoval = true)
	void afterMaxUrisReachedFurtherUrisAreDeniedWhenUsingCustomMetricName(CapturedOutput output) {
		this.contextRunner.withUserConfiguration(TestController.class)
				.withConfiguration(AutoConfigurations.of(MetricsAutoConfiguration.class,
						ObservationAutoConfiguration.class, WebFluxAutoConfiguration.class))
				.withPropertyValues("management.metrics.web.server.max-uri-tags=2",
						"management.metrics.web.server.request.metric-name=my.http.server.requests")
				.run((context) -> {
					MeterRegistry registry = getInitializedMeterRegistry(context);
					assertThat(registry.get("my.http.server.requests").meters().size()).isLessThanOrEqualTo(2);
					assertThat(output).contains("Reached the maximum number of URI tags for 'my.http.server.requests'");
				});
	}

	@Test
	void afterMaxUrisReachedFurtherUrisAreDeniedWhenUsingCustomObservationName(CapturedOutput output) {
		this.contextRunner.withUserConfiguration(TestController.class)
				.withConfiguration(AutoConfigurations.of(MetricsAutoConfiguration.class,
						ObservationAutoConfiguration.class, WebFluxAutoConfiguration.class))
				.withPropertyValues("management.metrics.web.server.max-uri-tags=2",
						"management.observations.http.server.requests.name=my.http.server.requests")
				.run((context) -> {
					MeterRegistry registry = getInitializedMeterRegistry(context);
					assertThat(registry.get("my.http.server.requests").meters().size()).isLessThanOrEqualTo(2);
					assertThat(output).contains("Reached the maximum number of URI tags for 'my.http.server.requests'");
				});
	}

	@Test
	void shouldNotDenyNorLogIfMaxUrisIsNotReached(CapturedOutput output) {
		this.contextRunner.withUserConfiguration(TestController.class)
				.withConfiguration(AutoConfigurations.of(MetricsAutoConfiguration.class,
						ObservationAutoConfiguration.class, WebFluxAutoConfiguration.class))
				.withPropertyValues("management.metrics.web.server.max-uri-tags=5").run((context) -> {
					MeterRegistry registry = getInitializedMeterRegistry(context);
					assertThat(registry.get("http.server.requests").meters()).hasSize(3);
					assertThat(output)
							.doesNotContain("Reached the maximum number of URI tags for 'http.server.requests'");
				});
	}

	private MeterRegistry getInitializedMeterRegistry(AssertableReactiveWebApplicationContext context)
			throws Exception {
		return getInitializedMeterRegistry(context, "/test0", "/test1", "/test2");
	}

	private MeterRegistry getInitializedMeterRegistry(AssertableReactiveWebApplicationContext context, String... urls)
			throws Exception {
		assertThat(context).hasSingleBean(ServerHttpObservationFilter.class);
		WebTestClient client = WebTestClient.bindToApplicationContext(context).build();
		for (String url : urls) {
			client.get().uri(url).exchange().expectStatus().isOk();
		}
		return context.getBean(MeterRegistry.class);
	}

	@Deprecated(since = "3.0.0", forRemoval = true)
	@Configuration(proxyBeanMethods = false)
	static class CustomTagsProviderConfiguration {

		@Bean
		WebFluxTagsProvider tagsProvider() {
			return new DefaultWebFluxTagsProvider();
		}

	}

	@Configuration(proxyBeanMethods = false)
	static class CustomTagsContributorConfiguration {

		@Bean
		WebFluxTagsContributor tagsContributor() {
			return new CustomTagsContributor();
		}

	}

	@Deprecated(since = "3.0.0", forRemoval = true)
	static class CustomTagsContributor implements WebFluxTagsContributor {

		@Override
		public Iterable<Tag> httpRequestTags(ServerWebExchange exchange, Throwable ex) {
			return Tags.of("custom", "testvalue");
		}

	}

}

// borrowed from Spring
// https://github.com/spring-projects/spring-boot/blob/67af4c0a653d7db77cc3093809c1b7ccdcb99f2a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/docs/PackagingDocumentationTests.java
