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 e64a2ab  [REFACTOR] Refactor slack tests
e64a2ab is described below

commit e64a2abb6908bdec7c69fd5c33940b6dcfe268e9
Author: Jan <[email protected]>
AuthorDate: Fri Nov 9 12:51:41 2018 +0100

    [REFACTOR] Refactor slack tests
---
 components/camel-slack/pom.xml                     |  6 +--
 .../camel/component/slack/SlackConsumerTest.java   | 55 +++++++++++++++++++---
 .../camel/component/slack/SlackProducerTest.java   | 48 +++++++++++++++----
 .../resources/OSGI-INF/blueprint/blueprint.xml     | 44 -----------------
 4 files changed, 89 insertions(+), 64 deletions(-)

diff --git a/components/camel-slack/pom.xml b/components/camel-slack/pom.xml
index 4adee8e..148e26d 100644
--- a/components/camel-slack/pom.xml
+++ b/components/camel-slack/pom.xml
@@ -58,9 +58,9 @@
     <!-- test dependencies -->
     <dependency>
       <groupId>org.apache.camel</groupId>
-      <artifactId>camel-test-blueprint</artifactId>
+      <artifactId>camel-test</artifactId>
       <scope>test</scope>
-    </dependency>  
+    </dependency>
     <!-- logging -->   
     <dependency>
       <groupId>org.apache.logging.log4j</groupId>
@@ -79,6 +79,4 @@
     </dependency>
 
   </dependencies>
-
-
 </project>
diff --git 
a/components/camel-slack/src/test/java/org/apache/camel/component/slack/SlackConsumerTest.java
 
b/components/camel-slack/src/test/java/org/apache/camel/component/slack/SlackConsumerTest.java
index 2a439cd..03364c7 100644
--- 
a/components/camel-slack/src/test/java/org/apache/camel/component/slack/SlackConsumerTest.java
+++ 
b/components/camel-slack/src/test/java/org/apache/camel/component/slack/SlackConsumerTest.java
@@ -16,29 +16,72 @@
  */
 package org.apache.camel.component.slack;
 
+import java.io.IOException;
+
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.component.slack.helper.SlackMessage;
 import org.apache.camel.test.junit4.CamelTestSupport;
-import org.junit.Ignore;
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpStatus;
+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.HttpClients;
+import org.hamcrest.CoreMatchers;
+import org.junit.Assert;
+import org.junit.Assume;
+import org.junit.Before;
 import org.junit.Test;
 
-@Ignore
+
+
 public class SlackConsumerTest extends CamelTestSupport {
 
+    private String token;
+    private String hook;
+
+    @Before
+    public void setUp() throws Exception {
+        token = System.getProperty("SLACK_TOKEN");
+        hook = System.getProperty("SLACK_HOOK", 
"https://hooks.slack.com/services/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";);
+
+        assumeCredentials();
+        super.setUp();
+    }
+
     @Test
     public void testConsumePrefixedMessages() throws Exception {
+        final String message = "Hi camel";
+        sendMessage(message);
+
         MockEndpoint mock = getMockEndpoint("mock:result");
-        mock.expectedMessageCount(9);
-        
+        mock.expectedMessageCount(1);
+        
mock.message(0).body(SlackMessage.class).method("getText").isEqualTo(message);
+
         assertMockEndpointsSatisfied();
     }
-    
+
+    private void assumeCredentials() {
+        Assume.assumeThat("You should specified access token", token, 
CoreMatchers.notNullValue());
+        Assume.assumeThat("You should specified slack application hook", hook, 
CoreMatchers.notNullValue());
+    }
+
+    private void sendMessage(String message) throws IOException {
+        HttpClient client = HttpClients.createDefault();
+        HttpPost post = new HttpPost(hook);
+        post.setHeader("Content-type", "application/json");
+        post.setEntity(new StringEntity(String.format("{ 'text': '%s'}", 
message)));
+        HttpResponse response = client.execute(post);
+        Assert.assertEquals(response.getStatusLine().getStatusCode(), 
HttpStatus.SC_OK);
+    }
+
     @Override
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {
             @Override
             public void configure() throws Exception {
-                from("slack://general?token=RAW(token)&maxResults=1")
+                
from(String.format("slack://general?token=RAW(%s)&maxResults=1", token))
                     .to("mock:result");
             }
         };
diff --git 
a/components/camel-slack/src/test/java/org/apache/camel/component/slack/SlackProducerTest.java
 
b/components/camel-slack/src/test/java/org/apache/camel/component/slack/SlackProducerTest.java
index 457418b..cfa3ae6 100644
--- 
a/components/camel-slack/src/test/java/org/apache/camel/component/slack/SlackProducerTest.java
+++ 
b/components/camel-slack/src/test/java/org/apache/camel/component/slack/SlackProducerTest.java
@@ -16,31 +16,59 @@
  */
 package org.apache.camel.component.slack;
 
-import org.apache.camel.test.blueprint.CamelBlueprintTestSupport;
+import org.apache.camel.EndpointInject;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.direct.DirectEndpoint;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit4.CamelTestSupport;
+
 import org.junit.Test;
 
-public class SlackProducerTest extends CamelBlueprintTestSupport {
+public class SlackProducerTest extends CamelTestSupport {
 
-    @Override
-    protected String getBlueprintDescriptor() {
-        return "OSGI-INF/blueprint/blueprint.xml";
-    }
+    @EndpointInject(uri = "mock:errors")
+    MockEndpoint errors;
+
+    @EndpointInject(uri = "direct:test")
+    DirectEndpoint test;
+
+    @EndpointInject(uri = "direct:error")
+    DirectEndpoint error;
 
     @Test
     public void testSlackMessage() throws Exception {
-        getMockEndpoint("mock:errors").expectedMessageCount(0);
+        errors.expectedMessageCount(0);
 
-        template.sendBody("direct:test", "Hello from Camel!");
+        template.sendBody(test, "Hello from Camel!");
 
         assertMockEndpointsSatisfied();
     }
 
     @Test
     public void testSlackError() throws Exception {
-        getMockEndpoint("mock:errors").expectedMessageCount(1);
+        errors.expectedMessageCount(1);
 
-        template.sendBody("direct:error", "Error from Camel!");
+        template.sendBody(error, "Error from Camel!");
 
         assertMockEndpointsSatisfied();
     }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                SlackComponent slack = new SlackComponent();
+                slack.setWebhookUrl(System.getProperty("SLACK_HOOK", 
"https://hooks.slack.com/services/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";));
+                context.addComponent("slack", slack);
+
+                onException(Exception.class).handled(true).to(errors);
+
+                final String slacUser =  System.getProperty("SLACK_USER", 
"CamelTest");
+                
from(test).to(String.format("slack:#general?iconEmoji=:camel:&username=%s", 
slacUser));
+
+                
from(error).to(String.format("slack:#badchannel?iconEmoji=:camel:&username=%s", 
slack));
+            }
+        };
+    }
 }
diff --git 
a/components/camel-slack/src/test/resources/OSGI-INF/blueprint/blueprint.xml 
b/components/camel-slack/src/test/resources/OSGI-INF/blueprint/blueprint.xml
deleted file mode 100644
index 8dcdaad..0000000
--- a/components/camel-slack/src/test/resources/OSGI-INF/blueprint/blueprint.xml
+++ /dev/null
@@ -1,44 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!--
-
-    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.
-
--->
-<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"; 
default-activation="lazy">
-
-    <bean id="slack" class="org.apache.camel.component.slack.SlackComponent">
-        <property name="webhookUrl" 
value="https://hooks.slack.com/services/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"/>
-    </bean>
-
-    <camelContext xmlns="http://camel.apache.org/schema/blueprint";>
-        <onException>
-            <exception>java.lang.Exception</exception>
-            <handled>
-                <constant>true</constant>
-            </handled>
-            <to uri="mock:errors"/>
-        </onException>
-        <route>
-            <from uri="direct:test"/>
-            <to uri="slack:#general?iconEmoji=:camel:&amp;username=CamelTest"/>
-        </route>
-        <route>
-            <from uri="direct:error"/>
-            <to 
uri="slack:#badchannel?iconEmoji=:camel:&amp;username=CamelTest"/>
-        </route>
-    </camelContext>
-
-</blueprint>

Reply via email to