use of io.github.microcks.domain.Request in project microcks by microcks.
the class ServiceServiceTest method testImportServiceDefinition.
@Test
public void testImportServiceDefinition() {
List<Service> services = null;
try {
File artifactFile = new File("target/test-classes/io/github/microcks/service/weather-forecast-openapi.yaml");
services = service.importServiceDefinition(artifactFile, null, new ArtifactInfo("weather-forecast-openapi.yaml", true));
} catch (MockRepositoryImportException mrie) {
mrie.printStackTrace();
fail("No MockRepositoryImportException should have be thrown");
}
assertNotNull(services);
assertEquals(1, services.size());
// Inspect Service own attributes.
Service importedSvc = services.get(0);
assertEquals("WeatherForecast API", importedSvc.getName());
assertEquals("1.0.0", importedSvc.getVersion());
assertEquals("weather-forecast-openapi.yaml", importedSvc.getSourceArtifact());
assertNotNull(importedSvc.getMetadata());
assertEquals(1, importedSvc.getOperations().size());
assertEquals("GET /forecast/{region}", importedSvc.getOperations().get(0).getName());
assertEquals(5, importedSvc.getOperations().get(0).getResourcePaths().size());
// Inspect and check resources.
List<Resource> resources = resourceRepository.findByServiceId(importedSvc.getId());
assertEquals(1, resources.size());
Resource resource = resources.get(0);
assertEquals("WeatherForecast API-1.0.0.yaml", resource.getName());
assertEquals("weather-forecast-openapi.yaml", resource.getSourceArtifact());
// Inspect and check requests.
List<Request> requests = requestRepository.findByOperationId(IdBuilder.buildOperationId(importedSvc, importedSvc.getOperations().get(0)));
assertEquals(5, requests.size());
for (Request request : requests) {
assertEquals("weather-forecast-openapi.yaml", request.getSourceArtifact());
}
// Inspect and check responses.
List<Response> responses = responseRepository.findByOperationId(IdBuilder.buildOperationId(importedSvc, importedSvc.getOperations().get(0)));
assertEquals(5, requests.size());
for (Response response : responses) {
assertEquals("weather-forecast-openapi.yaml", response.getSourceArtifact());
}
}
use of io.github.microcks.domain.Request in project microcks by microcks.
the class ServiceServiceTest method testImportServiceDefinitionMainGraphQLAndSecondaryPostman.
@Test
public void testImportServiceDefinitionMainGraphQLAndSecondaryPostman() {
List<Service> services = null;
try {
File artifactFile = new File("target/test-classes/io/github/microcks/util/graphql/films.graphql");
services = service.importServiceDefinition(artifactFile, null, new ArtifactInfo("films.graphql", true));
} catch (MockRepositoryImportException mrie) {
mrie.printStackTrace();
fail("No MockRepositoryImportException should have be thrown");
}
assertNotNull(services);
assertEquals(1, services.size());
// Inspect Service own attributes.
Service importedSvc = services.get(0);
assertEquals("Movie Graph API", importedSvc.getName());
assertEquals("1.0", importedSvc.getVersion());
assertEquals("films.graphql", importedSvc.getSourceArtifact());
assertNotNull(importedSvc.getMetadata());
assertEquals(4, importedSvc.getOperations().size());
// Inspect and check requests.
List<Request> requests = requestRepository.findByOperationId(IdBuilder.buildOperationId(importedSvc, importedSvc.getOperations().get(0)));
assertEquals(0, requests.size());
// Inspect and check responses.
List<Response> responses = responseRepository.findByOperationId(IdBuilder.buildOperationId(importedSvc, importedSvc.getOperations().get(0)));
assertEquals(0, responses.size());
try {
File artifactFile = new File("target/test-classes/io/github/microcks/util/graphql/films-postman.json");
services = service.importServiceDefinition(artifactFile, null, new ArtifactInfo("films-postman.json", false));
} catch (MockRepositoryImportException mrie) {
mrie.printStackTrace();
fail("No MockRepositoryImportException should have be thrown");
}
// Inspect Service own attributes.
importedSvc = services.get(0);
assertEquals("Movie Graph API", importedSvc.getName());
assertEquals("1.0", importedSvc.getVersion());
assertEquals("films.graphql", importedSvc.getSourceArtifact());
assertNotNull(importedSvc.getMetadata());
assertEquals(4, importedSvc.getOperations().size());
// Inspect and check requests.
requests = requestRepository.findByOperationId(IdBuilder.buildOperationId(importedSvc, importedSvc.getOperations().get(0)));
assertEquals(1, requests.size());
for (Request request : requests) {
assertEquals("films-postman.json", request.getSourceArtifact());
}
// Inspect and check responses.
responses = responseRepository.findByOperationId(IdBuilder.buildOperationId(importedSvc, importedSvc.getOperations().get(0)));
assertEquals(1, requests.size());
for (Response response : responses) {
assertEquals("films-postman.json", response.getSourceArtifact());
}
}
use of io.github.microcks.domain.Request in project microcks by microcks.
the class EvaluationContextTest method testVariableRegistrationAndLookup.
@Test
public void testVariableRegistrationAndLookup() throws Exception {
// Create a context, register and retrieve a variable.
EvaluationContext context = new EvaluationContext();
context.setVariable("request", new Request());
Object requestObj = context.lookupVariable("request");
assertTrue(requestObj instanceof Request);
}
use of io.github.microcks.domain.Request in project microcks by microcks.
the class PostmanCollectionImporter method buildRequest.
private Request buildRequest(JsonNode requestNode, String name) {
Request request = new Request();
request.setName(name);
if (isV2Collection) {
request.setHeaders(buildHeaders(requestNode.path("header")));
if (requestNode.has("body") && requestNode.path("body").has("raw")) {
request.setContent(requestNode.path("body").path("raw").asText());
} else if (requestNode.has("body") && requestNode.path("body").has("graphql")) {
String content = requestNode.path("body").path("graphql").path("query").asText();
String variables = requestNode.path("body").path("graphql").path("variables").asText();
content = replaceGraphQLVariables(content, variables);
request.setContent(content);
}
if (requestNode.path("url").has("variable")) {
JsonNode variablesNode = requestNode.path("url").path("variable");
for (JsonNode variableNode : variablesNode) {
Parameter param = new Parameter();
param.setName(variableNode.path("key").asText());
param.setValue(variableNode.path("value").asText());
request.addQueryParameter(param);
}
}
if (requestNode.path("url").has("query")) {
JsonNode queryNode = requestNode.path("url").path("query");
for (JsonNode variableNode : queryNode) {
Parameter param = new Parameter();
param.setName(variableNode.path("key").asText());
param.setValue(variableNode.path("value").asText());
request.addQueryParameter(param);
}
}
} else {
request.setHeaders(buildHeaders(requestNode.path("headers")));
}
return request;
}
use of io.github.microcks.domain.Request in project microcks by microcks.
the class PostmanCollectionImporter method getMessageDefinitionsV2.
private Map<Request, Response> getMessageDefinitionsV2(String folderName, JsonNode itemNode, Operation operation) {
log.debug("Extracting message definitions in folder " + folderName);
Map<Request, Response> result = new HashMap<Request, Response>();
String itemNodeName = itemNode.path("name").asText();
if (!itemNode.has("request")) {
// Item is simply a folder that may contain some other folders recursively.
Iterator<JsonNode> items = itemNode.path("item").elements();
while (items.hasNext()) {
JsonNode item = items.next();
result.putAll(getMessageDefinitionsV2(folderName + "/" + itemNodeName, item, operation));
}
} else {
// Item is here an operation description.
String operationName = buildOperationName(itemNode, folderName);
// Select item based onto operation name.
if (areOperationsEquivalent(operation.getName(), operationName)) {
Iterator<JsonNode> responses = itemNode.path("response").elements();
while (responses.hasNext()) {
JsonNode responseNode = responses.next();
JsonNode requestNode = responseNode.path("originalRequest");
// Build dispatchCriteria and complete operation resourcePaths.
String dispatchCriteria = null;
String requestUrl = requestNode.path("url").path("raw").asText();
if (DispatchStyles.URI_PARAMS.equals(operation.getDispatcher())) {
dispatchCriteria = DispatchCriteriaHelper.extractFromURIParams(operation.getDispatcherRules(), requestUrl);
// We only need the pattern here.
operation.addResourcePath(extractResourcePath(requestUrl, null));
} else if (DispatchStyles.URI_PARTS.equals(operation.getDispatcher())) {
Map<String, String> parts = buildRequestParts(requestNode);
dispatchCriteria = DispatchCriteriaHelper.buildFromPartsMap(parts);
// We should complete resourcePath here.
String resourcePath = extractResourcePath(requestUrl, null);
operation.addResourcePath(URIBuilder.buildURIFromPattern(resourcePath, parts));
} else if (DispatchStyles.URI_ELEMENTS.equals(operation.getDispatcher())) {
Map<String, String> parts = buildRequestParts(requestNode);
dispatchCriteria = DispatchCriteriaHelper.buildFromPartsMap(parts);
dispatchCriteria += DispatchCriteriaHelper.extractFromURIParams(operation.getDispatcherRules(), requestUrl);
// We should complete resourcePath here.
String resourcePath = extractResourcePath(requestUrl, null);
operation.addResourcePath(URIBuilder.buildURIFromPattern(resourcePath, parts));
} else if (DispatchStyles.QUERY_ARGS.equals(operation.getDispatcher())) {
// This dispatcher is used for GraphQL
if (requestNode.path("body").has("graphql")) {
// We must transform JSON representing variables into a Map<String, String>
// before building a dispatchCriteria matching the rules.
String variables = requestNode.path("body").path("graphql").path("variables").asText();
dispatchCriteria = extractGraphQLCriteria(operation.getDispatcherRules(), variables);
}
}
Request request = buildRequest(requestNode, responseNode.path("name").asText());
Response response = buildResponse(responseNode, dispatchCriteria);
result.put(request, response);
}
}
}
return result;
}
Aggregations