Search in sources :

Example 1 with DiagCollector

use of com.google.api.tools.framework.model.DiagCollector in project toolkit by googleapis.

the class PageStreamingConfig method createPageStreaming.

/**
 * Creates an instance of PageStreamingConfig based on Discovery Doc, linking it up with the
 * provided method. On errors, null will be returned, and diagnostics are reported to the diag
 * collector.
 *
 * @param method Method descriptor for the method to create config for.
 */
@Nullable
static // TODO(andrealin): Merge this function into the createPageStreaming(... Method protoMethod) function.
PageStreamingConfig createPageStreaming(DiscoApiModel apiModel, com.google.api.codegen.discovery.Method method) {
    Schema requestTokenField = method.parameters().get(PARAMETER_PAGE_TOKEN);
    DiagCollector diagCollector = apiModel.getDiagCollector();
    if (requestTokenField == null) {
        diagCollector.addDiag(Diag.error(SimpleLocation.TOPLEVEL, "Request field missing for page streaming: method = %s, message type = %s, field = %s", method.id(), method.id(), PARAMETER_PAGE_TOKEN));
    }
    Schema pageSizeField = method.parameters().get(PARAMETER_MAX_RESULTS);
    if (pageSizeField != null) {
        pageSizeField = method.parameters().get(PARAMETER_MAX_RESULTS);
        if (pageSizeField == null) {
            diagCollector.addDiag(Diag.error(SimpleLocation.TOPLEVEL, "Request field missing for page streaming: method = %s, message type = %s, field = %s", method.id(), method.id(), PARAMETER_MAX_RESULTS));
        }
    }
    Schema responseTokenField = null;
    Schema responseSchema = method.response().dereference();
    if (responseSchema.hasProperty(PARAMETER_NEXT_PAGE_TOKEN)) {
        responseTokenField = responseSchema.properties().get(PARAMETER_NEXT_PAGE_TOKEN);
    }
    if (responseTokenField == null) {
        diagCollector.addDiag(Diag.error(SimpleLocation.TOPLEVEL, "Response field missing for page streaming: method = %s, message type = %s, field = %s", method.id(), method.id(), PARAMETER_NEXT_PAGE_TOKEN));
    }
    Schema responseField = method.response().dereference();
    ImmutableList<FieldModel> resourcesFieldPath = ImmutableList.copyOf(getResourcesGetterPath(responseField, apiModel));
    FieldConfig resourcesFieldConfig;
    if (resourcesFieldPath.isEmpty()) {
        diagCollector.addDiag(Diag.error(SimpleLocation.TOPLEVEL, "Resources field missing for page streaming: method = %s, message type = %s, response field = %s", method.id(), method.id(), method.response() == null ? "null" : method.response().toString()));
        resourcesFieldConfig = null;
    } else {
        FieldModel resourcesField = resourcesFieldPath.get(resourcesFieldPath.size() - 1);
        resourcesFieldConfig = FieldConfig.createFieldConfig(resourcesField, ImmutableList.copyOf(resourcesFieldPath));
    }
    if (requestTokenField == null || responseTokenField == null || resourcesFieldConfig == null) {
        return null;
    }
    return new AutoValue_PageStreamingConfig(DiscoveryField.create(requestTokenField, apiModel), DiscoveryField.create(pageSizeField, apiModel), DiscoveryField.create(responseTokenField, apiModel), resourcesFieldConfig);
}
Also used : DiagCollector(com.google.api.tools.framework.model.DiagCollector) Schema(com.google.api.codegen.discovery.Schema) Nullable(javax.annotation.Nullable)

Example 2 with DiagCollector

use of com.google.api.tools.framework.model.DiagCollector in project toolkit by googleapis.

the class DiscoConfigBaselineTestCase method test.

/**
 * Run a test for the given file base name(s). Collects all .proto and .yaml files with the given
 * base name (i.e. baseName.proto or baseName.yaml), constructs model, and calls {@link #run()}.
 * Post that, prints diags and the result of the run to the baseline.
 */
protected void test() throws Exception {
    DiagCollector diagCollector = new BoundedDiagCollector();
    // Run test specific logic.
    Object result = run();
    if (!diagCollector.hasErrors() && result != null) {
        // Output the result depending on its type.
        if (result instanceof Map) {
            @SuppressWarnings("unchecked") Map<String, Object> map = (Map<String, Object>) result;
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                testOutput().printf("============== file: %s ==============%n", entry.getKey());
                testOutput().println(displayValue(entry.getValue()));
            }
        } else {
            testOutput().println(displayValue(result));
        }
    }
}
Also used : BoundedDiagCollector(com.google.api.tools.framework.model.BoundedDiagCollector) DiagCollector(com.google.api.tools.framework.model.DiagCollector) BoundedDiagCollector(com.google.api.tools.framework.model.BoundedDiagCollector) Map(java.util.Map)

Example 3 with DiagCollector

use of com.google.api.tools.framework.model.DiagCollector in project toolkit by googleapis.

the class DiscoGapicMethodConfig method createDiscoGapicMethodConfig.

/**
 * Creates an instance of DiscoGapicMethodConfig based on MethodConfigProto, linking it up with
 * the provided method. On errors, null will be returned, and diagnostics are reported to the diag
 * collector.
 */
@Nullable
static DiscoGapicMethodConfig createDiscoGapicMethodConfig(DiscoApiModel apiModel, TargetLanguage language, MethodConfigProto methodConfigProto, Method method, ResourceNameMessageConfigs messageConfigs, ImmutableMap<String, ResourceNameConfig> resourceNameConfigs, RetryCodesConfig retryCodesConfig, ImmutableSet<String> retryParamsConfigNames) {
    boolean error = false;
    DiscoveryMethodModel methodModel = new DiscoveryMethodModel(method, apiModel);
    DiagCollector diagCollector = apiModel.getDiagCollector();
    PageStreamingConfig pageStreaming = null;
    if (!PageStreamingConfigProto.getDefaultInstance().equals(methodConfigProto.getPageStreaming())) {
        pageStreaming = PageStreamingConfig.createPageStreamingFromGapicConfig(diagCollector, messageConfigs, resourceNameConfigs, methodConfigProto, methodModel);
        if (pageStreaming == null) {
            error = true;
        }
    }
    ImmutableList<FlatteningConfig> flattening = null;
    if (!FlatteningConfigProto.getDefaultInstance().equals(methodConfigProto.getFlattening())) {
        flattening = FlatteningConfig.createFlatteningConfigs(diagCollector, messageConfigs, resourceNameConfigs, methodConfigProto, methodModel);
        if (flattening == null) {
            error = true;
        }
    }
    BatchingConfig batching = null;
    if (!BatchingConfigProto.getDefaultInstance().equals(methodConfigProto.getBatching())) {
        batching = BatchingConfig.createBatching(diagCollector, methodConfigProto.getBatching(), methodModel);
        if (batching == null) {
            error = true;
        }
    }
    String retryCodesName = retryCodesConfig.getMethodRetryNames().get(methodConfigProto.getName());
    String retryParamsName = RetryDefinitionsTransformer.getRetryParamsName(methodConfigProto, diagCollector, retryParamsConfigNames);
    error |= (retryParamsName == null);
    Duration timeout = Duration.ofMillis(methodConfigProto.getTimeoutMillis());
    if (timeout.toMillis() <= 0) {
        diagCollector.addDiag(Diag.error(SimpleLocation.TOPLEVEL, "Default timeout not found or has invalid value (in method %s)", methodModel.getFullName()));
        error = true;
    }
    ImmutableListMultimap<String, String> fieldNamePatterns = ImmutableListMultimap.copyOf(methodConfigProto.getFieldNamePatterns().entrySet());
    ResourceNameTreatment defaultResourceNameTreatment = methodConfigProto.getResourceNameTreatment();
    if (defaultResourceNameTreatment == null || defaultResourceNameTreatment.equals(ResourceNameTreatment.UNSET_TREATMENT)) {
        defaultResourceNameTreatment = ResourceNameTreatment.NONE;
    }
    List<String> requiredFieldsList = Lists.newArrayList(methodConfigProto.getRequiredFieldsList());
    if (methodModel.hasExtraFieldMask()) {
        requiredFieldsList.add(DiscoveryMethodTransformer.FIELDMASK_STRING);
    }
    ImmutableList<FieldConfig> requiredFieldConfigs = createFieldNameConfigs(diagCollector, messageConfigs, defaultResourceNameTreatment, fieldNamePatterns, resourceNameConfigs, getRequiredFields(diagCollector, methodModel, requiredFieldsList));
    ImmutableList<FieldConfig> optionalFieldConfigs = createFieldNameConfigs(diagCollector, messageConfigs, defaultResourceNameTreatment, fieldNamePatterns, resourceNameConfigs, getOptionalFields(methodModel, requiredFieldsList));
    List<String> sampleCodeInitFields = new ArrayList<>();
    sampleCodeInitFields.addAll(methodConfigProto.getSampleCodeInitFieldsList());
    SampleSpec sampleSpec = new SampleSpec(methodConfigProto);
    VisibilityConfig visibility = VisibilityConfig.PUBLIC;
    ReleaseLevel releaseLevel = ReleaseLevel.ALPHA;
    for (SurfaceTreatmentProto treatment : methodConfigProto.getSurfaceTreatmentsList()) {
        if (!treatment.getIncludeLanguagesList().contains(language.toString().toLowerCase())) {
            continue;
        }
        if (treatment.getVisibility() != VisibilityProto.UNSET_VISIBILITY) {
            visibility = VisibilityConfig.fromProto(treatment.getVisibility());
        }
        if (treatment.getReleaseLevel() != ReleaseLevel.UNSET_RELEASE_LEVEL) {
            releaseLevel = treatment.getReleaseLevel();
        }
    }
    LongRunningConfig longRunningConfig = null;
    if (error) {
        return null;
    } else {
        return new AutoValue_DiscoGapicMethodConfig(pageStreaming, flattening, retryCodesName, retryParamsName, timeout, requiredFieldConfigs, optionalFieldConfigs, batching, fieldNamePatterns, sampleCodeInitFields, sampleSpec, visibility, releaseLevel, longRunningConfig, methodModel);
    }
}
Also used : SurfaceTreatmentProto(com.google.api.codegen.SurfaceTreatmentProto) ArrayList(java.util.ArrayList) ResourceNameTreatment(com.google.api.codegen.ResourceNameTreatment) Duration(org.threeten.bp.Duration) ReleaseLevel(com.google.api.codegen.ReleaseLevel) DiagCollector(com.google.api.tools.framework.model.DiagCollector) Nullable(javax.annotation.Nullable)

Example 4 with DiagCollector

use of com.google.api.tools.framework.model.DiagCollector in project toolkit by googleapis.

the class LongRunningConfigTest method testCreateLROWithAnnotationsOverridingGapicConfig.

@Test
public void testCreateLROWithAnnotationsOverridingGapicConfig() {
    DiagCollector diagCollector = new BoundedDiagCollector();
    Mockito.when(protoParser.isProtoAnnotationsEnabled()).thenReturn(true);
    // lroAnnotatedMethod contains different settings than that in lroConfigProtoWithPollSettings.
    LongRunningConfig longRunningConfig = LongRunningConfig.createLongRunningConfig(lroAnnotatedMethod, diagCollector, lroConfigProtoWithPollSettings, protoParser);
    assertThat(diagCollector.getErrorCount()).isEqualTo(0);
    assertThat(longRunningConfig).isNotNull();
    // Assert that proto annotations settings take precendence over gapic config for
    // return and metadata types.
    ProtoTypeRef metadataTypeModel = (ProtoTypeRef) longRunningConfig.getMetadataType();
    assertThat(metadataTypeModel.getProtoType()).isEqualTo(annotationsMetadataType);
    ProtoTypeRef returnTypeModel = (ProtoTypeRef) longRunningConfig.getReturnType();
    assertThat(returnTypeModel.getProtoType()).isEqualTo(annotationsReturnType);
    // Assert that GAPIC config timeout values are used.
    assertThat(longRunningConfig.getInitialPollDelay().toMillis()).isEqualTo(TEST_INITIAL_POLL_DELAY);
    assertThat(longRunningConfig.getMaxPollDelay().toMillis()).isEqualTo(TEST_MAX_POLL_DELAY);
    assertThat(longRunningConfig.getPollDelayMultiplier()).isEqualTo(TEST_POLL_DELAY_MULTIPLIER);
    assertThat(longRunningConfig.getTotalPollTimeout().toMillis()).isEqualTo(TEST_TOTAL_POLL_TIMEOUT);
}
Also used : BoundedDiagCollector(com.google.api.tools.framework.model.BoundedDiagCollector) DiagCollector(com.google.api.tools.framework.model.DiagCollector) BoundedDiagCollector(com.google.api.tools.framework.model.BoundedDiagCollector) Test(org.junit.Test)

Example 5 with DiagCollector

use of com.google.api.tools.framework.model.DiagCollector in project toolkit by googleapis.

the class LongRunningConfigTest method testCreateLROWithoutGapicConfig.

@Test
public void testCreateLROWithoutGapicConfig() {
    Mockito.when(protoParser.isProtoAnnotationsEnabled()).thenReturn(true);
    DiagCollector diagCollector = new BoundedDiagCollector();
    LongRunningConfig longRunningConfig = LongRunningConfig.createLongRunningConfig(lroAnnotatedMethod, diagCollector, LongRunningConfigProto.getDefaultInstance(), protoParser);
    assertThat(diagCollector.getErrorCount()).isEqualTo(0);
    assertThat(longRunningConfig).isNotNull();
    ProtoTypeRef metadataTypeModel = (ProtoTypeRef) longRunningConfig.getMetadataType();
    assertThat(metadataTypeModel.getProtoType()).isEqualTo(annotationsMetadataType);
    ProtoTypeRef returnTypeModel = (ProtoTypeRef) longRunningConfig.getReturnType();
    assertThat(returnTypeModel.getProtoType()).isEqualTo(annotationsReturnType);
    assertThat(longRunningConfig.getInitialPollDelay()).isEqualTo(LongRunningConfig.LRO_INITIAL_POLL_DELAY_MILLIS);
    assertThat(longRunningConfig.getMaxPollDelay()).isEqualTo(LongRunningConfig.LRO_MAX_POLL_DELAY_MILLIS);
    assertThat(longRunningConfig.getPollDelayMultiplier()).isEqualTo(LongRunningConfig.LRO_POLL_DELAY_MULTIPLIER);
    assertThat(longRunningConfig.getTotalPollTimeout()).isEqualTo(LongRunningConfig.LRO_TOTAL_POLL_TIMEOUT_MILLS);
}
Also used : BoundedDiagCollector(com.google.api.tools.framework.model.BoundedDiagCollector) DiagCollector(com.google.api.tools.framework.model.DiagCollector) BoundedDiagCollector(com.google.api.tools.framework.model.BoundedDiagCollector) Test(org.junit.Test)

Aggregations

DiagCollector (com.google.api.tools.framework.model.DiagCollector)17 BoundedDiagCollector (com.google.api.tools.framework.model.BoundedDiagCollector)11 Test (org.junit.Test)10 ConfigProto (com.google.api.codegen.ConfigProto)3 ResourceNameTreatment (com.google.api.codegen.ResourceNameTreatment)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 CollectionConfigProto (com.google.api.codegen.CollectionConfigProto)2 FlatteningConfigProto (com.google.api.codegen.FlatteningConfigProto)2 InterfaceConfigProto (com.google.api.codegen.InterfaceConfigProto)2 MethodConfigProto (com.google.api.codegen.MethodConfigProto)2 ReleaseLevel (com.google.api.codegen.ReleaseLevel)2 ResourceNameMessageConfigProto (com.google.api.codegen.ResourceNameMessageConfigProto)2 SurfaceTreatmentProto (com.google.api.codegen.SurfaceTreatmentProto)2 SimpleDiagCollector (com.google.api.tools.framework.model.SimpleDiagCollector)2 ImmutableList (com.google.common.collect.ImmutableList)2 File (java.io.File)2 Nullable (javax.annotation.Nullable)2 ResourceDescriptor (com.google.api.ResourceDescriptor)1 ResourceReference (com.google.api.ResourceReference)1