Search in sources :

Example 1 with InitCodeContext

use of com.google.api.codegen.metacode.InitCodeContext in project toolkit by googleapis.

the class TestCaseTransformer method createTestCaseView.

public TestCaseView createTestCaseView(MethodContext methodContext, SymbolTable testNameTable, InitCodeContext initCodeContext, ClientMethodType clientMethodType, Synchronicity synchronicity, InitCodeContext requestObjectInitCodeContext) {
    MethodModel method = methodContext.getMethodModel();
    MethodConfig methodConfig = methodContext.getMethodConfig();
    SurfaceNamer namer = methodContext.getNamer();
    ImportTypeTable typeTable = methodContext.getTypeTable();
    String clientMethodName;
    String responseTypeName;
    String callerResponseTypeName;
    String fullyQualifiedResponseTypeName = methodContext.getMethodModel().getOutputTypeName(typeTable).getFullName();
    if (methodConfig.isPageStreaming()) {
        clientMethodName = namer.getApiMethodName(method, methodConfig.getVisibility());
        responseTypeName = namer.getAndSavePagedResponseTypeName(methodContext, methodConfig.getPageStreaming().getResourcesFieldConfig());
        callerResponseTypeName = namer.getAndSaveCallerPagedResponseTypeName(methodContext, methodConfig.getPageStreaming().getResourcesFieldConfig());
    } else if (methodConfig.isLongRunningOperation()) {
        clientMethodName = namer.getLroApiMethodName(method, methodConfig.getVisibility());
        responseTypeName = methodContext.getTypeTable().getAndSaveNicknameFor(methodConfig.getLongRunningConfig().getReturnType());
        callerResponseTypeName = responseTypeName;
        fullyQualifiedResponseTypeName = methodContext.getTypeTable().getFullNameFor(methodConfig.getLongRunningConfig().getReturnType());
    } else if (clientMethodType == ClientMethodType.CallableMethod) {
        clientMethodName = namer.getCallableMethodName(method);
        responseTypeName = method.getAndSaveResponseTypeName(typeTable, namer);
        callerResponseTypeName = responseTypeName;
    } else {
        clientMethodName = synchronicity == Synchronicity.Sync ? namer.getApiMethodName(method, methodConfig.getVisibility()) : namer.getAsyncApiMethodName(method, methodConfig.getVisibility());
        responseTypeName = method.getAndSaveResponseTypeName(typeTable, namer);
        callerResponseTypeName = responseTypeName;
    }
    InitCodeView initCode = initCodeTransformer.generateInitCode(methodContext, initCodeContext);
    InitCodeView requestObjectInitCode = requestObjectInitCodeContext != null ? initCodeTransformer.generateInitCode(methodContext, requestObjectInitCodeContext) : null;
    boolean hasRequestParameters = initCode.lines().size() > 0;
    boolean hasReturnValue = !method.isOutputTypeEmpty();
    if (methodConfig.isLongRunningOperation()) {
        hasReturnValue = !methodConfig.getLongRunningConfig().getReturnType().isEmptyType();
    }
    InitCodeContext responseInitCodeContext = createResponseInitCodeContext(methodContext, initCodeContext.symbolTable());
    MockRpcResponseView mockRpcResponseView = createMockResponseView(methodContext, responseInitCodeContext);
    GrpcStreamingView grpcStreamingView = null;
    if (methodConfig.isGrpcStreaming()) {
        String resourceTypeName = null;
        String resourcesFieldGetterName = null;
        if (methodConfig.getGrpcStreaming().hasResourceField()) {
            FieldModel resourcesField = methodConfig.getGrpcStreaming().getResourcesField();
            resourceTypeName = methodContext.getTypeTable().getAndSaveNicknameForElementType(resourcesField);
            resourcesFieldGetterName = namer.getFieldGetFunctionName(resourcesField, Name.from(resourcesField.getSimpleName()));
        }
        grpcStreamingView = GrpcStreamingView.newBuilder().resourceTypeName(resourceTypeName).resourcesFieldGetterName(resourcesFieldGetterName).requestInitCodeList(createGrpcStreamingInitCodeViews(methodContext, initCodeContext, initCode)).responseInitCodeList(createGrpcStreamingInitCodeViews(methodContext, responseInitCodeContext, mockRpcResponseView.initCode())).build();
    }
    return TestCaseView.newBuilder().asserts(initCodeTransformer.generateRequestAssertViews(methodContext, initCodeContext)).clientMethodType(clientMethodType).grpcStreamingType(methodConfig.getGrpcStreamingType()).hasRequestParameters(hasRequestParameters).hasReturnValue(hasReturnValue).initCode(initCode).requestObjectInitCode(requestObjectInitCode).mockResponse(mockRpcResponseView).mockServiceVarName(namer.getMockServiceVarName(methodContext.getTargetInterface())).name(synchronicity == Synchronicity.Sync ? namer.getTestCaseName(testNameTable, method) : namer.getAsyncTestCaseName(testNameTable, method)).nameWithException(namer.getExceptionTestCaseName(testNameTable, method)).pageStreamingResponseViews(createPageStreamingResponseViews(methodContext)).grpcStreamingView(grpcStreamingView).requestTypeName(method.getAndSaveRequestTypeName(typeTable, namer)).responseTypeName(responseTypeName).callerResponseTypeName(callerResponseTypeName).fullyQualifiedRequestTypeName(method.getInputTypeName(typeTable).getFullName()).fullyQualifiedResponseTypeName(fullyQualifiedResponseTypeName).serviceConstructorName(namer.getApiWrapperClassConstructorName(methodContext.getInterfaceConfig())).fullyQualifiedServiceClassName(namer.getFullyQualifiedApiWrapperClassName(methodContext.getInterfaceConfig())).fullyQualifiedAliasedServiceClassName(namer.getTopLevelAliasedApiClassName((methodContext.getInterfaceConfig()), packageHasMultipleServices)).clientMethodName(clientMethodName).mockGrpcStubTypeName(namer.getMockGrpcServiceImplName(methodContext.getTargetInterface())).createStubFunctionName(namer.getCreateStubFunctionName(methodContext.getTargetInterface())).grpcStubCallString(namer.getGrpcStubCallString(methodContext.getTargetInterface(), method)).clientHasDefaultInstance(methodContext.getInterfaceConfig().hasDefaultInstance()).methodDescriptor(getMethodDescriptorName(methodContext)).grpcMethodName(synchronicity == Synchronicity.Sync ? namer.getGrpcMethodName(method) : namer.getAsyncGrpcMethodName(method)).build();
}
Also used : MethodConfig(com.google.api.codegen.config.MethodConfig) MethodModel(com.google.api.codegen.config.MethodModel) MockRpcResponseView(com.google.api.codegen.viewmodel.testing.MockRpcResponseView) GrpcStreamingView(com.google.api.codegen.viewmodel.testing.GrpcStreamingView) FieldModel(com.google.api.codegen.config.FieldModel) InitCodeContext(com.google.api.codegen.metacode.InitCodeContext) InitCodeView(com.google.api.codegen.viewmodel.InitCodeView)

Example 2 with InitCodeContext

use of com.google.api.codegen.metacode.InitCodeContext in project toolkit by googleapis.

the class TestCaseTransformer method createSmokeTestInitContext.

public InitCodeContext createSmokeTestInitContext(MethodContext context) {
    SmokeTestConfig testConfig = context.getInterfaceConfig().getSmokeTestConfig();
    InitCodeContext.InitCodeOutputType outputType;
    ImmutableMap<String, FieldConfig> fieldConfigMap;
    if (context.isFlattenedMethodContext()) {
        outputType = InitCodeContext.InitCodeOutputType.FieldList;
        fieldConfigMap = FieldConfig.toFieldConfigMap(context.getFlatteningConfig().getFlattenedFieldConfigs().values());
    } else {
        outputType = InitCodeContext.InitCodeOutputType.SingleObject;
        fieldConfigMap = FieldConfig.toFieldConfigMap(context.getMethodConfig().getRequiredFieldConfigs());
    }
    // Store project ID variable name into the symbol table since it is used
    // by the execute method as a parameter.
    SymbolTable table = new SymbolTable();
    table.getNewSymbol(Name.from(InitFieldConfig.PROJECT_ID_VARIABLE_NAME));
    InitCodeContext.Builder contextBuilder = InitCodeContext.newBuilder().initObjectType(testConfig.getMethod().getInputType()).suggestedName(Name.from("request")).outputType(outputType).initValueConfigMap(InitCodeTransformer.createCollectionMap(context)).initFieldConfigStrings(testConfig.getInitFieldConfigStrings()).symbolTable(table).fieldConfigMap(fieldConfigMap);
    if (context.isFlattenedMethodContext()) {
        contextBuilder.initFields(context.getFlatteningConfig().getFlattenedFields());
    }
    return contextBuilder.build();
}
Also used : InitFieldConfig(com.google.api.codegen.metacode.InitFieldConfig) FieldConfig(com.google.api.codegen.config.FieldConfig) SmokeTestConfig(com.google.api.codegen.config.SmokeTestConfig) SymbolTable(com.google.api.codegen.util.SymbolTable) InitCodeContext(com.google.api.codegen.metacode.InitCodeContext)

Example 3 with InitCodeContext

use of com.google.api.codegen.metacode.InitCodeContext in project toolkit by googleapis.

the class CSharpGapicUnitTestTransformer method createFlattenedTestCase.

private TestCaseView createFlattenedTestCase(MethodContext methodContext, SymbolTable testNameTable, Iterable<FieldConfig> fieldConfigs, ClientMethodType clientMethodType, Synchronicity synchronicity) {
    InitCodeContext initCodeContext = initCodeTransformer.createRequestInitCodeContext(methodContext, new SymbolTable(), fieldConfigs, InitCodeOutputType.FieldList, valueGenerator);
    InitCodeContext initCodeRequestObjectContext = InitCodeContext.newBuilder().initObjectType(methodContext.getMethodModel().getInputType()).symbolTable(new SymbolTable()).suggestedName(Name.from("expected_request")).initFieldConfigStrings(methodContext.getMethodConfig().getSampleCodeInitFields()).initValueConfigMap(InitCodeTransformer.createCollectionMap(methodContext)).initFields(FieldConfig.toFieldTypeIterable(fieldConfigs)).fieldConfigMap(methodContext.getProductConfig().getDefaultResourceNameFieldConfigMap()).outputType(InitCodeOutputType.SingleObject).valueGenerator(valueGenerator).build();
    return testCaseTransformer.createTestCaseView(methodContext, testNameTable, initCodeContext, clientMethodType, synchronicity, initCodeRequestObjectContext);
}
Also used : SymbolTable(com.google.api.codegen.util.SymbolTable) InitCodeContext(com.google.api.codegen.metacode.InitCodeContext)

Example 4 with InitCodeContext

use of com.google.api.codegen.metacode.InitCodeContext in project toolkit by googleapis.

the class GoGapicSurfaceTestTransformer method createTestCaseViews.

private List<TestCaseView> createTestCaseViews(GapicInterfaceContext context) {
    ArrayList<TestCaseView> testCaseViews = new ArrayList<>();
    SymbolTable testNameTable = new SymbolTable();
    for (MethodModel method : context.getSupportedMethods()) {
        GapicMethodContext methodContext = context.asRequestMethodContext(method);
        ClientMethodType clientMethodType = ClientMethodType.RequestObjectMethod;
        if (methodContext.getMethodConfig().isPageStreaming()) {
            clientMethodType = ClientMethodType.PagedRequestObjectMethod;
        } else if (methodContext.getMethodConfig().isLongRunningOperation()) {
            clientMethodType = ClientMethodType.OperationRequestObjectMethod;
        }
        InitCodeContext initCodeContext = initCodeTransformer.createRequestInitCodeContext(methodContext, new SymbolTable(), methodContext.getMethodConfig().getRequiredFieldConfigs(), InitCodeOutputType.SingleObject, valueGenerator);
        testCaseViews.add(testCaseTransformer.createTestCaseView(methodContext, testNameTable, initCodeContext, clientMethodType));
    }
    return testCaseViews;
}
Also used : MethodModel(com.google.api.codegen.config.MethodModel) GapicMethodContext(com.google.api.codegen.transformer.GapicMethodContext) ClientMethodType(com.google.api.codegen.viewmodel.ClientMethodType) TestCaseView(com.google.api.codegen.viewmodel.testing.TestCaseView) ArrayList(java.util.ArrayList) SymbolTable(com.google.api.codegen.util.SymbolTable) InitCodeContext(com.google.api.codegen.metacode.InitCodeContext)

Example 5 with InitCodeContext

use of com.google.api.codegen.metacode.InitCodeContext in project toolkit by googleapis.

the class PythonGapicSurfaceTestTransformer method createTestCaseViews.

private List<TestCaseView> createTestCaseViews(GapicInterfaceContext context) {
    ImmutableList.Builder<TestCaseView> testCaseViews = ImmutableList.builder();
    // certain string until all of the types have been disambiguated.
    for (int i = 0; i < 2; ++i) {
        testCaseViews = ImmutableList.builder();
        SymbolTable testNameTable = new SymbolTable();
        for (MethodModel method : context.getSupportedMethods()) {
            GapicMethodContext methodContext = context.asRequestMethodContext(method);
            ClientMethodType clientMethodType = ClientMethodType.OptionalArrayMethod;
            if (methodContext.getMethodConfig().isLongRunningOperation()) {
                clientMethodType = ClientMethodType.OperationOptionalArrayMethod;
            } else if (methodContext.getMethodConfig().isPageStreaming()) {
                clientMethodType = ClientMethodType.PagedOptionalArrayMethod;
            }
            Iterable<FieldConfig> fieldConfigs = methodContext.getMethodConfig().getRequiredFieldConfigs();
            InitCodeOutputType initCodeOutputType = method.getRequestStreaming() ? InitCodeOutputType.SingleObject : InitCodeOutputType.FieldList;
            InitCodeContext initCodeContext = InitCodeContext.newBuilder().initObjectType(methodContext.getMethodModel().getInputType()).suggestedName(Name.from("request")).initFieldConfigStrings(methodContext.getMethodConfig().getSampleCodeInitFields()).initValueConfigMap(InitCodeTransformer.createCollectionMap(methodContext)).initFields(FieldConfig.toFieldTypeIterable(fieldConfigs)).outputType(initCodeOutputType).fieldConfigMap(FieldConfig.toFieldConfigMap(fieldConfigs)).valueGenerator(valueGenerator).build();
            testCaseViews.add(testCaseTransformer.createTestCaseView(methodContext, testNameTable, initCodeContext, clientMethodType));
        }
    }
    return testCaseViews.build();
}
Also used : MethodModel(com.google.api.codegen.config.MethodModel) GapicMethodContext(com.google.api.codegen.transformer.GapicMethodContext) ClientMethodType(com.google.api.codegen.viewmodel.ClientMethodType) TestCaseView(com.google.api.codegen.viewmodel.testing.TestCaseView) ImmutableList(com.google.common.collect.ImmutableList) FieldConfig(com.google.api.codegen.config.FieldConfig) SymbolTable(com.google.api.codegen.util.SymbolTable) InitCodeContext(com.google.api.codegen.metacode.InitCodeContext) InitCodeOutputType(com.google.api.codegen.metacode.InitCodeContext.InitCodeOutputType)

Aggregations

InitCodeContext (com.google.api.codegen.metacode.InitCodeContext)8 SymbolTable (com.google.api.codegen.util.SymbolTable)7 MethodModel (com.google.api.codegen.config.MethodModel)6 TestCaseView (com.google.api.codegen.viewmodel.testing.TestCaseView)5 FieldConfig (com.google.api.codegen.config.FieldConfig)4 GapicMethodContext (com.google.api.codegen.transformer.GapicMethodContext)4 ClientMethodType (com.google.api.codegen.viewmodel.ClientMethodType)4 ArrayList (java.util.ArrayList)4 MethodConfig (com.google.api.codegen.config.MethodConfig)2 InitCodeOutputType (com.google.api.codegen.metacode.InitCodeContext.InitCodeOutputType)2 FieldModel (com.google.api.codegen.config.FieldModel)1 FlatteningConfig (com.google.api.codegen.config.FlatteningConfig)1 SmokeTestConfig (com.google.api.codegen.config.SmokeTestConfig)1 InitFieldConfig (com.google.api.codegen.metacode.InitFieldConfig)1 MethodContext (com.google.api.codegen.transformer.MethodContext)1 InitCodeView (com.google.api.codegen.viewmodel.InitCodeView)1 GrpcStreamingView (com.google.api.codegen.viewmodel.testing.GrpcStreamingView)1 MockRpcResponseView (com.google.api.codegen.viewmodel.testing.MockRpcResponseView)1 ImmutableList (com.google.common.collect.ImmutableList)1