Add setDirtyState to ConfigProvider

This commit is contained in:
anthonyraymond 2017-04-28 23:36:47 +02:00
parent e574ce5722
commit b02766eb3f
2 changed files with 31 additions and 11 deletions

View file

@ -2,6 +2,8 @@ package org.araymond.joal.core.config;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@ -20,7 +22,6 @@ import java.nio.file.Paths;
public class JoalConfigProvider implements Provider<AppConfiguration> {
private static final String CONF_FILE_NAME = "config.json";
private final String joalConfFolder;
private final ObjectMapper objectMapper;
private boolean isDirty = true;
@ -35,12 +36,17 @@ public class JoalConfigProvider implements Provider<AppConfiguration> {
@Override
public AppConfiguration get() {
if (this.isDirty || this.config == null) {
this.config = loadConfiguration();
}
return this.config;
}
// TODO: implement a watcher to check if config is updated (and then set isDirty)
void setDirtyState() {
this.isDirty = true;
}
AppConfiguration loadConfiguration() {
if (StringUtils.isBlank(joalConfFolder)) {

View file

@ -1,16 +1,10 @@
package org.araymond.joal.core.config;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.Mockito;
import javax.inject.Provider;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;
@ -20,7 +14,7 @@ import static org.junit.Assert.fail;
*/
public class JoalConfigProviderTest {
static final Path resourcePath = Paths.get("src/test/resources/configtest");
private static final Path resourcePath = Paths.get("src/test/resources/configtest");
public static final AppConfiguration defaultConfig = new AppConfiguration(
180,
190,
@ -54,9 +48,29 @@ public class JoalConfigProviderTest {
@Test
public void shouldLoadConf() {
final JoalConfigProvider provider = new JoalConfigProvider(new ObjectMapper(), resourcePath.toString());
final AppConfiguration conf = provider.loadConfiguration();
assertThat(conf).isEqualToComparingFieldByField(defaultConfig);
assertThat(provider.get()).isEqualToComparingFieldByField(defaultConfig);
}
@Test
public void shouldLoadConfOnlyOneTimeIfNoDirtyState() {
final JoalConfigProvider provider = Mockito.spy(new JoalConfigProvider(new ObjectMapper(), resourcePath.toString()));
provider.get();
provider.get();
Mockito.verify(provider, Mockito.times(1)).loadConfiguration();
}
@Test
public void shouldReLoadConfOnlyOneTimeIfSetToDirtyState() {
final JoalConfigProvider provider = Mockito.spy(new JoalConfigProvider(new ObjectMapper(), resourcePath.toString()));
provider.get();
provider.setDirtyState();
provider.get();
Mockito.verify(provider, Mockito.times(2)).loadConfiguration();
}
}