ProvidenceConfig implements ConfigResolver { @Nonnull public <M extends PMessage<M, F>, F extends PField> M getConfig(@Nonnull File configFile) throws ProvidenceConfigException { ConfigSupplier<M,F> supplier = resolveConfig(configFile); return supplier.get(); } ProvidenceConfig(TypeRegistry registry);  ProvidenceConfig(@Nonnull TypeRegistry registry,
                            @Nullable FileWatcher watcher, boolean strict);  ProvidenceConfig(@Nonnull TypeRegistry registry,
                            @Nullable FileWatcher watcher,
                            boolean strict,
                            @Nonnull Clock clock); @Nonnull @Override @SuppressWarnings("unchecked") ConfigSupplier<M, F> resolveConfig(@Nonnull File configFile,
                                       @Nullable ConfigSupplier<M, F> parentConfig); @Nonnull M getConfig(@Nonnull File configFile); @Nonnull M getConfig(@Nonnull File configFile, @Nonnull M parent);  }
@Test public void testGetConfig() throws IOException { File f_stage_db = copyResourceTo("/net/morimekta/providence/config/files/stage_db.cfg", temp.getRoot()); ProvidenceConfig config = new ProvidenceConfig(registry, null, true); Database stage_db = config.getConfig(f_stage_db); assertEquals("{\n" + " uri = \"jdbc:h2:localhost:mem\"\n" + " driver = \"org.h2.Driver\"\n" + " credentials = {\n" + " username = \"myuser\"\n" + " password = \"MyP4s5w0rd\"\n" + " }\n" + "}", debugString(stage_db)); }
@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)); }
@Test public void testGetConfig_withParent_badType() { try { File first = copyResourceTo("/net/morimekta/providence/config/files/base_service.cfg", temp.getRoot()); File second = copyResourceTo("/net/morimekta/providence/config/files/stage_db.cfg", temp.getRoot()); ProvidenceConfig config = new ProvidenceConfig(registry, null, true); Service firstConfig = config.getConfig(first); config.getConfig(second, firstConfig); fail("no exception"); } catch (ProvidenceConfigException e) { assertThat(e.getMessage(), is("Loaded config type config.Service does not match parent config.Database")); assertThat(e.getFile(), is("stage_db.cfg")); assertThat(e.asString(), is("Error in stage_db.cfg: Loaded config type config.Service does not match parent config.Database")); } }
@Test public void testDefinesEveryType() throws IOException { ProvidenceConfig config = new ProvidenceConfig(registry); File defs = copyResourceTo("/net/morimekta/providence/config/files/all_defs.cfg", temp.getRoot()); RefConfig1 ref = config.getConfig(defs); Assert.assertThat(ref, is(equalToMessage( RefConfig1.builder() .setBoolValue(true) .setByteValue((byte) 123) .setI16Value((short) 12345) .setI32Value(1234567890) .setI64Value(12345678901234567L) .setDoubleValue(1234567.1234567) .setEnumValue(Value.SECOND) .setBinValue(Binary.fromHexString("01020304")) .setStrValue("This is a string") .setMsgValue(Database.builder() .setDriver("Driver") .build()) .build()))); }
@Test public void testReusableObject() throws ProvidenceConfigException { FileWatcher watcher = new FileWatcher(); ProvidenceConfig lazy = new ProvidenceConfig(registry, watcher, false); ProvidenceConfig strict = new ProvidenceConfig(registry, watcher, true); File file = copyResourceTo("/net/morimekta/providence/config/files/reusable_object.cfg", temp.getRoot()); Service service = lazy.getConfig(file); assertThat(service.getAdmin().getOauthTokenKey(), is(service.getHttp().getSignatureKeys().get("app1"))); try { strict.getConfig(file); fail("no exception"); } catch (ProvidenceConfigException e) { assertThat(e.getMessage(), is("Reusable objects are not allowed in strict mode.")); assertThat(e.getFile(), is(file.getName())); assertThat(e.asString(), is("Error in reusable_object.cfg on line 9, pos 27: Reusable objects are not allowed in strict mode.\n" + " oauth_token_key & token = b64(VGVzdCBPYXV0aCBLZXkK)\n" + "--------------------------^^^^^")); } }
@Test public void testResolveConfig_simple() throws IOException { copyResourceTo("/net/morimekta/providence/config/files/base_service.cfg", temp.getRoot()); copyResourceTo("/net/morimekta/providence/config/files/prod_db.cfg", temp.getRoot()); copyResourceTo("/net/morimekta/providence/config/files/stage_db.cfg", temp.getRoot()); File prod = copyResourceTo("/net/morimekta/providence/config/files/prod.cfg", temp.getRoot()); File stage = copyResourceTo("/net/morimekta/providence/config/files/stage.cfg", temp.getRoot()); ProvidenceConfig config = new ProvidenceConfig(registry, null, true); Service stage_service = config.getConfig(stage); Service prod_service = config.getConfig(prod); assertEquals("{\n" + " name = \"prod\"\n" + " http = {\n" + " port = 8080\n" + " context = \"/app\"\n" + " signature_keys = {\n" + " \"app1\": b64(VGVzdCBPYXV0aCBLZXkK)\n" + " }\n" + " signature_override_keys = [\n" + " \"not_really_app_1\"\n" + " ]\n" + " }\n" + " admin = {\n" + " port = 8088\n" + " oauth_token_key = b64(VGVzdCBPYXV0aCBLZXkK)\n" + " }\n" + " db = {\n" + " uri = \"jdbc:mysql:db01:1364/my_db\"\n" + " driver = \"org.mysql.Driver\"\n" + " credentials = {\n" + " username = \"dbuser\"\n" + " password = \"DbP4s5w0rD\"\n" + " }\n" + " }\n" + "}", debugString(prod_service)); assertEquals("{\n" + " name = \"stage\"\n" + " http = {\n" + " port = 8080\n" + " context = \"/app\"\n" + " signature_keys = {\n" + " \"app1\": b64(VGVzdCBPYXV0aCBLZXkK)\n" + " }\n" + " signature_override_keys = [\n" + " \"not_really_app_1\"\n" + " ]\n" + " }\n" + " db = {\n" + " uri = \"jdbc:h2:localhost:mem\"\n" + " driver = \"org.h2.Driver\"\n" + " credentials = {\n" + " username = \"myuser\"\n" + " password = \"MyP4s5w0rd\"\n" + " }\n" + " }\n" + "}", debugString(stage_service)); }