Polish "Fix NPE in FlywayEndpoint when migration.installedOn is null"

Closes gh-14019
This commit is contained in:
Andy Wilkinson
2018-08-09 11:10:21 +01:00
parent 83fbdc6ea8
commit 27f5e46587
4 changed files with 45 additions and 9 deletions

View File

@@ -168,18 +168,17 @@ public class FlywayEndpoint {
this.installedBy = info.getInstalledBy();
this.installedRank = info.getInstalledRank();
this.executionTime = info.getExecutionTime();
this.installedOn = toEpochMilli(info.getInstalledOn());
}
private Instant toEpochMilli(Date installedOn) {
return (installedOn != null) ? Instant.ofEpochMilli(installedOn.getTime())
: null;
this.installedOn = nullSafeToInstant(info.getInstalledOn());
}
private String nullSafeToString(Object obj) {
return (obj != null) ? obj.toString() : null;
}
private Instant nullSafeToInstant(Date date) {
return (date != null) ? Instant.ofEpochMilli(date.getTime()) : null;
}
public MigrationType getType() {
return this.type;
}

View File

@@ -16,9 +16,13 @@
package org.springframework.boot.actuate.flyway;
import java.util.Map;
import org.junit.Test;
import org.springframework.boot.actuate.flyway.FlywayEndpoint.FlywayDescriptor;
import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration;
import org.springframework.boot.autoconfigure.flyway.FlywayMigrationStrategy;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.ApplicationContext;
@@ -40,9 +44,28 @@ public class FlywayEndpointTests {
@Test
public void flywayReportIsProduced() {
new ApplicationContextRunner().withUserConfiguration(Config.class)
.run((context) -> assertThat(
context.getBean(FlywayEndpoint.class).flywayBeans().getContexts()
.get(context.getId()).getFlywayBeans()).hasSize(1));
.run((context) -> {
Map<String, FlywayDescriptor> flywayBeans = context
.getBean(FlywayEndpoint.class).flywayBeans().getContexts()
.get(context.getId()).getFlywayBeans();
assertThat(flywayBeans).hasSize(1);
assertThat(flywayBeans.values().iterator().next().getMigrations())
.hasSize(3);
});
}
@Test
public void whenFlywayHasBeenBaselinedFlywayReportIsProduced() {
new ApplicationContextRunner()
.withUserConfiguration(BaselinedFlywayConfig.class, Config.class)
.run((context) -> {
Map<String, FlywayDescriptor> flywayBeans = context
.getBean(FlywayEndpoint.class).flywayBeans().getContexts()
.get(context.getId()).getFlywayBeans();
assertThat(flywayBeans).hasSize(1);
assertThat(flywayBeans.values().iterator().next().getMigrations())
.hasSize(3);
});
}
@Configuration
@@ -56,4 +79,18 @@ public class FlywayEndpointTests {
}
@Configuration
public static class BaselinedFlywayConfig {
@Bean
public FlywayMigrationStrategy baseliningMigrationStrategy() {
return (flyway) -> {
flyway.setBaselineVersionAsString("2");
flyway.baseline();
flyway.migrate();
};
}
}
}