Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 /*
  * $Id: JsonCustomTransformerTestCase.java 343 2010-05-05 05:43:44Z ross $
  * --------------------------------------------------------------------------------------
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
  *
  * The software in this package is published under the terms of the CPAL v1.0
  * license, a copy of which has been included with this distribution in the
  * LICENSE.txt file.
  */
package org.mule.module.json.transformers;
    public static final String PERSON_JSON = "{\"emailAddresses\":[{\"type\":\"home\",\"address\":\"john.doe@gmail.com\"},{\"type\":\"work\",\"address\":\"jdoe@bigco.com\"}],\"name\":\"John Doe\",\"dob\":\"01/01/1970\"}";
    public static final String EMAIL_JSON = "{\"type\":\"home\",\"address\":\"john.doe@gmail.com\"}";
    public static final String ITEMS_JSON = "[{\"code\":\"1234\",\"description\":\"Vacuum Cleaner\",\"in-stock\":true},{\"code\":\"1234-1\",\"description\":\"Cleaner Bag\",\"in-stock\":false}]";
    @Override
    protected void doSetUp() throws Exception
    {
        .getRegistry().registerObject("trans"new JsonCustomTransformer());
    }
    public void testCustomTransform() throws Exception
    {
        MuleMessage message = new DefaultMuleMessage();
        Person person = (Personmessage.getPayload(DataTypeFactory.create(Person.class));
        assertNotNull(person);
        assertEquals("John Doe"person.getName());
        assertEquals("01/01/1970"person.getDob());
        assertEquals(2, person.getEmailAddresses().size());
        assertEquals("home"person.getEmailAddresses().get(0).getType());
        assertEquals("john.doe@gmail.com"person.getEmailAddresses().get(0).getAddress());
        assertEquals("work"person.getEmailAddresses().get(1).getType());
        assertEquals("jdoe@bigco.com"person.getEmailAddresses().get(1).getAddress());
    }
    public void testCustomTransformWithMuleMessage() throws Exception
    {
        DefaultMuleMessage message = new DefaultMuleMessage(in);
        message.setInboundProperty("foo""fooValue");
        EmailAddress emailAddress = message.getPayload(new SimpleDataType<EmailAddress>(EmailAddress.class));
        assertNotNull(emailAddress);
        assertEquals("home"emailAddress.getType());
        assertEquals("john.doe@gmail.com"emailAddress.getAddress());
    }
    public void testCustomListTransform() throws Exception
    {
        MuleMessage message = new DefaultMuleMessage();
        List<Itemitems = message.getPayload(new CollectionDataType<List<Item>>(List.classItem.class));
        assertNotNull(items);
        assertEquals("1234"items.get(0).getCode());
        assertEquals("Vacuum Cleaner"items.get(0).getDescription());
        assertEquals("1234-1"items.get(1).getCode());
        assertEquals("Cleaner Bag"items.get(1).getDescription());
        //Call this transformer here to test that the cached transformer from the previous invocation does not interfer with
        //Finding the List<Person> transformer
        String people_json = "[" +  + "," +  + "]";
        message = new DefaultMuleMessage(people_json);
        List<Personpeople = message.getPayload(new CollectionDataType<List<Person>>(List.classPerson.class));
        assertNotNull(people);
        assertEquals(2, people.size());
    }
    public void testDifferentListTransformer() throws Exception
    {
        //Test that we can resolve other collections
        String people_json = "[" +  + "," +  + "]";
        MuleMessage message = new DefaultMuleMessage(people_json);
        List<Personpeople = message.getPayload(new ListDataType<List<Person>>(Person.class));
        assertNotNull(people);
        assertEquals(2, people.size());
    }
New to GrepCode? Check out our FAQ X