Search in sources :

Example 1 with APIIntegration

use of io.syndesis.server.api.generator.APIIntegration in project syndesis by syndesisio.

the class OpenApiGeneratorTest method testEmptyOperationSummary.

@Test
public void testEmptyOperationSummary() throws IOException {
    final ProvidedApiTemplate template = new ProvidedApiTemplate(dummyConnection(), "fromAction", "toAction");
    final String specification = TestHelper.resource("/openapi/v2/empty-summary.json");
    final OpenApiGenerator generator = new OpenApiGenerator();
    final APIIntegration apiIntegration = generator.generateIntegration(specification, template);
    assertThat(apiIntegration).isNotNull();
    assertThat(apiIntegration.getIntegration().getFlows()).hasSize(3);
    final List<Flow> flows = apiIntegration.getIntegration().getFlows();
    assertThat(flows).filteredOn(operationIdEquals("operation-1")).first().hasFieldOrPropertyWithValue("description", Optional.of("GET /hi")).hasFieldOrPropertyWithValue("name", "Receiving GET request on /hi");
    assertThat(flows).filteredOn(operationIdEquals("operation-2")).first().hasFieldOrPropertyWithValue("description", Optional.of("POST /hi")).hasFieldOrPropertyWithValue("name", "post operation");
    assertThat(flows).filteredOn(operationIdEquals("operation-3")).first().hasFieldOrPropertyWithValue("description", Optional.of("PUT /hi")).hasFieldOrPropertyWithValue("name", "Receiving PUT request on /hi");
}
Also used : ProvidedApiTemplate(io.syndesis.server.api.generator.ProvidedApiTemplate) APIIntegration(io.syndesis.server.api.generator.APIIntegration) Flow(io.syndesis.common.model.integration.Flow) Test(org.junit.Test)

Example 2 with APIIntegration

use of io.syndesis.server.api.generator.APIIntegration in project syndesis by syndesisio.

the class IntegrationSpecificationHandler method update.

@PUT
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Operation(description = "For an integration that is generated from a specification updates it so it conforms to the updated specification")
@RequestBody(content = @Content(mediaType = "multipart/form-data", schema = @Schema(implementation = ApiHandler.APIFormData.class)), description = "Next revision of the specification")
public void update(@NotNull @PathParam("id") @Parameter(required = true, example = "integration-id", description = "The ID of the integration") final String id, @NotNull @MultipartForm final ApiHandler.APIFormData apiFormData) {
    final Integration existing = integrationHandler.getIntegration(id);
    final APIIntegration apiIntegration = ApiGeneratorHelper.generateIntegrationUpdateFrom(existing, apiFormData, dataManager, apiGenerator);
    final Integration givenIntegration = apiIntegration.getIntegration();
    final Integration updated = ApiGeneratorHelper.updateFlowsAndStartAndEndDataShapes(existing, givenIntegration);
    final OpenApi existingApiSpecification = ApiGeneratorHelper.specificationFrom(resourceManager, existing).orElse(null);
    if (Objects.equals(existing.getFlows(), updated.getFlows()) && Objects.equals(existingApiSpecification, apiIntegration.getSpec())) {
        // no changes were made to the flows or to the specification
        return;
    }
    // store the OpenAPI resource, we keep the old one as it might
    // be referenced from Integration's stored in IntegrationDeployent's
    // this gives us a rollback mechanism
    dataManager.store(apiIntegration.getSpec(), OpenApi.class);
    // perform the regular update
    integrationHandler.update(id, updated);
}
Also used : APIIntegration(io.syndesis.server.api.generator.APIIntegration) Integration(io.syndesis.common.model.integration.Integration) APIIntegration(io.syndesis.server.api.generator.APIIntegration) OpenApi(io.syndesis.common.model.openapi.OpenApi) Consumes(javax.ws.rs.Consumes) Operation(io.swagger.v3.oas.annotations.Operation) PUT(javax.ws.rs.PUT) RequestBody(io.swagger.v3.oas.annotations.parameters.RequestBody)

Example 3 with APIIntegration

use of io.syndesis.server.api.generator.APIIntegration in project syndesis by syndesisio.

the class IntegrationSpecificationHandlerTest method shouldStoreUpdatedSpecificationForNonFlowChanges.

@Test
public void shouldStoreUpdatedSpecificationForNonFlowChanges() {
    final Step step = new Step.Builder().action(new ConnectorAction.Builder().descriptor(new ConnectorDescriptor.Builder().build()).build()).build();
    final Integration integration = new Integration.Builder().id("integration-1").addFlow(new Flow.Builder().putMetadata(OpenApi.OPERATION_ID, "flow1").addSteps(step, step).build()).build();
    final byte[] updatedSpecificationDocument = "updated specification".getBytes(StandardCharsets.UTF_8);
    final OpenApi updatedSpecification = new OpenApi.Builder().document(updatedSpecificationDocument).build();
    final APIIntegration updatedApiIntegration = new APIIntegration(integration, updatedSpecification);
    when(dataManager.fetch(Connection.class, "api-provider")).thenReturn(new Connection.Builder().connectorId("api-provider-connector").build());
    when(dataManager.fetch(Connector.class, "api-provider-connector")).thenReturn(new Connector.Builder().build());
    when(dataManager.fetch(Integration.class, "integration-1")).thenReturn(integration);
    when(encryptionSupport.encrypt(integration)).thenReturn(integration);
    when(apiGenerator.generateIntegration(any(String.class), any(ProvidedApiTemplate.class))).thenReturn(updatedApiIntegration);
    when(apiGenerator.updateFlowExcerpts(any(Integration.class))).then(ctx -> ctx.getArguments()[0]);
    final APIFormData openApiUpdate = new APIFormData();
    openApiUpdate.setSpecification(new ByteArrayInputStream(updatedSpecificationDocument));
    handler.update("integration-1", openApiUpdate);
    verify(dataManager).store(updatedSpecification, OpenApi.class);
    verify(dataManager).update(ArgumentMatchers.<Integration>argThat(v -> {
        assertThat(v).isEqualToIgnoringGivenFields(integration, "version", "updatedAt");
        assertThat(v.getVersion()).isEqualTo(2);
        return true;
    }));
}
Also used : ConnectorDescriptor(io.syndesis.common.model.action.ConnectorDescriptor) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ConnectorAction(io.syndesis.common.model.action.ConnectorAction) OpenApi(io.syndesis.common.model.openapi.OpenApi) ArgumentMatchers(org.mockito.ArgumentMatchers) Step(io.syndesis.common.model.integration.Step) ResourceIdentifier(io.syndesis.common.model.ResourceIdentifier) Kind(io.syndesis.common.model.Kind) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) APIIntegration(io.syndesis.server.api.generator.APIIntegration) EncryptionComponent(io.syndesis.server.dao.manager.EncryptionComponent) ByteArrayInputStream(java.io.ByteArrayInputStream) Connection(io.syndesis.common.model.connection.Connection) DataManager(io.syndesis.server.dao.manager.DataManager) Integration(io.syndesis.common.model.integration.Integration) Status(javax.ws.rs.core.Response.Status) Connector(io.syndesis.common.model.connection.Connector) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Flow(io.syndesis.common.model.integration.Flow) StandardCharsets(java.nio.charset.StandardCharsets) Mockito.verify(org.mockito.Mockito.verify) ProvidedApiTemplate(io.syndesis.server.api.generator.ProvidedApiTemplate) HttpHeaders(javax.ws.rs.core.HttpHeaders) Response(javax.ws.rs.core.Response) Optional(java.util.Optional) IntegrationResourceManager(io.syndesis.integration.api.IntegrationResourceManager) APIGenerator(io.syndesis.server.api.generator.APIGenerator) APIFormData(io.syndesis.server.endpoint.v1.handler.api.ApiHandler.APIFormData) Mockito.mock(org.mockito.Mockito.mock) APIIntegration(io.syndesis.server.api.generator.APIIntegration) Integration(io.syndesis.common.model.integration.Integration) Connection(io.syndesis.common.model.connection.Connection) Step(io.syndesis.common.model.integration.Step) Flow(io.syndesis.common.model.integration.Flow) ConnectorDescriptor(io.syndesis.common.model.action.ConnectorDescriptor) ByteArrayInputStream(java.io.ByteArrayInputStream) ProvidedApiTemplate(io.syndesis.server.api.generator.ProvidedApiTemplate) APIIntegration(io.syndesis.server.api.generator.APIIntegration) OpenApi(io.syndesis.common.model.openapi.OpenApi) APIFormData(io.syndesis.server.endpoint.v1.handler.api.ApiHandler.APIFormData) Test(org.junit.Test)

Example 4 with APIIntegration

use of io.syndesis.server.api.generator.APIIntegration in project syndesis by syndesisio.

the class IntegrationSpecificationHandlerTest method shouldPerformUpdatesBasedOnNewSpecification.

@Test
public void shouldPerformUpdatesBasedOnNewSpecification() {
    final Integration existing = new Integration.Builder().id("integration-1").addFlow(new Flow.Builder().putMetadata(OpenApi.OPERATION_ID, "flow1").build()).build();
    final Integration given = new Integration.Builder().id("integration-2").addFlow(new Flow.Builder().putMetadata(OpenApi.OPERATION_ID, "flow2").build()).build();
    final Integration expected = new Integration.Builder().id("integration-1").addFlow(new Flow.Builder().putMetadata(OpenApi.OPERATION_ID, "flow2").build()).build();
    final OpenApi updatedSpecification = new OpenApi.Builder().build();
    final APIIntegration updatedApiIntegration = new APIIntegration(given, updatedSpecification);
    when(dataManager.fetch(Connection.class, "api-provider")).thenReturn(new Connection.Builder().connectorId("api-provider-connector").build());
    when(dataManager.fetch(Connector.class, "api-provider-connector")).thenReturn(new Connector.Builder().build());
    when(dataManager.fetch(Integration.class, "integration-1")).thenReturn(existing);
    when(encryptionSupport.encrypt(expected)).thenReturn(expected);
    when(apiGenerator.generateIntegration(any(String.class), any(ProvidedApiTemplate.class))).thenReturn(updatedApiIntegration);
    when(apiGenerator.updateFlowExcerpts(any(Integration.class))).then(ctx -> ctx.getArguments()[0]);
    final APIFormData openApiUpdate = new APIFormData();
    openApiUpdate.setSpecification(new ByteArrayInputStream("updated specification".getBytes(StandardCharsets.UTF_8)));
    handler.update("integration-1", openApiUpdate);
    verify(dataManager).store(updatedSpecification, OpenApi.class);
    verify(dataManager).update(ArgumentMatchers.<Integration>argThat(v -> {
        assertThat(v).isEqualToIgnoringGivenFields(expected, "version", "updatedAt");
        assertThat(v.getVersion()).isEqualTo(2);
        return true;
    }));
}
Also used : ConnectorDescriptor(io.syndesis.common.model.action.ConnectorDescriptor) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ConnectorAction(io.syndesis.common.model.action.ConnectorAction) OpenApi(io.syndesis.common.model.openapi.OpenApi) ArgumentMatchers(org.mockito.ArgumentMatchers) Step(io.syndesis.common.model.integration.Step) ResourceIdentifier(io.syndesis.common.model.ResourceIdentifier) Kind(io.syndesis.common.model.Kind) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) APIIntegration(io.syndesis.server.api.generator.APIIntegration) EncryptionComponent(io.syndesis.server.dao.manager.EncryptionComponent) ByteArrayInputStream(java.io.ByteArrayInputStream) Connection(io.syndesis.common.model.connection.Connection) DataManager(io.syndesis.server.dao.manager.DataManager) Integration(io.syndesis.common.model.integration.Integration) Status(javax.ws.rs.core.Response.Status) Connector(io.syndesis.common.model.connection.Connector) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Flow(io.syndesis.common.model.integration.Flow) StandardCharsets(java.nio.charset.StandardCharsets) Mockito.verify(org.mockito.Mockito.verify) ProvidedApiTemplate(io.syndesis.server.api.generator.ProvidedApiTemplate) HttpHeaders(javax.ws.rs.core.HttpHeaders) Response(javax.ws.rs.core.Response) Optional(java.util.Optional) IntegrationResourceManager(io.syndesis.integration.api.IntegrationResourceManager) APIGenerator(io.syndesis.server.api.generator.APIGenerator) APIFormData(io.syndesis.server.endpoint.v1.handler.api.ApiHandler.APIFormData) Mockito.mock(org.mockito.Mockito.mock) APIIntegration(io.syndesis.server.api.generator.APIIntegration) Integration(io.syndesis.common.model.integration.Integration) Connection(io.syndesis.common.model.connection.Connection) ByteArrayInputStream(java.io.ByteArrayInputStream) ProvidedApiTemplate(io.syndesis.server.api.generator.ProvidedApiTemplate) APIIntegration(io.syndesis.server.api.generator.APIIntegration) OpenApi(io.syndesis.common.model.openapi.OpenApi) APIFormData(io.syndesis.server.endpoint.v1.handler.api.ApiHandler.APIFormData) Test(org.junit.Test)

Example 5 with APIIntegration

use of io.syndesis.server.api.generator.APIIntegration in project syndesis by syndesisio.

the class OpenApiGenerator method generateIntegration.

@Override
@SuppressWarnings({ "PMD.ExcessiveMethodLength" })
public APIIntegration generateIntegration(final String specification, final ProvidedApiTemplate template) {
    final OpenApiModelInfo info = OpenApiModelParser.parse(specification, APIValidationContext.NONE);
    final OasDocument openApiDoc = info.getModel();
    final String name = Optional.ofNullable(openApiDoc.info).flatMap(i -> Optional.ofNullable(i.title)).orElse("Untitled");
    final Integration.Builder integration = new Integration.Builder().addTag("api-provider").createdAt(System.currentTimeMillis()).name(name);
    switch(info.getApiVersion()) {
        case V2:
            oas20FlowGenerator.generateFlows(info.getV2Model(), integration, info, template);
            break;
        case V3:
            oas30FlowGenerator.generateFlows(info.getV3Model(), integration, info, template);
            break;
        default:
            throw new IllegalStateException(String.format("Unable to retrieve integration flow generator for OpenAPI document type '%s'", openApiDoc.getClass()));
    }
    // TODO: evaluate what can be shrunk (e.g. SpecificationOptimizer#minimizeForComponent)
    final byte[] updatedSpecification = Library.writeDocumentToJSONString(openApiDoc).getBytes(StandardCharsets.UTF_8);
    final String apiId = KeyGenerator.createKey();
    final OpenApi api = new OpenApi.Builder().id(apiId).name(name).document(updatedSpecification).putMetadata("Content-Type", getContentType(specification)).build();
    integration.addResource(new ResourceIdentifier.Builder().id(apiId).kind(Kind.OpenApi).build());
    return new APIIntegration(integration.build(), api);
}
Also used : OasDocument(io.apicurio.datamodels.openapi.models.OasDocument) OpenApi(io.syndesis.common.model.openapi.OpenApi) Action(io.syndesis.common.model.action.Action) KeyGenerator(io.syndesis.common.util.KeyGenerator) Step(io.syndesis.common.model.integration.Step) URL(java.net.URL) ResourceIdentifier(io.syndesis.common.model.ResourceIdentifier) Kind(io.syndesis.common.model.Kind) LoggerFactory(org.slf4j.LoggerFactory) Oas20FlowGenerator(io.syndesis.server.api.generator.openapi.v2.Oas20FlowGenerator) HashMap(java.util.HashMap) ConfigurationProperty(io.syndesis.common.model.connection.ConfigurationProperty) OasPathItem(io.apicurio.datamodels.openapi.models.OasPathItem) Function(java.util.function.Function) Info(io.apicurio.datamodels.core.models.common.Info) OpenApiModelParser(io.syndesis.server.api.generator.openapi.util.OpenApiModelParser) APIIntegration(io.syndesis.server.api.generator.APIIntegration) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Locale(java.util.Locale) Map(java.util.Map) FlowMetadata(io.syndesis.common.model.integration.FlowMetadata) OasModelHelper(io.syndesis.server.api.generator.openapi.util.OasModelHelper) Integration(io.syndesis.common.model.integration.Integration) APIValidationContext(io.syndesis.server.api.generator.APIValidationContext) APISummary(io.syndesis.common.model.api.APISummary) Logger(org.slf4j.Logger) MalformedURLException(java.net.MalformedURLException) Library(io.apicurio.datamodels.Library) Oas30FlowGenerator(io.syndesis.server.api.generator.openapi.v3.Oas30FlowGenerator) OasPaths(io.apicurio.datamodels.openapi.models.OasPaths) Collectors(java.util.stream.Collectors) Flow(io.syndesis.common.model.integration.Flow) StandardCharsets(java.nio.charset.StandardCharsets) List(java.util.List) ProvidedApiTemplate(io.syndesis.server.api.generator.ProvidedApiTemplate) Optional(java.util.Optional) APIGenerator(io.syndesis.server.api.generator.APIGenerator) OasDocument(io.apicurio.datamodels.openapi.models.OasDocument) ActionsSummary(io.syndesis.common.model.action.ActionsSummary) APIIntegration(io.syndesis.server.api.generator.APIIntegration) Integration(io.syndesis.common.model.integration.Integration) APIIntegration(io.syndesis.server.api.generator.APIIntegration) OpenApi(io.syndesis.common.model.openapi.OpenApi)

Aggregations

APIIntegration (io.syndesis.server.api.generator.APIIntegration)7 Integration (io.syndesis.common.model.integration.Integration)6 OpenApi (io.syndesis.common.model.openapi.OpenApi)5 Flow (io.syndesis.common.model.integration.Flow)4 ProvidedApiTemplate (io.syndesis.server.api.generator.ProvidedApiTemplate)4 Kind (io.syndesis.common.model.Kind)3 ResourceIdentifier (io.syndesis.common.model.ResourceIdentifier)3 Step (io.syndesis.common.model.integration.Step)3 APIGenerator (io.syndesis.server.api.generator.APIGenerator)3 DataManager (io.syndesis.server.dao.manager.DataManager)3 StandardCharsets (java.nio.charset.StandardCharsets)3 Optional (java.util.Optional)3 Operation (io.swagger.v3.oas.annotations.Operation)2 ConnectorAction (io.syndesis.common.model.action.ConnectorAction)2 ConnectorDescriptor (io.syndesis.common.model.action.ConnectorDescriptor)2 Connection (io.syndesis.common.model.connection.Connection)2 Connector (io.syndesis.common.model.connection.Connector)2 IntegrationResourceManager (io.syndesis.integration.api.IntegrationResourceManager)2 EncryptionComponent (io.syndesis.server.dao.manager.EncryptionComponent)2 APIFormData (io.syndesis.server.endpoint.v1.handler.api.ApiHandler.APIFormData)2