Java Code Examples for org.apache.camel.component.mock.MockEndpoint#setExpectedCount()

The following examples show how to use org.apache.camel.component.mock.MockEndpoint#setExpectedCount() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AhcIntegrationTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void testAsyncHttpRoute() throws Exception {

    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start")
            .to("ahc:" + HTTP_URL)
            .to("mock:results");
        }
    });

    MockEndpoint mockep = camelctx.getEndpoint("mock:results", MockEndpoint.class);
    mockep.setExpectedCount(1);

    camelctx.start();
    try {
        ProducerTemplate producer = camelctx.createProducerTemplate();
        producer.sendBody("direct:start", null);

        mockep.assertIsSatisfied();
        Message message = mockep.getExchanges().get(0).getIn();
        Assert.assertEquals(200, (int) message.getHeader(Exchange.HTTP_RESPONSE_CODE, Integer.class));
        final String body = message.getBody(String.class);
        Assert.assertTrue("Got body " + body, body.contains("Welcome to WildFly"));

    } finally {
        camelctx.close();
    }
}
 
Example 2
Source File: EventBehaviourTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Tests that the content update policy is triggered correctly for the first
 * event.
 */
@Test
public void onContentUpdatePolicyFirstEventNF() throws Exception
{
    this.policyFired = false;
    String endpointUri = getMockEndpointUri();

    MockEndpoint mockEndpoint = camelContext.getEndpoint(endpointUri, MockEndpoint.class);
    mockEndpoint.setAssertPeriod(500);

    retryingTransactionHelper.doInTransaction(() -> {
        BehaviourDefinition<ClassBehaviourBinding> classBehaviour = null;

        try
        {
            setupTestData();

            EventBehaviour eventBehaviour = new EventBehaviour(eventProducer, endpointUri, this, "createOnContentUpdateEvent", Behaviour.NotificationFrequency.FIRST_EVENT);

            // Register interest in the content update event for a versionable node
            classBehaviour = this.policyComponent.bindClassBehaviour(ContentServicePolicies.OnContentUpdatePolicy.QNAME, ContentModel.ASPECT_VERSIONABLE, eventBehaviour);

            // First check that the policy is not fired when the versionable aspect is not
            // present
            ContentWriter contentWriter = this.contentService.getWriter(contentNodeRef, ContentModel.PROP_CONTENT, true);
            contentWriter.putContent("content update one");
            assertFalse(this.policyFired);

            this.newContent = false;

            // Now check that the policy is fired when the versionable aspect is present
            this.nodeService.addAspect(this.contentNodeRef, ContentModel.ASPECT_VERSIONABLE, null);
            ContentWriter contentWriter2 = this.contentService.getWriter(contentNodeRef, ContentModel.PROP_CONTENT, true);
            contentWriter2.putContent("content update two");
            assertTrue(this.policyFired);
            this.policyFired = false;

            // Check that the policy is not fired when using a non updating content writer
            ContentWriter contentWriter3 = this.contentService.getWriter(contentNodeRef, ContentModel.PROP_CONTENT, false);
            contentWriter3.putContent("content update three");
            assertFalse(this.policyFired);

            // Now check that the policy isn't fired when the versionable aspect is present
            // (because it's triggered only for the first event regardless how many times it
            // is updated in that transaction)
            this.nodeService.addAspect(this.contentNodeRef, ContentModel.ASPECT_VERSIONABLE, null);
            ContentWriter contentWriter4 = this.contentService.getWriter(contentNodeRef, ContentModel.PROP_CONTENT, true);
            contentWriter4.putContent("content update four");
            assertFalse(this.policyFired);

            // Assert that the endpoint didn't receive any message
            // Event is sent only on transaction commit.
            mockEndpoint.setExpectedCount(0);
            mockEndpoint.assertIsSatisfied();
        }
        finally
        {
            if (classBehaviour != null)
            {
                this.policyComponent.removeClassDefinition(classBehaviour);
            }
        }

        return null;
    });

    // Assert that the endpoint received 1 messages
    mockEndpoint.setExpectedCount(1);
    mockEndpoint.assertIsSatisfied();
}
 
Example 3
Source File: EventBehaviourTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Tests that the content update policy is triggered correctly for every event.
 */
@Test
public void onContentUpdatePolicyEveryEventNF() throws Exception
{
    this.policyFired = false;
    String endpointUri = getMockEndpointUri();

    MockEndpoint mockEndpoint = camelContext.getEndpoint(endpointUri, MockEndpoint.class);
    mockEndpoint.setAssertPeriod(500);

    retryingTransactionHelper.doInTransaction(() -> {
        BehaviourDefinition<ClassBehaviourBinding> classBehaviour = null;

        try
        {
            setupTestData();

            EventBehaviour eventBehaviour = new EventBehaviour(eventProducer, endpointUri, this, "createOnContentUpdateEvent", Behaviour.NotificationFrequency.EVERY_EVENT);

            // Register interest in the content update event for a versionable node
            classBehaviour = this.policyComponent.bindClassBehaviour(ContentServicePolicies.OnContentUpdatePolicy.QNAME, ContentModel.ASPECT_VERSIONABLE, eventBehaviour);

            // First check that the policy is not fired when the versionable aspect is not
            // present
            ContentWriter contentWriter = this.contentService.getWriter(contentNodeRef, ContentModel.PROP_CONTENT, true);
            contentWriter.putContent("content update one");
            assertFalse(this.policyFired);

            this.newContent = false;

            // Now check that the policy is fired when the versionable aspect is present
            this.nodeService.addAspect(this.contentNodeRef, ContentModel.ASPECT_VERSIONABLE, null);
            ContentWriter contentWriter2 = this.contentService.getWriter(contentNodeRef, ContentModel.PROP_CONTENT, true);
            contentWriter2.putContent("content update two");
            assertTrue(this.policyFired);
            this.policyFired = false;

            // Check that the policy is not fired when using a non updating content writer
            ContentWriter contentWriter3 = this.contentService.getWriter(contentNodeRef, ContentModel.PROP_CONTENT, false);
            contentWriter3.putContent("content update three");
            assertFalse(this.policyFired);

            // Now check that the policy is fired when the versionable aspect is present
            this.nodeService.addAspect(this.contentNodeRef, ContentModel.ASPECT_VERSIONABLE, null);
            ContentWriter contentWriter4 = this.contentService.getWriter(contentNodeRef, ContentModel.PROP_CONTENT, true);
            contentWriter4.putContent("content update four");
            assertTrue(this.policyFired);
            this.policyFired = false;

            try
            {
                eventBehaviour.disable();

                ContentWriter contentWriter5 = this.contentService.getWriter(contentNodeRef, ContentModel.PROP_CONTENT, true);
                contentWriter5.putContent("content update five");
                assertFalse(this.policyFired);
            }
            finally
            {
                eventBehaviour.enable();
            }

            // Assert that the endpoint didn't receive any message
            // Event is sent only on transaction commit.
            mockEndpoint.setExpectedCount(0);
            mockEndpoint.assertIsSatisfied();
        }
        finally
        {
            if (classBehaviour != null)
            {
                this.policyComponent.removeClassDefinition(classBehaviour);
            }
        }

        return null;
    });

    // Assert that the endpoint received 2 messages.
    mockEndpoint.setExpectedCount(2);
    mockEndpoint.assertIsSatisfied();
}
 
Example 4
Source File: EventBehaviourTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Tests that the content update policy is triggered correctly for transaction
 * commit (the default notification frequency).
 */
@Test
public void onContentUpdatePolicyTxnCommitNF() throws Exception
{
    this.policyFired = false;
    String endpointUri = getMockEndpointUri();

    MockEndpoint mockEndpoint = camelContext.getEndpoint(endpointUri, MockEndpoint.class);
    mockEndpoint.setAssertPeriod(500);

    retryingTransactionHelper.doInTransaction(() -> {
        BehaviourDefinition<ClassBehaviourBinding> classBehaviour = null;

        try
        {
            setupTestData();

            EventBehaviour eventBehaviour = new EventBehaviour(eventProducer, endpointUri, this, "createOnContentUpdateEvent",
                    Behaviour.NotificationFrequency.TRANSACTION_COMMIT);

            // Register interest in the content update event for a versionable node
            classBehaviour = this.policyComponent.bindClassBehaviour(ContentServicePolicies.OnContentUpdatePolicy.QNAME, ContentModel.ASPECT_VERSIONABLE, eventBehaviour);

            // First check that the policy is not fired when the versionable aspect is not
            // present
            ContentWriter contentWriter = this.contentService.getWriter(contentNodeRef, ContentModel.PROP_CONTENT, true);
            contentWriter.putContent("content update one");
            assertFalse(this.policyFired);

            this.newContent = false;

            // The policy is fired when the versionable aspect is present (on transaction
            // commit)
            this.nodeService.addAspect(this.contentNodeRef, ContentModel.ASPECT_VERSIONABLE, null);
            ContentWriter contentWriter2 = this.contentService.getWriter(contentNodeRef, ContentModel.PROP_CONTENT, true);
            contentWriter2.putContent("content update two");
            assertFalse(this.policyFired);

            // Check that the policy is not fired when using a non updating content writer
            ContentWriter contentWriter3 = this.contentService.getWriter(contentNodeRef, ContentModel.PROP_CONTENT, false);
            contentWriter3.putContent("content update three");
            assertFalse(this.policyFired);

            // The policy is fired when the versionable aspect is present (on transaction
            // commit)
            this.nodeService.addAspect(this.contentNodeRef, ContentModel.ASPECT_VERSIONABLE, null);
            ContentWriter contentWriter4 = this.contentService.getWriter(contentNodeRef, ContentModel.PROP_CONTENT, true);
            contentWriter4.putContent("content update four");
            assertFalse(this.policyFired);

            // Assert that the endpoint didn't receive any message
            // Event is sent only on transaction commit.
            mockEndpoint.setExpectedCount(0);
            mockEndpoint.assertIsSatisfied();
        }
        finally
        {
            if (classBehaviour != null)
            {
                this.policyComponent.removeClassDefinition(classBehaviour);
            }
        }

        return null;
    });

    // Assert that the endpoint received 1 messages
    // Event is created once per transaction regardless how many times it is updated
    mockEndpoint.setExpectedCount(1);
    mockEndpoint.assertIsSatisfied();
}
 
Example 5
Source File: TransactionAwareEventProducerTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Test
public void send() throws Exception
{
    String endpointUri = getMockEndpointUri();

    MockEndpoint mockEndpoint = camelContext.getEndpoint(endpointUri, MockEndpoint.class);
    mockEndpoint.setAssertPeriod(500);

    String stringMessage = "stringMessage";
    OnContentUpdatePolicyEvent objectMessage = new OnContentUpdatePolicyEvent();
    objectMessage.setId(GUID.generate());
    objectMessage.setType(EventType.CONTENT_UPDATED.toString());
    objectMessage.setTimestamp(System.currentTimeMillis());

    retryingTransactionHelper.doInTransaction(() -> {
        eventProducer.send(endpointUri, stringMessage);

        // Assert that the endpoint didn't receive any message
        // Event is sent only on transaction commit.
        mockEndpoint.setExpectedCount(0);
        mockEndpoint.assertIsSatisfied();

        eventProducer.send(endpointUri, objectMessage);

        // Assert that the endpoint didn't receive any message
        // Event is sent only on transaction commit.
        mockEndpoint.setExpectedCount(0);
        mockEndpoint.assertIsSatisfied();

        return null;
    });

    // Assert that the endpoint received 2 messages
    mockEndpoint.setExpectedCount(2);
    mockEndpoint.assertIsSatisfied();

    // Get the sent string message
    String stringMessageSent = (String) mockEndpoint.getExchanges().get(0).getIn().getBody();

    assertNotNull(stringMessageSent);
    assertEquals(stringMessage, stringMessageSent);

    // Get the sent json marshaled object message
    String jsonMessageSent = (String) mockEndpoint.getExchanges().get(1).getIn().getBody();
    assertNotNull(jsonMessageSent);

    OnContentUpdatePolicyEvent objectMessageSent = messagingObjectMapper.readValue(jsonMessageSent, OnContentUpdatePolicyEvent.class);

    assertNotNull(objectMessageSent);
    assertEquals(objectMessage.getId(), objectMessageSent.getId());
    assertEquals(objectMessage.getType(), objectMessageSent.getType());
    assertEquals(objectMessage.getTimestamp(), objectMessageSent.getTimestamp());
}
 
Example 6
Source File: MustacheTemplateStepHandlerTest.java    From syndesis with Apache License 2.0 4 votes vote down vote up
@Test
public void testTemplateStepIterate() throws Exception {
    List<String> testMessages = new ArrayList<>();
    testMessages.add(
        data(
            dataPair("name", "Bob"),
            dataPair("course",
                dataPair("name", "Mathematics"),
                    dataPair("name", "English"),
                    dataPair("name", "Chemistry")
           ),
           dataPair("text", "Will be going to the University of Southampton.")
      )
    );

    testMessages.add(
        data(
            dataPair("name", "Susan"),
            dataPair("course",
                dataPair("name", "Physics"),
                dataPair("name", "German"),
                dataPair("name", "Art")
            ),
            dataPair("text", "Will be joining Evans, Richards and Dean.")
        )
    );

    Symbol[] symbols = {
        new Symbol("name", "string"),
        new Symbol("course", "array", "{{#", getSymbolSyntax().close()),
        new Symbol("body.text", "string")
    };

    String mustacheTemplate = EMPTY_STRING +
            symbols[0] + " passed the following courses:" + NEW_LINE +
            symbols[1] + NEW_LINE +
            "\t* {{name}}" + NEW_LINE +
            "{{/" + symbols[1].id + getSymbolSyntax().close() +
            symbols[2];

    try {
        IntegrationWithRouteBuilder irb = generateRoute(mustacheTemplate, Arrays.asList(symbols));
        final RouteBuilder routes = irb.routeBuilder();

        // Set up the camel context
        context.addRoutes(routes);
        dumpRoutes(context);

        context.start();

        // Dump routes as XML for troubleshooting
        dumpRoutes(context);

        MockEndpoint result = context.getEndpoint("mock:result", MockEndpoint.class);
        result.setExpectedCount(2);

        List<String> expectedMessages = new ArrayList<>();
        expectedMessages.add(toJson(
                             "Bob passed the following courses:" + NEW_LINE +
                             "\t* Mathematics" + NEW_LINE +
                             "\t* English" + NEW_LINE +
                             "\t* Chemistry" + NEW_LINE +
                             "Will be going to the University of Southampton."));
        expectedMessages.add(toJson(
                             "Susan passed the following courses:" + NEW_LINE +
                             "\t* Physics" + NEW_LINE +
                             "\t* German" + NEW_LINE +
                             "\t* Art" + NEW_LINE +
                             "Will be joining Evans, Richards and Dean."));
        result.expectedBodiesReceived(expectedMessages);

        sendData(context, testMessages);

        result.assertIsSatisfied();

    } finally {
        context.stop();
    }
}