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 org.apache.log4j.Logger;
38  import org.apache.xmlbeans.XmlObject;
39  import org.ogf.graap.wsag.api.AgreementOffer;
40  import org.ogf.graap.wsag.api.client.AgreementClient;
41  import org.ogf.graap.wsag.api.client.AgreementFactoryClient;
42  import org.ogf.graap.wsag.api.exceptions.AgreementFactoryException;
43  import org.ogf.graap.wsag.api.exceptions.ResourceUnavailableException;
44  import org.ogf.graap.wsag.api.exceptions.ResourceUnknownException;
45  import org.ogf.graap.wsag.api.types.AgreementOfferType;
46  import org.ogf.schemas.graap.wsAgreement.AgreementStateDefinition;
47  import org.ogf.schemas.graap.wsAgreement.AgreementTemplateType;
48  import org.ogf.schemas.graap.wsAgreement.ServiceDescriptionTermType;
49  import org.w3.x2005.x08.addressing.EndpointReferenceDocument;
50  import org.w3.x2005.x08.addressing.EndpointReferenceType;
51  
52  /**
53   * Abstract test case for creation of pending agreements.
54   * 
55   * @author Oliver Waeldrich
56   * 
57   */
58  public abstract class AbstractPendingAgreementFactoryTest extends AbstractIntegrationTest
59  {
60  
61      /**
62       * waiting time for doing the sub-contracting (pending agreement)
63       */
64      private static final int SUBCONTRACTING_WAITING_TIME = 8000;
65  
66      private static final Logger LOG = Logger.getLogger( AbstractPendingAgreementFactoryTest.class );
67  
68      /**
69       * 
70       * @param name
71       *            the test name
72       */
73      public AbstractPendingAgreementFactoryTest( String name )
74      {
75          super( name );
76      }
77  
78      /**
79       * Creates a pending agreement that is accepted after 1 second. The agreement is destroyed afterwards.
80       */
81      public void testCreatePendingAgreement()
82      {
83          try
84          {
85              LOG.info( "Entering TestCase: testCreatePendingAgreement" );
86  
87              AgreementFactoryClient[] factories = getAgreementFactoryClients();
88              assertEquals( EXPECTED_FACTORIES, factories.length );
89  
90              LOG.info( "Overview of the available factories and their templates." );
91              for ( int i = 0; i < 2; i++ )
92              {
93                  LOG.info( "+ " + factories[i].getResourceId() );
94  
95                  AgreementTemplateType[] templates = factories[i].getTemplates();
96                  LOG.info( "   + Num of templates: " + templates.length );
97  
98                  for ( int k = 0; k < templates.length; k++ )
99                  {
100                     LOG.info( "      + " + templates[k].getName() );
101                 }
102             }
103 
104             AgreementFactoryClient factory = factories[0];
105 
106             AgreementClient[] agreements;
107 
108             LOG.info( "Creating new Agreements" );
109             AgreementTemplateType template = factory.getTemplate( "SAMPLE4-PENDING-AGREEMENT", "1" );
110             AgreementOffer offer = new AgreementOfferType( template );
111 
112             LOG.info( "Create pending agreement" );
113             AgreementClient agreement1 = factory.createPendingAgreement( offer );
114 
115             assertNotNull( "the created agreement must not be <null>", agreement1 );
116 
117             assertEquals( "The state of the pending agreement must be PENDING.",
118                           AgreementStateDefinition.INT_PENDING, agreement1.getState().getState().intValue() );
119 
120             //
121             // Waiting for acceptance
122             //
123             while ( agreement1.getState().getState() != AgreementStateDefinition.OBSERVED )
124             {
125                 Thread.sleep( 1000 );
126             }
127 
128             LOG.info( "Destroy agreement 1" );
129             agreement1.destroy();
130             agreements = factory.listAgreements();
131             assertEquals( 0, agreements.length );
132 
133         }
134         catch ( AgreementFactoryException e )
135         {
136             fail( "AgreementFactoryException: " + e.getMessage() );
137         }
138         catch ( ResourceUnavailableException e )
139         {
140             fail( "ResourceUnavailableException: " + e.getMessage() );
141         }
142         catch ( ResourceUnknownException e )
143         {
144             fail( "ResourceUnknownException: " + e.getMessage() );
145         }
146         catch ( Exception e )
147         {
148             fail( "Could not create agreement factory instance. Error: " + e.getMessage() );
149 
150             if ( LOG.isDebugEnabled() )
151             {
152                 LOG.debug( e );
153             }
154         }
155     }
156 
157     /**
158      * Creates a pending agreement that is accepted after 1 second. On acceptance the creator is notified
159      * using the agreement acceptance port type.
160      */
161     public void testCreatePendingAgreementWithNotification()
162     {
163         try
164         {
165             LOG.info( "Entering TestCase: testCreatePendingAgreementWithNotification" );
166 
167             AgreementFactoryClient[] factories = getAgreementFactoryClients();
168             assertEquals( EXPECTED_FACTORIES, factories.length );
169 
170             LOG.info( "Overview of the available factories and their templates." );
171             for ( int i = 0; i < 2; i++ )
172             {
173                 LOG.info( "+ " + factories[i].getResourceId() );
174 
175                 AgreementTemplateType[] templates = factories[i].getTemplates();
176                 LOG.info( "   + Num of templates: " + templates.length );
177 
178                 for ( int k = 0; k < templates.length; k++ )
179                 {
180                     LOG.info( "      + " + templates[k].getName() );
181                 }
182             }
183 
184             AgreementFactoryClient factory = factories[1];
185             AgreementFactoryClient scFactory = factories[0];
186 
187             AgreementClient[] agreements;
188 
189             /*
190              * The agreement we create is a pending agreement. The agreement responder subcontracts another
191              * factory with the service provisioning. If the subcontractor accepts the creation of the
192              * agreement, it notifies the agreement responder via agreement acceptance. The responder than
193              * changes the state of the agreement accordingly.
194              */
195             LOG.info( "Creating new Agreements" );
196             AgreementTemplateType template =
197                 factory.getTemplate( "SAMPLE5-PENDING-AGREEMENT-WITH-NOTIFICATION", "1" );
198 
199             //
200             // update the subcontractor EPR for the offer
201             //
202             ServiceDescriptionTermType sdt = template.getTerms().getAll().getServiceDescriptionTermArray( 0 );
203             XmlObject[] children =
204                 sdt.selectChildren( EndpointReferenceDocument.type.getDocumentElementName() );
205             EndpointReferenceType targetEPR = (EndpointReferenceType) children[0];
206             targetEPR.set( scFactory.getEndpoint() );
207 
208             AgreementOffer offer = new AgreementOfferType( template );
209 
210             LOG.info( "Create pending agreement" );
211             AgreementClient agreement1 = factory.createPendingAgreement( offer );
212 
213             assertNotNull( "the created agreement must not be <null>", agreement1 );
214 
215             // assertEquals("The state of the pending agreement must be PENDING.",
216             // AgreementStateDefinition.INT_PENDING, agreement1.getState().getState().intValue());
217 
218             //
219             // The subcontractor needs 5 seconds for decision making,
220             // so we wait for 6 seconds.
221             //
222             Thread.sleep( SUBCONTRACTING_WAITING_TIME );
223 
224             // assertEquals("The state of the pending agreement must now be OBSERVED.",
225             // AgreementStateDefinition.INT_COMPLETE, agreement1.getState().getState().intValue());
226 
227             LOG.info( "Destroy agreement 1" );
228             agreement1.destroy();
229             agreements = factory.listAgreements();
230             assertEquals( 0, agreements.length );
231         }
232         catch ( AgreementFactoryException e )
233         {
234             fail( "AgreementFactoryException: " + e.getMessage() );
235         }
236         catch ( ResourceUnavailableException e )
237         {
238             fail( "ResourceUnavailableException: " + e.getMessage() );
239         }
240         catch ( ResourceUnknownException e )
241         {
242             fail( "ResourceUnknownException: " + e.getMessage() );
243         }
244         catch ( Exception e )
245         {
246             fail( "Could not create agreement factory instance. Error: " + e.getMessage() );
247 
248             if ( LOG.isDebugEnabled() )
249             {
250                 LOG.debug( e );
251             }
252         }
253     }
254 }