View Javadoc

1   /* 
2    * Copyright (c) 2005-2011, Fraunhofer-Gesellschaft
3    * All rights reserved.
4    * 
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are
7    * met:
8    * 
9    * (1) Redistributions of source code must retain the above copyright
10   *     notice, this list of conditions and the disclaimer at the end.
11   *     Redistributions in binary form must reproduce the above copyright
12   *     notice, this list of conditions and the following disclaimer in
13   *     the documentation and/or other materials provided with the
14   *     distribution.
15   * 
16   * (2) Neither the name of Fraunhofer nor the names of its
17   *     contributors may be used to endorse or promote products derived
18   *     from this software without specific prior written permission.
19   * 
20   * DISCLAIMER
21   * 
22   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25   * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26   * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33   *  
34   */
35  package org.ogf.graap.wsag.it;
36  
37  import java.text.MessageFormat;
38  
39  import javax.security.auth.login.LoginContext;
40  
41  import junit.framework.Assert;
42  
43  import org.apache.log4j.Logger;
44  import org.ogf.graap.wsag.api.client.AgreementClient;
45  import org.ogf.graap.wsag.api.client.AgreementFactoryClient;
46  import org.ogf.graap.wsag.api.client.AgreementFactoryRegistryClient;
47  import org.ogf.graap.wsag.client.AgreementFactoryRegistryLocator;
48  import org.ogf.graap.wsag.security.core.KeystoreProperties;
49  import org.ogf.graap.wsag.security.core.keystore.KeystoreLoginContext;
50  import org.w3.x2005.x08.addressing.EndpointReferenceType;
51  
52  /**
53   * Abstract test case for the WSAG4J framework. This test case uses the WSAG4J client API, but the
54   * instantiation of the clients is left to the concrete implementations. Multiple WSRF stack implementations
55   * can therefore use the same tests.
56   * 
57   * @author owaeld
58   * 
59   */
60  public abstract class AbstractIntegrationTest extends WsagTestCase
61  {
62  
63      private static final Logger LOG = Logger.getLogger( AbstractIntegrationTest.class );
64  
65      /**
66       * default WSAG4J URL
67       */
68      private static String url = "http://127.0.0.1:8080/wsag4j";
69  
70      /**
71       * @return the url
72       */
73      public static String getUrl()
74      {
75          return url;
76      }
77  
78      /**
79       * @param url
80       *            the url to set
81       */
82      public static void setUrl( String url )
83      {
84          AbstractIntegrationTest.url = url;
85      }
86  
87      /**
88       * number of factories configured in the framework
89       */
90      public static final int EXPECTED_FACTORIES = 2;
91  
92      /**
93       * number of templates at factory 1
94       */
95      public static final int EXPECTED_TEMPLATES_FACTORY_1 = 4;
96  
97      /**
98       * number of templates at factory 1
99       */
100     public static final int EXPECTED_TEMPLATES_FACTORY_2 = 2;
101 
102     static
103     {
104         String gatewayURL = System.getProperty( "wsag4j.gateway.address" );
105         if ( gatewayURL != null )
106         {
107             LOG.info( "Gateway address was set via SystemPorperties: " + gatewayURL );
108             url = gatewayURL;
109         }
110         else
111         {
112             LOG.info( "Gateway address was not set via SystemPorperties." );
113         }
114     }
115 
116     /**
117      * @param name
118      *            the test name
119      */
120     protected AbstractIntegrationTest( String name )
121     {
122         super( name );
123     }
124 
125     /**
126      * initializes the agreement factory clients for the test
127      * 
128      * @throws Exception
129      *             indicates an error while initializing thefactory clients
130      */
131     protected void setUp() throws Exception
132     {
133         super.setUp();
134 
135         EndpointReferenceType epr = EndpointReferenceType.Factory.newInstance();
136         epr.addNewAddress().setStringValue( url );
137 
138         AgreementFactoryRegistryClient registry = null;
139         AgreementFactoryClient[] factories = null;
140 
141         try
142         {
143             registry = AgreementFactoryRegistryLocator.getFactoryRegistry( epr, getLoginContext() );
144             factories = registry.listAgreementFactories();
145         }
146         catch ( Exception ex )
147         {
148             Assert.fail( "Failed to initialize factory list. " + ex.getMessage() );
149         }
150         //
151         // before running a test, we must make sure that we have a clean
152         // server environment. No agreement resources should exist on the server.
153         //
154         try
155         {
156             for ( int i = 0; i < factories.length; i++ )
157             {
158                 AgreementClient[] agreements = factories[i].listAgreements();
159                 for ( int j = 0; j < agreements.length; j++ )
160                 {
161                     agreements[j].destroy();
162                 }
163             }
164         }
165         catch ( Exception e )
166         {
167             String message =
168                 MessageFormat.format( "Error while cleaning up existing agreement resources. Error: {0}",
169                                       new Object[] { e.getMessage() } );
170             LOG.error( message );
171             throw e;
172         }
173     }
174 
175     /**
176      * {@inheritDoc}
177      */
178     protected void tearDown() throws Exception
179     {
180         super.tearDown();
181     }
182 
183     /**
184      * 
185      * @return the agreement factory clients used for testing
186      */
187     public static AgreementFactoryClient[] getAgreementFactoryClients()
188     {
189         AgreementFactoryClient[] factories = null;
190         try
191         {
192 
193             LoginContext loginContext = AbstractIntegrationTest.getLoginContext();
194 
195             // START SNIPPET: ListAgreementFactories
196             EndpointReferenceType epr = EndpointReferenceType.Factory.newInstance();
197             epr.addNewAddress().setStringValue( AbstractIntegrationTest.url );
198 
199             AgreementFactoryRegistryClient registry =
200                 AgreementFactoryRegistryLocator.getFactoryRegistry( epr, loginContext );
201             factories = registry.listAgreementFactories();
202             // END SNIPPET: ListAgreementFactories
203         }
204         catch ( Exception ex )
205         {
206             LOG.error( ex );
207         }
208 
209         return factories;
210     }
211 
212     /**
213      * @return the login context
214      * 
215      * @throws Exception
216      *             indicates a failure loading the login context (keystore)
217      */
218     public static LoginContext getLoginContext() throws Exception
219     {
220         /*
221          * create a keystore login context
222          */
223         LOG.info( "Create Keystore-LoginContext" );
224 
225         // START SNIPPET: CreateLoginContext
226         KeystoreProperties properties = new KeystoreProperties();
227         properties.setKeyStoreAlias( "wsag4j-user" );
228         properties.setPrivateKeyPassword( "user@wsag4j" );
229 
230         properties.setKeyStoreType( "JKS" );
231         properties.setKeystoreFilename( "/wsag4j-client-keystore.jks" );
232         properties.setKeystorePassword( "user@wsag4j" );
233 
234         properties.setTruststoreType( "JKS" );
235         properties.setTruststoreFilename( "/wsag4j-client-keystore.jks" );
236         properties.setTruststorePassword( "user@wsag4j" );
237 
238         LoginContext loginContext = new KeystoreLoginContext( properties );
239         loginContext.login();
240         // END SNIPPET: CreateLoginContext
241 
242         return loginContext;
243     }
244 }