Search in sources :

Example 1 with Database

use of net.morimekta.test.providence.config.Database in project providence by morimekta.

the class FixedConfigSupplierTest method testCreate.

@Test
@SuppressWarnings("unchecked")
public void testCreate() {
    FakeClock clock = FakeClock.forCurrentTimeMillis(System.currentTimeMillis());
    Database value = Database.builder().build();
    ConfigSupplier<Database, Database._Field> supplier = mock(ConfigSupplier.class);
    when(supplier.get()).thenReturn(value).thenReturn(value);
    when(supplier.configTimestamp()).thenAnswer(i -> clock.millis());
    when(supplier.snapshot()).thenCallRealMethod();
    FixedConfigSupplier<Database, Database._Field> fixed = new FixedConfigSupplier<>(supplier);
    clock.tick(1000);
    ConfigSupplier<Database, Database._Field> snapshot = supplier.snapshot();
    assertThat(fixed.get(), is(sameInstance(value)));
    assertThat(snapshot.get(), is(sameInstance(value)));
    assertThat(fixed.configTimestamp(), is(snapshot.configTimestamp() - 1000));
    assertThat(fixed.snapshot(), is(sameInstance(fixed)));
}
Also used : FakeClock(net.morimekta.testing.time.FakeClock) Database(net.morimekta.test.providence.config.Database) Test(org.junit.Test)

Example 2 with Database

use of net.morimekta.test.providence.config.Database in project providence by morimekta.

the class OverrideConfigSupplierTest method testSupplier.

@Test
public void testSupplier() throws IOException {
    OverrideConfigSupplier<Database, Database._Field> supplier = new OverrideConfigSupplier<>(clock, base, ImmutableMap.of("credentials.password", "password", "uri", "undefined"), true);
    Database instance = supplier.get();
    assertThat(supplier.get(), is(sameInstance(instance)));
    // it was reset.
    assertThat(instance.getUri(), is(nullValue()));
    assertThat(instance.getDriver(), is("driver"));
    assertThat(instance.getCredentials(), is(not(nullValue())));
    assertThat(instance.getCredentials().getUsername(), is("username"));
    // it was updated.
    assertThat(instance.getCredentials().getPassword(), is("password"));
    base.testUpdate(Database.builder().setUri("http://hostname:9057/path").setDriver("otherDriver").setCredentials(new Credentials("username", "complicated password that no one guesses")).build());
    // updating the base updates the result.
    assertThat(supplier.get(), is(not(sameInstance(instance))));
    instance = supplier.get();
    assertThat(supplier.get(), is(sameInstance(instance)));
    assertThat(supplier.configTimestamp(), is(clock.millis()));
    // it was reset.
    assertThat(instance.getUri(), is(nullValue()));
    assertThat(instance.getDriver(), is("otherDriver"));
    assertThat(instance.getCredentials(), is(not(nullValue())));
    assertThat(instance.getCredentials().getUsername(), is("username"));
    // it was updated.
    assertThat(instance.getCredentials().getPassword(), is("password"));
}
Also used : Database(net.morimekta.test.providence.config.Database) Credentials(net.morimekta.test.providence.config.Credentials) Test(org.junit.Test)

Example 3 with Database

use of net.morimekta.test.providence.config.Database in project providence by morimekta.

the class ProvidenceConfigTest method testGetConfig_withParent.

@Test
public void testGetConfig_withParent() throws IOException {
    File f_stage_db = copyResourceTo("/net/morimekta/providence/config/files/stage_db.cfg", temp.getRoot());
    File f_stage_nocred = copyResourceTo("/net/morimekta/providence/config/files/stage_nocred.cfg", temp.getRoot());
    ProvidenceConfig config = new ProvidenceConfig(registry, null, true);
    Database stage_db = config.getConfig(f_stage_db);
    Database stage_nocred = config.getConfig(f_stage_nocred, stage_db);
    assertEquals("{\n" + "  uri = \"jdbc:h2:localhost:mem\"\n" + "  driver = \"org.h2.Driver\"\n" + "}", debugString(stage_nocred));
}
Also used : Database(net.morimekta.test.providence.config.Database) File(java.io.File) Test(org.junit.Test)

Example 4 with Database

use of net.morimekta.test.providence.config.Database in project providence by morimekta.

the class ProvidenceConfigTest method testResolveConfig_withParent.

@Test
public void testResolveConfig_withParent() throws IOException {
    File f_stage_db = copyResourceTo("/net/morimekta/providence/config/files/stage_db.cfg", temp.getRoot());
    File f_stage_nocred = copyResourceTo("/net/morimekta/providence/config/files/stage_nocred.cfg", temp.getRoot());
    ProvidenceConfig config = new ProvidenceConfig(registry, null, true);
    ConfigSupplier<Database, Database._Field> stage_db = config.resolveConfig(f_stage_db);
    ConfigSupplier<Database, Database._Field> stage_nocred = config.resolveConfig(f_stage_nocred, stage_db);
    assertEquals("{\n" + "  uri = \"jdbc:h2:localhost:mem\"\n" + "  driver = \"org.h2.Driver\"\n" + "}", debugString(stage_nocred.get()));
}
Also used : Database(net.morimekta.test.providence.config.Database) File(java.io.File) Test(org.junit.Test)

Example 5 with Database

use of net.morimekta.test.providence.config.Database in project providence by morimekta.

the class ProvidenceConfigParserTest method testParseWithUnknownInclude.

@Test
public void testParseWithUnknownInclude() throws IOException {
    copyResourceTo("/net/morimekta/providence/config/files/unknown.cfg", temp.getRoot());
    File file = copyResourceTo("/net/morimekta/providence/config/files/unknown_include.cfg", temp.getRoot());
    Pair<Database, Set<String>> cfg = parser.parseConfig(file.toPath(), null);
    // all the unknowns are skipped.
    assertEquals("{\n" + "  uri = \"jdbc:h2:localhost:mem\"\n" + "  driver = \"org.h2.Driver\"\n" + "}", debugString(cfg.first));
    file = copyResourceTo("/net/morimekta/providence/config/files/unknown_field.cfg", temp.getRoot());
    cfg = parser.parseConfig(file.toPath(), null);
    assertEquals("{\n" + "  uri = \"jdbc:h2:localhost:mem\"\n" + "  driver = \"org.h2.Driver\"\n" + "}", debugString(cfg.first));
    file = copyResourceTo("/net/morimekta/providence/config/files/unknown_enum_value.cfg", temp.getRoot());
    cfg = parser.parseConfig(file.toPath(), null);
    assertEquals("{\n" + "  uri = \"jdbc:h2:localhost:mem\"\n" + "  driver = \"org.h2.Driver\"\n" + "}", debugString(cfg.first));
}
Also used : Set(java.util.Set) Database(net.morimekta.test.providence.config.Database) File(java.io.File) Test(org.junit.Test)

Aggregations

Database (net.morimekta.test.providence.config.Database)11 Test (org.junit.Test)11 File (java.io.File)8 FileWatcher (net.morimekta.util.FileWatcher)3 Pair (net.morimekta.util.Pair)3 ProvidenceConfigException (net.morimekta.providence.config.ProvidenceConfigException)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Set (java.util.Set)1 TreeMap (java.util.TreeMap)1 ProvidenceConfigUtil.asCollection (net.morimekta.providence.config.impl.ProvidenceConfigUtil.asCollection)1 ProvidenceConfigUtil.asMap (net.morimekta.providence.config.impl.ProvidenceConfigUtil.asMap)1 ProvidenceConfigUtil.asString (net.morimekta.providence.config.impl.ProvidenceConfigUtil.asString)1 TestConfigSupplier (net.morimekta.providence.config.util.TestConfigSupplier)1 PMap (net.morimekta.providence.descriptor.PMap)1