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.ssl;
8   
9   import org.mule.api.MuleMessage;
10  import org.mule.module.client.MuleClient;
11  import org.mule.tck.functional.FunctionalTestComponent;
12  import org.mule.tck.junit4.FunctionalTestCase;
13  import org.mule.tck.junit4.rule.DynamicPort;
14  
15  import java.security.cert.Certificate;
16  import java.util.Iterator;
17  
18  import org.junit.Rule;
19  import org.junit.Test;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertNotNull;
23  import static org.junit.Assert.assertTrue;
24  
25  /**
26   * A different version of {@link org.mule.transport.ssl.SslCertificateTestCase} to see if we can get
27   * different timing.
28   */
29  public class SslCertificatesTestCase extends FunctionalTestCase
30  {
31      private static int NUM_MESSAGES = 100;
32  
33      @Rule
34      public DynamicPort dynamicPort = new DynamicPort("port1");
35  
36      @Override
37      protected String getConfigResources()
38      {
39          return "ssl-certificates-test.xml";
40      }
41  
42      @Test
43      public void testOnce() throws Exception
44      {
45          doTests(1);
46      }
47  
48      @Test
49      public void testMany() throws Exception
50      {
51          doTests(NUM_MESSAGES);
52      }
53  
54      protected void doTests(int numberOfMessages) throws Exception
55      {
56          SaveCertificatesCallback callback = setupEventCallback();
57  
58          MuleClient client = new MuleClient(muleContext);
59          for (int i = 0; i < numberOfMessages; ++i)
60          {
61              String msg = TEST_MESSAGE + i;
62              MuleMessage result = client.send("in", msg, null);
63              assertEquals(msg  + " Received", result.getPayloadAsString());
64          }
65  
66          Iterator<Certificate[]> certificates = callback.getCertificates().iterator();
67          for (int i = 0; i < numberOfMessages; ++i)
68          {
69              assertTrue("No cert at " + i, certificates.hasNext());
70              assertNotNull("Null cert at " + i, certificates.next());
71          }
72      }
73  
74      private SaveCertificatesCallback setupEventCallback() throws Exception
75      {
76          FunctionalTestComponent ftc = (FunctionalTestComponent) getComponent("service");
77          assertNotNull(ftc);
78          assertNotNull(ftc.getEventCallback());
79  
80          SaveCertificatesCallback callback = (SaveCertificatesCallback) ftc.getEventCallback();
81          callback.clear();
82          return callback;
83      }
84  }