use of io.github.microcks.domain.Response in project microcks by microcks.
the class PostmanCollectionImporter method getMessageDefinitionsV2.
private List<Exchange> getMessageDefinitionsV2(Service service, Operation operation) {
Map<Request, Response> result = new HashMap<Request, Response>();
Iterator<JsonNode> items = collection.path("item").elements();
while (items.hasNext()) {
JsonNode item = items.next();
result.putAll(getMessageDefinitionsV2("", item, operation));
}
// Adapt map to list of Exchanges.
return result.entrySet().stream().map(entry -> new RequestResponsePair(entry.getKey(), entry.getValue())).collect(Collectors.toList());
}
use of io.github.microcks.domain.Response in project microcks by microcks.
the class HttpTestRunner method runTest.
@Override
public List<TestReturn> runTest(Service service, Operation operation, TestResult testResult, List<Request> requests, String endpointUrl, HttpMethod method) throws URISyntaxException, IOException {
if (log.isDebugEnabled()) {
log.debug("Launching test run on " + endpointUrl + " for " + requests.size() + " request(s)");
}
if (requests.isEmpty()) {
return null;
}
// Initialize result container.
List<TestReturn> result = new ArrayList<TestReturn>();
for (Request request : requests) {
// Reset status code, message and request each time.
int code = TestReturn.SUCCESS_CODE;
String message = null;
String customizedEndpointUrl = endpointUrl;
if (service.getType().equals(ServiceType.REST)) {
String operationName = operation.getName();
// Name may start with verb, remove it if present.
if (operationName.indexOf(' ') > 0 && operationName.indexOf(' ') < operationName.length()) {
operationName = operationName.split(" ")[1];
}
customizedEndpointUrl += URIBuilder.buildURIFromPattern(operationName, request.getQueryParameters());
log.debug("Using customized endpoint url: " + customizedEndpointUrl);
}
ClientHttpRequest httpRequest = clientHttpRequestFactory.createRequest(new URI(customizedEndpointUrl), method);
// Set headers to request if any. Start with those coming from request itself.
// Add or override existing headers with test specific ones for operation and globals.
Set<Header> headers = new HashSet<>();
if (request.getHeaders() != null) {
headers.addAll(request.getHeaders());
}
if (testResult.getOperationsHeaders() != null) {
if (testResult.getOperationsHeaders().getGlobals() != null) {
headers.addAll(testResult.getOperationsHeaders().getGlobals());
}
if (testResult.getOperationsHeaders().get(operation.getName()) != null) {
headers.addAll(testResult.getOperationsHeaders().get(operation.getName()));
}
}
if (headers.size() > 0) {
for (Header header : headers) {
log.debug("Adding header " + header.getName() + " to request");
httpRequest.getHeaders().add(header.getName(), buildValue(header.getValues()));
}
// Update request headers for traceability of possibly added ones.
request.setHeaders(headers);
}
// Now manage specific authorization headers if there's a secret.
if (secret != null) {
addAuthorizationHeadersFromSecret(httpRequest, request, secret);
}
// Allow extensions to realize some pre-processing of request.
prepareRequest(request);
// If there's input content, add it to request.
if (request.getContent() != null) {
// Update request content with rendered body if necessary.
request.setContent(TestRunnerCommons.renderRequestContent(request, headers));
log.trace("Sending following request content: " + request.getContent());
httpRequest.getBody().write(request.getContent().getBytes());
}
// Actually execute request.
long startTime = System.currentTimeMillis();
ClientHttpResponse httpResponse = null;
try {
httpResponse = httpRequest.execute();
} catch (IOException ioe) {
log.error("IOException while executing request " + request.getName() + " on " + endpointUrl, ioe);
code = TestReturn.FAILURE_CODE;
message = ioe.getMessage();
}
long duration = System.currentTimeMillis() - startTime;
// Extract and store response body so that stream may not be consumed more than o1 time ;-)
String responseContent = null;
if (httpResponse != null) {
StringWriter writer = new StringWriter();
IOUtils.copy(httpResponse.getBody(), writer);
responseContent = writer.toString();
}
// If still in success, check if http code is out of correct ranges (20x and 30x).
if (code == TestReturn.SUCCESS_CODE) {
code = extractTestReturnCode(service, operation, request, httpResponse, responseContent);
message = extractTestReturnMessage(service, operation, request, httpResponse);
}
// Create a Response object for returning.
Response response = new Response();
if (httpResponse != null) {
response.setContent(responseContent);
response.setStatus(String.valueOf(httpResponse.getRawStatusCode()));
log.debug("Response Content-Type: " + httpResponse.getHeaders().getContentType());
if (httpResponse.getHeaders().getContentType() != null) {
response.setMediaType(httpResponse.getHeaders().getContentType().toString());
}
headers = buildHeaders(httpResponse);
if (headers != null) {
response.setHeaders(headers);
}
httpResponse.close();
}
result.add(new TestReturn(code, duration, message, request, response));
}
return result;
}
use of io.github.microcks.domain.Response in project microcks by microcks.
the class GrpcTestRunner method runTest.
@Override
public List<TestReturn> runTest(Service service, Operation operation, TestResult testResult, List<Request> requests, String endpointUrl, HttpMethod method) throws URISyntaxException, IOException {
log.debug("Launching test run on {} for {} request(s)", endpointUrl, requests.size());
if (requests.isEmpty()) {
return null;
}
// Initialize results.
List<TestReturn> results = new ArrayList<>();
// Rebuild the GRPC fullMethodName.
String fullMethodName = service.getXmlNS() + "." + service.getName() + "/" + operation.getName();
// Build a new GRPC Channel from endpoint URL.
URL endpoint = new URL(endpointUrl);
ManagedChannel channel;
if (endpointUrl.startsWith("https://") || endpoint.getPort() == 443) {
TlsChannelCredentials.Builder tlsBuilder = TlsChannelCredentials.newBuilder();
if (secret != null && secret.getCaCertPem() != null) {
// Install a trust manager with custom CA certificate.
tlsBuilder.trustManager(new ByteArrayInputStream(secret.getCaCertPem().getBytes(StandardCharsets.UTF_8)));
} else {
// Install a trust manager that accepts everything and does not validate certificate chains.
tlsBuilder.trustManager(new TrustManager[] { new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
} });
}
// Build a Channel using the TLS Builder.
channel = Grpc.newChannelBuilderForAddress(endpoint.getHost(), endpoint.getPort(), tlsBuilder.build()).build();
} else {
// Build a simple Channel using plain text.
channel = Grpc.newChannelBuilderForAddress(endpoint.getHost(), endpoint.getPort(), null).usePlaintext().build();
}
// In order to produce outgoing byte array, we need the Protobuf binary descriptor that should
// have been processed while importing the .proto schema for the service.
List<Resource> resources = resourceRepository.findByServiceIdAndType(service.getId(), ResourceType.PROTOBUF_DESCRIPTOR);
if (resources == null || resources.size() != 1) {
log.error("Could not found any pre-processed Protobuf binary descriptor...");
results.add(new TestReturn(TestReturn.FAILURE_CODE, 0, "Could not found any pre-processed Protobuf binary descriptor...", null, null));
return results;
}
Resource pbResource = resources.get(0);
Descriptors.MethodDescriptor md = null;
try {
md = GrpcUtil.findMethodDescriptor(pbResource.getContent(), service.getName(), operation.getName());
} catch (Exception e) {
log.error("Protobuf descriptor cannot be read or parsed: " + e.getMessage());
results.add(new TestReturn(TestReturn.FAILURE_CODE, 0, "Protobuf descriptor cannot be read or parsed: " + e.getMessage(), null, null));
return results;
}
// Use a builder for out type with a Json parser to merge content and build outMsg.
DynamicMessage.Builder reqBuilder = DynamicMessage.newBuilder(md.getInputType());
DynamicMessage.Builder resBuilder = DynamicMessage.newBuilder(md.getOutputType());
JsonFormat.Parser parser = JsonFormat.parser();
JsonFormat.Printer printer = JsonFormat.printer();
for (Request request : requests) {
// Reset status code, message and request each time.
int code = TestReturn.SUCCESS_CODE;
String message = null;
reqBuilder.clear();
resBuilder.clear();
// Now produce the request message byte array.
parser.merge(request.getContent(), reqBuilder);
byte[] requestBytes = reqBuilder.build().toByteArray();
CallOptions callOptions = CallOptions.DEFAULT.withDeadline(Deadline.after(timeout, TimeUnit.MILLISECONDS));
if (secret != null && secret.getToken() != null) {
log.debug("Secret contains token and maybe token header, adding them as call credentials");
callOptions.withCallCredentials(new TokenCallCredentials(secret.getToken(), secret.getTokenHeader()));
}
// Actually execute request.
long startTime = System.currentTimeMillis();
byte[] responseBytes = ClientCalls.blockingUnaryCall(channel, GrpcUtil.buildGenericUnaryMethodDescriptor(fullMethodName), callOptions, requestBytes);
long duration = System.currentTimeMillis() - startTime;
// Create a Response object for returning.
Response response = new Response();
response.setStatus("200");
response.setMediaType("application/x-protobuf");
response.setContent(new String(responseBytes, "UTF-8"));
try {
// Validate incoming message parsing a DynamicMessage.
DynamicMessage respMsg = DynamicMessage.parseFrom(md.getOutputType(), responseBytes);
// Now update response content with readable content.
String respJson = printer.print(respMsg);
response.setContent(respJson);
results.add(new TestReturn(code, duration, message, request, response));
} catch (InvalidProtocolBufferException ipbe) {
log.error("Received bytes cannot be transformed in " + md.getOutputType().getFullName());
results.add(new TestReturn(TestReturn.FAILURE_CODE, duration, "Received bytes cannot be transformed in \" + md.getOutputType().getFullName()", request, response));
}
}
return results;
}
use of io.github.microcks.domain.Response in project microcks by microcks.
the class OpenAPITestRunner method extractTestReturnCode.
@Override
protected int extractTestReturnCode(Service service, Operation operation, Request request, ClientHttpResponse httpResponse, String responseContent) {
int code = TestReturn.SUCCESS_CODE;
int responseCode = 0;
try {
responseCode = httpResponse.getRawStatusCode();
log.debug("Response status code : " + responseCode);
} catch (IOException ioe) {
log.debug("IOException while getting raw status code in response", ioe);
return TestReturn.FAILURE_CODE;
}
// Extract response content-type in any case.
String contentType = null;
if (httpResponse.getHeaders().getContentType() != null) {
log.debug("Response media-type is {}", httpResponse.getHeaders().getContentType().toString());
contentType = httpResponse.getHeaders().getContentType().toString();
// Sanitize charset information from media-type.
if (contentType.contains("charset=") && contentType.indexOf(";") > 0) {
contentType = contentType.substring(0, contentType.indexOf(";"));
}
}
// If required, compare response code and content-type to expected ones.
if (validateResponseCode) {
Response expectedResponse = responseRepository.findById(request.getResponseId()).orElse(null);
log.debug("Response expected status code : " + expectedResponse.getStatus());
if (!String.valueOf(responseCode).equals(expectedResponse.getStatus())) {
log.debug("Response HttpStatus does not match expected one, returning failure");
return TestReturn.FAILURE_CODE;
}
if (!expectedResponse.getMediaType().equalsIgnoreCase(contentType)) {
log.debug("Response Content-Type does not match expected one, returning failure");
}
}
// Alternatives schemes are on their way for OpenAPI but not yet ready (see https://github.com/OAI/OpenAPI-Specification/pull/1736)
if (responseCode != 204 && APPLICATION_JSON_TYPE.equals(contentType)) {
boolean isOpenAPIv3 = true;
// Retrieve the resource corresponding to OpenAPI specification if any.
Resource openapiSpecResource = null;
List<Resource> resources = resourceRepository.findByServiceId(service.getId());
for (Resource resource : resources) {
if (ResourceType.OPEN_API_SPEC.equals(resource.getType())) {
openapiSpecResource = resource;
break;
} else if (ResourceType.SWAGGER.equals(resource.getType())) {
openapiSpecResource = resource;
isOpenAPIv3 = false;
break;
}
}
if (openapiSpecResource == null) {
log.debug("Found no OpenAPI specification resource for service {0}, so failing validating", service.getId());
return TestReturn.FAILURE_CODE;
}
JsonNode openApiSpec = null;
try {
openApiSpec = OpenAPISchemaValidator.getJsonNodeForSchema(openapiSpecResource.getContent());
} catch (IOException ioe) {
log.debug("OpenAPI specification cannot be transformed into valid JsonNode schema, so failing");
return TestReturn.FAILURE_CODE;
}
// Extract JsonNode corresponding to response.
String verb = operation.getName().split(" ")[0].toLowerCase();
String path = operation.getName().split(" ")[1].trim();
// Get body content as a string.
JsonNode contentNode = null;
try {
contentNode = OpenAPISchemaValidator.getJsonNode(responseContent);
} catch (IOException ioe) {
log.debug("Response body cannot be accessed or transformed as Json, returning failure");
return TestReturn.FAILURE_CODE;
}
String jsonPointer = "/paths/" + path.replace("/", "~1") + "/" + verb + "/responses/" + responseCode;
if (isOpenAPIv3) {
lastValidationErrors = OpenAPISchemaValidator.validateJsonMessage(openApiSpec, contentNode, jsonPointer, contentType, resourceUrl);
} else {
lastValidationErrors = SwaggerSchemaValidator.validateJsonMessage(openApiSpec, contentNode, jsonPointer, resourceUrl);
}
if (!lastValidationErrors.isEmpty()) {
log.debug("OpenAPI schema validation errors found " + lastValidationErrors.size() + ", marking test as failed.");
return TestReturn.FAILURE_CODE;
}
log.debug("OpenAPI schema validation of response is successful !");
}
return code;
}
use of io.github.microcks.domain.Response in project microcks by microcks.
the class GraphQLController method processGraphQLQuery.
/**
* Process a GraphQL field selection query (an Http query may contain many field selection queries).
* @param service The Service this query is targeting
* @param operationType The type of GraphQL operation (QUERY or MUTATION)
* @param graphqlField The Field selection we should apply
* @param fragmentDefinitions A list of fragment field selection
* @param body The Http request body
* @param graphqlHttpReq The Http GraphQL request wrapper
* @param request The bare Http Servlet request
* @return A GraphQL query response wrapper with some elements from the Microcks domain matching Response
* @throws GraphQLQueryProcessingException if incoming field selection query cannot be processed
*/
protected GraphQLQueryResponse processGraphQLQuery(Service service, String operationType, Field graphqlField, List<FragmentDefinition> fragmentDefinitions, String body, GraphQLHttpRequest graphqlHttpReq, HttpServletRequest request) throws GraphQLQueryProcessingException {
GraphQLQueryResponse result = new GraphQLQueryResponse();
String operationName = graphqlField.getName();
result.setAlias(graphqlField.getAlias());
result.setOperationName(operationName);
log.debug("Processing a '{}' operation with name '{}'", operationType, operationName);
Operation rOperation = null;
for (Operation operation : service.getOperations()) {
// Select operation based on type (QUERY or MUTATION)...
if (operation.getMethod().equals(operationType)) {
// ... then chek the operation name itself.
if (operation.getName().startsWith(operationName)) {
rOperation = operation;
break;
}
}
}
if (rOperation != null) {
log.debug("Found a valid operation {} with rules: {}", rOperation.getName(), rOperation.getDispatcherRules());
String violationMsg = validateParameterConstraintsIfAny(rOperation, request);
if (violationMsg != null) {
throw new GraphQLQueryProcessingException(violationMsg + ". Check parameter constraints.", HttpStatus.BAD_REQUEST);
}
// We must find dispatcher and its rules. Default to operation ones but
// if we have a Fallback this is the one who is holding the first pass rules.
String dispatcher = rOperation.getDispatcher();
String dispatcherRules = rOperation.getDispatcherRules();
FallbackSpecification fallback = MockControllerCommons.getFallbackIfAny(rOperation);
if (fallback != null) {
dispatcher = fallback.getDispatcher();
dispatcherRules = fallback.getDispatcherRules();
}
//
String dispatchCriteria = computeDispatchCriteria(dispatcher, dispatcherRules, graphqlField, graphqlHttpReq.getVariables(), request, body);
log.debug("Dispatch criteria for finding response is {}", dispatchCriteria);
// First try: using computed dispatchCriteria on main dispatcher.
Response response = null;
List<Response> responses = responseRepository.findByOperationIdAndDispatchCriteria(IdBuilder.buildOperationId(service, rOperation), dispatchCriteria);
if (!responses.isEmpty()) {
response = responses.get(0);
}
if (response == null) {
// When using the SCRIPT dispatcher, return of evaluation may be the name of response.
log.debug("No responses with dispatch criteria, trying the name...");
responses = responseRepository.findByOperationIdAndName(IdBuilder.buildOperationId(service, rOperation), dispatchCriteria);
if (!responses.isEmpty()) {
response = responses.get(0);
}
}
if (response == null && fallback != null) {
// If we've found nothing and got a fallback, that's the moment!
log.debug("No responses till now so far, applying the fallback...");
responses = responseRepository.findByOperationIdAndName(IdBuilder.buildOperationId(service, rOperation), fallback.getFallback());
if (!responses.isEmpty()) {
response = responses.get(0);
}
}
if (response == null) {
// In case no response found (because dispatcher is null for example), just get one for the operation.
// This will allow also OPTIONS operations (like pre-flight requests) with no dispatch criteria to work.
log.debug("No responses found so far, tempting with just bare operationId...");
responses = responseRepository.findByOperationId(IdBuilder.buildOperationId(service, rOperation));
if (!responses.isEmpty()) {
response = responses.get(0);
}
}
if (response != null) {
result.setResponse(response);
result.setOperationDelay(rOperation.getDefaultDelay());
// Prepare headers for evaluation.
Map<String, String> evaluableHeaders = new HashMap<>();
if (response.getHeaders() != null) {
for (Header header : response.getHeaders()) {
evaluableHeaders.put(header.getName(), request.getHeader(header.getName()));
}
}
// Render response content before waiting and returning.
String responseContent = MockControllerCommons.renderResponseContent(body, null, evaluableHeaders, response);
try {
JsonNode responseJson = mapper.readTree(responseContent);
filterFieldSelection(graphqlField.getSelectionSet(), fragmentDefinitions, responseJson.get("data").get(operationName));
result.setJsonResponse(responseJson);
} catch (JsonProcessingException pe) {
log.error("JsonProcessingException while filtering response according GraphQL field selection", pe);
throw new GraphQLQueryProcessingException("Exception while filtering response JSON", HttpStatus.INTERNAL_SERVER_ERROR);
}
return result;
}
log.debug("No response found. Throwing a BAD_REQUEST exception...");
throw new GraphQLQueryProcessingException("No matching response found", HttpStatus.BAD_REQUEST);
}
log.debug("No valid operation found. Throwing a NOT_FOUND exception...");
throw new GraphQLQueryProcessingException("No '" + operationName + "' operation found", HttpStatus.NOT_FOUND);
}
Aggregations