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