diff --git a/src/main/java/com/google/api/generator/gapic/composer/ServiceClientClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/ServiceClientClassComposer.java index 939381b818..8b5862d029 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/ServiceClientClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/ServiceClientClassComposer.java @@ -479,13 +479,14 @@ private static List createServiceMethods( Map messageTypes, Map types, Map resourceNames) { + String clientName = getClientClassName(service); List javaMethods = new ArrayList<>(); for (Method method : service.methods()) { if (method.stream().equals(Stream.NONE)) { javaMethods.addAll( - createMethodVariants( - method, getClientClassName(service), messageTypes, types, resourceNames)); - javaMethods.add(createMethodDefaultMethod(method, types)); + createMethodVariants(method, clientName, messageTypes, types, resourceNames)); + javaMethods.add( + createMethodDefaultMethod(method, clientName, messageTypes, types, resourceNames)); } if (method.hasLro()) { javaMethods.add(createLroCallableMethod(service, method, types)); @@ -594,7 +595,11 @@ private static List createMethodVariants( } private static MethodDefinition createMethodDefaultMethod( - Method method, Map types) { + Method method, + String clientName, + Map messageTypes, + Map types, + Map resourceNames) { String methodName = JavaStyle.toLowerCamelCase(method.name()); TypeNode methodInputType = method.inputType(); TypeNode methodOutputType = @@ -627,6 +632,11 @@ private static MethodDefinition createMethodDefaultMethod( callableMethodName = String.format(OPERATION_CALLABLE_NAME_PATTERN, methodName); } + Optional defaultMethodSampleCode = + Optional.of( + ServiceClientSampleCodeComposer.composeRpcDefaultMethodHeaderSampleCode( + method, types.get(clientName), resourceNames, messageTypes)); + MethodInvocationExpr callableMethodExpr = MethodInvocationExpr.builder().setMethodName(callableMethodName).build(); callableMethodExpr = @@ -639,7 +649,8 @@ private static MethodDefinition createMethodDefaultMethod( MethodDefinition.Builder methodBuilder = MethodDefinition.builder() .setHeaderCommentStatements( - ServiceClientCommentComposer.createRpcMethodHeaderComment(method)) + ServiceClientCommentComposer.createRpcMethodHeaderComment( + method, defaultMethodSampleCode)) .setScope(ScopeNode.PUBLIC) .setIsFinal(true) .setName(String.format(method.hasLro() ? "%sAsync" : "%s", methodName)) diff --git a/src/main/java/com/google/api/generator/gapic/composer/ServiceClientCommentComposer.java b/src/main/java/com/google/api/generator/gapic/composer/ServiceClientCommentComposer.java index 3601e380b2..6ef2eb4ffd 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/ServiceClientCommentComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/ServiceClientCommentComposer.java @@ -199,9 +199,9 @@ static List createRpcMethodHeaderComment( return comments; } - static List createRpcMethodHeaderComment(Method method) { - // TODO(summerji): Refactor this method when implement default method sample code. - return createRpcMethodHeaderComment(method, Collections.emptyList(), Optional.empty()); + static List createRpcMethodHeaderComment( + Method method, Optional sampleCode) { + return createRpcMethodHeaderComment(method, Collections.emptyList(), sampleCode); } static CommentStatement createMethodNoArgComment(String serviceName) { diff --git a/src/main/java/com/google/api/generator/gapic/composer/ServiceClientSampleCodeComposer.java b/src/main/java/com/google/api/generator/gapic/composer/ServiceClientSampleCodeComposer.java index 52b99d62ef..b60c329ab5 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/ServiceClientSampleCodeComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/ServiceClientSampleCodeComposer.java @@ -36,8 +36,8 @@ import com.google.api.generator.gapic.model.MethodArgument; import com.google.api.generator.gapic.model.ResourceName; import com.google.api.generator.gapic.utils.JavaStyle; -import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Map; @@ -194,121 +194,166 @@ public static String composeRpcMethodHeaderSampleCode( List arguments, Map resourceNames, Map messageTypes) { - // TODO(summerji): Add other types RPC methods' sample code. + VariableExpr clientVarExpr = + VariableExpr.withVariable( + Variable.builder() + .setName(JavaStyle.toLowerCamelCase(clientType.reference().name())) + .setType(clientType) + .build()); + + // Assign method's arguments variable with the default values. + List rpcMethodArgVarExprs = createRpcMethodArgumentVariableExprs(arguments); + List rpcMethodArgDefaultValueExprs = + createRpcMethodArgumentDefaultValueExprs(arguments, resourceNames); + List rpcMethodArgAssignmentExprs = + createAssignmentsForVarExprsWithValueExprs( + rpcMethodArgVarExprs, rpcMethodArgDefaultValueExprs); + + List bodyExprs = new ArrayList<>(); + bodyExprs.addAll(rpcMethodArgAssignmentExprs); + + List bodyStatements = new ArrayList<>(); if (method.isPaged()) { - // Find the repeated field. - Message methodOutputMessage = messageTypes.get(method.outputType().reference().simpleName()); - Field repeatedPagedResultsField = methodOutputMessage.findAndUnwrapFirstRepeatedField(); - Preconditions.checkNotNull( - repeatedPagedResultsField, - String.format( - "No repeated field found on message %s for method %s", - methodOutputMessage.name(), method.name())); - - TypeNode repeatedResponseType = repeatedPagedResultsField.type(); - return SampleCodeWriter.write( - composeUnaryPagedRpcMethodSampleCode( - method, clientType, repeatedResponseType, arguments, resourceNames)); - } - if (method.hasLro()) { - return SampleCodeWriter.write( - composeUnaryLroRpcMethodSampleCode(method, clientType, arguments, resourceNames)); + bodyStatements.addAll( + composeUnaryPagedRpcMethodSampleCodeBodyStatements( + method, clientVarExpr, rpcMethodArgVarExprs, bodyExprs, messageTypes)); + } else if (method.hasLro()) { + bodyStatements.addAll( + composeUnaryLroRpcMethodSampleCodeBodyStatements( + method, clientVarExpr, rpcMethodArgVarExprs, bodyExprs)); + } else { + bodyStatements.addAll( + composeUnaryRpcMethodSampleCodeBodyStatements( + method, clientVarExpr, rpcMethodArgVarExprs, bodyExprs)); } + return SampleCodeWriter.write( - composeUnaryRpcMethodSampleCode(method, clientType, arguments, resourceNames)); + TryCatchStatement.builder() + .setTryResourceExpr(assignClientVariableWithCreateMethodExpr(clientVarExpr)) + .setTryBody(bodyStatements) + .setIsSampleCode(true) + .build()); } - @VisibleForTesting - static TryCatchStatement composeUnaryRpcMethodSampleCode( + public static String composeRpcDefaultMethodHeaderSampleCode( Method method, TypeNode clientType, - List arguments, - Map resourceNames) { + Map resourceNames, + Map messageTypes) { VariableExpr clientVarExpr = VariableExpr.withVariable( Variable.builder() .setName(JavaStyle.toLowerCamelCase(clientType.reference().name())) .setType(clientType) .build()); - List rpcMethodArgVarExprs = createRpcMethodArgumentVariableExprs(arguments); - List rpcMethodArgDefaultValueExprs = - createRpcMethodArgumentDefaultValueExprs(arguments, resourceNames); - List bodyExprs = - createAssignmentsForVarExprsWithValueExprs( - rpcMethodArgVarExprs, rpcMethodArgDefaultValueExprs); + + // Create request variable expression and assign with its default value. + VariableExpr requestVarExpr = + VariableExpr.withVariable( + Variable.builder().setName("request").setType(method.inputType()).build()); + List rpcMethodArgVarExprs = Arrays.asList(requestVarExpr); + Message requestMessage = messageTypes.get(method.inputType().reference().simpleName()); + Preconditions.checkNotNull(requestMessage); + Expr requestBuilderExpr = + DefaultValueComposer.createSimpleMessageBuilderExpr( + requestMessage, resourceNames, messageTypes); + AssignmentExpr requestAssignmentExpr = + AssignmentExpr.builder() + .setVariableExpr(requestVarExpr.toBuilder().setIsDecl(true).build()) + .setValueExpr(requestBuilderExpr) + .build(); + + List bodyExprs = new ArrayList<>(); + bodyExprs.add(requestAssignmentExpr); + + List bodyStatements = new ArrayList<>(); + if (method.isPaged()) { + bodyStatements.addAll( + composeUnaryPagedRpcMethodSampleCodeBodyStatements( + method, clientVarExpr, rpcMethodArgVarExprs, bodyExprs, messageTypes)); + } else if (method.hasLro()) { + bodyStatements.addAll( + composeUnaryLroRpcMethodSampleCodeBodyStatements( + method, clientVarExpr, rpcMethodArgVarExprs, bodyExprs)); + } else { + bodyStatements.addAll( + composeUnaryRpcMethodSampleCodeBodyStatements( + method, clientVarExpr, rpcMethodArgVarExprs, bodyExprs)); + } + + return SampleCodeWriter.write( + TryCatchStatement.builder() + .setTryResourceExpr(assignClientVariableWithCreateMethodExpr(clientVarExpr)) + .setTryBody(bodyStatements) + .setIsSampleCode(true) + .build()); + } + + private static List composeUnaryRpcMethodSampleCodeBodyStatements( + Method method, + VariableExpr clientVarExpr, + List rpcMethodArgVarExprs, + List bodyExprs) { + // Invoke current method based on return type. // e.g. if return void, echoClient.echo(..); or, // e.g. if return other type, EchoResponse response = echoClient.echo(...); boolean returnsVoid = isProtoEmptyType(method.outputType()); + MethodInvocationExpr clientRpcMethodInvocationExpr = + MethodInvocationExpr.builder() + .setExprReferenceExpr(clientVarExpr) + .setMethodName(JavaStyle.toLowerCamelCase(method.name())) + .setArguments( + rpcMethodArgVarExprs.stream().map(e -> (Expr) e).collect(Collectors.toList())) + .setReturnType(method.outputType()) + .build(); if (returnsVoid) { - bodyExprs.add( - MethodInvocationExpr.builder() - .setExprReferenceExpr(clientVarExpr) - .setMethodName(JavaStyle.toLowerCamelCase(method.name())) - .setArguments( - rpcMethodArgVarExprs.stream().map(e -> (Expr) e).collect(Collectors.toList())) - .setReturnType(clientType) - .build()); + bodyExprs.add(clientRpcMethodInvocationExpr); } else { VariableExpr responseVarExpr = VariableExpr.withVariable( Variable.builder().setName("response").setType(method.outputType()).build()); - MethodInvocationExpr clientMethodInvocationExpr = - MethodInvocationExpr.builder() - .setExprReferenceExpr(clientVarExpr) - .setMethodName(JavaStyle.toLowerCamelCase(method.name())) - .setArguments( - rpcMethodArgVarExprs.stream().map(e -> (Expr) e).collect(Collectors.toList())) - .setReturnType(responseVarExpr.variable().type()) - .build(); bodyExprs.add( AssignmentExpr.builder() .setVariableExpr(responseVarExpr.toBuilder().setIsDecl(true).build()) - .setValueExpr(clientMethodInvocationExpr) + .setValueExpr(clientRpcMethodInvocationExpr) .build()); } - return TryCatchStatement.builder() - .setTryResourceExpr(assignClientVariableWithCreateMethodExpr(clientVarExpr)) - .setTryBody( - bodyExprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList())) - .setIsSampleCode(true) - .build(); + return bodyExprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList()); } - @VisibleForTesting - static TryCatchStatement composeUnaryPagedRpcMethodSampleCode( + private static List composeUnaryPagedRpcMethodSampleCodeBodyStatements( Method method, - TypeNode clientType, - TypeNode repeatedResponseType, - List arguments, - Map resourceNames) { - VariableExpr clientVarExpr = - VariableExpr.withVariable( - Variable.builder() - .setName(JavaStyle.toLowerCamelCase(clientType.reference().name())) - .setType(clientType) - .build()); - List rpcMethodArgVarExprs = createRpcMethodArgumentVariableExprs(arguments); - List rpcMethodArgDefaultValueExprs = - createRpcMethodArgumentDefaultValueExprs(arguments, resourceNames); - List bodyExprs = - createAssignmentsForVarExprsWithValueExprs( - rpcMethodArgVarExprs, rpcMethodArgDefaultValueExprs); + VariableExpr clientVarExpr, + List rpcMethodArgVarExprs, + List bodyExprs, + Map messageTypes) { + + // Find the repeated field. + Message methodOutputMessage = messageTypes.get(method.outputType().reference().simpleName()); + Field repeatedPagedResultsField = methodOutputMessage.findAndUnwrapFirstRepeatedField(); + Preconditions.checkNotNull( + repeatedPagedResultsField, + String.format( + "No repeated field found on message %s for method %s", + methodOutputMessage.name(), method.name())); + TypeNode repeatedResponseType = repeatedPagedResultsField.type(); + // For loop paged response item on iterateAll method. // e.g. for (LogEntry element : loggingServiceV2Client.ListLogs(parent).iterateAll()) { // //doThingsWith(element); // } - MethodInvocationExpr clientMethodExpr = + MethodInvocationExpr clientMethodIterateAllExpr = MethodInvocationExpr.builder() .setExprReferenceExpr(clientVarExpr) .setMethodName(JavaStyle.toLowerCamelCase(method.name())) .setArguments( rpcMethodArgVarExprs.stream().map(e -> (Expr) e).collect(Collectors.toList())) .build(); - Expr clientMethodIteratorAllExpr = + clientMethodIterateAllExpr = MethodInvocationExpr.builder() - .setExprReferenceExpr(clientMethodExpr) + .setExprReferenceExpr(clientMethodIterateAllExpr) .setMethodName("iterateAll") .setReturnType(repeatedResponseType) .build(); @@ -320,7 +365,7 @@ static TryCatchStatement composeUnaryPagedRpcMethodSampleCode( Variable.builder().setName("element").setType(repeatedResponseType).build()) .setIsDecl(true) .build()) - .setCollectionExpr(clientMethodIteratorAllExpr) + .setCollectionExpr(clientMethodIterateAllExpr) .setBody( Arrays.asList( CommentStatement.withComment( @@ -329,52 +374,36 @@ static TryCatchStatement composeUnaryPagedRpcMethodSampleCode( List bodyStatements = bodyExprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList()); + bodyExprs.clear(); bodyStatements.add(loopIteratorStatement); - return TryCatchStatement.builder() - .setTryResourceExpr(assignClientVariableWithCreateMethodExpr(clientVarExpr)) - .setTryBody(bodyStatements) - .setIsSampleCode(true) - .build(); + return bodyStatements; } - @VisibleForTesting - static TryCatchStatement composeUnaryLroRpcMethodSampleCode( + private static List composeUnaryLroRpcMethodSampleCodeBodyStatements( Method method, - TypeNode clientType, - List arguments, - Map resourceNames) { - VariableExpr clientVarExpr = - VariableExpr.withVariable( - Variable.builder() - .setName(JavaStyle.toLowerCamelCase(clientType.reference().name())) - .setType(clientType) - .build()); - List rpcMethodArgVarExprs = createRpcMethodArgumentVariableExprs(arguments); - List rpcMethodArgDefaultValueExprs = - createRpcMethodArgumentDefaultValueExprs(arguments, resourceNames); - List bodyExprs = - createAssignmentsForVarExprsWithValueExprs( - rpcMethodArgVarExprs, rpcMethodArgDefaultValueExprs); + VariableExpr clientVarExpr, + List rpcMethodArgVarExprs, + List bodyExprs) { // Assign response variable with invoking client's lro method. // e.g. if return void, echoClient.waitAsync(ttl).get(); or, // e.g. if return other type, WaitResponse response = echoClient.waitAsync(ttl).get(); - Expr invokeLroMethodExpr = + Expr invokeLroGetMethodExpr = MethodInvocationExpr.builder() .setExprReferenceExpr(clientVarExpr) .setMethodName(String.format("%sAsync", JavaStyle.toLowerCamelCase(method.name()))) .setArguments( rpcMethodArgVarExprs.stream().map(e -> (Expr) e).collect(Collectors.toList())) .build(); - Expr getResponseMethodExpr = + invokeLroGetMethodExpr = MethodInvocationExpr.builder() - .setExprReferenceExpr(invokeLroMethodExpr) + .setExprReferenceExpr(invokeLroGetMethodExpr) .setMethodName("get") .setReturnType(method.lro().responseType()) .build(); boolean returnsVoid = isProtoEmptyType(method.lro().responseType()); if (returnsVoid) { - bodyExprs.add(getResponseMethodExpr); + bodyExprs.add(invokeLroGetMethodExpr); } else { VariableExpr responseVarExpr = VariableExpr.builder() @@ -388,16 +417,11 @@ static TryCatchStatement composeUnaryLroRpcMethodSampleCode( bodyExprs.add( AssignmentExpr.builder() .setVariableExpr(responseVarExpr) - .setValueExpr(getResponseMethodExpr) + .setValueExpr(invokeLroGetMethodExpr) .build()); } - return TryCatchStatement.builder() - .setTryResourceExpr(assignClientVariableWithCreateMethodExpr(clientVarExpr)) - .setTryBody( - bodyExprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList())) - .setIsSampleCode(true) - .build(); + return bodyExprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList()); } // ==================================Helpers===================================================// diff --git a/src/test/java/com/google/api/generator/gapic/composer/ServiceClientSampleCodeComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/ServiceClientSampleCodeComposerTest.java index b3a923de7c..3e5d82a8b0 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/ServiceClientSampleCodeComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/ServiceClientSampleCodeComposerTest.java @@ -21,7 +21,6 @@ import com.google.api.generator.engine.ast.Reference; import com.google.api.generator.engine.ast.TypeNode; import com.google.api.generator.engine.ast.VaporReference; -import com.google.api.generator.gapic.composer.samplecode.SampleCodeWriter; import com.google.api.generator.gapic.model.Field; import com.google.api.generator.gapic.model.LongrunningOperation; import com.google.api.generator.gapic.model.Message; @@ -44,7 +43,7 @@ public class ServiceClientSampleCodeComposerTest { private static final String LRO_PACKAGE_NAME = "com.google.longrunning"; private static final String PROTO_PACKAGE_NAME = "com.google.protobuf"; - // =======================================RPC Method Header Sample Code=======================// + // =======================================Unary RPC Method Sample Code=======================// @Test public void validComposeRpcMethodHeaderSampleCode_pureUnaryRpc() { FileDescriptor echoFileDescriptor = EchoOuterClass.getDescriptor(); @@ -88,223 +87,10 @@ public void validComposeRpcMethodHeaderSampleCode_pureUnaryRpc() { } @Test - public void validComposeRpcMethodHeaderSampleCode_pagedUnaryRpc() { - FileDescriptor echoFileDescriptor = EchoOuterClass.getDescriptor(); - Map resourceNames = Parser.parseResourceNames(echoFileDescriptor); - Map messageTypes = Parser.parseMessages(echoFileDescriptor); - TypeNode clientType = - TypeNode.withReference( - VaporReference.builder() - .setName("EchoClient") - .setPakkage(SHOWCASE_PACKAGE_NAME) - .build()); - TypeNode inputType = - TypeNode.withReference( - VaporReference.builder() - .setName("PagedExpandRequest") - .setPakkage(SHOWCASE_PACKAGE_NAME) - .build()); - TypeNode outputType = - TypeNode.withReference( - VaporReference.builder() - .setName("PagedExpandResponse") - .setPakkage(SHOWCASE_PACKAGE_NAME) - .build()); - Method method = - Method.builder() - .setName("SimplePagedExpand") - .setMethodSignatures(Collections.emptyList()) - .setInputType(inputType) - .setOutputType(outputType) - .setIsPaged(true) - .build(); - String results = - ServiceClientSampleCodeComposer.composeRpcMethodHeaderSampleCode( - method, clientType, Collections.emptyList(), resourceNames, messageTypes); - String expected = - LineFormatter.lines( - "try (EchoClient echoClient = EchoClient.create()) {\n", - " for (EchoResponse element : echoClient.simplePagedExpand().iterateAll()) {\n", - " // doThingsWith(element);\n", - " }\n", - "}"); - assertEquals(expected, results); - } - - @Test - public void validComposeRpcMethodHeaderSampleCode_lroUnaryRpc() { - FileDescriptor echoFileDescriptor = EchoOuterClass.getDescriptor(); - Map resourceNames = Parser.parseResourceNames(echoFileDescriptor); - Map messageTypes = Parser.parseMessages(echoFileDescriptor); - TypeNode clientType = - TypeNode.withReference( - VaporReference.builder() - .setName("EchoClient") - .setPakkage(SHOWCASE_PACKAGE_NAME) - .build()); - TypeNode inputType = - TypeNode.withReference( - VaporReference.builder() - .setName("WaitRequest") - .setPakkage(SHOWCASE_PACKAGE_NAME) - .build()); - TypeNode outputType = - TypeNode.withReference( - VaporReference.builder().setName("Operation").setPakkage(LRO_PACKAGE_NAME).build()); - TypeNode responseType = - TypeNode.withReference( - VaporReference.builder() - .setName("WaitResponse") - .setPakkage(SHOWCASE_PACKAGE_NAME) - .build()); - TypeNode metadataType = - TypeNode.withReference( - VaporReference.builder() - .setName("WaitMetadata") - .setPakkage(SHOWCASE_PACKAGE_NAME) - .build()); - LongrunningOperation lro = LongrunningOperation.withTypes(responseType, metadataType); - TypeNode ttlTypeNode = - TypeNode.withReference( - VaporReference.builder().setName("Duration").setPakkage(PROTO_PACKAGE_NAME).build()); - MethodArgument ttl = - MethodArgument.builder() - .setName("ttl") - .setType(ttlTypeNode) - .setField( - Field.builder() - .setName("ttl") - .setType(ttlTypeNode) - .setIsMessage(true) - .setIsContainedInOneof(true) - .build()) - .build(); - List arguments = Arrays.asList(ttl); - Method method = - Method.builder() - .setName("Wait") - .setInputType(inputType) - .setOutputType(outputType) - .setLro(lro) - .setMethodSignatures(Arrays.asList(arguments)) - .build(); - - String results = - ServiceClientSampleCodeComposer.composeRpcMethodHeaderSampleCode( - method, clientType, arguments, resourceNames, messageTypes); - String expected = - LineFormatter.lines( - "try (EchoClient echoClient = EchoClient.create()) {\n", - " Duration ttl = Duration.newBuilder().build();\n", - " WaitResponse response = echoClient.waitAsync(ttl).get();\n", - "}"); - assertEquals(results, expected); - } - - @Test - public void invalidComposeRpcMethodHeaderSampleCode_noMatchedRepeatedResponseTypeInPagedMethod() { - FileDescriptor echoFileDescriptor = EchoOuterClass.getDescriptor(); - Map resourceNames = Parser.parseResourceNames(echoFileDescriptor); - Map messageTypes = Parser.parseMessages(echoFileDescriptor); - TypeNode clientType = - TypeNode.withReference( - VaporReference.builder() - .setName("EchoClient") - .setPakkage(SHOWCASE_PACKAGE_NAME) - .build()); - TypeNode inputType = - TypeNode.withReference( - VaporReference.builder() - .setName("EchoRequest") - .setPakkage(SHOWCASE_PACKAGE_NAME) - .build()); - TypeNode outputType = - TypeNode.withReference( - VaporReference.builder() - .setName("PagedResponse") - .setPakkage(SHOWCASE_PACKAGE_NAME) - .build()); - List methodArguments = Collections.emptyList(); - Method method = - Method.builder() - .setName("simplePagedMethod") - .setMethodSignatures(Arrays.asList(methodArguments)) - .setInputType(inputType) - .setOutputType(outputType) - .setIsPaged(true) - .build(); - assertThrows( - NullPointerException.class, - () -> - ServiceClientSampleCodeComposer.composeRpcMethodHeaderSampleCode( - method, clientType, methodArguments, resourceNames, messageTypes)); - } - - @Test - public void invalidComposeRpcMethodHeaderSampleCode_noRepeatedResponseTypeInPagedMethod() { + public void validComposeRpcMethodHeaderSampleCode_pureUnaryRpcWithResourceNameMethodArgument() { FileDescriptor echoFileDescriptor = EchoOuterClass.getDescriptor(); Map resourceNames = Parser.parseResourceNames(echoFileDescriptor); Map messageTypes = Parser.parseMessages(echoFileDescriptor); - TypeNode clientType = - TypeNode.withReference( - VaporReference.builder() - .setName("EchoClient") - .setPakkage(SHOWCASE_PACKAGE_NAME) - .build()); - TypeNode inputType = - TypeNode.withReference( - VaporReference.builder() - .setName("EchoRequest") - .setPakkage(SHOWCASE_PACKAGE_NAME) - .build()); - TypeNode outputType = - TypeNode.withReference( - VaporReference.builder() - .setName("PagedResponse") - .setPakkage(SHOWCASE_PACKAGE_NAME) - .build()); - List methodArguments = Collections.emptyList(); - Method method = - Method.builder() - .setName("simplePagedMethod") - .setMethodSignatures(Arrays.asList(methodArguments)) - .setInputType(inputType) - .setOutputType(outputType) - .setIsPaged(true) - .build(); - Field responseField = - Field.builder() - .setName("response") - .setType( - TypeNode.withReference( - ConcreteReference.builder() - .setClazz(List.class) - .setGenerics(ConcreteReference.withClazz(String.class)) - .build())) - .setIsMessage(true) - .setIsRepeated(false) - .build(); - Field nextPageToken = - Field.builder().setName("next_page_token").setType(TypeNode.STRING).build(); - Message noRepeatedFieldMessage = - Message.builder() - .setName("PagedResponse") - .setType(outputType) - .setFields(Arrays.asList(responseField, nextPageToken)) - .build(); - messageTypes.put("PagedResponse", noRepeatedFieldMessage); - assertThrows( - NullPointerException.class, - () -> - ServiceClientSampleCodeComposer.composeRpcMethodHeaderSampleCode( - method, clientType, methodArguments, resourceNames, messageTypes)); - } - - // =======================================Unary RPC Method Sample Code=======================// - @Test - public void composeUnaryRpcMethodSampleCode_resourceNameMethodArgument() { - FileDescriptor echoFileDescriptor = EchoOuterClass.getDescriptor(); - Map resourceNames = Parser.parseResourceNames(echoFileDescriptor); TypeNode clientType = TypeNode.withReference( VaporReference.builder() @@ -340,7 +126,7 @@ public void composeUnaryRpcMethodSampleCode_resourceNameMethodArgument() { .setIsResourceNameHelper(true) .build(); List> signatures = Arrays.asList(Arrays.asList(arg)); - Method unaryMethod = + Method method = Method.builder() .setName("echo") .setMethodSignatures(signatures) @@ -348,9 +134,8 @@ public void composeUnaryRpcMethodSampleCode_resourceNameMethodArgument() { .setOutputType(outputType) .build(); String results = - SampleCodeWriter.write( - ServiceClientSampleCodeComposer.composeUnaryRpcMethodSampleCode( - unaryMethod, clientType, signatures.get(0), resourceNames)); + ServiceClientSampleCodeComposer.composeRpcMethodHeaderSampleCode( + method, clientType, signatures.get(0), resourceNames, messageTypes); String expected = LineFormatter.lines( "try (EchoClient echoClient = EchoClient.create()) {\n", @@ -361,9 +146,11 @@ public void composeUnaryRpcMethodSampleCode_resourceNameMethodArgument() { } @Test - public void composeUnaryRpcMethodSampleCode_superReferenceIsResourceNameMethodArgument() { + public void + validComposeRpcMethodHeaderSampleCode_pureUnaryRpcWithSuperReferenceIsResourceNameMethodArgument() { FileDescriptor echoFileDescriptor = EchoOuterClass.getDescriptor(); Map resourceNames = Parser.parseResourceNames(echoFileDescriptor); + Map messageTypes = Parser.parseMessages(echoFileDescriptor); TypeNode clientType = TypeNode.withReference( VaporReference.builder() @@ -412,9 +199,8 @@ public void composeUnaryRpcMethodSampleCode_superReferenceIsResourceNameMethodAr .setOutputType(outputType) .build(); String results = - SampleCodeWriter.write( - ServiceClientSampleCodeComposer.composeUnaryRpcMethodSampleCode( - unaryMethod, clientType, signatures.get(0), resourceNames)); + ServiceClientSampleCodeComposer.composeRpcMethodHeaderSampleCode( + unaryMethod, clientType, signatures.get(0), resourceNames, messageTypes); String expected = LineFormatter.lines( "try (EchoClient echoClient = EchoClient.create()) {\n", @@ -425,9 +211,11 @@ public void composeUnaryRpcMethodSampleCode_superReferenceIsResourceNameMethodAr } @Test - public void composeUnaryRpcMethodSampleCode_stringWithResourceReferenceMethodArgument() { + public void + validComposeRpcMethodHeaderSampleCode_pureUnaryRpcWithStringWithResourceReferenceMethodArgument() { FileDescriptor echoFileDescriptor = EchoOuterClass.getDescriptor(); Map resourceNames = Parser.parseResourceNames(echoFileDescriptor); + Map messageTypes = Parser.parseMessages(echoFileDescriptor); TypeNode clientType = TypeNode.withReference( VaporReference.builder() @@ -467,9 +255,8 @@ public void composeUnaryRpcMethodSampleCode_stringWithResourceReferenceMethodArg .setOutputType(outputType) .build(); String results = - SampleCodeWriter.write( - ServiceClientSampleCodeComposer.composeUnaryRpcMethodSampleCode( - unaryMethod, clientType, signatures.get(0), resourceNames)); + ServiceClientSampleCodeComposer.composeRpcMethodHeaderSampleCode( + unaryMethod, clientType, signatures.get(0), resourceNames, messageTypes); String expected = LineFormatter.lines( "try (EchoClient echoClient = EchoClient.create()) {\n", @@ -480,9 +267,11 @@ public void composeUnaryRpcMethodSampleCode_stringWithResourceReferenceMethodArg } @Test - public void composeUnaryRpcMethodSampleCode_stringWithParentResourceReferenceMethodArgument() { + public void + validComposeRpcMethodHeaderSampleCode_pureUnaryRpcWithStringWithParentResourceReferenceMethodArgument() { FileDescriptor echoFileDescriptor = EchoOuterClass.getDescriptor(); Map resourceNames = Parser.parseResourceNames(echoFileDescriptor); + Map messageTypes = Parser.parseMessages(echoFileDescriptor); TypeNode clientType = TypeNode.withReference( VaporReference.builder() @@ -523,9 +312,8 @@ public void composeUnaryRpcMethodSampleCode_stringWithParentResourceReferenceMet .setOutputType(outputType) .build(); String results = - SampleCodeWriter.write( - ServiceClientSampleCodeComposer.composeUnaryRpcMethodSampleCode( - unaryMethod, clientType, signatures.get(0), resourceNames)); + ServiceClientSampleCodeComposer.composeRpcMethodHeaderSampleCode( + unaryMethod, clientType, signatures.get(0), resourceNames, messageTypes); String expected = LineFormatter.lines( "try (EchoClient echoClient = EchoClient.create()) {\n", @@ -536,9 +324,10 @@ public void composeUnaryRpcMethodSampleCode_stringWithParentResourceReferenceMet } @Test - public void composeUnaryRpcMethodSampleCode_isMessageMethodArgument() { + public void validComposeRpcMethodHeaderSampleCode_pureUnaryRpcWithIsMessageMethodArgument() { FileDescriptor echoFileDescriptor = EchoOuterClass.getDescriptor(); Map resourceNames = Parser.parseResourceNames(echoFileDescriptor); + Map messageTypes = Parser.parseMessages(echoFileDescriptor); TypeNode clientType = TypeNode.withReference( VaporReference.builder() @@ -582,9 +371,8 @@ public void composeUnaryRpcMethodSampleCode_isMessageMethodArgument() { .setOutputType(outputType) .build(); String results = - SampleCodeWriter.write( - ServiceClientSampleCodeComposer.composeUnaryRpcMethodSampleCode( - unaryMethod, clientType, signatures.get(0), resourceNames)); + ServiceClientSampleCodeComposer.composeRpcMethodHeaderSampleCode( + unaryMethod, clientType, signatures.get(0), resourceNames, messageTypes); String expected = LineFormatter.lines( "try (EchoClient echoClient = EchoClient.create()) {\n", @@ -595,9 +383,11 @@ public void composeUnaryRpcMethodSampleCode_isMessageMethodArgument() { } @Test - public void composeUnaryRpcMethodSampleCode_multipleWordNameMethodArgument() { + public void + validComposeRpcMethodHeaderSampleCode_pureUnaryRpcWithMultipleWordNameMethodArgument() { FileDescriptor echoFileDescriptor = EchoOuterClass.getDescriptor(); Map resourceNames = Parser.parseResourceNames(echoFileDescriptor); + Map messageTypes = Parser.parseMessages(echoFileDescriptor); TypeNode clientType = TypeNode.withReference( VaporReference.builder() @@ -655,9 +445,8 @@ public void composeUnaryRpcMethodSampleCode_multipleWordNameMethodArgument() { .setOutputType(outputType) .build(); String results = - SampleCodeWriter.write( - ServiceClientSampleCodeComposer.composeUnaryRpcMethodSampleCode( - unaryMethod, clientType, signatures.get(0), resourceNames)); + ServiceClientSampleCodeComposer.composeRpcMethodHeaderSampleCode( + unaryMethod, clientType, signatures.get(0), resourceNames, messageTypes); String expected = LineFormatter.lines( "try (EchoClient echoClient = EchoClient.create()) {\n", @@ -669,9 +458,11 @@ public void composeUnaryRpcMethodSampleCode_multipleWordNameMethodArgument() { } @Test - public void composeUnaryRpcMethodSampleCode_stringIsContainedInOneOfMethodArgument() { + public void + validComposeRpcMethodHeaderSampleCode_pureUnaryRpcWithStringIsContainedInOneOfMethodArgument() { FileDescriptor echoFileDescriptor = EchoOuterClass.getDescriptor(); Map resourceNames = Parser.parseResourceNames(echoFileDescriptor); + Map messageTypes = Parser.parseMessages(echoFileDescriptor); TypeNode clientType = TypeNode.withReference( VaporReference.builder() @@ -711,9 +502,8 @@ public void composeUnaryRpcMethodSampleCode_stringIsContainedInOneOfMethodArgume .setOutputType(outputType) .build(); String results = - SampleCodeWriter.write( - ServiceClientSampleCodeComposer.composeUnaryRpcMethodSampleCode( - unaryMethod, clientType, signatures.get(0), resourceNames)); + ServiceClientSampleCodeComposer.composeRpcMethodHeaderSampleCode( + unaryMethod, clientType, signatures.get(0), resourceNames, messageTypes); String expected = LineFormatter.lines( "try (EchoClient echoClient = EchoClient.create()) {\n", @@ -724,9 +514,10 @@ public void composeUnaryRpcMethodSampleCode_stringIsContainedInOneOfMethodArgume } @Test - public void composeUnaryRpcMethodSampleCode_multipleMethodArguments() { + public void validComposeRpcMethodHeaderSampleCode_pureUnaryRpcWithMultipleMethodArguments() { FileDescriptor echoFileDescriptor = EchoOuterClass.getDescriptor(); Map resourceNames = Parser.parseResourceNames(echoFileDescriptor); + Map messageTypes = Parser.parseMessages(echoFileDescriptor); TypeNode clientType = TypeNode.withReference( VaporReference.builder() @@ -770,9 +561,8 @@ public void composeUnaryRpcMethodSampleCode_multipleMethodArguments() { .setOutputType(outputType) .build(); String results = - SampleCodeWriter.write( - ServiceClientSampleCodeComposer.composeUnaryRpcMethodSampleCode( - unaryMethod, clientType, signatures.get(0), resourceNames)); + ServiceClientSampleCodeComposer.composeRpcMethodHeaderSampleCode( + unaryMethod, clientType, signatures.get(0), resourceNames, messageTypes); String expected = LineFormatter.lines( "try (EchoClient echoClient = EchoClient.create()) {\n", @@ -784,9 +574,10 @@ public void composeUnaryRpcMethodSampleCode_multipleMethodArguments() { } @Test - public void composeUnaryRpcMethodSampleCode_noMethodArguments() { + public void validComposeRpcMethodHeaderSampleCode_pureUnaryRpcWithNoMethodArguments() { FileDescriptor echoFileDescriptor = EchoOuterClass.getDescriptor(); Map resourceNames = Parser.parseResourceNames(echoFileDescriptor); + Map messageTypes = Parser.parseMessages(echoFileDescriptor); TypeNode clientType = TypeNode.withReference( VaporReference.builder() @@ -814,9 +605,8 @@ public void composeUnaryRpcMethodSampleCode_noMethodArguments() { .setOutputType(outputType) .build(); String results = - SampleCodeWriter.write( - ServiceClientSampleCodeComposer.composeUnaryRpcMethodSampleCode( - unaryMethod, clientType, signatures.get(0), resourceNames)); + ServiceClientSampleCodeComposer.composeRpcMethodHeaderSampleCode( + unaryMethod, clientType, signatures.get(0), resourceNames, messageTypes); String expected = LineFormatter.lines( "try (EchoClient echoClient = EchoClient.create()) {\n", @@ -826,9 +616,10 @@ public void composeUnaryRpcMethodSampleCode_noMethodArguments() { } @Test - public void composeUnaryRpcMethodSampleCode_methodReturnVoid() { + public void validComposeRpcMethodHeaderSampleCode_pureUnaryRpcWithMethodReturnVoid() { FileDescriptor echoFileDescriptor = EchoOuterClass.getDescriptor(); Map resourceNames = Parser.parseResourceNames(echoFileDescriptor); + Map messageTypes = Parser.parseMessages(echoFileDescriptor); TypeNode clientType = TypeNode.withReference( VaporReference.builder() @@ -860,9 +651,8 @@ public void composeUnaryRpcMethodSampleCode_methodReturnVoid() { .setOutputType(outputType) .build(); String results = - SampleCodeWriter.write( - ServiceClientSampleCodeComposer.composeUnaryRpcMethodSampleCode( - unaryMethod, clientType, methodSignatures.get(0), resourceNames)); + ServiceClientSampleCodeComposer.composeRpcMethodHeaderSampleCode( + unaryMethod, clientType, methodSignatures.get(0), resourceNames, messageTypes); String expected = LineFormatter.lines( "try (EchoClient echoClient = EchoClient.create()) {\n", @@ -874,9 +664,10 @@ public void composeUnaryRpcMethodSampleCode_methodReturnVoid() { // ===================================Unary Paged RPC Method Sample Code ======================// @Test - public void composeUnaryPagedRpcMethodSampleCode_multipleMethodArguments() { + public void validComposeRpcMethodHeaderSampleCode_pagedRpcWithMultipleMethodArguments() { FileDescriptor echoFileDescriptor = EchoOuterClass.getDescriptor(); Map resourceNames = Parser.parseResourceNames(echoFileDescriptor); + Map messageTypes = Parser.parseMessages(echoFileDescriptor); TypeNode clientType = TypeNode.withReference( VaporReference.builder() @@ -926,14 +717,33 @@ public void composeUnaryPagedRpcMethodSampleCode_multipleMethodArguments() { .setOutputType(outputType) .setIsPaged(true) .build(); - TypeNode repeatedResponseType = - TypeNode.withReference( - VaporReference.builder().setName("Content").setPakkage(SHOWCASE_PACKAGE_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") + .setType(outputType) + .setFields(Arrays.asList(repeatedField, nextPagedTokenField)) + .build(); + messageTypes.put("ListContentResponse", listContentResponseMessage); String results = - SampleCodeWriter.write( - ServiceClientSampleCodeComposer.composeUnaryPagedRpcMethodSampleCode( - method, clientType, repeatedResponseType, arguments, resourceNames)); + ServiceClientSampleCodeComposer.composeRpcMethodHeaderSampleCode( + method, clientType, arguments, resourceNames, messageTypes); String expected = LineFormatter.lines( "try (EchoClient echoClient = EchoClient.create()) {\n", @@ -947,9 +757,10 @@ public void composeUnaryPagedRpcMethodSampleCode_multipleMethodArguments() { } @Test - public void composeUnaryPagedRpcMethodSampleCode_noMethodArguments() { + public void validComposeRpcMethodHeaderSampleCode_pagedRpcWithNoMethodArguments() { FileDescriptor echoFileDescriptor = EchoOuterClass.getDescriptor(); Map resourceNames = Parser.parseResourceNames(echoFileDescriptor); + Map messageTypes = Parser.parseMessages(echoFileDescriptor); TypeNode clientType = TypeNode.withReference( VaporReference.builder() @@ -977,29 +788,201 @@ public void composeUnaryPagedRpcMethodSampleCode_noMethodArguments() { .setOutputType(outputType) .setIsPaged(true) .build(); - TypeNode repeatedResponseType = - TypeNode.withReference( - VaporReference.builder().setName("Content").setPakkage(SHOWCASE_PACKAGE_NAME).build()); - - String results = - SampleCodeWriter.write( - ServiceClientSampleCodeComposer.composeUnaryPagedRpcMethodSampleCode( - method, clientType, repeatedResponseType, arguments, resourceNames)); - String expected = - LineFormatter.lines( - "try (EchoClient echoClient = EchoClient.create()) {\n", - " for (Content element : echoClient.listContent().iterateAll()) {\n", - " // doThingsWith(element);\n", + 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") + .setType(outputType) + .setFields(Arrays.asList(repeatedField, nextPagedTokenField)) + .build(); + messageTypes.put("ListContentResponse", listContentResponseMessage); + + String results = + ServiceClientSampleCodeComposer.composeRpcMethodHeaderSampleCode( + method, clientType, arguments, resourceNames, messageTypes); + String expected = + LineFormatter.lines( + "try (EchoClient echoClient = EchoClient.create()) {\n", + " for (Content element : echoClient.listContent().iterateAll()) {\n", + " // doThingsWith(element);\n", " }\n", "}"); assertEquals(results, expected); } + @Test + public void invalidComposeRpcMethodHeaderSampleCode_noMatchedRepeatedResponseTypeInPagedMethod() { + FileDescriptor echoFileDescriptor = EchoOuterClass.getDescriptor(); + Map resourceNames = Parser.parseResourceNames(echoFileDescriptor); + Map messageTypes = Parser.parseMessages(echoFileDescriptor); + TypeNode clientType = + TypeNode.withReference( + VaporReference.builder() + .setName("EchoClient") + .setPakkage(SHOWCASE_PACKAGE_NAME) + .build()); + TypeNode inputType = + TypeNode.withReference( + VaporReference.builder() + .setName("EchoRequest") + .setPakkage(SHOWCASE_PACKAGE_NAME) + .build()); + TypeNode outputType = + TypeNode.withReference( + VaporReference.builder() + .setName("PagedResponse") + .setPakkage(SHOWCASE_PACKAGE_NAME) + .build()); + List methodArguments = Collections.emptyList(); + Method method = + Method.builder() + .setName("simplePagedMethod") + .setMethodSignatures(Arrays.asList(methodArguments)) + .setInputType(inputType) + .setOutputType(outputType) + .setIsPaged(true) + .build(); + assertThrows( + NullPointerException.class, + () -> + ServiceClientSampleCodeComposer.composeRpcMethodHeaderSampleCode( + method, clientType, methodArguments, resourceNames, messageTypes)); + } + + @Test + public void invalidComposeRpcMethodHeaderSampleCode_noRepeatedResponseTypeInPagedMethod() { + FileDescriptor echoFileDescriptor = EchoOuterClass.getDescriptor(); + Map resourceNames = Parser.parseResourceNames(echoFileDescriptor); + Map messageTypes = Parser.parseMessages(echoFileDescriptor); + TypeNode clientType = + TypeNode.withReference( + VaporReference.builder() + .setName("EchoClient") + .setPakkage(SHOWCASE_PACKAGE_NAME) + .build()); + TypeNode inputType = + TypeNode.withReference( + VaporReference.builder() + .setName("EchoRequest") + .setPakkage(SHOWCASE_PACKAGE_NAME) + .build()); + TypeNode outputType = + TypeNode.withReference( + VaporReference.builder() + .setName("PagedResponse") + .setPakkage(SHOWCASE_PACKAGE_NAME) + .build()); + List methodArguments = Collections.emptyList(); + Method method = + Method.builder() + .setName("simplePagedMethod") + .setMethodSignatures(Arrays.asList(methodArguments)) + .setInputType(inputType) + .setOutputType(outputType) + .setIsPaged(true) + .build(); + Field responseField = + Field.builder() + .setName("response") + .setType( + TypeNode.withReference( + ConcreteReference.builder() + .setClazz(List.class) + .setGenerics(ConcreteReference.withClazz(String.class)) + .build())) + .setIsMessage(true) + .setIsRepeated(false) + .build(); + Field nextPageToken = + Field.builder().setName("next_page_token").setType(TypeNode.STRING).build(); + Message noRepeatedFieldMessage = + Message.builder() + .setName("PagedResponse") + .setType(outputType) + .setFields(Arrays.asList(responseField, nextPageToken)) + .build(); + messageTypes.put("PagedResponse", noRepeatedFieldMessage); + assertThrows( + NullPointerException.class, + () -> + ServiceClientSampleCodeComposer.composeRpcMethodHeaderSampleCode( + method, clientType, methodArguments, resourceNames, messageTypes)); + } + // ===================================Unary LRO RPC Method Sample Code ======================// @Test - public void composeUnaryLroRpcMethodSampleCode_lroReturnResponseType() { + public void validComposeRpcMethodHeaderSampleCode_lroUnaryRpcWithNoMethodArgument() { FileDescriptor echoFileDescriptor = EchoOuterClass.getDescriptor(); Map resourceNames = Parser.parseResourceNames(echoFileDescriptor); + Map messageTypes = Parser.parseMessages(echoFileDescriptor); + TypeNode clientType = + TypeNode.withReference( + VaporReference.builder() + .setName("EchoClient") + .setPakkage(SHOWCASE_PACKAGE_NAME) + .build()); + TypeNode inputType = + TypeNode.withReference( + VaporReference.builder() + .setName("WaitRequest") + .setPakkage(SHOWCASE_PACKAGE_NAME) + .build()); + TypeNode outputType = + TypeNode.withReference( + VaporReference.builder().setName("Operation").setPakkage(LRO_PACKAGE_NAME).build()); + TypeNode responseType = + TypeNode.withReference( + VaporReference.builder() + .setName("WaitResponse") + .setPakkage(SHOWCASE_PACKAGE_NAME) + .build()); + TypeNode metadataType = + TypeNode.withReference( + VaporReference.builder() + .setName("WaitMetadata") + .setPakkage(SHOWCASE_PACKAGE_NAME) + .build()); + LongrunningOperation lro = LongrunningOperation.withTypes(responseType, metadataType); + Method method = + Method.builder() + .setName("Wait") + .setInputType(inputType) + .setOutputType(outputType) + .setLro(lro) + .setMethodSignatures(Collections.emptyList()) + .build(); + + String results = + ServiceClientSampleCodeComposer.composeRpcMethodHeaderSampleCode( + method, clientType, Collections.emptyList(), resourceNames, messageTypes); + String expected = + LineFormatter.lines( + "try (EchoClient echoClient = EchoClient.create()) {\n", + " WaitResponse response = echoClient.waitAsync().get();\n", + "}"); + assertEquals(results, expected); + } + + @Test + public void validComposeRpcMethodHeaderSampleCode_lroRpcWithReturnResponseType() { + FileDescriptor echoFileDescriptor = EchoOuterClass.getDescriptor(); + Map resourceNames = Parser.parseResourceNames(echoFileDescriptor); + Map messageTypes = Parser.parseMessages(echoFileDescriptor); TypeNode clientType = TypeNode.withReference( VaporReference.builder() @@ -1054,9 +1037,8 @@ public void composeUnaryLroRpcMethodSampleCode_lroReturnResponseType() { .build(); String results = - SampleCodeWriter.write( - ServiceClientSampleCodeComposer.composeUnaryLroRpcMethodSampleCode( - method, clientType, arguments, resourceNames)); + ServiceClientSampleCodeComposer.composeRpcMethodHeaderSampleCode( + method, clientType, arguments, resourceNames, messageTypes); String expected = LineFormatter.lines( "try (EchoClient echoClient = EchoClient.create()) {\n", @@ -1067,9 +1049,10 @@ public void composeUnaryLroRpcMethodSampleCode_lroReturnResponseType() { } @Test - public void composeUnaryLroRpcMethodSampleCode_lroReturnVoid() { + public void validComposeRpcMethodHeaderSampleCode_lroRpcWithReturnVoid() { FileDescriptor echoFileDescriptor = EchoOuterClass.getDescriptor(); Map resourceNames = Parser.parseResourceNames(echoFileDescriptor); + Map messageTypes = Parser.parseMessages(echoFileDescriptor); TypeNode clientType = TypeNode.withReference( VaporReference.builder() @@ -1121,9 +1104,8 @@ public void composeUnaryLroRpcMethodSampleCode_lroReturnVoid() { .build(); String results = - SampleCodeWriter.write( - ServiceClientSampleCodeComposer.composeUnaryLroRpcMethodSampleCode( - method, clientType, arguments, resourceNames)); + ServiceClientSampleCodeComposer.composeRpcMethodHeaderSampleCode( + method, clientType, arguments, resourceNames, messageTypes); String expected = LineFormatter.lines( "try (EchoClient echoClient = EchoClient.create()) {\n", @@ -1132,4 +1114,287 @@ public void composeUnaryLroRpcMethodSampleCode_lroReturnVoid() { "}"); assertEquals(results, expected); } + + // ================================Unary RPC Default Method Sample Code ====================// + @Test + public void validComposeRpcDefaultMethodHeaderSampleCode_isPagedMethod() { + FileDescriptor echoFileDescriptor = EchoOuterClass.getDescriptor(); + Map resourceNames = Parser.parseResourceNames(echoFileDescriptor); + Map messageTypes = Parser.parseMessages(echoFileDescriptor); + TypeNode clientType = + TypeNode.withReference( + VaporReference.builder() + .setName("EchoClient") + .setPakkage(SHOWCASE_PACKAGE_NAME) + .build()); + TypeNode inputType = + TypeNode.withReference( + VaporReference.builder() + .setName("PagedExpandRequest") + .setPakkage(SHOWCASE_PACKAGE_NAME) + .build()); + TypeNode outputType = + TypeNode.withReference( + VaporReference.builder() + .setName("PagedExpandResponse") + .setPakkage(SHOWCASE_PACKAGE_NAME) + .build()); + Method method = + Method.builder() + .setName("PagedExpand") + .setInputType(inputType) + .setOutputType(outputType) + .setMethodSignatures(Collections.emptyList()) + .setIsPaged(true) + .build(); + String results = + ServiceClientSampleCodeComposer.composeRpcDefaultMethodHeaderSampleCode( + method, clientType, resourceNames, messageTypes); + String expected = + LineFormatter.lines( + "try (EchoClient echoClient = EchoClient.create()) {\n", + " PagedExpandRequest request =\n", + " PagedExpandRequest.newBuilder()\n", + " .setContent(\"content951530617\")\n", + " .setPageSize(883849137)\n", + " .setPageToken(\"pageToken873572522\")\n", + " .build();\n", + " for (EchoResponse element : echoClient.pagedExpand(request).iterateAll()) {\n", + " // doThingsWith(element);\n", + " }\n", + "}"); + assertEquals(results, expected); + } + + @Test + public void invalidComposeRpcDefaultMethodHeaderSampleCode_isPagedMethod() { + FileDescriptor echoFileDescriptor = EchoOuterClass.getDescriptor(); + Map resourceNames = Parser.parseResourceNames(echoFileDescriptor); + Map messageTypes = Parser.parseMessages(echoFileDescriptor); + TypeNode clientType = + TypeNode.withReference( + VaporReference.builder() + .setName("EchoClient") + .setPakkage(SHOWCASE_PACKAGE_NAME) + .build()); + TypeNode inputType = + TypeNode.withReference( + VaporReference.builder() + .setName("NotExistRequest") + .setPakkage(SHOWCASE_PACKAGE_NAME) + .build()); + TypeNode outputType = + TypeNode.withReference( + VaporReference.builder() + .setName("PagedExpandResponse") + .setPakkage(SHOWCASE_PACKAGE_NAME) + .build()); + Method method = + Method.builder() + .setName("PagedExpand") + .setInputType(inputType) + .setOutputType(outputType) + .setMethodSignatures(Collections.emptyList()) + .setIsPaged(true) + .build(); + assertThrows( + NullPointerException.class, + () -> + ServiceClientSampleCodeComposer.composeRpcDefaultMethodHeaderSampleCode( + method, clientType, resourceNames, messageTypes)); + } + + @Test + public void validComposeRpcDefaultMethodHeaderSampleCode_hasLroMethodWithReturnResponse() { + FileDescriptor echoFileDescriptor = EchoOuterClass.getDescriptor(); + Map resourceNames = Parser.parseResourceNames(echoFileDescriptor); + Map messageTypes = Parser.parseMessages(echoFileDescriptor); + TypeNode clientType = + TypeNode.withReference( + VaporReference.builder() + .setName("EchoClient") + .setPakkage(SHOWCASE_PACKAGE_NAME) + .build()); + TypeNode inputType = + TypeNode.withReference( + VaporReference.builder() + .setName("WaitRequest") + .setPakkage(SHOWCASE_PACKAGE_NAME) + .build()); + TypeNode outputType = + TypeNode.withReference( + VaporReference.builder().setName("Operation").setPakkage(LRO_PACKAGE_NAME).build()); + TypeNode responseType = + TypeNode.withReference( + VaporReference.builder().setName("Empty").setPakkage(PROTO_PACKAGE_NAME).build()); + TypeNode metadataType = + TypeNode.withReference( + VaporReference.builder() + .setName("WaitMetadata") + .setPakkage(SHOWCASE_PACKAGE_NAME) + .build()); + LongrunningOperation lro = LongrunningOperation.withTypes(responseType, metadataType); + Method method = + Method.builder() + .setName("Wait") + .setInputType(inputType) + .setOutputType(outputType) + .setMethodSignatures(Collections.emptyList()) + .setLro(lro) + .build(); + String results = + ServiceClientSampleCodeComposer.composeRpcDefaultMethodHeaderSampleCode( + method, clientType, resourceNames, messageTypes); + String expected = + LineFormatter.lines( + "try (EchoClient echoClient = EchoClient.create()) {\n", + " WaitRequest request = WaitRequest.newBuilder().build();\n", + " echoClient.waitAsync(request).get();\n", + "}"); + assertEquals(results, expected); + } + + @Test + public void validComposeRpcDefaultMethodHeaderSampleCode_hasLroMethodWithReturnVoid() { + FileDescriptor echoFileDescriptor = EchoOuterClass.getDescriptor(); + Map resourceNames = Parser.parseResourceNames(echoFileDescriptor); + Map messageTypes = Parser.parseMessages(echoFileDescriptor); + TypeNode clientType = + TypeNode.withReference( + VaporReference.builder() + .setName("EchoClient") + .setPakkage(SHOWCASE_PACKAGE_NAME) + .build()); + TypeNode inputType = + TypeNode.withReference( + VaporReference.builder() + .setName("WaitRequest") + .setPakkage(SHOWCASE_PACKAGE_NAME) + .build()); + TypeNode outputType = + TypeNode.withReference( + VaporReference.builder().setName("Operation").setPakkage(LRO_PACKAGE_NAME).build()); + TypeNode responseType = + TypeNode.withReference( + VaporReference.builder() + .setName("WaitResponse") + .setPakkage(SHOWCASE_PACKAGE_NAME) + .build()); + TypeNode metadataType = + TypeNode.withReference( + VaporReference.builder() + .setName("WaitMetadata") + .setPakkage(SHOWCASE_PACKAGE_NAME) + .build()); + LongrunningOperation lro = LongrunningOperation.withTypes(responseType, metadataType); + Method method = + Method.builder() + .setName("Wait") + .setInputType(inputType) + .setOutputType(outputType) + .setMethodSignatures(Collections.emptyList()) + .setLro(lro) + .build(); + String results = + ServiceClientSampleCodeComposer.composeRpcDefaultMethodHeaderSampleCode( + method, clientType, resourceNames, messageTypes); + String expected = + LineFormatter.lines( + "try (EchoClient echoClient = EchoClient.create()) {\n", + " WaitRequest request = WaitRequest.newBuilder().build();\n", + " WaitResponse response = echoClient.waitAsync(request).get();\n", + "}"); + assertEquals(results, expected); + } + + @Test + public void validComposeRpcDefaultMethodHeaderSampleCode_pureUnaryReturnVoid() { + FileDescriptor echoFileDescriptor = EchoOuterClass.getDescriptor(); + Map resourceNames = Parser.parseResourceNames(echoFileDescriptor); + Map messageTypes = Parser.parseMessages(echoFileDescriptor); + TypeNode clientType = + TypeNode.withReference( + VaporReference.builder() + .setName("EchoClient") + .setPakkage(SHOWCASE_PACKAGE_NAME) + .build()); + TypeNode inputType = + TypeNode.withReference( + VaporReference.builder() + .setName("EchoRequest") + .setPakkage(SHOWCASE_PACKAGE_NAME) + .build()); + TypeNode outputType = + TypeNode.withReference( + VaporReference.builder().setName("Empty").setPakkage(PROTO_PACKAGE_NAME).build()); + Method method = + Method.builder() + .setName("Echo") + .setInputType(inputType) + .setOutputType(outputType) + .setMethodSignatures(Collections.emptyList()) + .build(); + String results = + ServiceClientSampleCodeComposer.composeRpcDefaultMethodHeaderSampleCode( + method, clientType, resourceNames, messageTypes); + String expected = + LineFormatter.lines( + "try (EchoClient echoClient = EchoClient.create()) {\n", + " EchoRequest request =\n", + " EchoRequest.newBuilder()\n", + " .setName(FoobarName.ofProjectFoobarName(\"[PROJECT]\", \"[FOOBAR]\").toString())\n", + " .setParent(FoobarName.ofProjectFoobarName(\"[PROJECT]\", \"[FOOBAR]\").toString())\n", + " .setFoobar(Foobar.newBuilder().build())\n", + " .build();\n", + " echoClient.echo(request);\n", + "}"); + assertEquals(results, expected); + } + + @Test + public void validComposeRpcDefaultMethodHeaderSampleCode_pureUnaryReturnResponse() { + FileDescriptor echoFileDescriptor = EchoOuterClass.getDescriptor(); + Map resourceNames = Parser.parseResourceNames(echoFileDescriptor); + Map messageTypes = Parser.parseMessages(echoFileDescriptor); + TypeNode clientType = + TypeNode.withReference( + VaporReference.builder() + .setName("EchoClient") + .setPakkage(SHOWCASE_PACKAGE_NAME) + .build()); + TypeNode inputType = + TypeNode.withReference( + VaporReference.builder() + .setName("EchoRequest") + .setPakkage(SHOWCASE_PACKAGE_NAME) + .build()); + TypeNode outputType = + TypeNode.withReference( + VaporReference.builder() + .setName("EchoResponse") + .setPakkage(SHOWCASE_PACKAGE_NAME) + .build()); + Method method = + Method.builder() + .setName("Echo") + .setInputType(inputType) + .setOutputType(outputType) + .setMethodSignatures(Collections.emptyList()) + .build(); + String results = + ServiceClientSampleCodeComposer.composeRpcDefaultMethodHeaderSampleCode( + method, clientType, resourceNames, messageTypes); + String expected = + LineFormatter.lines( + "try (EchoClient echoClient = EchoClient.create()) {\n", + " EchoRequest request =\n", + " EchoRequest.newBuilder()\n", + " .setName(FoobarName.ofProjectFoobarName(\"[PROJECT]\", \"[FOOBAR]\").toString())\n", + " .setParent(FoobarName.ofProjectFoobarName(\"[PROJECT]\", \"[FOOBAR]\").toString())\n", + " .setFoobar(Foobar.newBuilder().build())\n", + " .build();\n", + " EchoResponse response = echoClient.echo(request);\n", + "}"); + assertEquals(results, expected); + } } diff --git a/src/test/java/com/google/api/generator/gapic/composer/goldens/EchoClient.golden b/src/test/java/com/google/api/generator/gapic/composer/goldens/EchoClient.golden index f8d375314b..15d93be4eb 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/goldens/EchoClient.golden +++ b/src/test/java/com/google/api/generator/gapic/composer/goldens/EchoClient.golden @@ -300,6 +300,20 @@ public class EchoClient implements BackgroundResource { // AUTO-GENERATED DOCUMENTATION AND METHOD. /** + * Sample code: + * + *
{@code
+   * try (EchoClient echoClient = EchoClient.create()) {
+   *   EchoRequest request =
+   *       EchoRequest.newBuilder()
+   *           .setName(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString())
+   *           .setParent(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString())
+   *           .setFoobar(Foobar.newBuilder().build())
+   *           .build();
+   *   EchoResponse response = echoClient.echo(request);
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -339,6 +353,22 @@ public class EchoClient implements BackgroundResource { // AUTO-GENERATED DOCUMENTATION AND METHOD. /** + * Sample code: + * + *
{@code
+   * try (EchoClient echoClient = EchoClient.create()) {
+   *   PagedExpandRequest request =
+   *       PagedExpandRequest.newBuilder()
+   *           .setContent("content951530617")
+   *           .setPageSize(883849137)
+   *           .setPageToken("pageToken873572522")
+   *           .build();
+   *   for (EchoResponse element : echoClient.pagedExpand(request).iterateAll()) {
+   *     // doThingsWith(element);
+   *   }
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -381,6 +411,22 @@ public class EchoClient implements BackgroundResource { // AUTO-GENERATED DOCUMENTATION AND METHOD. /** + * Sample code: + * + *
{@code
+   * try (EchoClient echoClient = EchoClient.create()) {
+   *   PagedExpandRequest request =
+   *       PagedExpandRequest.newBuilder()
+   *           .setContent("content951530617")
+   *           .setPageSize(883849137)
+   *           .setPageToken("pageToken873572522")
+   *           .build();
+   *   for (EchoResponse element : echoClient.simplePagedExpand(request).iterateAll()) {
+   *     // doThingsWith(element);
+   *   }
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -441,6 +487,15 @@ public class EchoClient implements BackgroundResource { // AUTO-GENERATED DOCUMENTATION AND METHOD. /** + * Sample code: + * + *
{@code
+   * try (EchoClient echoClient = EchoClient.create()) {
+   *   WaitRequest request = WaitRequest.newBuilder().build();
+   *   WaitResponse response = echoClient.waitAsync(request).get();
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -462,6 +517,15 @@ public class EchoClient implements BackgroundResource { // AUTO-GENERATED DOCUMENTATION AND METHOD. /** + * Sample code: + * + *
{@code
+   * try (EchoClient echoClient = EchoClient.create()) {
+   *   BlockRequest request = BlockRequest.newBuilder().build();
+   *   BlockResponse response = echoClient.block(request);
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ diff --git a/src/test/java/com/google/api/generator/gapic/composer/goldens/IdentityClient.golden b/src/test/java/com/google/api/generator/gapic/composer/goldens/IdentityClient.golden index 8af5c700c1..cfc332034f 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/goldens/IdentityClient.golden +++ b/src/test/java/com/google/api/generator/gapic/composer/goldens/IdentityClient.golden @@ -201,6 +201,19 @@ public class IdentityClient implements BackgroundResource { // AUTO-GENERATED DOCUMENTATION AND METHOD. /** + * Sample code: + * + *
{@code
+   * try (IdentityClient identityClient = IdentityClient.create()) {
+   *   CreateUserRequest request =
+   *       CreateUserRequest.newBuilder()
+   *           .setParent(UserName.of("[USER]").toString())
+   *           .setUser(User.newBuilder().build())
+   *           .build();
+   *   User response = identityClient.createUser(request);
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -255,6 +268,16 @@ public class IdentityClient implements BackgroundResource { // AUTO-GENERATED DOCUMENTATION AND METHOD. /** + * Sample code: + * + *
{@code
+   * try (IdentityClient identityClient = IdentityClient.create()) {
+   *   GetUserRequest request =
+   *       GetUserRequest.newBuilder().setName(UserName.of("[USER]").toString()).build();
+   *   User response = identityClient.getUser(request);
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -270,6 +293,16 @@ public class IdentityClient implements BackgroundResource { // AUTO-GENERATED DOCUMENTATION AND METHOD. /** + * Sample code: + * + *
{@code
+   * try (IdentityClient identityClient = IdentityClient.create()) {
+   *   UpdateUserRequest request =
+   *       UpdateUserRequest.newBuilder().setUser(User.newBuilder().build()).build();
+   *   User response = identityClient.updateUser(request);
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -324,6 +357,16 @@ public class IdentityClient implements BackgroundResource { // AUTO-GENERATED DOCUMENTATION AND METHOD. /** + * Sample code: + * + *
{@code
+   * try (IdentityClient identityClient = IdentityClient.create()) {
+   *   DeleteUserRequest request =
+   *       DeleteUserRequest.newBuilder().setName(UserName.of("[USER]").toString()).build();
+   *   identityClient.deleteUser(request);
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -339,6 +382,21 @@ public class IdentityClient implements BackgroundResource { // AUTO-GENERATED DOCUMENTATION AND METHOD. /** + * Sample code: + * + *
{@code
+   * try (IdentityClient identityClient = IdentityClient.create()) {
+   *   ListUsersRequest request =
+   *       ListUsersRequest.newBuilder()
+   *           .setPageSize(883849137)
+   *           .setPageToken("pageToken873572522")
+   *           .build();
+   *   for (User element : identityClient.listUsers(request).iterateAll()) {
+   *     // doThingsWith(element);
+   *   }
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ diff --git a/test/integration/goldens/asset/AssetServiceClient.java b/test/integration/goldens/asset/AssetServiceClient.java index ce97a13c41..24702ea4f4 100644 --- a/test/integration/goldens/asset/AssetServiceClient.java +++ b/test/integration/goldens/asset/AssetServiceClient.java @@ -167,6 +167,21 @@ public final OperationsClient getOperationsClient() { * exponential retry to poll the export operation result. For regular-size resource parent, the * export operation usually finishes within 5 minutes. * + *

Sample code: + * + *

{@code
+   * try (AssetServiceClient assetServiceClient = AssetServiceClient.create()) {
+   *   ExportAssetsRequest request =
+   *       ExportAssetsRequest.newBuilder()
+   *           .setParent(FeedName.ofProjectFeedName("[PROJECT]", "[FEED]").toString())
+   *           .setReadTime(Timestamp.newBuilder().build())
+   *           .addAllAssetTypes(new ArrayList())
+   *           .setOutputConfig(OutputConfig.newBuilder().build())
+   *           .build();
+   *   ExportAssetsResponse response = assetServiceClient.exportAssetsAsync(request).get();
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -218,6 +233,20 @@ public final UnaryCallable exportAssetsCallable( * or deleted status. If a specified asset does not exist, this API returns an INVALID_ARGUMENT * error. * + *

Sample code: + * + *

{@code
+   * try (AssetServiceClient assetServiceClient = AssetServiceClient.create()) {
+   *   BatchGetAssetsHistoryRequest request =
+   *       BatchGetAssetsHistoryRequest.newBuilder()
+   *           .setParent(FeedName.ofProjectFeedName("[PROJECT]", "[FEED]").toString())
+   *           .addAllAssetNames(new ArrayList())
+   *           .setReadTimeWindow(TimeWindow.newBuilder().build())
+   *           .build();
+   *   BatchGetAssetsHistoryResponse response = assetServiceClient.batchGetAssetsHistory(request);
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -269,6 +298,20 @@ public final Feed createFeed(String parent) { /** * Creates a feed in a parent project/folder/organization to listen to its asset updates. * + *

Sample code: + * + *

{@code
+   * try (AssetServiceClient assetServiceClient = AssetServiceClient.create()) {
+   *   CreateFeedRequest request =
+   *       CreateFeedRequest.newBuilder()
+   *           .setParent("parent-995424086")
+   *           .setFeedId("feedId-1278410919")
+   *           .setFeed(Feed.newBuilder().build())
+   *           .build();
+   *   Feed response = assetServiceClient.createFeed(request);
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -337,6 +380,18 @@ public final Feed getFeed(String name) { /** * Gets details about an asset feed. * + *

Sample code: + * + *

{@code
+   * try (AssetServiceClient assetServiceClient = AssetServiceClient.create()) {
+   *   GetFeedRequest request =
+   *       GetFeedRequest.newBuilder()
+   *           .setName(FeedName.ofProjectFeedName("[PROJECT]", "[FEED]").toString())
+   *           .build();
+   *   Feed response = assetServiceClient.getFeed(request);
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -381,6 +436,16 @@ public final ListFeedsResponse listFeeds(String parent) { /** * Lists all asset feeds in a parent project/folder/organization. * + *

Sample code: + * + *

{@code
+   * try (AssetServiceClient assetServiceClient = AssetServiceClient.create()) {
+   *   ListFeedsRequest request =
+   *       ListFeedsRequest.newBuilder().setParent("parent-995424086").build();
+   *   ListFeedsResponse response = assetServiceClient.listFeeds(request);
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -425,6 +490,19 @@ public final Feed updateFeed(Feed feed) { /** * Updates an asset feed configuration. * + *

Sample code: + * + *

{@code
+   * try (AssetServiceClient assetServiceClient = AssetServiceClient.create()) {
+   *   UpdateFeedRequest request =
+   *       UpdateFeedRequest.newBuilder()
+   *           .setFeed(Feed.newBuilder().build())
+   *           .setUpdateMask(FieldMask.newBuilder().build())
+   *           .build();
+   *   Feed response = assetServiceClient.updateFeed(request);
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -493,6 +571,18 @@ public final void deleteFeed(String name) { /** * Deletes an asset feed. * + *

Sample code: + * + *

{@code
+   * try (AssetServiceClient assetServiceClient = AssetServiceClient.create()) {
+   *   DeleteFeedRequest request =
+   *       DeleteFeedRequest.newBuilder()
+   *           .setName(FeedName.ofProjectFeedName("[PROJECT]", "[FEED]").toString())
+   *           .build();
+   *   assetServiceClient.deleteFeed(request);
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -596,6 +686,26 @@ public final SearchAllResourcesPagedResponse searchAllResources( * organization. The caller must be granted the `cloudasset.assets.searchAllResources` permission * on the desired scope, otherwise the request will be rejected. * + *

Sample code: + * + *

{@code
+   * try (AssetServiceClient assetServiceClient = AssetServiceClient.create()) {
+   *   SearchAllResourcesRequest request =
+   *       SearchAllResourcesRequest.newBuilder()
+   *           .setScope("scope109264468")
+   *           .setQuery("query107944136")
+   *           .addAllAssetTypes(new ArrayList())
+   *           .setPageSize(883849137)
+   *           .setPageToken("pageToken873572522")
+   *           .setOrderBy("orderBy-1207110587")
+   *           .build();
+   *   for (ResourceSearchResult element :
+   *       assetServiceClient.searchAllResources(request).iterateAll()) {
+   *     // doThingsWith(element);
+   *   }
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -699,6 +809,24 @@ public final SearchAllIamPoliciesPagedResponse searchAllIamPolicies(String scope * organization. The caller must be granted the `cloudasset.assets.searchAllIamPolicies` * permission on the desired scope, otherwise the request will be rejected. * + *

Sample code: + * + *

{@code
+   * try (AssetServiceClient assetServiceClient = AssetServiceClient.create()) {
+   *   SearchAllIamPoliciesRequest request =
+   *       SearchAllIamPoliciesRequest.newBuilder()
+   *           .setScope("scope109264468")
+   *           .setQuery("query107944136")
+   *           .setPageSize(883849137)
+   *           .setPageToken("pageToken873572522")
+   *           .build();
+   *   for (IamPolicySearchResult element :
+   *       assetServiceClient.searchAllIamPolicies(request).iterateAll()) {
+   *     // doThingsWith(element);
+   *   }
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ diff --git a/test/integration/goldens/library/LibraryServiceClient.java b/test/integration/goldens/library/LibraryServiceClient.java index ea2d0df8b3..67c5a4a57b 100644 --- a/test/integration/goldens/library/LibraryServiceClient.java +++ b/test/integration/goldens/library/LibraryServiceClient.java @@ -191,6 +191,16 @@ public final Shelf createShelf(Shelf shelf) { /** * Creates a shelf, and returns the new Shelf. * + *

Sample code: + * + *

{@code
+   * try (LibraryServiceClient libraryServiceClient = LibraryServiceClient.create()) {
+   *   CreateShelfRequest request =
+   *       CreateShelfRequest.newBuilder().setShelf(Shelf.newBuilder().build()).build();
+   *   Shelf response = libraryServiceClient.createShelf(request);
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -255,6 +265,15 @@ public final Shelf getShelf(String name) { /** * Gets a shelf. Returns NOT_FOUND if the shelf does not exist. * + *

Sample code: + * + *

{@code
+   * try (LibraryServiceClient libraryServiceClient = LibraryServiceClient.create()) {
+   *   GetShelfRequest request = GetShelfRequest.newBuilder().setName("name3373707").build();
+   *   Shelf response = libraryServiceClient.getShelf(request);
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -277,6 +296,21 @@ public final UnaryCallable getShelfCallable() { * Lists shelves. The order is unspecified but deterministic. Newly created shelves will not * necessarily be added to the end of this list. * + *

Sample code: + * + *

{@code
+   * try (LibraryServiceClient libraryServiceClient = LibraryServiceClient.create()) {
+   *   ListShelvesRequest request =
+   *       ListShelvesRequest.newBuilder()
+   *           .setPageSize(883849137)
+   *           .setPageToken("pageToken873572522")
+   *           .build();
+   *   for (Shelf element : libraryServiceClient.listShelves(request).iterateAll()) {
+   *     // doThingsWith(element);
+   *   }
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -354,6 +388,15 @@ public final void deleteShelf(String name) { /** * Deletes a shelf. Returns NOT_FOUND if the shelf does not exist. * + *

Sample code: + * + *

{@code
+   * try (LibraryServiceClient libraryServiceClient = LibraryServiceClient.create()) {
+   *   DeleteShelfRequest request = DeleteShelfRequest.newBuilder().setName("name3373707").build();
+   *   libraryServiceClient.deleteShelf(request);
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -505,6 +548,19 @@ public final Shelf mergeShelves(String name, String otherShelfName) { *

Returns NOT_FOUND if either shelf does not exist. This call is a no-op if the specified * shelves are the same. * + *

Sample code: + * + *

{@code
+   * try (LibraryServiceClient libraryServiceClient = LibraryServiceClient.create()) {
+   *   MergeShelvesRequest request =
+   *       MergeShelvesRequest.newBuilder()
+   *           .setName("name3373707")
+   *           .setOtherShelfName("otherShelfName-1942963547")
+   *           .build();
+   *   Shelf response = libraryServiceClient.mergeShelves(request);
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -581,6 +637,19 @@ public final Book createBook(String name, Book book) { /** * Creates a book, and returns the new Book. * + *

Sample code: + * + *

{@code
+   * try (LibraryServiceClient libraryServiceClient = LibraryServiceClient.create()) {
+   *   CreateBookRequest request =
+   *       CreateBookRequest.newBuilder()
+   *           .setName("name3373707")
+   *           .setBook(Book.newBuilder().build())
+   *           .build();
+   *   Book response = libraryServiceClient.createBook(request);
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -645,6 +714,15 @@ public final Book getBook(String name) { /** * Gets a book. Returns NOT_FOUND if the book does not exist. * + *

Sample code: + * + *

{@code
+   * try (LibraryServiceClient libraryServiceClient = LibraryServiceClient.create()) {
+   *   GetBookRequest request = GetBookRequest.newBuilder().setName("name3373707").build();
+   *   Book response = libraryServiceClient.getBook(request);
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -719,6 +797,22 @@ public final ListBooksPagedResponse listBooks(String name) { * not necessarily be added to the end of this list. Returns NOT_FOUND if the shelf does not * exist. * + *

Sample code: + * + *

{@code
+   * try (LibraryServiceClient libraryServiceClient = LibraryServiceClient.create()) {
+   *   ListBooksRequest request =
+   *       ListBooksRequest.newBuilder()
+   *           .setName("name3373707")
+   *           .setPageSize(883849137)
+   *           .setPageToken("pageToken873572522")
+   *           .build();
+   *   for (Book element : libraryServiceClient.listBooks(request).iterateAll()) {
+   *     // doThingsWith(element);
+   *   }
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -754,6 +848,15 @@ public final UnaryCallable listBooksCallabl /** * Deletes a book. Returns NOT_FOUND if the book does not exist. * + *

Sample code: + * + *

{@code
+   * try (LibraryServiceClient libraryServiceClient = LibraryServiceClient.create()) {
+   *   DeleteBookRequest request = DeleteBookRequest.newBuilder().setName("name3373707").build();
+   *   libraryServiceClient.deleteBook(request);
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -798,6 +901,19 @@ public final Book updateBook(Book book) { * Updates a book. Returns INVALID_ARGUMENT if the name of the book is non-empty and does not * equal the existing name. * + *

Sample code: + * + *

{@code
+   * try (LibraryServiceClient libraryServiceClient = LibraryServiceClient.create()) {
+   *   UpdateBookRequest request =
+   *       UpdateBookRequest.newBuilder()
+   *           .setName("name3373707")
+   *           .setBook(Book.newBuilder().build())
+   *           .build();
+   *   Book response = libraryServiceClient.updateBook(request);
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -930,6 +1046,19 @@ public final Book moveBook(String name, String otherShelfName) { * Moves a book to another shelf, and returns the new book. The book id of the new book may not be * the same as the original book. * + *

Sample code: + * + *

{@code
+   * try (LibraryServiceClient libraryServiceClient = LibraryServiceClient.create()) {
+   *   MoveBookRequest request =
+   *       MoveBookRequest.newBuilder()
+   *           .setName("name3373707")
+   *           .setOtherShelfName("otherShelfName-1942963547")
+   *           .build();
+   *   Book response = libraryServiceClient.moveBook(request);
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ diff --git a/test/integration/goldens/logging/ConfigClient.java b/test/integration/goldens/logging/ConfigClient.java index 75930c33de..8f3b649b27 100644 --- a/test/integration/goldens/logging/ConfigClient.java +++ b/test/integration/goldens/logging/ConfigClient.java @@ -336,6 +336,24 @@ public final ListBucketsPagedResponse listBuckets(String parent) { /** * Lists buckets (Beta). * + *

Sample code: + * + *

{@code
+   * try (ConfigClient configClient = ConfigClient.create()) {
+   *   ListBucketsRequest request =
+   *       ListBucketsRequest.newBuilder()
+   *           .setParent(
+   *               LogBucketName.ofProjectLocationBucketName("[PROJECT]", "[LOCATION]", "[BUCKET]")
+   *                   .toString())
+   *           .setPageToken("pageToken873572522")
+   *           .setPageSize(883849137)
+   *           .build();
+   *   for (LogBucket element : configClient.listBuckets(request).iterateAll()) {
+   *     // doThingsWith(element);
+   *   }
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -368,6 +386,20 @@ public final UnaryCallable listBucketsC /** * Gets a bucket (Beta). * + *

Sample code: + * + *

{@code
+   * try (ConfigClient configClient = ConfigClient.create()) {
+   *   GetBucketRequest request =
+   *       GetBucketRequest.newBuilder()
+   *           .setName(
+   *               LogBucketName.ofProjectLocationBucketName("[PROJECT]", "[LOCATION]", "[BUCKET]")
+   *                   .toString())
+   *           .build();
+   *   LogBucket response = configClient.getBucket(request);
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -398,6 +430,22 @@ public final UnaryCallable getBucketCallable() { * *

A buckets region may not be modified after it is created. This method is in Beta. * + *

Sample code: + * + *

{@code
+   * try (ConfigClient configClient = ConfigClient.create()) {
+   *   UpdateBucketRequest request =
+   *       UpdateBucketRequest.newBuilder()
+   *           .setName(
+   *               LogBucketName.ofProjectLocationBucketName("[PROJECT]", "[LOCATION]", "[BUCKET]")
+   *                   .toString())
+   *           .setBucket(LogBucket.newBuilder().build())
+   *           .setUpdateMask(FieldMask.newBuilder().build())
+   *           .build();
+   *   LogBucket response = configClient.updateBucket(request);
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -557,6 +605,22 @@ public final ListSinksPagedResponse listSinks(String parent) { /** * Lists sinks. * + *

Sample code: + * + *

{@code
+   * try (ConfigClient configClient = ConfigClient.create()) {
+   *   ListSinksRequest request =
+   *       ListSinksRequest.newBuilder()
+   *           .setParent(LogSinkName.ofProjectSinkName("[PROJECT]", "[SINK]").toString())
+   *           .setPageToken("pageToken873572522")
+   *           .setPageSize(883849137)
+   *           .build();
+   *   for (LogSink element : configClient.listSinks(request).iterateAll()) {
+   *     // doThingsWith(element);
+   *   }
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -643,6 +707,18 @@ public final LogSink getSink(String sinkName) { /** * Gets a sink. * + *

Sample code: + * + *

{@code
+   * try (ConfigClient configClient = ConfigClient.create()) {
+   *   GetSinkRequest request =
+   *       GetSinkRequest.newBuilder()
+   *           .setSinkName(LogSinkName.ofProjectSinkName("[PROJECT]", "[SINK]").toString())
+   *           .build();
+   *   LogSink response = configClient.getSink(request);
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -834,6 +910,20 @@ public final LogSink createSink(String parent, LogSink sink) { * permitted to write to the destination. A sink can export log entries only from the resource * owning the sink. * + *

Sample code: + * + *

{@code
+   * try (ConfigClient configClient = ConfigClient.create()) {
+   *   CreateSinkRequest request =
+   *       CreateSinkRequest.newBuilder()
+   *           .setParent(LogSinkName.ofProjectSinkName("[PROJECT]", "[SINK]").toString())
+   *           .setSink(LogSink.newBuilder().build())
+   *           .setUniqueWriterIdentity(true)
+   *           .build();
+   *   LogSink response = configClient.createSink(request);
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -1033,6 +1123,21 @@ public final LogSink updateSink(String sinkName, LogSink sink, FieldMask updateM *

The updated sink might also have a new `writer_identity`; see the `unique_writer_identity` * field. * + *

Sample code: + * + *

{@code
+   * try (ConfigClient configClient = ConfigClient.create()) {
+   *   UpdateSinkRequest request =
+   *       UpdateSinkRequest.newBuilder()
+   *           .setSinkName(LogSinkName.ofProjectSinkName("[PROJECT]", "[SINK]").toString())
+   *           .setSink(LogSink.newBuilder().build())
+   *           .setUniqueWriterIdentity(true)
+   *           .setUpdateMask(FieldMask.newBuilder().build())
+   *           .build();
+   *   LogSink response = configClient.updateSink(request);
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -1118,6 +1223,18 @@ public final void deleteSink(String sinkName) { * Deletes a sink. If the sink has a unique `writer_identity`, then that service account is also * deleted. * + *

Sample code: + * + *

{@code
+   * try (ConfigClient configClient = ConfigClient.create()) {
+   *   DeleteSinkRequest request =
+   *       DeleteSinkRequest.newBuilder()
+   *           .setSinkName(LogSinkName.ofProjectSinkName("[PROJECT]", "[SINK]").toString())
+   *           .build();
+   *   configClient.deleteSink(request);
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -1278,6 +1395,23 @@ public final ListExclusionsPagedResponse listExclusions(String parent) { /** * Lists all the exclusions in a parent resource. * + *

Sample code: + * + *

{@code
+   * try (ConfigClient configClient = ConfigClient.create()) {
+   *   ListExclusionsRequest request =
+   *       ListExclusionsRequest.newBuilder()
+   *           .setParent(
+   *               LogExclusionName.ofProjectExclusionName("[PROJECT]", "[EXCLUSION]").toString())
+   *           .setPageToken("pageToken873572522")
+   *           .setPageSize(883849137)
+   *           .build();
+   *   for (LogExclusion element : configClient.listExclusions(request).iterateAll()) {
+   *     // doThingsWith(element);
+   *   }
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -1364,6 +1498,19 @@ public final LogExclusion getExclusion(String name) { /** * Gets the description of an exclusion. * + *

Sample code: + * + *

{@code
+   * try (ConfigClient configClient = ConfigClient.create()) {
+   *   GetExclusionRequest request =
+   *       GetExclusionRequest.newBuilder()
+   *           .setName(
+   *               LogExclusionName.ofProjectExclusionName("[PROJECT]", "[EXCLUSION]").toString())
+   *           .build();
+   *   LogExclusion response = configClient.getExclusion(request);
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -1544,6 +1691,20 @@ public final LogExclusion createExclusion(String parent, LogExclusion exclusion) * Creates a new exclusion in a specified parent resource. Only log entries belonging to that * resource can be excluded. You can have up to 10 exclusions in a resource. * + *

Sample code: + * + *

{@code
+   * try (ConfigClient configClient = ConfigClient.create()) {
+   *   CreateExclusionRequest request =
+   *       CreateExclusionRequest.newBuilder()
+   *           .setParent(
+   *               LogExclusionName.ofProjectExclusionName("[PROJECT]", "[EXCLUSION]").toString())
+   *           .setExclusion(LogExclusion.newBuilder().build())
+   *           .build();
+   *   LogExclusion response = configClient.createExclusion(request);
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -1650,6 +1811,21 @@ public final LogExclusion updateExclusion( /** * Changes one or more properties of an existing exclusion. * + *

Sample code: + * + *

{@code
+   * try (ConfigClient configClient = ConfigClient.create()) {
+   *   UpdateExclusionRequest request =
+   *       UpdateExclusionRequest.newBuilder()
+   *           .setName(
+   *               LogExclusionName.ofProjectExclusionName("[PROJECT]", "[EXCLUSION]").toString())
+   *           .setExclusion(LogExclusion.newBuilder().build())
+   *           .setUpdateMask(FieldMask.newBuilder().build())
+   *           .build();
+   *   LogExclusion response = configClient.updateExclusion(request);
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -1724,6 +1900,19 @@ public final void deleteExclusion(String name) { /** * Deletes an exclusion. * + *

Sample code: + * + *

{@code
+   * try (ConfigClient configClient = ConfigClient.create()) {
+   *   DeleteExclusionRequest request =
+   *       DeleteExclusionRequest.newBuilder()
+   *           .setName(
+   *               LogExclusionName.ofProjectExclusionName("[PROJECT]", "[EXCLUSION]").toString())
+   *           .build();
+   *   configClient.deleteExclusion(request);
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -1751,6 +1940,18 @@ public final UnaryCallable deleteExclusionCallabl *

See [Enabling CMEK for Logs * Router](https://cloud.google.com/logging/docs/routing/managed-encryption) for more information. * + *

Sample code: + * + *

{@code
+   * try (ConfigClient configClient = ConfigClient.create()) {
+   *   GetCmekSettingsRequest request =
+   *       GetCmekSettingsRequest.newBuilder()
+   *           .setName(CmekSettingsName.ofProjectName("[PROJECT]").toString())
+   *           .build();
+   *   CmekSettings response = configClient.getCmekSettings(request);
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -1789,6 +1990,20 @@ public final UnaryCallable getCmekSettings *

See [Enabling CMEK for Logs * Router](https://cloud.google.com/logging/docs/routing/managed-encryption) for more information. * + *

Sample code: + * + *

{@code
+   * try (ConfigClient configClient = ConfigClient.create()) {
+   *   UpdateCmekSettingsRequest request =
+   *       UpdateCmekSettingsRequest.newBuilder()
+   *           .setName("name3373707")
+   *           .setCmekSettings(CmekSettings.newBuilder().build())
+   *           .setUpdateMask(FieldMask.newBuilder().build())
+   *           .build();
+   *   CmekSettings response = configClient.updateCmekSettings(request);
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ diff --git a/test/integration/goldens/logging/LoggingClient.java b/test/integration/goldens/logging/LoggingClient.java index c893af954a..db2dcc3bdc 100644 --- a/test/integration/goldens/logging/LoggingClient.java +++ b/test/integration/goldens/logging/LoggingClient.java @@ -222,6 +222,18 @@ public final void deleteLog(String logName) { * written shortly before the delete operation might not be deleted. Entries received after the * delete operation with a timestamp before the operation will be deleted. * + *

Sample code: + * + *

{@code
+   * try (LoggingClient loggingClient = LoggingClient.create()) {
+   *   DeleteLogRequest request =
+   *       DeleteLogRequest.newBuilder()
+   *           .setLogName(LogName.ofProjectLogName("[PROJECT]", "[LOG]").toString())
+   *           .build();
+   *   loggingClient.deleteLog(request);
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -398,6 +410,23 @@ public final WriteLogEntriesResponse writeLogEntries( * libraries configured to use Logging. A single request may contain log entries for a maximum of * 1000 different resources (projects, organizations, billing accounts or folders) * + *

Sample code: + * + *

{@code
+   * try (LoggingClient loggingClient = LoggingClient.create()) {
+   *   WriteLogEntriesRequest request =
+   *       WriteLogEntriesRequest.newBuilder()
+   *           .setLogName(LogName.ofProjectLogName("[PROJECT]", "[LOG]").toString())
+   *           .setResource(MonitoredResource.newBuilder().build())
+   *           .putAllLabels(new HashMap())
+   *           .addAllEntries(new ArrayList())
+   *           .setPartialSuccess(true)
+   *           .setDryRun(true)
+   *           .build();
+   *   WriteLogEntriesResponse response = loggingClient.writeLogEntries(request);
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -474,6 +503,24 @@ public final ListLogEntriesPagedResponse listLogEntries( * project/folder/organization/billing account. For ways to export log entries, see [Exporting * Logs](https://cloud.google.com/logging/docs/export). * + *

Sample code: + * + *

{@code
+   * try (LoggingClient loggingClient = LoggingClient.create()) {
+   *   ListLogEntriesRequest request =
+   *       ListLogEntriesRequest.newBuilder()
+   *           .addAllResourceNames(new ArrayList())
+   *           .setFilter("filter-1274492040")
+   *           .setOrderBy("orderBy-1207110587")
+   *           .setPageSize(883849137)
+   *           .setPageToken("pageToken873572522")
+   *           .build();
+   *   for (LogEntry element : loggingClient.listLogEntries(request).iterateAll()) {
+   *     // doThingsWith(element);
+   *   }
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -511,6 +558,22 @@ public final ListLogEntriesPagedResponse listLogEntries(ListLogEntriesRequest re /** * Lists the descriptors for monitored resource types used by Logging. * + *

Sample code: + * + *

{@code
+   * try (LoggingClient loggingClient = LoggingClient.create()) {
+   *   ListMonitoredResourceDescriptorsRequest request =
+   *       ListMonitoredResourceDescriptorsRequest.newBuilder()
+   *           .setPageSize(883849137)
+   *           .setPageToken("pageToken873572522")
+   *           .build();
+   *   for (MonitoredResourceDescriptor element :
+   *       loggingClient.listMonitoredResourceDescriptors(request).iterateAll()) {
+   *     // doThingsWith(element);
+   *   }
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -682,6 +745,22 @@ public final ListLogsPagedResponse listLogs(String parent) { * Lists the logs in projects, organizations, folders, or billing accounts. Only logs that have * entries are listed. * + *

Sample code: + * + *

{@code
+   * try (LoggingClient loggingClient = LoggingClient.create()) {
+   *   ListLogsRequest request =
+   *       ListLogsRequest.newBuilder()
+   *           .setParent(LogName.ofProjectLogName("[PROJECT]", "[LOG]").toString())
+   *           .setPageSize(883849137)
+   *           .setPageToken("pageToken873572522")
+   *           .build();
+   *   for (String element : loggingClient.listLogs(request).iterateAll()) {
+   *     // doThingsWith(element);
+   *   }
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ diff --git a/test/integration/goldens/logging/MetricsClient.java b/test/integration/goldens/logging/MetricsClient.java index e63a29ac2e..c3a53ec4b8 100644 --- a/test/integration/goldens/logging/MetricsClient.java +++ b/test/integration/goldens/logging/MetricsClient.java @@ -203,6 +203,22 @@ public final ListLogMetricsPagedResponse listLogMetrics(String parent) { /** * Lists logs-based metrics. * + *

Sample code: + * + *

{@code
+   * try (MetricsClient metricsClient = MetricsClient.create()) {
+   *   ListLogMetricsRequest request =
+   *       ListLogMetricsRequest.newBuilder()
+   *           .setParent(ProjectName.of("[PROJECT]").toString())
+   *           .setPageToken("pageToken873572522")
+   *           .setPageSize(883849137)
+   *           .build();
+   *   for (LogMetric element : metricsClient.listLogMetrics(request).iterateAll()) {
+   *     // doThingsWith(element);
+   *   }
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -284,6 +300,18 @@ public final LogMetric getLogMetric(String metricName) { /** * Gets a logs-based metric. * + *

Sample code: + * + *

{@code
+   * try (MetricsClient metricsClient = MetricsClient.create()) {
+   *   GetLogMetricRequest request =
+   *       GetLogMetricRequest.newBuilder()
+   *           .setMetricName(LogMetricName.of("[PROJECT]", "[METRIC]").toString())
+   *           .build();
+   *   LogMetric response = metricsClient.getLogMetric(request);
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -362,6 +390,19 @@ public final LogMetric createLogMetric(String parent, LogMetric metric) { /** * Creates a logs-based metric. * + *

Sample code: + * + *

{@code
+   * try (MetricsClient metricsClient = MetricsClient.create()) {
+   *   CreateLogMetricRequest request =
+   *       CreateLogMetricRequest.newBuilder()
+   *           .setParent(LogMetricName.of("[PROJECT]", "[METRIC]").toString())
+   *           .setMetric(LogMetric.newBuilder().build())
+   *           .build();
+   *   LogMetric response = metricsClient.createLogMetric(request);
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -442,6 +483,19 @@ public final LogMetric updateLogMetric(String metricName, LogMetric metric) { /** * Creates or updates a logs-based metric. * + *

Sample code: + * + *

{@code
+   * try (MetricsClient metricsClient = MetricsClient.create()) {
+   *   UpdateLogMetricRequest request =
+   *       UpdateLogMetricRequest.newBuilder()
+   *           .setMetricName(LogMetricName.of("[PROJECT]", "[METRIC]").toString())
+   *           .setMetric(LogMetric.newBuilder().build())
+   *           .build();
+   *   LogMetric response = metricsClient.updateLogMetric(request);
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -511,6 +565,18 @@ public final void deleteLogMetric(String metricName) { /** * Deletes a logs-based metric. * + *

Sample code: + * + *

{@code
+   * try (MetricsClient metricsClient = MetricsClient.create()) {
+   *   DeleteLogMetricRequest request =
+   *       DeleteLogMetricRequest.newBuilder()
+   *           .setMetricName(LogMetricName.of("[PROJECT]", "[METRIC]").toString())
+   *           .build();
+   *   metricsClient.deleteLogMetric(request);
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ diff --git a/test/integration/goldens/redis/CloudRedisClient.java b/test/integration/goldens/redis/CloudRedisClient.java index 01c2dc68d9..72ebd6d532 100644 --- a/test/integration/goldens/redis/CloudRedisClient.java +++ b/test/integration/goldens/redis/CloudRedisClient.java @@ -260,6 +260,22 @@ public final ListInstancesPagedResponse listInstances(String parent) { *

If `location_id` is specified as `-` (wildcard), then all regions available to the project * are queried, and the results are aggregated. * + *

Sample code: + * + *

{@code
+   * try (CloudRedisClient cloudRedisClient = CloudRedisClient.create()) {
+   *   ListInstancesRequest request =
+   *       ListInstancesRequest.newBuilder()
+   *           .setParent(LocationName.of("[PROJECT]", "[LOCATION]").toString())
+   *           .setPageSize(883849137)
+   *           .setPageToken("pageToken873572522")
+   *           .build();
+   *   for (Instance element : cloudRedisClient.listInstances(request).iterateAll()) {
+   *     // doThingsWith(element);
+   *   }
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -359,6 +375,18 @@ public final Instance getInstance(String name) { /** * Gets the details of a specific Redis instance. * + *

Sample code: + * + *

{@code
+   * try (CloudRedisClient cloudRedisClient = CloudRedisClient.create()) {
+   *   GetInstanceRequest request =
+   *       GetInstanceRequest.newBuilder()
+   *           .setName(InstanceName.of("[PROJECT]", "[LOCATION]", "[INSTANCE]").toString())
+   *           .build();
+   *   Instance response = cloudRedisClient.getInstance(request);
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -495,6 +523,20 @@ public final OperationFuture createInstanceAsync( *

The returned operation is automatically deleted after a few hours, so there is no need to * call DeleteOperation. * + *

Sample code: + * + *

{@code
+   * try (CloudRedisClient cloudRedisClient = CloudRedisClient.create()) {
+   *   CreateInstanceRequest request =
+   *       CreateInstanceRequest.newBuilder()
+   *           .setParent(LocationName.of("[PROJECT]", "[LOCATION]").toString())
+   *           .setInstanceId("instanceId902024336")
+   *           .setInstance(Instance.newBuilder().build())
+   *           .build();
+   *   Instance response = cloudRedisClient.createInstanceAsync(request).get();
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -586,6 +628,19 @@ public final OperationFuture updateInstanceAsync( * The returned operation is automatically deleted after a few hours, so there is no need to call * DeleteOperation. * + *

Sample code: + * + *

{@code
+   * try (CloudRedisClient cloudRedisClient = CloudRedisClient.create()) {
+   *   UpdateInstanceRequest request =
+   *       UpdateInstanceRequest.newBuilder()
+   *           .setUpdateMask(FieldMask.newBuilder().build())
+   *           .setInstance(Instance.newBuilder().build())
+   *           .build();
+   *   Instance response = cloudRedisClient.updateInstanceAsync(request).get();
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -684,6 +739,19 @@ public final OperationFuture upgradeInstanceAsync( /** * Upgrades Redis instance to the newer Redis version specified in the request. * + *

Sample code: + * + *

{@code
+   * try (CloudRedisClient cloudRedisClient = CloudRedisClient.create()) {
+   *   UpgradeInstanceRequest request =
+   *       UpgradeInstanceRequest.newBuilder()
+   *           .setName(InstanceName.of("[PROJECT]", "[LOCATION]", "[INSTANCE]").toString())
+   *           .setRedisVersion("redisVersion-1972584739")
+   *           .build();
+   *   Instance response = cloudRedisClient.upgradeInstanceAsync(request).get();
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -756,6 +824,19 @@ public final OperationFuture importInstanceAsync( *

The returned operation is automatically deleted after a few hours, so there is no need to * call DeleteOperation. * + *

Sample code: + * + *

{@code
+   * try (CloudRedisClient cloudRedisClient = CloudRedisClient.create()) {
+   *   ImportInstanceRequest request =
+   *       ImportInstanceRequest.newBuilder()
+   *           .setName("name3373707")
+   *           .setInputConfig(InputConfig.newBuilder().build())
+   *           .build();
+   *   Instance response = cloudRedisClient.importInstanceAsync(request).get();
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -838,6 +919,19 @@ public final OperationFuture exportInstanceAsync( *

The returned operation is automatically deleted after a few hours, so there is no need to * call DeleteOperation. * + *

Sample code: + * + *

{@code
+   * try (CloudRedisClient cloudRedisClient = CloudRedisClient.create()) {
+   *   ExportInstanceRequest request =
+   *       ExportInstanceRequest.newBuilder()
+   *           .setName("name3373707")
+   *           .setOutputConfig(OutputConfig.newBuilder().build())
+   *           .build();
+   *   Instance response = cloudRedisClient.exportInstanceAsync(request).get();
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -948,6 +1042,18 @@ public final OperationFuture failoverInstanceAsync( * Initiates a failover of the master node to current replica node for a specific STANDARD tier * Cloud Memorystore for Redis instance. * + *

Sample code: + * + *

{@code
+   * try (CloudRedisClient cloudRedisClient = CloudRedisClient.create()) {
+   *   FailoverInstanceRequest request =
+   *       FailoverInstanceRequest.newBuilder()
+   *           .setName(InstanceName.of("[PROJECT]", "[LOCATION]", "[INSTANCE]").toString())
+   *           .build();
+   *   Instance response = cloudRedisClient.failoverInstanceAsync(request).get();
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @@ -1030,6 +1136,18 @@ public final OperationFuture deleteInstanceAsync(Strin /** * Deletes a specific Redis instance. Instance stops serving and data is deleted. * + *

Sample code: + * + *

{@code
+   * try (CloudRedisClient cloudRedisClient = CloudRedisClient.create()) {
+   *   DeleteInstanceRequest request =
+   *       DeleteInstanceRequest.newBuilder()
+   *           .setName(InstanceName.of("[PROJECT]", "[LOCATION]", "[INSTANCE]").toString())
+   *           .build();
+   *   cloudRedisClient.deleteInstanceAsync(request).get();
+   * }
+   * }
+ * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */