Search in sources :

Example 1 with MockRpcResponseView

use of com.google.api.codegen.viewmodel.testing.MockRpcResponseView 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)

Aggregations

FieldModel (com.google.api.codegen.config.FieldModel)1 MethodConfig (com.google.api.codegen.config.MethodConfig)1 MethodModel (com.google.api.codegen.config.MethodModel)1 InitCodeContext (com.google.api.codegen.metacode.InitCodeContext)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