Search in sources :

Example 6 with Database

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

the class ProvidenceConfigSupplierTest method testWatchedSupplier.

@Test
public void testWatchedSupplier() throws IOException {
    Database first = Database.builder().setDriver("com.mysql.Driver").build();
    Database second = Database.builder().setDriver("org.h2.Driver").build();
    File file = tmp.newFile().getAbsoluteFile().getCanonicalFile();
    when((Pair) parser.parseConfig(file.toPath(), null)).thenReturn(Pair.create(first, ImmutableSet.of(file.toString())));
    ArgumentCaptor<FileWatcher.Watcher> watcherCapture = ArgumentCaptor.forClass(FileWatcher.Watcher.class);
    doNothing().when(watcher).weakAddWatcher(watcherCapture.capture());
    ProvidenceConfigSupplier<Database, Database._Field> supplier = new ProvidenceConfigSupplier<>(file, null, watcher, parser, clock);
    assertThat(supplier.get(), is(sameInstance(first)));
    assertThat(supplier.get(), is(sameInstance(first)));
    assertThat(supplier.get(), is(sameInstance(first)));
    verify(parser).parseConfig(file.toPath(), null);
    verify(watcher).weakAddWatcher(any(FileWatcher.Watcher.class));
    verify(watcher, atMost(4)).startWatching(file);
    verifyNoMoreInteractions(watcher, parser);
    reset(parser, watcher);
    when((Pair) parser.parseConfig(file.toPath(), null)).thenReturn(Pair.create(second, ImmutableSet.of(file.toString())));
    doNothing().when(watcher).weakAddWatcher(watcherCapture.capture());
    watcherCapture.getValue().onFileUpdate(file);
    assertThat(supplier.get(), is(sameInstance(second)));
    assertThat(supplier.get(), is(sameInstance(second)));
    assertThat(supplier.get(), is(sameInstance(second)));
    verify(parser).parseConfig(file.toPath(), null);
    verifyNoMoreInteractions(parser, watcher);
}
Also used : FileWatcher(net.morimekta.util.FileWatcher) Database(net.morimekta.test.providence.config.Database) FileWatcher(net.morimekta.util.FileWatcher) File(java.io.File) Pair(net.morimekta.util.Pair) Test(org.junit.Test)

Example 7 with Database

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

the class ProvidenceConfigUtilTest method testAsType.

@Test
public void testAsType() throws ProvidenceConfigException {
    // null value.
    assertThat(asType(PPrimitive.STRING, null), is(nullValue()));
    // ENUM
    assertThat(asType(Value.kDescriptor, Value.SECOND), is(Value.SECOND));
    assertThat(asType(Value.kDescriptor, 2), is(Value.SECOND));
    assertThat(asType(Value.kDescriptor, "SECOND"), is(Value.SECOND));
    assertThat(asType(Value.kDescriptor, (Numeric) () -> 2), is(Value.SECOND));
    try {
        asType(Value.kDescriptor, Value.kDescriptor);
        fail("no exception");
    } catch (ProvidenceConfigException e) {
        assertThat(e.getMessage(), is("Unable to cast _Descriptor to enum config.Value"));
    }
    try {
        asType(Value.kDescriptor, PApplicationExceptionType.INVALID_MESSAGE_TYPE);
        fail("no exception");
    } catch (ProvidenceConfigException e) {
        assertThat(e.getMessage(), is("Unable to cast PApplicationExceptionType to enum config.Value"));
    }
    // MESSAGE
    Service service = Service.builder().build();
    Database db = Database.builder().build();
    assertThat(asType(Service.kDescriptor, service), is(service));
    try {
        asType(Service.kDescriptor, db);
        fail("no exception");
    } catch (ProvidenceConfigException e) {
        assertThat(e.getMessage(), is("Message type mismatch: config.Database is not compatible with config.Service"));
    }
    try {
        asType(Service.kDescriptor, "foo");
        fail("no exception");
    } catch (ProvidenceConfigException e) {
        assertThat(e.getMessage(), is("String is not compatible with message config.Service"));
    }
    // BINARY
    assertThat(asType(PPrimitive.BINARY, Binary.fromHexString("abcd")), is(Binary.fromHexString("abcd")));
    try {
        asType(PPrimitive.BINARY, 123);
        fail("no exception");
    } catch (ProvidenceConfigException e) {
        assertThat(e.getMessage(), is("Integer is not compatible with binary"));
    }
    // LIST
    assertThat(asType(PList.provider(PPrimitive.STRING.provider()).descriptor(), ImmutableList.of(1, 2)), is(ImmutableList.of("1", "2")));
    // SET
    assertThat(new ArrayList((Collection) asType(PSet.sortedProvider(PPrimitive.STRING.provider()).descriptor(), ImmutableList.of(3, 4, 2, 1))), is(ImmutableList.of("1", "2", "3", "4")));
    // MAP
    Map<String, String> map = (Map) asType(PMap.sortedProvider(PPrimitive.STRING.provider(), PPrimitive.STRING.provider()).descriptor(), ImmutableMap.of(1, 2, 3, 4));
    assertThat(map, is(instanceOf(ImmutableSortedMap.class)));
    assertThat(map, is(ImmutableMap.of("1", "2", "3", "4")));
    // General Failure
    try {
        asType(PPrimitive.VOID, "true");
        fail("no exception");
    } catch (IllegalStateException e) {
        assertThat(e.getMessage(), is("Unhandled field type: void"));
    }
}
Also used : Numeric(net.morimekta.util.Numeric) Database(net.morimekta.test.providence.config.Database) ArrayList(java.util.ArrayList) Service(net.morimekta.test.providence.config.Service) ProvidenceConfigUtil.asCollection(net.morimekta.providence.config.impl.ProvidenceConfigUtil.asCollection) Collection(java.util.Collection) ProvidenceConfigUtil.asString(net.morimekta.providence.config.impl.ProvidenceConfigUtil.asString) ProvidenceConfigUtil.asMap(net.morimekta.providence.config.impl.ProvidenceConfigUtil.asMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) PMap(net.morimekta.providence.descriptor.PMap) TreeMap(java.util.TreeMap) ProvidenceConfigException(net.morimekta.providence.config.ProvidenceConfigException) Test(org.junit.Test)

Example 8 with Database

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

the class ProvidenceConfigSupplierTest method testSupplier.

@Test
public void testSupplier() throws IOException {
    Database first = Database.builder().setDriver("com.mysql.Driver").build();
    Database second = Database.builder().setDriver("org.h2.Driver").build();
    File file = tmp.newFile().getAbsoluteFile().getCanonicalFile();
    when((Pair) parser.parseConfig(file.toPath(), null)).thenReturn(Pair.create(first, ImmutableSet.of(file.toString())));
    ArgumentCaptor<FileWatcher.Watcher> watcherCapture = ArgumentCaptor.forClass(FileWatcher.Watcher.class);
    doNothing().when(watcher).weakAddWatcher(watcherCapture.capture());
    ProvidenceConfigSupplier<Database, Database._Field> supplier = new ProvidenceConfigSupplier<>(file, null, watcher, parser, clock);
    assertThat(supplier.get(), is(sameInstance(first)));
    assertThat(supplier.get(), is(sameInstance(first)));
    assertThat(supplier.get(), is(sameInstance(first)));
    verify(parser).parseConfig(file.toPath(), null);
    verify(watcher).weakAddWatcher(any(FileWatcher.Watcher.class));
    verify(watcher, atLeast(1)).startWatching(any(File.class));
    verifyNoMoreInteractions(watcher, parser);
    reset(parser, watcher);
    when((Pair) parser.parseConfig(file.toPath(), null)).thenReturn(Pair.create(second, ImmutableSet.of(file.toString())));
    doNothing().when(watcher).weakAddWatcher(watcherCapture.capture());
    watcherCapture.getValue().onFileUpdate(file);
    assertThat(supplier.get(), is(sameInstance(second)));
    assertThat(supplier.get(), is(sameInstance(second)));
    assertThat(supplier.get(), is(sameInstance(second)));
    verify(parser).parseConfig(file.toPath(), null);
    verifyNoMoreInteractions(parser, watcher);
}
Also used : FileWatcher(net.morimekta.util.FileWatcher) Database(net.morimekta.test.providence.config.Database) FileWatcher(net.morimekta.util.FileWatcher) File(java.io.File) Pair(net.morimekta.util.Pair) Test(org.junit.Test)

Example 9 with Database

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

the class ProvidenceConfigSupplierTest method testParentConfigUpdatesPropagaing.

@Test
public void testParentConfigUpdatesPropagaing() throws IOException {
    TestConfigSupplier<Database, Database._Field> parent = new TestConfigSupplier<>(Database.builder().setUri("foo").build());
    File bar = writeContentTo("config.Database { driver = \"bar\" }", tmp.newFile("bar.config"));
    SimpleTypeRegistry registry = new SimpleTypeRegistry();
    registry.registerRecursively(Database.kDescriptor);
    parser = new ProvidenceConfigParser(registry, false);
    ProvidenceConfigSupplier<Database, Database._Field> config = new ProvidenceConfigSupplier<>(bar, parent, watcher, parser, clock);
    assertThat(config.get(), is(Database.builder().setUri("foo").setDriver("bar").build()));
    ConfigListener<Database, Database._Field> listener = mock(ConfigListener.class);
    config.addListener(listener);
    parent.testUpdate(Database.builder().setUri("fish").build());
    verify(listener).onConfigChange(Database.builder().setUri("fish").setDriver("bar").build());
    reset(listener);
    config.removeListener(listener);
    parent.testUpdate(Database.builder().build());
    verifyZeroInteractions(listener);
}
Also used : TestConfigSupplier(net.morimekta.providence.config.util.TestConfigSupplier) SimpleTypeRegistry(net.morimekta.providence.util.SimpleTypeRegistry) Database(net.morimekta.test.providence.config.Database) File(java.io.File) Test(org.junit.Test)

Example 10 with Database

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

the class ProvidenceConfigSupplierTest method testSupplierKeepInstanceOnFailedReload.

@Test
public void testSupplierKeepInstanceOnFailedReload() throws IOException {
    Database first = Database.builder().build();
    File file = tmp.newFile().getAbsoluteFile().getCanonicalFile();
    when((Pair) parser.parseConfig(file.toPath(), null)).thenReturn(Pair.create(first, ImmutableSet.of(file.toString())));
    ArgumentCaptor<FileWatcher.Watcher> watcherCapture = ArgumentCaptor.forClass(FileWatcher.Watcher.class);
    doNothing().when(watcher).weakAddWatcher(watcherCapture.capture());
    ProvidenceConfigSupplier<Database, Database._Field> supplier = new ProvidenceConfigSupplier<>(file, null, watcher, parser, clock);
    assertThat(supplier.get(), is(first));
    verify(watcher).weakAddWatcher(any(FileWatcher.Watcher.class));
    reset(parser, watcher);
    when(parser.parseConfig(file.toPath(), null)).thenThrow(new ProvidenceConfigException("test"));
    watcherCapture.getValue().onFileUpdate(file);
    verify(parser).parseConfig(file.toPath(), null);
    verifyNoMoreInteractions(parser);
    assertThat(supplier.get(), is(first));
}
Also used : FileWatcher(net.morimekta.util.FileWatcher) Database(net.morimekta.test.providence.config.Database) FileWatcher(net.morimekta.util.FileWatcher) File(java.io.File) ProvidenceConfigException(net.morimekta.providence.config.ProvidenceConfigException) Pair(net.morimekta.util.Pair) 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