CAMEL-8852 Added producer test related to different workspace URI param
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/73640069 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/73640069 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/73640069 Branch: refs/heads/master Commit: 73640069e8d5368690b2929a3e54e28adc015f05 Parents: 0521060 Author: Andrea Cosentino <anco...@gmail.com> Authored: Fri Jun 26 15:37:19 2015 +0200 Committer: Andrea Cosentino <anco...@gmail.com> Committed: Fri Jun 26 21:58:41 2015 +0200 ---------------------------------------------------------------------- .../jcr/JcrConsumerDifferentWorkspaceTest.java | 141 +++++++++++++++++++ .../jcr/JcrProducerDifferentWorkspaceTest.java | 64 +++++++++ .../JcrRouteDifferentWorkspaceTestSupport.java | 79 +++++++++++ 3 files changed, 284 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/73640069/components/camel-jcr/src/test/java/org/apache/camel/component/jcr/JcrConsumerDifferentWorkspaceTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jcr/src/test/java/org/apache/camel/component/jcr/JcrConsumerDifferentWorkspaceTest.java b/components/camel-jcr/src/test/java/org/apache/camel/component/jcr/JcrConsumerDifferentWorkspaceTest.java new file mode 100644 index 0000000..cef11b8 --- /dev/null +++ b/components/camel-jcr/src/test/java/org/apache/camel/component/jcr/JcrConsumerDifferentWorkspaceTest.java @@ -0,0 +1,141 @@ +/** + * 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.jcr; + +import java.util.List; +import javax.jcr.Node; +import javax.jcr.Session; +import javax.jcr.observation.Event; +import javax.jcr.observation.EventIterator; + +import org.apache.camel.Exchange; +import org.apache.camel.Message; +import org.apache.camel.builder.RouteBuilder; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class JcrConsumerDifferentWorkspaceTest extends JcrRouteDifferentWorkspaceTestSupport { + + private static final Logger LOG = LoggerFactory.getLogger(JcrConsumerDifferentWorkspaceTest.class); + + private String absPath = "/home/test"; + private int eventTypes = Event.NODE_ADDED; + private boolean deep = true; + private boolean noLocal; + + @Test + public void testJcrConsumer() throws Exception { + // start consumer thread first + JcrConsumerThread consumerThread = new JcrConsumerThread(); + consumerThread.start(); + // wait until the consumer thread has tried to receive event at least once + while (consumerThread.getReceiveTrialTimes() < 1) { + Thread.sleep(10L); + } + + // now create a node under the specified event node path + + Session session = openSession(CUSTOM_WORKSPACE_NAME); + + try { + Node folderNode = session.getRootNode(); + + for (String folderNodeName : absPath.split("\\/")) { + if (!"".equals(folderNodeName)) { + if (folderNode.hasNode(folderNodeName)) { + folderNode.getNode(folderNodeName).remove(); + } + + folderNode = folderNode.addNode(folderNodeName, "nt:unstructured"); + } + } + + folderNode.addNode("node", "nt:unstructured"); + session.save(); + } finally { + if (session != null && session.isLive()) { + session.logout(); + } + } + + // wait until the consumer thread captures an event + consumerThread.join(); + + Exchange exchange = consumerThread.getExchange(); + assertNotNull(exchange); + + Message message = exchange.getIn(); + assertNotNull(message); + assertTrue(message instanceof JcrMessage); + EventIterator eventIterator = ((JcrMessage)message).getEventIterator(); + assertNotNull(eventIterator); + assertEquals(1, eventIterator.getSize()); + + List<?> eventList = message.getBody(List.class); + assertEquals(1, eventList.size()); + Event event = (Event) eventList.get(0); + assertEquals(Event.NODE_ADDED, event.getType()); + assertNotNull(event.getPath()); + assertTrue(event.getPath().startsWith(absPath)); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + String uri = "jcr://user:pass@repository"; + uri += absPath; + uri += "?eventTypes=" + eventTypes; + uri += "&deep=" + deep; + uri += "&noLocal=" + noLocal; + uri += "&workspaceName=" + CUSTOM_WORKSPACE_NAME; + from(uri).to("direct:a"); + } + }; + } + + private class JcrConsumerThread extends Thread { + + private Exchange exchange; + private int receiveTrialTimes; + + public void run() { + while (exchange == null) { + exchange = consumer.receive("direct:a", 10L); + ++receiveTrialTimes; + + try { + Thread.sleep(10); + } catch (InterruptedException e) { + break; + } + } + + LOG.debug("JcrConsumerThread receive exchange, {} after {} trials", exchange, receiveTrialTimes); + } + + public Exchange getExchange() { + return exchange; + } + + public int getReceiveTrialTimes() { + return receiveTrialTimes; + } + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/73640069/components/camel-jcr/src/test/java/org/apache/camel/component/jcr/JcrProducerDifferentWorkspaceTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jcr/src/test/java/org/apache/camel/component/jcr/JcrProducerDifferentWorkspaceTest.java b/components/camel-jcr/src/test/java/org/apache/camel/component/jcr/JcrProducerDifferentWorkspaceTest.java new file mode 100644 index 0000000..a6d0ac6 --- /dev/null +++ b/components/camel-jcr/src/test/java/org/apache/camel/component/jcr/JcrProducerDifferentWorkspaceTest.java @@ -0,0 +1,64 @@ +/** + * 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.jcr; + +import javax.jcr.Node; +import javax.jcr.Session; +import javax.jcr.Workspace; + +import org.apache.camel.Exchange; +import org.apache.camel.builder.RouteBuilder; +import org.junit.Test; + +public class JcrProducerDifferentWorkspaceTest extends JcrRouteDifferentWorkspaceTestSupport { + + @Test + public void testJcrProducer() throws Exception { + Exchange exchange = createExchangeWithBody("<hello>world!</hello>"); + Exchange out = template.send("direct:a", exchange); + assertNotNull(out); + String uuid = out.getOut().getBody(String.class); + Session session = openSession(CUSTOM_WORKSPACE_NAME); + try { + Node node = session.getNodeByIdentifier(uuid); + Workspace workspace = session.getWorkspace(); + assertEquals(CUSTOM_WORKSPACE_NAME, workspace.getName()); + assertNotNull(node); + assertEquals("/home/test/node", node.getPath()); + assertEquals("<hello>world!</hello>", node.getProperty("my.contents.property").getString()); + } finally { + if (session != null && session.isLive()) { + session.logout(); + } + } + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + // START SNIPPET: jcr-create-node + from("direct:a").setHeader(JcrConstants.JCR_NODE_NAME, constant("node")) + .setHeader("my.contents.property", body()) + .to("jcr://user:pass@repository/home/test?workspaceName=" + CUSTOM_WORKSPACE_NAME); + // END SNIPPET: jcr-create-node + } + }; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/73640069/components/camel-jcr/src/test/java/org/apache/camel/component/jcr/JcrRouteDifferentWorkspaceTestSupport.java ---------------------------------------------------------------------- diff --git a/components/camel-jcr/src/test/java/org/apache/camel/component/jcr/JcrRouteDifferentWorkspaceTestSupport.java b/components/camel-jcr/src/test/java/org/apache/camel/component/jcr/JcrRouteDifferentWorkspaceTestSupport.java new file mode 100644 index 0000000..2f81449 --- /dev/null +++ b/components/camel-jcr/src/test/java/org/apache/camel/component/jcr/JcrRouteDifferentWorkspaceTestSupport.java @@ -0,0 +1,79 @@ +/** + * 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.jcr; + +import java.io.File; +import java.io.FileNotFoundException; + +import javax.jcr.Repository; +import javax.jcr.RepositoryException; +import javax.jcr.Session; +import javax.jcr.SimpleCredentials; +import javax.jcr.Workspace; +import javax.naming.Context; + +import org.apache.camel.test.junit4.CamelTestSupport; +import org.apache.jackrabbit.core.TransientRepository; +import org.junit.Before; + +/** + * JcrRouteDifferentWorkspaceTestSupport + * + */ +public abstract class JcrRouteDifferentWorkspaceTestSupport extends CamelTestSupport { + + protected static final String CONFIG_FILE = "target/test-classes/repository-simple-security.xml"; + + protected static final String REPO_PATH = "target/repository-simple-diff-workspace"; + + protected static final String CUSTOM_WORKSPACE_NAME = "testWorkspace"; + + private Repository repository; + + @Override + @Before + public void setUp() throws Exception { + deleteDirectory(REPO_PATH); + super.setUp(); + Session session = getRepository().login(new SimpleCredentials("user", "pass".toCharArray())); + Workspace workspace = session.getWorkspace(); + workspace.createWorkspace(CUSTOM_WORKSPACE_NAME); + session.save(); + session.logout(); + } + + protected Repository getRepository() { + return repository; + } + + protected Session openSession(String workspaceName) throws RepositoryException { + return getRepository().login(new SimpleCredentials("user", "pass".toCharArray()), workspaceName); + } + + @Override + protected Context createJndiContext() throws Exception { + File config = new File(CONFIG_FILE); + if (!config.exists()) { + throw new FileNotFoundException("Missing config file: " + config.getPath()); + } + + Context context = super.createJndiContext(); + repository = new TransientRepository(CONFIG_FILE, REPO_PATH); + context.bind("repository", repository); + return context; + } +}