View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.test.integration.client;
8   
9   import org.mule.DefaultMuleMessage;
10  import org.mule.api.MuleMessage;
11  import org.mule.api.transformer.wire.WireFormat;
12  import org.mule.module.client.remoting.notification.RemoteDispatcherNotification;
13  import org.mule.tck.junit4.AbstractMuleContextTestCase;
14  import org.mule.tck.testmodels.fruit.Apple;
15  import org.mule.transformer.wire.SerializationWireFormat;
16  
17  import java.io.ByteArrayInputStream;
18  import java.io.ByteArrayOutputStream;
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import org.junit.Test;
23  
24  import static org.junit.Assert.assertEquals;
25  import static org.junit.Assert.assertNotNull;
26  import static org.junit.Assert.assertTrue;
27  
28  public class RemoteDispatcherSerializationTestCase extends AbstractMuleContextTestCase
29  {
30      protected RemoteDispatcherNotification getNotification()
31      {
32          Map<String, Object> props = new HashMap<String, Object>();
33          props.put("key1", "value1");
34          
35          Apple apple = new Apple();
36          apple.wash();
37          
38          MuleMessage message = new DefaultMuleMessage(apple, props, muleContext);
39          RemoteDispatcherNotification notification = new RemoteDispatcherNotification(message,
40              RemoteDispatcherNotification.ACTION_SEND, "vm://foo");
41          notification.setProperty("foo", "bar");
42          return notification;
43      }
44      
45      @Test
46      public void testNotificationJavaSerialization() throws Exception
47      {
48          doTestNotificationSerialization(createObject(SerializationWireFormat.class));
49      }
50  
51      // TODO MULE-3118
52  //    @Test
53  //    public void testNotificationXmlSerialization() throws Exception
54  //    {
55  //        XStreamWireFormat wireFormat = new XStreamWireFormat();
56  //        wireFormat.setMuleContext(muleContext);
57  //        wireFormat.setTransferObjectClass(RemoteDispatcherNotification.class);
58  //
59  //        doTestNotificationSerialization(wireFormat);
60  //    }            
61  
62      public void doTestNotificationSerialization(WireFormat wf) throws Exception
63      {
64          ByteArrayOutputStream baos = new ByteArrayOutputStream();
65          wf.write(baos, getNotification(), "UTF-8");
66  
67          Object result = wf.read(new ByteArrayInputStream(baos.toByteArray()));
68  
69          assertNotNull(result);
70          assertTrue(result instanceof RemoteDispatcherNotification);
71          doTestNotification((RemoteDispatcherNotification)result);
72      }
73  
74      protected void doTestNotification(RemoteDispatcherNotification n) throws Exception
75      {
76          assertEquals("bar", n.getProperty("foo"));
77          MuleMessage m = n.getMessage();
78          assertTrue(m.getPayload() instanceof Apple);
79          assertTrue(((Apple)m.getPayload()).isWashed());
80          assertEquals("value1", m.getOutboundProperty("key1"));
81  
82      }
83  }