mirror of
https://github.com/anthonyraymond/joal.git
synced 2024-11-10 17:12:36 +08:00
JoalConfigProvider fail on build if config file missing
This commit is contained in:
parent
b02766eb3f
commit
afb253e589
2 changed files with 23 additions and 31 deletions
|
@ -20,17 +20,25 @@ import java.nio.file.Paths;
|
|||
*/
|
||||
@Component
|
||||
public class JoalConfigProvider implements Provider<AppConfiguration> {
|
||||
private static final Logger logger = LoggerFactory.getLogger(JoalConfigProvider.class);
|
||||
private static final String CONF_FILE_NAME = "config.json";
|
||||
|
||||
private final String joalConfFolder;
|
||||
private final Path joalConfPath;
|
||||
private final ObjectMapper objectMapper;
|
||||
private boolean isDirty = true;
|
||||
private AppConfiguration config = null;
|
||||
|
||||
@Inject
|
||||
public JoalConfigProvider(final ObjectMapper objectMapper, @Value("${joal-conf}") final String confFolder) {
|
||||
public JoalConfigProvider(final ObjectMapper objectMapper, @Value("${joal-conf}") final String confFolder) throws FileNotFoundException {
|
||||
this.objectMapper = objectMapper;
|
||||
this.joalConfFolder = confFolder;
|
||||
|
||||
if (StringUtils.isBlank(confFolder)) {
|
||||
throw new IllegalArgumentException("A config path is required.");
|
||||
}
|
||||
this.joalConfPath = Paths.get(confFolder).resolve(CONF_FILE_NAME);
|
||||
if (!Files.exists(joalConfPath)) {
|
||||
throw new FileNotFoundException(String.format("Configuration file '%s' not found.", joalConfPath));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -49,17 +57,6 @@ public class JoalConfigProvider implements Provider<AppConfiguration> {
|
|||
|
||||
|
||||
AppConfiguration loadConfiguration() {
|
||||
if (StringUtils.isBlank(joalConfFolder)) {
|
||||
throw new IllegalArgumentException("A config path is required.");
|
||||
}
|
||||
final Path joalConfPath = Paths.get(joalConfFolder).resolve(CONF_FILE_NAME);
|
||||
if (!Files.exists(joalConfPath)) {
|
||||
throw new IllegalStateException(
|
||||
String.format("Configuration file '%s' not found.", joalConfPath),
|
||||
new FileNotFoundException(String.format("Configuration file '%s' not found.", joalConfPath))
|
||||
);
|
||||
}
|
||||
|
||||
final AppConfiguration configuration;
|
||||
try {
|
||||
configuration = objectMapper.readValue(joalConfPath.toFile(), AppConfiguration.class);
|
||||
|
|
|
@ -4,9 +4,11 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.nio.file.*;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
|
@ -25,35 +27,28 @@ public class JoalConfigProviderTest {
|
|||
|
||||
@Test
|
||||
public void shouldFailIWithEmptyConfigPath() {
|
||||
try {
|
||||
final JoalConfigProvider provider = new JoalConfigProvider(new ObjectMapper(), " ");
|
||||
provider.loadConfiguration();
|
||||
fail();
|
||||
} catch (final Exception e) {
|
||||
assertThat(e.getMessage()).isEqualTo("A config path is required.");
|
||||
}
|
||||
assertThatThrownBy(() ->new JoalConfigProvider(new ObjectMapper(), " "))
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessageContaining("A config path is required.");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFailIfJsonFileIsNotPresent() {
|
||||
try {
|
||||
final JoalConfigProvider provider = new JoalConfigProvider(new ObjectMapper(), resourcePath.resolve("dd").toString());
|
||||
provider.loadConfiguration();
|
||||
fail();
|
||||
} catch (final Exception e) {
|
||||
assertThat(e.getMessage()).isEqualTo("Configuration file 'src\\test\\resources\\configtest\\dd\\config.json' not found.");
|
||||
}
|
||||
final String fakePath = resourcePath.resolve("nop").toString();
|
||||
assertThatThrownBy(() ->new JoalConfigProvider(new ObjectMapper(), fakePath))
|
||||
.isInstanceOf(FileNotFoundException.class)
|
||||
.hasMessageContaining("Configuration file '" + fakePath + "\\config.json' not found.");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldLoadConf() {
|
||||
public void shouldLoadConf() throws FileNotFoundException {
|
||||
final JoalConfigProvider provider = new JoalConfigProvider(new ObjectMapper(), resourcePath.toString());
|
||||
|
||||
assertThat(provider.get()).isEqualToComparingFieldByField(defaultConfig);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldLoadConfOnlyOneTimeIfNoDirtyState() {
|
||||
public void shouldLoadConfOnlyOneTimeIfNoDirtyState() throws FileNotFoundException {
|
||||
final JoalConfigProvider provider = Mockito.spy(new JoalConfigProvider(new ObjectMapper(), resourcePath.toString()));
|
||||
|
||||
provider.get();
|
||||
|
@ -63,7 +58,7 @@ public class JoalConfigProviderTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void shouldReLoadConfOnlyOneTimeIfSetToDirtyState() {
|
||||
public void shouldReLoadConfOnlyOneTimeIfSetToDirtyState() throws FileNotFoundException {
|
||||
final JoalConfigProvider provider = Mockito.spy(new JoalConfigProvider(new ObjectMapper(), resourcePath.toString()));
|
||||
|
||||
provider.get();
|
||||
|
|
Loading…
Reference in a new issue