Skip to content

Commit

Permalink
1069 http rest connector migrate from google http connector (#2660)
Browse files Browse the repository at this point in the history
* chore(rest): Remove Google http client, and add the Apache HTTPComponents client instead + global refactoring

* chore(rest): Generate templates + format

* chore(rest): Handle remote execution errors

* chore(rest): Remove toString

* chore(rest): Revert debug change

* chore(rest): Rename some services

* chore(rest): Remove unused method

* chore(rest): Add some UTs + fix some bugs

* chore(rest): Delete old tests

* chore(rest): Add new tests + fix some minor issues

* chore(rest): Add new tests + fix after PR review

* chore(rest): Add new tests

* chore(rest): Add new Cloud function tests

* chore(rest): Add tests + clean code

* chore(rest): Add tests + clean code

* chore(rest): Add tests + clean code

* chore(rest): Add spotless removeUnusedImports + linting

* chore(rest): Fix UT

* chore(rest): Fix license text

* chore(rest): Fix GraphQL tests

* chore(rest): Fix POST and PUT Apache http client behavior

* chore(rest): Get rid of Exception

* chore(rest): Apply spotless

* chore(rest): Reworked package structure

* chore(rest): Apply spotless

* chore(rest): Reworked auth package

* chore(rest): Reworked auth package

* chore(rest): License format

* chore(rest): Apply spotless

* chore(rest): Support multipart requests + reworked test packages to match the src ones

* chore(rest): License format

* chore(rest): Support for custom boundary
  • Loading branch information
johnBgood authored Jun 11, 2024
1 parent e2f22ff commit f8b659b
Show file tree
Hide file tree
Showing 110 changed files with 3,854 additions and 2,508 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ void shouldSetBothResultVariableAndExpression() {
var jobHandler =
new ConnectorJobHandler(
(context) -> Map.of("callStatus", Map.of("statusCode", "200 OK")), e -> {});
var resultExpression = "{\"processedOutput\": response.callStatus }";
var resultExpression = "{\"processedOutput\": response.callStatus, \"nullVar\": null}";
var resultVariable = "result";

// when
Expand All @@ -439,8 +439,9 @@ void shouldSetBothResultVariableAndExpression() {
.executeAndCaptureResult(jobHandler);

// then
assertThat(result.getVariables().size()).isEqualTo(2);
assertThat(result.getVariables().size()).isEqualTo(3);
assertThat(result.getVariable("processedOutput")).isEqualTo(Map.of("statusCode", "200 OK"));
assertThat(result.getVariable("nullVar")).isNull();
assertThat(result.getVariable(resultVariable))
.isEqualTo(Map.of("callStatus", Map.of("statusCode", "200 OK")));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@
import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
import io.camunda.connector.e2e.app.TestConnectorRuntimeApplication;
import io.camunda.connector.http.base.auth.ApiKeyAuthentication;
import io.camunda.connector.http.base.auth.BasicAuthentication;
import io.camunda.connector.http.base.auth.BearerAuthentication;
import io.camunda.connector.http.base.auth.OAuthAuthentication;
import io.camunda.connector.http.base.constants.Constants;
import io.camunda.connector.http.base.authentication.OAuthConstants;
import io.camunda.connector.http.base.model.auth.ApiKeyAuthentication;
import io.camunda.connector.http.base.model.auth.BasicAuthentication;
import io.camunda.connector.http.base.model.auth.BearerAuthentication;
import io.camunda.connector.http.base.model.auth.OAuthAuthentication;
import io.camunda.connector.runtime.inbound.importer.ProcessDefinitionSearch;
import io.camunda.connector.runtime.inbound.state.ProcessImportResult;
import io.camunda.connector.runtime.inbound.state.ProcessImportResult.ProcessDefinitionIdentifier;
Expand Down Expand Up @@ -74,12 +74,11 @@
@ExtendWith(MockitoExtension.class)
public class HttpTests {

@TempDir File tempDir;

@RegisterExtension
static WireMockExtension wm =
WireMockExtension.newInstance().options(wireMockConfig().dynamicPort()).build();

@TempDir File tempDir;
@Autowired ZeebeClient zeebeClient;

@MockBean ProcessDefinitionSearch processDefinitionSearch;
Expand Down Expand Up @@ -279,7 +278,7 @@ void oAuth() {
post(urlPathMatching("/mock-oauth"))
.withBasicAuth("test-clientId", "test-clientSecret")
.willReturn(
ResponseDefinitionBuilder.okForJson(Map.of(Constants.ACCESS_TOKEN, "123"))));
ResponseDefinitionBuilder.okForJson(Map.of(OAuthConstants.ACCESS_TOKEN, "123"))));

var mockOauthUrl = "http://localhost:" + wm.getPort() + "/mock-oauth";

Expand All @@ -295,7 +294,7 @@ void oAuth() {
.property("authentication.oauthTokenEndpoint", mockOauthUrl)
.property("authentication.clientId", "test-clientId")
.property("authentication.clientSecret", "test-clientSecret")
.property("authentication.clientAuthentication", Constants.BASIC_AUTH_HEADER)
.property("authentication.clientAuthentication", OAuthConstants.BASIC_AUTH_HEADER)
.property("resultExpression", "={orderStatus: response.body.order.status}")
.writeTo(new File(tempDir, "template.json"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
package io.camunda.connector.automationanywhere;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.api.client.http.HttpRequestFactory;
import io.camunda.connector.api.annotation.OutboundConnector;
import io.camunda.connector.api.json.ConnectorsObjectMapperSupplier;
import io.camunda.connector.api.outbound.OutboundConnectorContext;
Expand All @@ -16,8 +15,7 @@
import io.camunda.connector.automationanywhere.model.request.AutomationAnywhereRequest;
import io.camunda.connector.automationanywhere.operations.OperationFactory;
import io.camunda.connector.generator.java.annotation.ElementTemplate;
import io.camunda.connector.http.base.components.HttpTransportComponentSupplier;
import io.camunda.connector.http.base.services.HttpService;
import io.camunda.connector.http.base.HttpService;
import java.util.Map;

@OutboundConnector(
Expand Down Expand Up @@ -48,14 +46,11 @@ public class AutomationAnywhereConnector implements OutboundConnectorFunction {
private final ObjectMapper objectMapper;

public AutomationAnywhereConnector() {
this(
ConnectorsObjectMapperSupplier.getCopy(),
HttpTransportComponentSupplier.httpRequestFactoryInstance());
this(ConnectorsObjectMapperSupplier.getCopy());
}

public AutomationAnywhereConnector(
final ObjectMapper objectMapper, final HttpRequestFactory requestFactory) {
this(new HttpService(objectMapper, requestFactory), objectMapper);
public AutomationAnywhereConnector(final ObjectMapper objectMapper) {
this(new HttpService(), objectMapper);
}

public AutomationAnywhereConnector(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import io.camunda.connector.automationanywhere.model.AutomationAnywhereHttpRequestBuilder;
import io.camunda.connector.http.base.HttpService;
import io.camunda.connector.http.base.model.HttpMethod;
import io.camunda.connector.http.base.services.HttpService;
import java.util.Map;

public record ApiKeyAuthProvider(
Expand All @@ -20,8 +20,7 @@ public record ApiKeyAuthProvider(
private static final String API_KEY = "apiKey";

@Override
public String obtainToken(final HttpService httpService, final ObjectMapper objectMapper)
throws Exception {
public String obtainToken(final HttpService httpService, final ObjectMapper objectMapper) {

final var request =
new AutomationAnywhereHttpRequestBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import io.camunda.connector.automationanywhere.model.TokenResponse;
import io.camunda.connector.http.base.HttpService;
import io.camunda.connector.http.base.model.HttpCommonResult;
import io.camunda.connector.http.base.services.HttpService;

/**
* Interface representing a provider for obtaining authentication tokens. This is part of the
Expand Down Expand Up @@ -61,6 +61,6 @@ default String getAuthenticationRequestUrl(final String controlRoomUrl) {
* @return The extracted token as a String.
*/
default String fetchToken(final HttpCommonResult result, final ObjectMapper objectMapper) {
return objectMapper.convertValue(result.getBody(), TokenResponse.class).token();
return objectMapper.convertValue(result.body(), TokenResponse.class).token();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import io.camunda.connector.automationanywhere.model.AutomationAnywhereHttpRequestBuilder;
import io.camunda.connector.http.base.HttpService;
import io.camunda.connector.http.base.model.HttpMethod;
import io.camunda.connector.http.base.services.HttpService;
import java.util.Map;

public record PasswordBasedAuthProvider(
Expand All @@ -25,8 +25,7 @@ public record PasswordBasedAuthProvider(
private static final String MULTIPLE_LOGIN_KEY = "multipleLogin";

@Override
public String obtainToken(final HttpService httpService, final ObjectMapper objectMapper)
throws Exception {
public String obtainToken(final HttpService httpService, final ObjectMapper objectMapper) {

final var request =
new AutomationAnywhereHttpRequestBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
package io.camunda.connector.automationanywhere.auth;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.camunda.connector.http.base.services.HttpService;
import io.camunda.connector.http.base.HttpService;

public record TokenBasedAuthProvider(String token) implements AuthenticationProvider {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
*/
package io.camunda.connector.automationanywhere.model;

import io.camunda.connector.http.base.auth.NoAuthentication;
import io.camunda.connector.http.base.model.HttpCommonRequest;
import io.camunda.connector.http.base.model.HttpMethod;
import io.camunda.connector.http.base.model.auth.NoAuthentication;
import java.util.Map;

public class AutomationAnywhereHttpRequestBuilder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
package io.camunda.connector.automationanywhere.operations;

import io.camunda.connector.automationanywhere.model.AutomationAnywhereHttpRequestBuilder;
import io.camunda.connector.http.base.HttpService;
import io.camunda.connector.http.base.model.HttpMethod;
import io.camunda.connector.http.base.services.HttpService;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
Expand All @@ -26,8 +26,7 @@ public record AddWorkItemOperation(

@Override
public Object execute(
final HttpService httpService, final Map<String, String> authenticationHeader)
throws Exception {
final HttpService httpService, final Map<String, String> authenticationHeader) {
final var request =
new AutomationAnywhereHttpRequestBuilder()
.withMethod(HttpMethod.POST)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
package io.camunda.connector.automationanywhere.operations;

import io.camunda.connector.automationanywhere.model.AutomationAnywhereHttpRequestBuilder;
import io.camunda.connector.http.base.HttpService;
import io.camunda.connector.http.base.model.HttpMethod;
import io.camunda.connector.http.base.services.HttpService;
import java.util.Map;
import java.util.function.Function;

Expand All @@ -31,8 +31,7 @@ public record GetWorkItemOperation(

@Override
public Object execute(
final HttpService httpService, final Map<String, String> authenticationHeader)
throws Exception {
final HttpService httpService, final Map<String, String> authenticationHeader) {

final var request =
new AutomationAnywhereHttpRequestBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
package io.camunda.connector.automationanywhere.operations;

import io.camunda.connector.http.base.services.HttpService;
import io.camunda.connector.http.base.HttpService;
import java.util.Map;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
import io.camunda.connector.api.json.ConnectorsObjectMapperSupplier;
import io.camunda.connector.api.outbound.OutboundConnectorContext;
import io.camunda.connector.api.outbound.OutboundConnectorFunction;
import io.camunda.connector.http.base.auth.NoAuthentication;
import io.camunda.connector.http.base.HttpService;
import io.camunda.connector.http.base.model.HttpCommonRequest;
import io.camunda.connector.http.base.model.HttpCommonResult;
import io.camunda.connector.http.base.model.HttpMethod;
import io.camunda.connector.http.base.services.HttpService;
import io.camunda.connector.http.base.model.auth.NoAuthentication;
import java.io.IOException;
import java.util.List;
import org.junit.jupiter.api.BeforeAll;
Expand Down Expand Up @@ -56,8 +56,7 @@ public static void beforeAll() throws IOException {
@BeforeEach
public void beforeEach() {
connector = new AutomationAnywhereConnector(httpService, objectMapper);
successHttpCommonResult = new HttpCommonResult();
successHttpCommonResult.setStatus(STATUS_SUCCESS);
successHttpCommonResult = new HttpCommonResult(200, null, null);
}

@ParameterizedTest
Expand All @@ -70,14 +69,14 @@ public void execute_passwordBasedAuthenticationShouldReturnSuccessResult(String
.thenReturn(httpCommonResult)
.thenReturn(successHttpCommonResult);

when(httpCommonResult.getBody()).thenReturn(authenticationResponse);
when(httpCommonResult.body()).thenReturn(authenticationResponse);
// when
Object execute = connector.execute(context);
// then
verify(httpService, times(2)).executeConnectorRequest(any(HttpCommonRequest.class));

assertThat(execute).isNotNull().isInstanceOf(HttpCommonResult.class);
assertThat(((HttpCommonResult) execute).getStatus()).isEqualTo(STATUS_SUCCESS);
assertThat(((HttpCommonResult) execute).status()).isEqualTo(STATUS_SUCCESS);

List<HttpCommonRequest> allValues = requestCaptor.getAllValues();
HttpCommonRequest authRequest = allValues.get(0);
Expand All @@ -97,7 +96,7 @@ public void execute_apiKeyAuthenticationShouldReturnSuccessResult(String input)
.thenReturn(httpCommonResult)
.thenReturn(successHttpCommonResult);

when(httpCommonResult.getBody()).thenReturn(authenticationResponse);
when(httpCommonResult.body()).thenReturn(authenticationResponse);

// when
Object execute = connector.execute(context);
Expand All @@ -106,7 +105,7 @@ public void execute_apiKeyAuthenticationShouldReturnSuccessResult(String input)
verify(httpService, times(2)).executeConnectorRequest(any(HttpCommonRequest.class));

assertThat(execute).isNotNull().isInstanceOf(HttpCommonResult.class);
assertThat(((HttpCommonResult) execute).getStatus()).isEqualTo(STATUS_SUCCESS);
assertThat(((HttpCommonResult) execute).status()).isEqualTo(STATUS_SUCCESS);

List<HttpCommonRequest> allValues = requestCaptor.getAllValues();

Expand All @@ -131,7 +130,7 @@ public void execute_tokenAuthenticationShouldReturnSuccessResult(String input) t
verify(httpService, times(1)).executeConnectorRequest(any(HttpCommonRequest.class));

assertThat(execute).isNotNull().isInstanceOf(HttpCommonResult.class);
assertThat(((HttpCommonResult) execute).getStatus()).isEqualTo(STATUS_SUCCESS);
assertThat(((HttpCommonResult) execute).status()).isEqualTo(STATUS_SUCCESS);

HttpCommonRequest operationRequest = requestCaptor.getValue();
verifyRequest(operationRequest, EXPECTED_GET_ITEM_URL, EXPECTED_BODY_WITH_FILTER);
Expand Down
26 changes: 9 additions & 17 deletions connectors/http/graphql/pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

Expand Down Expand Up @@ -32,21 +32,6 @@
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client-gson</artifactId>
</dependency>

<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client-apache-v2</artifactId>
</dependency>

<dependency>
<groupId>com.google.auth</groupId>
<artifactId>google-auth-library-oauth2-http</artifactId>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
Expand All @@ -62,6 +47,13 @@
<artifactId>commons-text</artifactId>
</dependency>

<dependency>
<groupId>org.wiremock</groupId>
<artifactId>wiremock-standalone</artifactId>
<version>${version.wiremock}</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,15 @@
package io.camunda.connector.http.graphql;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.api.client.http.HttpRequestFactory;
import io.camunda.connector.api.annotation.OutboundConnector;
import io.camunda.connector.api.json.ConnectorsObjectMapperSupplier;
import io.camunda.connector.api.outbound.OutboundConnectorContext;
import io.camunda.connector.api.outbound.OutboundConnectorFunction;
import io.camunda.connector.generator.java.annotation.ElementTemplate;
import io.camunda.connector.http.base.HttpService;
import io.camunda.connector.http.base.model.HttpCommonRequest;
import io.camunda.connector.http.base.services.HttpService;
import io.camunda.connector.http.graphql.components.HttpTransportComponentSupplier;
import io.camunda.connector.http.graphql.model.GraphQLRequest;
import io.camunda.connector.http.graphql.utils.GraphQLRequestMapper;
import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -49,19 +46,16 @@ public class GraphQLFunction implements OutboundConnectorFunction {
private final GraphQLRequestMapper graphQLRequestMapper;

public GraphQLFunction() {
this(
ConnectorsObjectMapperSupplier.getCopy(),
HttpTransportComponentSupplier.httpRequestFactoryInstance());
this(ConnectorsObjectMapperSupplier.getCopy());
}

public GraphQLFunction(final ObjectMapper objectMapper, final HttpRequestFactory requestFactory) {
this.httpService = new HttpService(objectMapper, requestFactory);
public GraphQLFunction(final ObjectMapper objectMapper) {
this.httpService = new HttpService();
this.graphQLRequestMapper = new GraphQLRequestMapper(objectMapper);
}

@Override
public Object execute(OutboundConnectorContext context)
throws IOException, InstantiationException, IllegalAccessException {
public Object execute(OutboundConnectorContext context) {
var graphQLRequest = context.bindVariables(GraphQLRequest.class);
HttpCommonRequest commonRequest = graphQLRequestMapper.toHttpCommonRequest(graphQLRequest);
LOGGER.debug("Executing graphql connector with request {}", commonRequest);
Expand Down
Loading

0 comments on commit f8b659b

Please sign in to comment.