/** * Test the Camel-Kafka, set topic by headers and sync producer * * @throws Exception */ @Test public void springSupportTest() throws Exception { final String PAYLOAD = "SpringBasicTest"; mock.reset(); // reset the mock template.sendBody("direct:basicTest", PAYLOAD); // setup the actual expectation mock.expectedMessageCount(1); mock.expectedBodiesReceived(PAYLOAD); assertMockEndpointsSatisfied(); mock.reset(); }
public void testThrottlingRoutePolicy() throws Exception { // trigger one in flight from the start template.sendBody("seda:bar", "Hello World"); MockEndpoint result = getMockEndpoint("mock:result"); result.expectedBodiesReceived("A"); // only 1 message will get completed as the throttler will suspend the consumer // when A is done template.sendBody("direct:start", "A"); // need a little slack to ensure the seda consumer will be suspended in between Thread.sleep(2000); template.sendBody("direct:start", "B"); result.assertIsSatisfied(); result.reset(); result.expectedBodiesReceived("B"); // trigger seda:bar to complete now, which should signal // to the throttler to resume the seda:foo consumer, so B can get done latch.countDown(); result.assertIsSatisfied(); }
private void testValidateCDA(String endpoint, String file) throws Exception { mockOutput.reset(); mockError.reset(); InputStream stream = inputStream(file); mockOutput.expectedMessageCount(1); producerTemplate.sendBody(endpoint, stream); mockOutput.assertIsSatisfied(); }
private void testUnmarshalCDA(String endpoint, String file) throws Exception { mockOutput.reset(); mockError.reset(); InputStream stream = inputStream(file); mockOutput.expectedMessageCount(1); producerTemplate.sendBody(endpoint, stream); mockOutput.assertIsSatisfied(); assertTrue(messageAsString(resultAdapter()).contains("<ClinicalDocument")); }
private void testMarshalCDA(String endpoint, String file) throws Exception { mockOutput.reset(); mockError.reset(); ClinicalDocument message = inputMessage(file); mockOutput.expectedMessageCount(1); producerTemplate.sendBody(endpoint, message); mockOutput.assertIsSatisfied(); assertTrue(resultString().contains("<ClinicalDocument")); }
@Test public void testEndpointWithoutHttps() { mockEndpoint.reset(); try { template.sendBodyAndHeader( "http://localhost:" + port + "/test", expectedBody, "Content-Type", "application/xml"); fail("expect exception on access to https endpoint via http"); } catch (RuntimeCamelException expected) { } assertTrue("mock endpoint was not called", mockEndpoint.getExchanges().isEmpty()); }
@Test public void testIdempotent() throws Exception { String uri = "file:target/test-classes/idempotent?idempotent=true&delay=10"; context .getRouteDefinitions() .get(1) .adviceWith( context, new AdviceWithRouteBuilder() { @Override public void configure() throws Exception { replaceFromWith(uri); weaveByType(ToDefinition.class).selectFirst().replace().to("mock:endpoint"); } }); MockEndpoint end = context.getEndpoint("mock:endpoint", MockEndpoint.class); end.expectedMessageCount(1); producer.sendBodyAndHeader( "file://target/test-classes/idempotent", Exchange.FILE_NAME, "FCOO1.nc"); end.assertIsSatisfied(); String fileName = (String) end.getReceivedExchanges().get(0).getIn().getHeader(Exchange.FILE_NAME_ONLY); assertEquals(fileName, "FCOO1.nc"); // reset the mock end.reset(); end.expectedMessageCount(0); // move file back File file = new File("target/test-classes/idempotent/.camel/FCOO1.nc"); File renamed = new File("target/test-classes/idempotent/FCOO1.nc"); file.renameTo(renamed); producer.sendBodyAndHeader( "file://target/test-classes/idempotent", Exchange.FILE_NAME, "FCOO1.nc"); // let some time pass to let the consumer try to consume even though it cannot Thread.sleep(100); end.assertIsSatisfied(); FileEndpoint fe = context.getEndpoint(uri, FileEndpoint.class); assertNotNull(fe); // Make sure that there are no incoming messages MemoryIdempotentRepository repo = (MemoryIdempotentRepository) fe.getInProgressRepository(); assertEquals("Should be no in-progress files", 0, repo.getCacheSize()); }
@Test public void testMaxMessagesPerPoll() throws Exception { // start route context.startRoute("foo"); MockEndpoint mock = getMockEndpoint("mock:result"); mock.expectedBodiesReceived("Bye World", "Godday World"); mock.setResultWaitTime(4000); mock.expectedPropertyReceived(Exchange.BATCH_SIZE, 2); assertMockEndpointsSatisfied(); mock.reset(); mock.expectedBodiesReceived("Hello World"); mock.expectedPropertyReceived(Exchange.BATCH_SIZE, 1); 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); }
public void testHalfOpenCircuitClosesAfterTimeout() throws Exception { expectsMessageCount(2, result); result.whenAnyExchangeReceived( new Processor() { @Override public void process(Exchange exchange) throws Exception { exchange.setException(new MyCustomException()); } }); sendMessage("direct:start", "message one"); sendMessage("direct:start", "message two"); sendMessage("direct:start", "message three"); assertMockEndpointsSatisfied(); result.reset(); expectsMessageCount(1, result); Thread.sleep(1000); sendMessage("direct:start", "message four"); assertMockEndpointsSatisfied(); }
@Test public void testEndpoint() throws Exception { mockEndpoint.reset(); mockEndpoint.expectedBodiesReceived(expectedBody); template.sendBodyAndHeader( "https://localhost:" + port + "/test", expectedBody, "Content-Type", "application/xml"); mockEndpoint.assertIsSatisfied(); List<Exchange> list = mockEndpoint.getReceivedExchanges(); Exchange exchange = list.get(0); TestSupport.assertNotNull("exchange", exchange); Message in = exchange.getIn(); assertNotNull("in", in); Map<String, Object> headers = in.getHeaders(); log.info("Headers: " + headers); assertTrue("Should be more than one header but was: " + headers, headers.size() > 0); }
@After public void tearDown() throws Exception { mock.reset(); helper.tearDown(); super.tearDown(); }
@Test public void execute() throws Exception { startFlow(SampleTemperatureProcessor.FLOW.getId()); startFlow(SampleThermostatControl.FLOW.getId()); startFlow(SampleEnvironmentWidget.FLOW.getId()); LOG.info("##########################################################################"); MockEndpoint mockLivingroomSetpointActuator = context().getEndpoint("mock:livingroomSetpointActuator", MockEndpoint.class); MockEndpoint mockBedroomSetpointActuator = context().getEndpoint("mock:bedroomSetpointActuator", MockEndpoint.class); mockLivingroomSetpointActuator.expectedMessageCount(0); mockBedroomSetpointActuator.expectedMessageCount(0); mockEventReceiver.reset(); mockEventReceiver.expectedBodiesReceivedInAnyOrder( toJson(new Message(SampleEnvironmentWidget.LIVINGROOM_TEMPERATURE_SENSOR_SINK, "75")), toJson(new Message(SampleEnvironmentWidget.LIVINGROOM_THERMOSTAT_TEMPERATURE_SINK, "75")), toJson( new Message( SampleThermostatControl.TEMPERATURE_CONSUMER_SINK, SampleEnvironmentWidget.LIVINGROOM_THERMOSTAT.getId(), "75")), toJson( new Message( SampleThermostatControl.TEMPERATURE_PROCESSOR_FLOW_FAHRENHEIT_SINK, SampleEnvironmentWidget.LIVINGROOM_THERMOSTAT.getId(), "75")), toJson( new Message( SampleTemperatureProcessor.FAHRENHEIT_CONSUMER_SINK, SampleEnvironmentWidget.LIVINGROOM_THERMOSTAT.getId(), "75")), toJson( new Message( SampleTemperatureProcessor.CELCIUS_PRODUCER_SINK, SampleEnvironmentWidget.LIVINGROOM_THERMOSTAT.getId(), "24")), toJson( new Message( SampleTemperatureProcessor.LABEL_PRODUCER_SINK, SampleEnvironmentWidget.LIVINGROOM_THERMOSTAT.getId(), "24 \u00B0C")), toJson( new Message( SampleThermostatControl.TEMPERATURE_LABEL_SINK, SampleEnvironmentWidget.LIVINGROOM_THERMOSTAT.getId(), "24 \u00B0C")), toJson(new Message(SampleEnvironmentWidget.LIVINGROOM_SETPOINT_SENSOR_SINK, "70")), toJson(new Message(SampleEnvironmentWidget.LIVINGROOM_THERMOSTAT_SETPOINT_SINK, "70")), toJson( new Message( SampleThermostatControl.SETPOINT_CONSUMER_SINK, SampleEnvironmentWidget.LIVINGROOM_THERMOSTAT.getId(), "70")), toJson( new Message( SampleThermostatControl.SETPOINT_PROCESSOR_FLOW_FAHRENHEIT_SINK, SampleEnvironmentWidget.LIVINGROOM_THERMOSTAT.getId(), "70")), toJson( new Message( SampleTemperatureProcessor.FAHRENHEIT_CONSUMER_SINK, SampleEnvironmentWidget.LIVINGROOM_THERMOSTAT.getId(), "70")), toJson( new Message( SampleTemperatureProcessor.CELCIUS_PRODUCER_SINK, SampleEnvironmentWidget.LIVINGROOM_THERMOSTAT.getId(), "21")), toJson( new Message( SampleTemperatureProcessor.LABEL_PRODUCER_SINK, SampleEnvironmentWidget.LIVINGROOM_THERMOSTAT.getId(), "21 \u00B0C")), toJson( new Message( SampleThermostatControl.SETPOINT_LABEL_SINK, SampleEnvironmentWidget.LIVINGROOM_THERMOSTAT.getId(), "21 \u00B0C"))); Exchange exchange = new DefaultExchange(context()); exchange .getIn() .setBody(new Message(SampleEnvironmentWidget.LIVINGROOM_TEMPERATURE_SENSOR_SINK, "75")); producerTemplate.send("direct:sendEvent", exchange); exchange = new DefaultExchange(context()); exchange .getIn() .setBody(new Message(SampleEnvironmentWidget.LIVINGROOM_SETPOINT_SENSOR_SINK, "70")); producerTemplate.send("direct:sendEvent", exchange); LOG.info("##########################################################################"); mockLivingroomSetpointActuator.assertIsSatisfied(); mockBedroomSetpointActuator.assertIsSatisfied(); mockEventReceiver.assertIsSatisfied(); LOG.info("##########################################################################"); mockLivingroomSetpointActuator.reset(); mockLivingroomSetpointActuator.expectedMessageCount(1); mockBedroomSetpointActuator.reset(); mockBedroomSetpointActuator.expectedMessageCount(0); mockEventReceiver.reset(); mockEventReceiver.expectedBodiesReceivedInAnyOrder( toJson( new Message( SampleThermostatControl.SETPOINT_PRODUCER_SINK, SampleEnvironmentWidget.LIVINGROOM_THERMOSTAT.getId(), "69")), toJson(new Message(SampleEnvironmentWidget.LIVINGROOM_SETPOINT_ACTUATOR_SINK, "69")), toJson( new Message( SampleThermostatControl.SETPOINT_PRODUCER_SINK, SampleEnvironmentWidget.LIVINGROOM_THERMOSTAT.getId(), "69")), toJson(new Message(SampleEnvironmentWidget.LIVINGROOM_SETPOINT_ACTUATOR_SINK, "69"))); exchange = new DefaultExchange(context()); exchange .getIn() .setBody( new Message( SampleThermostatControl.SETPOINT_MINUS_BUTTON_SOURCE, SampleEnvironmentWidget.LIVINGROOM_THERMOSTAT.getId(), "1")); producerTemplate.send("direct:sendEvent", exchange); exchange = new DefaultExchange(context()); exchange .getIn() .setBody( new Message( SampleThermostatControl.SETPOINT_MINUS_BUTTON_SOURCE, SampleEnvironmentWidget.LIVINGROOM_THERMOSTAT.getId(), "1")); producerTemplate.send("direct:sendEvent", exchange); LOG.info("##########################################################################"); mockLivingroomSetpointActuator.assertIsSatisfied(); mockBedroomSetpointActuator.assertIsSatisfied(); mockEventReceiver.assertIsSatisfied(); }