diff --git a/src/main/java/org/araymond/joal/core/config/JoalConfigProvider.java b/src/main/java/org/araymond/joal/core/config/JoalConfigProvider.java index 561c154..f602a3c 100644 --- a/src/main/java/org/araymond/joal/core/config/JoalConfigProvider.java +++ b/src/main/java/org/araymond/joal/core/config/JoalConfigProvider.java @@ -20,17 +20,25 @@ import java.nio.file.Paths; */ @Component public class JoalConfigProvider implements Provider { + 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 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); diff --git a/src/test/java/org/araymond/joal/core/config/JoalConfigProviderTest.java b/src/test/java/org/araymond/joal/core/config/JoalConfigProviderTest.java index 893c9e2..846e3e6 100644 --- a/src/test/java/org/araymond/joal/core/config/JoalConfigProviderTest.java +++ b/src/test/java/org/araymond/joal/core/config/JoalConfigProviderTest.java @@ -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();