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 org.apache.log4j.Logger;
40  import org.ggf.schemas.jsdl.x2005.x11.jsdl.JobDescriptionDocument;
41  import org.ggf.schemas.jsdl.x2005.x11.jsdl.JobDescriptionType;
42  import org.ogf.graap.wsag.api.AgreementOffer;
43  import org.ogf.graap.wsag.api.client.AgreementClient;
44  import org.ogf.graap.wsag.api.client.AgreementFactoryClient;
45  import org.ogf.graap.wsag.api.client.AgreementFactoryRegistryClient;
46  import org.ogf.graap.wsag.api.exceptions.AgreementFactoryException;
47  import org.ogf.graap.wsag.api.exceptions.ResourceUnavailableException;
48  import org.ogf.graap.wsag.api.exceptions.ResourceUnknownException;
49  import org.ogf.graap.wsag.api.security.SecurityProperties;
50  import org.ogf.graap.wsag.api.types.AgreementOfferType;
51  import org.ogf.graap.wsag.client.AgreementFactoryRegistryLocator;
52  import org.ogf.graap.wsag.client.remote.RemoteAgreementFactoryClientImpl;
53  import org.ogf.schemas.graap.wsAgreement.AgreementTemplateType;
54  import org.ogf.schemas.graap.wsAgreement.GuaranteeTermStateDefinition;
55  import org.ogf.schemas.graap.wsAgreement.GuaranteeTermStateType;
56  import org.ogf.schemas.graap.wsAgreement.ServiceDescriptionTermType;
57  import org.w3.x2005.x08.addressing.EndpointReferenceDocument;
58  import org.w3.x2005.x08.addressing.EndpointReferenceType;
59  
60  /**
61   * AbstractAgreementFactoryTest
62   * 
63   * @author Oliver Waeldrich
64   * 
65   */
66  public abstract class AbstractAgreementFactoryTest extends AbstractIntegrationTest
67  {
68  
69      private static final Logger LOG = Logger.getLogger( AbstractAgreementFactoryTest.class );
70  
71      /**
72       * @param name
73       *            the test name
74       */
75      public AbstractAgreementFactoryTest( String name )
76      {
77          super( name );
78      }
79  
80      /**
81       * Tests creation and destruction of agreement WSRF resources.
82       */
83      public void testCreateDestroyAgreement()
84      {
85          try
86          {
87              LOG.info( "Entering TestCase: testCreateDestroyAgreement" );
88  
89              AgreementFactoryClient[] factories = getAgreementFactoryClients();
90              assertEquals( EXPECTED_FACTORIES, factories.length );
91  
92              AgreementFactoryClient factory = factories[0];
93  
94              AgreementClient[] agreements;
95  
96              /*
97               * log.info("Check the number of existing agreements. Excpected <0>"); AgreementClient[]
98               * agreements = factory.listAgreements();
99               * 
100              * for (int i = 0; i < agreements.length; i++) { log.info(agreements[i].getEndpoint().xmlText(new
101              * XmlOptions().setSavePrettyPrint())); }
102              * 
103              * if (agreements.length > 0) { System.exit(0); } assertEquals(0, agreements.length);
104              */
105 
106             LOG.info( "Creating new Agreements" );
107             AgreementTemplateType[] templates = factory.getTemplates();
108             AgreementOffer offer = new AgreementOfferType( templates[0] );
109 
110             LOG.info( "Create agreement 1" );
111             AgreementClient agreement1 = factory.createAgreement( offer );
112 
113             assertNotNull( "the created agreement must not be <null>", agreement1 );
114 
115             agreements = factory.listAgreements();
116             assertEquals( 1, agreements.length );
117 
118             LOG.info( "Create agreement 2" );
119             AgreementClient agreement2 = factory.createAgreement( offer );
120             assertNotNull( "the created agreement must not be <null>", agreement2 );
121 
122             LOG.info( "List agreements" );
123             agreements = factory.listAgreements();
124             assertEquals( 2, agreements.length );
125 
126             LOG.info( "Destroy agreement 1" );
127             agreement1.destroy();
128             agreements = factory.listAgreements();
129             assertEquals( 1, agreements.length );
130 
131             LOG.info( "Destroy agreement 2" );
132             agreement2.destroy();
133             agreements = factory.listAgreements();
134             assertEquals( 0, agreements.length );
135         }
136         catch ( AgreementFactoryException e )
137         {
138             fail( "AgreementFactoryException: " + e.getMessage() );
139         }
140         catch ( ResourceUnavailableException e )
141         {
142             fail( "ResourceUnavailableException: " + e.getMessage() );
143         }
144         catch ( ResourceUnknownException e )
145         {
146             fail( "ResourceUnknownException: " + e.getMessage() );
147         }
148         catch ( Exception e )
149         {
150             fail( "Could not create agreement factory instance. Error: " + e.getMessage() );
151         }
152     }
153 
154     /**
155      * Tests retrieving templates from an agreement factory.
156      * 
157      * @throws Exception
158      *             indicates a test failure
159      */
160     public void testGetTemplate() throws Exception
161     {
162         LOG.info( "Entering TestCase: testGetTemplate" );
163 
164         // START SNIPPET: GetTemplate
165         AgreementFactoryClient[] factories = getAgreementFactoryClients();
166         assertEquals( EXPECTED_FACTORIES, factories.length );
167 
168         //
169         // We have 2 factories installed. One returns 1 template, the other 2.
170         //
171         AgreementFactoryClient factory = null;
172         for ( int i = 0; i < factories.length; i++ )
173         {
174             AgreementTemplateType[] templates = factories[i].getTemplates();
175 
176             assertNotNull( templates );
177 
178             if ( templates.length == EXPECTED_TEMPLATES_FACTORY_1 )
179             {
180                 factory = factories[i];
181             }
182         }
183 
184         assertNotNull( factory );
185 
186         // factory.setTrace(true);
187 
188         AgreementTemplateType[] templates = factory.getTemplates();
189         // END SNIPPET: GetTemplate
190 
191         assertNotNull( "check that the call returned a template array", templates );
192 
193         assertEquals( "check that the call returned exactly " + EXPECTED_TEMPLATES_FACTORY_1 + " templates",
194                       EXPECTED_TEMPLATES_FACTORY_1, templates.length );
195 
196         //
197         // Here, we assume that the templates are returned in the same order
198         // as they were added to the server. WSAG4J supports this behavior.
199         //
200         assertEquals( "check that the name of the 1. template equals \"SAMPLE1\"", "SAMPLE1",
201                       templates[0].getName() );
202         assertEquals( "check that the id of the 1. template equals \"1\"", "1", templates[0].getTemplateId() );
203         assertEquals( "check that the name of the 2. template equals \"SAMPLE2\"", "SAMPLE2",
204                       templates[1].getName() );
205         assertEquals( "check that the agreement id of the 2. template equals \"2\"", "2",
206                       templates[1].getTemplateId() );
207         assertEquals( "check that the name of the 1. template equals \"sample_service_description_term\"",
208                       "SAMPLE1_SDT",
209                       templates[0].getTerms().getAll().getServiceDescriptionTermArray( 0 ).getName() );
210         assertEquals( "check that the service name of the 1. template equals \"sample_service\"",
211                       "SAMPLE1_SERVICE",
212                       templates[0].getTerms().getAll().getServiceDescriptionTermArray( 0 ).getServiceName() );
213 
214     }
215 
216     /**
217      * Tests agreement creation.
218      * 
219      * @throws Exception
220      *             indicates a test failure
221      */
222     public void testCreateAgreement() throws Exception
223     {
224         LOG.info( "Entering TestCase: testCreateAgreement" );
225 
226         // START SNIPPET: CreateAgreement
227 
228         AgreementFactoryClient[] factories = getAgreementFactoryClients();
229         assertEquals( EXPECTED_FACTORIES, factories.length );
230 
231         //
232         // lookup factory with resource id "SAMPLE-INSTANCE-1"
233         //
234         AgreementFactoryClient factory = null;
235         for ( int i = 0; i < factories.length; i++ )
236         {
237             if ( "SAMPLE-INSTANCE-1".equals( factories[i].getResourceId() ) )
238             {
239                 factory = factories[i];
240                 break;
241             }
242         }
243 
244         //
245         // retrieve template with name "SAMPLE1" and id "1" from the factory and print it on the console
246         //
247         AgreementTemplateType template = factory.getTemplate( "SAMPLE1", "1" );
248         // log.info("TEMPLATE:\n" + template.xmlText());
249 
250         //
251         // create the agreement offer based on the selected template
252         //
253         AgreementOffer offer = new AgreementOfferType( template );
254 
255         //
256         // get the service description term from the offer
257         //
258         ServiceDescriptionTermType sdt = offer.getTerms().getAll().getServiceDescriptionTermArray( 0 );
259 
260         //
261         // select the service description
262         //
263         // The JobDescriptionDocument is defined as a document element. We use the
264         // JobDescriptionDocument QName to select the JobDescriptionDocument from the SDT.
265         //
266         JobDescriptionType job =
267             (JobDescriptionType) sdt.selectChildren( JobDescriptionDocument.type.getDocumentElementName() )[0];
268 
269         //
270         // changes to the job object are automatically reflected in the offer
271         //
272         job.getResources().addNewIndividualCPUCount().addNewExact().setDoubleValue( 4 );
273 
274         // print the resulting offer to the console
275         // log.info("AGREEMENT OFFER:\n" + offer.getXMLObject());
276 
277         //
278         // now create the agreement instance
279         //
280         AgreementClient agreement = factory.createAgreement( offer );
281 
282         // END SNIPPET: CreateAgreement
283 
284         assertNotNull( "the created agreement must not be <null>", agreement );
285 
286         GuaranteeTermStateType[] guaranteeTermStates = agreement.getGuaranteeTermStates();
287         assertEquals( "the number of guarantee term states must be <2>", guaranteeTermStates.length, 2 );
288 
289         assertEquals( "term_0", guaranteeTermStates[0].getTermName() );
290         assertEquals( "term_1", guaranteeTermStates[1].getTermName() );
291 
292         assertEquals( GuaranteeTermStateDefinition.INT_NOT_DETERMINED,
293                       guaranteeTermStates[0].getState().intValue() );
294         assertEquals( GuaranteeTermStateDefinition.INT_FULFILLED,
295                       guaranteeTermStates[1].getState().intValue() );
296 
297         agreement.destroy();
298     }
299 
300     /**
301      * Tests agreement creation and termination.
302      * 
303      * @throws Exception
304      *             indicates a test failure
305      */
306     public void testCreateTerminateAgreement() throws Exception
307     {
308         LOG.info( "Entering TestCase: testCreateTerminateAgreement" );
309 
310         AgreementFactoryClient[] factories = getAgreementFactoryClients();
311         assertEquals( EXPECTED_FACTORIES, factories.length );
312 
313         for ( int i = 0; i < 2; i++ )
314         {
315             LOG.warn( "+ " + factories[i].getResourceId() );
316             LOG.warn( "   + Number of templates: " + factories[i].getTemplates().length );
317         }
318 
319         AgreementFactoryClient factory = factories[1];
320         AgreementTemplateType[] templates = factory.getTemplates();
321 
322         AgreementOffer offer = new AgreementOfferType( templates[0] );
323         AgreementClient agreement = factory.createAgreement( offer );
324         assertNotNull( agreement );
325 
326         // START SNIPPET: TerminateAgreement
327         // agreement.terminate(TerminateInputType.Factory.newInstance());
328         // END SNIPPET: TerminateAgreement
329 
330         agreement.destroy();
331     }
332 
333     /**
334      * Tests exception handling in the agreement creation process. This includes proper de-/serialization of
335      * faults.
336      * 
337      * @throws Exception
338      *             indicates a test failure
339      */
340     public void testCreateAgreementException() throws Exception
341     {
342         LOG.info( "Entering TestCase: testCreateAgreementException" );
343 
344         AgreementFactoryClient[] factories = getAgreementFactoryClients();
345         assertEquals( EXPECTED_FACTORIES, factories.length );
346 
347         AgreementFactoryClient factory = factories[0];
348         AgreementTemplateType[] templates = factory.getTemplates();
349 
350         AgreementOffer offer = new AgreementOfferType( templates[0] );
351         offer.getTerms().getAll().getServiceDescriptionTermArray( 0 ).setName( "TEST_EXCEPTION" );
352 
353         // START SNIPPET: CreateAgreementException
354         AgreementClient agreement = null;
355         AgreementFactoryException exception = null;
356         try
357         {
358             agreement = factory.createAgreement( offer );
359         }
360         catch ( AgreementFactoryException e )
361         {
362             exception = e;
363         }
364         // END SNIPPET: CreateAgreementException
365 
366         assertNull( agreement );
367         assertNotNull( exception );
368     }
369 
370     /**
371      * Test for Bug 79.
372      * 
373      * Bug 79: There seems to be a problem in creating agreement factory if the URL contains "/" at the end of
374      * the address. It works fine if one uses the following url string:
375      * 
376      * http://localhost:8080/wsag4j-agreement-factory-0.1.14
377      * 
378      */
379     public void testGetFactoryClientURL()
380     {
381         EndpointReferenceType epr = EndpointReferenceType.Factory.newInstance();
382         epr.addNewAddress().setStringValue( getUrl() + "/" );
383 
384         AgreementFactoryRegistryClient registry = null;
385         AgreementFactoryClient[] factories = null;
386 
387         try
388         {
389             registry = AgreementFactoryRegistryLocator.getFactoryRegistry( epr, getLoginContext() );
390             factories = registry.listAgreementFactories();
391             assertEquals( EXPECTED_FACTORIES, factories.length );
392         }
393         catch ( Exception ex )
394         {
395             fail( "Failed to initialize factory list (BUG 79). " + ex.getMessage() );
396         }
397 
398         try
399         {
400             AgreementTemplateType[] templates = factories[0].getTemplates();
401             assertTrue( templates.length > 0 );
402 
403         }
404         catch ( Exception ex )
405         {
406             fail( "Failed to list agreement templates  (BUG 79). " + ex.getMessage() );
407         }
408     }
409 
410     /**
411      * Test for accessing unknown agreement WSRF resources.
412      * 
413      * @throws Exception
414      *             indicates a test failure
415      */
416     public void testResourceUnknown() throws Exception
417     {
418 
419         System.out.println( "Test ResourceUnknownFault." );
420 
421         String eprText =
422             "<wsa:EndpointReference xmlns:wsa=\"http://www.w3.org/2005/08/addressing\">"
423                 + "<wsa:Address xmlns:wsa=\"http://www.w3.org/2005/08/addressing\">{0}{1}</wsa:Address>"
424                 + "<wsa:ReferenceParameters xmlns:wsa=\"http://www.w3.org/2005/08/addressing\">"
425                 + "<ResourceId xmlns=\"http://schemas.scai.fraunhofer.de/wsag4j\">WSAG4J_ResourceId-43</ResourceId>"
426                 + "</wsa:ReferenceParameters><Metadata xmlns=\"http://www.w3.org/2005/08/addressing\">"
427                 + "<ServerIdentity xmlns=\"http://schemas.scai.fraunhofer.de/2008/11/wsag4j/engine\">"
428                 + "1.2.840.113549.1.9.1=#16236f6c697665722e7761656c647269636840736361692e667261756e686f6665722e6465,"
429                 + "CN=wsag4j-server,OU=WSAG4J,O=Fraunhofer SCAI/bio,ST=NRW,C=DE</ServerIdentity>"
430                 + "</Metadata></wsa:EndpointReference>";
431 
432         Object[] filler = new Object[] { getUrl(), "/services/AgreementFactory" };
433         String eprXML = MessageFormat.format( eprText, filler );
434         EndpointReferenceType epr = EndpointReferenceDocument.Factory.parse( eprXML ).getEndpointReference();
435 
436         AgreementFactoryClient client =
437             new RemoteAgreementFactoryClientImpl( epr, new SecurityProperties( getLoginContext() ) );
438 
439         boolean exceptionReceived = false;
440 
441         try
442         {
443             LOG.info( "Invoking GetTemplates on an unknown resource should throw a ResourceUnknownFault." );
444             client.getTemplates();
445         }
446         catch ( ResourceUnknownException e )
447         {
448             exceptionReceived = true;
449             LOG.info( "ResourceUnknownFault received" );
450         }
451 
452         assertTrue( "ResourceUnknownFault was not received.", exceptionReceived );
453     }
454 
455 }