Search in sources :

Example 1 with PipelineOptionDescriptor

use of org.apache.beam.model.jobmanagement.v1.JobApi.PipelineOptionDescriptor in project beam by apache.

the class PipelineOptionsFactoryTest method testDescribe.

@Test
public void testDescribe() {
    List<PipelineOptionDescriptor> described = PipelineOptionsFactory.describe(Sets.newHashSet(PipelineOptions.class, TestDescribeOptions.class));
    Map<String, PipelineOptionDescriptor> mapped = uniqueIndex(described, input -> input.getName());
    assertEquals("no duplicates", described.size(), mapped.size());
    Collection<PipelineOptionDescriptor> filtered = Collections2.filter(described, input -> input.getGroup().equals(TestDescribeOptions.class.getName()));
    assertEquals(6, filtered.size());
    mapped = uniqueIndex(filtered, input -> input.getName());
    PipelineOptionDescriptor listDesc = mapped.get("list");
    assertThat(listDesc, notNullValue());
    assertThat(listDesc.getDescription(), isEmptyString());
    assertEquals(PipelineOptionType.Enum.ARRAY, listDesc.getType());
    assertThat(listDesc.getDefaultValue(), isEmptyString());
    PipelineOptionDescriptor stringDesc = mapped.get("string");
    assertThat(stringDesc, notNullValue());
    assertThat(stringDesc.getDescription(), isEmptyString());
    assertEquals(PipelineOptionType.Enum.STRING, stringDesc.getType());
    assertThat(stringDesc.getDefaultValue(), isEmptyString());
    PipelineOptionDescriptor integerDesc = mapped.get("integer");
    assertThat(integerDesc, notNullValue());
    assertEquals("integer property", integerDesc.getDescription());
    assertEquals(PipelineOptionType.Enum.INTEGER, integerDesc.getType());
    assertThat(integerDesc.getDefaultValue(), isEmptyString());
    PipelineOptionDescriptor floatDesc = mapped.get("float");
    assertThat(integerDesc, notNullValue());
    assertEquals("float number property", floatDesc.getDescription());
    assertEquals(PipelineOptionType.Enum.NUMBER, floatDesc.getType());
    assertThat(floatDesc.getDefaultValue(), isEmptyString());
    PipelineOptionDescriptor booleanSimpleDesc = mapped.get("boolean_simple");
    assertThat(booleanSimpleDesc, notNullValue());
    assertEquals("simple boolean property", booleanSimpleDesc.getDescription());
    assertEquals(PipelineOptionType.Enum.BOOLEAN, booleanSimpleDesc.getType());
    assertThat(booleanSimpleDesc.getDefaultValue(), equalTo("true"));
    PipelineOptionDescriptor booleanWrapperDesc = mapped.get("boolean_wrapper");
    assertThat(booleanWrapperDesc, notNullValue());
    assertThat(booleanWrapperDesc.getDescription(), isEmptyString());
    assertEquals(PipelineOptionType.Enum.BOOLEAN, booleanWrapperDesc.getType());
    assertThat(booleanWrapperDesc.getDefaultValue(), equalTo("false"));
}
Also used : Module(com.fasterxml.jackson.databind.Module) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) ExpectedLogs(org.apache.beam.sdk.testing.ExpectedLogs) PipelineResult(org.apache.beam.sdk.PipelineResult) Matchers.not(org.hamcrest.Matchers.not) PipelineOptionDescriptor(org.apache.beam.model.jobmanagement.v1.JobApi.PipelineOptionDescriptor) ImmutableMap(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap) StdDeserializer(com.fasterxml.jackson.databind.deser.std.StdDeserializer) Sets(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Sets) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) Matchers.nullValue(org.hamcrest.Matchers.nullValue) Map(java.util.Map) JsonSerializer(com.fasterxml.jackson.databind.JsonSerializer) ArrayListMultimap(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ArrayListMultimap) JsonDeserializer(com.fasterxml.jackson.databind.JsonDeserializer) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) Collection(java.util.Collection) Set(java.util.Set) InterceptingUrlClassLoader(org.apache.beam.sdk.testing.InterceptingUrlClassLoader) List(java.util.List) JsonTypeInfo(com.fasterxml.jackson.annotation.JsonTypeInfo) RestoreSystemProperties(org.apache.beam.sdk.testing.RestoreSystemProperties) Assert.assertFalse(org.junit.Assert.assertFalse) Matchers.equalTo(org.hamcrest.Matchers.equalTo) ReflectHelpers(org.apache.beam.sdk.util.common.ReflectHelpers) ListMultimap(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ListMultimap) ImmutableList(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList) Matchers.anyOf(org.hamcrest.Matchers.anyOf) Matchers.containsString(org.hamcrest.Matchers.containsString) JsonDeserialize(com.fasterxml.jackson.databind.annotation.JsonDeserialize) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TestRule(org.junit.rules.TestRule) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) Assert.assertThrows(org.junit.Assert.assertThrows) Assume.assumeFalse(org.junit.Assume.assumeFalse) RunWith(org.junit.runner.RunWith) Maps.uniqueIndex(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Maps.uniqueIndex) Assert.assertSame(org.junit.Assert.assertSame) Collections2(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Collections2) PipelineRunner(org.apache.beam.sdk.PipelineRunner) JsonSerialize(com.fasterxml.jackson.databind.annotation.JsonSerialize) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) PipelineOptionType(org.apache.beam.model.jobmanagement.v1.JobApi.PipelineOptionType) JsonIgnore(com.fasterxml.jackson.annotation.JsonIgnore) ROOT(java.util.Locale.ROOT) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) SerializerProvider(com.fasterxml.jackson.databind.SerializerProvider) Pipeline(org.apache.beam.sdk.Pipeline) ExpectedException(org.junit.rules.ExpectedException) StdSerializer(com.fasterxml.jackson.databind.ser.std.StdSerializer) PrintStream(java.io.PrintStream) JsonSubTypes(com.fasterxml.jackson.annotation.JsonSubTypes) DeserializationContext(com.fasterxml.jackson.databind.DeserializationContext) JsonParser(com.fasterxml.jackson.core.JsonParser) PipelineRunnerRegistrar(org.apache.beam.sdk.runners.PipelineRunnerRegistrar) SystemUtils(org.apache.commons.lang3.SystemUtils) Charsets(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Charsets) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Assert.assertTrue(org.junit.Assert.assertTrue) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) CrashingRunner(org.apache.beam.sdk.testing.CrashingRunner) Matchers.hasItem(org.hamcrest.Matchers.hasItem) Assert.assertNull(org.junit.Assert.assertNull) Rule(org.junit.Rule) AutoService(com.google.auto.service.AutoService) Assert.assertEquals(org.junit.Assert.assertEquals) PipelineOptionDescriptor(org.apache.beam.model.jobmanagement.v1.JobApi.PipelineOptionDescriptor) Matchers.containsString(org.hamcrest.Matchers.containsString) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) Test(org.junit.Test)

Example 2 with PipelineOptionDescriptor

use of org.apache.beam.model.jobmanagement.v1.JobApi.PipelineOptionDescriptor in project beam by apache.

the class PipelineOptionsFactory method describe.

/**
 * Outputs the set of options available to be set for the passed in {@link PipelineOptions}
 * interfaces. The output for consumption of the job service client.
 */
public static List<PipelineOptionDescriptor> describe(Set<Class<? extends PipelineOptions>> ifaces) {
    checkNotNull(ifaces);
    List<PipelineOptionDescriptor> result = new ArrayList<>();
    Set<Method> seenMethods = Sets.newHashSet();
    for (Class<? extends PipelineOptions> iface : ifaces) {
        CACHE.get().validateWellFormed(iface);
        Set<PipelineOptionSpec> properties = PipelineOptionsReflector.getOptionSpecs(iface, false);
        RowSortedTable<Class<?>, String, Method> ifacePropGetterTable = TreeBasedTable.create(ClassNameComparator.INSTANCE, Ordering.natural());
        for (PipelineOptionSpec prop : properties) {
            ifacePropGetterTable.put(prop.getDefiningInterface(), prop.getName(), prop.getGetterMethod());
        }
        for (Map.Entry<Class<?>, Map<String, Method>> ifaceToPropertyMap : ifacePropGetterTable.rowMap().entrySet()) {
            Class<?> currentIface = ifaceToPropertyMap.getKey();
            Map<String, Method> propertyNamesToGetters = ifaceToPropertyMap.getValue();
            List<@KeyFor("propertyNamesToGetters") String> lists = Lists.newArrayList(propertyNamesToGetters.keySet());
            lists.sort(String.CASE_INSENSITIVE_ORDER);
            for (String propertyName : lists) {
                Method method = propertyNamesToGetters.get(propertyName);
                if (!seenMethods.add(method)) {
                    continue;
                }
                Class<?> returnType = method.getReturnType();
                PipelineOptionType.Enum optionType = PipelineOptionType.Enum.STRING;
                if (JSON_INTEGER_TYPES.contains(returnType)) {
                    optionType = PipelineOptionType.Enum.INTEGER;
                } else if (JSON_NUMBER_TYPES.contains(returnType)) {
                    optionType = PipelineOptionType.Enum.NUMBER;
                } else if (returnType == boolean.class || returnType == Boolean.class) {
                    optionType = PipelineOptionType.Enum.BOOLEAN;
                } else if (List.class.isAssignableFrom(returnType)) {
                    optionType = PipelineOptionType.Enum.ARRAY;
                }
                String optionName = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, propertyName);
                Description description = method.getAnnotation(Description.class);
                PipelineOptionDescriptor.Builder builder = PipelineOptionDescriptor.newBuilder().setName(optionName).setType(optionType).setGroup(currentIface.getName());
                Optional<String> defaultValue = getDefaultValueFromAnnotation(method);
                if (defaultValue.isPresent()) {
                    builder.setDefaultValue(defaultValue.get());
                }
                if (description != null) {
                    builder.setDescription(description.value());
                }
                result.add(builder.build());
            }
        }
    }
    return result;
}
Also used : PipelineOptionType(org.apache.beam.model.jobmanagement.v1.JobApi.PipelineOptionType) ArrayList(java.util.ArrayList) PipelineOptionDescriptor(org.apache.beam.model.jobmanagement.v1.JobApi.PipelineOptionDescriptor) Method(java.lang.reflect.Method) AnnotatedMethod(com.fasterxml.jackson.databind.introspect.AnnotatedMethod) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableMap(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap) Map(java.util.Map) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap)

Aggregations

List (java.util.List)2 Map (java.util.Map)2 PipelineOptionDescriptor (org.apache.beam.model.jobmanagement.v1.JobApi.PipelineOptionDescriptor)2 PipelineOptionType (org.apache.beam.model.jobmanagement.v1.JobApi.PipelineOptionType)2 ImmutableMap (org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap)2 JsonIgnore (com.fasterxml.jackson.annotation.JsonIgnore)1 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)1 JsonSubTypes (com.fasterxml.jackson.annotation.JsonSubTypes)1 JsonTypeInfo (com.fasterxml.jackson.annotation.JsonTypeInfo)1 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 JsonParser (com.fasterxml.jackson.core.JsonParser)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 DeserializationContext (com.fasterxml.jackson.databind.DeserializationContext)1 JsonDeserializer (com.fasterxml.jackson.databind.JsonDeserializer)1 JsonSerializer (com.fasterxml.jackson.databind.JsonSerializer)1 Module (com.fasterxml.jackson.databind.Module)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 SerializerProvider (com.fasterxml.jackson.databind.SerializerProvider)1 JsonDeserialize (com.fasterxml.jackson.databind.annotation.JsonDeserialize)1 JsonSerialize (com.fasterxml.jackson.databind.annotation.JsonSerialize)1