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.security.KeystoreLoginContext;
45  import org.ogf.graap.wsag.api.security.KeystoreProperties;
46  import org.ogf.graap.wsag.client.api.AgreementClient;
47  import org.ogf.graap.wsag.client.api.AgreementFactoryClient;
48  import org.ogf.graap.wsag.client.api.AgreementFactoryRegistryClient;
49  import org.w3.x2005.x08.addressing.EndpointReferenceType;
50  
51  /**
52   * Abstract test case for the WSAG4J framework. This test case uses the WSAG4J client API, but the
53   * instantiation of the clients is left to the concrete implementations. Multiple WSRF stack implementations
54   * can therefore use the same tests.
55   * 
56   * @author owaeld
57   * 
58   */
59  public abstract class AbstractIntegrationTest extends WsagTestCase
60  {
61  
62      private static final Logger LOG = Logger.getLogger( AbstractIntegrationTest.class );
63  
64      /**
65       * default WSAG4J URL
66       */
67      private static String url = "http://127.0.0.1:8080/wsag4j";
68  
69      /**
70       * @return the url
71       */
72      public static String getUrl()
73      {
74          return url;
75      }
76  
77      /**
78       * @param url
79       *            the url to set
80       */
81      public static void setUrl( String url )
82      {
83          AbstractIntegrationTest.url = url;
84      }
85  
86      /**
87       * number of factories configured in the framework
88       */
89      private static final int EXPECTED_FACTORIES = 1;
90  
91      /**
92       * number of templates at factory 1
93       */
94      public static final int EXPECTED_TEMPLATES_FACTORY_1 = 6;
95  
96      /**
97       * number of templates at factory 1
98       */
99      public static final int EXPECTED_TEMPLATES_FACTORY_2 = 2;
100 
101     static
102     {
103         String gatewayURL = System.getProperty( "wsag4j.gateway.address" );
104         if ( gatewayURL != null )
105         {
106             LOG.info( "Gateway address was set via SystemPorperties: " + gatewayURL );
107             url = gatewayURL;
108         }
109         else
110         {
111             LOG.info( "Gateway address was not set via SystemPorperties." );
112         }
113     }
114 
115     /**
116      * @param name
117      *            the test name
118      */
119     protected AbstractIntegrationTest( String name )
120     {
121         super( name );
122     }
123 
124     /**
125      * initializes the agreement factory clients for the test
126      * 
127      * @throws Exception
128      *             indicates an error while initializing thefactory clients
129      */
130     @Override
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 = AgreementFactoryRegistryClient.FACTORY.newInstance( 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     @Override
179     protected void tearDown() throws Exception
180     {
181         super.tearDown();
182     }
183 
184     /**
185      * 
186      * @return the agreement factory clients used for testing
187      */
188     public static AgreementFactoryClient[] getAgreementFactoryClients()
189     {
190         AgreementFactoryClient[] factories = null;
191         try
192         {
193 
194             LoginContext loginContext = AbstractIntegrationTest.getLoginContext();
195 
196             // START SNIPPET: ListAgreementFactories
197             EndpointReferenceType epr = EndpointReferenceType.Factory.newInstance();
198             epr.addNewAddress().setStringValue( AbstractIntegrationTest.url );
199 
200             AgreementFactoryRegistryClient registry =
201                 AgreementFactoryRegistryClient.FACTORY.newInstance( epr, loginContext );
202             factories = registry.listAgreementFactories();
203             // END SNIPPET: ListAgreementFactories
204         }
205         catch ( Exception ex )
206         {
207             LOG.error( ex );
208         }
209 
210         return factories;
211     }
212 
213     /**
214      * Returns the agreement factory test client with the given id.
215      * 
216      * @param id
217      *            the factory id
218      * 
219      * @return the agreement factory client with the given id
220      */
221     public static AgreementFactoryClient getAgreementFactoryClient( String id )
222     {
223         AgreementFactoryClient factory = null;
224         try
225         {
226 
227             LoginContext loginContext = AbstractIntegrationTest.getLoginContext();
228 
229             // START SNIPPET: GetAgreementFactory
230             EndpointReferenceType epr = EndpointReferenceType.Factory.newInstance();
231             epr.addNewAddress().setStringValue( AbstractIntegrationTest.url );
232 
233             AgreementFactoryRegistryClient registry =
234                 AgreementFactoryRegistryClient.FACTORY.newInstance( epr, loginContext );
235             factory = registry.getFactory( id );
236             // END SNIPPET: GetAgreementFactory
237 
238             assertNotNull( "requested factory does not exist", factory );
239         }
240         catch ( Exception ex )
241         {
242             LOG.error( ex );
243         }
244 
245         return factory;
246     }
247 
248     /**
249      * @return the login context
250      * 
251      * @throws Exception
252      *             indicates a failure loading the login context (keystore)
253      */
254     public static LoginContext getLoginContext() throws Exception
255     {
256         /*
257          * create a keystore login context
258          */
259         LOG.info( "Create Keystore-LoginContext" );
260 
261         // START SNIPPET: CreateLoginContext
262         KeystoreProperties properties = new KeystoreProperties();
263         properties.setKeyStoreAlias( "wsag4j-user" );
264         properties.setPrivateKeyPassword( "user@wsag4j" );
265 
266         properties.setKeyStoreType( "JKS" );
267         properties.setKeystoreFilename( "/wsag4j-client-keystore.jks" );
268         properties.setKeystorePassword( "user@wsag4j" );
269 
270         properties.setTruststoreType( "JKS" );
271         properties.setTruststoreFilename( "/wsag4j-client-keystore.jks" );
272         properties.setTruststorePassword( "user@wsag4j" );
273 
274         LoginContext loginContext = new KeystoreLoginContext( properties );
275         loginContext.login();
276         // END SNIPPET: CreateLoginContext
277 
278         return loginContext;
279     }
280 
281     /**
282      * Returns the default number of expected agreement factories at an endpoint.
283      * 
284      * @return the number of expected agreement factories
285      */
286     public int getDefaultFactoryCount()
287     {
288         return EXPECTED_FACTORIES;
289     }
290 }