-
Notifications
You must be signed in to change notification settings - Fork 38
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
...nnector-runtime-spring/src/main/java/io/camunda/connector/runtime/test/ConnectorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
204 changes: 204 additions & 0 deletions
204
...spring/src/main/java/io/camunda/connector/runtime/test/ConnectorTestActivatedJobImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
.../src/main/java/io/camunda/connector/runtime/test/ConnectorTestCompleteJobCommandImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
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 Error loading related location Loading Method ConnectorTestCompleteJobCommandImpl.variables(..) could be confused with overloaded method variables Error loading related location Loading |
||
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(); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
...tor-runtime-spring/src/main/java/io/camunda/connector/runtime/test/ConnectorTestImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 Error loading related location Loading |
||
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); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check notice
Code scanning / CodeQL
Confusing overloading of methods Note test