use of net.morimekta.util.Numeric 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"));
}
}
Aggregations