Updated Branches: refs/heads/master 88a5104f2 -> 1c1d3a883
http://git-wip-us.apache.org/repos/asf/camel/blob/1c1d3a88/components/camel-aws/src/test/java/org/apache/camel/component/aws/swf/CamelSWFWorkflowClientTest.java ---------------------------------------------------------------------- diff --git a/components/camel-aws/src/test/java/org/apache/camel/component/aws/swf/CamelSWFWorkflowClientTest.java b/components/camel-aws/src/test/java/org/apache/camel/component/aws/swf/CamelSWFWorkflowClientTest.java new file mode 100644 index 0000000..42b53fe --- /dev/null +++ b/components/camel-aws/src/test/java/org/apache/camel/component/aws/swf/CamelSWFWorkflowClientTest.java @@ -0,0 +1,143 @@ +/** + * 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.aws.swf; + +import java.util.Collections; +import java.util.Date; +import java.util.List; +import java.util.Map; + +import com.amazonaws.services.simpleworkflow.AmazonSimpleWorkflowClient; +import com.amazonaws.services.simpleworkflow.flow.DynamicWorkflowClientExternal; +import com.amazonaws.services.simpleworkflow.flow.DynamicWorkflowClientExternalImpl; +import com.amazonaws.services.simpleworkflow.model.DescribeWorkflowExecutionRequest; +import com.amazonaws.services.simpleworkflow.model.WorkflowExecution; +import com.amazonaws.services.simpleworkflow.model.WorkflowExecutionDetail; +import com.amazonaws.services.simpleworkflow.model.WorkflowExecutionInfo; +import org.junit.Before; +import org.junit.Test; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +public class CamelSWFWorkflowClientTest { + + private SWFConfiguration configuration; + private AmazonSimpleWorkflowClient swClient; + private SWFEndpoint endpoint; + private CamelSWFWorkflowClient camelSWFWorkflowClient; + private DynamicWorkflowClientExternal clientExternal; + + @Before + public void setUp() throws Exception { + configuration = new SWFConfiguration(); + configuration.setDomainName("testDomain"); + swClient = mock(AmazonSimpleWorkflowClient.class); + configuration.setAmazonSWClient(swClient); + configuration.setStartWorkflowOptionsParameters(Collections.<String, Object>emptyMap()); + + endpoint = new SWFEndpoint(); + endpoint.setConfiguration(configuration); + clientExternal = mock(DynamicWorkflowClientExternalImpl.class); + + camelSWFWorkflowClient = new CamelSWFWorkflowClient(endpoint, configuration) { + @Override + DynamicWorkflowClientExternal getDynamicWorkflowClient(String workflowId, String runId) { + return clientExternal; + } + }; + } + + @Test + public void testDescribeWorkflowInstance() throws Exception { + WorkflowExecutionInfo executionInfo = new WorkflowExecutionInfo(); + executionInfo.setCloseStatus("COMPLETED"); + Date closeTimestamp = new Date(); + executionInfo.setCloseTimestamp(closeTimestamp); + executionInfo.setExecutionStatus("CLOSED"); + executionInfo.setTagList(Collections.EMPTY_LIST); + + WorkflowExecutionDetail workflowExecutionDetail = new WorkflowExecutionDetail(); + workflowExecutionDetail.setExecutionInfo(executionInfo); + + when(swClient.describeWorkflowExecution(any(DescribeWorkflowExecutionRequest.class))).thenReturn(workflowExecutionDetail); + Map<String, Object> description = camelSWFWorkflowClient.describeWorkflowInstance("123", "run1"); + + DescribeWorkflowExecutionRequest describeRequest = new DescribeWorkflowExecutionRequest(); + describeRequest.setDomain(configuration.getDomainName()); + describeRequest.setExecution(new WorkflowExecution().withWorkflowId("123").withRunId("run1")); + + + verify(swClient).describeWorkflowExecution(describeRequest); + assertThat((String) description.get("closeStatus"), is("COMPLETED")); + assertThat((Date) description.get("closeTimestamp"), is(closeTimestamp)); + assertThat((String) description.get("executionStatus"), is("CLOSED")); + assertThat((List) description.get("tagList"), is(Collections.EMPTY_LIST)); + assertThat((WorkflowExecutionDetail) description.get("executionDetail"), is(workflowExecutionDetail)); + } + + @Test + public void testSignalWorkflowExecution() throws Exception { + + camelSWFWorkflowClient.signalWorkflowExecution("123", "run1", "signalMethod", "Hi"); + verify(clientExternal).signalWorkflowExecution("signalMethod", new Object[] {"Hi"}); + } + + @Test + public void testGetWorkflowExecutionState() throws Throwable { + + Class<String> stateType = String.class; + when(clientExternal.getWorkflowExecutionState(stateType)).thenReturn("some state"); + String state = (String) camelSWFWorkflowClient.getWorkflowExecutionState("123", "run1", stateType); + + verify(clientExternal).getWorkflowExecutionState(stateType); + assertThat(state, is("some state")); + } + + @Test + public void testRequestCancelWorkflowExecution() throws Throwable { + camelSWFWorkflowClient.requestCancelWorkflowExecution("123", "run1"); + + verify(clientExternal).requestCancelWorkflowExecution(); + } + + @Test + public void testTerminateWorkflowExecution() throws Throwable { + camelSWFWorkflowClient.terminateWorkflowExecution("123", "run1", "reason", "details", null); + + verify(clientExternal).terminateWorkflowExecution("reason", "details", null); + } + + @Test + public void testStartWorkflowExecution() throws Throwable { + + WorkflowExecution workflowExecution = new WorkflowExecution(); + workflowExecution.setWorkflowId("123"); + workflowExecution.setRunId("run1"); + when(clientExternal.getWorkflowExecution()).thenReturn(workflowExecution); + + + String[] ids = camelSWFWorkflowClient.startWorkflowExecution(null, null, "eventName", "version", null); + + verify(clientExternal).startWorkflowExecution(new Object[]{null}); + assertThat("123", is(ids[0])); + assertThat("run1", is(ids[1])); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/1c1d3a88/components/camel-aws/src/test/java/org/apache/camel/component/aws/swf/CamelSWFWorkflowConsumerTest.java ---------------------------------------------------------------------- diff --git a/components/camel-aws/src/test/java/org/apache/camel/component/aws/swf/CamelSWFWorkflowConsumerTest.java b/components/camel-aws/src/test/java/org/apache/camel/component/aws/swf/CamelSWFWorkflowConsumerTest.java new file mode 100644 index 0000000..2159fb8 --- /dev/null +++ b/components/camel-aws/src/test/java/org/apache/camel/component/aws/swf/CamelSWFWorkflowConsumerTest.java @@ -0,0 +1,66 @@ +/** + * 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.aws.swf; + +import com.amazonaws.services.simpleworkflow.model.DecisionTask; +import com.amazonaws.services.simpleworkflow.model.PollForDecisionTaskRequest; +import org.apache.camel.Exchange; +import org.apache.camel.Predicate; +import org.apache.camel.builder.RouteBuilder; +import org.junit.Test; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.atLeastOnce; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +public class CamelSWFWorkflowConsumerTest extends CamelSWFTestSupport { + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + + public void configure() throws Exception { + from("aws-swf://workflow?" + options) + .to("mock:result"); + } + }; + } + + @Override + public boolean isUseAdviceWith() { + return true; + } + + @Test + public void receivesDecisionTask() throws Exception { + result.expectedMessageCount(1); + result.expectedMessagesMatches(new Predicate() { + public boolean matches(Exchange exchange) { + return exchange.getIn().getHeader(SWFConstants.ACTION) != null; + } + }); + + DecisionTask decisionTask = new DecisionTask(); + decisionTask.setTaskToken("token"); + when(amazonSWClient.pollForDecisionTask(any(PollForDecisionTaskRequest.class))).thenReturn(decisionTask); + + context.start(); + + assertMockEndpointsSatisfied(); + verify(amazonSWClient, atLeastOnce()).pollForDecisionTask(any(PollForDecisionTaskRequest.class)); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/1c1d3a88/components/camel-aws/src/test/java/org/apache/camel/component/aws/swf/CamelSWFWorkflowProducerTest.java ---------------------------------------------------------------------- diff --git a/components/camel-aws/src/test/java/org/apache/camel/component/aws/swf/CamelSWFWorkflowProducerTest.java b/components/camel-aws/src/test/java/org/apache/camel/component/aws/swf/CamelSWFWorkflowProducerTest.java new file mode 100644 index 0000000..f394792 --- /dev/null +++ b/components/camel-aws/src/test/java/org/apache/camel/component/aws/swf/CamelSWFWorkflowProducerTest.java @@ -0,0 +1,60 @@ +/** + * 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.aws.swf; + +import com.amazonaws.services.simpleworkflow.model.Run; +import com.amazonaws.services.simpleworkflow.model.StartWorkflowExecutionRequest; +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.junit.Test; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.when; + +public class CamelSWFWorkflowProducerTest extends CamelSWFTestSupport { + + @Test + public void sendInOnly() throws Exception { + + result.expectedMessageCount(1); + when(amazonSWClient.startWorkflowExecution(any(StartWorkflowExecutionRequest.class))).thenReturn(new Run().withRunId("run1")); + + template.send("direct:start", new Processor() { + public void process(Exchange exchange) throws Exception { + exchange.getIn().setBody("This is my message text."); + } + }); + + assertMockEndpointsSatisfied(); + + Exchange resultExchange = result.getExchanges().get(0); + assertEquals("This is my message text.", resultExchange.getIn().getBody()); + + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + + public void configure() throws Exception { + from("direct:start") + .to("aws-swf://workflow?" + options) + .to("mock:result"); + } + }; + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/1c1d3a88/components/camel-aws/src/test/java/org/apache/camel/component/aws/swf/SwfComponentSpringTest.java ---------------------------------------------------------------------- diff --git a/components/camel-aws/src/test/java/org/apache/camel/component/aws/swf/SwfComponentSpringTest.java b/components/camel-aws/src/test/java/org/apache/camel/component/aws/swf/SwfComponentSpringTest.java new file mode 100644 index 0000000..12b8437 --- /dev/null +++ b/components/camel-aws/src/test/java/org/apache/camel/component/aws/swf/SwfComponentSpringTest.java @@ -0,0 +1,59 @@ +/** + * 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.aws.swf; + +import org.apache.camel.EndpointInject; +import org.apache.camel.Exchange; +import org.apache.camel.ExchangePattern; +import org.apache.camel.Processor; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.component.aws.sqs.SqsConstants; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.test.spring.CamelSpringTestSupport; +import org.junit.Test; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class SwfComponentSpringTest extends CamelSpringTestSupport { + + @EndpointInject(uri = "direct:start") + private ProducerTemplate template; + + @EndpointInject(uri = "mock:result") + private MockEndpoint result; + + @Test + public void sendInOut() throws Exception { + result.expectedMessageCount(1); + + template.send("direct:start", new Processor() { + public void process(Exchange exchange) throws Exception { + exchange.getIn().setHeader(SWFConstants.WORKFLOW_ID, "123"); + } + }); + + assertMockEndpointsSatisfied(); + + Exchange resultExchange = result.getExchanges().get(0); + assertNotNull(resultExchange.getIn().getHeader(SWFConstants.WORKFLOW_ID)); + assertNotNull(resultExchange.getIn().getHeader(SWFConstants.RUN_ID)); + } + + @Override + protected ClassPathXmlApplicationContext createApplicationContext() { + return new ClassPathXmlApplicationContext("org/apache/camel/component/aws/swf/SwfComponentSpringTest-context.xml"); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/1c1d3a88/components/camel-aws/src/test/java/org/apache/camel/component/aws/swf/integration/CamelSWFEndToEndTest.java ---------------------------------------------------------------------- diff --git a/components/camel-aws/src/test/java/org/apache/camel/component/aws/swf/integration/CamelSWFEndToEndTest.java b/components/camel-aws/src/test/java/org/apache/camel/component/aws/swf/integration/CamelSWFEndToEndTest.java new file mode 100644 index 0000000..1a9fdf1 --- /dev/null +++ b/components/camel-aws/src/test/java/org/apache/camel/component/aws/swf/integration/CamelSWFEndToEndTest.java @@ -0,0 +1,90 @@ +/** + * 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.aws.swf.integration; + +import org.apache.camel.EndpointInject; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.aws.swf.SWFConstants; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.Ignore; +import org.junit.Test; + +import static org.apache.camel.component.mock.MockEndpoint.assertIsSatisfied; + +@Ignore("Must be manually tested. Provide your own accessKey and secretKey and also create a SWF domain in advance") +public class CamelSWFEndToEndTest extends CamelTestSupport { + protected String options = + "accessKey=XXX" + + "&secretKey=YYY" + + "&domainName=ZZZ" + + "&activityList=swf-alist" + + "&workflowList=swf-wlist" + + "&clientConfiguration.endpoint=swf.eu-west-1.amazonaws.com" + + "&version=1.0"; + + @EndpointInject(uri = "mock:starter") + private MockEndpoint starter; + + @EndpointInject(uri = "mock:decider") + private MockEndpoint decider; + + @EndpointInject(uri = "mock:worker") + private MockEndpoint worker; + + @Test + public void consumerReceivedPreAndPostEntryCreatedEventNotifications() throws Exception { + starter.expectedMessageCount(1); + decider.expectedMinimumMessageCount(1); + worker.expectedMessageCount(2); + + template.requestBody("direct:start", "Hello world!"); + + assertIsSatisfied(context); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + public void configure() { + + from("aws-swf://activity?" + options + "&eventName=processActivities") + .log("FOUND ACTIVITY TASK ${body}") + .setBody(constant("1")) + .to("mock:worker"); + + from("aws-swf://workflow?" + options + "&eventName=processWorkflows") + .log("FOUND WORKFLOW TASK ${body}") + + .filter(header(SWFConstants.ACTION).isEqualTo(SWFConstants.EXECUTE_ACTION)) + + .to("aws-swf://activity?" + options + "&eventName=processActivities") + + .setBody(constant("Message two")) + .to("aws-swf://activity?" + options + "&eventName=processActivities") + + .log("SENT ACTIVITY TASK ${body}") + .to("mock:decider"); + + from("direct:start") + .to("aws-swf://workflow?" + options + "&eventName=processWorkflows") + .log("SENT WORKFLOW TASK ${body}") + .to("mock:starter"); + } + }; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/1c1d3a88/components/camel-aws/src/test/resources/org/apache/camel/component/aws/swf/SwfComponentSpringTest-context.xml ---------------------------------------------------------------------- diff --git a/components/camel-aws/src/test/resources/org/apache/camel/component/aws/swf/SwfComponentSpringTest-context.xml b/components/camel-aws/src/test/resources/org/apache/camel/component/aws/swf/SwfComponentSpringTest-context.xml new file mode 100644 index 0000000..4b02072 --- /dev/null +++ b/components/camel-aws/src/test/resources/org/apache/camel/component/aws/swf/SwfComponentSpringTest-context.xml @@ -0,0 +1,32 @@ +<?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. +--> +<beans xmlns="http://www.springframework.org/schema/beans" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation=" + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"> + + <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring"> + <route> + <from uri="direct:start"/> + <to uri="aws-swf://workflow?amazonSWClient=#amazonSWClient&accessKey=key&secretKey=secret&domainName=testDomain&version=1.0&eventName=testEvent"/> + <to uri="mock:result"/> + </route> + </camelContext> + + <bean id="amazonSWClient" class="org.apache.camel.component.aws.swf.AmazonSWFClientMock"/> +</beans> \ No newline at end of file