Skip to content

Commit

Permalink
Users can override driverClassName property (spring-cloud#4144)
Browse files Browse the repository at this point in the history
* Users can override driverClassName property

resolves spring-cloud#4128

url can also be overridden using properties.

* Updated based on code review.

Thanks for the review
  • Loading branch information
cppwfs authored Sep 17, 2020
1 parent e6b9813 commit 0b911db
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,12 @@ public static TaskDefinition updateTaskProperties(TaskDefinition taskDefinition,
}
builder.setProperty("spring.datasource.username", dataSourceProperties.getUsername());
}
builder.setProperty("spring.datasource.url", dataSourceProperties.getUrl());
builder.setProperty("spring.datasource.driverClassName", dataSourceProperties.getDriverClassName());
if(!isPropertyPresent("spring.datasource.url", taskDefinition)) {
builder.setProperty("spring.datasource.url", dataSourceProperties.getUrl());
}
if(!isPropertyPresent("spring.datasource.driverClassName", taskDefinition)) {
builder.setProperty("spring.datasource.driverClassName", dataSourceProperties.getDriverClassName());
}
builder.setTaskName(taskDefinition.getTaskName());
builder.setDslText(taskDefinition.getDslText());
return builder.build();
Expand Down Expand Up @@ -228,4 +232,17 @@ private static String updateProperties(TaskNode taskNode, TaskApp subTask, Map<S
}
return result;
}

private static boolean isPropertyPresent(String property, TaskDefinition taskDefinition) {
RelaxedNames relaxedNames = new RelaxedNames(property);
boolean result = false;
Map<String, String> properties = taskDefinition.getProperties();
for (String dataFlowUriKey : relaxedNames) {
if (properties.containsKey(dataFlowUriKey)) {
result = true;
break;
}
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.rules.ExpectedException;

import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
Expand All @@ -32,6 +33,7 @@
import org.springframework.cloud.dataflow.server.controller.VisibleProperties;
import org.springframework.cloud.deployer.spi.core.AppDefinition;
import org.springframework.core.io.Resource;
import org.springframework.util.StringUtils;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertTrue;
Expand All @@ -47,7 +49,7 @@ public class TaskServiceUtilsTests {
public static final String BASE_GRAPH = "AAA && BBB";

@Rule
public ExpectedException expectedException = ExpectedException.none();
public ExpectedException expectedException;

@Test
public void testCreateComposedTaskDefinition() {
Expand All @@ -58,16 +60,18 @@ public void testCreateComposedTaskDefinition() {

@Test
public void testCreateComposeTaskDefinitionNullNameCheck() {
this.expectedException.expect(IllegalArgumentException.class);
TaskConfigurationProperties props = new TaskConfigurationProperties();
TaskServiceUtils.createComposedTaskDefinition(BASE_GRAPH);
TaskServiceUtils.createComposedTaskDefinition(null);
Assertions.assertThrows(IllegalArgumentException.class, () -> {
TaskConfigurationProperties props = new TaskConfigurationProperties();
TaskServiceUtils.createComposedTaskDefinition(BASE_GRAPH);
TaskServiceUtils.createComposedTaskDefinition(null);
});
}

@Test
public void testCreateComposeTaskDefinitionNullProperties() {
this.expectedException.expect(IllegalArgumentException.class);
TaskServiceUtils.createComposedTaskDefinition(BASE_GRAPH, null);
Assertions.assertThrows(IllegalArgumentException.class, () -> {
TaskServiceUtils.createComposedTaskDefinition(BASE_GRAPH, null);
});
}

@Test
Expand All @@ -88,16 +92,11 @@ public void testCTRPropertyReplacement() {
@Test
public void testDatabasePropUpdate() {
TaskDefinition taskDefinition = new TaskDefinition("testTask", "testApp");
DataSourceProperties dataSourceProperties = new DataSourceProperties();
dataSourceProperties.setUsername("myUser");
dataSourceProperties.setDriverClassName("myDriver");
dataSourceProperties.setPassword("myPassword");
dataSourceProperties.setUrl("myUrl");
DataSourceProperties dataSourceProperties = getDataSourceProperties();
TaskDefinition definition = TaskServiceUtils.updateTaskProperties(
taskDefinition,
dataSourceProperties);

assertThat(definition.getProperties().size()).isEqualTo(5);
assertThat(definition.getProperties().get("spring.datasource.url")).isEqualTo("myUrl");
assertThat(definition.getProperties().get("spring.datasource.driverClassName")).isEqualTo("myDriver");
assertThat(definition.getProperties().get("spring.datasource.username")).isEqualTo("myUser");
Expand All @@ -107,18 +106,58 @@ public void testDatabasePropUpdate() {
@Test
public void testDatabasePropUpdateWithPlatform() {
TaskDefinition taskDefinition = new TaskDefinition("testTask", "testApp");
DataSourceProperties dataSourceProperties = new DataSourceProperties();
dataSourceProperties.setUsername("myUser");
dataSourceProperties.setDriverClassName("myDriver");
dataSourceProperties.setPassword("myPassword");
dataSourceProperties.setUrl("myUrl");
DataSourceProperties dataSourceProperties = getDataSourceProperties();
TaskDefinition definition = TaskServiceUtils.updateTaskProperties(
taskDefinition,
dataSourceProperties, false);

assertThat(definition.getProperties().size()).isEqualTo(3);
assertThat(definition.getProperties().get("spring.datasource.url")).isEqualTo("myUrl");
validateProperties(definition, 3);
assertThat(definition.getProperties().get("spring.datasource.driverClassName")).isEqualTo("myDriver");
}

@Test
public void testDatabasePropUpdateWithPlatformForUserDriverClassName() {
TaskDefinition definition = createUpdatedDefinitionForProperty("spring.datasource.driverClassName", "foobar");
validateProperties(definition, 2);
assertThat(definition.getProperties().get("spring.datasource.driverClassName")).isEqualTo("foobar");

definition = createUpdatedDefinitionForProperty("spring.datasource.driver-class-name", "feebar");
validateProperties(definition, 2);
assertThat(definition.getProperties().get("spring.datasource.driver-class-name")).isEqualTo("feebar");

definition = createUpdatedDefinitionForProperty(null, null);
validateProperties(definition, 2);
assertThat(definition.getProperties().get("spring.datasource.driverClassName")).isEqualTo("myDriver");
}

@Test
public void testDatabasePropUpdateWithPlatformForUrl() {
TaskDefinition definition = createUpdatedDefinitionForProperty("spring.datasource.url", "newurl");
assertThat(definition.getProperties().get("spring.datasource.url")).isEqualTo("newurl");

definition = createUpdatedDefinitionForProperty(null, null);
assertThat(definition.getProperties().get("spring.datasource.url")).isEqualTo("myUrl");
}

private TaskDefinition createUpdatedDefinitionForProperty(String key, String value) {
Map<String, String> props = new HashMap<>();
if(StringUtils.hasText(key) && StringUtils.hasText(value)) {
props.put(key, value);
}
TaskDefinition taskDefinition = (new TaskDefinition.TaskDefinitionBuilder()).
addProperties(props).
setTaskName("testTask").
setRegisteredAppName("testApp").
build();
DataSourceProperties dataSourceProperties = getDataSourceProperties();
return TaskServiceUtils.updateTaskProperties(
taskDefinition,
dataSourceProperties, false);
}

private void validateProperties(TaskDefinition definition, int size) {
assertThat(definition.getProperties().size()).isEqualTo(size);
assertThat(definition.getProperties().get("spring.datasource.url")).isEqualTo("myUrl");
assertThat(definition.getProperties().get("spring.datasource.username")).isNull();
assertThat(definition.getProperties().get("spring.datasource.password")).isNull();
}
Expand Down Expand Up @@ -201,4 +240,13 @@ private TaskNode parse(String dsltext) {
TaskNode ctn = new TaskParser("test", dsltext, true, true).parse();
return ctn;
}

private DataSourceProperties getDataSourceProperties() {
DataSourceProperties dataSourceProperties = new DataSourceProperties();
dataSourceProperties.setUsername("myUser");
dataSourceProperties.setDriverClassName("myDriver");
dataSourceProperties.setPassword("myPassword");
dataSourceProperties.setUrl("myUrl");
return dataSourceProperties;
}
}

0 comments on commit 0b911db

Please sign in to comment.