@@ -140,11 +140,7 @@ public GapicClass generate(GapicContext context, Service service) {
140140 Map <String , Message > messageTypes = context .messages ();
141141 String pakkage = service .pakkage ();
142142 TypeStore typeStore = new TypeStore ();
143- addDynamicTypes (service , typeStore );
144- for (Service mixinService : context .mixinServices ()) {
145- addDynamicTypes (mixinService , typeStore );
146- }
147-
143+ addDynamicTypes (context , service , typeStore );
148144 String className = ClassNames .getServiceClientTestClassName (service );
149145 GapicClass .Kind kind = Kind .MAIN ;
150146
@@ -466,6 +462,7 @@ private static List<MethodDefinition> createTestMethods(
466462 javaMethods .add (
467463 createRpcTestMethod (
468464 method ,
465+ service ,
469466 matchingService ,
470467 Collections .emptyList (),
471468 0 ,
@@ -487,6 +484,7 @@ private static List<MethodDefinition> createTestMethods(
487484 javaMethods .add (
488485 createRpcTestMethod (
489486 method ,
487+ service ,
490488 matchingService ,
491489 method .methodSignatures ().get (i ),
492490 i ,
@@ -509,9 +507,27 @@ private static List<MethodDefinition> createTestMethods(
509507 return javaMethods ;
510508 }
511509
510+ /**
511+ * Creates a test method for a given RPC, e.g. createAssetTest.
512+ *
513+ * @param method the RPC for which this test method is created.
514+ * @param apiService the host service under test.
515+ * @param rpcService the service that {@code method} belongs to. This is not equal to {@code
516+ * apiService} only when {@code method} is a mixin, in which case {@code rpcService} is the
517+ * mixed-in service. If {@code apiService} and {@code rpcService} are different, they will be
518+ * used only for pagination. Otherwise, {@code rpcService} subsumes {@code apiService}.
519+ * @param methodSignature the method signature of the RPC under test.
520+ * @param variantIndex the nth variant of the RPC under test. This applies when we have
521+ * polymorphism due to the presence of several method signature annotations in the proto.
522+ * @param isRequestArg whether the RPC variant under test take only the request proto message.
523+ * @param classMemberVarExprs the class members in the generated test class.
524+ * @param resourceNames the resource names available for use.
525+ * @param messageTypes the proto message types available for use.
526+ */
512527 private static MethodDefinition createRpcTestMethod (
513528 Method method ,
514- Service service ,
529+ Service apiService ,
530+ Service rpcService ,
515531 List <MethodArgument > methodSignature ,
516532 int variantIndex ,
517533 boolean isRequestArg ,
@@ -520,14 +536,15 @@ private static MethodDefinition createRpcTestMethod(
520536 Map <String , Message > messageTypes ) {
521537 if (!method .stream ().equals (Method .Stream .NONE )) {
522538 return createStreamingRpcTestMethod (
523- service , method , classMemberVarExprs , resourceNames , messageTypes );
539+ rpcService , method , classMemberVarExprs , resourceNames , messageTypes );
524540 }
525541 // Construct the expected response.
526542 TypeNode methodOutputType = method .hasLro () ? method .lro ().responseType () : method .outputType ();
527543 List <Expr > methodExprs = new ArrayList <>();
528544
529545 TypeNode repeatedResponseType = null ;
530546 VariableExpr responsesElementVarExpr = null ;
547+ String mockServiceVarName = getMockServiceVarName (rpcService );
531548 if (method .isPaged ()) {
532549 Message methodOutputMessage = messageTypes .get (method .outputType ().reference ().simpleName ());
533550 Field repeatedPagedResultsField = methodOutputMessage .findAndUnwrapFirstRepeatedField ();
@@ -596,6 +613,7 @@ private static MethodDefinition createRpcTestMethod(
596613 .setVariableExpr (expectedResponseVarExpr .toBuilder ().setIsDecl (true ).build ())
597614 .setValueExpr (expectedResponseValExpr )
598615 .build ());
616+
599617 if (method .hasLro ()) {
600618 VariableExpr resultOperationVarExpr =
601619 VariableExpr .withVariable (
@@ -613,14 +631,14 @@ private static MethodDefinition createRpcTestMethod(
613631 .build ());
614632 methodExprs .add (
615633 MethodInvocationExpr .builder ()
616- .setExprReferenceExpr (classMemberVarExprs .get (getMockServiceVarName ( service ) ))
634+ .setExprReferenceExpr (classMemberVarExprs .get (mockServiceVarName ))
617635 .setMethodName ("addResponse" )
618636 .setArguments (resultOperationVarExpr )
619637 .build ());
620638 } else {
621639 methodExprs .add (
622640 MethodInvocationExpr .builder ()
623- .setExprReferenceExpr (classMemberVarExprs .get (getMockServiceVarName ( service ) ))
641+ .setExprReferenceExpr (classMemberVarExprs .get (mockServiceVarName ))
624642 .setMethodName ("addResponse" )
625643 .setArguments (expectedResponseVarExpr )
626644 .build ());
@@ -675,7 +693,12 @@ private static MethodDefinition createRpcTestMethod(
675693 VariableExpr .withVariable (
676694 Variable .builder ()
677695 .setType (
678- method .isPaged () ? getPagedResponseType (method , service ) : methodOutputType )
696+ !method .isPaged ()
697+ ? methodOutputType
698+ // If this method is a paginated mixin, use the host service, since
699+ // ServiceClient defines the paged response class and the mixed-in service
700+ // does not have a client.
701+ : getPagedResponseType (method , method .isMixin () ? apiService : rpcService ))
679702 .setName (method .isPaged () ? "pagedListResponse" : "actualResponse" )
680703 .build ());
681704 Expr rpcJavaMethodInvocationExpr =
@@ -828,7 +851,7 @@ private static MethodDefinition createRpcTestMethod(
828851 .setVariableExpr (actualRequestsVarExpr .toBuilder ().setIsDecl (true ).build ())
829852 .setValueExpr (
830853 MethodInvocationExpr .builder ()
831- .setExprReferenceExpr (classMemberVarExprs .get (getMockServiceVarName ( service ) ))
854+ .setExprReferenceExpr (classMemberVarExprs .get (mockServiceVarName ))
832855 .setMethodName ("getRequests" )
833856 .setReturnType (actualRequestsVarExpr .type ())
834857 .build ())
@@ -1021,6 +1044,8 @@ private static MethodDefinition createStreamingRpcTestMethod(
10211044 .setVariableExpr (expectedResponseVarExpr .toBuilder ().setIsDecl (true ).build ())
10221045 .setValueExpr (expectedResponseValExpr )
10231046 .build ());
1047+
1048+ String mockServiceVarName = getMockServiceVarName (service );
10241049 if (method .hasLro ()) {
10251050 VariableExpr resultOperationVarExpr =
10261051 VariableExpr .withVariable (
@@ -1038,14 +1063,14 @@ private static MethodDefinition createStreamingRpcTestMethod(
10381063 .build ());
10391064 methodExprs .add (
10401065 MethodInvocationExpr .builder ()
1041- .setExprReferenceExpr (classMemberVarExprs .get (getMockServiceVarName ( service ) ))
1066+ .setExprReferenceExpr (classMemberVarExprs .get (mockServiceVarName ))
10421067 .setMethodName ("addResponse" )
10431068 .setArguments (resultOperationVarExpr )
10441069 .build ());
10451070 } else {
10461071 methodExprs .add (
10471072 MethodInvocationExpr .builder ()
1048- .setExprReferenceExpr (classMemberVarExprs .get (getMockServiceVarName ( service ) ))
1073+ .setExprReferenceExpr (classMemberVarExprs .get (mockServiceVarName ))
10491074 .setMethodName ("addResponse" )
10501075 .setArguments (expectedResponseVarExpr )
10511076 .build ());
@@ -1251,7 +1276,19 @@ private static MethodDefinition createStreamingRpcTestMethod(
12511276 .build ();
12521277 }
12531278
1254- // TODO(imraleung): Reorder params.
1279+ /**
1280+ * Creates a test method to exercise exceptions for a given RPC, e.g. createAssetTest.
1281+ *
1282+ * @param method the RPC for which this test method is created.
1283+ * @param service the service that {@code method} belongs to.
1284+ * @param methodSignature the method signature of the RPC under test.
1285+ * @param variantIndex the nth variant of the RPC under test. This applies when we have
1286+ * polymorphism due to the presence of several method signature annotations in the proto.
1287+ * @param classMemberVarExprs the class members in the generated test class.
1288+ * @param resourceNames the resource names available for use.
1289+ * @param messageTypes the proto message types available for use.
1290+ */
1291+ // TODO(miraleung): Reorder params.
12551292 private static MethodDefinition createRpcExceptionTestMethod (
12561293 Method method ,
12571294 Service service ,
@@ -1759,7 +1796,7 @@ private static Map<String, TypeNode> createDefaultMethodNamesToTypes() {
17591796 return javaMethodNameToReturnType ;
17601797 }
17611798
1762- private static void addDynamicTypes (Service service , TypeStore typeStore ) {
1799+ private static void addDynamicTypes (GapicContext context , Service service , TypeStore typeStore ) {
17631800 typeStore .putAll (
17641801 service .pakkage (),
17651802 Arrays .asList (
@@ -1775,6 +1812,19 @@ private static void addDynamicTypes(Service service, TypeStore typeStore) {
17751812 .collect (Collectors .toList ()),
17761813 true ,
17771814 ClassNames .getServiceClientClassName (service ));
1815+ for (Service mixinService : context .mixinServices ()) {
1816+ typeStore .put (mixinService .pakkage (), ClassNames .getMockServiceClassName (mixinService ));
1817+ for (Method mixinMethod : mixinService .methods ()) {
1818+ if (!mixinMethod .isPaged ()) {
1819+ continue ;
1820+ }
1821+ typeStore .put (
1822+ service .pakkage (),
1823+ String .format (PAGED_RESPONSE_TYPE_NAME_PATTERN , mixinMethod .name ()),
1824+ true ,
1825+ ClassNames .getServiceClientClassName (service ));
1826+ }
1827+ }
17781828 }
17791829
17801830 private static TypeNode getOperationCallSettingsType (Method protoMethod ) {
0 commit comments