diff --git a/jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/JavaProject.java b/jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/JavaProject.java index a7287cbb98..279fd9e99b 100644 --- a/jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/JavaProject.java +++ b/jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/JavaProject.java @@ -21,7 +21,6 @@ import java.io.File; import java.io.Serializable; -import java.net.URL; import java.time.LocalDate; import java.util.List; import java.util.Locale; @@ -38,14 +37,7 @@ public class JavaProject implements Serializable { private static final long serialVersionUID = 6438404976521622633L; - - /** - * Properties file for the project - * - * @param propertiesFile for the project. - * @return Properties file for the project. - */ - private URL propertiesFile; + /** * Project's name. * diff --git a/jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/PropertiesExtender.java b/jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/PropertiesExtender.java deleted file mode 100644 index 3174ba635a..0000000000 --- a/jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/PropertiesExtender.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (c) 2019 Red Hat, Inc. - * This program and the accompanying materials are made - * available under the terms of the Eclipse Public License 2.0 - * which is available at: - * - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * Red Hat, Inc. - initial API and implementation - */ - -package org.eclipse.jkube.kit.common; - -import java.net.URL; -import java.util.Properties; - -import lombok.Getter; -import lombok.Setter; - -// This class expends Properties class -// with URL of properties file path. -public class PropertiesExtender extends Properties { - @Getter - @Setter - private URL propertiesFile; -} diff --git a/jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/util/PropertiesUtil.java b/jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/util/PropertiesUtil.java index 932c82a554..cb16ee5386 100644 --- a/jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/util/PropertiesUtil.java +++ b/jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/util/PropertiesUtil.java @@ -32,6 +32,7 @@ import static org.eclipse.jkube.kit.common.util.YamlUtil.getPropertiesFromYamlResource; public class PropertiesUtil { + private static final String JKUBE_INTERNAL_APP_CONFIG_FILE_LOCATION = "jkube.internal.application-config-file.path"; private PropertiesUtil() {} @@ -102,14 +103,16 @@ public static Properties fromApplicationConfig(JavaProject javaProject, String[] final URLClassLoader urlClassLoader = getClassLoader(javaProject); for (String source : appConfigSources) { final Properties properties; + URL applicationConfigSource = urlClassLoader.findResource(source); if (source.endsWith(".properties")) { - properties = getPropertiesFromResource(urlClassLoader.findResource(source)); + properties = getPropertiesFromResource(applicationConfigSource); } else { - properties = getPropertiesFromYamlResource(urlClassLoader.findResource(source)); + properties = getPropertiesFromYamlResource(applicationConfigSource); } // Consider only the first non-empty application config source if (!properties.isEmpty()) { properties.putAll(toMap(javaProject.getProperties())); + properties.put(JKUBE_INTERNAL_APP_CONFIG_FILE_LOCATION, applicationConfigSource); return properties; } } diff --git a/jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/util/SpringBootConfiguration.java b/jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/util/SpringBootConfiguration.java index dd07199a0a..c396356c34 100644 --- a/jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/util/SpringBootConfiguration.java +++ b/jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/util/SpringBootConfiguration.java @@ -16,7 +16,6 @@ import lombok.Builder; import lombok.Getter; import org.eclipse.jkube.kit.common.JavaProject; -import org.eclipse.jkube.kit.common.PropertiesExtender; import java.util.Optional; import java.util.Properties; @@ -40,11 +39,9 @@ public class SpringBootConfiguration { private boolean managementHealthProbesEnabled; public static SpringBootConfiguration from(JavaProject project) { - final PropertiesExtender properties = SpringBootUtil.getSpringBootApplicationProperties( + final Properties properties = SpringBootUtil.getSpringBootApplicationProperties( SpringBootUtil.getSpringBootActiveProfile(project), JKubeProjectUtil.getClassLoader(project)); - project.setPropertiesFile(properties.getPropertiesFile()); - final int majorVersion = SpringBootUtil.getSpringBootVersion(project) .map(semVer -> { try { diff --git a/jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/util/SpringBootUtil.java b/jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/util/SpringBootUtil.java index adfee8fb61..59602cfc12 100644 --- a/jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/util/SpringBootUtil.java +++ b/jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/util/SpringBootUtil.java @@ -23,7 +23,6 @@ import org.eclipse.jkube.kit.common.JavaProject; import org.eclipse.jkube.kit.common.Plugin; -import org.eclipse.jkube.kit.common.PropertiesExtender; import static org.eclipse.jkube.kit.common.util.PropertiesUtil.getPropertiesFromResource; @@ -44,6 +43,7 @@ public class SpringBootUtil { private static final String PLACEHOLDER_PREFIX = "${"; private static final String PLACEHOLDER_SUFFIX = "}"; private static final String VALUE_SEPARATOR = ":"; + private static final String JKUBE_INTERNAL_APP_CONFIG_FILE_LOCATION = "jkube.internal.application-config-file.path"; private SpringBootUtil() {} @@ -66,22 +66,19 @@ public static Properties getSpringBootApplicationProperties(URLClassLoader compi * @param compileClassLoader compile class loader * @return properties object */ - public static PropertiesExtender getSpringBootApplicationProperties(String springActiveProfile, URLClassLoader compileClassLoader) { + public static Properties getSpringBootApplicationProperties(String springActiveProfile, URLClassLoader compileClassLoader) { URL ymlResource = compileClassLoader.findResource("application.yml"); URL propertiesResource = compileClassLoader.findResource("application.properties"); - + Properties props = YamlUtil.getPropertiesFromYamlResource(springActiveProfile, ymlResource); props.putAll(getPropertiesFromResource(propertiesResource)); - props = new SpringBootPropertyPlaceholderHelper(PLACEHOLDER_PREFIX, PLACEHOLDER_SUFFIX, VALUE_SEPARATOR, true) + if (ymlResource != null) { + props.put(JKUBE_INTERNAL_APP_CONFIG_FILE_LOCATION, ymlResource.toString()); + } else if (propertiesResource != null) { + props.put(JKUBE_INTERNAL_APP_CONFIG_FILE_LOCATION, propertiesResource.toString()); + } + return new SpringBootPropertyPlaceholderHelper(PLACEHOLDER_PREFIX, PLACEHOLDER_SUFFIX, VALUE_SEPARATOR, true) .replaceAllPlaceholders(props); - - // Extend Properties object with resources file path - PropertiesExtender propsExtender = new PropertiesExtender(); - URL propertiesFile = ymlResource != null ? ymlResource : propertiesResource; - propsExtender.setPropertiesFile(propertiesFile); - propsExtender.putAll(props); - - return propsExtender; } /** diff --git a/jkube-kit/common/src/test/java/org/eclipse/jkube/kit/common/util/PropertiesUtilTest.java b/jkube-kit/common/src/test/java/org/eclipse/jkube/kit/common/util/PropertiesUtilTest.java index 5618ff6f2a..ca9e9f41e6 100644 --- a/jkube-kit/common/src/test/java/org/eclipse/jkube/kit/common/util/PropertiesUtilTest.java +++ b/jkube-kit/common/src/test/java/org/eclipse/jkube/kit/common/util/PropertiesUtilTest.java @@ -154,8 +154,10 @@ void yml() { // When Properties properties = PropertiesUtil.fromApplicationConfig(javaProject, new String[]{"application.yml"}); // Then - assertThat(properties).containsExactly( - entry("application.name", "name-via-yaml")); + assertThat(properties) + .containsOnly( + entry("jkube.internal.application-config-file.path", PropertiesUtilTest.class.getResource("/util/properties-util/yaml/application.yml")), + entry("application.name", "name-via-yaml")); } @Test @@ -164,8 +166,10 @@ void properties() { // When Properties properties = PropertiesUtil.fromApplicationConfig(javaProject, new String[]{"application.properties"}); // Then - assertThat(properties).containsExactly( - entry("application.name", "name-via-properties")); + assertThat(properties) + .containsOnly( + entry("jkube.internal.application-config-file.path", PropertiesUtilTest.class.getResource("/util/properties-util/properties/application.properties")), + entry("application.name", "name-via-properties")); } @Test @@ -174,8 +178,10 @@ void multipleSources_thenFirstOneTakesPrecedence() { // When Properties properties = PropertiesUtil.fromApplicationConfig(javaProject, new String[]{"application.properties", "application.yml"}); // Then - assertThat(properties).containsExactly( - entry("application.name", "name-via-properties")); + assertThat(properties) + .containsOnly( + entry("jkube.internal.application-config-file.path", PropertiesUtilTest.class.getResource("/util/properties-util/properties/application.properties")), + entry("application.name", "name-via-properties")); } @Test @@ -184,8 +190,10 @@ void multipleSourcesWithEmpty_thenFirstNonEmptyTakesPrecedence() { // When Properties properties = PropertiesUtil.fromApplicationConfig(javaProject, new String[]{"not-there", "application.yml", "application.properties"}); // Then - assertThat(properties).containsExactly( - entry("application.name", "name-via-yaml")); + assertThat(properties) + .containsOnly( + entry("jkube.internal.application-config-file.path", PropertiesUtilTest.class.getResource("/util/properties-util/yaml/application.yml")), + entry("application.name", "name-via-yaml")); } } } diff --git a/jkube-kit/common/src/test/java/org/eclipse/jkube/kit/common/util/SpringBootConfigurationTest.java b/jkube-kit/common/src/test/java/org/eclipse/jkube/kit/common/util/SpringBootConfigurationTest.java index bea2af32a4..4271d9e4a8 100644 --- a/jkube-kit/common/src/test/java/org/eclipse/jkube/kit/common/util/SpringBootConfigurationTest.java +++ b/jkube-kit/common/src/test/java/org/eclipse/jkube/kit/common/util/SpringBootConfigurationTest.java @@ -60,7 +60,6 @@ void setUp(@TempDir Path target) throws IOException { project = JavaProject.builder() .properties(properties) .outputDirectory(target.toFile()) - .baseDirectory(target.toFile()) .build(); } diff --git a/jkube-kit/common/src/test/java/org/eclipse/jkube/kit/common/util/SpringBootUtilTest.java b/jkube-kit/common/src/test/java/org/eclipse/jkube/kit/common/util/SpringBootUtilTest.java index 7cada9aa18..7ad36ffd16 100644 --- a/jkube-kit/common/src/test/java/org/eclipse/jkube/kit/common/util/SpringBootUtilTest.java +++ b/jkube-kit/common/src/test/java/org/eclipse/jkube/kit/common/util/SpringBootUtilTest.java @@ -57,6 +57,7 @@ void getSpringBootApplicationPropertiesLoadsStandardProperties(@TempDir File tem Properties result = SpringBootUtil.getSpringBootApplicationProperties(cl); //Then assertThat(result).containsOnly( + entry("jkube.internal.application-config-file.path", tempDir.toPath().resolve("target/classes/application.properties").toUri().toURL().toString()), entry("spring.application.name", "demoservice"), entry("server.port", "9090") ); @@ -99,7 +100,6 @@ void getSpringBootApplicationProperties_withCompileClassloader_shouldLoadPropert JavaProject javaProject = JavaProject.builder() .compileClassPathElement(Objects.requireNonNull(getClass().getResource("/util/springboot/resources")).getPath()) .outputDirectory(new File("target")) - .baseDirectory(new File("target")) .build(); URLClassLoader compileClassLoader = JKubeProjectUtil.getClassLoader(javaProject); diff --git a/jkube-kit/generator/api/src/main/java/org/eclipse/jkube/generator/api/DefaultGeneratorManager.java b/jkube-kit/generator/api/src/main/java/org/eclipse/jkube/generator/api/DefaultGeneratorManager.java index 39157955b5..7937a95483 100644 --- a/jkube-kit/generator/api/src/main/java/org/eclipse/jkube/generator/api/DefaultGeneratorManager.java +++ b/jkube-kit/generator/api/src/main/java/org/eclipse/jkube/generator/api/DefaultGeneratorManager.java @@ -16,7 +16,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; -import java.net.URL; import java.util.List; import java.util.Properties; import java.util.Set; @@ -103,12 +102,6 @@ private List generateImages(List imageCo if (generator.isApplicable(ret)) { log.info("Running generator %s", generator.getName()); ret = generator.customize(ret, genCtx.isPrePackagePhase()); - URL propFileURL = genCtx.getProject().getPropertiesFile(); - if (propFileURL != null) { - log.info("The following properties file is used %s", propFileURL); - } else { - log.info("No properties file found"); - } } } return ret; diff --git a/jkube-kit/jkube-kit-helidon/pom.xml b/jkube-kit/jkube-kit-helidon/pom.xml index a9fdbfd85a..edc407f53f 100644 --- a/jkube-kit/jkube-kit-helidon/pom.xml +++ b/jkube-kit/jkube-kit-helidon/pom.xml @@ -65,6 +65,10 @@ org.assertj assertj-core + + org.mockito + mockito-core + diff --git a/jkube-kit/jkube-kit-helidon/src/main/java/org/eclipse/jkube/helidon/enricher/HelidonHealthCheckEnricher.java b/jkube-kit/jkube-kit-helidon/src/main/java/org/eclipse/jkube/helidon/enricher/HelidonHealthCheckEnricher.java index 470e51501f..705e1e264d 100644 --- a/jkube-kit/jkube-kit-helidon/src/main/java/org/eclipse/jkube/helidon/enricher/HelidonHealthCheckEnricher.java +++ b/jkube-kit/jkube-kit-helidon/src/main/java/org/eclipse/jkube/helidon/enricher/HelidonHealthCheckEnricher.java @@ -16,6 +16,8 @@ import org.eclipse.jkube.kit.enricher.api.JKubeEnricherContext; import org.eclipse.jkube.microprofile.enricher.AbstractMicroprofileHealthCheckEnricher; +import java.util.Properties; + import static org.eclipse.jkube.helidon.HelidonUtils.extractPort; import static org.eclipse.jkube.helidon.HelidonUtils.getHelidonConfiguration; import static org.eclipse.jkube.helidon.HelidonUtils.hasHelidonHealthDependency; @@ -23,8 +25,13 @@ public class HelidonHealthCheckEnricher extends AbstractMicroprofileHealthCheckEnricher { private static final String DEFAULT_HELIDON_PORT = "8080"; + private static final String JKUBE_INTERNAL_APP_CONFIG_FILE_LOCATION = "jkube.internal.application-config-file.path"; + private final Properties helidonApplicationConfiguration; + public HelidonHealthCheckEnricher(JKubeEnricherContext buildContext) { super(buildContext, "jkube-healthcheck-helidon"); + helidonApplicationConfiguration = getHelidonConfiguration(getContext().getProject()); + log.debug("Helidon Application Config loaded from : %s", helidonApplicationConfiguration.get(JKUBE_INTERNAL_APP_CONFIG_FILE_LOCATION)); } @Override @@ -34,6 +41,6 @@ protected boolean shouldAddProbe() { @Override protected int getPort() { - return asInteger(extractPort(getHelidonConfiguration(getContext().getProject()), DEFAULT_HELIDON_PORT)); + return asInteger(extractPort(helidonApplicationConfiguration, DEFAULT_HELIDON_PORT)); } } \ No newline at end of file diff --git a/jkube-kit/jkube-kit-helidon/src/main/java/org/eclipse/jkube/helidon/generator/HelidonGenerator.java b/jkube-kit/jkube-kit-helidon/src/main/java/org/eclipse/jkube/helidon/generator/HelidonGenerator.java index aa0ed1195e..f736af16ed 100644 --- a/jkube-kit/jkube-kit-helidon/src/main/java/org/eclipse/jkube/helidon/generator/HelidonGenerator.java +++ b/jkube-kit/jkube-kit-helidon/src/main/java/org/eclipse/jkube/helidon/generator/HelidonGenerator.java @@ -21,6 +21,7 @@ import java.util.List; import java.util.Optional; +import java.util.Properties; import static org.eclipse.jkube.helidon.HelidonUtils.extractPort; import static org.eclipse.jkube.helidon.HelidonUtils.getHelidonConfiguration; @@ -29,10 +30,14 @@ public class HelidonGenerator extends JavaExecGenerator { public static final String HELIDON = "helidon"; private final HelidonNestedGenerator nestedGenerator; + private static final String JKUBE_INTERNAL_APP_CONFIG_FILE_LOCATION = "jkube.internal.application-config-file.path"; + private final Properties helidonApplicationConfiguration; public HelidonGenerator(GeneratorContext context) { super(context, HELIDON); nestedGenerator = HelidonNestedGenerator.from(context, getGeneratorConfig()); + helidonApplicationConfiguration = getHelidonConfiguration(getContext().getProject()); + log.debug("Helidon Application Config loaded from : %s", helidonApplicationConfiguration.get(JKUBE_INTERNAL_APP_CONFIG_FILE_LOCATION)); } @Override @@ -62,7 +67,7 @@ protected Arguments getBuildEntryPoint() { @Override protected String getDefaultWebPort() { - return extractPort(getHelidonConfiguration(getProject()), super.getDefaultWebPort()); + return extractPort(helidonApplicationConfiguration, super.getDefaultWebPort()); } @Override diff --git a/jkube-kit/jkube-kit-helidon/src/test/java/org/eclipse/jkube/helidon/HelidonUtilsTest.java b/jkube-kit/jkube-kit-helidon/src/test/java/org/eclipse/jkube/helidon/HelidonUtilsTest.java index 9dbf6fd89f..d76657545a 100644 --- a/jkube-kit/jkube-kit-helidon/src/test/java/org/eclipse/jkube/helidon/HelidonUtilsTest.java +++ b/jkube-kit/jkube-kit-helidon/src/test/java/org/eclipse/jkube/helidon/HelidonUtilsTest.java @@ -92,6 +92,7 @@ void getHelidonConfiguration_whenApplicationYamlProvided_thenShouldExtractConfig final Properties props = HelidonUtils.getHelidonConfiguration(javaProject); // Then assertThat(props).containsOnly( + entry("jkube.internal.application-config-file.path", getClass().getResource("/utils-test/config/yaml/application.yml")), entry("app.greeting", "Hello"), entry("server.port", "8080"), entry("server.host", "0.0.0.0")); @@ -109,6 +110,7 @@ void getHelidonConfiguration_whenMicroprofilePropertiesProvided_thenShouldExtrac final Properties props = HelidonUtils.getHelidonConfiguration(javaProject); // Then assertThat(props).containsOnly( + entry("jkube.internal.application-config-file.path", getClass().getResource("/utils-test/config/properties/META-INF/microprofile-config.properties")), entry("app.greeting", "Hello"), entry("server.port", "8080"), entry("server.host", "0.0.0.0")); diff --git a/jkube-kit/jkube-kit-helidon/src/test/java/org/eclipse/jkube/helidon/enricher/HelidonHealthCheckEnricherTest.java b/jkube-kit/jkube-kit-helidon/src/test/java/org/eclipse/jkube/helidon/enricher/HelidonHealthCheckEnricherTest.java index d914c0c167..b45fa8accf 100644 --- a/jkube-kit/jkube-kit-helidon/src/test/java/org/eclipse/jkube/helidon/enricher/HelidonHealthCheckEnricherTest.java +++ b/jkube-kit/jkube-kit-helidon/src/test/java/org/eclipse/jkube/helidon/enricher/HelidonHealthCheckEnricherTest.java @@ -36,17 +36,22 @@ import java.util.function.Supplier; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; class HelidonHealthCheckEnricherTest { private JKubeEnricherContext context; private JavaProject javaProject; private Properties properties; private KubernetesListBuilder klb; + private KitLogger logger; @BeforeEach void setup() { properties = new Properties(); klb = new KubernetesListBuilder(); + logger = spy(new KitLogger.SilentLogger()); klb.addToItems(new DeploymentBuilder() .editOrNewSpec() .editOrNewTemplate() @@ -67,12 +72,27 @@ void setup() { .dependenciesWithTransitive(new ArrayList<>()) .build(); context = JKubeEnricherContext.builder() - .log(new KitLogger.SilentLogger()) + .log(logger) .project(javaProject) .processorConfig(new ProcessorConfig()) .build(); } + @Test + void constructorShouldLogHelidonApplicationConfigPath() { + // Given + context = context.toBuilder() + .project(javaProject.toBuilder() + .compileClassPathElement(Objects.requireNonNull(getClass().getResource("/custom-port-microprofile-configuration")).getPath()) + .build()) + .build(); + // When + HelidonHealthCheckEnricher helidonHealthCheckEnricher = new HelidonHealthCheckEnricher(context); + // Then + assertThat(helidonHealthCheckEnricher).isNotNull(); + verify(logger, times(1)).debug("jkube-healthcheck-helidon: Helidon Application Config loaded from : %s", getClass().getResource("/custom-port-microprofile-configuration/META-INF/microprofile-config.properties")); + } + @Test void create_withNoMicroprofileDependency_shouldNotAddProbes() { // Given diff --git a/jkube-kit/jkube-kit-helidon/src/test/java/org/eclipse/jkube/helidon/generator/HelidonGeneratorTest.java b/jkube-kit/jkube-kit-helidon/src/test/java/org/eclipse/jkube/helidon/generator/HelidonGeneratorTest.java index d74a85729a..2a438b2e6c 100644 --- a/jkube-kit/jkube-kit-helidon/src/test/java/org/eclipse/jkube/helidon/generator/HelidonGeneratorTest.java +++ b/jkube-kit/jkube-kit-helidon/src/test/java/org/eclipse/jkube/helidon/generator/HelidonGeneratorTest.java @@ -41,10 +41,14 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Objects; import java.util.Properties; import java.util.stream.Stream; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; class HelidonGeneratorTest { @TempDir @@ -54,11 +58,13 @@ class HelidonGeneratorTest { private Properties projectProps; private JavaProject project; private GeneratorContext ctx; + private KitLogger logger; @BeforeEach public void setUp() throws IOException { ProcessorConfig config = new ProcessorConfig(); projectProps = new Properties(); + logger = spy(new KitLogger.SilentLogger()); projectProps.put("jkube.generator.name", "helidon"); targetDir = Files.createDirectory(temporaryFolder.resolve("target")).toFile(); project = JavaProject.builder() @@ -73,13 +79,28 @@ public void setUp() throws IOException { .packaging("jar") .build(); ctx = GeneratorContext.builder() - .logger(new KitLogger.SilentLogger()) + .logger(logger) .project(project) .config(config) .strategy(JKubeBuildStrategy.s2i) .build(); } + @Test + void constructorShouldLogHelidonApplicationConfigPath() { + // Given + ctx = ctx.toBuilder() + .project(project.toBuilder() + .compileClassPathElement(Objects.requireNonNull(getClass().getResource("/custom-port-microprofile-configuration")).getPath()) + .build()) + .build(); + // When + HelidonGenerator helidonGenerator = new HelidonGenerator(ctx); + // Then + assertThat(helidonGenerator).isNotNull(); + verify(logger, times(1)).debug("helidon: Helidon Application Config loaded from : %s", getClass().getResource("/custom-port-microprofile-configuration/META-INF/microprofile-config.properties")); + } + @Test @DisplayName("isApplicable, when valid ImageConfiguration present, then returns false") void isApplicable_whenImageConfigurationPresent_thenReturnFalse() { diff --git a/jkube-kit/jkube-kit-micronaut/src/main/java/org/eclipse/jkube/micronaut/enricher/MicronautHealthCheckEnricher.java b/jkube-kit/jkube-kit-micronaut/src/main/java/org/eclipse/jkube/micronaut/enricher/MicronautHealthCheckEnricher.java index c5c00b30ea..9e6b1e93e9 100644 --- a/jkube-kit/jkube-kit-micronaut/src/main/java/org/eclipse/jkube/micronaut/enricher/MicronautHealthCheckEnricher.java +++ b/jkube-kit/jkube-kit-micronaut/src/main/java/org/eclipse/jkube/micronaut/enricher/MicronautHealthCheckEnricher.java @@ -14,6 +14,7 @@ package org.eclipse.jkube.micronaut.enricher; import java.util.Collections; +import java.util.Properties; import org.eclipse.jkube.kit.common.Configs; import org.eclipse.jkube.kit.config.image.ImageConfiguration; @@ -27,12 +28,13 @@ import lombok.Getter; import static org.eclipse.jkube.kit.common.Configs.asInteger; -import static org.eclipse.jkube.kit.common.util.JKubeProjectUtil.getClassLoader; import static org.eclipse.jkube.micronaut.MicronautUtils.getMicronautConfiguration; import static org.eclipse.jkube.micronaut.MicronautUtils.hasMicronautPlugin; import static org.eclipse.jkube.micronaut.MicronautUtils.isHealthEnabled; public class MicronautHealthCheckEnricher extends AbstractHealthCheckEnricher { + private static final String JKUBE_INTERNAL_APP_CONFIG_FILE_LOCATION = "jkube.internal.application-config-file.path"; + private final Properties micronautApplicationConfiguration; @AllArgsConstructor private enum Config implements Configs.Config { @@ -55,6 +57,8 @@ private enum Config implements Configs.Config { public MicronautHealthCheckEnricher(JKubeEnricherContext buildContext) { super(buildContext, "jkube-healthcheck-micronaut"); + micronautApplicationConfiguration = getMicronautConfiguration(getContext().getProject()); + log.debug("Micronaut Application Config loaded from : %s", micronautApplicationConfiguration.get(JKUBE_INTERNAL_APP_CONFIG_FILE_LOCATION)); } @Override @@ -77,7 +81,7 @@ private boolean isApplicable() { if (!hasMicronautPlugin(getContext().getProject())){ return false; } - return isHealthEnabled(getMicronautConfiguration(getContext().getProject())); + return isHealthEnabled(micronautApplicationConfiguration); } private Probe buildProbe(Integer initialDelaySeconds, Integer periodSeconds){ diff --git a/jkube-kit/jkube-kit-micronaut/src/main/java/org/eclipse/jkube/micronaut/generator/MicronautGenerator.java b/jkube-kit/jkube-kit-micronaut/src/main/java/org/eclipse/jkube/micronaut/generator/MicronautGenerator.java index 00cfd56753..6f4ea530f0 100644 --- a/jkube-kit/jkube-kit-micronaut/src/main/java/org/eclipse/jkube/micronaut/generator/MicronautGenerator.java +++ b/jkube-kit/jkube-kit-micronaut/src/main/java/org/eclipse/jkube/micronaut/generator/MicronautGenerator.java @@ -16,24 +16,29 @@ import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.Properties; import org.eclipse.jkube.generator.api.GeneratorContext; import org.eclipse.jkube.generator.javaexec.JavaExecGenerator; import org.eclipse.jkube.kit.common.Arguments; import org.eclipse.jkube.kit.common.AssemblyConfiguration; import org.eclipse.jkube.kit.config.image.ImageConfiguration; +import org.eclipse.jkube.micronaut.MicronautUtils; import static org.eclipse.jkube.micronaut.MicronautUtils.extractPort; -import static org.eclipse.jkube.micronaut.MicronautUtils.getMicronautConfiguration; import static org.eclipse.jkube.micronaut.MicronautUtils.hasMicronautPlugin; public class MicronautGenerator extends JavaExecGenerator { private final MicronautNestedGenerator nestedGenerator; + private static final String JKUBE_INTERNAL_APP_CONFIG_FILE_LOCATION = "jkube.internal.application-config-file.path"; + private final Properties micronautApplicationConfiguration; public MicronautGenerator(GeneratorContext context) { super(context, "micronaut"); this.nestedGenerator = MicronautNestedGenerator.from(context, getGeneratorConfig()); + micronautApplicationConfiguration = MicronautUtils.getMicronautConfiguration(getContext().getProject()); + log.debug("Micronaut Application Config loaded from : %s", micronautApplicationConfiguration.get(JKUBE_INTERNAL_APP_CONFIG_FILE_LOCATION)); } @Override @@ -80,7 +85,7 @@ protected AssemblyConfiguration createAssembly() { @Override protected String getDefaultWebPort() { return extractPort( - getMicronautConfiguration(getProject()), super.getDefaultWebPort() + micronautApplicationConfiguration, super.getDefaultWebPort() ); } } diff --git a/jkube-kit/jkube-kit-micronaut/src/test/java/org/eclipse/jkube/micronaut/MicronautUtilsGetMicronautConfigurationTest.java b/jkube-kit/jkube-kit-micronaut/src/test/java/org/eclipse/jkube/micronaut/MicronautUtilsGetMicronautConfigurationTest.java index 5690f3e61f..fc4c628d62 100644 --- a/jkube-kit/jkube-kit-micronaut/src/test/java/org/eclipse/jkube/micronaut/MicronautUtilsGetMicronautConfigurationTest.java +++ b/jkube-kit/jkube-kit-micronaut/src/test/java/org/eclipse/jkube/micronaut/MicronautUtilsGetMicronautConfigurationTest.java @@ -52,9 +52,10 @@ void getMicronautConfigurationFromProperties(String directory, String nameSuffix // When final Properties props = getMicronautConfiguration(javaProject); // Then - assertThat(props).containsExactly( + assertThat(props).contains( entry("micronaut.application.name", "port-config-test-" + nameSuffix), - entry("micronaut.server.port", "1337")); + entry("micronaut.server.port", "1337")) + .containsKey("jkube.internal.application-config-file.path"); } } diff --git a/jkube-kit/jkube-kit-micronaut/src/test/java/org/eclipse/jkube/micronaut/MicronautUtilsTest.java b/jkube-kit/jkube-kit-micronaut/src/test/java/org/eclipse/jkube/micronaut/MicronautUtilsTest.java index 89d12abdc7..6b1fcc93e7 100644 --- a/jkube-kit/jkube-kit-micronaut/src/test/java/org/eclipse/jkube/micronaut/MicronautUtilsTest.java +++ b/jkube-kit/jkube-kit-micronaut/src/test/java/org/eclipse/jkube/micronaut/MicronautUtilsTest.java @@ -85,7 +85,8 @@ void getMicronautConfigurationPrecedence() throws IOException { // When final Properties props = getMicronautConfiguration(javaProject); // Then - assertThat(props).containsExactly( + assertThat(props).containsOnly( + entry("jkube.internal.application-config-file.path", MicronautUtilsTest.class.getResource("/utils-test/port-config/properties/application.properties")), entry("micronaut.application.name", "port-config-test-PROPERTIES"), entry("micronaut.server.port", "1337")); } diff --git a/jkube-kit/jkube-kit-micronaut/src/test/java/org/eclipse/jkube/micronaut/enricher/MicronautHealthCheckEnricherTest.java b/jkube-kit/jkube-kit-micronaut/src/test/java/org/eclipse/jkube/micronaut/enricher/MicronautHealthCheckEnricherTest.java index d93d6c660b..4015ce41a9 100644 --- a/jkube-kit/jkube-kit-micronaut/src/test/java/org/eclipse/jkube/micronaut/enricher/MicronautHealthCheckEnricherTest.java +++ b/jkube-kit/jkube-kit-micronaut/src/test/java/org/eclipse/jkube/micronaut/enricher/MicronautHealthCheckEnricherTest.java @@ -15,6 +15,7 @@ import java.io.File; import java.util.Collections; +import java.util.Objects; import org.eclipse.jkube.kit.common.JavaProject; import org.eclipse.jkube.kit.common.KitLogger; @@ -38,12 +39,16 @@ import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; class MicronautHealthCheckEnricherTest { private JKubeEnricherContext context; private JavaProject project; private KubernetesListBuilder klb; + private KitLogger logger; @BeforeEach void setUp() { @@ -51,16 +56,32 @@ void setUp() { .outputDirectory(new File("target")) .build(); klb = new KubernetesListBuilder(); + logger = spy(new KitLogger.SilentLogger()); klb.addToItems(new ServiceBuilder() .withNewMetadata().withName("make-it-real").endMetadata() .build()); context = JKubeEnricherContext.builder() - .log(new KitLogger.SilentLogger()) + .log(logger) .processorConfig(new ProcessorConfig()) .project(project) .build(); } + @Test + void constructorShouldLogMicronautApplicationConfigPath() { + // Given + context = context.toBuilder() + .project(project.toBuilder() + .compileClassPathElement(Objects.requireNonNull(getClass().getResource("/utils-test/port-config/properties")).getPath()) + .build()) + .build(); + // When + MicronautHealthCheckEnricher micronautHealthCheckEnricher = new MicronautHealthCheckEnricher(context); + // Then + assertThat(micronautHealthCheckEnricher).isNotNull(); + verify(logger, times(1)).debug("jkube-healthcheck-micronaut: Micronaut Application Config loaded from : %s", getClass().getResource("/utils-test/port-config/properties/application.properties")); + } + @Test void createWithNoDeployment() { // When diff --git a/jkube-kit/jkube-kit-micronaut/src/test/java/org/eclipse/jkube/micronaut/generator/MicronautGeneratorTest.java b/jkube-kit/jkube-kit-micronaut/src/test/java/org/eclipse/jkube/micronaut/generator/MicronautGeneratorTest.java index 99da146189..ac2b23df58 100644 --- a/jkube-kit/jkube-kit-micronaut/src/test/java/org/eclipse/jkube/micronaut/generator/MicronautGeneratorTest.java +++ b/jkube-kit/jkube-kit-micronaut/src/test/java/org/eclipse/jkube/micronaut/generator/MicronautGeneratorTest.java @@ -14,9 +14,11 @@ package org.eclipse.jkube.micronaut.generator; import java.io.File; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Objects; import java.util.Properties; import org.eclipse.jkube.generator.api.GeneratorContext; @@ -33,26 +35,47 @@ import org.junit.jupiter.params.provider.CsvSource; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; class MicronautGeneratorTest { private GeneratorContext ctx; private MicronautGenerator micronautGenerator; + private KitLogger logger; @BeforeEach - void setUp() { + void setUp(@TempDir Path temporaryFolder) { final Properties projectProperties = new Properties(); + logger = spy(new KitLogger.SilentLogger()); projectProperties.put("jkube.generator.micronaut.mainClass", "com.example.Main"); ctx = GeneratorContext.builder() - .logger(new KitLogger.SilentLogger()) + .logger(logger) .project(JavaProject.builder() .version("1.33.7-SNAPSHOT") .properties(projectProperties) + .outputDirectory(temporaryFolder.resolve("target").toFile()) .build()) .build(); micronautGenerator = new MicronautGenerator(ctx); } + @Test + void constructorShouldLogHelidonApplicationConfigPath() { + // Given + ctx = ctx.toBuilder() + .project(ctx.getProject().toBuilder() + .compileClassPathElement(Objects.requireNonNull(getClass().getResource("/utils-test/port-config/properties")).getPath()) + .build()) + .build(); + // When + MicronautGenerator micronautGenerator = new MicronautGenerator(ctx); + // Then + assertThat(micronautGenerator).isNotNull(); + verify(logger, times(1)).debug("micronaut: Micronaut Application Config loaded from : %s", getClass().getResource("/utils-test/port-config/properties/application.properties")); + } + @Test void isApplicableWithNoPlugin() { // When diff --git a/jkube-kit/jkube-kit-quarkus/src/main/java/org/eclipse/jkube/quarkus/enricher/QuarkusHealthCheckEnricher.java b/jkube-kit/jkube-kit-quarkus/src/main/java/org/eclipse/jkube/quarkus/enricher/QuarkusHealthCheckEnricher.java index ee4bfa5191..df10809c26 100644 --- a/jkube-kit/jkube-kit-quarkus/src/main/java/org/eclipse/jkube/quarkus/enricher/QuarkusHealthCheckEnricher.java +++ b/jkube-kit/jkube-kit-quarkus/src/main/java/org/eclipse/jkube/quarkus/enricher/QuarkusHealthCheckEnricher.java @@ -13,6 +13,7 @@ */ package org.eclipse.jkube.quarkus.enricher; +import java.util.Properties; import java.util.function.Function; import org.eclipse.jkube.kit.common.Configs; @@ -39,9 +40,13 @@ * Enriches Quarkus containers with health checks if the quarkus-smallrye-health is present */ public class QuarkusHealthCheckEnricher extends AbstractHealthCheckEnricher { + private static final String JKUBE_INTERNAL_APP_CONFIG_FILE_LOCATION = "jkube.internal.application-config-file.path"; + private final Properties quarkusApplicationConfiguration; public QuarkusHealthCheckEnricher(JKubeEnricherContext buildContext) { super(buildContext, "jkube-healthcheck-quarkus"); + quarkusApplicationConfiguration = getQuarkusConfiguration(getContext().getProject()); + log.debug("Quarkus Application Config loaded from : %s", quarkusApplicationConfiguration.get(JKUBE_INTERNAL_APP_CONFIG_FILE_LOCATION)); } @AllArgsConstructor @@ -89,7 +94,7 @@ private Probe discoverQuarkusHealthCheck(int initialDelay, Function configs) { @Override protected String getDefaultWebPort() { - return extractPort(getProject(), getQuarkusConfiguration(getProject()), super.getDefaultWebPort()); + return extractPort(getProject(), quarkusApplicationConfiguration, super.getDefaultWebPort()); } @Override diff --git a/jkube-kit/jkube-kit-quarkus/src/test/java/org/eclipse/jkube/quarkus/QuarkusUtilsTest.java b/jkube-kit/jkube-kit-quarkus/src/test/java/org/eclipse/jkube/quarkus/QuarkusUtilsTest.java index c980e9e6b0..a63d97909a 100644 --- a/jkube-kit/jkube-kit-quarkus/src/test/java/org/eclipse/jkube/quarkus/QuarkusUtilsTest.java +++ b/jkube-kit/jkube-kit-quarkus/src/test/java/org/eclipse/jkube/quarkus/QuarkusUtilsTest.java @@ -109,6 +109,7 @@ void getQuarkusConfiguration_propertiesAndYamlProjectProperties_shouldUseProject final Properties props = getQuarkusConfiguration(javaProject); // Then assertThat(props).containsOnly( + entry("jkube.internal.application-config-file.path", QuarkusUtilsTest.class.getResource("/utils-test/config/properties/application.properties")), entry("quarkus.http.port", "42"), entry("%dev.quarkus.http.port", "8082")); } @@ -124,6 +125,7 @@ void getQuarkusConfiguration_propertiesAndYaml_shouldUseProperties() { final Properties props = getQuarkusConfiguration(javaProject); // Then assertThat(props).containsOnly( + entry("jkube.internal.application-config-file.path", QuarkusUtilsTest.class.getResource("/utils-test/config/properties/application.properties")), entry("quarkus.http.port", "1337"), entry("%dev.quarkus.http.port", "8082")); } @@ -138,6 +140,7 @@ void getQuarkusConfiguration_yamlOnly_shouldUseYaml() { final Properties props = getQuarkusConfiguration(javaProject); // Then assertThat(props).containsOnly( + entry("jkube.internal.application-config-file.path", QuarkusUtilsTest.class.getResource("/utils-test/config/yaml/application.yml")), entry("quarkus.http.port", "31337"), entry("%dev.quarkus.http.port", "13373")); } diff --git a/jkube-kit/jkube-kit-quarkus/src/test/java/org/eclipse/jkube/quarkus/enricher/QuarkusHealthCheckEnricherTest.java b/jkube-kit/jkube-kit-quarkus/src/test/java/org/eclipse/jkube/quarkus/enricher/QuarkusHealthCheckEnricherTest.java index 5fc807023b..ce2b0fd4b9 100644 --- a/jkube-kit/jkube-kit-quarkus/src/test/java/org/eclipse/jkube/quarkus/enricher/QuarkusHealthCheckEnricherTest.java +++ b/jkube-kit/jkube-kit-quarkus/src/test/java/org/eclipse/jkube/quarkus/enricher/QuarkusHealthCheckEnricherTest.java @@ -41,6 +41,9 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.tuple; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; class QuarkusHealthCheckEnricherTest { @@ -48,11 +51,13 @@ class QuarkusHealthCheckEnricherTest { private KubernetesListBuilder klb; private JavaProject javaProject; private JKubeEnricherContext context; + private KitLogger logger; @BeforeEach void setUp() { properties = new Properties(); klb = new KubernetesListBuilder(); + logger = spy(new KitLogger.SilentLogger()); // @formatter:off klb.addToItems(new DeploymentBuilder() .editOrNewSpec() @@ -74,12 +79,27 @@ void setUp() { .dependenciesWithTransitive(new ArrayList<>()) .build(); context = JKubeEnricherContext.builder() - .log(new KitLogger.SilentLogger()) + .log(logger) .project(javaProject) .processorConfig(new ProcessorConfig()) .build(); } + @Test + void constructorShouldLogQuarkusApplicationConfigPath() { + // Given + context = context.toBuilder() + .project(javaProject.toBuilder() + .compileClassPathElement(Objects.requireNonNull(getClass().getResource("/utils-test/config/properties")).getPath()) + .build()) + .build(); + // When + QuarkusHealthCheckEnricher quarkusHealthCheckEnricher = new QuarkusHealthCheckEnricher(context); + // Then + assertThat(quarkusHealthCheckEnricher).isNotNull(); + verify(logger, times(1)).debug("jkube-healthcheck-quarkus: Quarkus Application Config loaded from : %s", getClass().getResource("/utils-test/config/properties/application.properties")); + } + @Test void create_withCustomPath_shouldReturnCustomPath() { // Given diff --git a/jkube-kit/jkube-kit-quarkus/src/test/java/org/eclipse/jkube/quarkus/generator/QuarkusGeneratorTest.java b/jkube-kit/jkube-kit-quarkus/src/test/java/org/eclipse/jkube/quarkus/generator/QuarkusGeneratorTest.java index aad82f905e..623e33943d 100644 --- a/jkube-kit/jkube-kit-quarkus/src/test/java/org/eclipse/jkube/quarkus/generator/QuarkusGeneratorTest.java +++ b/jkube-kit/jkube-kit-quarkus/src/test/java/org/eclipse/jkube/quarkus/generator/QuarkusGeneratorTest.java @@ -21,6 +21,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.Objects; import java.util.Properties; import java.util.stream.Stream; @@ -52,6 +53,9 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatIllegalStateException; import static org.mockito.Mockito.mockConstruction; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; /** @@ -70,6 +74,7 @@ class QuarkusGeneratorTest { private Properties projectProps; private JavaProject project; private GeneratorContext ctx; + private KitLogger logger; @BeforeEach void setUp() throws IOException { @@ -77,6 +82,7 @@ void setUp() throws IOException { projectProps = new Properties(); projectProps.put("jkube.generator.name", "quarkus"); targetDir = Files.createDirectory(temporaryFolder.resolve("target")).toFile(); + logger = spy(new KitLogger.SilentLogger()); project = JavaProject.builder() .version("0.0.1-SNAPSHOT") .baseDirectory(targetDir) @@ -86,13 +92,28 @@ void setUp() throws IOException { .outputDirectory(targetDir) .build(); ctx = GeneratorContext.builder() - .logger(new KitLogger.SilentLogger()) + .logger(logger) .project(project) .config(config) .strategy(JKubeBuildStrategy.s2i) .build(); } + @Test + void constructorShouldLogQuarkusApplicationConfigPath() { + // Given + ctx = ctx.toBuilder() + .project(project.toBuilder() + .compileClassPathElement(Objects.requireNonNull(getClass().getResource("/generator-extract-ports")).getPath()) + .build()) + .build(); + // When + QuarkusGenerator quarkusGenerator = new QuarkusGenerator(ctx); + // Then + assertThat(quarkusGenerator).isNotNull(); + verify(logger, times(1)).debug("quarkus: Quarkus Application Config loaded from : %s", getClass().getResource("/generator-extract-ports/application.properties")); + } + @Nested @TestInstance(TestInstance.Lifecycle.PER_CLASS) @DisplayName("isApplicable") diff --git a/jkube-kit/jkube-kit-spring-boot/src/main/java/org/eclipse/jkube/springboot/enricher/SpringBootHealthCheckEnricher.java b/jkube-kit/jkube-kit-spring-boot/src/main/java/org/eclipse/jkube/springboot/enricher/SpringBootHealthCheckEnricher.java index 09f0f81dd3..daa2d437f8 100644 --- a/jkube-kit/jkube-kit-spring-boot/src/main/java/org/eclipse/jkube/springboot/enricher/SpringBootHealthCheckEnricher.java +++ b/jkube-kit/jkube-kit-spring-boot/src/main/java/org/eclipse/jkube/springboot/enricher/SpringBootHealthCheckEnricher.java @@ -19,17 +19,22 @@ import lombok.AllArgsConstructor; import lombok.Getter; import org.eclipse.jkube.kit.common.Configs; +import org.eclipse.jkube.kit.common.util.JKubeProjectUtil; import org.eclipse.jkube.kit.common.util.SpringBootConfiguration; +import org.eclipse.jkube.kit.common.util.SpringBootUtil; import org.eclipse.jkube.kit.enricher.api.JKubeEnricherContext; import org.eclipse.jkube.kit.enricher.specific.AbstractHealthCheckEnricher; import org.apache.commons.lang3.StringUtils; +import java.util.Properties; + import static org.eclipse.jkube.kit.common.util.SpringBootUtil.hasSpringWebFluxDependency; /** * Enriches spring-boot containers with health checks if the actuator module is present. */ public class SpringBootHealthCheckEnricher extends AbstractHealthCheckEnricher { + private static final String JKUBE_INTERNAL_APP_CONFIG_FILE_LOCATION = "jkube.internal.application-config-file.path"; public static final String ENRICHER_NAME = "jkube-healthcheck-spring-boot"; @@ -63,6 +68,10 @@ private enum Config implements Configs.Config { public SpringBootHealthCheckEnricher(JKubeEnricherContext buildContext) { super(buildContext, ENRICHER_NAME); + Properties springBootApplicationConfig = SpringBootUtil.getSpringBootApplicationProperties( + SpringBootUtil.getSpringBootActiveProfile(getContext().getProject()), + JKubeProjectUtil.getClassLoader(getContext().getProject())); + log.debug("Spring Boot Application Config loaded from : %s", springBootApplicationConfig.get(JKUBE_INTERNAL_APP_CONFIG_FILE_LOCATION)); } @Override diff --git a/jkube-kit/jkube-kit-spring-boot/src/main/java/org/eclipse/jkube/springboot/generator/SpringBootGenerator.java b/jkube-kit/jkube-kit-spring-boot/src/main/java/org/eclipse/jkube/springboot/generator/SpringBootGenerator.java index 41d01bee0f..3e50d37cca 100644 --- a/jkube-kit/jkube-kit-spring-boot/src/main/java/org/eclipse/jkube/springboot/generator/SpringBootGenerator.java +++ b/jkube-kit/jkube-kit-spring-boot/src/main/java/org/eclipse/jkube/springboot/generator/SpringBootGenerator.java @@ -16,6 +16,7 @@ import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.Properties; import org.eclipse.jkube.generator.api.GeneratorContext; import org.eclipse.jkube.generator.api.GeneratorMode; @@ -33,6 +34,7 @@ import org.apache.commons.lang3.StringUtils; import static org.eclipse.jkube.kit.common.util.SpringBootUtil.DEV_TOOLS_REMOTE_SECRET; +import static org.eclipse.jkube.kit.common.util.SpringBootUtil.getSpringBootApplicationProperties; import static org.eclipse.jkube.kit.common.util.SpringBootUtil.isSpringBootRepackage; import static org.eclipse.jkube.springboot.SpringBootDevtoolsUtils.addDevToolsFilesToFatJar; import static org.eclipse.jkube.springboot.SpringBootDevtoolsUtils.ensureSpringDevToolSecretToken; @@ -42,6 +44,7 @@ * @author roland */ public class SpringBootGenerator extends JavaExecGenerator { + private static final String JKUBE_INTERNAL_APP_CONFIG_FILE_LOCATION = "jkube.internal.application-config-file.path"; @AllArgsConstructor public enum Config implements Configs.Config { @@ -58,6 +61,10 @@ public enum Config implements Configs.Config { public SpringBootGenerator(GeneratorContext context) { super(context, "spring-boot"); nestedGenerator = SpringBootNestedGenerator.from(context, getGeneratorConfig(), detectFatJar()); + Properties springBootApplicationConfig = SpringBootUtil.getSpringBootApplicationProperties( + SpringBootUtil.getSpringBootActiveProfile(getContext().getProject()), + JKubeProjectUtil.getClassLoader(getContext().getProject())); + log.debug("Spring Boot Application Config loaded from : %s", springBootApplicationConfig.get(JKUBE_INTERNAL_APP_CONFIG_FILE_LOCATION)); } @Override diff --git a/jkube-kit/jkube-kit-spring-boot/src/test/java/org/eclipse/jkube/springboot/enricher/AbstractSpringBootHealthCheckEnricherTestSupport.java b/jkube-kit/jkube-kit-spring-boot/src/test/java/org/eclipse/jkube/springboot/enricher/AbstractSpringBootHealthCheckEnricherTestSupport.java index 612c74d037..5bf0f7840d 100644 --- a/jkube-kit/jkube-kit-spring-boot/src/test/java/org/eclipse/jkube/springboot/enricher/AbstractSpringBootHealthCheckEnricherTestSupport.java +++ b/jkube-kit/jkube-kit-spring-boot/src/test/java/org/eclipse/jkube/springboot/enricher/AbstractSpringBootHealthCheckEnricherTestSupport.java @@ -20,6 +20,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.Collections; +import java.util.Objects; import java.util.Properties; import java.util.TreeMap; @@ -43,6 +44,8 @@ import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; /** @@ -52,6 +55,7 @@ */ public abstract class AbstractSpringBootHealthCheckEnricherTestSupport { private Properties props; + private KitLogger logger; protected JKubeEnricherContext context; @@ -60,9 +64,10 @@ public abstract class AbstractSpringBootHealthCheckEnricherTestSupport { @BeforeEach void init(@TempDir Path project) throws IOException { props = new Properties(); + logger = spy(new KitLogger.SilentLogger()); projectClassLoaders = mock(ProjectClassLoaders.class, RETURNS_DEEP_STUBS); context = spy(JKubeEnricherContext.builder() - .log(new KitLogger.SilentLogger()) + .log(logger) .project(JavaProject.builder() .properties(props) .baseDirectory(project.toFile()) @@ -85,6 +90,21 @@ private boolean isSpringBootOne() { return getSpringBootVersion().substring(0, getSpringBootVersion().indexOf('.')).equals("1"); } + @Test + void constructorShouldLogSpringBootApplicationConfigPath() { + // Given + context = context.toBuilder() + .project(context.getProject().toBuilder() + .compileClassPathElement(Objects.requireNonNull(getClass().getResource("/port-override-application-properties")).getPath()) + .build()) + .build(); + // When + SpringBootHealthCheckEnricher springBootHealthCheckEnricher = new SpringBootHealthCheckEnricher(context); + // Then + assertThat(springBootHealthCheckEnricher).isNotNull(); + verify(logger, times(1)).debug("jkube-healthcheck-spring-boot: Spring Boot Application Config loaded from : %s", Objects.requireNonNull(getClass().getResource("/port-override-application-properties/application.properties")).toString()); + } + @Test void zeroConfig() { SpringBootHealthCheckEnricher enricher = new SpringBootHealthCheckEnricher(context); diff --git a/jkube-kit/jkube-kit-spring-boot/src/test/java/org/eclipse/jkube/springboot/generator/SpringBootGeneratorTest.java b/jkube-kit/jkube-kit-spring-boot/src/test/java/org/eclipse/jkube/springboot/generator/SpringBootGeneratorTest.java index 89d6a20a4c..709116867b 100644 --- a/jkube-kit/jkube-kit-spring-boot/src/test/java/org/eclipse/jkube/springboot/generator/SpringBootGeneratorTest.java +++ b/jkube-kit/jkube-kit-spring-boot/src/test/java/org/eclipse/jkube/springboot/generator/SpringBootGeneratorTest.java @@ -40,15 +40,20 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatIllegalStateException; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; class SpringBootGeneratorTest { private GeneratorContext context; + private KitLogger logger; @BeforeEach void setUp(@TempDir Path temporaryFolder) throws IOException { + logger = spy(new KitLogger.SilentLogger()); context = GeneratorContext.builder() - .logger(new KitLogger.SilentLogger()) + .logger(logger) .project(JavaProject.builder() .outputDirectory(Files.createDirectory(temporaryFolder.resolve("target")).toFile()) .version("1.0.0") @@ -56,6 +61,21 @@ void setUp(@TempDir Path temporaryFolder) throws IOException { .build(); } + @Test + void constructorShouldLogSpringBootApplicationConfigPath() { + // Given + context = context.toBuilder() + .project(context.getProject().toBuilder() + .compileClassPathElement(Objects.requireNonNull(getClass().getResource("/port-override-application-properties")).getPath()) + .build()) + .build(); + // When + SpringBootGenerator springBootGenerator = new SpringBootGenerator(context); + // Then + assertThat(springBootGenerator).isNotNull(); + verify(logger, times(1)).debug("spring-boot: Spring Boot Application Config loaded from : %s", Objects.requireNonNull(getClass().getResource("/port-override-application-properties/application.properties")).toString()); + } + @Test @DisplayName("isApplicable, when valid ImageConfiguration present, then returns false") void isApplicable_whenImageConfigurationPresent_thenReturnFalse() { @@ -224,7 +244,7 @@ void getExtraJavaOptions_whenColorEnabled_thenAddColorOption() { private void withPlugin(Plugin plugin) { context = context.toBuilder() - .project(JavaProject.builder().plugin(plugin).build()) + .project(context.getProject().toBuilder().plugin(plugin).build()) .build(); } } diff --git a/jkube-kit/jkube-kit-thorntail-v2/src/main/java/org/eclipse/jkube/thorntail/v2/enricher/ThorntailV2HealthCheckEnricher.java b/jkube-kit/jkube-kit-thorntail-v2/src/main/java/org/eclipse/jkube/thorntail/v2/enricher/ThorntailV2HealthCheckEnricher.java index 341ba313ed..ea3b118975 100644 --- a/jkube-kit/jkube-kit-thorntail-v2/src/main/java/org/eclipse/jkube/thorntail/v2/enricher/ThorntailV2HealthCheckEnricher.java +++ b/jkube-kit/jkube-kit-thorntail-v2/src/main/java/org/eclipse/jkube/thorntail/v2/enricher/ThorntailV2HealthCheckEnricher.java @@ -30,9 +30,13 @@ public class ThorntailV2HealthCheckEnricher extends AbstractHealthCheckEnricher { private static final String IO_THORNTAIL = "io.thorntail"; + private static final String JKUBE_INTERNAL_APP_CONFIG_FILE_LOCATION = "jkube.internal.application-config-file.path"; + private final Properties thorntailApplicationConfiguration; public ThorntailV2HealthCheckEnricher(JKubeEnricherContext buildContext) { super(buildContext, "jkube-healthcheck-thorntail-v2"); + thorntailApplicationConfiguration = ThorntailUtil.getThorntailProperties(getContext().getProject()); + log.debug("Thorntail Application Config loaded from : %s", thorntailApplicationConfiguration.get(JKUBE_INTERNAL_APP_CONFIG_FILE_LOCATION)); } @AllArgsConstructor @@ -91,10 +95,9 @@ protected String getScheme() { } protected int getPort() { - final Properties properties = ThorntailUtil.getThorntailProperties(getContext().getProject()); - properties.putAll(System.getProperties()); - if (properties.containsKey("thorntail.http.port")) { - return Integer.parseInt((String) properties.get("thorntail.http.port")); + thorntailApplicationConfiguration.putAll(System.getProperties()); + if (thorntailApplicationConfiguration.containsKey("thorntail.http.port")) { + return Integer.parseInt((String) thorntailApplicationConfiguration.get("thorntail.http.port")); } return Configs.asInt(getConfig(Config.PORT)); diff --git a/jkube-kit/jkube-kit-thorntail-v2/src/main/java/org/eclipse/jkube/thorntail/v2/generator/ThorntailV2Generator.java b/jkube-kit/jkube-kit-thorntail-v2/src/main/java/org/eclipse/jkube/thorntail/v2/generator/ThorntailV2Generator.java index 760b98fbca..a42cb4c006 100644 --- a/jkube-kit/jkube-kit-thorntail-v2/src/main/java/org/eclipse/jkube/thorntail/v2/generator/ThorntailV2Generator.java +++ b/jkube-kit/jkube-kit-thorntail-v2/src/main/java/org/eclipse/jkube/thorntail/v2/generator/ThorntailV2Generator.java @@ -15,16 +15,22 @@ import org.eclipse.jkube.generator.api.GeneratorContext; import org.eclipse.jkube.generator.javaexec.JavaExecGenerator; +import org.eclipse.jkube.kit.common.util.ThorntailUtil; import org.eclipse.jkube.kit.config.image.ImageConfiguration; import org.eclipse.jkube.kit.common.util.JKubeProjectUtil; import java.util.List; import java.util.Map; +import java.util.Properties; public class ThorntailV2Generator extends JavaExecGenerator { + private final Properties thorntailApplicationConfiguration; + private static final String JKUBE_INTERNAL_APP_CONFIG_FILE_LOCATION = "jkube.internal.application-config-file.path"; public ThorntailV2Generator(GeneratorContext context) { super(context, "thorntail-v2", JDK.JDK_11); + thorntailApplicationConfiguration = ThorntailUtil.getThorntailProperties(getContext().getProject()); + log.debug("Thorntail Application Config loaded from %s", thorntailApplicationConfiguration.get(JKUBE_INTERNAL_APP_CONFIG_FILE_LOCATION)); } @Override diff --git a/jkube-kit/jkube-kit-thorntail-v2/src/test/java/org/eclipse/jkube/thorntail/v2/enricher/ThorntailV2HealthCheckEnricherTest.java b/jkube-kit/jkube-kit-thorntail-v2/src/test/java/org/eclipse/jkube/thorntail/v2/enricher/ThorntailV2HealthCheckEnricherTest.java index 8bb7ae7d09..74346e250a 100644 --- a/jkube-kit/jkube-kit-thorntail-v2/src/test/java/org/eclipse/jkube/thorntail/v2/enricher/ThorntailV2HealthCheckEnricherTest.java +++ b/jkube-kit/jkube-kit-thorntail-v2/src/test/java/org/eclipse/jkube/thorntail/v2/enricher/ThorntailV2HealthCheckEnricherTest.java @@ -17,6 +17,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.Collections; +import java.util.Objects; import java.util.Properties; import org.eclipse.jkube.kit.common.Dependency; @@ -37,11 +38,15 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.tuple; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; class ThorntailV2HealthCheckEnricherTest { private JKubeEnricherContext context; private Properties properties; private KubernetesListBuilder klb; + private KitLogger logger; @TempDir private Path temporaryFolder; @@ -51,6 +56,7 @@ void setUp() throws IOException { properties = new Properties(); ProcessorConfig processorConfig = new ProcessorConfig(); klb = new KubernetesListBuilder(); + logger = spy(new KitLogger.SilentLogger()); // @formatter:off klb.addToItems(new DeploymentBuilder() .editOrNewSpec() @@ -77,10 +83,25 @@ void setUp() throws IOException { .outputDirectory(Files.createDirectory(temporaryFolder.resolve("target")).toFile()) .build()) .processorConfig(processorConfig) - .log(new KitLogger.SilentLogger()) + .log(logger) .build(); } + @Test + void constructorShouldLogThorntailApplicationConfigPath() { + // Given + context = context.toBuilder() + .project(context.getProject().toBuilder() + .compileClassPathElement(Objects.requireNonNull(getClass().getResource("/application-config/properties")).getPath()) + .build()) + .build(); + // When + ThorntailV2HealthCheckEnricher thorntailV2HealthCheckEnricher = new ThorntailV2HealthCheckEnricher(context); + // Then + assertThat(thorntailV2HealthCheckEnricher).isNotNull(); + verify(logger, times(1)).debug("jkube-healthcheck-thorntail-v2: Thorntail Application Config loaded from : %s", getClass().getResource("/application-config/properties/project-defaults.yml")); + } + @Test void createWithDefaultsInKubernetes() { // When diff --git a/jkube-kit/jkube-kit-thorntail-v2/src/test/java/org/eclipse/jkube/thorntail/v2/generator/ThorntailV2GeneratorIsApplicableTest.java b/jkube-kit/jkube-kit-thorntail-v2/src/test/java/org/eclipse/jkube/thorntail/v2/generator/ThorntailV2GeneratorIsApplicableTest.java index 227b62be75..811466ebe3 100644 --- a/jkube-kit/jkube-kit-thorntail-v2/src/test/java/org/eclipse/jkube/thorntail/v2/generator/ThorntailV2GeneratorIsApplicableTest.java +++ b/jkube-kit/jkube-kit-thorntail-v2/src/test/java/org/eclipse/jkube/thorntail/v2/generator/ThorntailV2GeneratorIsApplicableTest.java @@ -17,20 +17,22 @@ import org.eclipse.jkube.generator.api.GeneratorContext; import org.eclipse.jkube.kit.common.Dependency; import org.eclipse.jkube.kit.common.JavaProject; +import org.eclipse.jkube.kit.common.KitLogger; import org.eclipse.jkube.kit.common.Plugin; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.io.TempDir; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; -import org.mockito.Mockito; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.Collections; import java.util.List; import java.util.stream.Stream; import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; class ThorntailV2GeneratorIsApplicableTest { @@ -38,10 +40,13 @@ class ThorntailV2GeneratorIsApplicableTest { private GeneratorContext context; @BeforeEach - void setUp() { - project = mock(JavaProject.class, Mockito.RETURNS_DEEP_STUBS); - context = mock(GeneratorContext.class, Mockito.RETURNS_DEEP_STUBS); - when(context.getProject()).thenReturn(project); + void setUp(@TempDir Path temporaryFolder) throws IOException { + context = GeneratorContext.builder() + .project(JavaProject.builder() + .outputDirectory(Files.createDirectory(temporaryFolder.resolve("target")).toFile()) + .build()) + .logger(new KitLogger.SilentLogger()) + .build(); } public static Stream data() { @@ -65,9 +70,11 @@ public static Stream data() { void isApplicable(String testDescription, List pluginList, List dependencyList, List gradlePluginList, boolean expectedValue) { // Given - when(project.getPlugins()).thenReturn(pluginList); - when(project.getDependencies()).thenReturn(dependencyList); - when(project.getGradlePlugins()).thenReturn(gradlePluginList); + context = context.toBuilder().project(context.getProject().toBuilder() + .plugins(pluginList) + .dependencies(dependencyList) + .gradlePlugins(gradlePluginList) + .build()).build(); // When final boolean result = new ThorntailV2Generator(context).isApplicable(Collections.emptyList()); // Then diff --git a/jkube-kit/jkube-kit-thorntail-v2/src/test/resources/application-config/properties/project-defaults.yml b/jkube-kit/jkube-kit-thorntail-v2/src/test/resources/application-config/properties/project-defaults.yml new file mode 100644 index 0000000000..6ec8b83530 --- /dev/null +++ b/jkube-kit/jkube-kit-thorntail-v2/src/test/resources/application-config/properties/project-defaults.yml @@ -0,0 +1,3 @@ +thorntail: + http: + port: 8081 \ No newline at end of file