Search in sources :

Example 46 with FieldDescriptor

use of com.google.protobuf.Descriptors.FieldDescriptor in project core-java by SpineEventEngine.

the class FieldValidatorFactoryShould method create_double_field_validator.

@Test
public void create_double_field_validator() {
    final FieldDescriptor field = DoubleValue.getDescriptor().getFields().get(0);
    final FieldValidator validator = FieldValidatorFactory.create(field, 0, FIELD_PATH);
    assertTrue(validator instanceof DoubleFieldValidator);
}
Also used : ByteStringFieldValidator(io.spine.validate.ByteStringFieldValidator) FloatFieldValidator(io.spine.validate.FloatFieldValidator) StringFieldValidator(io.spine.validate.StringFieldValidator) LongFieldValidator(io.spine.validate.LongFieldValidator) BooleanFieldValidator(io.spine.validate.BooleanFieldValidator) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) Test(org.junit.Test)

Example 47 with FieldDescriptor

use of com.google.protobuf.Descriptors.FieldDescriptor in project core-java by SpineEventEngine.

the class ReferenceValidatorShould method skip_mapping_if_no_mapping_function_is_defined.

@Test
public void skip_mapping_if_no_mapping_function_is_defined() {
    final EventEnricher mockEnricher = mock(EventEnricher.class);
    when(mockEnricher.functionFor(any(Class.class), any(Class.class))).thenReturn(Optional.<EnrichmentFunction<?, ?>>absent());
    final ReferenceValidator validator = new ReferenceValidator(mockEnricher, UserDeletedEvent.class, EnrichmentBoundWithMultipleFieldsWithDifferentNames.class);
    final ValidationResult result = validator.validate();
    final List<EnrichmentFunction<?, ?>> functions = result.getFunctions();
    assertTrue(functions.isEmpty());
    final Multimap<FieldDescriptor, FieldDescriptor> fields = result.getFieldMap();
    assertEmpty(fields);
}
Also used : ValidationResult(io.spine.server.event.enrich.ReferenceValidator.ValidationResult) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) Test(org.junit.Test)

Example 48 with FieldDescriptor

use of com.google.protobuf.Descriptors.FieldDescriptor in project core-java by SpineEventEngine.

the class EventMessageEnricher method setFields.

// it is assured that
@SuppressWarnings({ "ConstantConditions", "MethodWithMultipleLoops" })
private // collections are not null
void setFields(Message.Builder builder, S eventMsg) {
    for (FieldDescriptor srcField : fieldMap.keySet()) {
        final Object srcFieldValue = getSrcFieldValue(srcField, eventMsg);
        final Class<?> sourceFieldClass = srcFieldValue.getClass();
        final Collection<EnrichmentFunction> functions = fieldFunctions.get(sourceFieldClass);
        final Collection<FieldDescriptor> targetFields = fieldMap.get(srcField);
        for (FieldDescriptor targetField : targetFields) {
            final Optional<EnrichmentFunction> function = FluentIterable.from(functions).firstMatch(SupportsFieldConversion.of(sourceFieldClass, Field.getFieldClass(targetField)));
            @SuppressWarnings({ "unchecked", /* the model is checked during */
            "OptionalGetWithoutIsPresent" }) final Object targetValue = function.get().apply(srcFieldValue);
            if (targetValue != null) {
                builder.setField(targetField, targetValue);
            }
        }
    }
}
Also used : FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor)

Example 49 with FieldDescriptor

use of com.google.protobuf.Descriptors.FieldDescriptor in project core-java by SpineEventEngine.

the class ReferenceValidator method validate.

/**
     * Returns those fields and functions, that may be used for the enrichment at the moment.
     *
     * @return a {@code ValidationResult} data transfer object, containing the valid fields and
     * functions.
     */
ValidationResult validate() {
    final List<EnrichmentFunction<?, ?>> functions = new LinkedList<>();
    final Multimap<FieldDescriptor, FieldDescriptor> fields = LinkedListMultimap.create();
    for (FieldDescriptor enrichmentField : enrichmentDescriptor.getFields()) {
        final Collection<FieldDescriptor> sourceFields = findSourceFields(enrichmentField);
        putEnrichmentsByField(functions, fields, enrichmentField, sourceFields);
    }
    final ImmutableMultimap<FieldDescriptor, FieldDescriptor> sourceToTargetMap = ImmutableMultimap.copyOf(fields);
    final ImmutableList<EnrichmentFunction<?, ?>> enrichmentFunctions = ImmutableList.copyOf(functions);
    final ValidationResult result = new ValidationResult(enrichmentFunctions, sourceToTargetMap);
    return result;
}
Also used : LinkedList(java.util.LinkedList) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor)

Example 50 with FieldDescriptor

use of com.google.protobuf.Descriptors.FieldDescriptor in project core-java by SpineEventEngine.

the class FieldShould method assertReturnsFieldClass.

private static void assertReturnsFieldClass(Class<?> expectedClass, Descriptor msgDescriptor) {
    final FieldDescriptor field = msgDescriptor.getFields().get(0);
    assertEquals(expectedClass, Field.getFieldClass(field));
}
Also used : FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor)

Aggregations

FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)58 Descriptor (com.google.protobuf.Descriptors.Descriptor)22 Test (org.junit.Test)17 EnumDescriptor (com.google.protobuf.Descriptors.EnumDescriptor)11 EnumValueDescriptor (com.google.protobuf.Descriptors.EnumValueDescriptor)11 BooleanFieldValidator (io.spine.validate.BooleanFieldValidator)9 ByteStringFieldValidator (io.spine.validate.ByteStringFieldValidator)9 FloatFieldValidator (io.spine.validate.FloatFieldValidator)9 LongFieldValidator (io.spine.validate.LongFieldValidator)9 StringFieldValidator (io.spine.validate.StringFieldValidator)9 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 TypicalData (protos.TypicalData)5 Collection (java.util.Collection)4 ConfigNode (com.google.api.codegen.configgen.nodes.ConfigNode)3 FieldConfigNode (com.google.api.codegen.configgen.nodes.FieldConfigNode)3 ListItemConfigNode (com.google.api.codegen.configgen.nodes.ListItemConfigNode)3 NullConfigNode (com.google.api.codegen.configgen.nodes.NullConfigNode)3 ScalarConfigNode (com.google.api.codegen.configgen.nodes.ScalarConfigNode)3 ByteString (com.google.protobuf.ByteString)3