public void testSplitterWithException() throws Exception { MockEndpoint resultEndpoint = getMockEndpoint("mock:result"); resultEndpoint.expectedMessageCount(4); resultEndpoint.expectedHeaderReceived("foo", "bar"); MockEndpoint failedEndpoint = getMockEndpoint("mock:failed"); failedEndpoint.expectedMessageCount(1); failedEndpoint.expectedHeaderReceived("foo", "bar"); Exchange result = template.request( "direct:exception", new Processor() { public void process(Exchange exchange) { Message in = exchange.getIn(); in.setBody("James,Guillaume,Hiram,Rob,Exception"); in.setHeader("foo", "bar"); } }); assertTrue( "The result exchange should have a camel exception", result.getException() instanceof CamelException); assertMockEndpointsSatisfied(); }
@Test public void testSendMessageToPartner() throws Exception { mockAgreement1.setExpectedMessageCount(1); mockAgreement2.setExpectedMessageCount(1); mockUpdateMessageStore.setExpectedMessageCount(4); mockEbmsResponseInbound.setExpectedMessageCount(1); mockEbmsResponseInbound.expectedBodiesReceived( IOUtils.toString(new FileInputStream(fileFromClasspath("simple-as4-receipt.xml")))); mockEbmsResponseInbound.expectedHeaderReceived( EbmsConstants.CONTENT_TYPE, EbmsConstants.SOAP_XML_CONTENT_TYPE); mockEbmsResponseInbound.expectedHeaderReceived(Exchange.HTTP_METHOD, "POST"); sendMessage( "agreement1", "simple-as4-user-message.txt", "Multipart/Related; boundary=\"----=_Part_7_10584188.1123489648993\"; type=\"application/soap+xml\"; start=\"<*****@*****.**>\"", "*****@*****.**", MessageType.USER_MESSAGE); sendMessage( "agreement2", "simple-as4-receipt.xml", EbmsConstants.SOAP_XML_CONTENT_TYPE, "*****@*****.**", MessageType.SIGNAL_MESSAGE_WITH_USER_MESSAGE); assertMockEndpointsSatisfied(); Exchange e = mockEbmsResponseInbound.getExchanges().get(0); assertThat(e.getIn().getHeaders().values(), hasSize(3)); }
@Test public void testTransaction() throws InterruptedException { // Assertions deletedEndpoint.expectedMessageCount(8); deletedEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 204); transactedEndpoint.expectedMessageCount(1); verifiedEndpoint.expectedMessageCount(3); midtransactionEndpoint.expectedMessageCount(3); midtransactionEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 201); notfoundEndpoint.expectedMessageCount(6); notfoundEndpoint.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 404); // Start the transaction final Map<String, Object> headers = new HashMap<>(); headers.put(Exchange.HTTP_METHOD, "POST"); headers.put(Exchange.CONTENT_TYPE, "text/turtle"); // Create the object final String fullPath = template.requestBodyAndHeaders( "direct:create", FcrepoTestUtils.getTurtleDocument(), headers, String.class); assertNotNull(fullPath); final String identifier = fullPath.replaceAll(FcrepoTestUtils.getFcrepoBaseUrl(), ""); // Test the creation of several objects template.sendBodyAndHeader("direct:transact", null, "TestIdentifierBase", identifier); // Test the object template.sendBodyAndHeader( "direct:verify", null, FcrepoHeaders.FCREPO_IDENTIFIER, identifier + "/one"); template.sendBodyAndHeader( "direct:verify", null, FcrepoHeaders.FCREPO_IDENTIFIER, identifier + "/two"); template.sendBodyAndHeader( "direct:verify", null, FcrepoHeaders.FCREPO_IDENTIFIER, identifier + "/three"); // Teardown template.sendBodyAndHeader( "direct:teardown", null, FcrepoHeaders.FCREPO_IDENTIFIER, identifier + "/one"); template.sendBodyAndHeader( "direct:teardown", null, FcrepoHeaders.FCREPO_IDENTIFIER, identifier + "/two"); template.sendBodyAndHeader( "direct:teardown", null, FcrepoHeaders.FCREPO_IDENTIFIER, identifier + "/three"); template.sendBodyAndHeader( "direct:teardown", null, FcrepoHeaders.FCREPO_IDENTIFIER, identifier); // Confirm assertions verifiedEndpoint.assertIsSatisfied(); deletedEndpoint.assertIsSatisfied(); transactedEndpoint.assertIsSatisfied(); notfoundEndpoint.assertIsSatisfied(); midtransactionEndpoint.assertIsSatisfied(); }
@Test @Ignore public void testHttpGetWithISO8859EncodedParamsViaURI() throws Exception { MockEndpoint mock = getMockEndpoint("mock:result"); mock.expectedMessageCount(1); mock.expectedHeaderReceived("message", "Keine g\u00C6ltige GPS-Daten!"); mock.expectedHeaderReceived(Exchange.HTTP_METHOD, "GET"); template.requestBody(serverUri + "&message=Keine+g%C6ltige+GPS-Daten%21", null, Object.class); assertMockEndpointsSatisfied(); }
@Test public void testHttpGetWithSpaceAsPlusInParams() throws Exception { MockEndpoint mock = getMockEndpoint("mock:result"); mock.expectedMessageCount(1); mock.expectedHeaderReceived("message", " World"); mock.expectedHeaderReceived(Exchange.HTTP_METHOD, "GET"); // parameter starts with a space using + decoded template.requestBody(serverUri + "&message=+World", null, Object.class); assertMockEndpointsSatisfied(); }
@Test public void testHttpGetWithParamsViaURI() throws Exception { MockEndpoint mock = getMockEndpoint("mock:result"); mock.expectedMessageCount(1); mock.expectedHeaderReceived("one", "einz"); mock.expectedHeaderReceived("two", "twei"); mock.expectedHeaderReceived(Exchange.HTTP_METHOD, "GET"); template.requestBody(serverUri + "&one=einz&two=twei", null, Object.class); assertMockEndpointsSatisfied(); }
@Test @Ignore("HTTP_QUERY not yet supported") public void testHttpGetWithParamsViaHeader() throws Exception { MockEndpoint mock = getMockEndpoint("mock:result"); mock.expectedMessageCount(1); mock.expectedHeaderReceived("one", "uno"); mock.expectedHeaderReceived("two", "dos"); mock.expectedHeaderReceived(Exchange.HTTP_METHOD, "GET"); template.requestBodyAndHeader(serverUri, null, Exchange.HTTP_QUERY, "one=uno&two=dos"); assertMockEndpointsSatisfied(); }
@Test public void testInsert() throws IOException, InterruptedException { final String base = "http://localhost/rest"; final String path = "/path/a/b/c"; final String document = getN3Document(); // Assertions resultEndpoint.allMessages().body().contains("update=INSERT DATA { "); resultEndpoint.allMessages().body().contains(" }"); for (final String s : document.split("\n")) { resultEndpoint.expectedBodyReceived().body().contains(s); } resultEndpoint .expectedBodyReceived() .body() .contains("<" + base + path + "> dc:title \"some title\" ."); resultEndpoint.expectedHeaderReceived("Content-Type", "application/x-www-form-urlencoded"); resultEndpoint.expectedHeaderReceived(Exchange.HTTP_METHOD, "POST"); // Test final Map<String, Object> headers = new HashMap<>(); headers.put(FcrepoHeaders.FCREPO_BASE_URL, base); headers.put(FcrepoHeaders.FCREPO_IDENTIFIER, path); headers.put(Exchange.CONTENT_TYPE, "application/n-triples"); template.sendBodyAndHeaders(document, headers); headers.clear(); headers.put(JmsHeaders.BASE_URL, base); headers.put(JmsHeaders.IDENTIFIER, path); headers.put(Exchange.CONTENT_TYPE, "application/n-triples"); template.sendBodyAndHeaders(document, headers); headers.clear(); headers.put(JmsHeaders.BASE_URL, base); headers.put(FcrepoHeaders.FCREPO_IDENTIFIER, path); headers.put(Exchange.CONTENT_TYPE, "text/turtle"); template.sendBodyAndHeaders(getTurtleDocument(), headers); headers.clear(); headers.put(FcrepoHeaders.FCREPO_BASE_URL, base); headers.put(JmsHeaders.IDENTIFIER, path); headers.put(Exchange.CONTENT_TYPE, "application/n-triples"); template.sendBodyAndHeaders(document, headers); // Confirm that assertions passed resultEndpoint.expectedMessageCount(4); resultEndpoint.assertIsSatisfied(); }
public void testXPathWithNamespaceOther() throws Exception { MockEndpoint mock = getMockEndpoint("mock:result"); mock.expectedBodiesReceived("<number xmlns=\"http://acme.com/cheese\">99</number>"); mock.expectedHeaderReceived("foo", 99); template.sendBody("direct:in", "<number xmlns=\"http://acme.com/cheese\">99</number>"); mock.assertIsSatisfied(); }
@Test public void testDefault() throws Exception { mock.expectedBodiesReceived("test1"); mock.expectedHeaderReceived("test", "test2"); producerTemplate.sendBodyAndHeader("direct:input", "test1", "test", "test2"); mock.assertIsSatisfied(); Message received = mock.getExchanges().get(0).getIn(); assertEquals("default", received.getHeader(GTaskBinding.GTASK_QUEUE_NAME)); assertEquals(0, received.getHeader(GTaskBinding.GTASK_RETRY_COUNT)); }
@Test public void testUsingContextComponent() throws Exception { resultEndpoint.expectedHeaderReceived("received", "true"); resultEndpoint.expectedMessageCount(2); template.sendBody("<purchaseOrder>one</purchaseOrder>"); template.sendBody("<purchaseOrder>two</purchaseOrder>"); assertMockEndpointsSatisfied(); }
@Test public void testSimulate404() throws Exception { MockEndpoint mock = getMockEndpoint("mock:result"); mock.expectedBodiesReceived("Page not found"); mock.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 404); template.sendBody("direct:start", "Hello World"); assertMockEndpointsSatisfied(); }
@Test public void aggregatesTwoMessagesIntoOne() throws Exception { mockAggregated.expectedHeaderReceived("invoiceItemTotal", BigDecimal.valueOf(5)); mockAggregated.expectedMessageCount(1); start.sendBodyAndHeaders( null, toHeadersMap("invoiceId", "invoiceOne", "invoiceItemTotal", BigDecimal.valueOf(2))); start.sendBodyAndHeaders( null, toHeadersMap("invoiceId", "invoiceOne", "invoiceItemTotal", BigDecimal.valueOf(3))); assertMockEndpointsSatisfied(); }
@Test public void testHttpPost() throws Exception { MockEndpoint mock = getMockEndpoint("mock:result"); mock.expectedMessageCount(1); mock.expectedBodiesReceived("Hello World"); mock.expectedHeaderReceived(Exchange.HTTP_METHOD, "POST"); template.requestBody(serverUri, "Hello World"); assertMockEndpointsSatisfied(); }
public void testCustomPolicy() throws Exception { MockEndpoint mock = getMockEndpoint(ERROR_QUEUE); mock.expectedMessageCount(1); mock.expectedHeaderReceived(MESSAGE_INFO, "Damm my policy exception"); try { template.sendBody("direct:a", "Hello Camel"); } catch (Exception e) { // expected } mock.assertIsSatisfied(); }
@Test public void testSendMessage() throws Exception { MockEndpoint mock = getMandatoryEndpoint("mock:bar", MockEndpoint.class); assertNotNull("The mock endpoint should not be null", mock); PersonType expected = new PersonType(); expected.setFirstName("FOO"); expected.setLastName("BAR"); mock.expectedBodiesReceived(expected); mock.expectedHeaderReceived("foo", "bar"); template.sendBodyAndHeader( "direct:start", "<Person><firstName>FOO</firstName><lastName>BAR</lastName></Person>", "foo", "bar"); assertMockEndpointsSatisfied(); }
public void testRemoveHeadersWithExcludePatterns() throws Exception { MockEndpoint mockEndpoint = getMockEndpoint("mock:result"); mockEndpoint.expectedMessageCount(1); mockEndpoint.expectedHeaderReceived("header1", "value1"); mockEndpoint.expectedHeaderReceived("header3", "value3"); Map<String, Object> headers = new HashMap<String, Object>(); headers.put("header1", "value1"); headers.put("header2", "value2"); headers.put("header3", "value3"); template.sendBodyAndHeaders("direct:start", null, headers); assertMockEndpointsSatisfied(); }
public void testFireAndForgetUsingProcessor() throws Exception { context.addRoutes( new RouteBuilder() { @SuppressWarnings("deprecation") @Override public void configure() throws Exception { // START SNIPPET: e1 from("direct:start") .wireTap( "direct:foo", new Processor() { public void process(Exchange exchange) throws Exception { String body = exchange.getIn().getBody(String.class); exchange.getIn().setBody("Bye " + body); exchange.getIn().setHeader("foo", "bar"); } }) .to("mock:result"); from("direct:foo").to("mock:foo"); // END SNIPPET: e1 } }); context.start(); MockEndpoint result = getMockEndpoint("mock:result"); result.expectedBodiesReceived("World"); MockEndpoint foo = getMockEndpoint("mock:foo"); foo.expectedBodiesReceived("Bye World"); foo.expectedHeaderReceived("foo", "bar"); template.sendBody("direct:start", "World"); assertMockEndpointsSatisfied(); // should be different exchange instances Exchange e1 = result.getReceivedExchanges().get(0); Exchange e2 = foo.getReceivedExchanges().get(0); assertNotSame("Should not be same Exchange", e1, e2); // should have same from endpoint assertEquals("direct://start", e1.getFromEndpoint().getEndpointUri()); assertEquals("direct://start", e2.getFromEndpoint().getEndpointUri()); }
@Test public void testUseOriginalMessage() throws Exception { context.addRoutes( new RouteBuilder() { @Override public void configure() throws Exception { onException(Exception.class).useOriginalMessage().to(mockResult); from("jms:queue:foo").throwException(new Exception("forced exception for test")); } }); context.start(); mockResult.expectedBodiesReceived("Hello World"); mockResult.expectedHeaderReceived("header-key", "header-value"); template.sendBodyAndHeader("jms:queue:foo", "Hello World", "header-key", "header-value"); assertMockEndpointsSatisfied(); }
@Test public void testUnmarshal() throws Exception { final String xml = "<Person><firstName>FOO</firstName><lastName>BAR</lastName></Person>"; PersonType expected = new PersonType(); expected.setFirstName("FOO"); expected.setLastName("BAR"); MockEndpoint resultEndpoint = resolveMandatoryEndpoint("mock:result", MockEndpoint.class); resultEndpoint.expectedBodiesReceived(expected); resultEndpoint.expectedHeaderReceived("foo", "bar"); template.sendBodyAndHeader("direct:getJAXBElementValue", xml, "foo", "bar"); resultEndpoint.assertIsSatisfied(); resultEndpoint.reset(); resultEndpoint.expectedMessageCount(1); template.sendBody("direct:getJAXBElement", xml); resultEndpoint.assertIsSatisfied(); assertTrue( "We should get the JAXBElement here", resultEndpoint.getExchanges().get(0).getIn().getBody() instanceof JAXBElement); }
@Test public void testSftpSimpleConsume() throws Exception { if (!canTest()) { return; } String expected = "Hello World"; // create file using regular file template.sendBodyAndHeader("file://" + FTP_ROOT_DIR, expected, Exchange.FILE_NAME, "hello.txt"); MockEndpoint mock = getMockEndpoint("mock:result"); mock.expectedMessageCount(1); mock.expectedHeaderReceived(Exchange.FILE_NAME, "hello.txt"); mock.expectedBodiesReceived(expected); context.startRoute("foo"); assertMockEndpointsSatisfied(); }
@Test public void testPostStatusUpdateRequestResponse() throws Exception { Date now = new Date(); String tweet = "UserProducerInOnlyTest: This is a tweet posted on " + now.toString(); LOG.info("Tweet: " + tweet); ProducerTemplate producerTemplate = context.createProducerTemplate(); // send tweet to the twitter endpoint producerTemplate.sendBodyAndHeader("direct:tweets", tweet, "customHeader", 12312); resultEndpoint.expectedMessageCount(1); resultEndpoint.expectedBodyReceived().body(String.class); // Message headers should be preserved resultEndpoint.expectedHeaderReceived("customHeader", 12312); resultEndpoint.assertIsSatisfied(); List<Exchange> tweets = resultEndpoint.getExchanges(); assertNotNull(tweets); assertThat(tweets.size(), is(1)); String receivedTweet = tweets.get(0).getIn().getBody(String.class); assertThat(receivedTweet, is(tweet)); }
public void testSplitterWithStreaming() throws Exception { MockEndpoint resultEndpoint = getMockEndpoint("mock:result"); resultEndpoint.expectedMessageCount(5); resultEndpoint.expectedHeaderReceived("foo", "bar"); template.request( "direct:streaming", new Processor() { public void process(Exchange exchange) { Message in = exchange.getIn(); in.setBody("James,Guillaume,Hiram,Rob,Roman"); in.setHeader("foo", "bar"); } }); assertMockEndpointsSatisfied(); for (Exchange exchange : resultEndpoint.getReceivedExchanges()) { assertNotNull(exchange.getProperty(Exchange.SPLIT_INDEX)); // this header cannot be set when streaming is used assertNull(exchange.getProperty(Exchange.SPLIT_SIZE)); } }
@Test public void testFtpSimpleConsumeAbsolute() throws Exception { if (!canTest()) { return; } String expected = "Hello World"; // create file using regular file // FTP Server does not support absolute path, so lets simulate it String path = FTP_ROOT_DIR + "/tmp/mytemp"; template.sendBodyAndHeader("file:" + path, expected, Exchange.FILE_NAME, "hello.txt"); MockEndpoint mock = getMockEndpoint("mock:result"); mock.expectedMessageCount(1); mock.expectedHeaderReceived(Exchange.FILE_NAME, "hello.txt"); context.startRoute("foo"); assertMockEndpointsSatisfied(); GenericFile<?> remoteFile = (GenericFile<?>) mock.getExchanges().get(0).getIn().getBody(); assertTrue(remoteFile.getBody() instanceof InputStream); }
public void testManagedCamelContext() throws Exception { // JMX tests dont work well on AIX CI servers (hangs them) if (isPlatform("aix")) { return; } MBeanServer mbeanServer = getMBeanServer(); ObjectName on = ObjectName.getInstance("org.apache.camel:context=19-camel-1,type=context,name=\"camel-1\""); assertTrue("Should be registered", mbeanServer.isRegistered(on)); String name = (String) mbeanServer.getAttribute(on, "CamelId"); assertEquals("camel-1", name); String managementName = (String) mbeanServer.getAttribute(on, "ManagementName"); assertEquals("19-camel-1", managementName); String uptime = (String) mbeanServer.getAttribute(on, "Uptime"); assertNotNull(uptime); String status = (String) mbeanServer.getAttribute(on, "State"); assertEquals("Started", status); Boolean messageHistory = (Boolean) mbeanServer.getAttribute(on, "MessageHistory"); assertEquals(Boolean.TRUE, messageHistory); Integer total = (Integer) mbeanServer.getAttribute(on, "TotalRoutes"); assertEquals(2, total.intValue()); Integer started = (Integer) mbeanServer.getAttribute(on, "StartedRoutes"); assertEquals(2, started.intValue()); // invoke operations MockEndpoint mock = getMockEndpoint("mock:result"); mock.expectedBodiesReceived("Hello World"); mbeanServer.invoke( on, "sendBody", new Object[] {"direct:start", "Hello World"}, new String[] {"java.lang.String", "java.lang.Object"}); assertMockEndpointsSatisfied(); resetMocks(); mock.expectedBodiesReceived("Hello World"); mbeanServer.invoke( on, "sendStringBody", new Object[] {"direct:start", "Hello World"}, new String[] {"java.lang.String", "java.lang.String"}); assertMockEndpointsSatisfied(); Object reply = mbeanServer.invoke( on, "requestBody", new Object[] {"direct:foo", "Hello World"}, new String[] {"java.lang.String", "java.lang.Object"}); assertEquals("Bye World", reply); reply = mbeanServer.invoke( on, "requestStringBody", new Object[] {"direct:foo", "Hello World"}, new String[] {"java.lang.String", "java.lang.String"}); assertEquals("Bye World", reply); resetMocks(); mock = getMockEndpoint("mock:result"); mock.expectedBodiesReceived("Hello World"); mock.expectedHeaderReceived("foo", 123); Map<String, Object> headers = new HashMap<String, Object>(); headers.put("foo", 123); mbeanServer.invoke( on, "sendBodyAndHeaders", new Object[] {"direct:start", "Hello World", headers}, new String[] {"java.lang.String", "java.lang.Object", "java.util.Map"}); assertMockEndpointsSatisfied(); resetMocks(); mock = getMockEndpoint("mock:result"); mock.expectedBodiesReceived("Hello World"); mock.expectedHeaderReceived("foo", 123); reply = mbeanServer.invoke( on, "requestBodyAndHeaders", new Object[] {"direct:start", "Hello World", headers}, new String[] {"java.lang.String", "java.lang.Object", "java.util.Map"}); assertEquals("Hello World", reply); assertMockEndpointsSatisfied(); // test can send Boolean can = (Boolean) mbeanServer.invoke( on, "canSendToEndpoint", new Object[] {"direct:start"}, new String[] {"java.lang.String"}); assertEquals(true, can.booleanValue()); can = (Boolean) mbeanServer.invoke( on, "canSendToEndpoint", new Object[] {"timer:foo"}, new String[] {"java.lang.String"}); assertEquals(false, can.booleanValue()); // stop Camel mbeanServer.invoke(on, "stop", null, null); }
private void assertMailbox(String name) throws Exception { MockEndpoint mock = getMockEndpoint("mock:" + name); mock.expectedMessageCount(1); mock.expectedBodiesReceived("Hello World"); mock.expectedHeaderReceived("cc", "james@localhost"); }