Search in sources :

Example 1 with Resource

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);
}
Also used : PathVariable(org.springframework.web.bind.annotation.PathVariable) URLDecoder(java.net.URLDecoder) LoggerFactory(org.slf4j.LoggerFactory) ClassPathResource(org.springframework.core.io.ClassPathResource) Autowired(org.springframework.beans.factory.annotation.Autowired) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ServiceRepository(io.github.microcks.repository.ServiceRepository) HttpServletRequest(javax.servlet.http.HttpServletRequest) ContentDisposition(org.springframework.http.ContentDisposition) Resource(io.github.microcks.domain.Resource) ResourceRepository(io.github.microcks.repository.ResourceRepository) Logger(org.slf4j.Logger) HttpHeaders(org.springframework.http.HttpHeaders) MediaType(org.springframework.http.MediaType) HttpServletResponse(javax.servlet.http.HttpServletResponse) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) StandardCharsets(java.nio.charset.StandardCharsets) HttpStatus(org.springframework.http.HttpStatus) Service(io.github.microcks.domain.Service) List(java.util.List) Stream(java.util.stream.Stream) Operation(io.github.microcks.domain.Operation) ServiceType(io.github.microcks.domain.ServiceType) java.io(java.io) ResponseEntity(org.springframework.http.ResponseEntity) HttpHeaders(org.springframework.http.HttpHeaders) java.io(java.io) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(io.github.microcks.domain.Resource) Service(io.github.microcks.domain.Service) ClassPathResource(org.springframework.core.io.ClassPathResource) ResponseEntity(org.springframework.http.ResponseEntity) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with Resource

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);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(io.github.microcks.domain.Resource) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with Resource

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);
}
Also used : Resource(io.github.microcks.domain.Resource) Service(io.github.microcks.domain.Service) Before(org.junit.Before)

Example 4 with 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());
}
Also used : Resource(io.github.microcks.domain.Resource) Service(io.github.microcks.domain.Service) Test(org.junit.Test)

Example 5 with Resource

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());
    }
}
Also used : Response(io.github.microcks.domain.Response) Resource(io.github.microcks.domain.Resource) Request(io.github.microcks.domain.Request) Service(io.github.microcks.domain.Service) File(java.io.File) MockRepositoryImportException(io.github.microcks.util.MockRepositoryImportException) Test(org.junit.Test)

Aggregations

Resource (io.github.microcks.domain.Resource)19 Service (io.github.microcks.domain.Service)11 IOException (java.io.IOException)11 MockRepositoryImportException (io.github.microcks.util.MockRepositoryImportException)8 Test (org.junit.Test)7 ArrayList (java.util.ArrayList)6 Operation (io.github.microcks.domain.Operation)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 Response (io.github.microcks.domain.Response)4 Request (io.github.microcks.domain.Request)3 HttpHeaders (org.springframework.http.HttpHeaders)3 ResponseEntity (org.springframework.http.ResponseEntity)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 File (java.io.File)2 ClassPathResource (org.springframework.core.io.ClassPathResource)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 Descriptors (com.google.protobuf.Descriptors)1 DynamicMessage (com.google.protobuf.DynamicMessage)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1