Skip to content

Commit

Permalink
Add typesafe provider unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Constantin Muraru committed Apr 15, 2016
1 parent 60c0a8c commit e84bc6d
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 0 deletions.
2 changes: 2 additions & 0 deletions archaius2-typesafe/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ apply plugin: 'java'
dependencies {
compile project(':archaius2-core')
compile 'com.typesafe:config:1.2.1'

testCompile project(':archaius2-guice')
}

eclipse {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package com.netflix.archaius.typesafe.dynamic;

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Scopes;
import com.netflix.archaius.api.Config;
import com.netflix.archaius.api.exceptions.ConfigException;
import com.netflix.archaius.api.inject.RemoteLayer;
import com.netflix.archaius.guice.ArchaiusModule;
import com.typesafe.config.ConfigFactory;
import org.junit.Test;

import java.util.function.Supplier;

import static org.junit.Assert.assertEquals;

public class DynamicTypesafeConfigTest {

@Test
public void test() throws ConfigException {
final TypesafeClientConfig config = new DefaultTypesafeClientConfig.Builder()
.withConfigFilePath(getResourceUrl("dynamic/reference.conf"))
.withRefreshIntervalMs(100)
.build();

Injector injector = Guice.createInjector(
new ArchaiusModule() {
@Override
protected void configureArchaius() {
bind(TypesafeClientConfig.class).toInstance(config);
bind(Config.class)
.annotatedWith(RemoteLayer.class)
.toProvider(TypesafeConfigProvider.class)
.in(Scopes.SINGLETON);
}
});

SampleAppConfig appConfig = injector.getInstance(SampleAppConfig.class);

assertEquals("app name", appConfig.name);
assertEquals(true, appConfig.flag);
assertEquals(111, appConfig.number.intValue());
}

@Test
public void testDynamicConfigChange() throws ConfigException, InterruptedException {
String[] configPath = new String[] {"dynamic/reference.conf"};

final TypesafeClientConfig config = new TypesafeClientConfig() {
@Override
public boolean isEnabled() {
return true;
}

@Override
public int getRefreshRateMs() {
return 200;
}

@Override
public String getTypesafeConfigPath() {
return configPath[0];
}

@Override
public Supplier<com.typesafe.config.Config> getTypesafeConfigSupplier() {
return () -> {
String typesafeConfigPath = getTypesafeConfigPath();
System.out.println("Load from: " + typesafeConfigPath);
return ConfigFactory.load(typesafeConfigPath);
};
}
};

Injector injector = Guice.createInjector(
new ArchaiusModule() {
@Override
protected void configureArchaius() {
bind(TypesafeClientConfig.class).toInstance(config);
bind(Config.class)
.annotatedWith(RemoteLayer.class)
.toProvider(TypesafeConfigProvider.class)
.in(Scopes.SINGLETON);
}
});

SampleAppConfig appConfig = injector.getInstance(SampleAppConfig.class);
assertEquals("app name", appConfig.name);
assertEquals(true, appConfig.flag);
assertEquals(111, appConfig.number.intValue());

// Simulate configuration update at runtime.
configPath[0] = "dynamic/updated.conf";
Thread.sleep(400);

SampleAppConfig updatedAppConfig = injector.getInstance(SampleAppConfig.class);
assertEquals("updated app name", updatedAppConfig.name);
assertEquals(false, updatedAppConfig.flag);
assertEquals(222, updatedAppConfig.number.intValue());
}

private String getResourceUrl(String resourceName) {
return "file://" + getClass().getClassLoader().getResource(resourceName).getPath();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.netflix.archaius.typesafe.dynamic;

import com.netflix.archaius.api.annotations.Configuration;

@Configuration(prefix = "app", allowFields = true)
public class SampleAppConfig {

public String name;
public Boolean flag;
public Integer number;
}
5 changes: 5 additions & 0 deletions archaius2-typesafe/src/test/resources/dynamic/reference.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
app {
name: "app name"
flag: true
number: 111
}
5 changes: 5 additions & 0 deletions archaius2-typesafe/src/test/resources/dynamic/updated.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
app {
name: "updated app name"
flag: false
number: 222
}

0 comments on commit e84bc6d

Please sign in to comment.