use of com.google.api.generator.gapic.model.Method in project gapic-generator-java by googleapis.
the class ServiceClientHeaderSampleComposerTest method valid_composeShowcaseMethodSample_pagedRpcWithMultipleMethodArguments.
/*Testing composeShowcaseMethodSample*/
@Test
public void valid_composeShowcaseMethodSample_pagedRpcWithMultipleMethodArguments() {
Descriptors.FileDescriptor echoFileDescriptor = EchoOuterClass.getDescriptor();
Map<String, ResourceName> resourceNames = Parser.parseResourceNames(echoFileDescriptor);
Map<String, Message> messageTypes = Parser.parseMessages(echoFileDescriptor);
TypeNode clientType = TypeNode.withReference(VaporReference.builder().setName("EchoClient").setPakkage(SHOWCASE_PACKAGE_NAME).build());
TypeNode inputType = TypeNode.withReference(VaporReference.builder().setName("ListContentRequest").setPakkage(SHOWCASE_PACKAGE_NAME).build());
TypeNode outputType = TypeNode.withReference(VaporReference.builder().setName("ListContentResponse").setPakkage(SHOWCASE_PACKAGE_NAME).build());
TypeNode resourceNameType = TypeNode.withReference(ConcreteReference.builder().setClazz(List.class).setGenerics(ConcreteReference.withClazz(String.class)).build());
List<MethodArgument> arguments = Arrays.asList(MethodArgument.builder().setName("resourceName").setType(resourceNameType).setField(Field.builder().setName("resourceName").setType(resourceNameType).setIsRepeated(true).build()).build(), MethodArgument.builder().setName("filter").setType(TypeNode.STRING).setField(Field.builder().setName("filter").setType(TypeNode.STRING).build()).build());
Method method = Method.builder().setName("ListContent").setMethodSignatures(Arrays.asList(arguments)).setInputType(inputType).setOutputType(outputType).setPageSizeFieldName(PAGINATED_FIELD_NAME).build();
Reference repeatedResponseReference = VaporReference.builder().setName("Content").setPakkage(SHOWCASE_PACKAGE_NAME).build();
Field repeatedField = Field.builder().setName("responses").setType(TypeNode.withReference(ConcreteReference.builder().setClazz(List.class).setGenerics(repeatedResponseReference).build())).setIsMessage(true).setIsRepeated(true).build();
Field nextPagedTokenField = Field.builder().setName("next_page_token").setType(TypeNode.STRING).build();
Message listContentResponseMessage = Message.builder().setName("ListContentResponse").setFullProtoName("google.showcase.v1beta1.ListContentResponse").setType(outputType).setFields(Arrays.asList(repeatedField, nextPagedTokenField)).build();
messageTypes.put("com.google.showcase.v1beta1.ListContentResponse", listContentResponseMessage);
String results = writeStatements(ServiceClientHeaderSampleComposer.composeShowcaseMethodSample(method, clientType, arguments, resourceNames, messageTypes));
String expected = LineFormatter.lines("try (EchoClient echoClient = EchoClient.create()) {\n", " List<String> resourceName = new ArrayList<>();\n", " String filter = \"filter-1274492040\";\n", " for (Content element : echoClient.listContent(resourceName, filter).iterateAll())" + " {\n", " // doThingsWith(element);\n", " }\n", "}");
Assert.assertEquals(results, expected);
}
use of com.google.api.generator.gapic.model.Method in project gapic-generator-java by googleapis.
the class AbstractServiceClientClassComposer method createNestedRpcPageClass.
private static ClassDefinition createNestedRpcPageClass(Method method, TypeNode repeatedResponseType, Map<String, Message> messageTypes, TypeStore typeStore) {
Preconditions.checkState(method.isPaged(), String.format("Expected method %s to be paged", method.name()));
String upperJavaMethodName = JavaStyle.toUpperCamelCase(method.name());
String className = String.format("%sPage", upperJavaMethodName);
TypeNode classType = typeStore.get(className);
TypeNode classExtendsType = TypeNode.withReference(ConcreteReference.builder().setClazz(AbstractPage.class).setGenerics(Arrays.asList(method.inputType(), method.outputType(), repeatedResponseType, classType).stream().map(t -> t.reference()).collect(Collectors.toList())).build());
// Private constructor.
VariableExpr contextVarExpr = VariableExpr.withVariable(Variable.builder().setName("context").setType(TypeNode.withReference(ConcreteReference.builder().setClazz(PageContext.class).setGenerics(Arrays.asList(method.inputType(), method.outputType(), repeatedResponseType).stream().map(t -> t.reference()).collect(Collectors.toList())).build())).build());
VariableExpr responseVarExpr = VariableExpr.withVariable(Variable.builder().setName("response").setType(method.outputType()).build());
MethodDefinition privateCtor = MethodDefinition.constructorBuilder().setScope(ScopeNode.PRIVATE).setReturnType(classType).setArguments(Arrays.asList(contextVarExpr, responseVarExpr).stream().map(e -> e.toBuilder().setIsDecl(true).build()).collect(Collectors.toList())).setBody(Arrays.asList(ExprStatement.withExpr(ReferenceConstructorExpr.superBuilder().setType(classExtendsType).setArguments(contextVarExpr, responseVarExpr).build()))).build();
// createEmptyPage method.
ValueExpr nullExpr = ValueExpr.createNullExpr();
MethodDefinition createEmptyPageMethod = MethodDefinition.builder().setScope(ScopeNode.PRIVATE).setIsStatic(true).setReturnType(classType).setName("createEmptyPage").setReturnExpr(NewObjectExpr.builder().setType(classType).setArguments(nullExpr, nullExpr).build()).build();
// createPage method.
MethodDefinition createPageMethod = MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PROTECTED).setReturnType(classType).setName("createPage").setArguments(Arrays.asList(contextVarExpr, responseVarExpr).stream().map(e -> e.toBuilder().setIsDecl(true).build()).collect(Collectors.toList())).setReturnExpr(NewObjectExpr.builder().setType(classType).setArguments(contextVarExpr, responseVarExpr).build()).build();
// createPageAsync method.
Function<TypeNode, TypeNode> futureTypeFn = t -> TypeNode.withReference(ConcreteReference.builder().setClazz(ApiFuture.class).setGenerics(Arrays.asList(t.reference())).build());
VariableExpr futureResponseVarExpr = VariableExpr.withVariable(Variable.builder().setName("futureResponse").setType(futureTypeFn.apply(method.outputType())).build());
TypeNode futurePageType = futureTypeFn.apply(classType);
MethodDefinition createPageAsyncMethod = MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PUBLIC).setReturnType(futurePageType).setName("createPageAsync").setArguments(Arrays.asList(contextVarExpr, futureResponseVarExpr).stream().map(e -> e.toBuilder().setIsDecl(true).build()).collect(Collectors.toList())).setReturnExpr(MethodInvocationExpr.builder().setExprReferenceExpr(ValueExpr.withValue(SuperObjectValue.withType(classExtendsType))).setMethodName("createPageAsync").setArguments(contextVarExpr, futureResponseVarExpr).setReturnType(futurePageType).build()).build();
// Build the class.
List<MethodDefinition> javaMethods = new ArrayList<>();
javaMethods.add(privateCtor);
javaMethods.add(createEmptyPageMethod);
javaMethods.add(createPageMethod);
javaMethods.add(createPageAsyncMethod);
return ClassDefinition.builder().setIsNested(true).setScope(ScopeNode.PUBLIC).setIsStatic(true).setExtendsType(classExtendsType).setName(className).setMethods(javaMethods).build();
}
use of com.google.api.generator.gapic.model.Method in project gapic-generator-java by googleapis.
the class AbstractServiceClientClassComposer method createVaporTypes.
private static void createVaporTypes(Service service, TypeStore typeStore) {
// Client stub typeStore.
typeStore.putAll(String.format("%s.stub", service.pakkage()), Arrays.asList(ClassNames.getServiceStubClassName(service), ClassNames.getServiceStubSettingsClassName(service)));
// Client ServiceClient and ServiceSettings typeStore.
typeStore.putAll(service.pakkage(), Arrays.asList(ClassNames.getServiceClientClassName(service), ClassNames.getServiceSettingsClassName(service)));
// Nested class typeStore.
for (Method method : service.methods()) {
if (!method.isPaged()) {
continue;
}
typeStore.putAll(service.pakkage(), Arrays.asList("%sPagedResponse", "%sPage", "%sFixedSizeCollection").stream().map(p -> String.format(p, JavaStyle.toUpperCamelCase(method.name()))).collect(Collectors.toList()), true, ClassNames.getServiceClientClassName(service));
}
// Pagination types.
typeStore.putAll(service.pakkage(), service.methods().stream().filter(m -> m.isPaged()).map(m -> String.format(PAGED_RESPONSE_TYPE_NAME_PATTERN, m.name())).collect(Collectors.toList()), true, ClassNames.getServiceClientClassName(service));
}
use of com.google.api.generator.gapic.model.Method in project gapic-generator-java by googleapis.
the class AbstractServiceClientTestClassComposer method addDynamicTypes.
private void addDynamicTypes(GapicContext context, Service service, TypeStore typeStore) {
typeStore.putAll(service.pakkage(), Arrays.asList(ClassNames.getMockServiceClassName(service), ClassNames.getServiceClientClassName(service), ClassNames.getServiceSettingsClassName(service)));
String stubPakkage = String.format("%s.stub", service.pakkage());
typeStore.put(stubPakkage, getTransportContext().classNames().getTransportServiceStubClassName(service));
// Pagination types.
typeStore.putAll(service.pakkage(), service.methods().stream().filter(m -> m.isPaged()).map(m -> String.format(PAGED_RESPONSE_TYPE_NAME_PATTERN, m.name())).collect(Collectors.toList()), true, ClassNames.getServiceClientClassName(service));
for (Service mixinService : context.mixinServices()) {
typeStore.put(mixinService.pakkage(), ClassNames.getMockServiceClassName(mixinService));
for (Method mixinMethod : mixinService.methods()) {
if (!mixinMethod.isPaged()) {
continue;
}
typeStore.put(service.pakkage(), String.format(PAGED_RESPONSE_TYPE_NAME_PATTERN, mixinMethod.name()), true, ClassNames.getServiceClientClassName(service));
}
}
}
use of com.google.api.generator.gapic.model.Method in project gapic-generator-java by googleapis.
the class AbstractServiceClientTestClassComposer method createTestMethods.
private List<MethodDefinition> createTestMethods(Service service, GapicContext context, Map<String, VariableExpr> classMemberVarExprs, Map<String, ResourceName> resourceNames) {
Map<String, Message> messageTypes = context.messages();
List<MethodDefinition> javaMethods = new ArrayList<>();
for (Method method : service.methods()) {
Service matchingService = service;
if (method.isMixin()) {
int dotIndex = method.mixedInApiName().lastIndexOf(".");
String mixinServiceName = method.mixedInApiName().substring(dotIndex + 1);
String mixinServiceProtoPackage = method.mixedInApiName().substring(0, dotIndex);
Optional<Service> mixinServiceOpt = context.mixinServices().stream().filter(s -> s.name().equals(mixinServiceName) && s.protoPakkage().equals(mixinServiceProtoPackage)).findFirst();
if (mixinServiceOpt.isPresent()) {
matchingService = mixinServiceOpt.get();
}
}
// Ignore variants for streaming methods as well.
if (method.methodSignatures().isEmpty() || !method.stream().equals(Method.Stream.NONE)) {
javaMethods.add(createRpcTestMethod(method, service, matchingService, Collections.emptyList(), 0, true, classMemberVarExprs, resourceNames, messageTypes));
javaMethods.add(createRpcExceptionTestMethod(method, matchingService, Collections.emptyList(), 0, classMemberVarExprs, resourceNames, messageTypes));
} else {
for (int i = 0; i < method.methodSignatures().size(); i++) {
javaMethods.add(createRpcTestMethod(method, service, matchingService, method.methodSignatures().get(i), i, false, classMemberVarExprs, resourceNames, messageTypes));
javaMethods.add(createRpcExceptionTestMethod(method, matchingService, method.methodSignatures().get(i), i, classMemberVarExprs, resourceNames, messageTypes));
}
}
}
return javaMethods;
}
Aggregations