This is an automated email from the ASF dual-hosted git repository. acosentino pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/master by this push: new 024533c CAMEL-12495 - Camel-Slack: add component verifier from Syndesis project 024533c is described below commit 024533cf9e684485c2b1d56fada2a2890911ba0b Author: Andrea Cosentino <anco...@gmail.com> AuthorDate: Wed May 9 13:51:28 2018 +0200 CAMEL-12495 - Camel-Slack: add component verifier from Syndesis project --- .../camel/component/slack/SlackComponent.java | 11 +- .../slack/SlackComponentVerifierExtension.java | 114 +++++++++++++++++++++ .../slack/SlackComponentVerifierExtensionTest.java | 65 ++++++++++++ 3 files changed, 187 insertions(+), 3 deletions(-) diff --git a/components/camel-slack/src/main/java/org/apache/camel/component/slack/SlackComponent.java b/components/camel-slack/src/main/java/org/apache/camel/component/slack/SlackComponent.java index 9929d82..132a92e 100644 --- a/components/camel-slack/src/main/java/org/apache/camel/component/slack/SlackComponent.java +++ b/components/camel-slack/src/main/java/org/apache/camel/component/slack/SlackComponent.java @@ -18,15 +18,20 @@ package org.apache.camel.component.slack; import java.util.Map; +import org.apache.camel.CamelContext; import org.apache.camel.Endpoint; -import org.apache.camel.impl.UriEndpointComponent; +import org.apache.camel.impl.DefaultComponent; -public class SlackComponent extends UriEndpointComponent { +public class SlackComponent extends DefaultComponent { private String webhookUrl; public SlackComponent() { - super(SlackEndpoint.class); + this(null); + } + + public SlackComponent(CamelContext context) { + registerExtension(new SlackComponentVerifierExtension()); } /** diff --git a/components/camel-slack/src/main/java/org/apache/camel/component/slack/SlackComponentVerifierExtension.java b/components/camel-slack/src/main/java/org/apache/camel/component/slack/SlackComponentVerifierExtension.java new file mode 100644 index 0000000..a4f3e88 --- /dev/null +++ b/components/camel-slack/src/main/java/org/apache/camel/component/slack/SlackComponentVerifierExtension.java @@ -0,0 +1,114 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); 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 org.apache.camel.component.slack; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.camel.component.extension.verifier.DefaultComponentVerifierExtension; +import org.apache.camel.component.extension.verifier.ResultBuilder; +import org.apache.camel.component.extension.verifier.ResultErrorBuilder; +import org.apache.camel.component.extension.verifier.ResultErrorHelper; +import org.apache.camel.component.slack.helper.SlackMessage; +import org.apache.http.HttpResponse; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.HttpClientBuilder; +import org.json.simple.JSONObject; + +public class SlackComponentVerifierExtension extends DefaultComponentVerifierExtension { + + public SlackComponentVerifierExtension() { + this("slack"); + } + + public SlackComponentVerifierExtension(String scheme) { + super(scheme); + } + + // ********************************* + // Parameters validation + // ********************************* + @Override + protected Result verifyParameters(Map<String, Object> parameters) { + ResultBuilder builder = ResultBuilder.withStatusAndScope(Result.Status.OK, Scope.PARAMETERS) + .error(ResultErrorHelper.requiresOption("webhookUrl", parameters)); + + return builder.build(); + } + + // ********************************* + // Connectivity validation + // ********************************* + @Override + protected Result verifyConnectivity(Map<String, Object> parameters) { + return ResultBuilder.withStatusAndScope(Result.Status.OK, Scope.CONNECTIVITY) + .error(parameters, this::verifyCredentials).build(); + } + + private void verifyCredentials(ResultBuilder builder, Map<String, Object> parameters) { + + String webhookUrl = (String) parameters.get("webhookUrl"); + + try { + HttpClient client = HttpClientBuilder.create().useSystemProperties().build(); + HttpPost httpPost = new HttpPost(webhookUrl); + + // Build Helper object + SlackMessage slackMessage; + slackMessage = new SlackMessage(); + slackMessage.setText("Test connection"); + + // Set the post body + String json = asJson(slackMessage); + StringEntity body = new StringEntity(json); + + // Do the post + httpPost.setEntity(body); + + HttpResponse response = client.execute(httpPost); + + // 2xx is OK, anything else we regard as failure + if (response.getStatusLine().getStatusCode() < 200 || response.getStatusLine().getStatusCode() > 299) { + builder.error(ResultErrorBuilder + .withCodeAndDescription(VerificationError.StandardCode.AUTHENTICATION, + "Invalid webhookUrl") + .parameterKey("webhookUrl").build()); + } + } catch (Exception e) { + builder.error(ResultErrorBuilder + .withCodeAndDescription(VerificationError.StandardCode.AUTHENTICATION, + "Invalid webhookUrl") + .parameterKey("webhookUrl").build()); + } + } + + protected String asJson(SlackMessage message) { + Map<String, Object> jsonMap = new HashMap<>(); + + // Put the values in a map + jsonMap.put("text", message.getText()); + + // Generate a JSONObject + JSONObject jsonObject = new JSONObject(jsonMap); + + // Return the string based on the JSON Object + return JSONObject.toJSONString(jsonObject); + } + +} \ No newline at end of file diff --git a/components/camel-slack/src/test/java/org/apache/camel/component/slack/SlackComponentVerifierExtensionTest.java b/components/camel-slack/src/test/java/org/apache/camel/component/slack/SlackComponentVerifierExtensionTest.java new file mode 100644 index 0000000..74418c5 --- /dev/null +++ b/components/camel-slack/src/test/java/org/apache/camel/component/slack/SlackComponentVerifierExtensionTest.java @@ -0,0 +1,65 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); 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 org.apache.camel.component.slack; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.camel.Component; +import org.apache.camel.component.extension.ComponentVerifierExtension; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.Assert; +import org.junit.Test; + +public class SlackComponentVerifierExtensionTest extends CamelTestSupport { + + // ************************************************* + // Tests (parameters) + // ************************************************* + @Override + public boolean isUseRouteBuilder() { + return false; + } + + @Test + public void testParameters() throws Exception { + Component component = context().getComponent("slack"); + + ComponentVerifierExtension verifier = component.getExtension(ComponentVerifierExtension.class).orElseThrow(IllegalStateException::new); + + Map<String, Object> parameters = new HashMap<>(); + parameters.put("webhookUrl", "l"); + + ComponentVerifierExtension.Result result = verifier.verify(ComponentVerifierExtension.Scope.PARAMETERS, parameters); + + Assert.assertEquals(ComponentVerifierExtension.Result.Status.OK, result.getStatus()); + } + + @Test + public void testConnectivity() throws Exception { + Component component = context().getComponent("slack"); + ComponentVerifierExtension verifier = component.getExtension(ComponentVerifierExtension.class).orElseThrow(IllegalStateException::new); + + Map<String, Object> parameters = new HashMap<>(); + parameters.put("webhookUrl", "l"); + + ComponentVerifierExtension.Result result = verifier.verify(ComponentVerifierExtension.Scope.CONNECTIVITY, parameters); + + Assert.assertEquals(ComponentVerifierExtension.Result.Status.ERROR, result.getStatus()); + } + +} -- To stop receiving notification emails like this one, please contact acosent...@apache.org.