Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Commit

Permalink
Refactor:
Browse files Browse the repository at this point in the history
* added spring boot dependency to parse the spring boot properties
* using spring boot run to make it compute env and retrieved props from it
  • Loading branch information
kameshsampath committed Jun 11, 2017
1 parent 8f0c4c2 commit f510110
Show file tree
Hide file tree
Showing 10 changed files with 434 additions and 220 deletions.
7 changes: 7 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@
<artifactId>mockwebserver</artifactId>
</dependency>

<!-- Spring Boot Profiles handling -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>${version.spring-boot}</version>
</dependency>

</dependencies>

<build>
Expand Down
363 changes: 204 additions & 159 deletions core/src/main/java/io/fabric8/maven/core/util/SpringBootUtil.java

Large diffs are not rendered by default.

216 changes: 173 additions & 43 deletions core/src/test/java/io/fabric8/maven/core/util/SpringBootUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,50 @@
*/
package io.fabric8.maven.core.util;

import java.util.*;

import io.fabric8.utils.PropertiesHelper;

import org.apache.maven.model.Build;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.FileUtils;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.util.Collections;
import java.util.Properties;
import java.util.UUID;

import static org.junit.Assert.*;

/**
* Checking the behaviour of utility methods.
*/
public class SpringBootUtilTest {


@Test
public void testYamlToPropertiesParsing() {
public void testYamlToPropertiesParsing() throws Exception {

MavenProject project = new MavenProject();
Build build = new Build();

setMavenProject(project, build);

URL testAppPropertyResource = SpringBootUtilTest.class.getResource("/util/test-application.yml");

FileUtils.copyFile(ResourceUtils.getFile(testAppPropertyResource), new File("target/test-classes",
"application.yml"));

Properties props = SpringBootUtil.getApplicationProperties(project, Collections.<String>emptyList());

Properties props = SpringBootUtil.getPropertiesFromYamlResource(
SpringBootUtilTest.class.getResource("/util/test-application.yml"), Collections.<String>emptyList());
assertNotEquals(0, props.size());

assertEquals(new Integer(8081), PropertiesHelper.getInteger(props, "management.port"));
Expand All @@ -48,55 +71,154 @@ public void testYamlToPropertiesParsing() {
}

@Test
public void testYamlToPropertiesParsingWithActiveProfiles() {
public void testYamlToPropertiesMerge() throws Exception {

MavenProject project = new MavenProject();
Build build = new Build();

setMavenProject(project, build);

URL testAppPropertyResource = SpringBootUtilTest.class.getResource("/util/test-application-merge-multi.yml");

List<String> activeProfiles = new ArrayList<String>() {{
add("dev");
add("qa");
}};
FileUtils.copyFile(ResourceUtils.getFile(testAppPropertyResource), new File("target/test-classes",
"application.yml"), "UTF-8", null, true);

Properties props = SpringBootUtil.getApplicationProperties(project, Collections.<String>emptyList());

Properties props = SpringBootUtil.getPropertiesFromYamlResource(
SpringBootUtilTest.class.getResource("/util/test-application-multi.yml"), activeProfiles);
assertNotEquals(0, props.size());

assertEquals(new Integer(9090), PropertiesHelper.getInteger(props, "server.port"));
assertEquals("Hello", props.getProperty("my.name"));
assertEquals("Hola!", props.getProperty("their.name"));
assertEquals("Foo", props.getProperty("their.name"));
}

@Test
public void testYamlToPropertiesParsingWithActiveProfiles2() {
public void testWithDifferentConfigName() throws Exception {

System.setProperty("spring.config.name", "foo");

MavenProject project = new MavenProject();
Build build = new Build();

List<String> activeProfiles = new ArrayList<String>() {{
add("qa");
add("dev");
}};
setMavenProject(project, build);

URL testAppPropertyResource = SpringBootUtilTest.class.getResource("/util/test-application-named.yml");

FileUtils.copyFile(ResourceUtils.getFile(testAppPropertyResource), new File("target/test-classes",
"foo.yml"), "UTF-8", null, true);

Properties props = SpringBootUtil.getApplicationProperties(project, Collections.<String>emptyList());

Properties props = SpringBootUtil.getPropertiesFromYamlResource(
SpringBootUtilTest.class.getResource("/util/test-application-multi.yml"), activeProfiles);
assertNotEquals(0, props.size());

assertEquals(new Integer(9090), PropertiesHelper.getInteger(props, "server.port"));
assertEquals("Foo", props.getProperty("their.name"));

System.getProperties().remove("spring.config.name");
}

@Test
public void testPropertiesInclude() throws Exception {

MavenProject project = new MavenProject();
Build build = new Build();

setMavenProject(project, build);

URL testAppPropertyResource = SpringBootUtilTest.class.getResource("/util/test-application-include.yml");

FileUtils.copyFile(ResourceUtils.getFile(testAppPropertyResource), new File("target/test-classes",
"application.yml"), "UTF-8", null, true);

Properties props = SpringBootUtil.getApplicationProperties(project,Collections.<String>emptyList());

assertNotEquals(0, props.size());

assertEquals(new Integer(2020), PropertiesHelper.getInteger(props, "my.port"));
assertEquals("bar", props.getProperty("my.name"));
assertEquals("foo", props.getProperty("name"));
}


@Test
public void testProfilePropertiesForDev() throws Exception {

MavenProject project = new MavenProject();
Build build = new Build();

setMavenProject(project, build);

URL testAppPropertyResource = SpringBootUtilTest.class.getResource("/util/test-application-multi.yml");

FileUtils.copyFile(ResourceUtils.getFile(testAppPropertyResource), new File("target/test-classes",
"application.yml"), "UTF-8", null, true);

Properties props = SpringBootUtil.getApplicationProperties(project,"dev");

assertEquals(new Integer(8080), PropertiesHelper.getInteger(props, "server.port"));
assertEquals("Hello", props.getProperty("my.name"));
assertEquals("Hola!", props.getProperty("their.name"));
}

@Test
public void testNonExistentYamlToPropertiesParsing() {
public void testProfilePropertiesForQa() throws Exception {

MavenProject project = new MavenProject();
Build build = new Build();

setMavenProject(project, build);

URL testAppPropertyResource = SpringBootUtilTest.class.getResource("/util/test-application-multi.yml");

Properties props = SpringBootUtil.getPropertiesFromYamlResource(
SpringBootUtilTest.class.getResource("/this-file-does-not-exist")
, Collections.<String>emptyList());
assertNotNull(props);
assertEquals(0, props.size());
FileUtils.copyFile(ResourceUtils.getFile(testAppPropertyResource), new File("target/test-classes",
"application.yml"), "UTF-8", null, true);

Properties props = SpringBootUtil.getApplicationProperties(project,"qa");

assertNotEquals(0, props.size());

assertEquals(new Integer(9090), PropertiesHelper.getInteger(props, "server.port"));
assertEquals("Hola!", props.getProperty("their.name"));
}

// // @Test TODO SK Remove this after Roland Review - this will not happen at all
// public void testNonExistentYamlToPropertiesParsing() throws Exception {
//
// Properties props = SpringBootUtil.getPropertiesFromYamlResource(
// SpringBootUtilTest.class.getResource("/this-file-does-not-exist")
// , null);
//
// MavenProject project = new MavenProject();
// Build build = new Build();
//
// setMavenProject(project, build);
//
// URL testAppPropertyResource = SpringBootUtilTest.class.getResource("/this-file-does-not-exist");
//
// FileUtils.copyFile(ResourceUtils.getFile(testAppPropertyResource), new File("target/test-classes",
// "application.yml"), "UTF-8", null, true);
//
// Properties props = SpringBootUtil.getApplicationProperties(project,"qa");
// assertNotNull(props);
// assertEquals(0, props.size());
//
// }

@Test
public void testPropertiesParsing() {
public void testPropertiesParsing() throws Exception {

MavenProject project = new MavenProject();
Build build = new Build();

setMavenProject(project, build);

URL testAppPropertyResource = SpringBootUtilTest.class.getResource("/util/test-application.properties");

FileUtils.copyFile(ResourceUtils.getFile(testAppPropertyResource), new File("target/test-classes",
"application.properties"), "UTF-8", null, true);

Properties props = SpringBootUtil.getApplicationProperties(project,Collections.<String>emptyList());


Properties props = SpringBootUtil.getPropertiesResource(
SpringBootUtilTest.class.getResource("/util/test-application.properties"));
assertNotEquals(0, props.size());

assertEquals(new Integer(8081), PropertiesHelper.getInteger(props, "management.port"));
Expand All @@ -106,13 +228,21 @@ public void testPropertiesParsing() {

}

@Test
public void testNonExistentPropertiesParsing() {

Properties props = SpringBootUtil.getPropertiesResource(SpringBootUtilTest.class.getResource("/this-file-does-not-exist"));
assertNotNull(props);
assertEquals(0, props.size());

// @Test TODO SK Remove this after Roland Review
// public void testNonExistentPropertiesParsing() throws IOException {
//
// Properties props = SpringBootUtil.getPropertiesResource(SpringBootUtilTest.class.getResource(
// "/this-file-does-not-exist"), null);
// assertNotNull(props);
// assertEquals(0, props.size());
// }

public void setMavenProject(final MavenProject project, final Build build) throws IOException {
//Set Build Dir
final String outputTempDir = Files.createTempDirectory(UUID.randomUUID().toString()).toFile().getAbsolutePath();
new File(outputTempDir).mkdirs();
build.setOutputDirectory(outputTempDir);
project.setBuild(build);
}

}
14 changes: 14 additions & 0 deletions core/src/test/resources/util/test-application-include.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
spring:
active:
profiles: foo
profiles:
include: bar
name: foo

---
my:
name: bar
port: 2020
spring:
profiles: bar
9 changes: 9 additions & 0 deletions core/src/test/resources/util/test-application-merge-multi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
my:
name: "Hello"

---
server:
port: 9090
their:
name: Foo
4 changes: 0 additions & 4 deletions core/src/test/resources/util/test-application-multi.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
spring:
profiles:
active: dev,qa

---

spring:
Expand Down
4 changes: 4 additions & 0 deletions core/src/test/resources/util/test-application-named.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
server:
port: 9090
their:
name: Foo
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ public class SpringBootGenerator extends JavaExecGenerator {
private static final String DEFAULT_SERVER_PORT = "8080";

public enum Config implements Configs.Key {
color {{ d = "false"; }},
color {{
d = "false";
}},

// comma separated list of spring boot profile(s) that would be passed set as -Dspring.profiles.active
activeProfiles;
Expand Down Expand Up @@ -89,8 +91,7 @@ public List<ImageConfiguration> customize(List<ImageConfiguration> configs, bool
protected Map<String, String> getEnv(boolean prePackagePhase) throws MojoExecutionException {
Map<String, String> res = super.getEnv(prePackagePhase);
if (getContext().isWatchMode()) {
String strActiveProfiles = getContext().getConfig().getConfig("spring-boot", "activeProfiles");
Properties properties = SpringBootUtil.getApplicationProperties(getProject(), strActiveProfiles);
Properties properties = getSpringBootProperties();
// adding dev tools token to env variables to prevent override during recompile
String secret = properties.getProperty(SpringBootProperties.DEV_TOOLS_REMOTE_SECRET);
if (secret != null) {
Expand Down Expand Up @@ -127,12 +128,8 @@ protected boolean isFatJar() throws MojoExecutionException {
protected List<String> extractPorts() {
List<String> answer = new ArrayList<>();

String strActiveProfiles = getConfig(activeProfiles);

Properties properties = SpringBootUtil.getApplicationProperties(getContext().getProject(),
SpringBootUtil.getActiveProfiles(strActiveProfiles));

//TODO SK - do we need to handle the parsin of port properties like ${PORT:1234}
Properties properties = getSpringBootProperties();
//TODO SK - do we need to handle the parsing of port properties like ${PORT:1234}
String port = properties.getProperty(SpringBootProperties.SERVER_PORT, DEFAULT_SERVER_PORT);

addPortIfValid(answer, getConfig(JavaExecGenerator.Config.webPort, port));
Expand All @@ -144,10 +141,7 @@ protected List<String> extractPorts() {
// =============================================================================

private void ensureSpringDevToolSecretToken() throws MojoExecutionException {
String strActiveProfiles = getConfig(activeProfiles);

Properties properties = SpringBootUtil.getApplicationProperties(getContext().getProject(),
SpringBootUtil.getActiveProfiles(strActiveProfiles));
Properties properties = getSpringBootProperties();
String remoteSecret = properties.getProperty(DEV_TOOLS_REMOTE_SECRET);
if (Strings.isNullOrEmpty(remoteSecret)) {
addSecretTokenToApplicationProperties();
Expand Down Expand Up @@ -295,6 +289,15 @@ private boolean isSpringBootRepackage() {
return false;
}

private Properties getSpringBootProperties() {
try {
String strActiveProfiles = getContext().getConfig().getConfig("spring-boot", "activeProfiles");
return SpringBootUtil.getApplicationProperties(getProject(), strActiveProfiles);
} catch (IOException e) {
throw new IllegalStateException("Failed to load Spring Boot properties", e);
}
}

private File getSpringBootDevToolsJar() throws IOException {
String version = SpringBootUtil.getSpringBootDevToolsVersion(getProject());
if (version == null) {
Expand Down
1 change: 1 addition & 0 deletions parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
<version.jacoco>0.7.8</version.jacoco>
<version.fabric8>2.2.215</version.fabric8>
<version.kubernetes-client>2.3.1</version.kubernetes-client>
<version.spring-boot>1.5.3.RELEASE</version.spring-boot>
<version.mockwebserver>0.0.13</version.mockwebserver>
<version.docker-maven-plugin>0.21.0</version.docker-maven-plugin>

Expand Down
Loading

0 comments on commit f510110

Please sign in to comment.