From bdbcaba38214e108511c6d07df85893833aaa8da Mon Sep 17 00:00:00 2001 From: DinethH Date: Fri, 19 Apr 2024 09:56:37 +0530 Subject: [PATCH 01/29] added grpc spec structs for ballerina --- .../ballerina/APIClient.bal | 99 ++++++++++++++++++- .../ballerina/ConfigGenreatorClient.bal | 2 +- .../ballerina/constants.bal | 3 +- .../ballerina/modules/model/APIArtifact.bal | 2 + .../ballerina/modules/model/GRPCRoute.bal | 76 ++++++++++++++ 5 files changed, 178 insertions(+), 4 deletions(-) create mode 100644 runtime/config-deployer-service/ballerina/modules/model/GRPCRoute.bal diff --git a/runtime/config-deployer-service/ballerina/APIClient.bal b/runtime/config-deployer-service/ballerina/APIClient.bal index 5fcdd78ee..58b0921d3 100644 --- a/runtime/config-deployer-service/ballerina/APIClient.bal +++ b/runtime/config-deployer-service/ballerina/APIClient.bal @@ -442,6 +442,18 @@ public class APIClient { sandboxRoutes.push(gqlRoute.metadata.name); } } + } else if apkConf.'type == API_TYPE_GRPC{ + foreach model:GRPCRoute grpcRoute in apiArtifact.productionGrpcRoutes { + if grpcRoute.spec.rules.length() > 0 { + productionRoutes.push(grpcRoute.metadata.name); + } + } + foreach model:GRPCRoute grpcRoute in apiArtifact.sandboxGrpcRoutes { + if grpcRoute.spec.rules.length() > 0 { + sandboxRoutes.push(grpcRoute.metadata.name); + } + } + } else { foreach model:HTTPRoute httpRoute in apiArtifact.productionHttpRoutes { if httpRoute.spec.rules.length() > 0 { @@ -532,6 +544,28 @@ public class APIClient { apiArtifact.sandboxGqlRoutes.push(gqlRoute); } } + } else if apkConf.'type == API_TYPE_GRPC { + model:GRPCRoute grpcRoute = { + metadata: + { + name: uniqueId + "-" + endpointType + "-grpcroute-" + count.toString(), + labels: self.getLabels(apkConf, organization) + }, + spec: { + parentRefs: self.generateAndRetrieveParentRefs(apkConf, uniqueId), + rules: check self.generateGRPCRouteRules(apiArtifact, apkConf, endpoint, endpointType, organization), + hostnames: self.getHostNames(apkConf, uniqueId, endpointType, organization) + } + }; + if endpoint is model:Endpoint { + grpcRoute.spec.backendRefs = self.retrieveGeneratedBackend(apkConf, endpoint, endpointType); + } + if grpcRoute.spec.rules.length() > 0 { + if endpointType == PRODUCTION_TYPE { + apiArtifact.productionGrpcRoutes.push(grpcRoute); + } else { + apiArtifact.sandboxGrpcRoutes.push(grpcRoute); + } } else { model:HTTPRoute httpRoute = { metadata: @@ -555,6 +589,7 @@ public class APIClient { } return; + } } private isolated function generateAndRetrieveParentRefs(APKConf apkConf, string uniqueId) returns model:ParentReference[] { @@ -571,7 +606,7 @@ public class APIClient { APKOperations[]? operations = apkConf.operations; if operations is APKOperations[] { foreach APKOperations operation in operations { - model:HTTPRouteRule|model:GQLRouteRule|() routeRule = check self.generateRouteRule(apiArtifact, apkConf, endpoint, operation, endpointType, organization); + model:HTTPRouteRule|model:GQLRouteRule|model:GRPCRouteRule|() routeRule = check self.generateRouteRule(apiArtifact, apkConf, endpoint, operation, endpointType, organization); if routeRule is model:HTTPRouteRule { model:HTTPRouteFilter[]? filters = routeRule.filters; if filters is () { @@ -625,12 +660,72 @@ public class APIClient { return httpRouteRules; } + private isolated function generateGRPCRouteRules(model:APIArtifact apiArtifact, APKConf apkConf, model:Endpoint? endpoint, string endpointType, commons:Organization organization) returns model:GRPCRouteRule[]|commons:APKError|error { + model:GRPCRouteRule[] grpcRouteRules = []; + APKOperations[]? operations = apkConf.operations; + if operations is APKOperations[] { + foreach APKOperations operation in operations { + model:HTTPRouteRule|model:GQLRouteRule|model:GRPCRouteRule|() routeRule = check self.generateRouteRule(apiArtifact, apkConf, endpoint, operation, endpointType, organization); + if routeRule is model:GRPCRouteRule { + model:GRPCRouteFilter[]? filters = routeRule.filters; + if filters is () { + filters = []; + routeRule.filters = filters; + } + string disableAuthenticationRefName = self.retrieveDisableAuthenticationRefName(apkConf, endpointType, organization); + if !(operation.secured ?: true) { + if !apiArtifact.authenticationMap.hasKey(disableAuthenticationRefName) { + model:Authentication generateDisableAuthenticationCR = self.generateDisableAuthenticationCR(apiArtifact, apkConf, endpointType, organization); + apiArtifact.authenticationMap[disableAuthenticationRefName] = generateDisableAuthenticationCR; + } + model:GRPCRouteFilter disableAuthenticationFilter = {'type: "ExtensionRef", extensionRef: {group: "dp.wso2.com", kind: "Authentication", name: disableAuthenticationRefName}}; + (filters).push(disableAuthenticationFilter); + } + string[]? scopes = operation.scopes; + if scopes is string[] { + int count = 1; + foreach string scope in scopes { + model:Scope scopeCr; + if apiArtifact.scopes.hasKey(scope) { + scopeCr = apiArtifact.scopes.get(scope); + } else { + scopeCr = self.generateScopeCR(apiArtifact, apkConf, organization, scope, count); + count = count + 1; + } + model:GRPCRouteFilter scopeFilter = {'type: "ExtensionRef", extensionRef: {group: "dp.wso2.com", kind: scopeCr.kind, name: scopeCr.metadata.name}}; + (filters).push(scopeFilter); + } + } + if operation.rateLimit != () { + model:RateLimitPolicy? rateLimitPolicyCR = self.generateRateLimitPolicyCR(apkConf, operation.rateLimit, apiArtifact.uniqueId, operation, organization); + if rateLimitPolicyCR != () { + apiArtifact.rateLimitPolicies[rateLimitPolicyCR.metadata.name] = rateLimitPolicyCR; + model:GRPCRouteFilter rateLimitPolicyFilter = {'type: "ExtensionRef", extensionRef: {group: "dp.wso2.com", kind: "RateLimitPolicy", name: rateLimitPolicyCR.metadata.name}}; + (filters).push(rateLimitPolicyFilter); + } + } + if operation.operationPolicies != () { + model:APIPolicy? apiPolicyCR = check self.generateAPIPolicyAndBackendCR(apiArtifact, apkConf, operation, operation.operationPolicies, organization, apiArtifact.uniqueId); + if apiPolicyCR != () { + apiArtifact.apiPolicies[apiPolicyCR.metadata.name] = apiPolicyCR; + model:GRPCRouteFilter apiPolicyFilter = {'type: "ExtensionRef", extensionRef: {group: "dp.wso2.com", kind: "APIPolicy", name: apiPolicyCR.metadata.name}}; + (filters).push(apiPolicyFilter); + } + } + grpcRouteRules.push(routeRule); + } + } + } + return grpcRouteRules; + } + + private isolated function generateGQLRouteRules(model:APIArtifact apiArtifact, APKConf apkConf, model:Endpoint? endpoint, string endpointType, commons:Organization organization) returns model:GQLRouteRule[]|commons:APKError|error { model:GQLRouteRule[] gqlRouteRules = []; APKOperations[]? operations = apkConf.operations; if operations is APKOperations[] { foreach APKOperations operation in operations { - model:HTTPRouteRule|model:GQLRouteRule|() routeRule = check self.generateRouteRule(apiArtifact, apkConf, endpoint, operation, endpointType, organization); + model:HTTPRouteRule|model:GQLRouteRule|model:GRPCRouteRule|() routeRule = check self.generateRouteRule(apiArtifact, apkConf, endpoint, operation, endpointType, organization); if routeRule is model:GQLRouteRule { model:GQLRouteFilter[]? filters = routeRule.filters; if filters is () { diff --git a/runtime/config-deployer-service/ballerina/ConfigGenreatorClient.bal b/runtime/config-deployer-service/ballerina/ConfigGenreatorClient.bal index e410cdb51..723c471f3 100644 --- a/runtime/config-deployer-service/ballerina/ConfigGenreatorClient.bal +++ b/runtime/config-deployer-service/ballerina/ConfigGenreatorClient.bal @@ -93,7 +93,7 @@ public class ConfigGeneratorClient { private isolated function validateAndRetrieveDefinition(string 'type, string? url, byte[]? content, string? fileName) returns runtimeapi:APIDefinitionValidationResponse|runtimeapi:APIManagementException|error|commons:APKError { runtimeapi:APIDefinitionValidationResponse|runtimeapi:APIManagementException|error validationResponse; boolean typeAvailable = 'type.length() > 0; - string[] ALLOWED_API_DEFINITION_TYPES = [API_TYPE_REST, API_TYPE_GRAPHQL, "ASYNC"]; + string[] ALLOWED_API_DEFINITION_TYPES = [API_TYPE_REST, API_TYPE_GRAPHQL, "ASYNC",API_TYPE_GRPC]; if !typeAvailable { return e909005("type"); } diff --git a/runtime/config-deployer-service/ballerina/constants.bal b/runtime/config-deployer-service/ballerina/constants.bal index 66f523456..ed2ad0f6f 100644 --- a/runtime/config-deployer-service/ballerina/constants.bal +++ b/runtime/config-deployer-service/ballerina/constants.bal @@ -11,6 +11,7 @@ public final string[] WS_SUPPORTED_METHODS = ["subscribe", "publish"]; const string API_TYPE_REST = "REST"; const string API_TYPE_GRAPHQL = "GRAPHQL"; +const string API_TYPE_GRPC = "GRPC"; const string API_TYPE_SOAP = "SOAP"; const string API_TYPE_SSE = "SSE"; const string API_TYPE_WS = "WS"; @@ -52,7 +53,7 @@ const string ENDPOINT_SECURITY_PASSWORD = "password"; const string ZIP_FILE_EXTENSTION = ".zip"; const string PROTOCOL_HTTP = "http"; const string PROTOCOL_HTTPS = "https"; -final string[] & readonly ALLOWED_API_TYPES = [API_TYPE_REST, API_TYPE_GRAPHQL]; +final string[] & readonly ALLOWED_API_TYPES = [API_TYPE_REST, API_TYPE_GRAPHQL, API_TYPE_GRPC]; const string MEDIATION_POLICY_TYPE_REQUEST_HEADER_MODIFIER = "RequestHeaderModifier"; const string MEDIATION_POLICY_TYPE_RESPONSE_HEADER_MODIFIER = "ResponseHeaderModifier"; diff --git a/runtime/config-deployer-service/ballerina/modules/model/APIArtifact.bal b/runtime/config-deployer-service/ballerina/modules/model/APIArtifact.bal index 7f272176a..b40f95448 100644 --- a/runtime/config-deployer-service/ballerina/modules/model/APIArtifact.bal +++ b/runtime/config-deployer-service/ballerina/modules/model/APIArtifact.bal @@ -6,6 +6,8 @@ public type APIArtifact record {| HTTPRoute[] sandboxHttpRoutes = []; GQLRoute[] productionGqlRoutes = []; GQLRoute[] sandboxGqlRoutes = []; + GRPCRoute[] productionGrpcRoutes = []; + GRPCRoute[] sandboxGrpcRoutes = []; ConfigMap definition?; map endpointCertificates = {}; map certificateMap = {}; diff --git a/runtime/config-deployer-service/ballerina/modules/model/GRPCRoute.bal b/runtime/config-deployer-service/ballerina/modules/model/GRPCRoute.bal new file mode 100644 index 000000000..5f77e6434 --- /dev/null +++ b/runtime/config-deployer-service/ballerina/modules/model/GRPCRoute.bal @@ -0,0 +1,76 @@ +// +// Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com). +// +// WSO2 LLC. licenses this file to you under the Apache License, +// Version 2.0 (the "License"); you may not use this file except +// in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +// + +public type GRPCRouteSpec record { + *CommonRouteSpec; + string[] hostnames?; + GRPCRouteRule[] rules = []; + BackendRef[] backendRefs?; +}; + +public type GRPCRouteRule record { + GRPCRouteMatch[] matches?; + GRPCRouteFilter[] filters?; + GRPCBackendRef[] backendRefs?; +}; +public type GRPCRouteMatch record { + GRPCMethodMatch path?; + GRPCHeaderMatch[] headers?; +}; + +public type GRPCHeaderMatch record { + string 'type; + string name; + string value; +}; + +public type GRPCMethodMatch record { + string 'type; + string 'service; + string method; +}; + +public type GRPCRouteFilter record { + string 'type; + HTTPHeaderFilter requestHeaderModifier?; + HTTPHeaderFilter responseHeaderModifier?; + HTTPRequestMirrorFilter requestMirror?; + LocalObjectReference extensionRef?; +}; + +public type GRPCBackendRef record { + *BackendRef; + GRPCRouteFilter[] filters?; +}; + +public type GRPCRoute record {| + string apiVersion = "gateway.networking.k8s.io/v1alpha2"; + string kind = "GRPCRoute"; + Metadata metadata; + GRPCRouteSpec spec; +|}; + + +public type GRPCRouteList record {| + string apiVersion = "gateway.networking.k8s.io/v1alpha2"; + string kind = "GRPCRouteList"; + ListMeta metadata; + GRPCRoute[] items; +|}; + + From aab6843996492df3e8a3eb235b00d4cfee276634 Mon Sep 17 00:00:00 2001 From: DinethH Date: Fri, 19 Apr 2024 23:31:37 +0530 Subject: [PATCH 02/29] conf gets generated from proto file --- .../org/wso2/apk/config/APIConstants.java | 3 +- .../apk/config/DefinitionParserFactory.java | 2 + .../wso2/apk/config/RuntimeAPICommonUtil.java | 33 ++++ .../wso2/apk/config/api/ExceptionCodes.java | 4 + .../apk/config/definitions/OASParserUtil.java | 28 +++ .../apk/config/definitions/ProtoParser.java | 165 ++++++++++++++++++ 6 files changed, 234 insertions(+), 1 deletion(-) create mode 100644 runtime/config-deployer-service/java/src/main/java/org/wso2/apk/config/definitions/ProtoParser.java diff --git a/runtime/config-deployer-service/java/src/main/java/org/wso2/apk/config/APIConstants.java b/runtime/config-deployer-service/java/src/main/java/org/wso2/apk/config/APIConstants.java index ed4a91e2a..1fcaaadb0 100644 --- a/runtime/config-deployer-service/java/src/main/java/org/wso2/apk/config/APIConstants.java +++ b/runtime/config-deployer-service/java/src/main/java/org/wso2/apk/config/APIConstants.java @@ -49,6 +49,7 @@ public final class APIConstants { public static final String OBJECT = "object"; public static final String GRAPHQL_API = "GRAPHQL"; + public static final String GRPC_API = "GRPC"; public static final String APPLICATION_JSON_MEDIA_TYPE = "application/json"; // registry location for OpenAPI files @@ -90,7 +91,7 @@ public final class APIConstants { public static final String GRAPHQL_QUERY = "QUERY"; public enum ParserType { - REST, ASYNC, GRAPHQL + REST, ASYNC, GRAPHQL, GRPC } public static class OperationParameter { diff --git a/runtime/config-deployer-service/java/src/main/java/org/wso2/apk/config/DefinitionParserFactory.java b/runtime/config-deployer-service/java/src/main/java/org/wso2/apk/config/DefinitionParserFactory.java index 8cfb71084..167b2d138 100644 --- a/runtime/config-deployer-service/java/src/main/java/org/wso2/apk/config/DefinitionParserFactory.java +++ b/runtime/config-deployer-service/java/src/main/java/org/wso2/apk/config/DefinitionParserFactory.java @@ -25,6 +25,8 @@ public static APIDefinition getParser(API api) { return new OAS3Parser(); } else if (APIConstants.ParserType.ASYNC.name().equals(api.getType())) { return new AsyncApiParser(); + } else if (APIConstants.ParserType.GRPC.name().equals(api.getType())) { + return new OAS3Parser(); } return null; } diff --git a/runtime/config-deployer-service/java/src/main/java/org/wso2/apk/config/RuntimeAPICommonUtil.java b/runtime/config-deployer-service/java/src/main/java/org/wso2/apk/config/RuntimeAPICommonUtil.java index 03f043359..4477692c1 100644 --- a/runtime/config-deployer-service/java/src/main/java/org/wso2/apk/config/RuntimeAPICommonUtil.java +++ b/runtime/config-deployer-service/java/src/main/java/org/wso2/apk/config/RuntimeAPICommonUtil.java @@ -6,6 +6,7 @@ import org.wso2.apk.config.api.ExceptionCodes; import org.wso2.apk.config.definitions.GraphQLSchemaDefinition; import org.wso2.apk.config.definitions.OASParserUtil; +import org.wso2.apk.config.definitions.ProtoParser; import org.wso2.apk.config.model.API; import org.wso2.apk.config.model.URITemplate; @@ -66,7 +67,18 @@ public static APIDefinitionValidationResponse validateOpenAPIDefinition(String t OASParserUtil.addErrorToValidationResponse(validationResponse, "Invalid definition file type provided."); } + } else if (APIConstants.ParserType.GRPC.name().equals(type.toUpperCase())) { + if (fileName.endsWith(".proto")) { + validationResponse = OASParserUtil.validateProtoDefinition( + new String(inputByteArray, StandardCharsets.UTF_8), + returnContent); + } else { + OASParserUtil.addErrorToValidationResponse(validationResponse, + "Invalid definition file type provided."); + } } + + return validationResponse; } @@ -99,6 +111,8 @@ public static API getAPIFromDefinition(String definition, String apiType) throws if (apiType.toUpperCase().equals(APIConstants.GRAPHQL_API)) { return getGQLAPIFromDefinition(definition); + } else if (apiType.toUpperCase().equals(APIConstants.GRPC_API)) { + return getGRPCAPIFromProtoDefinition(definition); } else { APIDefinition parser = DefinitionParserFactory.getParser(apiType); if (parser != null) { @@ -128,6 +142,25 @@ private static API getGQLAPIFromDefinition(String definition) { return api; } + private static API getGRPCAPIFromProtoDefinition(String definition) { + ProtoParser protoParser = new ProtoParser(definition); + List uriTemplates = new ArrayList<>(); + API api = new API(); + api.setBasePath(protoParser.protoFile.basePath); + api.setVersion(protoParser.protoFile.version); + for (ProtoParser.Service service : protoParser.getServices()) { + for (String method : service.methods) { + URITemplate uriTemplate = new URITemplate(); + uriTemplate.setUriTemplate("/" + protoParser.protoFile.packageName + "." + service.name); + uriTemplate.setVerb(method); + uriTemplates.add(uriTemplate); + } + } + api.setUriTemplates(uriTemplates.toArray(new URITemplate[uriTemplates.size()])); + + return api; + } + private RuntimeAPICommonUtil() { } diff --git a/runtime/config-deployer-service/java/src/main/java/org/wso2/apk/config/api/ExceptionCodes.java b/runtime/config-deployer-service/java/src/main/java/org/wso2/apk/config/api/ExceptionCodes.java index f24a3a2fe..6324d8700 100644 --- a/runtime/config-deployer-service/java/src/main/java/org/wso2/apk/config/api/ExceptionCodes.java +++ b/runtime/config-deployer-service/java/src/main/java/org/wso2/apk/config/api/ExceptionCodes.java @@ -361,6 +361,10 @@ public enum ExceptionCodes implements ErrorHandler { UNSUPPORTED_GRAPHQL_FILE_EXTENSION(900802, "Unsupported GraphQL Schema File Extension", 400, "Unsupported extension. Only supported extensions are .graphql, .txt and .sdl"), + //GRPC API related codes + GRPC_PROTO_DEFINTION_CANNOT_BE_NULL(900803, "gRPC Proto Definition cannot be empty or null", 400, + "gRPC Proto Definition cannot be empty or null"), + // Oauth related codes AUTH_GENERAL_ERROR(900900, "Authorization Error", 403, " Error in authorization"), INVALID_CREDENTIALS(900901, "Invalid Credentials", 401, " Invalid username or password"), diff --git a/runtime/config-deployer-service/java/src/main/java/org/wso2/apk/config/definitions/OASParserUtil.java b/runtime/config-deployer-service/java/src/main/java/org/wso2/apk/config/definitions/OASParserUtil.java index 66eae511c..4ec23b7df 100644 --- a/runtime/config-deployer-service/java/src/main/java/org/wso2/apk/config/definitions/OASParserUtil.java +++ b/runtime/config-deployer-service/java/src/main/java/org/wso2/apk/config/definitions/OASParserUtil.java @@ -668,6 +668,34 @@ public static APIDefinitionValidationResponse validateGraphQLSchema(String apiDe return validationResponse; } + /** + * Validate gRPC proto definition + * + * @return Validation response + */ + public static APIDefinitionValidationResponse validateProtoDefinition(String apiDefinition, + boolean returnContent) { + APIDefinitionValidationResponse validationResponse = new APIDefinitionValidationResponse(); + ArrayList errors = new ArrayList<>(); + try { + if (apiDefinition.isBlank()) { + validationResponse.setValid(false); + errors.add(ExceptionCodes.GRPC_PROTO_DEFINTION_CANNOT_BE_NULL); + validationResponse.setErrorItems(errors); + } else { + validationResponse.setValid(true); + validationResponse.setContent(apiDefinition); + } + } catch (Exception e) { + OASParserUtil.addErrorToValidationResponse(validationResponse, e.getMessage()); + validationResponse.setValid(false); + errors.add(new ErrorItem("API Definition Validation Error", "API Definition is invalid", 400, 400)); + validationResponse.setErrorItems(errors); + } + return validationResponse; + + } + /** * This method removes the unsupported json blocks from the given json string. * diff --git a/runtime/config-deployer-service/java/src/main/java/org/wso2/apk/config/definitions/ProtoParser.java b/runtime/config-deployer-service/java/src/main/java/org/wso2/apk/config/definitions/ProtoParser.java new file mode 100644 index 000000000..2f371f420 --- /dev/null +++ b/runtime/config-deployer-service/java/src/main/java/org/wso2/apk/config/definitions/ProtoParser.java @@ -0,0 +1,165 @@ +package org.wso2.apk.config.definitions; + +import java.util.List; +import java.util.ArrayList; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class ProtoParser { + + public ProtoFile protoFile; + public ProtoParser(String content) { + protoFile = parseProtoContent(content); + } + + public String getPackageString(String content) { + Pattern packagePattern = Pattern.compile("package\\s+([\\w\\.]+);"); + Matcher packageMatcher = packagePattern.matcher(content); + if (packageMatcher.find()) { + return packageMatcher.group(1); + } + return null; + } + public String getVersion(String packageString) { + Pattern versionPattern = Pattern.compile("v\\d+(\\.\\d+)*"); + Matcher versionMatcher = versionPattern.matcher(packageString); + if (versionMatcher.find()) { + return versionMatcher.group(0); + } + System.out.println("No version found"); + return null; + } + public String getPackageName(String packageString){ + Pattern namePattern = Pattern.compile("v\\d+(\\.\\d+)*\\.(\\w+)"); + Matcher nameMatcher = namePattern.matcher(packageString); + if (nameMatcher.find()) { + return nameMatcher.group(2); + } + System.out.println("No name found"); + return null; + } + + public String getBasePath(String packageString){ + Pattern basePathPattern = Pattern.compile("^(.*?)v\\d"); + + Matcher basePathMatcher = basePathPattern.matcher(packageString); + if (basePathMatcher.find()) { + String basePath = basePathMatcher.group(1); + if(basePath.charAt(basePath.length()-1) == '.'){ + basePath = basePath.substring(0, basePath.length()-1); + } + return basePath; + } + System.out.println("No base path found"); + return null; + } + + // Method to extract service blocks from a given text + public List extractServiceBlocks(String text) { + // Regular expression pattern to match the service blocks + String patternString = "service\\s+\\w+\\s*\\{[^{}]*(?:\\{[^{}]*\\}[^{}]*)*\\}"; + + // Compile the regular expression + Pattern pattern = Pattern.compile(patternString, Pattern.DOTALL); + Matcher matcher = pattern.matcher(text); + + // Find all matches and append them to the result + List result = new ArrayList<>(); + while (matcher.find()) { + result.add(matcher.group()); + } + return result; + } + + public List extractMethodNames(String serviceBlock) { + // Regular expression pattern to match the method names + String patternString = "(?<=rpc\\s)\\w+"; + + // Compile the regular expression + Pattern pattern = Pattern.compile(patternString); + Matcher matcher = pattern.matcher(serviceBlock); + + // Find all matches and append them to the result + List result = new ArrayList<>(); + while (matcher.find()) { + result.add(matcher.group()); + } + return result; + } + + public String getServiceName(String serviceBlock) { + // Regular expression pattern to match the service name + String patternString = "(?<=service\\s)\\w+"; + + // Compile the regular expression + Pattern pattern = Pattern.compile(patternString); + Matcher matcher = pattern.matcher(serviceBlock); + + // Find the first match and return it + if (matcher.find()) { + return matcher.group(); + } + return null; + } + + public ProtoFile parseProtoContent(String content) { + ProtoFile protoFile = new ProtoFile(); + protoFile.services = new ArrayList<>(); + + List serviceBlocks = extractServiceBlocks(content); + for (String serviceBlock : serviceBlocks) { + Service service = new Service(); + service.name = getServiceName(serviceBlock); + service.methods = new ArrayList<>(); + service.methods.addAll(extractMethodNames(serviceBlock)); + protoFile.services.add(service); + } + + // Extract package name + String packageName = getPackageString(content); + protoFile.packageName = getPackageName(packageName); + protoFile.version = getVersion(packageName); + protoFile.basePath = getBasePath(packageName); + +// System.out.println(protoFile); + + return protoFile; + } + + public List getMethods(Service Service){ + return Service.methods; + } + public List getServices(){ + return this.protoFile.services; + } + public class ProtoFile { + public String packageName; + public String basePath; + public String version; + public List services; + + @Override + public String toString() { + return "ProtoFile{" + + "packageName='" + packageName + '\'' + + ", basePath='" + basePath + '\'' + + ", version='" + version + '\'' + + ", services=" + services + + '}'; + } + + } + + public class Service { + public String name; + public List methods; + + @Override + public String toString() { + return " Service{" + + "name='" + name + '\'' + + ", methods=" + methods + + '}'; + } + } +} \ No newline at end of file From 1c93b91d9a824b7a95734b653c250039461ae17d Mon Sep 17 00:00:00 2001 From: DinethH Date: Mon, 22 Apr 2024 12:20:16 +0530 Subject: [PATCH 03/29] CRs get generated --- .../ballerina/APIClient.bal | 47 +++++++++++++++++-- .../ballerina/ConfigGenreatorClient.bal | 8 ++++ .../ballerina/DeployerClient.bal | 16 +++++++ .../ballerina/modules/model/GRPCRoute.bal | 2 +- .../java/org/wso2/apk/config/model/API.java | 3 ++ 5 files changed, 72 insertions(+), 4 deletions(-) diff --git a/runtime/config-deployer-service/ballerina/APIClient.bal b/runtime/config-deployer-service/ballerina/APIClient.bal index 58b0921d3..85d2b4cf5 100644 --- a/runtime/config-deployer-service/ballerina/APIClient.bal +++ b/runtime/config-deployer-service/ballerina/APIClient.bal @@ -280,6 +280,14 @@ public class APIClient { return fullBasePath; } + isolated function returnFullGRPCBasePath(string basePath, string 'version) returns string { + string fullBasePath = basePath; + if (!string:endsWith(basePath, 'version)) { + fullBasePath = string:'join(".", basePath, 'version); + } + return fullBasePath; + } + private isolated function constructURlFromK8sService(K8sService 'k8sService) returns string { return k8sService.protocol + "://" + string:'join(".", k8sService.name, k8sService.namespace, "svc.cluster.local") + ":" + k8sService.port.toString(); } @@ -443,6 +451,7 @@ public class APIClient { } } } else if apkConf.'type == API_TYPE_GRPC{ + k8sAPI.spec.basePath = self.returnFullGRPCBasePath(apkConf.basePath, apkConf.'version); foreach model:GRPCRoute grpcRoute in apiArtifact.productionGrpcRoutes { if grpcRoute.spec.rules.length() > 0 { productionRoutes.push(grpcRoute.metadata.name); @@ -849,7 +858,7 @@ public class APIClient { return authentication; } - private isolated function generateRouteRule(model:APIArtifact apiArtifact, APKConf apkConf, model:Endpoint? endpoint, APKOperations operation, string endpointType, commons:Organization organization) returns model:HTTPRouteRule|model:GQLRouteRule|()|commons:APKError { + private isolated function generateRouteRule(model:APIArtifact apiArtifact, APKConf apkConf, model:Endpoint? endpoint, APKOperations operation, string endpointType, commons:Organization organization) returns model:HTTPRouteRule|model:GQLRouteRule|model:GRPCRouteRule|()|commons:APKError { do { EndpointConfigurations? endpointConfig = operation.endpointConfigurations; model:Endpoint? endpointToUse = (); @@ -873,7 +882,16 @@ public class APIClient { } else { return e909022("Provided Type currently not supported for GraphQL APIs.", error("Provided Type currently not supported for GraphQL APIs.")); } - } else { + } else if apkConf.'type == API_TYPE_GRPC { + model:GRPCRouteMatch[]|error routeMatches = self.retrieveGRPCMatches(apkConf, operation, organization); + if routeMatches is model:GRPCRouteMatch[] && routeMatches.length() > 0 { + model:GRPCRouteRule grpcRouteRule = {matches: routeMatches, backendRefs: self.retrieveGeneratedBackend(apkConf, endpointToUse, endpointType)}; + return grpcRouteRule; + } else { + return e909022("Provided Type currently not supported for GRPC APIs.", error("Provided Type currently not supported for GRPC APIs.")); + } + } + else { model:HTTPRouteRule httpRouteRule = {matches: self.retrieveHTTPMatches(apkConf, operation, organization), backendRefs: self.retrieveGeneratedBackend(apkConf, endpointToUse, endpointType), filters: self.generateFilters(apiArtifact, apkConf, endpointToUse, operation, endpointType, organization)}; return httpRouteRule; } @@ -1018,6 +1036,13 @@ public class APIClient { return gqlRouteMatch; } + + private isolated function retrieveGRPCMatches(APKConf apkConf, APKOperations apiOperation, commons:Organization organization) returns model:GRPCRouteMatch[] { + model:GRPCRouteMatch[] grpcRouteMatch = []; + model:GRPCRouteMatch grpcRoute = self.retrieveGRPCRouteMatch(apiOperation); + grpcRouteMatch.push(grpcRoute); + return grpcRouteMatch; + } private isolated function retrieveHttpRouteMatch(APKConf apkConf, APKOperations apiOperation, commons:Organization organization) returns model:HTTPRouteMatch { return {method: apiOperation.verb, path: {'type: "RegularExpression", value: self.retrievePathPrefix(apkConf.basePath, apkConf.'version, apiOperation.target ?: "/*", organization)}}; @@ -1032,6 +1057,17 @@ public class APIClient { } } + private isolated function retrieveGRPCRouteMatch(APKOperations apiOperation) returns model:GRPCRouteMatch { + model:GRPCRouteMatch grpcRouteMatch = { + method: { + 'type: "RegularExpression", + 'service: apiOperation.target, + method: apiOperation.verb + } + }; + return grpcRouteMatch; + } + isolated function retrieveGeneratedSwaggerDefinition(APKConf apkConf, string? definition) returns string|json|commons:APKError|error { runtimeModels:API api1 = runtimeModels:newAPI1(); api1.setName(apkConf.name); @@ -1068,6 +1104,11 @@ public class APIClient { api1.setGraphQLSchema(definition); return definition; } + if apkConf.'type == API_TYPE_GRPC && definition is string { + // TODO (Dineth) fix this + // api1.setProtoDefinition(definition); + return definition; + } if definition is string && definition.toString().trim().length() > 0 { retrievedDefinition = runtimeUtil:RuntimeAPICommonUtil_generateDefinition2(api1, definition); } else { @@ -1739,7 +1780,7 @@ public class APIClient { } else if definitionFile.fileName.endsWith(".json") { apiDefinition = definitionFileContent; } - } else if apiType == API_TYPE_GRAPHQL { + } else if apiType == API_TYPE_GRAPHQL || apiType == API_TYPE_GRPC { apiDefinition = definitionFileContent; } if apkConf is () { diff --git a/runtime/config-deployer-service/ballerina/ConfigGenreatorClient.bal b/runtime/config-deployer-service/ballerina/ConfigGenreatorClient.bal index 723c471f3..5ca4070f1 100644 --- a/runtime/config-deployer-service/ballerina/ConfigGenreatorClient.bal +++ b/runtime/config-deployer-service/ballerina/ConfigGenreatorClient.bal @@ -187,6 +187,14 @@ public class ConfigGeneratorClient { string yamlString = check self.convertJsonToYaml(gqlRoute.toJsonString()); _ = check self.storeFile(yamlString, gqlRoute.metadata.name, zipDir); } + foreach model:GRPCRoute grpcRoute in apiArtifact.productionGrpcRoutes { + string yamlString = check self.convertJsonToYaml(grpcRoute.toJsonString()); + _ = check self.storeFile(yamlString, grpcRoute.metadata.name, zipDir); + } + foreach model:GRPCRoute grpcRoute in apiArtifact.sandboxGrpcRoutes { + string yamlString = check self.convertJsonToYaml(grpcRoute.toJsonString()); + _ = check self.storeFile(yamlString, grpcRoute.metadata.name, zipDir); + } foreach model:Backend backend in apiArtifact.backendServices { string yamlString = check self.convertJsonToYaml(backend.toJsonString()); _ = check self.storeFile(yamlString, backend.metadata.name, zipDir); diff --git a/runtime/config-deployer-service/ballerina/DeployerClient.bal b/runtime/config-deployer-service/ballerina/DeployerClient.bal index e5beb5d2d..30dfdd9c5 100644 --- a/runtime/config-deployer-service/ballerina/DeployerClient.bal +++ b/runtime/config-deployer-service/ballerina/DeployerClient.bal @@ -442,6 +442,22 @@ public class DeployerClient { return e909022("Error occured while sorting gqlRoutes", e); } } + public isolated function createGrpcRoutesOrder(model:GRPCRoute[] grpcRoutes) returns model:GRPCRoute[]|commons:APKError { + do { + foreach model:GRPCRoute route in grpcRoutes { + model:GRPCRouteRule[] routeRules = route.spec.rules; + // TODO (Dineth) order the routes + // model:GRPCRouteRule[] sortedRouteRules = from var routeRule in routeRules + // order by ((routeRule.matches)[0]).path descending + // select routeRule; + route.spec.rules = routeRules; + } + return grpcRoutes; + } on fail var e { + log:printError("Error occured while sorting grpcRoutes", e); + return e909022("Error occured while sorting grpcRoutes", e); + } + } private isolated function deployAuthenticationCRs(model:APIArtifact apiArtifact, model:OwnerReference ownerReference) returns error? { string[] keys = apiArtifact.authenticationMap.keys(); diff --git a/runtime/config-deployer-service/ballerina/modules/model/GRPCRoute.bal b/runtime/config-deployer-service/ballerina/modules/model/GRPCRoute.bal index 5f77e6434..ff519281d 100644 --- a/runtime/config-deployer-service/ballerina/modules/model/GRPCRoute.bal +++ b/runtime/config-deployer-service/ballerina/modules/model/GRPCRoute.bal @@ -29,7 +29,7 @@ public type GRPCRouteRule record { GRPCBackendRef[] backendRefs?; }; public type GRPCRouteMatch record { - GRPCMethodMatch path?; + GRPCMethodMatch method?; GRPCHeaderMatch[] headers?; }; diff --git a/runtime/config-deployer-service/java/src/main/java/org/wso2/apk/config/model/API.java b/runtime/config-deployer-service/java/src/main/java/org/wso2/apk/config/model/API.java index d2715d77b..9ec779906 100644 --- a/runtime/config-deployer-service/java/src/main/java/org/wso2/apk/config/model/API.java +++ b/runtime/config-deployer-service/java/src/main/java/org/wso2/apk/config/model/API.java @@ -79,6 +79,9 @@ public void setGraphQLSchema(String graphQLSchema) { this.graphQLSchema = graphQLSchema; } + public void setProtoDefinition(String protoDefinition) { + this.swaggerDefinition = protoDefinition; + } public String getSwaggerDefinition() { return swaggerDefinition; } From a6169225424f8f7c2be3615a22260ab73fc21c92 Mon Sep 17 00:00:00 2001 From: DinethH Date: Mon, 22 Apr 2024 15:08:07 +0530 Subject: [PATCH 04/29] gRPC CRs get generated --- runtime/config-deployer-service/ballerina/APIClient.bal | 3 --- .../ballerina/modules/model/GRPCRoute.bal | 1 - 2 files changed, 4 deletions(-) diff --git a/runtime/config-deployer-service/ballerina/APIClient.bal b/runtime/config-deployer-service/ballerina/APIClient.bal index 85d2b4cf5..ea269744a 100644 --- a/runtime/config-deployer-service/ballerina/APIClient.bal +++ b/runtime/config-deployer-service/ballerina/APIClient.bal @@ -566,9 +566,6 @@ public class APIClient { hostnames: self.getHostNames(apkConf, uniqueId, endpointType, organization) } }; - if endpoint is model:Endpoint { - grpcRoute.spec.backendRefs = self.retrieveGeneratedBackend(apkConf, endpoint, endpointType); - } if grpcRoute.spec.rules.length() > 0 { if endpointType == PRODUCTION_TYPE { apiArtifact.productionGrpcRoutes.push(grpcRoute); diff --git a/runtime/config-deployer-service/ballerina/modules/model/GRPCRoute.bal b/runtime/config-deployer-service/ballerina/modules/model/GRPCRoute.bal index ff519281d..887fb3119 100644 --- a/runtime/config-deployer-service/ballerina/modules/model/GRPCRoute.bal +++ b/runtime/config-deployer-service/ballerina/modules/model/GRPCRoute.bal @@ -20,7 +20,6 @@ public type GRPCRouteSpec record { *CommonRouteSpec; string[] hostnames?; GRPCRouteRule[] rules = []; - BackendRef[] backendRefs?; }; public type GRPCRouteRule record { From 75a2b9f021eefc30406b988ab0d86c979b235bc0 Mon Sep 17 00:00:00 2001 From: DinethH Date: Mon, 22 Apr 2024 15:29:09 +0530 Subject: [PATCH 05/29] fixed basepath and service path issue --- .../main/java/org/wso2/apk/config/RuntimeAPICommonUtil.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/runtime/config-deployer-service/java/src/main/java/org/wso2/apk/config/RuntimeAPICommonUtil.java b/runtime/config-deployer-service/java/src/main/java/org/wso2/apk/config/RuntimeAPICommonUtil.java index 4477692c1..ebca7e8e4 100644 --- a/runtime/config-deployer-service/java/src/main/java/org/wso2/apk/config/RuntimeAPICommonUtil.java +++ b/runtime/config-deployer-service/java/src/main/java/org/wso2/apk/config/RuntimeAPICommonUtil.java @@ -146,12 +146,12 @@ private static API getGRPCAPIFromProtoDefinition(String definition) { ProtoParser protoParser = new ProtoParser(definition); List uriTemplates = new ArrayList<>(); API api = new API(); - api.setBasePath(protoParser.protoFile.basePath); + api.setBasePath("/"+protoParser.protoFile.basePath); api.setVersion(protoParser.protoFile.version); for (ProtoParser.Service service : protoParser.getServices()) { for (String method : service.methods) { URITemplate uriTemplate = new URITemplate(); - uriTemplate.setUriTemplate("/" + protoParser.protoFile.packageName + "." + service.name); + uriTemplate.setUriTemplate(protoParser.protoFile.packageName + "." + service.name); uriTemplate.setVerb(method); uriTemplates.add(uriTemplate); } From 250f3ffc8c41acc99016b601d5df8a5d0da1a163 Mon Sep 17 00:00:00 2001 From: DinethH Date: Mon, 22 Apr 2024 17:22:02 +0530 Subject: [PATCH 06/29] fixed TODOs --- .../ballerina/APIClient.bal | 3 +-- .../ballerina/DeployerClient.bal | 9 ++++----- .../ballerina/modules/model/GRPCRoute.bal | 4 ++-- .../modules/org.wso2.apk.config.model/API.bal | 20 +++++++++++++++++++ .../java/org/wso2/apk/config/model/API.java | 7 ++++++- 5 files changed, 33 insertions(+), 10 deletions(-) diff --git a/runtime/config-deployer-service/ballerina/APIClient.bal b/runtime/config-deployer-service/ballerina/APIClient.bal index ea269744a..02e78c1e6 100644 --- a/runtime/config-deployer-service/ballerina/APIClient.bal +++ b/runtime/config-deployer-service/ballerina/APIClient.bal @@ -1102,8 +1102,7 @@ public class APIClient { return definition; } if apkConf.'type == API_TYPE_GRPC && definition is string { - // TODO (Dineth) fix this - // api1.setProtoDefinition(definition); + api1.setProtoDefinition(definition); return definition; } if definition is string && definition.toString().trim().length() > 0 { diff --git a/runtime/config-deployer-service/ballerina/DeployerClient.bal b/runtime/config-deployer-service/ballerina/DeployerClient.bal index 30dfdd9c5..e2c770dd7 100644 --- a/runtime/config-deployer-service/ballerina/DeployerClient.bal +++ b/runtime/config-deployer-service/ballerina/DeployerClient.bal @@ -446,11 +446,10 @@ public class DeployerClient { do { foreach model:GRPCRoute route in grpcRoutes { model:GRPCRouteRule[] routeRules = route.spec.rules; - // TODO (Dineth) order the routes - // model:GRPCRouteRule[] sortedRouteRules = from var routeRule in routeRules - // order by ((routeRule.matches)[0]).path descending - // select routeRule; - route.spec.rules = routeRules; + model:GRPCRouteRule[] sortedRouteRules = from var routeRule in routeRules + order by (routeRule.matches[0].method.'service) descending + select routeRule; + route.spec.rules = sortedRouteRules; } return grpcRoutes; } on fail var e { diff --git a/runtime/config-deployer-service/ballerina/modules/model/GRPCRoute.bal b/runtime/config-deployer-service/ballerina/modules/model/GRPCRoute.bal index 887fb3119..4087ea485 100644 --- a/runtime/config-deployer-service/ballerina/modules/model/GRPCRoute.bal +++ b/runtime/config-deployer-service/ballerina/modules/model/GRPCRoute.bal @@ -23,12 +23,12 @@ public type GRPCRouteSpec record { }; public type GRPCRouteRule record { - GRPCRouteMatch[] matches?; + GRPCRouteMatch[] matches; GRPCRouteFilter[] filters?; GRPCBackendRef[] backendRefs?; }; public type GRPCRouteMatch record { - GRPCMethodMatch method?; + GRPCMethodMatch method; GRPCHeaderMatch[] headers?; }; diff --git a/runtime/config-deployer-service/ballerina/modules/org.wso2.apk.config.model/API.bal b/runtime/config-deployer-service/ballerina/modules/org.wso2.apk.config.model/API.bal index 8638e05be..42470efc5 100644 --- a/runtime/config-deployer-service/ballerina/modules/org.wso2.apk.config.model/API.bal +++ b/runtime/config-deployer-service/ballerina/modules/org.wso2.apk.config.model/API.bal @@ -183,6 +183,13 @@ public distinct class API { org_wso2_apk_config_model_API_setGraphQLSchema(self.jObj, java:fromString(arg0)); } + # The function that maps to the `setProtoDefinition` method of `org.wso2.apk.config.model.API`. + # + // # + arg0 - The `string` value required to map with the Java method parameter. + public isolated function setProtoDefinition(string arg0) { + org_wso2_apk_config_model_API_setProtoDefinition(self.jObj, java:fromString(arg0)); + } + # The function that maps to the `setName` method of `org.wso2.apk.config.model.API`. # # + arg0 - The `string` value required to map with the Java method parameter. @@ -328,6 +335,13 @@ isolated function org_wso2_apk_config_model_API_getGraphQLSchema(handle receiver paramTypes: [] } external; + +isolated function org_wso2_apk_config_model_API_getProtoDefinition(handle receiver) returns handle = @java:Method { + name: "getProtoDefinition", + 'class: "org.wso2.apk.config.model.API", + paramTypes: [] +} external; + isolated function org_wso2_apk_config_model_API_getName(handle receiver) returns handle = @java:Method { name: "getName", 'class: "org.wso2.apk.config.model.API", @@ -412,6 +426,12 @@ isolated function org_wso2_apk_config_model_API_setGraphQLSchema(handle receiver paramTypes: ["java.lang.String"] } external; +isolated function org_wso2_apk_config_model_API_setProtoDefinition(handle receiver, handle arg0) = @java:Method { + name: "setProtoDefinition", + 'class: "org.wso2.apk.config.model.API", + paramTypes: ["java.lang.String"] +} external; + isolated function org_wso2_apk_config_model_API_setName(handle receiver, handle arg0) = @java:Method { name: "setName", 'class: "org.wso2.apk.config.model.API", diff --git a/runtime/config-deployer-service/java/src/main/java/org/wso2/apk/config/model/API.java b/runtime/config-deployer-service/java/src/main/java/org/wso2/apk/config/model/API.java index 9ec779906..a88c2508f 100644 --- a/runtime/config-deployer-service/java/src/main/java/org/wso2/apk/config/model/API.java +++ b/runtime/config-deployer-service/java/src/main/java/org/wso2/apk/config/model/API.java @@ -11,6 +11,7 @@ public class API { private String apiSecurity; private String[] scopes; private String graphQLSchema; + private String protoDefinition; private String swaggerDefinition; private String environment; @@ -80,7 +81,11 @@ public void setGraphQLSchema(String graphQLSchema) { } public void setProtoDefinition(String protoDefinition) { - this.swaggerDefinition = protoDefinition; + this.protoDefinition = protoDefinition; + } + + public String getProtoDefinition() { + return protoDefinition; } public String getSwaggerDefinition() { return swaggerDefinition; From 41ae5cc0eac756ca8848f4117c393dd4f7c16f09 Mon Sep 17 00:00:00 2001 From: DinethH Date: Wed, 24 Apr 2024 10:51:59 +0530 Subject: [PATCH 07/29] Add deploying api using apk-conf and proto file --- .../ballerina/DeployerClient.bal | 60 ++++++++++++++++++- .../ballerina/K8sClient.bal | 25 +++++++- 2 files changed, 81 insertions(+), 4 deletions(-) diff --git a/runtime/config-deployer-service/ballerina/DeployerClient.bal b/runtime/config-deployer-service/ballerina/DeployerClient.bal index e2c770dd7..21d0aef40 100644 --- a/runtime/config-deployer-service/ballerina/DeployerClient.bal +++ b/runtime/config-deployer-service/ballerina/DeployerClient.bal @@ -95,6 +95,7 @@ public class DeployerClient { apiArtifact.namespace = apiPartition.namespace; if existingAPI is model:API { check self.deleteHttpRoutes(existingAPI, apiArtifact?.organization); + check self.deleteGrpcRoutes(existingAPI, apiArtifact?.organization); check self.deleteAuthenticationCRs(existingAPI, apiArtifact?.organization); _ = check self.deleteScopeCrsForAPI(existingAPI, apiArtifact?.organization); check self.deleteBackends(existingAPI, apiArtifact?.organization); @@ -122,8 +123,8 @@ public class DeployerClient { check self.deployBackendJWTConfigs(apiArtifact, ownerReference); check self.deployAPIPolicyCRs(apiArtifact, ownerReference); - check self.deployRoutes(apiArtifact.productionHttpRoutes, apiArtifact.productionGqlRoutes, apiArtifact?.namespace, ownerReference); - check self.deployRoutes(apiArtifact.sandboxHttpRoutes, apiArtifact.sandboxGqlRoutes, apiArtifact?.namespace, ownerReference); + check self.deployRoutes(apiArtifact.productionHttpRoutes, apiArtifact.productionGqlRoutes,apiArtifact.productionGrpcRoutes, apiArtifact?.namespace, ownerReference); + check self.deployRoutes(apiArtifact.sandboxHttpRoutes, apiArtifact.sandboxGqlRoutes,apiArtifact.sandboxGrpcRoutes, apiArtifact?.namespace, ownerReference); return deployK8sAPICrResult; } on fail var e { @@ -173,6 +174,30 @@ public class DeployerClient { } } + private isolated function deleteGrpcRoutes(model:API api, string organization) returns commons:APKError? { + do { + model:GRPCRouteList|http:ClientError grpcRouteListResponse = check getGrpcRoutesForAPIs(api.spec.apiName, api.spec.apiVersion, api.metadata?.namespace, organization); + if grpcRouteListResponse is model:GRPCRouteList { + foreach model:GRPCRoute item in grpcRouteListResponse.items { + http:Response|http:ClientError grpcRouteDeletionResponse = deleteGrpcRoute(item.metadata.name, api.metadata?.namespace); + if grpcRouteDeletionResponse is http:Response { + if grpcRouteDeletionResponse.statusCode != http:STATUS_OK { + json responsePayLoad = check grpcRouteDeletionResponse.getJsonPayload(); + model:Status statusResponse = check responsePayLoad.cloneWithType(model:Status); + check self.handleK8sTimeout(statusResponse); + } + } else { + log:printError("Error occured while deleting GrpcRoute", grpcRouteDeletionResponse); + } + } + return; + } + } on fail var e { + log:printError("Error occured deleting grpcRoutes", e); + return e909022("Error occured deleting grpcRoutes", e); + } + } + private isolated function deleteHttpRoutes(model:API api, string organization) returns commons:APKError? { do { model:HTTPRouteList|http:ClientError httpRouteListResponse = check getHttproutesForAPIS(api.spec.apiName, api.spec.apiVersion, api.metadata?.namespace, organization); @@ -349,7 +374,7 @@ public class DeployerClient { return e909022("Internal error occured", e = error("Internal error occured")); } } - private isolated function deployRoutes(model:HTTPRoute[]? httproutes, model:GQLRoute[]? gqlroutes, string namespace, model:OwnerReference ownerReference) returns error? { + private isolated function deployRoutes(model:HTTPRoute[]? httproutes, model:GQLRoute[]? gqlroutes,model:GRPCRoute[]? grpcroutes, string namespace, model:OwnerReference ownerReference) returns error? { if httproutes is model:HTTPRoute[] && httproutes.length() > 0 { model:HTTPRoute[] deployReadyHttproutes = httproutes; model:HTTPRoute[]|commons:APKError orderedHttproutes = self.createHttpRoutesOrder(httproutes); @@ -408,6 +433,35 @@ public class DeployerClient { } } } + } else if grpcroutes is model:GRPCRoute[] && grpcroutes.length() > 0 { + model:GRPCRoute[] deployReadyGrpcRoutes = grpcroutes; + model:GRPCRoute[]|commons:APKError orderedGrpcRoutes = self.createGrpcRoutesOrder(grpcroutes); + if orderedGrpcRoutes is model:GRPCRoute[] { + deployReadyGrpcRoutes = orderedGrpcRoutes; + } + foreach model:GRPCRoute grpcRoute in deployReadyGrpcRoutes { + grpcRoute.metadata.ownerReferences = [ownerReference]; + if grpcRoute.spec.rules.length() > 0 { + http:Response deployGrpcRouteResult = check deployGrpcRoute(grpcRoute, namespace); + if deployGrpcRouteResult.statusCode == http:STATUS_CREATED { + log:printDebug("Deployed GrpcRoute Successfully" + grpcRoute.toString()); + } else if deployGrpcRouteResult.statusCode == http:STATUS_CONFLICT { + log:printDebug("GrpcRoute already exists" + grpcRoute.toString()); + model:GRPCRoute grpcRouteFromK8s = check getGrpcRoute(grpcRoute.metadata.name, namespace); + grpcRoute.metadata.resourceVersion = grpcRouteFromK8s.metadata.resourceVersion; + http:Response grpcRouteCR = check updateGrpcRoute(grpcRoute, namespace); + if grpcRouteCR.statusCode != http:STATUS_OK { + json responsePayLoad = check grpcRouteCR.getJsonPayload(); + model:Status statusResponse = check responsePayLoad.cloneWithType(model:Status); + check self.handleK8sTimeout(statusResponse); + } + } else { + json responsePayLoad = check deployGrpcRouteResult.getJsonPayload(); + model:Status statusResponse = check responsePayLoad.cloneWithType(model:Status); + check self.handleK8sTimeout(statusResponse); + } + } + } } } diff --git a/runtime/config-deployer-service/ballerina/K8sClient.bal b/runtime/config-deployer-service/ballerina/K8sClient.bal index 5e7757206..cdc03e00b 100644 --- a/runtime/config-deployer-service/ballerina/K8sClient.bal +++ b/runtime/config-deployer-service/ballerina/K8sClient.bal @@ -100,6 +100,16 @@ isolated function deleteGqlRoute(string name, string namespace) returns http:Res return k8sApiServerEp->delete(endpoint, targetType = http:Response); } +isolated function getGrpcRoute(string name, string namespace) returns model:GRPCRoute|http:ClientError { + string endpoint = "/apis/gateway.networking.k8s.io/v1alpha2/namespaces/" + namespace + "/grpcroutes/" + name; + return k8sApiServerEp->get(endpoint, targetType = model:GRPCRoute); +} + +isolated function deleteGrpcRoute(string name, string namespace) returns http:Response|http:ClientError { + string endpoint = "/apis/gateway.networking.k8s.io/v1alpha2/namespaces/" + namespace + "/grpcroutes/" + name; + return k8sApiServerEp->delete(endpoint, targetType = http:Response); +} + isolated function getConfigMap(string name, string namespace) returns model:ConfigMap|http:ClientError { string endpoint = "/api/v1/namespaces/" + namespace + "/configmaps/" + name; return k8sApiServerEp->get(endpoint, targetType = model:ConfigMap); @@ -150,6 +160,16 @@ isolated function updateGqlRoute(model:GQLRoute gqlroute, string namespace) retu return k8sApiServerEp->put(endpoint, gqlroute, targetType = http:Response); } +isolated function deployGrpcRoute(model:GRPCRoute grpcRoute, string namespace) returns http:Response|http:ClientError { + string endpoint = "/apis/gateway.networking.k8s.io/v1alpha2/namespaces/" + namespace + "/grpcroutes"; + return k8sApiServerEp->post(endpoint, grpcRoute, targetType = http:Response); +} + +isolated function updateGrpcRoute(model:GRPCRoute grpcRoute, string namespace) returns http:Response|http:ClientError { + string endpoint = "/apis/gateway.networking.k8s.io/v1alpha2/namespaces/" + namespace + "/grpcroutes/" + grpcRoute.metadata.name; + return k8sApiServerEp->put(endpoint, grpcRoute, targetType = http:Response); +} + public isolated function getK8sAPIByNameAndNamespace(string name, string namespace) returns model:API?|commons:APKError { string endpoint = "/apis/dp.wso2.com/v1alpha2/namespaces/" + namespace + "/apis/" + name; do { @@ -244,7 +264,10 @@ public isolated function getGqlRoutesForAPIs(string apiName, string apiVersion, string endpoint = "/apis/dp.wso2.com/v1alpha2/namespaces/" + namespace + "/gqlroutes/?labelSelector=" + check generateUrlEncodedLabelSelector(apiName, apiVersion, organization); return k8sApiServerEp->get(endpoint, targetType = model:GQLRouteList); } - +public isolated function getGrpcRoutesForAPIs(string apiName, string apiVersion, string namespace, string organization) returns model:GRPCRouteList|http:ClientError|error { + string endpoint = "/apis/dp.wso2.com/v1alpha2/namespaces/" + namespace + "/grpcroutes/?labelSelector=" + check generateUrlEncodedLabelSelector(apiName, apiVersion, organization); + return k8sApiServerEp->get(endpoint, targetType = model:GRPCRouteList); +} isolated function deployRateLimitPolicyCR(model:RateLimitPolicy rateLimitPolicy, string namespace) returns http:Response|http:ClientError { string endpoint = "/apis/dp.wso2.com/v1alpha1/namespaces/" + namespace + "/ratelimitpolicies"; return k8sApiServerEp->post(endpoint, rateLimitPolicy, targetType = http:Response); From f4249420ac485bf8081cb4da14a22d82d07971d3 Mon Sep 17 00:00:00 2001 From: DinethH Date: Wed, 24 Apr 2024 11:23:49 +0530 Subject: [PATCH 08/29] add cucumber tests --- .../artifacts/apk-confs/grpc/grpc.apk-conf | 27 +++++++++++++++++++ .../artifacts/definitions/student.proto | 21 +++++++++++++++ .../src/test/resources/tests/api/GRPC.feature | 6 +++++ 3 files changed, 54 insertions(+) create mode 100644 test/cucumber-tests/src/test/resources/artifacts/apk-confs/grpc/grpc.apk-conf create mode 100644 test/cucumber-tests/src/test/resources/artifacts/definitions/student.proto create mode 100644 test/cucumber-tests/src/test/resources/tests/api/GRPC.feature diff --git a/test/cucumber-tests/src/test/resources/artifacts/apk-confs/grpc/grpc.apk-conf b/test/cucumber-tests/src/test/resources/artifacts/apk-confs/grpc/grpc.apk-conf new file mode 100644 index 000000000..1653a1f88 --- /dev/null +++ b/test/cucumber-tests/src/test/resources/artifacts/apk-confs/grpc/grpc.apk-conf @@ -0,0 +1,27 @@ +--- +name: "demo-grpc-api" +basePath: "/dineth.grpc.api" +version: "v1" +type: "GRPC" +endpointConfigurations: + production: + endpoint: "http://grpc-backend:6565" +defaultVersion: false +subscriptionValidation: false +operations: +- target: "student.StudentService" + verb: "GetStudent" + secured: true + scopes: [] +- target: "student.StudentService" + verb: "GetStudentStream" + secured: true + scopes: [] +- target: "student.StudentService" + verb: "SendStudentStream" + secured: true + scopes: [] +- target: "student.StudentService" + verb: "SendAndGetStudentStream" + secured: true + scopes: [] \ No newline at end of file diff --git a/test/cucumber-tests/src/test/resources/artifacts/definitions/student.proto b/test/cucumber-tests/src/test/resources/artifacts/definitions/student.proto new file mode 100644 index 000000000..885c45cfb --- /dev/null +++ b/test/cucumber-tests/src/test/resources/artifacts/definitions/student.proto @@ -0,0 +1,21 @@ +syntax = "proto3"; + +option java_multiple_files = true; +option java_package = "org.example"; +package dineth.grpc.api.v1.student; + +service StudentService { + rpc GetStudent(StudentRequest) returns (StudentResponse) {}; + rpc GetStudentStream(StudentRequest) returns (stream StudentResponse) {}; + rpc SendStudentStream(stream StudentRequest) returns (StudentResponse) {}; + rpc SendAndGetStudentStream(stream StudentRequest) returns (stream StudentResponse) {} +} + +message StudentRequest { + int32 id = 3; +} + +message StudentResponse { + string name = 1; + int32 age = 2; +} diff --git a/test/cucumber-tests/src/test/resources/tests/api/GRPC.feature b/test/cucumber-tests/src/test/resources/tests/api/GRPC.feature new file mode 100644 index 000000000..66fd7e748 --- /dev/null +++ b/test/cucumber-tests/src/test/resources/tests/api/GRPC.feature @@ -0,0 +1,6 @@ +Feature: Generating APK conf for gRPC API + Scenario: Generating APK conf using a valid GRPC API definition + Given The system is ready + When I use the definition file "artifacts/definitions/student.proto" in resources + And generate the APK conf file for a "gRPC" API + Then the response status code should be 200 From 7fb480c495c311426867e25618941e19107cc201 Mon Sep 17 00:00:00 2001 From: DinethH Date: Wed, 24 Apr 2024 14:49:39 +0530 Subject: [PATCH 09/29] add grpc backend deployment and service CRs --- test/cucumber-tests/CRs/artifacts.yaml | 48 +++++++++++++++++++ .../artifacts/apk-confs/grpc/grpc.apk-conf | 1 + 2 files changed, 49 insertions(+) diff --git a/test/cucumber-tests/CRs/artifacts.yaml b/test/cucumber-tests/CRs/artifacts.yaml index cf592dad8..c49bb4680 100644 --- a/test/cucumber-tests/CRs/artifacts.yaml +++ b/test/cucumber-tests/CRs/artifacts.yaml @@ -1154,3 +1154,51 @@ spec: protocol: TCP selector: app: graphql-faker +--- +apiVersion: v1 +kind: Service +metadata: + name: grpc-backend-v1 + namespace: apk-integration-test +spec: + selector: + app: grpc-backend-v1 + ports: + - protocol: TCP + port: 6565 + targetPort: 6565 +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: grpc-backend-v1 + namespace: apk-integration-test + labels: + app: grpc-backend-v1 +spec: + replicas: 1 + selector: + matchLabels: + app: grpc-backend-v1 + template: + metadata: + labels: + app: grpc-backend-v1 + spec: + containers: + - name: grpc-backend-v1 + image: ddh13/dineth-grpc-demo-server:1.0.0 + imagePullPolicy: Always + env: + - name: POD_NAME + valueFrom: + fieldRef: + fieldPath: metadata.name + - name: NAMESPACE + valueFrom: + fieldRef: + fieldPath: metadata.namespace + resources: + requests: + cpu: 10m +--- \ No newline at end of file diff --git a/test/cucumber-tests/src/test/resources/artifacts/apk-confs/grpc/grpc.apk-conf b/test/cucumber-tests/src/test/resources/artifacts/apk-confs/grpc/grpc.apk-conf index 1653a1f88..4d291742b 100644 --- a/test/cucumber-tests/src/test/resources/artifacts/apk-confs/grpc/grpc.apk-conf +++ b/test/cucumber-tests/src/test/resources/artifacts/apk-confs/grpc/grpc.apk-conf @@ -3,6 +3,7 @@ name: "demo-grpc-api" basePath: "/dineth.grpc.api" version: "v1" type: "GRPC" +id: "grpc-basic-api" endpointConfigurations: production: endpoint: "http://grpc-backend:6565" From 5fa31113eec2efbfc1687a643d9cb546bfe4f9c3 Mon Sep 17 00:00:00 2001 From: DinethH Date: Wed, 24 Apr 2024 15:58:02 +0530 Subject: [PATCH 10/29] add code needed for cucumber tests --- .../ballerina/DeployerClient.bal | 2 + test/cucumber-tests/build.gradle | 3 + .../wso2/apk/integration/api/BaseSteps.java | 7 + .../apk/integration/api/SharedContext.java | 20 + .../clients/SimpleGRPCStudentClient.java | 40 ++ .../clients/studentGrpcClient/Student.java | 68 ++ .../studentGrpcClient/StudentRequest.java | 483 ++++++++++++++ .../StudentRequestOrBuilder.java | 15 + .../studentGrpcClient/StudentResponse.java | 621 ++++++++++++++++++ .../StudentResponseOrBuilder.java | 27 + .../studentGrpcClient/StudentServiceGrpc.java | 458 +++++++++++++ .../src/test/resources/tests/api/GRPC.feature | 29 + 12 files changed, 1773 insertions(+) create mode 100644 test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/SimpleGRPCStudentClient.java create mode 100644 test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/studentGrpcClient/Student.java create mode 100644 test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/studentGrpcClient/StudentRequest.java create mode 100644 test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/studentGrpcClient/StudentRequestOrBuilder.java create mode 100644 test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/studentGrpcClient/StudentResponse.java create mode 100644 test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/studentGrpcClient/StudentResponseOrBuilder.java create mode 100644 test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/studentGrpcClient/StudentServiceGrpc.java diff --git a/runtime/config-deployer-service/ballerina/DeployerClient.bal b/runtime/config-deployer-service/ballerina/DeployerClient.bal index 21d0aef40..3bcfb20e6 100644 --- a/runtime/config-deployer-service/ballerina/DeployerClient.bal +++ b/runtime/config-deployer-service/ballerina/DeployerClient.bal @@ -337,8 +337,10 @@ public class DeployerClient { model:StatusCause[] 'causes = details.'causes; foreach model:StatusCause 'cause in 'causes { if 'cause.'field == "spec.basePath" { + log:printError("Error occured while updating K8sAPI due to basePath ", e909015(k8sAPI.spec.basePath)); return e909015(k8sAPI.spec.basePath); } else if 'cause.'field == "spec.apiName" { + log:printError("Error occured while updating K8sAPI due to apiName ", e909016(k8sAPI.spec.apiName)); return e909016(k8sAPI.spec.apiName); } } diff --git a/test/cucumber-tests/build.gradle b/test/cucumber-tests/build.gradle index 1e348b543..c589336f9 100644 --- a/test/cucumber-tests/build.gradle +++ b/test/cucumber-tests/build.gradle @@ -35,6 +35,9 @@ dependencies { testImplementation 'io.cucumber:cucumber-testng:7.13.0' testImplementation 'commons-io:commons-io:2.13.0' testImplementation 'com.nimbusds:nimbus-jose-jwt:9.31' + implementation 'io.grpc:grpc-netty-shaded:1.57.0' + implementation 'io.grpc:grpc-protobuf:1.57.0' + implementation 'io.grpc:grpc-stub:1.57.0' } test { diff --git a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/BaseSteps.java b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/BaseSteps.java index bdc423ec6..053a0ab0e 100644 --- a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/BaseSteps.java +++ b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/BaseSteps.java @@ -49,6 +49,7 @@ import org.testng.Assert; import org.wso2.apk.integration.utils.Constants; import org.wso2.apk.integration.utils.Utils; +import org.wso2.apk.integration.utils.clients.SimpleGRPCStudentClient; import org.wso2.apk.integration.utils.clients.SimpleHTTPClient; import java.io.IOException; @@ -139,6 +140,12 @@ public void sendHttpRequest(String httpMethod, String url, String body) throws I } } + @Then("I make grpc request") + public void sendGrpcRequest() throws IOException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException { + SimpleGRPCStudentClient grpcStudentClient = new SimpleGRPCStudentClient(); + sharedContext.setStudentResponse(grpcStudentClient.GetStudent()); + } + // It will send request using a new thread and forget about the response @Then("I send {string} async request to {string} with body {string}") public void sendAsyncHttpRequest(String httpMethod, String url, String body) throws IOException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException { diff --git a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/SharedContext.java b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/SharedContext.java index 782e06173..822ff6335 100644 --- a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/SharedContext.java +++ b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/SharedContext.java @@ -18,7 +18,9 @@ package org.wso2.apk.integration.api; import org.apache.http.HttpResponse; +import org.wso2.apk.integration.utils.clients.SimpleGRPCStudentClient; import org.wso2.apk.integration.utils.clients.SimpleHTTPClient; +import org.wso2.apk.integration.utils.clients.studentGrpcClient.StudentResponse; import java.security.KeyManagementException; import java.security.KeyStoreException; @@ -31,6 +33,8 @@ public class SharedContext { private SimpleHTTPClient httpClient; + private SimpleGRPCStudentClient grpcStudentClient; + private StudentResponse studentResponse; private String accessToken; private HttpResponse response; private String responseBody; @@ -43,6 +47,12 @@ public SimpleHTTPClient getHttpClient() throws NoSuchAlgorithmException, KeyStor } return httpClient; } + public SimpleGRPCStudentClient getGrpcStudentClient()throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException { + if (grpcStudentClient == null) { + grpcStudentClient = new SimpleGRPCStudentClient(); + } + return grpcStudentClient; + } public String getAccessToken() { @@ -59,6 +69,16 @@ public HttpResponse getResponse() { return response; } + public StudentResponse getStudentResponse() { + + return studentResponse; + } + + public void setStudentResponse(StudentResponse studentResponse) { + + this.studentResponse = studentResponse; + } + public void setResponse(HttpResponse response) { this.response = response; diff --git a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/SimpleGRPCStudentClient.java b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/SimpleGRPCStudentClient.java new file mode 100644 index 000000000..0ab586bbd --- /dev/null +++ b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/SimpleGRPCStudentClient.java @@ -0,0 +1,40 @@ +package org.wso2.apk.integration.utils.clients; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import io.grpc.ManagedChannel; +import io.grpc.ManagedChannelBuilder; +import org.wso2.apk.integration.utils.clients.studentGrpcClient.StudentRequest; +import org.wso2.apk.integration.utils.clients.studentGrpcClient.StudentResponse; +import org.wso2.apk.integration.utils.clients.studentGrpcClient.StudentServiceGrpc; + +import java.security.KeyManagementException; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; + +public class SimpleGRPCStudentClient { + protected Log log = LogFactory.getLog(SimpleGRPCStudentClient.class); + private static final int EVENTUAL_SUCCESS_RESPONSE_TIMEOUT_IN_SECONDS = 10; + + private ManagedChannel managedChannel; + + public SimpleGRPCStudentClient() throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException { + this.managedChannel = ManagedChannelBuilder.forAddress("default.gw.wso2.com", 9095).usePlaintext().build(); + log.info("ManagedChannel created"); + } + + public StudentResponse GetStudent() { + //Create a blocking stub for the StudentService + StudentServiceGrpc.StudentServiceBlockingStub blockingStub = StudentServiceGrpc.newBlockingStub(managedChannel); + + // Make a synchronous gRPC call to get student details for ID 1 + StudentResponse studentResponse = blockingStub.getStudent(StudentRequest.newBuilder().setId(1).build()); + + // Log the response received from the gRPC server + log.info("response = " + studentResponse.getName() + " " + studentResponse.getAge()); + return studentResponse; + } + + +} + diff --git a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/studentGrpcClient/Student.java b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/studentGrpcClient/Student.java new file mode 100644 index 000000000..30ef19bd2 --- /dev/null +++ b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/studentGrpcClient/Student.java @@ -0,0 +1,68 @@ +// Generated by the protocol buffer compiler. DO NOT EDIT! +// source: student.proto + +package org.wso2.apk.integration.utils.clients.studentGrpcClient; + +public final class Student { + private Student() {} + public static void registerAllExtensions( + com.google.protobuf.ExtensionRegistryLite registry) { + } + + public static void registerAllExtensions( + com.google.protobuf.ExtensionRegistry registry) { + registerAllExtensions( + (com.google.protobuf.ExtensionRegistryLite) registry); + } + static final com.google.protobuf.Descriptors.Descriptor + internal_static_student_StudentRequest_descriptor; + static final + com.google.protobuf.GeneratedMessageV3.FieldAccessorTable + internal_static_student_StudentRequest_fieldAccessorTable; + static final com.google.protobuf.Descriptors.Descriptor + internal_static_student_StudentResponse_descriptor; + static final + com.google.protobuf.GeneratedMessageV3.FieldAccessorTable + internal_static_student_StudentResponse_fieldAccessorTable; + + public static com.google.protobuf.Descriptors.FileDescriptor + getDescriptor() { + return descriptor; + } + private static com.google.protobuf.Descriptors.FileDescriptor + descriptor; + static { + String[] descriptorData = { + "\n\rstudent.proto\022\007student\"\034\n\016StudentReque" + + "st\022\n\n\002id\030\003 \001(\005\",\n\017StudentResponse\022\014\n\004nam" + + "e\030\001 \001(\t\022\013\n\003age\030\002 \001(\0052\276\002\n\016StudentService\022" + + "A\n\nGetStudent\022\027.student.StudentRequest\032\030" + + ".student.StudentResponse\"\000\022I\n\020GetStudent" + + "Stream\022\027.student.StudentRequest\032\030.studen" + + "t.StudentResponse\"\0000\001\022J\n\021SendStudentStre" + + "am\022\027.student.StudentRequest\032\030.student.St" + + "udentResponse\"\000(\001\022R\n\027SendAndGetStudentSt" + + "ream\022\027.student.StudentRequest\032\030.student." + + "StudentResponse\"\000(\0010\001B\017\n\013org.exampleP\001b\006" + + "proto3" + }; + descriptor = com.google.protobuf.Descriptors.FileDescriptor + .internalBuildGeneratedFileFrom(descriptorData, + new com.google.protobuf.Descriptors.FileDescriptor[] { + }); + internal_static_student_StudentRequest_descriptor = + getDescriptor().getMessageTypes().get(0); + internal_static_student_StudentRequest_fieldAccessorTable = new + com.google.protobuf.GeneratedMessageV3.FieldAccessorTable( + internal_static_student_StudentRequest_descriptor, + new String[] { "Id", }); + internal_static_student_StudentResponse_descriptor = + getDescriptor().getMessageTypes().get(1); + internal_static_student_StudentResponse_fieldAccessorTable = new + com.google.protobuf.GeneratedMessageV3.FieldAccessorTable( + internal_static_student_StudentResponse_descriptor, + new String[] { "Name", "Age", }); + } + + // @@protoc_insertion_point(outer_class_scope) +} diff --git a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/studentGrpcClient/StudentRequest.java b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/studentGrpcClient/StudentRequest.java new file mode 100644 index 000000000..a79d2d9d9 --- /dev/null +++ b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/studentGrpcClient/StudentRequest.java @@ -0,0 +1,483 @@ +// Generated by the protocol buffer compiler. DO NOT EDIT! +// source: student.proto + +package org.wso2.apk.integration.utils.clients.studentGrpcClient; + +/** + * Protobuf type {@code student.StudentRequest} + */ +public final class StudentRequest extends + com.google.protobuf.GeneratedMessageV3 implements + // @@protoc_insertion_point(message_implements:student.StudentRequest) + StudentRequestOrBuilder { +private static final long serialVersionUID = 0L; + // Use StudentRequest.newBuilder() to construct. + private StudentRequest(com.google.protobuf.GeneratedMessageV3.Builder builder) { + super(builder); + } + private StudentRequest() { + } + + @Override + @SuppressWarnings({"unused"}) + protected Object newInstance( + UnusedPrivateParameter unused) { + return new StudentRequest(); + } + + @Override + public final com.google.protobuf.UnknownFieldSet + getUnknownFields() { + return this.unknownFields; + } + private StudentRequest( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + this(); + if (extensionRegistry == null) { + throw new NullPointerException(); + } + com.google.protobuf.UnknownFieldSet.Builder unknownFields = + com.google.protobuf.UnknownFieldSet.newBuilder(); + try { + boolean done = false; + while (!done) { + int tag = input.readTag(); + switch (tag) { + case 0: + done = true; + break; + case 24: { + + id_ = input.readInt32(); + break; + } + default: { + if (!parseUnknownField( + input, unknownFields, extensionRegistry, tag)) { + done = true; + } + break; + } + } + } + } catch (com.google.protobuf.InvalidProtocolBufferException e) { + throw e.setUnfinishedMessage(this); + } catch (java.io.IOException e) { + throw new com.google.protobuf.InvalidProtocolBufferException( + e).setUnfinishedMessage(this); + } finally { + this.unknownFields = unknownFields.build(); + makeExtensionsImmutable(); + } + } + public static final com.google.protobuf.Descriptors.Descriptor + getDescriptor() { + return Student.internal_static_student_StudentRequest_descriptor; + } + + @Override + protected FieldAccessorTable + internalGetFieldAccessorTable() { + return Student.internal_static_student_StudentRequest_fieldAccessorTable + .ensureFieldAccessorsInitialized( + StudentRequest.class, StudentRequest.Builder.class); + } + + public static final int ID_FIELD_NUMBER = 3; + private int id_; + /** + * int32 id = 3; + * @return The id. + */ + @Override + public int getId() { + return id_; + } + + private byte memoizedIsInitialized = -1; + @Override + public final boolean isInitialized() { + byte isInitialized = memoizedIsInitialized; + if (isInitialized == 1) return true; + if (isInitialized == 0) return false; + + memoizedIsInitialized = 1; + return true; + } + + @Override + public void writeTo(com.google.protobuf.CodedOutputStream output) + throws java.io.IOException { + if (id_ != 0) { + output.writeInt32(3, id_); + } + unknownFields.writeTo(output); + } + + @Override + public int getSerializedSize() { + int size = memoizedSize; + if (size != -1) return size; + + size = 0; + if (id_ != 0) { + size += com.google.protobuf.CodedOutputStream + .computeInt32Size(3, id_); + } + size += unknownFields.getSerializedSize(); + memoizedSize = size; + return size; + } + + @Override + public boolean equals(final Object obj) { + if (obj == this) { + return true; + } + if (!(obj instanceof StudentRequest)) { + return super.equals(obj); + } + StudentRequest other = (StudentRequest) obj; + + if (getId() + != other.getId()) return false; + if (!unknownFields.equals(other.unknownFields)) return false; + return true; + } + + @Override + public int hashCode() { + if (memoizedHashCode != 0) { + return memoizedHashCode; + } + int hash = 41; + hash = (19 * hash) + getDescriptor().hashCode(); + hash = (37 * hash) + ID_FIELD_NUMBER; + hash = (53 * hash) + getId(); + hash = (29 * hash) + unknownFields.hashCode(); + memoizedHashCode = hash; + return hash; + } + + public static StudentRequest parseFrom( + java.nio.ByteBuffer data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + public static StudentRequest parseFrom( + java.nio.ByteBuffer data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + public static StudentRequest parseFrom( + com.google.protobuf.ByteString data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + public static StudentRequest parseFrom( + com.google.protobuf.ByteString data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + public static StudentRequest parseFrom(byte[] data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + public static StudentRequest parseFrom( + byte[] data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + public static StudentRequest parseFrom(java.io.InputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input); + } + public static StudentRequest parseFrom( + java.io.InputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input, extensionRegistry); + } + public static StudentRequest parseDelimitedFrom(java.io.InputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseDelimitedWithIOException(PARSER, input); + } + public static StudentRequest parseDelimitedFrom( + java.io.InputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseDelimitedWithIOException(PARSER, input, extensionRegistry); + } + public static StudentRequest parseFrom( + com.google.protobuf.CodedInputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input); + } + public static StudentRequest parseFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input, extensionRegistry); + } + + @Override + public Builder newBuilderForType() { return newBuilder(); } + public static Builder newBuilder() { + return DEFAULT_INSTANCE.toBuilder(); + } + public static Builder newBuilder(StudentRequest prototype) { + return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype); + } + @Override + public Builder toBuilder() { + return this == DEFAULT_INSTANCE + ? new Builder() : new Builder().mergeFrom(this); + } + + @Override + protected Builder newBuilderForType( + BuilderParent parent) { + Builder builder = new Builder(parent); + return builder; + } + /** + * Protobuf type {@code student.StudentRequest} + */ + public static final class Builder extends + com.google.protobuf.GeneratedMessageV3.Builder implements + // @@protoc_insertion_point(builder_implements:student.StudentRequest) + StudentRequestOrBuilder { + public static final com.google.protobuf.Descriptors.Descriptor + getDescriptor() { + return Student.internal_static_student_StudentRequest_descriptor; + } + + @Override + protected FieldAccessorTable + internalGetFieldAccessorTable() { + return Student.internal_static_student_StudentRequest_fieldAccessorTable + .ensureFieldAccessorsInitialized( + StudentRequest.class, StudentRequest.Builder.class); + } + + // Construct using StudentRequest.newBuilder() + private Builder() { + maybeForceBuilderInitialization(); + } + + private Builder( + BuilderParent parent) { + super(parent); + maybeForceBuilderInitialization(); + } + private void maybeForceBuilderInitialization() { + if (com.google.protobuf.GeneratedMessageV3 + .alwaysUseFieldBuilders) { + } + } + @Override + public Builder clear() { + super.clear(); + id_ = 0; + + return this; + } + + @Override + public com.google.protobuf.Descriptors.Descriptor + getDescriptorForType() { + return Student.internal_static_student_StudentRequest_descriptor; + } + + @Override + public StudentRequest getDefaultInstanceForType() { + return StudentRequest.getDefaultInstance(); + } + + @Override + public StudentRequest build() { + StudentRequest result = buildPartial(); + if (!result.isInitialized()) { + throw newUninitializedMessageException(result); + } + return result; + } + + @Override + public StudentRequest buildPartial() { + StudentRequest result = new StudentRequest(this); + result.id_ = id_; + onBuilt(); + return result; + } + + @Override + public Builder clone() { + return super.clone(); + } + @Override + public Builder setField( + com.google.protobuf.Descriptors.FieldDescriptor field, + Object value) { + return super.setField(field, value); + } + @Override + public Builder clearField( + com.google.protobuf.Descriptors.FieldDescriptor field) { + return super.clearField(field); + } + @Override + public Builder clearOneof( + com.google.protobuf.Descriptors.OneofDescriptor oneof) { + return super.clearOneof(oneof); + } + @Override + public Builder setRepeatedField( + com.google.protobuf.Descriptors.FieldDescriptor field, + int index, Object value) { + return super.setRepeatedField(field, index, value); + } + @Override + public Builder addRepeatedField( + com.google.protobuf.Descriptors.FieldDescriptor field, + Object value) { + return super.addRepeatedField(field, value); + } + @Override + public Builder mergeFrom(com.google.protobuf.Message other) { + if (other instanceof StudentRequest) { + return mergeFrom((StudentRequest)other); + } else { + super.mergeFrom(other); + return this; + } + } + + public Builder mergeFrom(StudentRequest other) { + if (other == StudentRequest.getDefaultInstance()) return this; + if (other.getId() != 0) { + setId(other.getId()); + } + this.mergeUnknownFields(other.unknownFields); + onChanged(); + return this; + } + + @Override + public final boolean isInitialized() { + return true; + } + + @Override + public Builder mergeFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + StudentRequest parsedMessage = null; + try { + parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry); + } catch (com.google.protobuf.InvalidProtocolBufferException e) { + parsedMessage = (StudentRequest) e.getUnfinishedMessage(); + throw e.unwrapIOException(); + } finally { + if (parsedMessage != null) { + mergeFrom(parsedMessage); + } + } + return this; + } + + private int id_ ; + /** + * int32 id = 3; + * @return The id. + */ + @Override + public int getId() { + return id_; + } + /** + * int32 id = 3; + * @param value The id to set. + * @return This builder for chaining. + */ + public Builder setId(int value) { + + id_ = value; + onChanged(); + return this; + } + /** + * int32 id = 3; + * @return This builder for chaining. + */ + public Builder clearId() { + + id_ = 0; + onChanged(); + return this; + } + @Override + public final Builder setUnknownFields( + final com.google.protobuf.UnknownFieldSet unknownFields) { + return super.setUnknownFields(unknownFields); + } + + @Override + public final Builder mergeUnknownFields( + final com.google.protobuf.UnknownFieldSet unknownFields) { + return super.mergeUnknownFields(unknownFields); + } + + + // @@protoc_insertion_point(builder_scope:student.StudentRequest) + } + + // @@protoc_insertion_point(class_scope:student.StudentRequest) + private static final StudentRequest DEFAULT_INSTANCE; + static { + DEFAULT_INSTANCE = new StudentRequest(); + } + + public static StudentRequest getDefaultInstance() { + return DEFAULT_INSTANCE; + } + + private static final com.google.protobuf.Parser + PARSER = new com.google.protobuf.AbstractParser() { + @Override + public StudentRequest parsePartialFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return new StudentRequest(input, extensionRegistry); + } + }; + + public static com.google.protobuf.Parser parser() { + return PARSER; + } + + @Override + public com.google.protobuf.Parser getParserForType() { + return PARSER; + } + + @Override + public StudentRequest getDefaultInstanceForType() { + return DEFAULT_INSTANCE; + } + +} + diff --git a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/studentGrpcClient/StudentRequestOrBuilder.java b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/studentGrpcClient/StudentRequestOrBuilder.java new file mode 100644 index 000000000..266630f4c --- /dev/null +++ b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/studentGrpcClient/StudentRequestOrBuilder.java @@ -0,0 +1,15 @@ +// Generated by the protocol buffer compiler. DO NOT EDIT! +// source: student.proto + +package org.wso2.apk.integration.utils.clients.studentGrpcClient; + +public interface StudentRequestOrBuilder extends + // @@protoc_insertion_point(interface_extends:student.StudentRequest) + com.google.protobuf.MessageOrBuilder { + + /** + * int32 id = 3; + * @return The id. + */ + int getId(); +} diff --git a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/studentGrpcClient/StudentResponse.java b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/studentGrpcClient/StudentResponse.java new file mode 100644 index 000000000..906538a20 --- /dev/null +++ b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/studentGrpcClient/StudentResponse.java @@ -0,0 +1,621 @@ +// Generated by the protocol buffer compiler. DO NOT EDIT! +// source: student.proto + +package org.wso2.apk.integration.utils.clients.studentGrpcClient; + +/** + * Protobuf type {@code student.StudentResponse} + */ +public final class StudentResponse extends + com.google.protobuf.GeneratedMessageV3 implements + // @@protoc_insertion_point(message_implements:student.StudentResponse) + StudentResponseOrBuilder { +private static final long serialVersionUID = 0L; + // Use StudentResponse.newBuilder() to construct. + private StudentResponse(com.google.protobuf.GeneratedMessageV3.Builder builder) { + super(builder); + } + private StudentResponse() { + name_ = ""; + } + + @Override + @SuppressWarnings({"unused"}) + protected Object newInstance( + UnusedPrivateParameter unused) { + return new StudentResponse(); + } + + @Override + public final com.google.protobuf.UnknownFieldSet + getUnknownFields() { + return this.unknownFields; + } + private StudentResponse( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + this(); + if (extensionRegistry == null) { + throw new NullPointerException(); + } + com.google.protobuf.UnknownFieldSet.Builder unknownFields = + com.google.protobuf.UnknownFieldSet.newBuilder(); + try { + boolean done = false; + while (!done) { + int tag = input.readTag(); + switch (tag) { + case 0: + done = true; + break; + case 10: { + String s = input.readStringRequireUtf8(); + + name_ = s; + break; + } + case 16: { + + age_ = input.readInt32(); + break; + } + default: { + if (!parseUnknownField( + input, unknownFields, extensionRegistry, tag)) { + done = true; + } + break; + } + } + } + } catch (com.google.protobuf.InvalidProtocolBufferException e) { + throw e.setUnfinishedMessage(this); + } catch (java.io.IOException e) { + throw new com.google.protobuf.InvalidProtocolBufferException( + e).setUnfinishedMessage(this); + } finally { + this.unknownFields = unknownFields.build(); + makeExtensionsImmutable(); + } + } + public static final com.google.protobuf.Descriptors.Descriptor + getDescriptor() { + return Student.internal_static_student_StudentResponse_descriptor; + } + + @Override + protected FieldAccessorTable + internalGetFieldAccessorTable() { + return Student.internal_static_student_StudentResponse_fieldAccessorTable + .ensureFieldAccessorsInitialized( + StudentResponse.class, StudentResponse.Builder.class); + } + + public static final int NAME_FIELD_NUMBER = 1; + private volatile Object name_; + /** + * string name = 1; + * @return The name. + */ + @Override + public String getName() { + Object ref = name_; + if (ref instanceof String) { + return (String) ref; + } else { + com.google.protobuf.ByteString bs = + (com.google.protobuf.ByteString) ref; + String s = bs.toStringUtf8(); + name_ = s; + return s; + } + } + /** + * string name = 1; + * @return The bytes for name. + */ + @Override + public com.google.protobuf.ByteString + getNameBytes() { + Object ref = name_; + if (ref instanceof String) { + com.google.protobuf.ByteString b = + com.google.protobuf.ByteString.copyFromUtf8( + (String) ref); + name_ = b; + return b; + } else { + return (com.google.protobuf.ByteString) ref; + } + } + + public static final int AGE_FIELD_NUMBER = 2; + private int age_; + /** + * int32 age = 2; + * @return The age. + */ + @Override + public int getAge() { + return age_; + } + + private byte memoizedIsInitialized = -1; + @Override + public final boolean isInitialized() { + byte isInitialized = memoizedIsInitialized; + if (isInitialized == 1) return true; + if (isInitialized == 0) return false; + + memoizedIsInitialized = 1; + return true; + } + + @Override + public void writeTo(com.google.protobuf.CodedOutputStream output) + throws java.io.IOException { + if (!getNameBytes().isEmpty()) { + com.google.protobuf.GeneratedMessageV3.writeString(output, 1, name_); + } + if (age_ != 0) { + output.writeInt32(2, age_); + } + unknownFields.writeTo(output); + } + + @Override + public int getSerializedSize() { + int size = memoizedSize; + if (size != -1) return size; + + size = 0; + if (!getNameBytes().isEmpty()) { + size += com.google.protobuf.GeneratedMessageV3.computeStringSize(1, name_); + } + if (age_ != 0) { + size += com.google.protobuf.CodedOutputStream + .computeInt32Size(2, age_); + } + size += unknownFields.getSerializedSize(); + memoizedSize = size; + return size; + } + + @Override + public boolean equals(final Object obj) { + if (obj == this) { + return true; + } + if (!(obj instanceof StudentResponse)) { + return super.equals(obj); + } + StudentResponse other = (StudentResponse) obj; + + if (!getName() + .equals(other.getName())) return false; + if (getAge() + != other.getAge()) return false; + if (!unknownFields.equals(other.unknownFields)) return false; + return true; + } + + @Override + public int hashCode() { + if (memoizedHashCode != 0) { + return memoizedHashCode; + } + int hash = 41; + hash = (19 * hash) + getDescriptor().hashCode(); + hash = (37 * hash) + NAME_FIELD_NUMBER; + hash = (53 * hash) + getName().hashCode(); + hash = (37 * hash) + AGE_FIELD_NUMBER; + hash = (53 * hash) + getAge(); + hash = (29 * hash) + unknownFields.hashCode(); + memoizedHashCode = hash; + return hash; + } + + public static StudentResponse parseFrom( + java.nio.ByteBuffer data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + public static StudentResponse parseFrom( + java.nio.ByteBuffer data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + public static StudentResponse parseFrom( + com.google.protobuf.ByteString data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + public static StudentResponse parseFrom( + com.google.protobuf.ByteString data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + public static StudentResponse parseFrom(byte[] data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + public static StudentResponse parseFrom( + byte[] data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + public static StudentResponse parseFrom(java.io.InputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input); + } + public static StudentResponse parseFrom( + java.io.InputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input, extensionRegistry); + } + public static StudentResponse parseDelimitedFrom(java.io.InputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseDelimitedWithIOException(PARSER, input); + } + public static StudentResponse parseDelimitedFrom( + java.io.InputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseDelimitedWithIOException(PARSER, input, extensionRegistry); + } + public static StudentResponse parseFrom( + com.google.protobuf.CodedInputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input); + } + public static StudentResponse parseFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input, extensionRegistry); + } + + @Override + public Builder newBuilderForType() { return newBuilder(); } + public static Builder newBuilder() { + return DEFAULT_INSTANCE.toBuilder(); + } + public static Builder newBuilder(StudentResponse prototype) { + return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype); + } + @Override + public Builder toBuilder() { + return this == DEFAULT_INSTANCE + ? new Builder() : new Builder().mergeFrom(this); + } + + @Override + protected Builder newBuilderForType( + BuilderParent parent) { + Builder builder = new Builder(parent); + return builder; + } + /** + * Protobuf type {@code student.StudentResponse} + */ + public static final class Builder extends + com.google.protobuf.GeneratedMessageV3.Builder implements + // @@protoc_insertion_point(builder_implements:student.StudentResponse) + StudentResponseOrBuilder { + public static final com.google.protobuf.Descriptors.Descriptor + getDescriptor() { + return Student.internal_static_student_StudentResponse_descriptor; + } + + @Override + protected FieldAccessorTable + internalGetFieldAccessorTable() { + return Student.internal_static_student_StudentResponse_fieldAccessorTable + .ensureFieldAccessorsInitialized( + StudentResponse.class, StudentResponse.Builder.class); + } + + // Construct using StudentResponse.newBuilder() + private Builder() { + maybeForceBuilderInitialization(); + } + + private Builder( + BuilderParent parent) { + super(parent); + maybeForceBuilderInitialization(); + } + private void maybeForceBuilderInitialization() { + if (com.google.protobuf.GeneratedMessageV3 + .alwaysUseFieldBuilders) { + } + } + @Override + public Builder clear() { + super.clear(); + name_ = ""; + + age_ = 0; + + return this; + } + + @Override + public com.google.protobuf.Descriptors.Descriptor + getDescriptorForType() { + return Student.internal_static_student_StudentResponse_descriptor; + } + + @Override + public StudentResponse getDefaultInstanceForType() { + return StudentResponse.getDefaultInstance(); + } + + @Override + public StudentResponse build() { + StudentResponse result = buildPartial(); + if (!result.isInitialized()) { + throw newUninitializedMessageException(result); + } + return result; + } + + @Override + public StudentResponse buildPartial() { + StudentResponse result = new StudentResponse(this); + result.name_ = name_; + result.age_ = age_; + onBuilt(); + return result; + } + + @Override + public Builder clone() { + return super.clone(); + } + @Override + public Builder setField( + com.google.protobuf.Descriptors.FieldDescriptor field, + Object value) { + return super.setField(field, value); + } + @Override + public Builder clearField( + com.google.protobuf.Descriptors.FieldDescriptor field) { + return super.clearField(field); + } + @Override + public Builder clearOneof( + com.google.protobuf.Descriptors.OneofDescriptor oneof) { + return super.clearOneof(oneof); + } + @Override + public Builder setRepeatedField( + com.google.protobuf.Descriptors.FieldDescriptor field, + int index, Object value) { + return super.setRepeatedField(field, index, value); + } + @Override + public Builder addRepeatedField( + com.google.protobuf.Descriptors.FieldDescriptor field, + Object value) { + return super.addRepeatedField(field, value); + } + @Override + public Builder mergeFrom(com.google.protobuf.Message other) { + if (other instanceof StudentResponse) { + return mergeFrom((StudentResponse)other); + } else { + super.mergeFrom(other); + return this; + } + } + + public Builder mergeFrom(StudentResponse other) { + if (other == StudentResponse.getDefaultInstance()) return this; + if (!other.getName().isEmpty()) { + name_ = other.name_; + onChanged(); + } + if (other.getAge() != 0) { + setAge(other.getAge()); + } + this.mergeUnknownFields(other.unknownFields); + onChanged(); + return this; + } + + @Override + public final boolean isInitialized() { + return true; + } + + @Override + public Builder mergeFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + StudentResponse parsedMessage = null; + try { + parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry); + } catch (com.google.protobuf.InvalidProtocolBufferException e) { + parsedMessage = (StudentResponse) e.getUnfinishedMessage(); + throw e.unwrapIOException(); + } finally { + if (parsedMessage != null) { + mergeFrom(parsedMessage); + } + } + return this; + } + + private Object name_ = ""; + /** + * string name = 1; + * @return The name. + */ + public String getName() { + Object ref = name_; + if (!(ref instanceof String)) { + com.google.protobuf.ByteString bs = + (com.google.protobuf.ByteString) ref; + String s = bs.toStringUtf8(); + name_ = s; + return s; + } else { + return (String) ref; + } + } + /** + * string name = 1; + * @return The bytes for name. + */ + public com.google.protobuf.ByteString + getNameBytes() { + Object ref = name_; + if (ref instanceof String) { + com.google.protobuf.ByteString b = + com.google.protobuf.ByteString.copyFromUtf8( + (String) ref); + name_ = b; + return b; + } else { + return (com.google.protobuf.ByteString) ref; + } + } + /** + * string name = 1; + * @param value The name to set. + * @return This builder for chaining. + */ + public Builder setName( + String value) { + if (value == null) { + throw new NullPointerException(); + } + + name_ = value; + onChanged(); + return this; + } + /** + * string name = 1; + * @return This builder for chaining. + */ + public Builder clearName() { + + name_ = getDefaultInstance().getName(); + onChanged(); + return this; + } + /** + * string name = 1; + * @param value The bytes for name to set. + * @return This builder for chaining. + */ + public Builder setNameBytes( + com.google.protobuf.ByteString value) { + if (value == null) { + throw new NullPointerException(); + } + checkByteStringIsUtf8(value); + + name_ = value; + onChanged(); + return this; + } + + private int age_ ; + /** + * int32 age = 2; + * @return The age. + */ + @Override + public int getAge() { + return age_; + } + /** + * int32 age = 2; + * @param value The age to set. + * @return This builder for chaining. + */ + public Builder setAge(int value) { + + age_ = value; + onChanged(); + return this; + } + /** + * int32 age = 2; + * @return This builder for chaining. + */ + public Builder clearAge() { + + age_ = 0; + onChanged(); + return this; + } + @Override + public final Builder setUnknownFields( + final com.google.protobuf.UnknownFieldSet unknownFields) { + return super.setUnknownFields(unknownFields); + } + + @Override + public final Builder mergeUnknownFields( + final com.google.protobuf.UnknownFieldSet unknownFields) { + return super.mergeUnknownFields(unknownFields); + } + + + // @@protoc_insertion_point(builder_scope:student.StudentResponse) + } + + // @@protoc_insertion_point(class_scope:student.StudentResponse) + private static final StudentResponse DEFAULT_INSTANCE; + static { + DEFAULT_INSTANCE = new StudentResponse(); + } + + public static StudentResponse getDefaultInstance() { + return DEFAULT_INSTANCE; + } + + private static final com.google.protobuf.Parser + PARSER = new com.google.protobuf.AbstractParser() { + @Override + public StudentResponse parsePartialFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return new StudentResponse(input, extensionRegistry); + } + }; + + public static com.google.protobuf.Parser parser() { + return PARSER; + } + + @Override + public com.google.protobuf.Parser getParserForType() { + return PARSER; + } + + @Override + public StudentResponse getDefaultInstanceForType() { + return DEFAULT_INSTANCE; + } + +} + diff --git a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/studentGrpcClient/StudentResponseOrBuilder.java b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/studentGrpcClient/StudentResponseOrBuilder.java new file mode 100644 index 000000000..4b16af775 --- /dev/null +++ b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/studentGrpcClient/StudentResponseOrBuilder.java @@ -0,0 +1,27 @@ +// Generated by the protocol buffer compiler. DO NOT EDIT! +// source: student.proto + +package org.wso2.apk.integration.utils.clients.studentGrpcClient; + +public interface StudentResponseOrBuilder extends + // @@protoc_insertion_point(interface_extends:student.StudentResponse) + com.google.protobuf.MessageOrBuilder { + + /** + * string name = 1; + * @return The name. + */ + String getName(); + /** + * string name = 1; + * @return The bytes for name. + */ + com.google.protobuf.ByteString + getNameBytes(); + + /** + * int32 age = 2; + * @return The age. + */ + int getAge(); +} diff --git a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/studentGrpcClient/StudentServiceGrpc.java b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/studentGrpcClient/StudentServiceGrpc.java new file mode 100644 index 000000000..60a066612 --- /dev/null +++ b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/studentGrpcClient/StudentServiceGrpc.java @@ -0,0 +1,458 @@ +package org.wso2.apk.integration.utils.clients.studentGrpcClient; + +import static io.grpc.MethodDescriptor.generateFullMethodName; + +/** + */ +//@javax.annotation.Generated( +// value = "by gRPC proto compiler (version 1.39.0)", +// comments = "Source: student.proto") +public final class StudentServiceGrpc { + + private StudentServiceGrpc() {} + + public static final String SERVICE_NAME = "student.StudentService"; + + // Static method descriptors that strictly reflect the proto. + private static volatile io.grpc.MethodDescriptor getGetStudentMethod; + + @io.grpc.stub.annotations.RpcMethod( + fullMethodName = SERVICE_NAME + '/' + "GetStudent", + requestType = StudentRequest.class, + responseType = StudentResponse.class, + methodType = io.grpc.MethodDescriptor.MethodType.UNARY) + public static io.grpc.MethodDescriptor getGetStudentMethod() { + io.grpc.MethodDescriptor getGetStudentMethod; + if ((getGetStudentMethod = StudentServiceGrpc.getGetStudentMethod) == null) { + synchronized (StudentServiceGrpc.class) { + if ((getGetStudentMethod = StudentServiceGrpc.getGetStudentMethod) == null) { + StudentServiceGrpc.getGetStudentMethod = getGetStudentMethod = + io.grpc.MethodDescriptor.newBuilder() + .setType(io.grpc.MethodDescriptor.MethodType.UNARY) + .setFullMethodName(generateFullMethodName(SERVICE_NAME, "GetStudent")) + .setSampledToLocalTracing(true) + .setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller( + StudentRequest.getDefaultInstance())) + .setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller( + StudentResponse.getDefaultInstance())) + .setSchemaDescriptor(new StudentServiceMethodDescriptorSupplier("GetStudent")) + .build(); + } + } + } + return getGetStudentMethod; + } + + private static volatile io.grpc.MethodDescriptor getGetStudentStreamMethod; + + @io.grpc.stub.annotations.RpcMethod( + fullMethodName = SERVICE_NAME + '/' + "GetStudentStream", + requestType = StudentRequest.class, + responseType = StudentResponse.class, + methodType = io.grpc.MethodDescriptor.MethodType.SERVER_STREAMING) + public static io.grpc.MethodDescriptor getGetStudentStreamMethod() { + io.grpc.MethodDescriptor getGetStudentStreamMethod; + if ((getGetStudentStreamMethod = StudentServiceGrpc.getGetStudentStreamMethod) == null) { + synchronized (StudentServiceGrpc.class) { + if ((getGetStudentStreamMethod = StudentServiceGrpc.getGetStudentStreamMethod) == null) { + StudentServiceGrpc.getGetStudentStreamMethod = getGetStudentStreamMethod = + io.grpc.MethodDescriptor.newBuilder() + .setType(io.grpc.MethodDescriptor.MethodType.SERVER_STREAMING) + .setFullMethodName(generateFullMethodName(SERVICE_NAME, "GetStudentStream")) + .setSampledToLocalTracing(true) + .setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller( + StudentRequest.getDefaultInstance())) + .setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller( + StudentResponse.getDefaultInstance())) + .setSchemaDescriptor(new StudentServiceMethodDescriptorSupplier("GetStudentStream")) + .build(); + } + } + } + return getGetStudentStreamMethod; + } + + private static volatile io.grpc.MethodDescriptor getSendStudentStreamMethod; + + @io.grpc.stub.annotations.RpcMethod( + fullMethodName = SERVICE_NAME + '/' + "SendStudentStream", + requestType = StudentRequest.class, + responseType = StudentResponse.class, + methodType = io.grpc.MethodDescriptor.MethodType.CLIENT_STREAMING) + public static io.grpc.MethodDescriptor getSendStudentStreamMethod() { + io.grpc.MethodDescriptor getSendStudentStreamMethod; + if ((getSendStudentStreamMethod = StudentServiceGrpc.getSendStudentStreamMethod) == null) { + synchronized (StudentServiceGrpc.class) { + if ((getSendStudentStreamMethod = StudentServiceGrpc.getSendStudentStreamMethod) == null) { + StudentServiceGrpc.getSendStudentStreamMethod = getSendStudentStreamMethod = + io.grpc.MethodDescriptor.newBuilder() + .setType(io.grpc.MethodDescriptor.MethodType.CLIENT_STREAMING) + .setFullMethodName(generateFullMethodName(SERVICE_NAME, "SendStudentStream")) + .setSampledToLocalTracing(true) + .setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller( + StudentRequest.getDefaultInstance())) + .setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller( + StudentResponse.getDefaultInstance())) + .setSchemaDescriptor(new StudentServiceMethodDescriptorSupplier("SendStudentStream")) + .build(); + } + } + } + return getSendStudentStreamMethod; + } + + private static volatile io.grpc.MethodDescriptor getSendAndGetStudentStreamMethod; + + @io.grpc.stub.annotations.RpcMethod( + fullMethodName = SERVICE_NAME + '/' + "SendAndGetStudentStream", + requestType = StudentRequest.class, + responseType = StudentResponse.class, + methodType = io.grpc.MethodDescriptor.MethodType.BIDI_STREAMING) + public static io.grpc.MethodDescriptor getSendAndGetStudentStreamMethod() { + io.grpc.MethodDescriptor getSendAndGetStudentStreamMethod; + if ((getSendAndGetStudentStreamMethod = StudentServiceGrpc.getSendAndGetStudentStreamMethod) == null) { + synchronized (StudentServiceGrpc.class) { + if ((getSendAndGetStudentStreamMethod = StudentServiceGrpc.getSendAndGetStudentStreamMethod) == null) { + StudentServiceGrpc.getSendAndGetStudentStreamMethod = getSendAndGetStudentStreamMethod = + io.grpc.MethodDescriptor.newBuilder() + .setType(io.grpc.MethodDescriptor.MethodType.BIDI_STREAMING) + .setFullMethodName(generateFullMethodName(SERVICE_NAME, "SendAndGetStudentStream")) + .setSampledToLocalTracing(true) + .setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller( + StudentRequest.getDefaultInstance())) + .setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller( + StudentResponse.getDefaultInstance())) + .setSchemaDescriptor(new StudentServiceMethodDescriptorSupplier("SendAndGetStudentStream")) + .build(); + } + } + } + return getSendAndGetStudentStreamMethod; + } + + /** + * Creates a new async stub that supports all call types for the service + */ + public static StudentServiceStub newStub(io.grpc.Channel channel) { + io.grpc.stub.AbstractStub.StubFactory factory = + new io.grpc.stub.AbstractStub.StubFactory() { + @Override + public StudentServiceStub newStub(io.grpc.Channel channel, io.grpc.CallOptions callOptions) { + return new StudentServiceStub(channel, callOptions); + } + }; + return StudentServiceStub.newStub(factory, channel); + } + + /** + * Creates a new blocking-style stub that supports unary and streaming output calls on the service + */ + public static StudentServiceBlockingStub newBlockingStub( + io.grpc.Channel channel) { + io.grpc.stub.AbstractStub.StubFactory factory = + new io.grpc.stub.AbstractStub.StubFactory() { + @Override + public StudentServiceBlockingStub newStub(io.grpc.Channel channel, io.grpc.CallOptions callOptions) { + return new StudentServiceBlockingStub(channel, callOptions); + } + }; + return StudentServiceBlockingStub.newStub(factory, channel); + } + + /** + * Creates a new ListenableFuture-style stub that supports unary calls on the service + */ + public static StudentServiceFutureStub newFutureStub( + io.grpc.Channel channel) { + io.grpc.stub.AbstractStub.StubFactory factory = + new io.grpc.stub.AbstractStub.StubFactory() { + @Override + public StudentServiceFutureStub newStub(io.grpc.Channel channel, io.grpc.CallOptions callOptions) { + return new StudentServiceFutureStub(channel, callOptions); + } + }; + return StudentServiceFutureStub.newStub(factory, channel); + } + + /** + */ + public static abstract class StudentServiceImplBase implements io.grpc.BindableService { + + /** + */ + public void getStudent(StudentRequest request, + io.grpc.stub.StreamObserver responseObserver) { + io.grpc.stub.ServerCalls.asyncUnimplementedUnaryCall(getGetStudentMethod(), responseObserver); + } + + /** + */ + public void getStudentStream(StudentRequest request, + io.grpc.stub.StreamObserver responseObserver) { + io.grpc.stub.ServerCalls.asyncUnimplementedUnaryCall(getGetStudentStreamMethod(), responseObserver); + } + + /** + */ + public io.grpc.stub.StreamObserver sendStudentStream( + io.grpc.stub.StreamObserver responseObserver) { + return io.grpc.stub.ServerCalls.asyncUnimplementedStreamingCall(getSendStudentStreamMethod(), responseObserver); + } + + /** + */ + public io.grpc.stub.StreamObserver sendAndGetStudentStream( + io.grpc.stub.StreamObserver responseObserver) { + return io.grpc.stub.ServerCalls.asyncUnimplementedStreamingCall(getSendAndGetStudentStreamMethod(), responseObserver); + } + + @Override public final io.grpc.ServerServiceDefinition bindService() { + return io.grpc.ServerServiceDefinition.builder(getServiceDescriptor()) + .addMethod( + getGetStudentMethod(), + io.grpc.stub.ServerCalls.asyncUnaryCall( + new MethodHandlers< + StudentRequest, + StudentResponse>( + this, METHODID_GET_STUDENT))) + .addMethod( + getGetStudentStreamMethod(), + io.grpc.stub.ServerCalls.asyncServerStreamingCall( + new MethodHandlers< + StudentRequest, + StudentResponse>( + this, METHODID_GET_STUDENT_STREAM))) + .addMethod( + getSendStudentStreamMethod(), + io.grpc.stub.ServerCalls.asyncClientStreamingCall( + new MethodHandlers< + StudentRequest, + StudentResponse>( + this, METHODID_SEND_STUDENT_STREAM))) + .addMethod( + getSendAndGetStudentStreamMethod(), + io.grpc.stub.ServerCalls.asyncBidiStreamingCall( + new MethodHandlers< + StudentRequest, + StudentResponse>( + this, METHODID_SEND_AND_GET_STUDENT_STREAM))) + .build(); + } + } + + /** + */ + public static final class StudentServiceStub extends io.grpc.stub.AbstractAsyncStub { + private StudentServiceStub( + io.grpc.Channel channel, io.grpc.CallOptions callOptions) { + super(channel, callOptions); + } + + @Override + protected StudentServiceStub build( + io.grpc.Channel channel, io.grpc.CallOptions callOptions) { + return new StudentServiceStub(channel, callOptions); + } + + /** + */ + public void getStudent(StudentRequest request, + io.grpc.stub.StreamObserver responseObserver) { + io.grpc.stub.ClientCalls.asyncUnaryCall( + getChannel().newCall(getGetStudentMethod(), getCallOptions()), request, responseObserver); + } + + /** + */ + public void getStudentStream(StudentRequest request, + io.grpc.stub.StreamObserver responseObserver) { + io.grpc.stub.ClientCalls.asyncServerStreamingCall( + getChannel().newCall(getGetStudentStreamMethod(), getCallOptions()), request, responseObserver); + } + + /** + */ + public io.grpc.stub.StreamObserver sendStudentStream( + io.grpc.stub.StreamObserver responseObserver) { + return io.grpc.stub.ClientCalls.asyncClientStreamingCall( + getChannel().newCall(getSendStudentStreamMethod(), getCallOptions()), responseObserver); + } + + /** + */ + public io.grpc.stub.StreamObserver sendAndGetStudentStream( + io.grpc.stub.StreamObserver responseObserver) { + return io.grpc.stub.ClientCalls.asyncBidiStreamingCall( + getChannel().newCall(getSendAndGetStudentStreamMethod(), getCallOptions()), responseObserver); + } + } + + /** + */ + public static final class StudentServiceBlockingStub extends io.grpc.stub.AbstractBlockingStub { + private StudentServiceBlockingStub( + io.grpc.Channel channel, io.grpc.CallOptions callOptions) { + super(channel, callOptions); + } + + @Override + protected StudentServiceBlockingStub build( + io.grpc.Channel channel, io.grpc.CallOptions callOptions) { + return new StudentServiceBlockingStub(channel, callOptions); + } + + /** + */ + public StudentResponse getStudent(StudentRequest request) { + return io.grpc.stub.ClientCalls.blockingUnaryCall( + getChannel(), getGetStudentMethod(), getCallOptions(), request); + } + + /** + */ + public java.util.Iterator getStudentStream( + StudentRequest request) { + return io.grpc.stub.ClientCalls.blockingServerStreamingCall( + getChannel(), getGetStudentStreamMethod(), getCallOptions(), request); + } + } + + /** + */ + public static final class StudentServiceFutureStub extends io.grpc.stub.AbstractFutureStub { + private StudentServiceFutureStub( + io.grpc.Channel channel, io.grpc.CallOptions callOptions) { + super(channel, callOptions); + } + + @Override + protected StudentServiceFutureStub build( + io.grpc.Channel channel, io.grpc.CallOptions callOptions) { + return new StudentServiceFutureStub(channel, callOptions); + } + + /** + */ + public com.google.common.util.concurrent.ListenableFuture getStudent( + StudentRequest request) { + return io.grpc.stub.ClientCalls.futureUnaryCall( + getChannel().newCall(getGetStudentMethod(), getCallOptions()), request); + } + } + + private static final int METHODID_GET_STUDENT = 0; + private static final int METHODID_GET_STUDENT_STREAM = 1; + private static final int METHODID_SEND_STUDENT_STREAM = 2; + private static final int METHODID_SEND_AND_GET_STUDENT_STREAM = 3; + + private static final class MethodHandlers implements + io.grpc.stub.ServerCalls.UnaryMethod, + io.grpc.stub.ServerCalls.ServerStreamingMethod, + io.grpc.stub.ServerCalls.ClientStreamingMethod, + io.grpc.stub.ServerCalls.BidiStreamingMethod { + private final StudentServiceImplBase serviceImpl; + private final int methodId; + + MethodHandlers(StudentServiceImplBase serviceImpl, int methodId) { + this.serviceImpl = serviceImpl; + this.methodId = methodId; + } + + @Override + @SuppressWarnings("unchecked") + public void invoke(Req request, io.grpc.stub.StreamObserver responseObserver) { + switch (methodId) { + case METHODID_GET_STUDENT: + serviceImpl.getStudent((StudentRequest) request, + (io.grpc.stub.StreamObserver) responseObserver); + break; + case METHODID_GET_STUDENT_STREAM: + serviceImpl.getStudentStream((StudentRequest) request, + (io.grpc.stub.StreamObserver) responseObserver); + break; + default: + throw new AssertionError(); + } + } + + @Override + @SuppressWarnings("unchecked") + public io.grpc.stub.StreamObserver invoke( + io.grpc.stub.StreamObserver responseObserver) { + switch (methodId) { + case METHODID_SEND_STUDENT_STREAM: + return (io.grpc.stub.StreamObserver) serviceImpl.sendStudentStream( + (io.grpc.stub.StreamObserver) responseObserver); + case METHODID_SEND_AND_GET_STUDENT_STREAM: + return (io.grpc.stub.StreamObserver) serviceImpl.sendAndGetStudentStream( + (io.grpc.stub.StreamObserver) responseObserver); + default: + throw new AssertionError(); + } + } + } + + private static abstract class StudentServiceBaseDescriptorSupplier + implements io.grpc.protobuf.ProtoFileDescriptorSupplier, io.grpc.protobuf.ProtoServiceDescriptorSupplier { + StudentServiceBaseDescriptorSupplier() {} + + @Override + public com.google.protobuf.Descriptors.FileDescriptor getFileDescriptor() { + return Student.getDescriptor(); + } + + @Override + public com.google.protobuf.Descriptors.ServiceDescriptor getServiceDescriptor() { + return getFileDescriptor().findServiceByName("StudentService"); + } + } + + private static final class StudentServiceFileDescriptorSupplier + extends StudentServiceBaseDescriptorSupplier { + StudentServiceFileDescriptorSupplier() {} + } + + private static final class StudentServiceMethodDescriptorSupplier + extends StudentServiceBaseDescriptorSupplier + implements io.grpc.protobuf.ProtoMethodDescriptorSupplier { + private final String methodName; + + StudentServiceMethodDescriptorSupplier(String methodName) { + this.methodName = methodName; + } + + @Override + public com.google.protobuf.Descriptors.MethodDescriptor getMethodDescriptor() { + return getServiceDescriptor().findMethodByName(methodName); + } + } + + private static volatile io.grpc.ServiceDescriptor serviceDescriptor; + + public static io.grpc.ServiceDescriptor getServiceDescriptor() { + io.grpc.ServiceDescriptor result = serviceDescriptor; + if (result == null) { + synchronized (StudentServiceGrpc.class) { + result = serviceDescriptor; + if (result == null) { + serviceDescriptor = result = io.grpc.ServiceDescriptor.newBuilder(SERVICE_NAME) + .setSchemaDescriptor(new StudentServiceFileDescriptorSupplier()) + .addMethod(getGetStudentMethod()) + .addMethod(getGetStudentStreamMethod()) + .addMethod(getSendStudentStreamMethod()) + .addMethod(getSendAndGetStudentStreamMethod()) + .build(); + } + } + } + return result; + } +} diff --git a/test/cucumber-tests/src/test/resources/tests/api/GRPC.feature b/test/cucumber-tests/src/test/resources/tests/api/GRPC.feature index 66fd7e748..1821732b4 100644 --- a/test/cucumber-tests/src/test/resources/tests/api/GRPC.feature +++ b/test/cucumber-tests/src/test/resources/tests/api/GRPC.feature @@ -4,3 +4,32 @@ Feature: Generating APK conf for gRPC API When I use the definition file "artifacts/definitions/student.proto" in resources And generate the APK conf file for a "gRPC" API Then the response status code should be 200 + + Scenario: Deploying APK conf using a valid gRPC API definition + Given The system is ready + And I have a valid subscription + When I use the APK Conf file "artifacts/apk-confs/grpc/grpc.apk-conf" + And the definition file "artifacts/definitions/student.proto" + And make the API deployment request + Then the response status code should be 200 + Then I set headers + | Authorization | bearer ${accessToken} | + And I make grpc request + And I eventually receive 200 response code, not accepting + | 429 | + | 500 | + And the response body should contain "\"name\":\"string\"" + Then I set headers + | Authorization | bearer ${accessToken} | + And I send "POST" request to "https://default.gw.wso2.com:9095/grpc" + And I eventually receive 200 response code, not accepting + | 429 | + | 500 | + And the response body should contain "\"name\":\"string\"" + + Scenario: Undeploy API + Given The system is ready + And I have a valid subscription + When I undeploy the API whose ID is "grpc-basic-api" + Then the response status code should be 202 + From 80be9cccda584e94931fbf9b3dff3a16e4ca9103 Mon Sep 17 00:00:00 2001 From: DinethH Date: Thu, 25 Apr 2024 14:25:08 +0530 Subject: [PATCH 11/29] add more code for cucumber test --- test/cucumber-tests/build.gradle | 8 +-- .../apk/integration/api/SharedContext.java | 1 - .../utils/JWTClientInterceptor.java | 28 +++++++++++ .../clients/SimpleGRPCStudentClient.java | 50 ++++++++++++++----- .../clients/studentGrpcClient/Student.java | 48 ++++++++++-------- .../studentGrpcClient/StudentRequest.java | 28 +++++------ .../StudentRequestOrBuilder.java | 2 +- .../studentGrpcClient/StudentResponse.java | 28 +++++------ .../StudentResponseOrBuilder.java | 2 +- .../studentGrpcClient/StudentServiceGrpc.java | 16 +++--- 10 files changed, 134 insertions(+), 77 deletions(-) create mode 100644 test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/JWTClientInterceptor.java diff --git a/test/cucumber-tests/build.gradle b/test/cucumber-tests/build.gradle index c589336f9..decdfb1ca 100644 --- a/test/cucumber-tests/build.gradle +++ b/test/cucumber-tests/build.gradle @@ -35,9 +35,11 @@ dependencies { testImplementation 'io.cucumber:cucumber-testng:7.13.0' testImplementation 'commons-io:commons-io:2.13.0' testImplementation 'com.nimbusds:nimbus-jose-jwt:9.31' - implementation 'io.grpc:grpc-netty-shaded:1.57.0' - implementation 'io.grpc:grpc-protobuf:1.57.0' - implementation 'io.grpc:grpc-stub:1.57.0' + implementation 'javax.annotation:javax.annotation-api:1.3.2' + implementation 'io.grpc:grpc-netty:1.48.0' + implementation 'io.grpc:grpc-protobuf:1.48.0' + implementation 'io.grpc:grpc-stub:1.48.0' + implementation 'io.grpc:grpc-auth:1.48.0' } test { diff --git a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/SharedContext.java b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/SharedContext.java index 822ff6335..6ba56a673 100644 --- a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/SharedContext.java +++ b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/SharedContext.java @@ -25,7 +25,6 @@ import java.security.KeyManagementException; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; -import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.Map; diff --git a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/JWTClientInterceptor.java b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/JWTClientInterceptor.java new file mode 100644 index 000000000..fe01777ea --- /dev/null +++ b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/JWTClientInterceptor.java @@ -0,0 +1,28 @@ +package org.wso2.apk.integration.utils; +import io.grpc.*; + +public class JWTClientInterceptor implements ClientInterceptor { + + private String jwtToken; + + public JWTClientInterceptor(String jwtToken) { + this.jwtToken = jwtToken; + } + + @Override + public ClientCall interceptCall( + MethodDescriptor method, CallOptions callOptions, Channel next) { + return new ForwardingClientCall.SimpleForwardingClientCall( + next.newCall(method, callOptions)) { + + @Override + public void start(Listener responseListener, Metadata headers) { + headers.put( + Metadata.Key.of("Authorization", Metadata.ASCII_STRING_MARSHALLER), + "Bearer " + jwtToken); + super.start(responseListener, headers); + } + }; + } +} + diff --git a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/SimpleGRPCStudentClient.java b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/SimpleGRPCStudentClient.java index 0ab586bbd..c3533fa3a 100644 --- a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/SimpleGRPCStudentClient.java +++ b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/SimpleGRPCStudentClient.java @@ -1,13 +1,19 @@ package org.wso2.apk.integration.utils.clients; +import io.grpc.StatusRuntimeException; +import io.grpc.netty.GrpcSslContexts; +import io.grpc.netty.NettyChannelBuilder; +import io.netty.handler.ssl.SslContext; +import io.netty.handler.ssl.util.InsecureTrustManagerFactory; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import io.grpc.ManagedChannel; -import io.grpc.ManagedChannelBuilder; +import org.wso2.apk.integration.utils.JWTClientInterceptor; import org.wso2.apk.integration.utils.clients.studentGrpcClient.StudentRequest; import org.wso2.apk.integration.utils.clients.studentGrpcClient.StudentResponse; import org.wso2.apk.integration.utils.clients.studentGrpcClient.StudentServiceGrpc; +import javax.net.ssl.SSLException; import java.security.KeyManagementException; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; @@ -16,23 +22,41 @@ public class SimpleGRPCStudentClient { protected Log log = LogFactory.getLog(SimpleGRPCStudentClient.class); private static final int EVENTUAL_SUCCESS_RESPONSE_TIMEOUT_IN_SECONDS = 10; - private ManagedChannel managedChannel; - public SimpleGRPCStudentClient() throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException { - this.managedChannel = ManagedChannelBuilder.forAddress("default.gw.wso2.com", 9095).usePlaintext().build(); - log.info("ManagedChannel created"); + this.GetStudent(); } public StudentResponse GetStudent() { - //Create a blocking stub for the StudentService - StudentServiceGrpc.StudentServiceBlockingStub blockingStub = StudentServiceGrpc.newBlockingStub(managedChannel); - - // Make a synchronous gRPC call to get student details for ID 1 - StudentResponse studentResponse = blockingStub.getStudent(StudentRequest.newBuilder().setId(1).build()); + try { + //sleep for 5 seconds + try { + log.info("Sleeping for 5 seconds"); + Thread.sleep(5000); + log.info("Woke up after 5 seconds"); + } catch (InterruptedException e) { + log.error("Thread sleep interrupted"); + } + SslContext sslContext = GrpcSslContexts.forClient() + .trustManager(InsecureTrustManagerFactory.INSTANCE) + .build(); - // Log the response received from the gRPC server - log.info("response = " + studentResponse.getName() + " " + studentResponse.getAge()); - return studentResponse; + ManagedChannel managedChannel = NettyChannelBuilder.forAddress("default.gw.wso2.com", 9095) + .sslContext(sslContext) + .intercept(new JWTClientInterceptor("eyJhbGciOiJSUzI1NiIsICJ0eXAiOiJKV1QiLCAia2lkIjoiZ2F0ZXdheV9jZXJ0aWZpY2F0ZV9hbGlhcyJ9.eyJpc3MiOiJodHRwczovL2lkcC5hbS53c28yLmNvbS90b2tlbiIsICJzdWIiOiI0NWYxYzVjOC1hOTJlLTExZWQtYWZhMS0wMjQyYWMxMjAwMDIiLCAiYXVkIjoiYXVkMSIsICJleHAiOjE3MTQwMzc1NDMsICJuYmYiOjE3MTQwMzM5NDMsICJpYXQiOjE3MTQwMzM5NDMsICJqdGkiOiIwMWVmMDJkZS01NjhmLTE0NTgtYTJlMS0wYTk3N2NmMTA5MGMiLCAiY2xpZW50SWQiOiI0NWYxYzVjOC1hOTJlLTExZWQtYWZhMS0wMjQyYWMxMjAwMDIiLCAic2NvcGUiOiJhcGs6YXBpX2NyZWF0ZSJ9.NbvlpbNL0Q3Op7I36nWSMb9R6zCtUeI7las0vOYMNQxMgLrTOBLkXmd9EfSg46fqOD7a9YqoGmKgn5UhXcQSFhtUwAKvbYDvnTyYfT6X3fBqFWl59xt74yJ8f6cSRSBb88Is0qDCWoTVgM-5eTqb93uU8KC0LG1YwU3OoxoiuM1_ix1qbugb-X7gYVvfqttnHl_0e-4jgNN5YLQl8xo8DBs9D-yDWDkDQpj_NonCY1AXqlrynmKbf7kRPR3abHJiF07BQoXJzOXUv4lyHJ1K6DnHj9l2w-KNAWDTQ-kffkVtkQ3hNjbl0Q5ieHsVXLQ9HB1AkOGrho6W8CIO4qlb9A")) // replace "your-jwt-token" with your actual JWT token + .build(); + //Create a blocking stub for the StudentService + StudentServiceGrpc.StudentServiceBlockingStub blockingStub = StudentServiceGrpc.newBlockingStub(managedChannel); + // Make a synchronous gRPC call to get student details for ID 1 + StudentResponse studentResponse = blockingStub.getStudent(StudentRequest.newBuilder().setId(1).build()); + // Log the response received from the gRPC server + log.info("response = " + studentResponse.getName() + " " + studentResponse.getAge()); + return studentResponse; + } catch (StatusRuntimeException e) { + log.error("Failed to retrieve student: " + e.getStatus().getDescription()); + throw e; + } catch (SSLException e) { + throw new RuntimeException(e); + } } diff --git a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/studentGrpcClient/Student.java b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/studentGrpcClient/Student.java index 30ef19bd2..8ec85fec1 100644 --- a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/studentGrpcClient/Student.java +++ b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/studentGrpcClient/Student.java @@ -15,15 +15,15 @@ public static void registerAllExtensions( (com.google.protobuf.ExtensionRegistryLite) registry); } static final com.google.protobuf.Descriptors.Descriptor - internal_static_student_StudentRequest_descriptor; + internal_static_dineth_grpc_v1_student_StudentRequest_descriptor; static final com.google.protobuf.GeneratedMessageV3.FieldAccessorTable - internal_static_student_StudentRequest_fieldAccessorTable; + internal_static_dineth_grpc_v1_student_StudentRequest_fieldAccessorTable; static final com.google.protobuf.Descriptors.Descriptor - internal_static_student_StudentResponse_descriptor; + internal_static_dineth_grpc_v1_student_StudentResponse_descriptor; static final com.google.protobuf.GeneratedMessageV3.FieldAccessorTable - internal_static_student_StudentResponse_fieldAccessorTable; + internal_static_dineth_grpc_v1_student_StudentResponse_fieldAccessorTable; public static com.google.protobuf.Descriptors.FileDescriptor getDescriptor() { @@ -33,34 +33,38 @@ public static void registerAllExtensions( descriptor; static { String[] descriptorData = { - "\n\rstudent.proto\022\007student\"\034\n\016StudentReque" + - "st\022\n\n\002id\030\003 \001(\005\",\n\017StudentResponse\022\014\n\004nam" + - "e\030\001 \001(\t\022\013\n\003age\030\002 \001(\0052\276\002\n\016StudentService\022" + - "A\n\nGetStudent\022\027.student.StudentRequest\032\030" + - ".student.StudentResponse\"\000\022I\n\020GetStudent" + - "Stream\022\027.student.StudentRequest\032\030.studen" + - "t.StudentResponse\"\0000\001\022J\n\021SendStudentStre" + - "am\022\027.student.StudentRequest\032\030.student.St" + - "udentResponse\"\000(\001\022R\n\027SendAndGetStudentSt" + - "ream\022\027.student.StudentRequest\032\030.student." + - "StudentResponse\"\000(\0010\001B\017\n\013org.exampleP\001b\006" + - "proto3" + "\n\rstudent.proto\022\026dineth.grpc.v1.student\"" + + "\034\n\016StudentRequest\022\n\n\002id\030\003 \001(\005\",\n\017Student" + + "Response\022\014\n\004name\030\001 \001(\t\022\013\n\003age\030\002 \001(\0052\266\003\n\016" + + "StudentService\022_\n\nGetStudent\022&.dineth.gr" + + "pc.v1.student.StudentRequest\032\'.dineth.gr" + + "pc.v1.student.StudentResponse\"\000\022g\n\020GetSt" + + "udentStream\022&.dineth.grpc.v1.student.Stu" + + "dentRequest\032\'.dineth.grpc.v1.student.Stu" + + "dentResponse\"\0000\001\022h\n\021SendStudentStream\022&." + + "dineth.grpc.v1.student.StudentRequest\032\'." + + "dineth.grpc.v1.student.StudentResponse\"\000" + + "(\001\022p\n\027SendAndGetStudentStream\022&.dineth.g" + + "rpc.v1.student.StudentRequest\032\'.dineth.g" + + "rpc.v1.student.StudentResponse\"\000(\0010\001B<\n8" + + "org.wso2.apk.integration.utils.clients.s" + + "tudentGrpcClientP\001b\006proto3" }; descriptor = com.google.protobuf.Descriptors.FileDescriptor .internalBuildGeneratedFileFrom(descriptorData, new com.google.protobuf.Descriptors.FileDescriptor[] { }); - internal_static_student_StudentRequest_descriptor = + internal_static_dineth_grpc_v1_student_StudentRequest_descriptor = getDescriptor().getMessageTypes().get(0); - internal_static_student_StudentRequest_fieldAccessorTable = new + internal_static_dineth_grpc_v1_student_StudentRequest_fieldAccessorTable = new com.google.protobuf.GeneratedMessageV3.FieldAccessorTable( - internal_static_student_StudentRequest_descriptor, + internal_static_dineth_grpc_v1_student_StudentRequest_descriptor, new String[] { "Id", }); - internal_static_student_StudentResponse_descriptor = + internal_static_dineth_grpc_v1_student_StudentResponse_descriptor = getDescriptor().getMessageTypes().get(1); - internal_static_student_StudentResponse_fieldAccessorTable = new + internal_static_dineth_grpc_v1_student_StudentResponse_fieldAccessorTable = new com.google.protobuf.GeneratedMessageV3.FieldAccessorTable( - internal_static_student_StudentResponse_descriptor, + internal_static_dineth_grpc_v1_student_StudentResponse_descriptor, new String[] { "Name", "Age", }); } diff --git a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/studentGrpcClient/StudentRequest.java b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/studentGrpcClient/StudentRequest.java index a79d2d9d9..c487c6567 100644 --- a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/studentGrpcClient/StudentRequest.java +++ b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/studentGrpcClient/StudentRequest.java @@ -4,11 +4,11 @@ package org.wso2.apk.integration.utils.clients.studentGrpcClient; /** - * Protobuf type {@code student.StudentRequest} + * Protobuf type {@code dineth.grpc.v1.student.StudentRequest} */ public final class StudentRequest extends com.google.protobuf.GeneratedMessageV3 implements - // @@protoc_insertion_point(message_implements:student.StudentRequest) + // @@protoc_insertion_point(message_implements:dineth.grpc.v1.student.StudentRequest) StudentRequestOrBuilder { private static final long serialVersionUID = 0L; // Use StudentRequest.newBuilder() to construct. @@ -74,15 +74,15 @@ private StudentRequest( } public static final com.google.protobuf.Descriptors.Descriptor getDescriptor() { - return Student.internal_static_student_StudentRequest_descriptor; + return Student.internal_static_dineth_grpc_v1_student_StudentRequest_descriptor; } @Override protected FieldAccessorTable internalGetFieldAccessorTable() { - return Student.internal_static_student_StudentRequest_fieldAccessorTable + return Student.internal_static_dineth_grpc_v1_student_StudentRequest_fieldAccessorTable .ensureFieldAccessorsInitialized( - StudentRequest.class, StudentRequest.Builder.class); + StudentRequest.class, Builder.class); } public static final int ID_FIELD_NUMBER = 3; @@ -252,26 +252,26 @@ protected Builder newBuilderForType( return builder; } /** - * Protobuf type {@code student.StudentRequest} + * Protobuf type {@code dineth.grpc.v1.student.StudentRequest} */ public static final class Builder extends com.google.protobuf.GeneratedMessageV3.Builder implements - // @@protoc_insertion_point(builder_implements:student.StudentRequest) + // @@protoc_insertion_point(builder_implements:dineth.grpc.v1.student.StudentRequest) StudentRequestOrBuilder { public static final com.google.protobuf.Descriptors.Descriptor getDescriptor() { - return Student.internal_static_student_StudentRequest_descriptor; + return Student.internal_static_dineth_grpc_v1_student_StudentRequest_descriptor; } @Override protected FieldAccessorTable internalGetFieldAccessorTable() { - return Student.internal_static_student_StudentRequest_fieldAccessorTable + return Student.internal_static_dineth_grpc_v1_student_StudentRequest_fieldAccessorTable .ensureFieldAccessorsInitialized( - StudentRequest.class, StudentRequest.Builder.class); + StudentRequest.class, Builder.class); } - // Construct using StudentRequest.newBuilder() + // Construct using org.wso2.apk.integration.utils.clients.studentGrpcClient.StudentRequest.newBuilder() private Builder() { maybeForceBuilderInitialization(); } @@ -297,7 +297,7 @@ public Builder clear() { @Override public com.google.protobuf.Descriptors.Descriptor getDescriptorForType() { - return Student.internal_static_student_StudentRequest_descriptor; + return Student.internal_static_dineth_grpc_v1_student_StudentRequest_descriptor; } @Override @@ -441,10 +441,10 @@ public final Builder mergeUnknownFields( } - // @@protoc_insertion_point(builder_scope:student.StudentRequest) + // @@protoc_insertion_point(builder_scope:dineth.grpc.v1.student.StudentRequest) } - // @@protoc_insertion_point(class_scope:student.StudentRequest) + // @@protoc_insertion_point(class_scope:dineth.grpc.v1.student.StudentRequest) private static final StudentRequest DEFAULT_INSTANCE; static { DEFAULT_INSTANCE = new StudentRequest(); diff --git a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/studentGrpcClient/StudentRequestOrBuilder.java b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/studentGrpcClient/StudentRequestOrBuilder.java index 266630f4c..62c36bc77 100644 --- a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/studentGrpcClient/StudentRequestOrBuilder.java +++ b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/studentGrpcClient/StudentRequestOrBuilder.java @@ -4,7 +4,7 @@ package org.wso2.apk.integration.utils.clients.studentGrpcClient; public interface StudentRequestOrBuilder extends - // @@protoc_insertion_point(interface_extends:student.StudentRequest) + // @@protoc_insertion_point(interface_extends:dineth.grpc.v1.student.StudentRequest) com.google.protobuf.MessageOrBuilder { /** diff --git a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/studentGrpcClient/StudentResponse.java b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/studentGrpcClient/StudentResponse.java index 906538a20..33e343eba 100644 --- a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/studentGrpcClient/StudentResponse.java +++ b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/studentGrpcClient/StudentResponse.java @@ -4,11 +4,11 @@ package org.wso2.apk.integration.utils.clients.studentGrpcClient; /** - * Protobuf type {@code student.StudentResponse} + * Protobuf type {@code dineth.grpc.v1.student.StudentResponse} */ public final class StudentResponse extends com.google.protobuf.GeneratedMessageV3 implements - // @@protoc_insertion_point(message_implements:student.StudentResponse) + // @@protoc_insertion_point(message_implements:dineth.grpc.v1.student.StudentResponse) StudentResponseOrBuilder { private static final long serialVersionUID = 0L; // Use StudentResponse.newBuilder() to construct. @@ -81,15 +81,15 @@ private StudentResponse( } public static final com.google.protobuf.Descriptors.Descriptor getDescriptor() { - return Student.internal_static_student_StudentResponse_descriptor; + return Student.internal_static_dineth_grpc_v1_student_StudentResponse_descriptor; } @Override protected FieldAccessorTable internalGetFieldAccessorTable() { - return Student.internal_static_student_StudentResponse_fieldAccessorTable + return Student.internal_static_dineth_grpc_v1_student_StudentResponse_fieldAccessorTable .ensureFieldAccessorsInitialized( - StudentResponse.class, StudentResponse.Builder.class); + StudentResponse.class, Builder.class); } public static final int NAME_FIELD_NUMBER = 1; @@ -307,26 +307,26 @@ protected Builder newBuilderForType( return builder; } /** - * Protobuf type {@code student.StudentResponse} + * Protobuf type {@code dineth.grpc.v1.student.StudentResponse} */ public static final class Builder extends com.google.protobuf.GeneratedMessageV3.Builder implements - // @@protoc_insertion_point(builder_implements:student.StudentResponse) + // @@protoc_insertion_point(builder_implements:dineth.grpc.v1.student.StudentResponse) StudentResponseOrBuilder { public static final com.google.protobuf.Descriptors.Descriptor getDescriptor() { - return Student.internal_static_student_StudentResponse_descriptor; + return Student.internal_static_dineth_grpc_v1_student_StudentResponse_descriptor; } @Override protected FieldAccessorTable internalGetFieldAccessorTable() { - return Student.internal_static_student_StudentResponse_fieldAccessorTable + return Student.internal_static_dineth_grpc_v1_student_StudentResponse_fieldAccessorTable .ensureFieldAccessorsInitialized( - StudentResponse.class, StudentResponse.Builder.class); + StudentResponse.class, Builder.class); } - // Construct using StudentResponse.newBuilder() + // Construct using org.wso2.apk.integration.utils.clients.studentGrpcClient.StudentResponse.newBuilder() private Builder() { maybeForceBuilderInitialization(); } @@ -354,7 +354,7 @@ public Builder clear() { @Override public com.google.protobuf.Descriptors.Descriptor getDescriptorForType() { - return Student.internal_static_student_StudentResponse_descriptor; + return Student.internal_static_dineth_grpc_v1_student_StudentResponse_descriptor; } @Override @@ -579,10 +579,10 @@ public final Builder mergeUnknownFields( } - // @@protoc_insertion_point(builder_scope:student.StudentResponse) + // @@protoc_insertion_point(builder_scope:dineth.grpc.v1.student.StudentResponse) } - // @@protoc_insertion_point(class_scope:student.StudentResponse) + // @@protoc_insertion_point(class_scope:dineth.grpc.v1.student.StudentResponse) private static final StudentResponse DEFAULT_INSTANCE; static { DEFAULT_INSTANCE = new StudentResponse(); diff --git a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/studentGrpcClient/StudentResponseOrBuilder.java b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/studentGrpcClient/StudentResponseOrBuilder.java index 4b16af775..08946635c 100644 --- a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/studentGrpcClient/StudentResponseOrBuilder.java +++ b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/studentGrpcClient/StudentResponseOrBuilder.java @@ -4,7 +4,7 @@ package org.wso2.apk.integration.utils.clients.studentGrpcClient; public interface StudentResponseOrBuilder extends - // @@protoc_insertion_point(interface_extends:student.StudentResponse) + // @@protoc_insertion_point(interface_extends:dineth.grpc.v1.student.StudentResponse) com.google.protobuf.MessageOrBuilder { /** diff --git a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/studentGrpcClient/StudentServiceGrpc.java b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/studentGrpcClient/StudentServiceGrpc.java index 60a066612..b9b158c0f 100644 --- a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/studentGrpcClient/StudentServiceGrpc.java +++ b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/studentGrpcClient/StudentServiceGrpc.java @@ -4,14 +4,14 @@ /** */ -//@javax.annotation.Generated( -// value = "by gRPC proto compiler (version 1.39.0)", -// comments = "Source: student.proto") +@javax.annotation.Generated( + value = "by gRPC proto compiler (version 1.39.0)", + comments = "Source: student.proto") public final class StudentServiceGrpc { private StudentServiceGrpc() {} - public static final String SERVICE_NAME = "student.StudentService"; + public static final String SERVICE_NAME = "dineth.grpc.api.v1.student.StudentService"; // Static method descriptors that strictly reflect the proto. private static volatile io.grpc.MethodDescriptor responseObserver) { + io.grpc.stub.StreamObserver responseObserver) { io.grpc.stub.ServerCalls.asyncUnimplementedUnaryCall(getGetStudentMethod(), responseObserver); } /** */ public void getStudentStream(StudentRequest request, - io.grpc.stub.StreamObserver responseObserver) { + io.grpc.stub.StreamObserver responseObserver) { io.grpc.stub.ServerCalls.asyncUnimplementedUnaryCall(getGetStudentStreamMethod(), responseObserver); } @@ -265,7 +265,7 @@ protected StudentServiceStub build( /** */ public void getStudent(StudentRequest request, - io.grpc.stub.StreamObserver responseObserver) { + io.grpc.stub.StreamObserver responseObserver) { io.grpc.stub.ClientCalls.asyncUnaryCall( getChannel().newCall(getGetStudentMethod(), getCallOptions()), request, responseObserver); } @@ -273,7 +273,7 @@ public void getStudent(StudentRequest request, /** */ public void getStudentStream(StudentRequest request, - io.grpc.stub.StreamObserver responseObserver) { + io.grpc.stub.StreamObserver responseObserver) { io.grpc.stub.ClientCalls.asyncServerStreamingCall( getChannel().newCall(getGetStudentStreamMethod(), getCallOptions()), request, responseObserver); } From 781793ce3ec4ed76057c07a23c6af0c4eb895dd7 Mon Sep 17 00:00:00 2001 From: DinethH Date: Thu, 25 Apr 2024 16:11:47 +0530 Subject: [PATCH 12/29] cucumber test for grpc works --- .../wso2/apk/integration/api/BaseSteps.java | 22 ++++++-- .../apk/integration/api/SharedContext.java | 6 --- .../clients/SimpleGRPCStudentClient.java | 50 +++++++++++-------- .../src/test/resources/tests/api/GRPC.feature | 13 ++--- 4 files changed, 51 insertions(+), 40 deletions(-) diff --git a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/BaseSteps.java b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/BaseSteps.java index 053a0ab0e..e3c713805 100644 --- a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/BaseSteps.java +++ b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/BaseSteps.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, WSO2 LLC (http://www.wso2.com). + * Copyright (c) 2024, WSO2 LLC (http://www.wso2.com). * * WSO2 LLC licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except @@ -93,6 +93,14 @@ public void systemIsReady() { } + @Then("the student response body should contain name: {string} age: {int}") + public void theStudentResponseBodyShouldContainNameAndAge(String arg0, int arg1) { + int age = sharedContext.getStudentResponse().getAge(); + String name = sharedContext.getStudentResponse().getName(); + Assert.assertEquals(name, arg0); + Assert.assertEquals(age, arg1); + } + @Then("the response body should contain {string}") public void theResponseBodyShouldContain(String expectedText) throws IOException { Assert.assertTrue(sharedContext.getResponseBody().contains(expectedText), "Actual response body: " + sharedContext.getResponseBody()); @@ -140,12 +148,18 @@ public void sendHttpRequest(String httpMethod, String url, String body) throws I } } - @Then("I make grpc request") - public void sendGrpcRequest() throws IOException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException { - SimpleGRPCStudentClient grpcStudentClient = new SimpleGRPCStudentClient(); + @Then("I make grpc request to GetStudent to {string} with port {int}") + public void GetStudent(String arg0, int arg1) { + SimpleGRPCStudentClient grpcStudentClient = new SimpleGRPCStudentClient(arg0,arg1); sharedContext.setStudentResponse(grpcStudentClient.GetStudent()); } + @Then("I make grpc request GetStudent with token to {string} with port {int}") + public void GetStudentWithToken(String arg0, int arg1 ) { + SimpleGRPCStudentClient grpcStudentClient = new SimpleGRPCStudentClient(arg0,arg1); + sharedContext.setStudentResponse(grpcStudentClient.GetStudent(sharedContext.getAccessToken())); + } + // It will send request using a new thread and forget about the response @Then("I send {string} async request to {string} with body {string}") public void sendAsyncHttpRequest(String httpMethod, String url, String body) throws IOException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException { diff --git a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/SharedContext.java b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/SharedContext.java index 6ba56a673..cca88b764 100644 --- a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/SharedContext.java +++ b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/SharedContext.java @@ -46,12 +46,6 @@ public SimpleHTTPClient getHttpClient() throws NoSuchAlgorithmException, KeyStor } return httpClient; } - public SimpleGRPCStudentClient getGrpcStudentClient()throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException { - if (grpcStudentClient == null) { - grpcStudentClient = new SimpleGRPCStudentClient(); - } - return grpcStudentClient; - } public String getAccessToken() { diff --git a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/SimpleGRPCStudentClient.java b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/SimpleGRPCStudentClient.java index c3533fa3a..b028a70dc 100644 --- a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/SimpleGRPCStudentClient.java +++ b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/SimpleGRPCStudentClient.java @@ -14,43 +14,51 @@ import org.wso2.apk.integration.utils.clients.studentGrpcClient.StudentServiceGrpc; import javax.net.ssl.SSLException; -import java.security.KeyManagementException; -import java.security.KeyStoreException; -import java.security.NoSuchAlgorithmException; + public class SimpleGRPCStudentClient { protected Log log = LogFactory.getLog(SimpleGRPCStudentClient.class); private static final int EVENTUAL_SUCCESS_RESPONSE_TIMEOUT_IN_SECONDS = 10; + private final String host; + private final int port; - public SimpleGRPCStudentClient() throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException { - this.GetStudent(); + public SimpleGRPCStudentClient(String host, int port) { + this.host = host; + this.port = port; } public StudentResponse GetStudent() { try { - //sleep for 5 seconds - try { - log.info("Sleeping for 5 seconds"); - Thread.sleep(5000); - log.info("Woke up after 5 seconds"); - } catch (InterruptedException e) { - log.error("Thread sleep interrupted"); - } SslContext sslContext = GrpcSslContexts.forClient() .trustManager(InsecureTrustManagerFactory.INSTANCE) .build(); - ManagedChannel managedChannel = NettyChannelBuilder.forAddress("default.gw.wso2.com", 9095) + ManagedChannel managedChannel = NettyChannelBuilder.forAddress(host, port) + .sslContext(sslContext) + .build(); + StudentServiceGrpc.StudentServiceBlockingStub blockingStub = StudentServiceGrpc.newBlockingStub(managedChannel); + + return blockingStub.getStudent(StudentRequest.newBuilder().setId(1).build()); + } catch (StatusRuntimeException e) { + log.error("Failed to retrieve student: " + e.getStatus().getDescription()); + throw e; + } catch (SSLException e) { + throw new RuntimeException(e); + } + } + public StudentResponse GetStudent(String token) { + try { + SslContext sslContext = GrpcSslContexts.forClient() + .trustManager(InsecureTrustManagerFactory.INSTANCE) + .build(); + + ManagedChannel managedChannel = NettyChannelBuilder.forAddress(host, port) .sslContext(sslContext) - .intercept(new JWTClientInterceptor("eyJhbGciOiJSUzI1NiIsICJ0eXAiOiJKV1QiLCAia2lkIjoiZ2F0ZXdheV9jZXJ0aWZpY2F0ZV9hbGlhcyJ9.eyJpc3MiOiJodHRwczovL2lkcC5hbS53c28yLmNvbS90b2tlbiIsICJzdWIiOiI0NWYxYzVjOC1hOTJlLTExZWQtYWZhMS0wMjQyYWMxMjAwMDIiLCAiYXVkIjoiYXVkMSIsICJleHAiOjE3MTQwMzc1NDMsICJuYmYiOjE3MTQwMzM5NDMsICJpYXQiOjE3MTQwMzM5NDMsICJqdGkiOiIwMWVmMDJkZS01NjhmLTE0NTgtYTJlMS0wYTk3N2NmMTA5MGMiLCAiY2xpZW50SWQiOiI0NWYxYzVjOC1hOTJlLTExZWQtYWZhMS0wMjQyYWMxMjAwMDIiLCAic2NvcGUiOiJhcGs6YXBpX2NyZWF0ZSJ9.NbvlpbNL0Q3Op7I36nWSMb9R6zCtUeI7las0vOYMNQxMgLrTOBLkXmd9EfSg46fqOD7a9YqoGmKgn5UhXcQSFhtUwAKvbYDvnTyYfT6X3fBqFWl59xt74yJ8f6cSRSBb88Is0qDCWoTVgM-5eTqb93uU8KC0LG1YwU3OoxoiuM1_ix1qbugb-X7gYVvfqttnHl_0e-4jgNN5YLQl8xo8DBs9D-yDWDkDQpj_NonCY1AXqlrynmKbf7kRPR3abHJiF07BQoXJzOXUv4lyHJ1K6DnHj9l2w-KNAWDTQ-kffkVtkQ3hNjbl0Q5ieHsVXLQ9HB1AkOGrho6W8CIO4qlb9A")) // replace "your-jwt-token" with your actual JWT token + .intercept(new JWTClientInterceptor(token)) // replace "your-jwt-token" with your actual JWT token .build(); - //Create a blocking stub for the StudentService StudentServiceGrpc.StudentServiceBlockingStub blockingStub = StudentServiceGrpc.newBlockingStub(managedChannel); - // Make a synchronous gRPC call to get student details for ID 1 - StudentResponse studentResponse = blockingStub.getStudent(StudentRequest.newBuilder().setId(1).build()); - // Log the response received from the gRPC server - log.info("response = " + studentResponse.getName() + " " + studentResponse.getAge()); - return studentResponse; + + return blockingStub.getStudent(StudentRequest.newBuilder().setId(1).build()); } catch (StatusRuntimeException e) { log.error("Failed to retrieve student: " + e.getStatus().getDescription()); throw e; diff --git a/test/cucumber-tests/src/test/resources/tests/api/GRPC.feature b/test/cucumber-tests/src/test/resources/tests/api/GRPC.feature index 1821732b4..9c69c0b5d 100644 --- a/test/cucumber-tests/src/test/resources/tests/api/GRPC.feature +++ b/test/cucumber-tests/src/test/resources/tests/api/GRPC.feature @@ -14,18 +14,13 @@ Feature: Generating APK conf for gRPC API Then the response status code should be 200 Then I set headers | Authorization | bearer ${accessToken} | - And I make grpc request + And I make grpc request GetStudent with token to "default.gw.wso2.com" with port 9095 And I eventually receive 200 response code, not accepting | 429 | | 500 | - And the response body should contain "\"name\":\"string\"" - Then I set headers - | Authorization | bearer ${accessToken} | - And I send "POST" request to "https://default.gw.wso2.com:9095/grpc" - And I eventually receive 200 response code, not accepting - | 429 | - | 500 | - And the response body should contain "\"name\":\"string\"" + And the student response body should contain name: "Dineth" age: 10 + + Scenario: Undeploy API Given The system is ready From 55cf2ad22fe973d9dc794cba4e85aa082f0feb96 Mon Sep 17 00:00:00 2001 From: DinethH Date: Fri, 26 Apr 2024 09:45:59 +0530 Subject: [PATCH 13/29] generalized interceptor --- .../wso2/apk/integration/api/BaseSteps.java | 12 ++---- .../utils/GenericClientInterceptor.java | 39 +++++++++++++++++++ .../utils/JWTClientInterceptor.java | 28 ------------- .../clients/SimpleGRPCStudentClient.java | 28 +++---------- .../src/test/resources/tests/api/GRPC.feature | 4 +- 5 files changed, 48 insertions(+), 63 deletions(-) create mode 100644 test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/GenericClientInterceptor.java delete mode 100644 test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/JWTClientInterceptor.java diff --git a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/BaseSteps.java b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/BaseSteps.java index e3c713805..e747a56f3 100644 --- a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/BaseSteps.java +++ b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/BaseSteps.java @@ -148,16 +148,10 @@ public void sendHttpRequest(String httpMethod, String url, String body) throws I } } - @Then("I make grpc request to GetStudent to {string} with port {int}") - public void GetStudent(String arg0, int arg1) { + @Then("I make grpc request GetStudent to {string} with port {int}") + public void GetStudent(String arg0, int arg1 ) { SimpleGRPCStudentClient grpcStudentClient = new SimpleGRPCStudentClient(arg0,arg1); - sharedContext.setStudentResponse(grpcStudentClient.GetStudent()); - } - - @Then("I make grpc request GetStudent with token to {string} with port {int}") - public void GetStudentWithToken(String arg0, int arg1 ) { - SimpleGRPCStudentClient grpcStudentClient = new SimpleGRPCStudentClient(arg0,arg1); - sharedContext.setStudentResponse(grpcStudentClient.GetStudent(sharedContext.getAccessToken())); + sharedContext.setStudentResponse(grpcStudentClient.GetStudent(sharedContext.getHeaders())); } // It will send request using a new thread and forget about the response diff --git a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/GenericClientInterceptor.java b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/GenericClientInterceptor.java new file mode 100644 index 000000000..17ab5702e --- /dev/null +++ b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/GenericClientInterceptor.java @@ -0,0 +1,39 @@ +package org.wso2.apk.integration.utils; +import io.grpc.ClientInterceptor; +import io.grpc.ForwardingClientCall; +import io.grpc.Metadata; +import io.grpc.MethodDescriptor; +import io.grpc.CallOptions; +import io.grpc.ClientCall; +import io.grpc.Channel; +import java.util.Map; + + +import java.util.Map; + +public class GenericClientInterceptor implements ClientInterceptor { + + private Map headers; + + public GenericClientInterceptor(Map headers) { + this.headers = headers; + } + + @Override + public ClientCall interceptCall( + MethodDescriptor method, CallOptions callOptions, Channel next) { + return new ForwardingClientCall.SimpleForwardingClientCall( + next.newCall(method, callOptions)) { + + @Override + public void start(Listener responseListener, Metadata headersMetadata) { + // Set each header in the map to the Metadata headers + headers.forEach((key, value) -> headersMetadata.put( + Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER), value)); + + super.start(responseListener, headersMetadata); + } + }; + } +} + diff --git a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/JWTClientInterceptor.java b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/JWTClientInterceptor.java deleted file mode 100644 index fe01777ea..000000000 --- a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/JWTClientInterceptor.java +++ /dev/null @@ -1,28 +0,0 @@ -package org.wso2.apk.integration.utils; -import io.grpc.*; - -public class JWTClientInterceptor implements ClientInterceptor { - - private String jwtToken; - - public JWTClientInterceptor(String jwtToken) { - this.jwtToken = jwtToken; - } - - @Override - public ClientCall interceptCall( - MethodDescriptor method, CallOptions callOptions, Channel next) { - return new ForwardingClientCall.SimpleForwardingClientCall( - next.newCall(method, callOptions)) { - - @Override - public void start(Listener responseListener, Metadata headers) { - headers.put( - Metadata.Key.of("Authorization", Metadata.ASCII_STRING_MARSHALLER), - "Bearer " + jwtToken); - super.start(responseListener, headers); - } - }; - } -} - diff --git a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/SimpleGRPCStudentClient.java b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/SimpleGRPCStudentClient.java index b028a70dc..19c0111df 100644 --- a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/SimpleGRPCStudentClient.java +++ b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/SimpleGRPCStudentClient.java @@ -8,12 +8,13 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import io.grpc.ManagedChannel; -import org.wso2.apk.integration.utils.JWTClientInterceptor; +import org.wso2.apk.integration.utils.GenericClientInterceptor; import org.wso2.apk.integration.utils.clients.studentGrpcClient.StudentRequest; import org.wso2.apk.integration.utils.clients.studentGrpcClient.StudentResponse; import org.wso2.apk.integration.utils.clients.studentGrpcClient.StudentServiceGrpc; import javax.net.ssl.SSLException; +import java.util.Map; public class SimpleGRPCStudentClient { @@ -27,34 +28,15 @@ public SimpleGRPCStudentClient(String host, int port) { this.port = port; } - public StudentResponse GetStudent() { + public StudentResponse GetStudent(Map headers) { try { SslContext sslContext = GrpcSslContexts.forClient() .trustManager(InsecureTrustManagerFactory.INSTANCE) .build(); - - ManagedChannel managedChannel = NettyChannelBuilder.forAddress(host, port) - .sslContext(sslContext) - .build(); - StudentServiceGrpc.StudentServiceBlockingStub blockingStub = StudentServiceGrpc.newBlockingStub(managedChannel); - - return blockingStub.getStudent(StudentRequest.newBuilder().setId(1).build()); - } catch (StatusRuntimeException e) { - log.error("Failed to retrieve student: " + e.getStatus().getDescription()); - throw e; - } catch (SSLException e) { - throw new RuntimeException(e); - } - } - public StudentResponse GetStudent(String token) { - try { - SslContext sslContext = GrpcSslContexts.forClient() - .trustManager(InsecureTrustManagerFactory.INSTANCE) - .build(); - + GenericClientInterceptor interceptor = new GenericClientInterceptor(headers); ManagedChannel managedChannel = NettyChannelBuilder.forAddress(host, port) .sslContext(sslContext) - .intercept(new JWTClientInterceptor(token)) // replace "your-jwt-token" with your actual JWT token + .intercept(interceptor) .build(); StudentServiceGrpc.StudentServiceBlockingStub blockingStub = StudentServiceGrpc.newBlockingStub(managedChannel); diff --git a/test/cucumber-tests/src/test/resources/tests/api/GRPC.feature b/test/cucumber-tests/src/test/resources/tests/api/GRPC.feature index 9c69c0b5d..af54d3123 100644 --- a/test/cucumber-tests/src/test/resources/tests/api/GRPC.feature +++ b/test/cucumber-tests/src/test/resources/tests/api/GRPC.feature @@ -14,14 +14,12 @@ Feature: Generating APK conf for gRPC API Then the response status code should be 200 Then I set headers | Authorization | bearer ${accessToken} | - And I make grpc request GetStudent with token to "default.gw.wso2.com" with port 9095 + And I make grpc request GetStudent to "default.gw.wso2.com" with port 9095 And I eventually receive 200 response code, not accepting | 429 | | 500 | And the student response body should contain name: "Dineth" age: 10 - - Scenario: Undeploy API Given The system is ready And I have a valid subscription From 2667f325950e581008002432b15df03cb0b83de0 Mon Sep 17 00:00:00 2001 From: DinethH Date: Fri, 26 Apr 2024 09:53:32 +0530 Subject: [PATCH 14/29] add cucumber test for oauth2 disabled --- .../grpc/grpc_with_disabled_auth.apk-conf | 32 +++++++++++++++++++ .../src/test/resources/tests/api/GRPC.feature | 18 +++++++++++ 2 files changed, 50 insertions(+) create mode 100644 test/cucumber-tests/src/test/resources/artifacts/apk-confs/grpc/grpc_with_disabled_auth.apk-conf diff --git a/test/cucumber-tests/src/test/resources/artifacts/apk-confs/grpc/grpc_with_disabled_auth.apk-conf b/test/cucumber-tests/src/test/resources/artifacts/apk-confs/grpc/grpc_with_disabled_auth.apk-conf new file mode 100644 index 000000000..50bdc465b --- /dev/null +++ b/test/cucumber-tests/src/test/resources/artifacts/apk-confs/grpc/grpc_with_disabled_auth.apk-conf @@ -0,0 +1,32 @@ +--- +name: "demo-grpc-api" +basePath: "/dineth.grpc.api" +version: "v1" +type: "GRPC" +id: "grpc-auth-disabled-api" +endpointConfigurations: + production: + endpoint: "http://grpc-backend:6565" +defaultVersion: false +subscriptionValidation: false +operations: +- target: "student.StudentService" + verb: "GetStudent" + secured: true + scopes: [] +- target: "student.StudentService" + verb: "GetStudentStream" + secured: true + scopes: [] +- target: "student.StudentService" + verb: "SendStudentStream" + secured: true + scopes: [] +- target: "student.StudentService" + verb: "SendAndGetStudentStream" + secured: true + scopes: [] +authentication: + - authType: OAuth2 + required: mandatory + enabled: false diff --git a/test/cucumber-tests/src/test/resources/tests/api/GRPC.feature b/test/cucumber-tests/src/test/resources/tests/api/GRPC.feature index af54d3123..13a19a501 100644 --- a/test/cucumber-tests/src/test/resources/tests/api/GRPC.feature +++ b/test/cucumber-tests/src/test/resources/tests/api/GRPC.feature @@ -26,3 +26,21 @@ Feature: Generating APK conf for gRPC API When I undeploy the API whose ID is "grpc-basic-api" Then the response status code should be 202 + Scenario: Deploying gRPC API with OAuth2 disabled + Given The system is ready + And I have a valid subscription + When I use the APK Conf file "artifacts/apk-confs/grpc/grpc_with_disabled_auth.apk-conf" + And the definition file "artifacts/definitions/student.proto" + And make the API deployment request + Then the response status code should be 200 + And I make grpc request GetStudent to "default.gw.wso2.com" with port 9095 + And I eventually receive 200 response code, not accepting + | 429 | + | 500 | + And the student response body should contain name: "Dineth" age: 10 + + Scenario: Undeploy API + Given The system is ready + And I have a valid subscription + When I undeploy the API whose ID is "grpc-auth-disabled-api" + Then the response status code should be 202 From 3182a1303fe01eb12f0e8283601d5f222acf055f Mon Sep 17 00:00:00 2001 From: DinethH Date: Fri, 26 Apr 2024 11:02:51 +0530 Subject: [PATCH 15/29] add cucumber test for scopes --- .../wso2/apk/integration/api/BaseSteps.java | 21 ++++++++++++-- .../apk/integration/api/SharedContext.java | 8 +++++- .../clients/SimpleGRPCStudentClient.java | 28 +++++++++++++------ .../apk-confs/grpc/grpc_scopes.apk-conf | 28 +++++++++++++++++++ .../src/test/resources/tests/api/GRPC.feature | 27 ++++++++++++++++++ 5 files changed, 100 insertions(+), 12 deletions(-) create mode 100644 test/cucumber-tests/src/test/resources/artifacts/apk-confs/grpc/grpc_scopes.apk-conf diff --git a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/BaseSteps.java b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/BaseSteps.java index e747a56f3..ba54e504e 100644 --- a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/BaseSteps.java +++ b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/BaseSteps.java @@ -39,6 +39,8 @@ import io.cucumber.java.Before; import io.cucumber.java.en.Given; import io.cucumber.java.en.Then; +import io.grpc.Status; +import io.grpc.StatusRuntimeException; import org.apache.commons.io.IOUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -125,6 +127,13 @@ public void theResponseStatusCodeShouldBe(int expectedStatusCode) throws IOExcep Assert.assertEquals(actualStatusCode, expectedStatusCode); } + @Then("the grpc error response status code should be {int}") + public void theGrpcErrorResponseStatusCodeShouldBe(int expectedStatusCode) throws IOException { + + int actualStatusCode = sharedContext.getGrpcErrorCode(); + Assert.assertEquals(actualStatusCode, expectedStatusCode); + } + @Then("I send {string} request to {string} with body {string}") public void sendHttpRequest(String httpMethod, String url, String body) throws IOException { body = Utils.resolveVariables(body, sharedContext.getValueStore()); @@ -149,9 +158,15 @@ public void sendHttpRequest(String httpMethod, String url, String body) throws I } @Then("I make grpc request GetStudent to {string} with port {int}") - public void GetStudent(String arg0, int arg1 ) { - SimpleGRPCStudentClient grpcStudentClient = new SimpleGRPCStudentClient(arg0,arg1); - sharedContext.setStudentResponse(grpcStudentClient.GetStudent(sharedContext.getHeaders())); + public void GetStudent(String arg0, int arg1) throws StatusRuntimeException { + try { + SimpleGRPCStudentClient grpcStudentClient = new SimpleGRPCStudentClient(arg0, arg1); + sharedContext.setStudentResponse(grpcStudentClient.GetStudent(sharedContext.getHeaders())); + } catch (StatusRuntimeException e) { + if (e.getStatus().getCode()== Status.Code.PERMISSION_DENIED){ + sharedContext.setGrpcErrorCode(403); + } + } } // It will send request using a new thread and forget about the response diff --git a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/SharedContext.java b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/SharedContext.java index cca88b764..2b7c88bb0 100644 --- a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/SharedContext.java +++ b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/SharedContext.java @@ -32,11 +32,11 @@ public class SharedContext { private SimpleHTTPClient httpClient; - private SimpleGRPCStudentClient grpcStudentClient; private StudentResponse studentResponse; private String accessToken; private HttpResponse response; private String responseBody; + private int grpcErrorCode; private HashMap valueStore = new HashMap<>(); private HashMap headers = new HashMap<>(); @@ -56,6 +56,12 @@ public void setAccessToken(String accessToken) { this.accessToken = accessToken; } + public int getGrpcErrorCode() { + return grpcErrorCode; + } + public void setGrpcErrorCode(int grpcErrorCode) { + this.grpcErrorCode = grpcErrorCode; + } public HttpResponse getResponse() { diff --git a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/SimpleGRPCStudentClient.java b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/SimpleGRPCStudentClient.java index 19c0111df..c6a133183 100644 --- a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/SimpleGRPCStudentClient.java +++ b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/SimpleGRPCStudentClient.java @@ -15,6 +15,7 @@ import javax.net.ssl.SSLException; import java.util.Map; +import java.util.concurrent.TimeUnit; public class SimpleGRPCStudentClient { @@ -28,24 +29,35 @@ public SimpleGRPCStudentClient(String host, int port) { this.port = port; } - public StudentResponse GetStudent(Map headers) { + public StudentResponse GetStudent(Map headers) throws StatusRuntimeException{ + ManagedChannel managedChannel = null; try { SslContext sslContext = GrpcSslContexts.forClient() .trustManager(InsecureTrustManagerFactory.INSTANCE) .build(); + GenericClientInterceptor interceptor = new GenericClientInterceptor(headers); - ManagedChannel managedChannel = NettyChannelBuilder.forAddress(host, port) + managedChannel = NettyChannelBuilder.forAddress(host, port) .sslContext(sslContext) .intercept(interceptor) .build(); StudentServiceGrpc.StudentServiceBlockingStub blockingStub = StudentServiceGrpc.newBlockingStub(managedChannel); - return blockingStub.getStudent(StudentRequest.newBuilder().setId(1).build()); - } catch (StatusRuntimeException e) { - log.error("Failed to retrieve student: " + e.getStatus().getDescription()); - throw e; - } catch (SSLException e) { - throw new RuntimeException(e); + }catch (SSLException e) { + throw new RuntimeException("Failed to create SSL context", e); + } finally { + // Shut down the channel to release resources + if (managedChannel != null) { + managedChannel.shutdown(); // Initiates a graceful shutdown + try { + // Wait at most 5 seconds for the channel to terminate + if (!managedChannel.awaitTermination(5, TimeUnit.SECONDS)) { + managedChannel.shutdownNow(); // Force shutdown if it does not complete within the timeout + } + } catch (InterruptedException ie) { + managedChannel.shutdownNow(); // Force shutdown if the thread is interrupted + } + } } } diff --git a/test/cucumber-tests/src/test/resources/artifacts/apk-confs/grpc/grpc_scopes.apk-conf b/test/cucumber-tests/src/test/resources/artifacts/apk-confs/grpc/grpc_scopes.apk-conf new file mode 100644 index 000000000..c4edb6983 --- /dev/null +++ b/test/cucumber-tests/src/test/resources/artifacts/apk-confs/grpc/grpc_scopes.apk-conf @@ -0,0 +1,28 @@ +--- +name: "demo-grpc-api" +basePath: "/dineth.grpc.api" +version: "v1" +type: "GRPC" +id: "grpc-scopes" +endpointConfigurations: + production: + endpoint: "http://grpc-backend:6565" +defaultVersion: false +subscriptionValidation: false +operations: +- target: "student.StudentService" + verb: "GetStudent" + secured: true + scopes: ["wso2"] +- target: "student.StudentService" + verb: "GetStudentStream" + secured: true + scopes: [] +- target: "student.StudentService" + verb: "SendStudentStream" + secured: true + scopes: [] +- target: "student.StudentService" + verb: "SendAndGetStudentStream" + secured: true + scopes: [] \ No newline at end of file diff --git a/test/cucumber-tests/src/test/resources/tests/api/GRPC.feature b/test/cucumber-tests/src/test/resources/tests/api/GRPC.feature index 13a19a501..a851159c3 100644 --- a/test/cucumber-tests/src/test/resources/tests/api/GRPC.feature +++ b/test/cucumber-tests/src/test/resources/tests/api/GRPC.feature @@ -44,3 +44,30 @@ Feature: Generating APK conf for gRPC API And I have a valid subscription When I undeploy the API whose ID is "grpc-auth-disabled-api" Then the response status code should be 202 + + Scenario: Deploying gRPC API with scopes + Given The system is ready + And I have a valid subscription + When I use the APK Conf file "artifacts/apk-confs/grpc/grpc_scopes.apk-conf" + And the definition file "artifacts/definitions/student.proto" + And make the API deployment request + Then the response status code should be 200 + Then I set headers + | Authorization | bearer ${accessToken} | + And I make grpc request GetStudent to "default.gw.wso2.com" with port 9095 + And the grpc error response status code should be 403 + Given I have a valid subscription with scopes + | wso2 | + Then I set headers + | Authorization | bearer ${accessToken} | + And I make grpc request GetStudent to "default.gw.wso2.com" with port 9095 + And I eventually receive 200 response code, not accepting + | 429 | + | 500 | + And the student response body should contain name: "Dineth" age: 10 + + Scenario: Undeploy API + Given The system is ready + And I have a valid subscription + When I undeploy the API whose ID is "grpc-scopes" + Then the response status code should be 202 \ No newline at end of file From efc2d79dad7e4992965c9c1a9b8c92e26142123e Mon Sep 17 00:00:00 2001 From: DinethH Date: Fri, 26 Apr 2024 11:26:54 +0530 Subject: [PATCH 16/29] add cucumber test for default version --- .../wso2/apk/integration/api/BaseSteps.java | 12 + .../clients/SimpleGRPCStudentClient.java | 31 ++ .../StudentServiceDefaultVersionGrpc.java | 458 ++++++++++++++++++ .../grpc/grpc_with_default_version.apk-conf | 28 ++ .../src/test/resources/tests/api/GRPC.feature | 29 ++ 5 files changed, 558 insertions(+) create mode 100644 test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/studentGrpcClient/StudentServiceDefaultVersionGrpc.java create mode 100644 test/cucumber-tests/src/test/resources/artifacts/apk-confs/grpc/grpc_with_default_version.apk-conf diff --git a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/BaseSteps.java b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/BaseSteps.java index ba54e504e..6321d4ee7 100644 --- a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/BaseSteps.java +++ b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/BaseSteps.java @@ -169,6 +169,18 @@ public void GetStudent(String arg0, int arg1) throws StatusRuntimeException { } } + @Then("I make grpc request GetStudent default version to {string} with port {int}") + public void GetStudentDefaultVersion(String arg0, int arg1) throws StatusRuntimeException { + try { + SimpleGRPCStudentClient grpcStudentClient = new SimpleGRPCStudentClient(arg0, arg1); + sharedContext.setStudentResponse(grpcStudentClient.GetStudent(sharedContext.getHeaders())); + } catch (StatusRuntimeException e) { + if (e.getStatus().getCode()== Status.Code.PERMISSION_DENIED){ + sharedContext.setGrpcErrorCode(403); + } + } + } + // It will send request using a new thread and forget about the response @Then("I send {string} async request to {string} with body {string}") public void sendAsyncHttpRequest(String httpMethod, String url, String body) throws IOException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException { diff --git a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/SimpleGRPCStudentClient.java b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/SimpleGRPCStudentClient.java index c6a133183..d8fcc7e7c 100644 --- a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/SimpleGRPCStudentClient.java +++ b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/SimpleGRPCStudentClient.java @@ -11,6 +11,7 @@ import org.wso2.apk.integration.utils.GenericClientInterceptor; import org.wso2.apk.integration.utils.clients.studentGrpcClient.StudentRequest; import org.wso2.apk.integration.utils.clients.studentGrpcClient.StudentResponse; +import org.wso2.apk.integration.utils.clients.studentGrpcClient.StudentServiceDefaultVersionGrpc; import org.wso2.apk.integration.utils.clients.studentGrpcClient.StudentServiceGrpc; import javax.net.ssl.SSLException; @@ -60,7 +61,37 @@ public StudentResponse GetStudent(Map headers) throws StatusRunt } } } + public StudentResponse GetStudentDefaultVersion(Map headers) throws StatusRuntimeException{ + ManagedChannel managedChannel = null; + try { + SslContext sslContext = GrpcSslContexts.forClient() + .trustManager(InsecureTrustManagerFactory.INSTANCE) + .build(); + GenericClientInterceptor interceptor = new GenericClientInterceptor(headers); + managedChannel = NettyChannelBuilder.forAddress(host, port) + .sslContext(sslContext) + .intercept(interceptor) + .build(); + StudentServiceDefaultVersionGrpc.StudentServiceBlockingStub blockingStub = StudentServiceDefaultVersionGrpc.newBlockingStub(managedChannel); + return blockingStub.getStudent(StudentRequest.newBuilder().setId(1).build()); + }catch (SSLException e) { + throw new RuntimeException("Failed to create SSL context", e); + } finally { + // Shut down the channel to release resources + if (managedChannel != null) { + managedChannel.shutdown(); // Initiates a graceful shutdown + try { + // Wait at most 5 seconds for the channel to terminate + if (!managedChannel.awaitTermination(5, TimeUnit.SECONDS)) { + managedChannel.shutdownNow(); // Force shutdown if it does not complete within the timeout + } + } catch (InterruptedException ie) { + managedChannel.shutdownNow(); // Force shutdown if the thread is interrupted + } + } + } + } } diff --git a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/studentGrpcClient/StudentServiceDefaultVersionGrpc.java b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/studentGrpcClient/StudentServiceDefaultVersionGrpc.java new file mode 100644 index 000000000..9a2878343 --- /dev/null +++ b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/studentGrpcClient/StudentServiceDefaultVersionGrpc.java @@ -0,0 +1,458 @@ +package org.wso2.apk.integration.utils.clients.studentGrpcClient; + +import static io.grpc.MethodDescriptor.generateFullMethodName; + +/** + */ +@javax.annotation.Generated( + value = "by gRPC proto compiler (version 1.39.0)", + comments = "Source: student.proto") +public final class StudentServiceDefaultVersionGrpc { + + private StudentServiceDefaultVersionGrpc() {} + + public static final String SERVICE_NAME = "dineth.grpc.api.student.StudentService"; + + // Static method descriptors that strictly reflect the proto. + private static volatile io.grpc.MethodDescriptor getGetStudentMethod; + + @io.grpc.stub.annotations.RpcMethod( + fullMethodName = SERVICE_NAME + '/' + "GetStudent", + requestType = StudentRequest.class, + responseType = StudentResponse.class, + methodType = io.grpc.MethodDescriptor.MethodType.UNARY) + public static io.grpc.MethodDescriptor getGetStudentMethod() { + io.grpc.MethodDescriptor getGetStudentMethod; + if ((getGetStudentMethod = StudentServiceDefaultVersionGrpc.getGetStudentMethod) == null) { + synchronized (StudentServiceDefaultVersionGrpc.class) { + if ((getGetStudentMethod = StudentServiceDefaultVersionGrpc.getGetStudentMethod) == null) { + StudentServiceDefaultVersionGrpc.getGetStudentMethod = getGetStudentMethod = + io.grpc.MethodDescriptor.newBuilder() + .setType(io.grpc.MethodDescriptor.MethodType.UNARY) + .setFullMethodName(generateFullMethodName(SERVICE_NAME, "GetStudent")) + .setSampledToLocalTracing(true) + .setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller( + StudentRequest.getDefaultInstance())) + .setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller( + StudentResponse.getDefaultInstance())) + .setSchemaDescriptor(new StudentServiceMethodDescriptorSupplier("GetStudent")) + .build(); + } + } + } + return getGetStudentMethod; + } + + private static volatile io.grpc.MethodDescriptor getGetStudentStreamMethod; + + @io.grpc.stub.annotations.RpcMethod( + fullMethodName = SERVICE_NAME + '/' + "GetStudentStream", + requestType = StudentRequest.class, + responseType = StudentResponse.class, + methodType = io.grpc.MethodDescriptor.MethodType.SERVER_STREAMING) + public static io.grpc.MethodDescriptor getGetStudentStreamMethod() { + io.grpc.MethodDescriptor getGetStudentStreamMethod; + if ((getGetStudentStreamMethod = StudentServiceDefaultVersionGrpc.getGetStudentStreamMethod) == null) { + synchronized (StudentServiceDefaultVersionGrpc.class) { + if ((getGetStudentStreamMethod = StudentServiceDefaultVersionGrpc.getGetStudentStreamMethod) == null) { + StudentServiceDefaultVersionGrpc.getGetStudentStreamMethod = getGetStudentStreamMethod = + io.grpc.MethodDescriptor.newBuilder() + .setType(io.grpc.MethodDescriptor.MethodType.SERVER_STREAMING) + .setFullMethodName(generateFullMethodName(SERVICE_NAME, "GetStudentStream")) + .setSampledToLocalTracing(true) + .setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller( + StudentRequest.getDefaultInstance())) + .setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller( + StudentResponse.getDefaultInstance())) + .setSchemaDescriptor(new StudentServiceMethodDescriptorSupplier("GetStudentStream")) + .build(); + } + } + } + return getGetStudentStreamMethod; + } + + private static volatile io.grpc.MethodDescriptor getSendStudentStreamMethod; + + @io.grpc.stub.annotations.RpcMethod( + fullMethodName = SERVICE_NAME + '/' + "SendStudentStream", + requestType = StudentRequest.class, + responseType = StudentResponse.class, + methodType = io.grpc.MethodDescriptor.MethodType.CLIENT_STREAMING) + public static io.grpc.MethodDescriptor getSendStudentStreamMethod() { + io.grpc.MethodDescriptor getSendStudentStreamMethod; + if ((getSendStudentStreamMethod = StudentServiceDefaultVersionGrpc.getSendStudentStreamMethod) == null) { + synchronized (StudentServiceDefaultVersionGrpc.class) { + if ((getSendStudentStreamMethod = StudentServiceDefaultVersionGrpc.getSendStudentStreamMethod) == null) { + StudentServiceDefaultVersionGrpc.getSendStudentStreamMethod = getSendStudentStreamMethod = + io.grpc.MethodDescriptor.newBuilder() + .setType(io.grpc.MethodDescriptor.MethodType.CLIENT_STREAMING) + .setFullMethodName(generateFullMethodName(SERVICE_NAME, "SendStudentStream")) + .setSampledToLocalTracing(true) + .setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller( + StudentRequest.getDefaultInstance())) + .setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller( + StudentResponse.getDefaultInstance())) + .setSchemaDescriptor(new StudentServiceMethodDescriptorSupplier("SendStudentStream")) + .build(); + } + } + } + return getSendStudentStreamMethod; + } + + private static volatile io.grpc.MethodDescriptor getSendAndGetStudentStreamMethod; + + @io.grpc.stub.annotations.RpcMethod( + fullMethodName = SERVICE_NAME + '/' + "SendAndGetStudentStream", + requestType = StudentRequest.class, + responseType = StudentResponse.class, + methodType = io.grpc.MethodDescriptor.MethodType.BIDI_STREAMING) + public static io.grpc.MethodDescriptor getSendAndGetStudentStreamMethod() { + io.grpc.MethodDescriptor getSendAndGetStudentStreamMethod; + if ((getSendAndGetStudentStreamMethod = StudentServiceDefaultVersionGrpc.getSendAndGetStudentStreamMethod) == null) { + synchronized (StudentServiceDefaultVersionGrpc.class) { + if ((getSendAndGetStudentStreamMethod = StudentServiceDefaultVersionGrpc.getSendAndGetStudentStreamMethod) == null) { + StudentServiceDefaultVersionGrpc.getSendAndGetStudentStreamMethod = getSendAndGetStudentStreamMethod = + io.grpc.MethodDescriptor.newBuilder() + .setType(io.grpc.MethodDescriptor.MethodType.BIDI_STREAMING) + .setFullMethodName(generateFullMethodName(SERVICE_NAME, "SendAndGetStudentStream")) + .setSampledToLocalTracing(true) + .setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller( + StudentRequest.getDefaultInstance())) + .setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller( + StudentResponse.getDefaultInstance())) + .setSchemaDescriptor(new StudentServiceMethodDescriptorSupplier("SendAndGetStudentStream")) + .build(); + } + } + } + return getSendAndGetStudentStreamMethod; + } + + /** + * Creates a new async stub that supports all call types for the service + */ + public static StudentServiceStub newStub(io.grpc.Channel channel) { + io.grpc.stub.AbstractStub.StubFactory factory = + new io.grpc.stub.AbstractStub.StubFactory() { + @Override + public StudentServiceStub newStub(io.grpc.Channel channel, io.grpc.CallOptions callOptions) { + return new StudentServiceStub(channel, callOptions); + } + }; + return StudentServiceStub.newStub(factory, channel); + } + + /** + * Creates a new blocking-style stub that supports unary and streaming output calls on the service + */ + public static StudentServiceBlockingStub newBlockingStub( + io.grpc.Channel channel) { + io.grpc.stub.AbstractStub.StubFactory factory = + new io.grpc.stub.AbstractStub.StubFactory() { + @Override + public StudentServiceBlockingStub newStub(io.grpc.Channel channel, io.grpc.CallOptions callOptions) { + return new StudentServiceBlockingStub(channel, callOptions); + } + }; + return StudentServiceBlockingStub.newStub(factory, channel); + } + + /** + * Creates a new ListenableFuture-style stub that supports unary calls on the service + */ + public static StudentServiceFutureStub newFutureStub( + io.grpc.Channel channel) { + io.grpc.stub.AbstractStub.StubFactory factory = + new io.grpc.stub.AbstractStub.StubFactory() { + @Override + public StudentServiceFutureStub newStub(io.grpc.Channel channel, io.grpc.CallOptions callOptions) { + return new StudentServiceFutureStub(channel, callOptions); + } + }; + return StudentServiceFutureStub.newStub(factory, channel); + } + + /** + */ + public static abstract class StudentServiceImplBase implements io.grpc.BindableService { + + /** + */ + public void getStudent(StudentRequest request, + io.grpc.stub.StreamObserver responseObserver) { + io.grpc.stub.ServerCalls.asyncUnimplementedUnaryCall(getGetStudentMethod(), responseObserver); + } + + /** + */ + public void getStudentStream(StudentRequest request, + io.grpc.stub.StreamObserver responseObserver) { + io.grpc.stub.ServerCalls.asyncUnimplementedUnaryCall(getGetStudentStreamMethod(), responseObserver); + } + + /** + */ + public io.grpc.stub.StreamObserver sendStudentStream( + io.grpc.stub.StreamObserver responseObserver) { + return io.grpc.stub.ServerCalls.asyncUnimplementedStreamingCall(getSendStudentStreamMethod(), responseObserver); + } + + /** + */ + public io.grpc.stub.StreamObserver sendAndGetStudentStream( + io.grpc.stub.StreamObserver responseObserver) { + return io.grpc.stub.ServerCalls.asyncUnimplementedStreamingCall(getSendAndGetStudentStreamMethod(), responseObserver); + } + + @Override public final io.grpc.ServerServiceDefinition bindService() { + return io.grpc.ServerServiceDefinition.builder(getServiceDescriptor()) + .addMethod( + getGetStudentMethod(), + io.grpc.stub.ServerCalls.asyncUnaryCall( + new MethodHandlers< + StudentRequest, + StudentResponse>( + this, METHODID_GET_STUDENT))) + .addMethod( + getGetStudentStreamMethod(), + io.grpc.stub.ServerCalls.asyncServerStreamingCall( + new MethodHandlers< + StudentRequest, + StudentResponse>( + this, METHODID_GET_STUDENT_STREAM))) + .addMethod( + getSendStudentStreamMethod(), + io.grpc.stub.ServerCalls.asyncClientStreamingCall( + new MethodHandlers< + StudentRequest, + StudentResponse>( + this, METHODID_SEND_STUDENT_STREAM))) + .addMethod( + getSendAndGetStudentStreamMethod(), + io.grpc.stub.ServerCalls.asyncBidiStreamingCall( + new MethodHandlers< + StudentRequest, + StudentResponse>( + this, METHODID_SEND_AND_GET_STUDENT_STREAM))) + .build(); + } + } + + /** + */ + public static final class StudentServiceStub extends io.grpc.stub.AbstractAsyncStub { + private StudentServiceStub( + io.grpc.Channel channel, io.grpc.CallOptions callOptions) { + super(channel, callOptions); + } + + @Override + protected StudentServiceStub build( + io.grpc.Channel channel, io.grpc.CallOptions callOptions) { + return new StudentServiceStub(channel, callOptions); + } + + /** + */ + public void getStudent(StudentRequest request, + io.grpc.stub.StreamObserver responseObserver) { + io.grpc.stub.ClientCalls.asyncUnaryCall( + getChannel().newCall(getGetStudentMethod(), getCallOptions()), request, responseObserver); + } + + /** + */ + public void getStudentStream(StudentRequest request, + io.grpc.stub.StreamObserver responseObserver) { + io.grpc.stub.ClientCalls.asyncServerStreamingCall( + getChannel().newCall(getGetStudentStreamMethod(), getCallOptions()), request, responseObserver); + } + + /** + */ + public io.grpc.stub.StreamObserver sendStudentStream( + io.grpc.stub.StreamObserver responseObserver) { + return io.grpc.stub.ClientCalls.asyncClientStreamingCall( + getChannel().newCall(getSendStudentStreamMethod(), getCallOptions()), responseObserver); + } + + /** + */ + public io.grpc.stub.StreamObserver sendAndGetStudentStream( + io.grpc.stub.StreamObserver responseObserver) { + return io.grpc.stub.ClientCalls.asyncBidiStreamingCall( + getChannel().newCall(getSendAndGetStudentStreamMethod(), getCallOptions()), responseObserver); + } + } + + /** + */ + public static final class StudentServiceBlockingStub extends io.grpc.stub.AbstractBlockingStub { + private StudentServiceBlockingStub( + io.grpc.Channel channel, io.grpc.CallOptions callOptions) { + super(channel, callOptions); + } + + @Override + protected StudentServiceBlockingStub build( + io.grpc.Channel channel, io.grpc.CallOptions callOptions) { + return new StudentServiceBlockingStub(channel, callOptions); + } + + /** + */ + public StudentResponse getStudent(StudentRequest request) { + return io.grpc.stub.ClientCalls.blockingUnaryCall( + getChannel(), getGetStudentMethod(), getCallOptions(), request); + } + + /** + */ + public java.util.Iterator getStudentStream( + StudentRequest request) { + return io.grpc.stub.ClientCalls.blockingServerStreamingCall( + getChannel(), getGetStudentStreamMethod(), getCallOptions(), request); + } + } + + /** + */ + public static final class StudentServiceFutureStub extends io.grpc.stub.AbstractFutureStub { + private StudentServiceFutureStub( + io.grpc.Channel channel, io.grpc.CallOptions callOptions) { + super(channel, callOptions); + } + + @Override + protected StudentServiceFutureStub build( + io.grpc.Channel channel, io.grpc.CallOptions callOptions) { + return new StudentServiceFutureStub(channel, callOptions); + } + + /** + */ + public com.google.common.util.concurrent.ListenableFuture getStudent( + StudentRequest request) { + return io.grpc.stub.ClientCalls.futureUnaryCall( + getChannel().newCall(getGetStudentMethod(), getCallOptions()), request); + } + } + + private static final int METHODID_GET_STUDENT = 0; + private static final int METHODID_GET_STUDENT_STREAM = 1; + private static final int METHODID_SEND_STUDENT_STREAM = 2; + private static final int METHODID_SEND_AND_GET_STUDENT_STREAM = 3; + + private static final class MethodHandlers implements + io.grpc.stub.ServerCalls.UnaryMethod, + io.grpc.stub.ServerCalls.ServerStreamingMethod, + io.grpc.stub.ServerCalls.ClientStreamingMethod, + io.grpc.stub.ServerCalls.BidiStreamingMethod { + private final StudentServiceImplBase serviceImpl; + private final int methodId; + + MethodHandlers(StudentServiceImplBase serviceImpl, int methodId) { + this.serviceImpl = serviceImpl; + this.methodId = methodId; + } + + @Override + @SuppressWarnings("unchecked") + public void invoke(Req request, io.grpc.stub.StreamObserver responseObserver) { + switch (methodId) { + case METHODID_GET_STUDENT: + serviceImpl.getStudent((StudentRequest) request, + (io.grpc.stub.StreamObserver) responseObserver); + break; + case METHODID_GET_STUDENT_STREAM: + serviceImpl.getStudentStream((StudentRequest) request, + (io.grpc.stub.StreamObserver) responseObserver); + break; + default: + throw new AssertionError(); + } + } + + @Override + @SuppressWarnings("unchecked") + public io.grpc.stub.StreamObserver invoke( + io.grpc.stub.StreamObserver responseObserver) { + switch (methodId) { + case METHODID_SEND_STUDENT_STREAM: + return (io.grpc.stub.StreamObserver) serviceImpl.sendStudentStream( + (io.grpc.stub.StreamObserver) responseObserver); + case METHODID_SEND_AND_GET_STUDENT_STREAM: + return (io.grpc.stub.StreamObserver) serviceImpl.sendAndGetStudentStream( + (io.grpc.stub.StreamObserver) responseObserver); + default: + throw new AssertionError(); + } + } + } + + private static abstract class StudentServiceBaseDescriptorSupplier + implements io.grpc.protobuf.ProtoFileDescriptorSupplier, io.grpc.protobuf.ProtoServiceDescriptorSupplier { + StudentServiceBaseDescriptorSupplier() {} + + @Override + public com.google.protobuf.Descriptors.FileDescriptor getFileDescriptor() { + return Student.getDescriptor(); + } + + @Override + public com.google.protobuf.Descriptors.ServiceDescriptor getServiceDescriptor() { + return getFileDescriptor().findServiceByName("StudentService"); + } + } + + private static final class StudentServiceFileDescriptorSupplier + extends StudentServiceBaseDescriptorSupplier { + StudentServiceFileDescriptorSupplier() {} + } + + private static final class StudentServiceMethodDescriptorSupplier + extends StudentServiceBaseDescriptorSupplier + implements io.grpc.protobuf.ProtoMethodDescriptorSupplier { + private final String methodName; + + StudentServiceMethodDescriptorSupplier(String methodName) { + this.methodName = methodName; + } + + @Override + public com.google.protobuf.Descriptors.MethodDescriptor getMethodDescriptor() { + return getServiceDescriptor().findMethodByName(methodName); + } + } + + private static volatile io.grpc.ServiceDescriptor serviceDescriptor; + + public static io.grpc.ServiceDescriptor getServiceDescriptor() { + io.grpc.ServiceDescriptor result = serviceDescriptor; + if (result == null) { + synchronized (StudentServiceDefaultVersionGrpc.class) { + result = serviceDescriptor; + if (result == null) { + serviceDescriptor = result = io.grpc.ServiceDescriptor.newBuilder(SERVICE_NAME) + .setSchemaDescriptor(new StudentServiceFileDescriptorSupplier()) + .addMethod(getGetStudentMethod()) + .addMethod(getGetStudentStreamMethod()) + .addMethod(getSendStudentStreamMethod()) + .addMethod(getSendAndGetStudentStreamMethod()) + .build(); + } + } + } + return result; + } +} diff --git a/test/cucumber-tests/src/test/resources/artifacts/apk-confs/grpc/grpc_with_default_version.apk-conf b/test/cucumber-tests/src/test/resources/artifacts/apk-confs/grpc/grpc_with_default_version.apk-conf new file mode 100644 index 000000000..2c2f88441 --- /dev/null +++ b/test/cucumber-tests/src/test/resources/artifacts/apk-confs/grpc/grpc_with_default_version.apk-conf @@ -0,0 +1,28 @@ +--- +name: "demo-grpc-api" +basePath: "/dineth.grpc.api" +version: "v1" +type: "GRPC" +id: "grpc-default-version-api" +endpointConfigurations: + production: + endpoint: "http://grpc-backend:6565" +defaultVersion: true +subscriptionValidation: false +operations: +- target: "student.StudentService" + verb: "GetStudent" + secured: true + scopes: [] +- target: "student.StudentService" + verb: "GetStudentStream" + secured: true + scopes: [] +- target: "student.StudentService" + verb: "SendStudentStream" + secured: true + scopes: [] +- target: "student.StudentService" + verb: "SendAndGetStudentStream" + secured: true + scopes: [] \ No newline at end of file diff --git a/test/cucumber-tests/src/test/resources/tests/api/GRPC.feature b/test/cucumber-tests/src/test/resources/tests/api/GRPC.feature index a851159c3..c6b7ebcd8 100644 --- a/test/cucumber-tests/src/test/resources/tests/api/GRPC.feature +++ b/test/cucumber-tests/src/test/resources/tests/api/GRPC.feature @@ -70,4 +70,33 @@ Feature: Generating APK conf for gRPC API Given The system is ready And I have a valid subscription When I undeploy the API whose ID is "grpc-scopes" + Then the response status code should be 202 + + Scenario: Deploying gRPC API with default version enabled + Given The system is ready + And I have a valid subscription + When I use the APK Conf file "artifacts/apk-confs/grpc/grpc_with_default_version.apk-conf" + And the definition file "artifacts/definitions/student.proto" + And make the API deployment request + Then the response status code should be 200 + Then I set headers + | Authorization | bearer ${accessToken} | + And I make grpc request GetStudent to "default.gw.wso2.com" with port 9095 + And I eventually receive 200 response code, not accepting + | 429 | + | 500 | + And the student response body should contain name: "Dineth" age: 10 + Given I have a valid subscription + Then I set headers + | Authorization | bearer ${accessToken} | + And I make grpc request GetStudent default version to "default.gw.wso2.com" with port 9095 + And I eventually receive 200 response code, not accepting + | 429 | + | 500 | + And the student response body should contain name: "Dineth" age: 10 + + Scenario: Undeploy API + Given The system is ready + And I have a valid subscription + When I undeploy the API whose ID is "grpc-default-version-api" Then the response status code should be 202 \ No newline at end of file From 9a5f436b11968bef4b42c28deef7a92ac5cc825e Mon Sep 17 00:00:00 2001 From: DinethH Date: Fri, 26 Apr 2024 14:19:46 +0530 Subject: [PATCH 17/29] add missing bracket to fix issue in commit bdc279f566d230cce3c195b5adeee76666caff1a --- runtime/config-deployer-service/ballerina/APIClient.bal | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/runtime/config-deployer-service/ballerina/APIClient.bal b/runtime/config-deployer-service/ballerina/APIClient.bal index 02e78c1e6..5c86fc3bc 100644 --- a/runtime/config-deployer-service/ballerina/APIClient.bal +++ b/runtime/config-deployer-service/ballerina/APIClient.bal @@ -572,6 +572,7 @@ public class APIClient { } else { apiArtifact.sandboxGrpcRoutes.push(grpcRoute); } + } } else { model:HTTPRoute httpRoute = { metadata: @@ -593,11 +594,10 @@ public class APIClient { } } } - return; - } } + private isolated function generateAndRetrieveParentRefs(APKConf apkConf, string uniqueId) returns model:ParentReference[] { string gatewayName = gatewayConfiguration.name; string listenerName = gatewayConfiguration.listenerName; From 1d8ed4dd8f0093fc695f56b485291f9ca5d5547c Mon Sep 17 00:00:00 2001 From: DinethH Date: Fri, 26 Apr 2024 16:29:12 +0530 Subject: [PATCH 18/29] resolved comments --- runtime/config-deployer-service/ballerina/K8sClient.bal | 2 +- .../java/org/wso2/apk/config/DefinitionParserFactory.java | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/runtime/config-deployer-service/ballerina/K8sClient.bal b/runtime/config-deployer-service/ballerina/K8sClient.bal index cdc03e00b..45723229a 100644 --- a/runtime/config-deployer-service/ballerina/K8sClient.bal +++ b/runtime/config-deployer-service/ballerina/K8sClient.bal @@ -265,7 +265,7 @@ public isolated function getGqlRoutesForAPIs(string apiName, string apiVersion, return k8sApiServerEp->get(endpoint, targetType = model:GQLRouteList); } public isolated function getGrpcRoutesForAPIs(string apiName, string apiVersion, string namespace, string organization) returns model:GRPCRouteList|http:ClientError|error { - string endpoint = "/apis/dp.wso2.com/v1alpha2/namespaces/" + namespace + "/grpcroutes/?labelSelector=" + check generateUrlEncodedLabelSelector(apiName, apiVersion, organization); + string endpoint = "/apis/gateway.networking.k8s.io/v1alpha2/namespaces/" + namespace + "/grpcroutes/?labelSelector=" + check generateUrlEncodedLabelSelector(apiName, apiVersion, organization); return k8sApiServerEp->get(endpoint, targetType = model:GRPCRouteList); } isolated function deployRateLimitPolicyCR(model:RateLimitPolicy rateLimitPolicy, string namespace) returns http:Response|http:ClientError { diff --git a/runtime/config-deployer-service/java/src/main/java/org/wso2/apk/config/DefinitionParserFactory.java b/runtime/config-deployer-service/java/src/main/java/org/wso2/apk/config/DefinitionParserFactory.java index 167b2d138..585e1010e 100644 --- a/runtime/config-deployer-service/java/src/main/java/org/wso2/apk/config/DefinitionParserFactory.java +++ b/runtime/config-deployer-service/java/src/main/java/org/wso2/apk/config/DefinitionParserFactory.java @@ -21,12 +21,11 @@ private DefinitionParserFactory() { public static APIDefinition getParser(API api) { if (APIConstants.ParserType.REST.name().equals(api.getType()) - || APIConstants.ParserType.GRAPHQL.name().equals(api.getType())) { + || APIConstants.ParserType.GRAPHQL.name().equals(api.getType()) + || APIConstants.ParserType.GRPC.name().equals(api.getType())) { return new OAS3Parser(); } else if (APIConstants.ParserType.ASYNC.name().equals(api.getType())) { return new AsyncApiParser(); - } else if (APIConstants.ParserType.GRPC.name().equals(api.getType())) { - return new OAS3Parser(); } return null; } From 3c10d001e9545d3c025960fbc3778cf6ef5c77fc Mon Sep 17 00:00:00 2001 From: DinethH Date: Mon, 29 Apr 2024 10:24:46 +0530 Subject: [PATCH 19/29] changed grpc deployment and service CR --- test/cucumber-tests/CRs/artifacts.yaml | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/test/cucumber-tests/CRs/artifacts.yaml b/test/cucumber-tests/CRs/artifacts.yaml index c49bb4680..0ee441c99 100644 --- a/test/cucumber-tests/CRs/artifacts.yaml +++ b/test/cucumber-tests/CRs/artifacts.yaml @@ -1158,11 +1158,11 @@ spec: apiVersion: v1 kind: Service metadata: - name: grpc-backend-v1 + name: grpc-backend namespace: apk-integration-test spec: selector: - app: grpc-backend-v1 + app: grpc-backend ports: - protocol: TCP port: 6565 @@ -1171,22 +1171,22 @@ spec: apiVersion: apps/v1 kind: Deployment metadata: - name: grpc-backend-v1 + name: grpc-backend namespace: apk-integration-test labels: - app: grpc-backend-v1 + app: grpc-backend spec: replicas: 1 selector: matchLabels: - app: grpc-backend-v1 + app: grpc-backend template: metadata: labels: - app: grpc-backend-v1 + app: grpc-backend spec: containers: - - name: grpc-backend-v1 + - name: grpc-backend image: ddh13/dineth-grpc-demo-server:1.0.0 imagePullPolicy: Always env: From 064a67827e7d89723e1a9446776725cfc3fa9f20 Mon Sep 17 00:00:00 2001 From: DinethH Date: Mon, 29 Apr 2024 12:02:32 +0530 Subject: [PATCH 20/29] Add handling for errors --- .../wso2/apk/integration/api/BaseSteps.java | 13 +++++++++++-- .../utils/clients/SimpleGRPCStudentClient.java | 18 ++++++++++++++++-- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/BaseSteps.java b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/BaseSteps.java index 6321d4ee7..cba1b1530 100644 --- a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/BaseSteps.java +++ b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/BaseSteps.java @@ -53,6 +53,7 @@ import org.wso2.apk.integration.utils.Utils; import org.wso2.apk.integration.utils.clients.SimpleGRPCStudentClient; import org.wso2.apk.integration.utils.clients.SimpleHTTPClient; +import org.wso2.apk.integration.utils.clients.studentGrpcClient.StudentResponse; import java.io.IOException; import java.io.InputStream; @@ -97,8 +98,12 @@ public void systemIsReady() { @Then("the student response body should contain name: {string} age: {int}") public void theStudentResponseBodyShouldContainNameAndAge(String arg0, int arg1) { - int age = sharedContext.getStudentResponse().getAge(); - String name = sharedContext.getStudentResponse().getName(); + StudentResponse studentResponse = sharedContext.getStudentResponse(); + if (studentResponse == null) { + Assert.fail("Student response is null."); + } + int age = studentResponse.getAge(); + String name = studentResponse.getName(); Assert.assertEquals(name, arg0); Assert.assertEquals(age, arg1); } @@ -165,6 +170,8 @@ public void GetStudent(String arg0, int arg1) throws StatusRuntimeException { } catch (StatusRuntimeException e) { if (e.getStatus().getCode()== Status.Code.PERMISSION_DENIED){ sharedContext.setGrpcErrorCode(403); + } else { + logger.error(e.getMessage()); } } } @@ -177,6 +184,8 @@ public void GetStudentDefaultVersion(String arg0, int arg1) throws StatusRuntime } catch (StatusRuntimeException e) { if (e.getStatus().getCode()== Status.Code.PERMISSION_DENIED){ sharedContext.setGrpcErrorCode(403); + } else { + logger.error(e.getMessage()); } } } diff --git a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/SimpleGRPCStudentClient.java b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/SimpleGRPCStudentClient.java index d8fcc7e7c..c7f04210b 100644 --- a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/SimpleGRPCStudentClient.java +++ b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/SimpleGRPCStudentClient.java @@ -43,7 +43,14 @@ public StudentResponse GetStudent(Map headers) throws StatusRunt .intercept(interceptor) .build(); StudentServiceGrpc.StudentServiceBlockingStub blockingStub = StudentServiceGrpc.newBlockingStub(managedChannel); - return blockingStub.getStudent(StudentRequest.newBuilder().setId(1).build()); + if (blockingStub == null) { + throw new RuntimeException("Failed to create blocking stub"); + } + StudentResponse response = blockingStub.getStudent(StudentRequest.newBuilder().setId(1).build()); + if (response == null) { + throw new RuntimeException("Failed to get student"); + } + return response; }catch (SSLException e) { throw new RuntimeException("Failed to create SSL context", e); } finally { @@ -74,7 +81,14 @@ public StudentResponse GetStudentDefaultVersion(Map headers) thr .intercept(interceptor) .build(); StudentServiceDefaultVersionGrpc.StudentServiceBlockingStub blockingStub = StudentServiceDefaultVersionGrpc.newBlockingStub(managedChannel); - return blockingStub.getStudent(StudentRequest.newBuilder().setId(1).build()); + if (blockingStub == null) { + throw new RuntimeException("Failed to create blocking stub"); + } + StudentResponse response = blockingStub.getStudent(StudentRequest.newBuilder().setId(1).build()); + if (response == null) { + throw new RuntimeException("Failed to get student"); + } + return response; }catch (SSLException e) { throw new RuntimeException("Failed to create SSL context", e); } finally { From fe87a63a34068358af0e662a1d0516eef4c89dc9 Mon Sep 17 00:00:00 2001 From: DinethH Date: Tue, 30 Apr 2024 09:30:49 +0530 Subject: [PATCH 21/29] Slight change to test --- test/cucumber-tests/scripts/setup-hosts.sh | 1 + .../src/test/java/org/wso2/apk/integration/api/BaseSteps.java | 2 +- .../integration/utils/clients/SimpleGRPCStudentClient.java | 4 ++++ 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/test/cucumber-tests/scripts/setup-hosts.sh b/test/cucumber-tests/scripts/setup-hosts.sh index 5fb451248..2f541a7b5 100644 --- a/test/cucumber-tests/scripts/setup-hosts.sh +++ b/test/cucumber-tests/scripts/setup-hosts.sh @@ -6,6 +6,7 @@ kubectl wait deployment/backend-retry-deployment -n apk-integration-test --for=c kubectl wait deployment/dynamic-backend -n apk-integration-test --for=condition=available --timeout=600s kubectl wait deployment/interceptor-service-deployment -n apk-integration-test --for=condition=available --timeout=600s kubectl wait deployment/graphql-faker -n apk-integration-test --for=condition=available --timeout=600s +kubectl wait deployment/grpc-backend -n apk-integration-test --for=condition=available --timeout=600s kubectl wait --timeout=5m -n apk-integration-test deployment/apk-test-setup-wso2-apk-adapter-deployment --for=condition=Available kubectl wait --timeout=15m -n apk-integration-test deployment/apk-test-setup-wso2-apk-gateway-runtime-deployment --for=condition=Available IP=$(kubectl get svc apk-test-setup-wso2-apk-gateway-service -n apk-integration-test --output jsonpath='{.status.loadBalancer.ingress[0].ip}') diff --git a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/BaseSteps.java b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/BaseSteps.java index cba1b1530..d31b2c245 100644 --- a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/BaseSteps.java +++ b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/BaseSteps.java @@ -180,7 +180,7 @@ public void GetStudent(String arg0, int arg1) throws StatusRuntimeException { public void GetStudentDefaultVersion(String arg0, int arg1) throws StatusRuntimeException { try { SimpleGRPCStudentClient grpcStudentClient = new SimpleGRPCStudentClient(arg0, arg1); - sharedContext.setStudentResponse(grpcStudentClient.GetStudent(sharedContext.getHeaders())); + sharedContext.setStudentResponse(grpcStudentClient.GetStudentDefaultVersion(sharedContext.getHeaders())); } catch (StatusRuntimeException e) { if (e.getStatus().getCode()== Status.Code.PERMISSION_DENIED){ sharedContext.setGrpcErrorCode(403); diff --git a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/SimpleGRPCStudentClient.java b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/SimpleGRPCStudentClient.java index c7f04210b..0f7ba0b98 100644 --- a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/SimpleGRPCStudentClient.java +++ b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/utils/clients/SimpleGRPCStudentClient.java @@ -44,10 +44,12 @@ public StudentResponse GetStudent(Map headers) throws StatusRunt .build(); StudentServiceGrpc.StudentServiceBlockingStub blockingStub = StudentServiceGrpc.newBlockingStub(managedChannel); if (blockingStub == null) { + log.error("Failed to create blocking stub"); throw new RuntimeException("Failed to create blocking stub"); } StudentResponse response = blockingStub.getStudent(StudentRequest.newBuilder().setId(1).build()); if (response == null) { + log.error("Failed to get student"); throw new RuntimeException("Failed to get student"); } return response; @@ -82,10 +84,12 @@ public StudentResponse GetStudentDefaultVersion(Map headers) thr .build(); StudentServiceDefaultVersionGrpc.StudentServiceBlockingStub blockingStub = StudentServiceDefaultVersionGrpc.newBlockingStub(managedChannel); if (blockingStub == null) { + log.error("Failed to create blocking stub"); throw new RuntimeException("Failed to create blocking stub"); } StudentResponse response = blockingStub.getStudent(StudentRequest.newBuilder().setId(1).build()); if (response == null) { + log.error("Failed to get student"); throw new RuntimeException("Failed to get student"); } return response; From ddd006652193debddf480b28f8bf0369755ba661 Mon Sep 17 00:00:00 2001 From: DinethH Date: Thu, 2 May 2024 11:34:04 +0530 Subject: [PATCH 22/29] fixed api-definition-endpoint --- .../server/swagger/SwaggerServerHandler.java | 39 ++++++++++++++++--- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/gateway/enforcer/org.wso2.apk.enforcer/src/main/java/org/wso2/apk/enforcer/server/swagger/SwaggerServerHandler.java b/gateway/enforcer/org.wso2.apk.enforcer/src/main/java/org/wso2/apk/enforcer/server/swagger/SwaggerServerHandler.java index 16538f4d0..c97326e8b 100644 --- a/gateway/enforcer/org.wso2.apk.enforcer/src/main/java/org/wso2/apk/enforcer/server/swagger/SwaggerServerHandler.java +++ b/gateway/enforcer/org.wso2.apk.enforcer/src/main/java/org/wso2/apk/enforcer/server/swagger/SwaggerServerHandler.java @@ -16,8 +16,11 @@ import org.wso2.apk.enforcer.constants.AdminConstants; import org.wso2.apk.enforcer.constants.HttpConstants; import org.wso2.apk.enforcer.models.ResponsePayload; + import java.util.HashMap; import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class SwaggerServerHandler extends SimpleChannelInboundHandler { @@ -43,17 +46,32 @@ public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Excep boolean isSwagger = false; - String [] params = request.uri().split("/"); - final String basePath = "/" + params[1] + "/" + params[2]; - final String vHost = params[3].split("\\?")[0]; - final String queryParam = params[3].split("\\?")[1]; + //check if it's GRPC request using the header + String header = request.headers().get("ApiType"); + String[] params = request.uri().split("/"); + final String basePath; + final String vHost; + final String queryParam; + final String version; + if (header != null && header.equals("GRPC")) { + basePath = "/" + params[1]; + vHost = params[2].split("\\?")[0]; + queryParam = params[2].split("\\?")[1]; + version = extractVersionFromGrpcBasePath(params[1]); + } else { + basePath = "/" + params[1] + "/" + params[2]; + vHost = params[3].split("\\?")[0]; + queryParam = params[3].split("\\?")[1]; + version = params[2]; + } + if (APIDefinitionConstants.SWAGGER_DEFINITION.equalsIgnoreCase(queryParam)) { isSwagger = true; } if(isSwagger){ // load the corresponding swagger definition from the API name - byte[] apiDefinition = apiFactory.getAPIDefinition(basePath, params[2], vHost); + byte[] apiDefinition = apiFactory.getAPIDefinition(basePath, version, vHost); if (apiDefinition == null || apiDefinition.length == 0) { String error = AdminConstants.ErrorMessages.NOT_FOUND; responsePayload = new ResponsePayload(); @@ -91,4 +109,15 @@ private void buildAndSendResponse(ChannelHandlerContext ctx, ResponsePayload res httpResponse.headers().set(HTTP.CONTENT_LEN, httpResponse.content().readableBytes()); ctx.writeAndFlush(httpResponse); } + + private static String extractVersionFromGrpcBasePath(String input) { + // Pattern to match '.v' followed by digits and optional periods followed by more digits + Pattern pattern = Pattern.compile("\\.v\\d+(\\.\\d+)*"); + Matcher matcher = pattern.matcher(input); + + if (matcher.find()) { + return matcher.group().substring(1); // Returns the matched version + } + return ""; + } } From bbaa071e8b563ce400bbf95c45767723dabec219 Mon Sep 17 00:00:00 2001 From: DinethH Date: Thu, 2 May 2024 12:47:17 +0530 Subject: [PATCH 23/29] fixed api-definition-endpoint --- .../apk/enforcer/server/swagger/SwaggerServerHandler.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/gateway/enforcer/org.wso2.apk.enforcer/src/main/java/org/wso2/apk/enforcer/server/swagger/SwaggerServerHandler.java b/gateway/enforcer/org.wso2.apk.enforcer/src/main/java/org/wso2/apk/enforcer/server/swagger/SwaggerServerHandler.java index c97326e8b..04d6cd220 100644 --- a/gateway/enforcer/org.wso2.apk.enforcer/src/main/java/org/wso2/apk/enforcer/server/swagger/SwaggerServerHandler.java +++ b/gateway/enforcer/org.wso2.apk.enforcer/src/main/java/org/wso2/apk/enforcer/server/swagger/SwaggerServerHandler.java @@ -53,7 +53,9 @@ public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Excep final String vHost; final String queryParam; final String version; - if (header != null && header.equals("GRPC")) { + //if len params is 3, then it's a GRPC request else other + final String type = params.length == 3 ? "GRPC" : "REST"; + if (type.equals("GRPC")) { basePath = "/" + params[1]; vHost = params[2].split("\\?")[0]; queryParam = params[2].split("\\?")[1]; From c2a09975aadf6ea10e3a058345fb5821cdecf829 Mon Sep 17 00:00:00 2001 From: DinethH Date: Thu, 2 May 2024 13:48:36 +0530 Subject: [PATCH 24/29] changed path match type to Exact --- runtime/config-deployer-service/ballerina/APIClient.bal | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/config-deployer-service/ballerina/APIClient.bal b/runtime/config-deployer-service/ballerina/APIClient.bal index 5c86fc3bc..9151f2119 100644 --- a/runtime/config-deployer-service/ballerina/APIClient.bal +++ b/runtime/config-deployer-service/ballerina/APIClient.bal @@ -1057,7 +1057,7 @@ public class APIClient { private isolated function retrieveGRPCRouteMatch(APKOperations apiOperation) returns model:GRPCRouteMatch { model:GRPCRouteMatch grpcRouteMatch = { method: { - 'type: "RegularExpression", + 'type: "Exact", 'service: apiOperation.target, method: apiOperation.verb } From ed46c2c316adffc8d6e6db9adbbebe76dd9c3780 Mon Sep 17 00:00:00 2001 From: DinethH Date: Thu, 2 May 2024 16:27:45 +0530 Subject: [PATCH 25/29] add cucumber test for api definition endpoint --- .../wso2/apk/integration/api/BaseSteps.java | 6 ++++- .../src/test/resources/tests/api/GRPC.feature | 23 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/BaseSteps.java b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/BaseSteps.java index d31b2c245..7f4e7826a 100644 --- a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/BaseSteps.java +++ b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/BaseSteps.java @@ -112,6 +112,11 @@ public void theStudentResponseBodyShouldContainNameAndAge(String arg0, int arg1) public void theResponseBodyShouldContain(String expectedText) throws IOException { Assert.assertTrue(sharedContext.getResponseBody().contains(expectedText), "Actual response body: " + sharedContext.getResponseBody()); } + @Then("the response body should contain endpoint definition for student.proto") + public void theResponseBodyShouldContainEndpointDefinition() throws IOException { + String expectedText = "{\"apiDefinition\":\"syntax = \\\"proto3\\\";\\n\\noption java_multiple_files = true;\\noption java_package = \\\"org.example\\\";\\npackage dineth.grpc.api.v1.student;\\n\\nservice StudentService {\\n rpc GetStudent(StudentRequest) returns (StudentResponse) {};\\n rpc GetStudentStream(StudentRequest) returns (stream StudentResponse) {};\\n rpc SendStudentStream(stream StudentRequest) returns (StudentResponse) {};\\n rpc SendAndGetStudentStream(stream StudentRequest) returns (stream StudentResponse) {}\\n}\\n\\nmessage StudentRequest {\\n int32 id = 3;\\n}\\n\\nmessage StudentResponse {\\n string name = 1;\\n int32 age = 2;\\n}\\n\"}"; + Assert.assertTrue(sharedContext.getResponseBody().contains(expectedText), "Actual response body: " + sharedContext.getResponseBody()); + } @Then("the response body should not contain {string}") public void theResponseBodyShouldNotContain(String expectedText) throws IOException { Assert.assertFalse(sharedContext.getResponseBody().contains(expectedText), "Actual response body: " + sharedContext.getResponseBody()); @@ -134,7 +139,6 @@ public void theResponseStatusCodeShouldBe(int expectedStatusCode) throws IOExcep @Then("the grpc error response status code should be {int}") public void theGrpcErrorResponseStatusCodeShouldBe(int expectedStatusCode) throws IOException { - int actualStatusCode = sharedContext.getGrpcErrorCode(); Assert.assertEquals(actualStatusCode, expectedStatusCode); } diff --git a/test/cucumber-tests/src/test/resources/tests/api/GRPC.feature b/test/cucumber-tests/src/test/resources/tests/api/GRPC.feature index c6b7ebcd8..8ebc10e4d 100644 --- a/test/cucumber-tests/src/test/resources/tests/api/GRPC.feature +++ b/test/cucumber-tests/src/test/resources/tests/api/GRPC.feature @@ -20,6 +20,29 @@ Feature: Generating APK conf for gRPC API | 500 | And the student response body should contain name: "Dineth" age: 10 + Scenario: Undeploy API + Given The system is ready + And I have a valid subscription + When I undeploy the API whose ID is "grpc-basic-api" + Then the response status code should be 202 + + Scenario: Checking api-definition endpoint to get proto file + Given The system is ready + And I have a valid subscription + When I use the APK Conf file "artifacts/apk-confs/grpc/grpc.apk-conf" + And the definition file "artifacts/definitions/student.proto" + And make the API deployment request + Then the response status code should be 200 + Then I set headers + | Authorization | bearer ${accessToken} | + | Host | default.gw.wso2.com | + And I send "GET" request to "https://default.gw.wso2.com:9095/dineth.grpc.api.v1/api-definition/" with body "" + And I eventually receive 200 response code, not accepting + | 429 | + | 500 | + And the response body should contain endpoint definition for student.proto + + Scenario: Undeploy API Given The system is ready And I have a valid subscription From 2282d778d8631b0009ef5b9378557e8216094b6f Mon Sep 17 00:00:00 2001 From: DinethH Date: Fri, 3 May 2024 11:48:51 +0530 Subject: [PATCH 26/29] Add name generation for apk-conf --- .../config-deployer-service/ballerina/APIClient.bal | 7 ++++++- .../java/org/wso2/apk/config/RuntimeAPICommonUtil.java | 10 ++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/runtime/config-deployer-service/ballerina/APIClient.bal b/runtime/config-deployer-service/ballerina/APIClient.bal index 9151f2119..549ae6423 100644 --- a/runtime/config-deployer-service/ballerina/APIClient.bal +++ b/runtime/config-deployer-service/ballerina/APIClient.bal @@ -46,7 +46,7 @@ public class APIClient { encodedString = encodedString.substring(0, encodedString.length() - 1); } APKConf apkConf = { - name: api.getName(), + name: api.getType() == API_TYPE_GRPC ? self.getUniqueNameForGrpcApi(api.getName()) : api.getName(), basePath: api.getBasePath().length() > 0 ? api.getBasePath() : encodedString, version: api.getVersion(), 'type: api.getType() == "" ? API_TYPE_REST : api.getType().toUpperAscii() @@ -1679,6 +1679,11 @@ public class APIClient { return hashedValue.toBase16(); } + public isolated function getUniqueNameForGrpcApi(string concatanatedServices) returns string { + byte[] hashedValue = crypto:hashSha1(concatanatedServices.toBytes()); + return hashedValue.toBase16(); + } + public isolated function retrieveHttpRouteRefName(APKConf apkConf, string 'type, commons:Organization organization) returns string { return uuid:createType1AsString(); } diff --git a/runtime/config-deployer-service/java/src/main/java/org/wso2/apk/config/RuntimeAPICommonUtil.java b/runtime/config-deployer-service/java/src/main/java/org/wso2/apk/config/RuntimeAPICommonUtil.java index ebca7e8e4..c93073e6d 100644 --- a/runtime/config-deployer-service/java/src/main/java/org/wso2/apk/config/RuntimeAPICommonUtil.java +++ b/runtime/config-deployer-service/java/src/main/java/org/wso2/apk/config/RuntimeAPICommonUtil.java @@ -148,7 +148,11 @@ private static API getGRPCAPIFromProtoDefinition(String definition) { API api = new API(); api.setBasePath("/"+protoParser.protoFile.basePath); api.setVersion(protoParser.protoFile.version); + StringBuilder apiName = new StringBuilder(); + List sortedServices = new ArrayList<>(); + for (ProtoParser.Service service : protoParser.getServices()) { + sortedServices.add(service.name); for (String method : service.methods) { URITemplate uriTemplate = new URITemplate(); uriTemplate.setUriTemplate(protoParser.protoFile.packageName + "." + service.name); @@ -156,6 +160,12 @@ private static API getGRPCAPIFromProtoDefinition(String definition) { uriTemplates.add(uriTemplate); } } + sortedServices.sort(String::compareTo); + for (String service : sortedServices) { + apiName.append(service).append("-"); + } + apiName.deleteCharAt(apiName.length() - 1); + api.setName(apiName.toString()); api.setUriTemplates(uriTemplates.toArray(new URITemplate[uriTemplates.size()])); return api; From af1445e41d97322aacba8db08b97d9cf63a3c666 Mon Sep 17 00:00:00 2001 From: DinethH Date: Mon, 6 May 2024 10:26:46 +0530 Subject: [PATCH 27/29] prevent GRPC API from propagating to CP --- adapter/internal/operator/controllers/dp/api_controller.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/adapter/internal/operator/controllers/dp/api_controller.go b/adapter/internal/operator/controllers/dp/api_controller.go index ed541a472..4a18a7b58 100644 --- a/adapter/internal/operator/controllers/dp/api_controller.go +++ b/adapter/internal/operator/controllers/dp/api_controller.go @@ -529,6 +529,9 @@ func (apiReconciler *APIReconciler) resolveAPIRefs(ctx context.Context, api dpv1 func isAPIPropagatable(apiState *synchronizer.APIState) bool { validOrgs := []string{"carbon.super"} + if apiState.APIDefinition.Spec.APIType == "GRPC" { + return false + } // System APIs should not be propagated to CP if apiState.APIDefinition.Spec.SystemAPI { return false From aaa9180d79eb1fefaac2090cb34d301a4f3063f2 Mon Sep 17 00:00:00 2001 From: DinethH Date: Wed, 8 May 2024 17:34:38 +0530 Subject: [PATCH 28/29] add cucumber test for mtls --- .../wso2/apk/integration/api/BaseSteps.java | 10 ++++- ...th_mtls_mandatory_oauth2_disabled.apk-conf | 36 +++++++++++++++++ ...ith_mtls_optional_oauth2_optional.apk-conf | 36 +++++++++++++++++ .../test/resources/tests/api/GRPCMTLS.feature | 39 +++++++++++++++++++ 4 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 test/cucumber-tests/src/test/resources/artifacts/apk-confs/grpc/grpc_with_mtls_mandatory_oauth2_disabled.apk-conf create mode 100644 test/cucumber-tests/src/test/resources/artifacts/apk-confs/grpc/grpc_with_mtls_optional_oauth2_optional.apk-conf create mode 100644 test/cucumber-tests/src/test/resources/tests/api/GRPCMTLS.feature diff --git a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/BaseSteps.java b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/BaseSteps.java index 7f4e7826a..7663f9dee 100644 --- a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/BaseSteps.java +++ b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/BaseSteps.java @@ -174,8 +174,16 @@ public void GetStudent(String arg0, int arg1) throws StatusRuntimeException { } catch (StatusRuntimeException e) { if (e.getStatus().getCode()== Status.Code.PERMISSION_DENIED){ sharedContext.setGrpcErrorCode(403); + } else if (e.getStatus().getCode()== Status.Code.UNIMPLEMENTED){ + sharedContext.setGrpcErrorCode(501); + } else if (e.getStatus().getCode()== Status.Code.UNAVAILABLE){ + sharedContext.setGrpcErrorCode(503); + } else if (e.getStatus().getCode()== Status.Code.NOT_FOUND){ + sharedContext.setGrpcErrorCode(404); + } else if (e.getStatus().getCode()== Status.Code.UNAUTHENTICATED){ + sharedContext.setGrpcErrorCode(401); } else { - logger.error(e.getMessage()); + logger.error(e.getMessage() + "code: " + e.getStatus().getCode()); } } } diff --git a/test/cucumber-tests/src/test/resources/artifacts/apk-confs/grpc/grpc_with_mtls_mandatory_oauth2_disabled.apk-conf b/test/cucumber-tests/src/test/resources/artifacts/apk-confs/grpc/grpc_with_mtls_mandatory_oauth2_disabled.apk-conf new file mode 100644 index 000000000..8341e9095 --- /dev/null +++ b/test/cucumber-tests/src/test/resources/artifacts/apk-confs/grpc/grpc_with_mtls_mandatory_oauth2_disabled.apk-conf @@ -0,0 +1,36 @@ +--- +name: "demo-grpc-api" +basePath: "/dineth.grpc.api" +version: "v1" +type: "GRPC" +id: "grpc-mtls-mandatory-oauth2-disabled" +endpointConfigurations: + production: + endpoint: "http://grpc-backend:6565" +defaultVersion: false +subscriptionValidation: false +operations: +- target: "student.StudentService" + verb: "GetStudent" + secured: true + scopes: [] +- target: "student.StudentService" + verb: "GetStudentStream" + secured: true + scopes: [] +- target: "student.StudentService" + verb: "SendStudentStream" + secured: true + scopes: [] +- target: "student.StudentService" + verb: "SendAndGetStudentStream" + secured: true + scopes: [] +authentication: + - authType: OAuth2 + enabled: false + - authType: mTLS + required: mandatory + certificates: + - name: mtls-test-configmap + key: tls.crt diff --git a/test/cucumber-tests/src/test/resources/artifacts/apk-confs/grpc/grpc_with_mtls_optional_oauth2_optional.apk-conf b/test/cucumber-tests/src/test/resources/artifacts/apk-confs/grpc/grpc_with_mtls_optional_oauth2_optional.apk-conf new file mode 100644 index 000000000..57a849c5a --- /dev/null +++ b/test/cucumber-tests/src/test/resources/artifacts/apk-confs/grpc/grpc_with_mtls_optional_oauth2_optional.apk-conf @@ -0,0 +1,36 @@ +--- +name: "demo-grpc-api" +basePath: "/dineth.grpc.api" +version: "v1" +type: "GRPC" +id: "grpc-mtls-optional-oauth2-optional" +endpointConfigurations: + production: + endpoint: "http://grpc-backend:6565" +defaultVersion: false +subscriptionValidation: false +operations: +- target: "student.StudentService" + verb: "GetStudent" + secured: true + scopes: [] +- target: "student.StudentService" + verb: "GetStudentStream" + secured: true + scopes: [] +- target: "student.StudentService" + verb: "SendStudentStream" + secured: true + scopes: [] +- target: "student.StudentService" + verb: "SendAndGetStudentStream" + secured: true + scopes: [] +authentication: + - authType: OAuth2 + required: optional + - authType: mTLS + required: optional + certificates: + - name: mtls-test-configmap + key: tls.crt diff --git a/test/cucumber-tests/src/test/resources/tests/api/GRPCMTLS.feature b/test/cucumber-tests/src/test/resources/tests/api/GRPCMTLS.feature new file mode 100644 index 000000000..05548f7ca --- /dev/null +++ b/test/cucumber-tests/src/test/resources/tests/api/GRPCMTLS.feature @@ -0,0 +1,39 @@ +Feature: Test mTLS between client and gateway with client certificate sent in header + Scenario: Test API with mandatory mTLS and OAuth2 disabled + Given The system is ready + And I have a valid token with a client certificate "invalid-cert.txt" + When I use the APK Conf file "artifacts/apk-confs/grpc/grpc_with_mtls_mandatory_oauth2_disabled.apk-conf" + And the definition file "artifacts/definitions/student.proto" + And make the API deployment request + Then the response status code should be 200 + Then I set headers + | X-WSO2-CLIENT-CERTIFICATE | ${clientCertificate} | + And I make grpc request GetStudent to "default.gw.wso2.com" with port 9095 + And I eventually receive 200 response code, not accepting + | 401 | + And the student response body should contain name: "Dineth" age: 10 + + Scenario: Undeploy API + Given The system is ready + And I have a valid subscription + When I undeploy the API whose ID is "grpc-mtls-mandatory-oauth2-disabled" + Then the response status code should be 202 + + Scenario: Test optional mTLS and optional OAuth2 with an invalid client certificate and invalid token in header + Given The system is ready + And I have a valid token with a client certificate "invalid-cert.txt" + When I use the APK Conf file "artifacts/apk-confs/grpc/grpc_with_mtls_optional_oauth2_optional.apk-conf" + And the definition file "artifacts/definitions/student.proto" + And make the API deployment request + Then the response status code should be 200 + Then I set headers + | X-WSO2-CLIENT-CERTIFICATE | ${clientCertificate} | + | Authorization | bearer {accessToken} | + And I make grpc request GetStudent to "default.gw.wso2.com" with port 9095 + And the grpc error response status code should be 401 + + Scenario: Undeploy API + Given The system is ready + And I have a valid subscription + When I undeploy the API whose ID is "grpc-mtls-optional-oauth2-optional" + Then the response status code should be 202 From b43369023e8cdc2a2a9b166ba838f14b7df1b101 Mon Sep 17 00:00:00 2001 From: DinethH Date: Thu, 9 May 2024 10:09:42 +0530 Subject: [PATCH 29/29] changed gRPC status code --- .../wso2/apk/integration/api/BaseSteps.java | 30 ++++++------------- .../apk/integration/api/SharedContext.java | 11 ++++--- .../src/test/resources/tests/api/GRPC.feature | 22 ++++---------- .../test/resources/tests/api/GRPCMTLS.feature | 5 ++-- 4 files changed, 22 insertions(+), 46 deletions(-) diff --git a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/BaseSteps.java b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/BaseSteps.java index 7663f9dee..faf39122f 100644 --- a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/BaseSteps.java +++ b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/BaseSteps.java @@ -137,9 +137,9 @@ public void theResponseStatusCodeShouldBe(int expectedStatusCode) throws IOExcep Assert.assertEquals(actualStatusCode, expectedStatusCode); } - @Then("the grpc error response status code should be {int}") - public void theGrpcErrorResponseStatusCodeShouldBe(int expectedStatusCode) throws IOException { - int actualStatusCode = sharedContext.getGrpcErrorCode(); + @Then("the gRPC response status code should be {int}") + public void theGrpcResponseStatusCodeShouldBe(int expectedStatusCode) throws IOException { + int actualStatusCode = sharedContext.getGrpcStatusCode(); Assert.assertEquals(actualStatusCode, expectedStatusCode); } @@ -171,20 +171,10 @@ public void GetStudent(String arg0, int arg1) throws StatusRuntimeException { try { SimpleGRPCStudentClient grpcStudentClient = new SimpleGRPCStudentClient(arg0, arg1); sharedContext.setStudentResponse(grpcStudentClient.GetStudent(sharedContext.getHeaders())); + sharedContext.setGrpcStatusCode(0); } catch (StatusRuntimeException e) { - if (e.getStatus().getCode()== Status.Code.PERMISSION_DENIED){ - sharedContext.setGrpcErrorCode(403); - } else if (e.getStatus().getCode()== Status.Code.UNIMPLEMENTED){ - sharedContext.setGrpcErrorCode(501); - } else if (e.getStatus().getCode()== Status.Code.UNAVAILABLE){ - sharedContext.setGrpcErrorCode(503); - } else if (e.getStatus().getCode()== Status.Code.NOT_FOUND){ - sharedContext.setGrpcErrorCode(404); - } else if (e.getStatus().getCode()== Status.Code.UNAUTHENTICATED){ - sharedContext.setGrpcErrorCode(401); - } else { - logger.error(e.getMessage() + "code: " + e.getStatus().getCode()); - } + sharedContext.setGrpcStatusCode(e.getStatus().getCode().value()); + logger.error(e.getMessage() + " Status code: " + e.getStatus().getCode().value()) ; } } @@ -193,12 +183,10 @@ public void GetStudentDefaultVersion(String arg0, int arg1) throws StatusRuntime try { SimpleGRPCStudentClient grpcStudentClient = new SimpleGRPCStudentClient(arg0, arg1); sharedContext.setStudentResponse(grpcStudentClient.GetStudentDefaultVersion(sharedContext.getHeaders())); + sharedContext.setGrpcStatusCode(0); } catch (StatusRuntimeException e) { - if (e.getStatus().getCode()== Status.Code.PERMISSION_DENIED){ - sharedContext.setGrpcErrorCode(403); - } else { - logger.error(e.getMessage()); - } + sharedContext.setGrpcStatusCode(e.getStatus().getCode().value()); + logger.error(e.getMessage() + " Status code: " + e.getStatus().getCode().value()) ; } } diff --git a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/SharedContext.java b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/SharedContext.java index 2b7c88bb0..66bc9bfbc 100644 --- a/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/SharedContext.java +++ b/test/cucumber-tests/src/test/java/org/wso2/apk/integration/api/SharedContext.java @@ -18,7 +18,6 @@ package org.wso2.apk.integration.api; import org.apache.http.HttpResponse; -import org.wso2.apk.integration.utils.clients.SimpleGRPCStudentClient; import org.wso2.apk.integration.utils.clients.SimpleHTTPClient; import org.wso2.apk.integration.utils.clients.studentGrpcClient.StudentResponse; @@ -36,7 +35,7 @@ public class SharedContext { private String accessToken; private HttpResponse response; private String responseBody; - private int grpcErrorCode; + private int grpcStatusCode; private HashMap valueStore = new HashMap<>(); private HashMap headers = new HashMap<>(); @@ -56,11 +55,11 @@ public void setAccessToken(String accessToken) { this.accessToken = accessToken; } - public int getGrpcErrorCode() { - return grpcErrorCode; + public int getGrpcStatusCode() { + return grpcStatusCode; } - public void setGrpcErrorCode(int grpcErrorCode) { - this.grpcErrorCode = grpcErrorCode; + public void setGrpcStatusCode(int grpcStatusCode) { + this.grpcStatusCode = grpcStatusCode; } public HttpResponse getResponse() { diff --git a/test/cucumber-tests/src/test/resources/tests/api/GRPC.feature b/test/cucumber-tests/src/test/resources/tests/api/GRPC.feature index 8ebc10e4d..873ad7c48 100644 --- a/test/cucumber-tests/src/test/resources/tests/api/GRPC.feature +++ b/test/cucumber-tests/src/test/resources/tests/api/GRPC.feature @@ -15,9 +15,7 @@ Feature: Generating APK conf for gRPC API Then I set headers | Authorization | bearer ${accessToken} | And I make grpc request GetStudent to "default.gw.wso2.com" with port 9095 - And I eventually receive 200 response code, not accepting - | 429 | - | 500 | + And the gRPC response status code should be 0 And the student response body should contain name: "Dineth" age: 10 Scenario: Undeploy API @@ -57,9 +55,7 @@ Feature: Generating APK conf for gRPC API And make the API deployment request Then the response status code should be 200 And I make grpc request GetStudent to "default.gw.wso2.com" with port 9095 - And I eventually receive 200 response code, not accepting - | 429 | - | 500 | + And the gRPC response status code should be 0 And the student response body should contain name: "Dineth" age: 10 Scenario: Undeploy API @@ -78,15 +74,13 @@ Feature: Generating APK conf for gRPC API Then I set headers | Authorization | bearer ${accessToken} | And I make grpc request GetStudent to "default.gw.wso2.com" with port 9095 - And the grpc error response status code should be 403 + And the gRPC response status code should be 7 Given I have a valid subscription with scopes | wso2 | Then I set headers | Authorization | bearer ${accessToken} | And I make grpc request GetStudent to "default.gw.wso2.com" with port 9095 - And I eventually receive 200 response code, not accepting - | 429 | - | 500 | + And the gRPC response status code should be 0 And the student response body should contain name: "Dineth" age: 10 Scenario: Undeploy API @@ -105,17 +99,13 @@ Feature: Generating APK conf for gRPC API Then I set headers | Authorization | bearer ${accessToken} | And I make grpc request GetStudent to "default.gw.wso2.com" with port 9095 - And I eventually receive 200 response code, not accepting - | 429 | - | 500 | + And the gRPC response status code should be 0 And the student response body should contain name: "Dineth" age: 10 Given I have a valid subscription Then I set headers | Authorization | bearer ${accessToken} | And I make grpc request GetStudent default version to "default.gw.wso2.com" with port 9095 - And I eventually receive 200 response code, not accepting - | 429 | - | 500 | + And the gRPC response status code should be 0 And the student response body should contain name: "Dineth" age: 10 Scenario: Undeploy API diff --git a/test/cucumber-tests/src/test/resources/tests/api/GRPCMTLS.feature b/test/cucumber-tests/src/test/resources/tests/api/GRPCMTLS.feature index 05548f7ca..87fa0fac0 100644 --- a/test/cucumber-tests/src/test/resources/tests/api/GRPCMTLS.feature +++ b/test/cucumber-tests/src/test/resources/tests/api/GRPCMTLS.feature @@ -9,8 +9,7 @@ Feature: Test mTLS between client and gateway with client certificate sent in he Then I set headers | X-WSO2-CLIENT-CERTIFICATE | ${clientCertificate} | And I make grpc request GetStudent to "default.gw.wso2.com" with port 9095 - And I eventually receive 200 response code, not accepting - | 401 | + And the gRPC response status code should be 0 And the student response body should contain name: "Dineth" age: 10 Scenario: Undeploy API @@ -30,7 +29,7 @@ Feature: Test mTLS between client and gateway with client certificate sent in he | X-WSO2-CLIENT-CERTIFICATE | ${clientCertificate} | | Authorization | bearer {accessToken} | And I make grpc request GetStudent to "default.gw.wso2.com" with port 9095 - And the grpc error response status code should be 401 + And the gRPC response status code should be 16 Scenario: Undeploy API Given The system is ready