Search in sources :

Example 6 with Service

use of net.morimekta.test.providence.config.Service 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 7 with Service

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

the class ProvidenceConfigTest method testGetConfig_withParent_badType.

@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"));
    }
}
Also used : Service(net.morimekta.test.providence.config.Service) File(java.io.File) Test(org.junit.Test)

Example 8 with Service

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

the class ProvidenceConfigTest method testName.

@Test
public void testName() throws ProvidenceConfigException {
    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<Service, Service._Field> stage_db = config.resolveConfig(f_stage_db);
    ConfigSupplier<Service, Service._Field> stage_nocred = config.resolveConfig(f_stage_nocred, stage_db);
    Assert.assertThat(stage_db.getName(), is("ProvidenceConfig{stage_db.cfg}"));
    Assert.assertThat(stage_db.toString(), is("ProvidenceConfig{stage_db.cfg}"));
    Assert.assertThat(stage_nocred.getName(), is("ProvidenceConfig{stage_nocred.cfg}"));
    Assert.assertThat(stage_nocred.toString(), is("ProvidenceConfig{stage_nocred.cfg, parent=ProvidenceConfig{stage_db.cfg}}"));
}
Also used : Service(net.morimekta.test.providence.config.Service) File(java.io.File) Test(org.junit.Test)

Aggregations

Service (net.morimekta.test.providence.config.Service)8 Test (org.junit.Test)8 File (java.io.File)7 FileWatcher (net.morimekta.util.FileWatcher)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 ProvidenceConfigException (net.morimekta.providence.config.ProvidenceConfigException)2 Database (net.morimekta.test.providence.config.Database)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)1 IOException (java.io.IOException)1 Files (java.nio.file.Files)1 StandardCopyOption (java.nio.file.StandardCopyOption)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 TreeMap (java.util.TreeMap)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1