Package org.apache.servicemix.client

Source Code of org.apache.servicemix.client.ServiceMixClientTest

/*
* 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.servicemix.client;

import java.io.StringReader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import javax.jbi.JBIException;
import javax.jbi.messaging.InOnly;
import javax.jbi.messaging.InOut;
import javax.jbi.messaging.NormalizedMessage;
import javax.xml.namespace.QName;
import javax.xml.transform.stream.StreamSource;

import junit.framework.TestCase;

import org.apache.servicemix.jbi.api.EndpointResolver;
import org.apache.servicemix.jbi.container.SpringJBIContainer;
import org.apache.servicemix.jbi.jaxp.SourceTransformer;
import org.apache.servicemix.tck.Receiver;
import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.AbstractXmlApplicationContext;

/**
* @version $Revision: 1148530 $
*/
public class ServiceMixClientTest extends TestCase {
   
    private static final transient Logger LOGGER = LoggerFactory.getLogger(ServiceMixClientTest.class);

    protected AbstractXmlApplicationContext context;
    protected ServiceMixClient client;
    protected Receiver receiver;

    protected SourceTransformer transformer = new SourceTransformer();


    // Send methods
    //-------------------------------------------------------------------------
    public void testSendUsingJbiAPIs() throws Exception {

        InOnly exchange = client.createInOnlyExchange();

        NormalizedMessage message = exchange.getInMessage();
        message.setProperty("name", "James");
        message.setContent(new StreamSource(new StringReader("<hello>world</hello>")));

        QName service = new QName("http://servicemix.org/cheese", "receiver");
        exchange.setService(service);
        client.send(exchange);

        receiver.getMessageList().assertMessagesReceived(1);
    }

    public void testSendUsingMapAndPOJOsByServiceName() throws Exception {

        Map properties = new HashMap();
        properties.put("name", "James");

        QName service = new QName("http://servicemix.org/cheese", "receiver");
        EndpointResolver resolver = client.createResolverForService(service);
        client.send(resolver, null, properties, "<hello>world</hello>");

        receiver.getMessageList().assertMessagesReceived(1);
    }

    public void testSendUsingMapAndPOJOsUsingContainerRouting() throws Exception {

        ServiceMixClient clientNoRouting = (ServiceMixClient) context.getBean("clientWithRouting");

        Map properties = new HashMap();
        properties.put("name", "James");

        clientNoRouting.send(null, null, properties, "<hello>world</hello>");

        receiver.getMessageList().assertMessagesReceived(1);
    }

    public void testSendUsingMapAndPOJOsUsingContainerRoutingWithNoConfiguration() throws Exception {
        try {
            Map properties = new HashMap();
            properties.put("name", "James");

            client.send(null, null, properties, "<hello>world</hello>");
            fail("Should have thrown an exception as we have not wired in any container routing information to this client");
        } catch (JBIException e) {
            LOGGER.info("Caught expected exception as we have specified no endpoint resolver", e);
            assertNotNull(e);
        }
    }

    // Request methods
    //-------------------------------------------------------------------------
    public void testRequestUsingJbiAPIsByServiceName() throws Exception {
        QName service = new QName("http://servicemix.org/cheese", "myService");
        assertRequestUsingJBIAPIs(service);
    }

    public void testRequestUsingMapAndPOJOsByServiceName() throws Exception {
        QName service = new QName("http://servicemix.org/cheese", "myService");
        assertRequestUsingMapAndPOJOByServiceName(service);
    }

    public void testRequestUsingPOJOWithXStreamMarshaling() throws Exception {
        QName service = new QName("http://servicemix.org/cheese", "myService");

        ServiceMixClient clientWithXStream = (ServiceMixClient) context.getBean("clientWithXStream");

        Map properties = new HashMap();
        properties.put("name", "James");

        EndpointResolver resolver = clientWithXStream.createResolverForService(service);
        TestBean bean = new TestBean();
        bean.setName("James");
        bean.setLength(12);
        bean.getAddresses().addAll(Arrays.asList(new String[] {"London", "LA"}));

        Object response = clientWithXStream.request(resolver, null, properties, bean);

        assertNotNull("Should have returned a non-null response!", response);

        LOGGER.info("Received result: {}", response);
    }

    // Implementation methods
    //-------------------------------------------------------------------------
    protected void assertRequestUsingJBIAPIs(QName service) throws Exception {
        InOut exchange = client.createInOutExchange();

        NormalizedMessage inMessage = exchange.getInMessage();
        inMessage.setProperty("name", "James");
        inMessage.setContent(new StreamSource(new StringReader("<hello>world</hello>")));

        exchange.setService(service);
        boolean answer = client.sendSync(exchange);
        assertTrue("Should have successed", answer);

        NormalizedMessage outMessage = exchange.getOutMessage();
        assertNotNull("outMessage is null!", outMessage);

        assertEquals("foo header", "hello", outMessage.getProperty("foo"));
        LOGGER.info("Received result: {}", outMessage.getContent());
        LOGGER.info("XML is: {}", transformer.toString(outMessage.getContent()));
    }

    protected void assertRequestUsingMapAndPOJOByServiceName(QName service) throws Exception {
        Map properties = new HashMap();
        properties.put("name", "James");

        EndpointResolver resolver = client.createResolverForService(service);
        Object response = client.request(resolver, null, properties, "<hello>world</hello>");

        assertNotNull("Should have returned a non-null response!", response);
       
        LOGGER.info("Received result: {}", response);
    }

    protected void setUp() throws Exception {
        context = createBeanFactory();
        //context.setXmlValidating(false);

        client = getClient();

        // TODO
        //receiver = (Receiver) getBean("receiver");

        SpringJBIContainer jbi = (SpringJBIContainer) getBean("jbi");
        receiver = (Receiver) jbi.getBean("receiver");
        assertNotNull("receiver not found in JBI container", receiver);
    }
   
    protected ServiceMixClient getClient() throws Exception {
        return (ServiceMixClient) getBean("client");
    }

    protected void tearDown() throws Exception {
        super.tearDown();

        if (context != null) {
            context.close();
        }
    }

    protected Object getBean(String name) {
        Object answer = context.getBean(name);
        assertNotNull("Could not find object in Spring for key: " + name, answer);
        return answer;
    }

    protected AbstractXmlApplicationContext createBeanFactory() {
        return new ClassPathXmlApplicationContext("org/apache/servicemix/client/example.xml");
    }

}
TOP

Related Classes of org.apache.servicemix.client.ServiceMixClientTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.