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 authored and rhuss committed Jul 30, 2018
1 parent 99cbd2e commit bbf1914
Show file tree
Hide file tree
Showing 10 changed files with 450 additions and 227 deletions.
7 changes: 7 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@
<artifactId>json-path-assert</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
362 changes: 203 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
Loading

0 comments on commit bbf1914

Please sign in to comment.