View Javadoc

1   /*
2    * $Id: SslCertificatesTestCase.java 22466 2011-07-20 08:49:26Z justin.calleja $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.transport.ssl;
12  
13  import static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertNotNull;
15  import static org.junit.Assert.assertTrue;
16  
17  import java.security.cert.Certificate;
18  import java.util.Arrays;
19  import java.util.Collection;
20  import java.util.Iterator;
21  
22  import org.junit.Rule;
23  import org.junit.Test;
24  import org.junit.runners.Parameterized.Parameters;
25  import org.mule.api.MuleMessage;
26  import org.mule.module.client.MuleClient;
27  import org.mule.tck.AbstractServiceAndFlowTestCase;
28  import org.mule.tck.functional.FunctionalTestComponent;
29  import org.mule.tck.junit4.rule.DynamicPort;
30  
31  /**
32   * A different version of {@link org.mule.transport.ssl.SslCertificateTestCase} to see if we can get
33   * different timing.
34   */
35  public class SslCertificatesTestCase extends AbstractServiceAndFlowTestCase
36  {
37      private static int NUM_MESSAGES = 100;
38  
39      @Rule
40      public DynamicPort dynamicPort = new DynamicPort("port1");
41  
42      public SslCertificatesTestCase(ConfigVariant variant, String configResources)
43      {
44          super(variant, configResources);
45      }
46          
47      @Parameters
48      public static Collection<Object[]> parameters()
49      {
50          return Arrays.asList(new Object[][]{{ConfigVariant.SERVICE, "ssl-certificates-test-service.xml"},
51              {ConfigVariant.FLOW, "ssl-certificates-test-flow.xml"}});
52      }
53  
54      @Test
55      public void testOnce() throws Exception
56      {
57          doTests(1);
58      }
59  
60      @Test
61      public void testMany() throws Exception
62      {
63          doTests(NUM_MESSAGES);
64      }
65  
66      protected void doTests(int numberOfMessages) throws Exception
67      {
68          SaveCertificatesCallback callback = setupEventCallback();
69  
70          MuleClient client = new MuleClient(muleContext);
71          for (int i = 0; i < numberOfMessages; ++i)
72          {
73              String msg = TEST_MESSAGE + i;
74              MuleMessage result = client.send("in", msg, null);
75              assertEquals(msg  + " Received", result.getPayloadAsString());
76          }
77  
78          Iterator<Certificate[]> certificates = callback.getCertificates().iterator();
79          for (int i = 0; i < numberOfMessages; ++i)
80          {
81              assertTrue("No cert at " + i, certificates.hasNext());
82              assertNotNull("Null cert at " + i, certificates.next());
83          }
84      }
85  
86      private SaveCertificatesCallback setupEventCallback() throws Exception
87      {
88          FunctionalTestComponent ftc = (FunctionalTestComponent) getComponent("service");
89          assertNotNull(ftc);
90          assertNotNull(ftc.getEventCallback());
91  
92          SaveCertificatesCallback callback = (SaveCertificatesCallback) ftc.getEventCallback();
93          callback.clear();
94          return callback;
95      }
96  }