Search in sources :

Example 1 with ReferenceResolver

use of io.github.microcks.util.ReferenceResolver 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 2 with ReferenceResolver

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

the class OpenAPIImporterTest method testExternalAbsoluteReferenceOpenAPIImport.

@Test
public void testExternalAbsoluteReferenceOpenAPIImport() {
    OpenAPIImporter importer = null;
    ReferenceResolver resolver = new ReferenceResolver("https://raw.githubusercontent.com/microcks/microcks/1.5.x/webapp/src/test/resources/io/github/microcks/util/openapi/", null, true);
    try {
        importer = new OpenAPIImporter("target/test-classes/io/github/microcks/util/openapi/weather-forecast-openapi-absolute-ref.yaml", resolver);
    } catch (IOException ioe) {
        ioe.printStackTrace();
        fail("Exception should not be thrown");
    }
    // Check that basic service properties are there.
    List<Service> services = null;
    try {
        services = importer.getServiceDefinitions();
    } catch (MockRepositoryImportException e) {
        fail("Exception should not be thrown");
    }
    assertEquals(1, services.size());
    Service service = services.get(0);
    assertEquals("WeatherForecast API", service.getName());
    Assert.assertEquals(ServiceType.REST, service.getType());
    assertEquals("1.0.0", service.getVersion());
    List<Resource> resources = importer.getResourceDefinitions(service);
    assertEquals(2, resources.size());
    Resource openAPISpec = resources.get(0);
    assertEquals("WeatherForecast API-1.0.0.yaml", openAPISpec.getName());
    assertEquals(ResourceType.OPEN_API_SPEC, openAPISpec.getType());
    assertFalse(openAPISpec.getContent().contains("./WeatherForecast API-1.0.0-weather-forecast-schema.yaml"));
    Resource refSchema = resources.get(1);
    assertEquals("WeatherForecast API-1.0.0-weather-forecast-schema.yaml", refSchema.getName());
    assertEquals(ResourceType.JSON_SCHEMA, refSchema.getType());
    assertEquals("https://raw.githubusercontent.com/microcks/microcks/1.5.x/webapp/src/test/resources/io/github/microcks/util/openapi/weather-forecast-schema.yaml", refSchema.getPath());
    assertNotNull(refSchema.getContent());
    assertTrue(refSchema.getContent().contains("A weather forecast for a requested region"));
}
Also used : IOException(java.io.IOException) MockRepositoryImportException(io.github.microcks.util.MockRepositoryImportException) ReferenceResolver(io.github.microcks.util.ReferenceResolver) Test(org.junit.Test)

Example 3 with ReferenceResolver

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

the class OpenAPIImporterTest method testExternalRelativeReferenceOpenAPIImport.

@Test
public void testExternalRelativeReferenceOpenAPIImport() {
    OpenAPIImporter importer = null;
    ReferenceResolver resolver = new ReferenceResolver("https://raw.githubusercontent.com/microcks/microcks/1.5.x/webapp/src/test/resources/io/github/microcks/util/openapi/weather-forecast-openapi-relative-ref.yaml", null, true);
    try {
        importer = new OpenAPIImporter("target/test-classes/io/github/microcks/util/openapi/weather-forecast-openapi-relative-ref.yaml", resolver);
    } catch (IOException ioe) {
        ioe.printStackTrace();
        fail("Exception should not be thrown");
    }
    // Check that basic service properties are there.
    List<Service> services = null;
    try {
        services = importer.getServiceDefinitions();
    } catch (MockRepositoryImportException e) {
        fail("Exception should not be thrown");
    }
    assertEquals(1, services.size());
    Service service = services.get(0);
    assertEquals("WeatherForecast API", service.getName());
    Assert.assertEquals(ServiceType.REST, service.getType());
    assertEquals("1.0.0", service.getVersion());
    List<Resource> resources = importer.getResourceDefinitions(service);
    assertEquals(2, resources.size());
    Resource openAPISpec = resources.get(0);
    assertEquals("WeatherForecast API-1.0.0.yaml", openAPISpec.getName());
    assertEquals(ResourceType.OPEN_API_SPEC, openAPISpec.getType());
    assertTrue(openAPISpec.getContent().contains("./WeatherForecast+API-1.0.0-weather-forecast-schema.yaml"));
    Resource refSchema = resources.get(1);
    assertEquals("WeatherForecast API-1.0.0-weather-forecast-schema.yaml", refSchema.getName());
    assertEquals(ResourceType.JSON_SCHEMA, refSchema.getType());
    assertEquals("./weather-forecast-schema.yaml", refSchema.getPath());
    assertNotNull(refSchema.getContent());
    assertTrue(refSchema.getContent().contains("A weather forecast for a requested region"));
}
Also used : IOException(java.io.IOException) MockRepositoryImportException(io.github.microcks.util.MockRepositoryImportException) ReferenceResolver(io.github.microcks.util.ReferenceResolver) Test(org.junit.Test)

Example 4 with ReferenceResolver

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

the class ProtobufImporterTest method testProtobufWithRemoteDependenciesImport.

@Test
public void testProtobufWithRemoteDependenciesImport() {
    ProtobufImporter importer = null;
    try {
        importer = new ProtobufImporter("target/test-classes/io/github/microcks/util/grpc/remote/goodbye-v1.proto", new ReferenceResolver("https://raw.githubusercontent.com/microcks/microcks/1.5.x/webapp/src/test/resources/io/github/microcks/util/grpc/base.proto", null, true));
    } catch (IOException ioe) {
        fail("Exception should not be thrown");
    }
    // Check that basic service properties are there.
    List<Service> services = null;
    try {
        services = importer.getServiceDefinitions();
    } catch (MockRepositoryImportException e) {
        fail("Service definition import should not fail");
    }
    assertEquals(1, services.size());
    Service service = services.get(0);
    assertEquals("GoodbyeService", service.getName());
    assertEquals(ServiceType.GRPC, service.getType());
    assertEquals("v1", service.getVersion());
    assertEquals("io.github.microcks.grpc.goodbye.v1", service.getXmlNS());
    // Check that resources have been parsed, correctly renamed, etc...
    List<Resource> resources = null;
    try {
        resources = importer.getResourceDefinitions(service);
    } catch (MockRepositoryImportException mrie) {
        fail("Resource definition import should not fail");
    }
    assertEquals(3, resources.size());
    for (Resource resource : resources) {
        assertNotNull(resource.getContent());
        if (ResourceType.PROTOBUF_SCHEMA.equals(resource.getType())) {
            assertTrue("GoodbyeService-v1.proto".equals(resource.getName()) || "GoodbyeService-v1-shared~1uuid.proto".equals(resource.getName()));
        } else if (ResourceType.PROTOBUF_DESCRIPTOR.equals(resource.getType())) {
            assertEquals("GoodbyeService-v1.pbb", resource.getName());
            System.err.println("base64: " + resource.getContent());
            try {
                // Check Protobuf Descriptor.
                byte[] decodedBinaryPB = Base64.getDecoder().decode(resource.getContent().getBytes("UTF-8"));
                DescriptorProtos.FileDescriptorSet fds = DescriptorProtos.FileDescriptorSet.parseFrom(decodedBinaryPB);
                assertEquals(2, fds.getFileCount());
                assertEquals("shared/uuid.proto", fds.getFile(0).getName());
                assertEquals("goodbye-v1.proto", fds.getFile(1).getName());
            } catch (Exception e) {
                fail("Protobuf file descriptor is not correct");
            }
        } else {
            fail("Resource has not the expected type");
        }
    }
}
Also used : Resource(io.github.microcks.domain.Resource) Service(io.github.microcks.domain.Service) IOException(java.io.IOException) MockRepositoryImportException(io.github.microcks.util.MockRepositoryImportException) ReferenceResolver(io.github.microcks.util.ReferenceResolver) IOException(java.io.IOException) MockRepositoryImportException(io.github.microcks.util.MockRepositoryImportException) Test(org.junit.Test)

Example 5 with ReferenceResolver

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

the class ServiceService method importServiceDefinition.

/**
 * Import definitions of services and bounded resources and messages into Microcks
 * repository. This uses a MockRepositoryImporter underhood.
 * @param repositoryUrl The String representing mock repository url.
 * @param repositorySecret The authentication secret associated with the repository url. Can be set to null if none.
 * @param disableSSLValidation Whether SSL certificates validation should be turned off.
 * @param mainArtifact Whether this repository should be considered as main artifact for Services to import.
 * @return The list of imported Services
 * @throws MockRepositoryImportException if something goes wrong (URL not reachable nor readable, etc...)
 */
public List<Service> importServiceDefinition(String repositoryUrl, Secret repositorySecret, boolean disableSSLValidation, boolean mainArtifact) throws MockRepositoryImportException {
    log.info("Importing service definitions from " + repositoryUrl);
    File localFile = null;
    Map<String, List<String>> fileProperties = null;
    if (repositoryUrl.startsWith("http")) {
        try {
            HTTPDownloader.FileAndHeaders fileAndHeaders = HTTPDownloader.handleHTTPDownloadToFileAndHeaders(repositoryUrl, repositorySecret, disableSSLValidation);
            localFile = fileAndHeaders.getLocalFile();
            fileProperties = fileAndHeaders.getResponseHeaders();
        } catch (IOException ioe) {
            log.error("Exception while downloading " + repositoryUrl, ioe);
            throw new MockRepositoryImportException(repositoryUrl + " cannot be downloaded", ioe);
        }
    } else {
        // Simply build localFile from repository url.
        localFile = new File(repositoryUrl);
    }
    RelativeReferenceURLBuilder referenceURLBuilder = RelativeReferenceURLBuilderFactory.getRelativeReferenceURLBuilder(fileProperties);
    String artifactName = referenceURLBuilder.getFileName(repositoryUrl, fileProperties);
    // Initialize a reference resolver to the folder of this repositoryUrl.
    ReferenceResolver referenceResolver = new ReferenceResolver(repositoryUrl, repositorySecret, disableSSLValidation, referenceURLBuilder);
    return importServiceDefinition(localFile, referenceResolver, new ArtifactInfo(artifactName, mainArtifact));
}
Also used : HTTPDownloader(io.github.microcks.util.HTTPDownloader) RelativeReferenceURLBuilder(io.github.microcks.util.RelativeReferenceURLBuilder) List(java.util.List) IOException(java.io.IOException) File(java.io.File) MockRepositoryImportException(io.github.microcks.util.MockRepositoryImportException) ReferenceResolver(io.github.microcks.util.ReferenceResolver)

Aggregations

MockRepositoryImportException (io.github.microcks.util.MockRepositoryImportException)5 ReferenceResolver (io.github.microcks.util.ReferenceResolver)5 IOException (java.io.IOException)5 Test (org.junit.Test)3 Service (io.github.microcks.domain.Service)2 HTTPDownloader (io.github.microcks.util.HTTPDownloader)2 File (java.io.File)2 Resource (io.github.microcks.domain.Resource)1 ArtifactInfo (io.github.microcks.service.ArtifactInfo)1 ServiceService (io.github.microcks.service.ServiceService)1 RelativeReferenceURLBuilder (io.github.microcks.util.RelativeReferenceURLBuilder)1 List (java.util.List)1 ResponseEntity (org.springframework.http.ResponseEntity)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 MultipartFile (org.springframework.web.multipart.MultipartFile)1