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.transport.tcp.protocols;
8   
9   import org.mule.api.MuleException;
10  import org.mule.api.MuleMessage;
11  import org.mule.module.client.MuleClient;
12  import org.mule.tck.junit4.FunctionalTestCase;
13  import org.mule.tck.junit4.rule.DynamicPort;
14  
15  import org.junit.Rule;
16  import org.junit.Test;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.assertNotNull;
20  import static org.junit.Assert.assertTrue;
21  import static org.junit.Assert.fail;
22  
23  public class SafeProtocolTestCase extends FunctionalTestCase
24  {
25  
26      protected static String TEST_MESSAGE = "Test TCP Request";
27  
28      @Rule
29      public DynamicPort dynamicPort1 = new DynamicPort("port1");
30  
31      @Rule
32      public DynamicPort dynamicPort2 = new DynamicPort("port2");
33  
34      @Override
35      protected String getConfigResources()
36      {
37          return "safe-protocol-test.xml";
38      }
39  
40      @Test
41      public void testSafeToSafe() throws MuleException
42      {
43          MuleClient client = new MuleClient(muleContext);
44          assertResponseOk(client.send("tcp://localhost:" + dynamicPort1.getNumber() + "?connector=safe", TEST_MESSAGE, null));
45      }
46  
47      @Test
48      public void testUnsafeToSafe() throws MuleException
49      {
50          MuleClient client = new MuleClient(muleContext);
51          assertResponseBad(client.send("tcp://localhost:" + dynamicPort1.getNumber() + "?connector=unsafe", TEST_MESSAGE, null));
52      }
53  
54      private void assertResponseOk(MuleMessage message)
55      {
56          assertNotNull("Null message", message);
57          Object payload = message.getPayload();
58          assertNotNull("Null payload", payload);
59          assertTrue("Payload not byte[]", payload instanceof byte[]);
60          assertEquals(TEST_MESSAGE + " Received", new String((byte[]) payload));
61      }
62  
63      protected void assertResponseBad(MuleMessage message)
64      {
65          try
66          {
67              if (message.getPayloadAsString().equals(TEST_MESSAGE + " Received"))
68              {
69                  fail("expected error");
70              }
71          }
72          catch (Exception e)
73          {
74              // expected
75          }
76      }
77  
78  }