| 1 |
/** |
| 2 |
* |
| 3 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
| 4 |
* contributor license agreements. See the NOTICE file distributed with |
| 5 |
* this work for additional information regarding copyright ownership. |
| 6 |
* The ASF licenses this file to You under the Apache License, Version 2.0 |
| 7 |
* (the "License"); you may not use this file except in compliance with |
| 8 |
* the License. You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, software |
| 13 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
* See the License for the specific language governing permissions and |
| 16 |
* limitations under the License. |
| 17 |
*/ |
| 18 |
package org.apache.camel.component.context; |
| 19 |
|
| 20 |
import org.apache.camel.CamelContext; |
| 21 |
import org.apache.camel.EndpointInject; |
| 22 |
import org.apache.camel.Produce; |
| 23 |
import org.apache.camel.ProducerTemplate; |
| 24 |
import org.apache.camel.builder.RouteBuilder; |
| 25 |
import org.apache.camel.component.mock.MockEndpoint; |
| 26 |
import org.apache.camel.impl.DefaultCamelContext; |
| 27 |
import org.apache.camel.impl.JndiRegistry; |
| 28 |
import org.apache.camel.test.CamelTestSupport; |
| 29 |
import org.junit.Test; |
| 30 |
|
| 31 |
/** |
| 32 |
* Lets use the Java DSL to create a black box CamelContext and then test its use from another context |
| 33 |
*/ |
| 34 |
public class JavaDslBlackBoxTest extends CamelTestSupport { |
| 35 |
|
| 36 |
@EndpointInject(uri = "mock:results") |
| 37 |
private MockEndpoint resultEndpoint; |
| 38 |
|
| 39 |
@Produce(uri = "direct:start") |
| 40 |
private ProducerTemplate template; |
| 41 |
|
| 42 |
@Test |
| 43 |
public void testUsingBlackBox() throws Exception { |
| 44 |
resultEndpoint.expectedHeaderReceived("received", "true"); |
| 45 |
resultEndpoint.expectedMessageCount(2); |
| 46 |
|
| 47 |
template.sendBody("<purchaseOrder>one</purchaseOrder>"); |
| 48 |
template.sendBody("<purchaseOrder>two</purchaseOrder>"); |
| 49 |
|
| 50 |
assertMockEndpointsSatisfied(); |
| 51 |
} |
| 52 |
|
| 53 |
@Override |
| 54 |
protected JndiRegistry createRegistry() throws Exception { |
| 55 |
JndiRegistry registry = super.createRegistry(); |
| 56 |
|
| 57 |
// lets create our black box as a camel context and a set of routes |
| 58 |
DefaultCamelContext blackBox = new DefaultCamelContext(registry); |
| 59 |
blackBox.setName("blackBox"); |
| 60 |
blackBox.addRoutes(new RouteBuilder() { |
| 61 |
@Override |
| 62 |
public void configure() throws Exception { |
| 63 |
// receive purchase orders, lets process it in some way then send an invoice |
| 64 |
// to our invoice endpoint |
| 65 |
from("direct:purchaseOrder").setHeader("received").constant("true").to("direct:invoice"); |
| 66 |
} |
| 67 |
}); |
| 68 |
blackBox.start(); |
| 69 |
|
| 70 |
registry.bind("accounts", blackBox); |
| 71 |
return registry; |
| 72 |
|
| 73 |
} |
| 74 |
|
| 75 |
@Override |
| 76 |
protected RouteBuilder createRouteBuilder() throws Exception { |
| 77 |
return new RouteBuilder() { |
| 78 |
@Override |
| 79 |
public void configure() throws Exception { |
| 80 |
from("direct:start").to("accounts:purchaseOrder"); |
| 81 |
from("accounts:invoice").to("mock:results"); |
| 82 |
} |
| 83 |
}; |
| 84 |
} |
| 85 |
} |