Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add /connector-test endpoint to test outbound connector #3081

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public SecurityFilterChain filterChain2(HttpSecurity http) throws Exception {
.securityMatchers(
requestMatcherConfigurer ->
requestMatcherConfigurer
.requestMatchers(HttpMethod.POST, "/connector-test")
.requestMatchers(HttpMethod.GET, "/inbound/*")
.requestMatchers(HttpMethod.POST, "/inbound/*")
.requestMatchers(HttpMethod.PUT, "/inbound/*")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,19 @@
import io.camunda.connector.runtime.core.secret.SecretProviderAggregator;
import io.camunda.connector.runtime.outbound.lifecycle.OutboundConnectorAnnotationProcessor;
import io.camunda.connector.runtime.outbound.lifecycle.OutboundConnectorManager;
import io.camunda.connector.runtime.test.ConnectorTestImpl;
import io.camunda.connector.runtime.test.ConnectorTestRestController;
import io.camunda.zeebe.client.ZeebeClient;
import io.camunda.zeebe.spring.client.jobhandling.CommandExceptionHandlingStrategy;
import io.camunda.zeebe.spring.client.jobhandling.JobWorkerManager;
import io.camunda.zeebe.spring.client.metrics.MetricsRecorder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
@Import({ConnectorTestRestController.class})
public class OutboundConnectorRuntimeConfiguration {

@Bean
Expand Down Expand Up @@ -64,4 +69,12 @@ public OutboundConnectorAnnotationProcessor annotationProcessor(
OutboundConnectorManager manager, OutboundConnectorFactory factory) {
return new OutboundConnectorAnnotationProcessor(manager, factory);
}

@Bean
public ConnectorTestImpl connectorTest(
ZeebeClient zeebeClient,
ObjectMapper objectMapper,
SecretProviderAggregator secretProviderAggregator) {
return new ConnectorTestImpl(zeebeClient, objectMapper, secretProviderAggregator);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH
* under one or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information regarding copyright
* ownership. Camunda licenses this file to you under the Apache License,
* Version 2.0; you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.camunda.connector.runtime.test;

public interface ConnectorTest {

void test(ConnectorTestRq req) throws Exception;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
/*
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH
* under one or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information regarding copyright
* ownership. Camunda licenses this file to you under the Apache License,
* Version 2.0; you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.camunda.connector.runtime.test;

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.camunda.connector.feel.FeelEngineWrapper;
import io.camunda.zeebe.client.api.JsonMapper;
import io.camunda.zeebe.client.api.command.ClientException;
import io.camunda.zeebe.client.api.response.ActivatedJob;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

public class ConnectorTestActivatedJobImpl implements ActivatedJob {

public static FeelEngineWrapper FEEL_ENGINE_WRAPPER = new FeelEngineWrapper();

@JsonIgnore private final JsonMapper jsonMapper;

private final long key;
private final String type;
private final Map<String, String> customHeaders;
private final long processInstanceKey;
private final String bpmnProcessId;
private final int processDefinitionVersion;
private final long processDefinitionKey;
private final String elementId;
private final long elementInstanceKey;
private final String tenantId;
private final String worker;
private final int retries;
private final long deadline;
private final String variables;

private Map<String, Object> variablesAsMap;

public ConnectorTestActivatedJobImpl(
final JsonMapper jsonMapper, final ConnectorTestRq connectorTestRq) {
this.jsonMapper = jsonMapper;

var variables = resolveVariables(connectorTestRq, this.jsonMapper);

key = 123l;
type = connectorTestRq.type(); // "io.camunda:http-json:1";

final String customHeaders = connectorTestRq.customHeaders(); // "{\"retryBackoff\":\"PT0S\"}";
this.customHeaders =
customHeaders.isEmpty() ? new HashMap<>() : jsonMapper.fromJsonAsStringMap(customHeaders);
worker = "HTTP REST";
retries = 3;
deadline = 123l;
this.variables =
variables; // "{\"url\":\"https://abc.com/def\",\"method\":\"GET\",\"authentication\":{\"type\":\"noAuth\"},\"readTimeoutInSeconds\":\"20\",\"connectionTimeoutInSeconds\":\"20\"}";
processInstanceKey = 123l;
bpmnProcessId = "job.getBpmnProcessId()";
processDefinitionVersion = 1;
processDefinitionKey = 123l;
elementId = "job.getElementId()";
elementInstanceKey = 123l;
tenantId = "job.getTenantId()";
}

private String resolveVariables(
final ConnectorTestRq connectorTestRq, final JsonMapper jsonMapper) {
var contextAsMap =
Optional.ofNullable(connectorTestRq.context())
.filter(context -> !connectorTestRq.context().isBlank())
.map(context -> jsonMapper.fromJsonAsMap(context))
.orElse(Collections.emptyMap());
var variablesMap = jsonMapper.fromJsonAsMap(connectorTestRq.variables());
evaluateAllFeelStringsInMap(variablesMap, contextAsMap);
return jsonMapper.toJson(variablesMap);
}

public void evaluateAllFeelStringsInMap(
Map<String, Object> variablesMap, Map<String, Object> contextAsMap) {
for (Map.Entry<String, Object> entry : variablesMap.entrySet()) {
Object value = entry.getValue();
if (value instanceof String && ((String) entry.getValue()).startsWith("=")) {
var newValue = FEEL_ENGINE_WRAPPER.evaluateToJson((String) entry.getValue(), contextAsMap);
entry.setValue(newValue);
} else if (value instanceof Map) {
evaluateAllFeelStringsInMap((Map<String, Object>) value, contextAsMap);
}
}
}

@Override
public long getKey() {
return key;
}

@Override
public String getType() {
return type;
}

@Override
public long getProcessInstanceKey() {
return processInstanceKey;
}

@Override
public String getBpmnProcessId() {
return bpmnProcessId;
}

@Override
public int getProcessDefinitionVersion() {
return processDefinitionVersion;
}

@Override
public long getProcessDefinitionKey() {
return processDefinitionKey;
}

@Override
public String getElementId() {
return elementId;
}

@Override
public long getElementInstanceKey() {
return elementInstanceKey;
}

@Override
public Map<String, String> getCustomHeaders() {
return customHeaders;
}

@Override
public String getWorker() {
return worker;
}

@Override
public int getRetries() {
return retries;
}

@Override
public long getDeadline() {
return deadline;
}

@Override
public String getVariables() {
return variables;
}

@Override
public Map<String, Object> getVariablesAsMap() {
if (variablesAsMap == null) {
variablesAsMap = jsonMapper.fromJsonAsMap(variables);
}
return variablesAsMap;
}

@Override
public <T> T getVariablesAsType(final Class<T> variableType) {
return jsonMapper.fromJson(variables, variableType);
}

@Override
public Object getVariable(final String name) {
final Map<String, Object> variables = getVariablesAsMap();
if (!variables.containsKey(name)) {
throw new ClientException(String.format("The variable %s is not available", name));
}
return getVariablesAsMap().get(name);
}

@Override
public String toJson() {
return jsonMapper.toJson(this);
}

@Override
public String getTenantId() {
return tenantId;
}

@Override
public String toString() {
return ""; // return toJson();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH
* under one or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information regarding copyright
* ownership. Camunda licenses this file to you under the Apache License,
* Version 2.0; you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.camunda.connector.runtime.test;

import io.camunda.zeebe.client.api.ZeebeFuture;
import io.camunda.zeebe.client.api.command.CompleteJobCommandStep1;
import io.camunda.zeebe.client.api.command.FinalCommandStep;
import io.camunda.zeebe.client.api.response.CompleteJobResponse;
import java.io.InputStream;
import java.time.Duration;
import java.util.Map;

class ConnectorTestCompleteJobCommandImpl implements CompleteJobCommandStep1 {

long jobKey;

ConnectorTestCompleteJobCommandImpl(long jobKey) {
this.jobKey = jobKey;
}

@Override
public CompleteJobCommandStep1 variables(InputStream variables) {

Check notice

Code scanning / CodeQL

Confusing overloading of methods Note test

Method ConnectorTestCompleteJobCommandImpl.variables(..) could be confused with overloaded method
variables
, since dispatch depends on static types.
return this;
}

@Override
public CompleteJobCommandStep1 variables(String variables) {
return this;
}

@Override
public CompleteJobCommandStep1 variables(Map<String, Object> variables) {
return this;
}

@Override
public CompleteJobCommandStep1 variables(Object variables) {

Check notice

Code scanning / CodeQL

Confusing overloading of methods Note test

Method ConnectorTestCompleteJobCommandImpl.variables(..) could be confused with overloaded method
variables
, since dispatch depends on static types.
Method ConnectorTestCompleteJobCommandImpl.variables(..) could be confused with overloaded method
variables
, since dispatch depends on static types.
return this;
}

@Override
public CompleteJobCommandStep1 variable(String key, Object value) {
return this;
}

@Override
public FinalCommandStep<CompleteJobResponse> requestTimeout(Duration requestTimeout) {
return null;
}

@Override
public ZeebeFuture<CompleteJobResponse> send() {
return new ConnectorTestZeebeFutureImpl();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH
* under one or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information regarding copyright
* ownership. Camunda licenses this file to you under the Apache License,
* Version 2.0; you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.camunda.connector.runtime.test;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.camunda.connector.api.outbound.OutboundConnectorFunction;
import io.camunda.connector.runtime.core.outbound.ConnectorJobHandler;
import io.camunda.connector.runtime.core.outbound.DefaultOutboundConnectorFactory;
import io.camunda.connector.runtime.core.outbound.OutboundConnectorDiscovery;
import io.camunda.connector.runtime.core.outbound.OutboundConnectorFactory;
import io.camunda.connector.runtime.core.secret.SecretProviderAggregator;
import io.camunda.zeebe.client.ZeebeClient;
import io.camunda.zeebe.client.api.worker.JobClient;
import io.camunda.zeebe.client.impl.ZeebeObjectMapper;
import org.springframework.stereotype.Component;

@Component
public class ConnectorTestImpl implements ConnectorTest {
ZeebeClient zeebeClient;

private final JobClient jobClient = new ConnectorTestJobClientImpl();

ObjectMapper objectMapper;

SecretProviderAggregator secretProviderAggregator;

OutboundConnectorFactory connectorFactory =
new DefaultOutboundConnectorFactory(OutboundConnectorDiscovery.loadConnectorConfigurations());

public ConnectorTestImpl(
ZeebeClient zeebeClient,
ObjectMapper objectMapper,
SecretProviderAggregator secretProviderAggregator) {
this.secretProviderAggregator = secretProviderAggregator;
this.zeebeClient = zeebeClient;
this.objectMapper = objectMapper;
}

public void test(ConnectorTestRq req) throws Exception {

Check notice

Code scanning / CodeQL

Missing Override annotation Note test

This method overrides
ConnectorTest.test
; it is advisable to add an Override annotation.
var job = new ConnectorTestActivatedJobImpl(new ZeebeObjectMapper(), req);
OutboundConnectorFunction connectorFunction = connectorFactory.getInstance(job.getType());
var jobHandler =
new ConnectorJobHandler(connectorFunction, secretProviderAggregator, e -> {}, objectMapper);
jobHandler.handle(jobClient, job);
}
}
Loading