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();
}
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();
}
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);
}
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;
}
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();
}
Aggregations