use of io.github.microcks.domain.Resource in project microcks by microcks.
the class ResourceController method getServiceResource.
@RequestMapping(value = "/resources/{serviceId}/{resourceType}", method = RequestMethod.GET)
public ResponseEntity<byte[]> getServiceResource(@PathVariable("serviceId") String serviceId, @PathVariable("resourceType") String resourceType, HttpServletResponse response) {
log.info("Requesting {} resource for service {}", resourceType, serviceId);
Service service = serviceRepository.findById(serviceId).orElse(null);
if (service != null && ServiceType.GENERIC_REST.equals(service.getType())) {
// Prepare HttpHeaders.
InputStream stream = null;
String resource = findResource(service);
HttpHeaders headers = new HttpHeaders();
// Get the correct template depending on resource type.
if (SWAGGER_20.equals(resourceType)) {
org.springframework.core.io.Resource template = new ClassPathResource("templates/swagger-2.0.json");
try {
stream = template.getInputStream();
} catch (IOException e) {
log.error("IOException while reading swagger-2.0.json template", e);
}
headers.setContentType(MediaType.APPLICATION_JSON);
} else if (OPENAPI_30.equals(resourceType)) {
org.springframework.core.io.Resource template = new ClassPathResource("templates/openapi-3.0.yaml");
try {
stream = template.getInputStream();
} catch (IOException e) {
log.error("IOException while reading openapi-3.0.yaml template", e);
}
headers.set("Content-Type", "text/yaml");
}
// Now process the stream, replacing patterns by value.
if (stream != null) {
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
StringWriter writer = new StringWriter();
try (Stream<String> lines = reader.lines()) {
lines.map(line -> replaceInLine(line, service, resource)).forEach(line -> writer.write(line + "\n"));
}
return new ResponseEntity<>(writer.toString().getBytes(), headers, HttpStatus.OK);
}
}
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
use of io.github.microcks.domain.Resource in project microcks by microcks.
the class ResourceController method execute.
@RequestMapping(value = "/resources/{name}", method = RequestMethod.GET)
public ResponseEntity<?> execute(@PathVariable("name") String name, HttpServletRequest request) {
String extension = request.getRequestURI().substring(request.getRequestURI().lastIndexOf('.'));
try {
name = URLDecoder.decode(name, StandardCharsets.UTF_8.toString());
} catch (UnsupportedEncodingException e) {
log.error("Exception while decoding resource name: {}", e.getMessage());
}
log.info("Requesting resource named " + name);
Resource resource = resourceRepository.findByName(name);
if (resource != null) {
HttpHeaders headers = new HttpHeaders();
if (".json".equals(extension)) {
headers.setContentType(MediaType.APPLICATION_JSON);
} else if (".yaml".equals(extension) || ".yml".equals(extension)) {
headers.set("Content-Type", "text/yaml");
headers.setContentDisposition(ContentDisposition.builder("inline").filename(name).build());
} else if (".wsdl".equals(extension) || ".xsd".equals(extension)) {
headers.setContentType(MediaType.TEXT_XML);
} else if (".avsc".equals(extension)) {
headers.setContentType(MediaType.APPLICATION_JSON);
}
return new ResponseEntity<Object>(resource.getContent(), headers, HttpStatus.OK);
}
return new ResponseEntity<Object>(HttpStatus.NOT_FOUND);
}
use of io.github.microcks.domain.Resource in project microcks by microcks.
the class ImportExportServiceTest method setUp.
@Before
public void setUp() {
// Create a bunch of services...
Service service = new Service();
service.setName("HelloWorld");
service.setVersion("1.2");
repository.save(service);
ids.add(service.getId());
// with same name and different version ...
service = new Service();
service.setName("HelloWorld");
service.setVersion("1.1");
repository.save(service);
ids.add(service.getId());
// with different name ...
service = new Service();
service.setName("MyService-hello");
service.setVersion("1.1");
repository.save(service);
ids.add(service.getId());
// Create associated resources.
Resource resource = new Resource();
resource.setServiceId(ids.get(0));
resource.setName("Resource 1");
resource.setType(ResourceType.WSDL);
resource.setContent("<wsdl></wsdl>");
resourceRepository.save(resource);
}
use of io.github.microcks.domain.Resource in project microcks by microcks.
the class ImportExportServiceTest method testImportRepository.
@Test
public void testImportRepository() {
// Setup and export result.
String json = "{\"services\":[{\"name\":\"Imp1\",\"version\":\"1.2\",\"xmlNS\":null,\"type\":null,\"operations\":[],\"id\":25638445759706201468670970602}," + "{\"name\":\"Imp2\",\"version\":\"1.1\",\"xmlNS\":null,\"type\":null,\"operations\":[],\"id\":25638445759706201468670970603}], " + "\"resources\":[{\"name\":\"Resource 1\",\"content\":\"<wsdl></wsdl>\",\"type\":\"WSDL\",\"serviceId\":25638445759706201468670970602,\"id\":25638445759706201468670970605}], " + "\"requests\":[], \"responses\":[]}";
boolean result = service.importRepository(json);
// Get imported service and assert on content.
Service service = repository.findByNameAndVersion("Imp1", "1.2");
assertNotNull(service);
assertEquals("Imp1", service.getName());
assertEquals("1.2", service.getVersion());
assertEquals("25638445759706201468670970602", service.getId().toString());
// Get imported resources and assert on content.
List<Resource> resources = resourceRepository.findByServiceId(service.getId());
assertEquals(1, resources.size());
assertEquals("Resource 1", resources.get(0).getName());
assertEquals("<wsdl></wsdl>", resources.get(0).getContent());
assertEquals(ResourceType.WSDL, resources.get(0).getType());
}
use of io.github.microcks.domain.Resource 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());
}
}
Aggregations