Search in sources :

Example 11 with MockRepositoryImportException

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

the class MetadataImporter method getServiceDefinitions.

@Override
public List<Service> getServiceDefinitions() throws MockRepositoryImportException {
    List<Service> result = new ArrayList<>();
    // Build a new service.
    Service service = new Service();
    JsonNode metadataNode = spec.get("metadata");
    if (metadataNode == null) {
        log.error("Missing mandatory metadata in {}", spec.asText());
        throw new MockRepositoryImportException("Mandatory metadata property is missing in APIMetadata");
    }
    service.setName(metadataNode.path("name").asText());
    service.setVersion(metadataNode.path("version").asText());
    Metadata metadata = new Metadata();
    MetadataExtractor.completeMetadata(metadata, metadataNode);
    service.setMetadata(metadata);
    // Then build its operations.
    service.setOperations(extractOperations());
    result.add(service);
    return result;
}
Also used : ArrayList(java.util.ArrayList) Metadata(io.github.microcks.domain.Metadata) Service(io.github.microcks.domain.Service) JsonNode(com.fasterxml.jackson.databind.JsonNode) MockRepositoryImportException(io.github.microcks.util.MockRepositoryImportException)

Example 12 with MockRepositoryImportException

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

the class GraphQLImporter method getServiceDefinitions.

@Override
public List<Service> getServiceDefinitions() throws MockRepositoryImportException {
    List<Service> results = new ArrayList<>();
    Service service = new Service();
    service.setType(ServiceType.GRAPHQL);
    // 1st thing: look for comments to get service and version identifiers.
    for (Comment comment : graphqlSchema.getComments()) {
        String content = comment.getContent().trim();
        if (content.startsWith(MICROCKS_ID_STARTER)) {
            String identifiers = content.substring(MICROCKS_ID_STARTER.length());
            if (identifiers.indexOf(":") != -1) {
                String[] serviceAndVersion = identifiers.split(":");
                service.setName(serviceAndVersion[0].trim());
                service.setVersion(serviceAndVersion[1].trim());
                break;
            }
            log.error("microcksId comment is malformed. Expecting \'microcksId: <API_name>:<API_version>\'");
            throw new MockRepositoryImportException("microcksId comment is malformed. Expecting \'microcksId: <API_name>:<API_version>\'");
        }
    }
    if (service.getName() == null || service.getVersion() == null) {
        log.error("No microcksId: comment found into GraphQL schema to get API name and version");
        throw new MockRepositoryImportException("No microcksId: comment found into GraphQL schema to get API name and version");
    }
    // We found a service, build its operations.
    service.setOperations(extractOperations());
    results.add(service);
    return results;
}
Also used : Comment(graphql.language.Comment) ArrayList(java.util.ArrayList) Service(io.github.microcks.domain.Service) MockRepositoryImportException(io.github.microcks.util.MockRepositoryImportException)

Example 13 with MockRepositoryImportException

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

the class SoapUIProjectImporter method getMessageDefinitions.

@Override
public List<Exchange> getMessageDefinitions(Service service, Operation operation) throws MockRepositoryImportException {
    // First try with a Soap Service mock...
    MockService mockService = project.getMockServiceByName(service.getName());
    if (mockService != null) {
        try {
            return getSoapMessageDefinitions(mockService, operation);
        } catch (XPathExpressionException xpe) {
            log.error("Got a XPathExpressionException while retrieving soap messages", xpe);
            throw new MockRepositoryImportException("XPathExpressionExceotion while retrieving soap messages", xpe);
        }
    }
    // ... then with a Rest Service mock.
    RestMockService restMockService = project.getRestMockServiceByName(service.getName());
    if (restMockService != null) {
        return getRestMessageDefinitions(restMockService, operation);
    }
    return new ArrayList<Exchange>();
}
Also used : RestMockService(com.eviware.soapui.impl.rest.mock.RestMockService) XPathExpressionException(javax.xml.xpath.XPathExpressionException) WsdlMockService(com.eviware.soapui.impl.wsdl.mock.WsdlMockService) RestMockService(com.eviware.soapui.impl.rest.mock.RestMockService) MockService(com.eviware.soapui.model.mock.MockService) MockRepositoryImportException(io.github.microcks.util.MockRepositoryImportException)

Example 14 with MockRepositoryImportException

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

the class ServiceServiceTest method testImportServiceDefinitionMainAndSecondary.

@Test
public void testImportServiceDefinitionMainAndSecondary() {
    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");
    }
    assertNotNull(services);
    assertEquals(1, services.size());
    // 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(1, importedSvc.getOperations().size());
    assertNull(importedSvc.getOperations().get(0).getResourcePaths());
    // Inspect and check resources.
    List<Resource> resources = resourceRepository.findByServiceId(importedSvc.getId());
    assertEquals(1, resources.size());
    Resource resource = resources.get(0);
    assertEquals("WeatherForecast API-1.1.0.yaml", resource.getName());
    assertEquals("weather-forecast-raw-openapi.yaml", resource.getSourceArtifact());
    // 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/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");
    }
    // Inspect Service own attributes.
    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(1, importedSvc.getOperations().size());
    assertEquals(DispatchStyles.URI_ELEMENTS, importedSvc.getOperations().get(0).getDispatcher());
    assertEquals(5, importedSvc.getOperations().get(0).getResourcePaths().size());
    // Inspect and check resources.
    resources = resourceRepository.findByServiceId(importedSvc.getId());
    assertEquals(1, resources.size());
    resource = resources.get(0);
    assertEquals("WeatherForecast API-1.1.0.yaml", resource.getName());
    assertEquals("weather-forecast-raw-openapi.yaml", resource.getSourceArtifact());
    // Inspect and check requests.
    requests = requestRepository.findByOperationId(IdBuilder.buildOperationId(importedSvc, importedSvc.getOperations().get(0)));
    assertEquals(5, requests.size());
    for (Request request : requests) {
        assertEquals("weather-forecast-postman.json", request.getSourceArtifact());
    }
    // Inspect and check responses.
    responses = responseRepository.findByOperationId(IdBuilder.buildOperationId(importedSvc, importedSvc.getOperations().get(0)));
    assertEquals(5, requests.size());
    for (Response response : responses) {
        assertEquals("weather-forecast-postman.json", 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 15 with MockRepositoryImportException

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

the class ProtobufImporterTest method testProtobufWithDependenciesImport.

@Test
public void testProtobufWithDependenciesImport() {
    ProtobufImporter importer = null;
    try {
        importer = new ProtobufImporter("target/test-classes/io/github/microcks/util/grpc/goodbye-v1.proto", null);
    } 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(2, resources.size());
    for (Resource resource : resources) {
        assertNotNull(resource.getContent());
        if (ResourceType.PROTOBUF_SCHEMA.equals(resource.getType())) {
            assertEquals("GoodbyeService-v1.proto", resource.getName());
        } else if (ResourceType.PROTOBUF_DESCRIPTOR.equals(resource.getType())) {
            assertEquals("GoodbyeService-v1.pbb", resource.getName());
            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) IOException(java.io.IOException) MockRepositoryImportException(io.github.microcks.util.MockRepositoryImportException) Test(org.junit.Test)

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