Search in sources :

Example 1 with MockRepositoryImportException

use of io.github.microcks.util.MockRepositoryImportException in project microcks by microcks.

the class ServiceServiceTest method testImportServiceDefinitionMainAndSecondariesWithAPIMetadata.

@Test
public void testImportServiceDefinitionMainAndSecondariesWithAPIMetadata() {
    List<Service> services = null;
    try {
        File artifactFile = new File("target/test-classes/io/github/microcks/service/weather-forecast-raw-openapi.yaml");
        services = service.importServiceDefinition(artifactFile, null, new ArtifactInfo("weather-forecast-raw-openapi.yaml", true));
    } catch (MockRepositoryImportException mrie) {
        mrie.printStackTrace();
        fail("No MockRepositoryImportException should have be thrown");
    }
    try {
        File artifactFile = new File("target/test-classes/io/github/microcks/service/weather-forecast-postman.json");
        services = service.importServiceDefinition(artifactFile, null, new ArtifactInfo("weather-forecast-postman.json", false));
    } catch (MockRepositoryImportException mrie) {
        mrie.printStackTrace();
        fail("No MockRepositoryImportException should have be thrown");
    }
    try {
        File artifactFile = new File("target/test-classes/io/github/microcks/service/weather-forecast-metadata.yaml");
        services = service.importServiceDefinition(artifactFile, null, new ArtifactInfo("weather-forecast-metadata.yaml", false));
    } catch (MockRepositoryImportException mrie) {
        mrie.printStackTrace();
        fail("No MockRepositoryImportException should have be thrown");
    }
    // Inspect Service own attributes.
    Service importedSvc = services.get(0);
    assertEquals("WeatherForecast API", importedSvc.getName());
    assertEquals("1.1.0", importedSvc.getVersion());
    assertEquals("weather-forecast-raw-openapi.yaml", importedSvc.getSourceArtifact());
    assertNotNull(importedSvc.getMetadata());
    assertEquals(3, importedSvc.getMetadata().getLabels().size());
    assertEquals("weather", importedSvc.getMetadata().getLabels().get("domain"));
    assertEquals("GA", importedSvc.getMetadata().getLabels().get("status"));
    assertEquals("Team C", importedSvc.getMetadata().getLabels().get("team"));
    assertEquals(1, importedSvc.getOperations().size());
    assertEquals(100, importedSvc.getOperations().get(0).getDefaultDelay().longValue());
    assertEquals(DispatchStyles.FALLBACK, importedSvc.getOperations().get(0).getDispatcher());
    assertNotNull(importedSvc.getOperations().get(0).getDispatcherRules());
    assertEquals(5, importedSvc.getOperations().get(0).getResourcePaths().size());
}
Also used : Service(io.github.microcks.domain.Service) File(java.io.File) MockRepositoryImportException(io.github.microcks.util.MockRepositoryImportException) Test(org.junit.Test)

Example 2 with MockRepositoryImportException

use of io.github.microcks.util.MockRepositoryImportException 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)

Example 3 with MockRepositoryImportException

use of io.github.microcks.util.MockRepositoryImportException 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());
    }
}
Also used : Response(io.github.microcks.domain.Response) 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)

Example 4 with MockRepositoryImportException

use of io.github.microcks.util.MockRepositoryImportException in project microcks by microcks.

the class UploadArtifactController method importArtifact.

@RequestMapping(value = "/artifact/download", method = RequestMethod.POST)
public ResponseEntity<?> importArtifact(@RequestParam(value = "url", required = true) String url, @RequestParam(value = "mainArtifact", defaultValue = "true") boolean mainArtifact) {
    if (!url.isEmpty()) {
        List<Service> services = null;
        try {
            // Download remote to local file before import.
            HTTPDownloader.FileAndHeaders fileAndHeaders = HTTPDownloader.handleHTTPDownloadToFileAndHeaders(url, null, true);
            File localFile = fileAndHeaders.getLocalFile();
            // Now try importing services.
            services = serviceService.importServiceDefinition(localFile, new ReferenceResolver(url, null, true, RelativeReferenceURLBuilderFactory.getRelativeReferenceURLBuilder(fileAndHeaders.getResponseHeaders())), new ArtifactInfo(url, mainArtifact));
        } catch (IOException ioe) {
            log.error("Exception while retrieving remote item " + url, ioe);
            return new ResponseEntity<Object>("Exception while retrieving remote item", HttpStatus.INTERNAL_SERVER_ERROR);
        } catch (MockRepositoryImportException mrie) {
            return new ResponseEntity<Object>(mrie.getMessage(), HttpStatus.BAD_REQUEST);
        }
        if (services != null && services.size() > 0) {
            return new ResponseEntity<Object>("{\"name\": \"" + services.get(0).getName() + ":" + services.get(0).getVersion() + "\"}", HttpStatus.CREATED);
        }
    }
    return new ResponseEntity<Object>(HttpStatus.NO_CONTENT);
}
Also used : HTTPDownloader(io.github.microcks.util.HTTPDownloader) ResponseEntity(org.springframework.http.ResponseEntity) ArtifactInfo(io.github.microcks.service.ArtifactInfo) ServiceService(io.github.microcks.service.ServiceService) Service(io.github.microcks.domain.Service) IOException(java.io.IOException) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) MockRepositoryImportException(io.github.microcks.util.MockRepositoryImportException) ReferenceResolver(io.github.microcks.util.ReferenceResolver) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with MockRepositoryImportException

use of io.github.microcks.util.MockRepositoryImportException in project microcks by microcks.

the class UploadArtifactController method importArtifact.

@RequestMapping(value = "/artifact/upload", method = RequestMethod.POST)
public ResponseEntity<?> importArtifact(@RequestParam(value = "file") MultipartFile file, @RequestParam(value = "mainArtifact", defaultValue = "true") boolean mainArtifact) {
    if (!file.isEmpty()) {
        log.debug("Content type of " + file.getOriginalFilename() + " is " + file.getContentType());
        List<Service> services = null;
        try {
            // Save upload to local file before import.
            String localFile = System.getProperty("java.io.tmpdir") + "/microcks-" + System.currentTimeMillis() + ".artifact";
            ReadableByteChannel rbc = null;
            FileOutputStream fos = null;
            try {
                rbc = Channels.newChannel(file.getInputStream());
                // Transfer file to local.
                fos = new FileOutputStream(localFile);
                fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
            } finally {
                if (fos != null)
                    fos.close();
                if (rbc != null)
                    rbc.close();
            }
            // Now try importing services.
            services = serviceService.importServiceDefinition(new File(localFile), null, new ArtifactInfo(file.getOriginalFilename(), mainArtifact));
        } catch (IOException ioe) {
            log.error("Exception while writing uploaded item " + file.getOriginalFilename(), ioe);
            return new ResponseEntity<Object>("Exception while writing uploaded item", HttpStatus.INTERNAL_SERVER_ERROR);
        } catch (MockRepositoryImportException mrie) {
            return new ResponseEntity<Object>(mrie.getMessage(), HttpStatus.BAD_REQUEST);
        }
        if (services != null && services.size() > 0) {
            return new ResponseEntity<Object>(services.get(0).getName() + ":" + services.get(0).getVersion(), HttpStatus.CREATED);
        }
    }
    return new ResponseEntity<Object>(HttpStatus.NO_CONTENT);
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) ResponseEntity(org.springframework.http.ResponseEntity) ArtifactInfo(io.github.microcks.service.ArtifactInfo) FileOutputStream(java.io.FileOutputStream) ServiceService(io.github.microcks.service.ServiceService) Service(io.github.microcks.domain.Service) IOException(java.io.IOException) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) MockRepositoryImportException(io.github.microcks.util.MockRepositoryImportException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

MockRepositoryImportException (io.github.microcks.util.MockRepositoryImportException)23 Service (io.github.microcks.domain.Service)16 IOException (java.io.IOException)12 Test (org.junit.Test)12 Resource (io.github.microcks.domain.Resource)8 File (java.io.File)7 ReferenceResolver (io.github.microcks.util.ReferenceResolver)5 Operation (io.github.microcks.domain.Operation)4 Request (io.github.microcks.domain.Request)3 Response (io.github.microcks.domain.Response)3 ArrayList (java.util.ArrayList)3 RestMockService (com.eviware.soapui.impl.rest.mock.RestMockService)2 WsdlMockService (com.eviware.soapui.impl.wsdl.mock.WsdlMockService)2 MockService (com.eviware.soapui.model.mock.MockService)2 Metadata (io.github.microcks.domain.Metadata)2 ArtifactInfo (io.github.microcks.service.ArtifactInfo)2 ServiceService (io.github.microcks.service.ServiceService)2 HTTPDownloader (io.github.microcks.util.HTTPDownloader)2 MockRepositoryImporter (io.github.microcks.util.MockRepositoryImporter)2 ResponseEntity (org.springframework.http.ResponseEntity)2