View Javadoc

1   /* 
2    * Copyright (c) 2007, 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.util.Calendar;
38  import java.util.GregorianCalendar;
39  
40  import org.apache.log4j.Logger;
41  import org.ogf.graap.wsag.api.AgreementOffer;
42  import org.ogf.graap.wsag.api.client.AgreementClient;
43  import org.ogf.graap.wsag.api.client.AgreementFactoryClient;
44  import org.ogf.graap.wsag.api.client.NegotiationClient;
45  import org.ogf.graap.wsag.api.exceptions.NegotiationException;
46  import org.ogf.graap.wsag.api.exceptions.NegotiationFactoryException;
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.types.AgreementOfferType;
50  import org.ogf.schemas.graap.wsAgreement.AgreementTemplateType;
51  import org.ogf.schemas.graap.wsAgreement.negotiation.NegotiationConstraintSectionType;
52  import org.ogf.schemas.graap.wsAgreement.negotiation.NegotiationContextDocument;
53  import org.ogf.schemas.graap.wsAgreement.negotiation.NegotiationContextType;
54  import org.ogf.schemas.graap.wsAgreement.negotiation.NegotiationOfferContextType;
55  import org.ogf.schemas.graap.wsAgreement.negotiation.NegotiationOfferStateType;
56  import org.ogf.schemas.graap.wsAgreement.negotiation.NegotiationOfferType;
57  import org.ogf.schemas.graap.wsAgreement.negotiation.NegotiationRoleType;
58  
59  /**
60   * Default unit test implementation of the WS-Agreement Negotiation port type.
61   * 
62   * @author hrasheed
63   * 
64   */
65  public abstract class AbstractAgreementNegotiationTest extends AbstractIntegrationTest
66  {
67  
68      private static final Logger LOG = Logger.getLogger( AbstractAgreementNegotiationTest.class );
69  
70      private static final String FACTORY_RESOURCE_ID = "SAMPLE-INSTANCE-1";
71  
72      private static final String TEMPLATE_NAME = "SAMPLE1";
73  
74      /**
75       * 
76       * @param name
77       *            the test name
78       */
79      public AbstractAgreementNegotiationTest( String name )
80      {
81          super( name );
82      }
83  
84      /**
85       * Tests the negotiation of offers and counter offers.
86       * 
87       * @throws Exception
88       *             indicates a test failure
89       */
90      public void testAgreementNegotiation() throws Exception
91      {
92  
93          NegotiationClient negotiation = initiateNegotiation();
94  
95          try
96          {
97  
98              LOG.info( "getting NegotionContext" );
99  
100             NegotiationContextType context = negotiation.getNegotiationContext();
101             assertNotNull( context );
102             if ( LOG.isTraceEnabled() )
103             {
104                 LOG.trace( "negotiationContext: " + context.xmlText() );
105             }
106 
107             //
108             // retrieve the agreement templates for which negotiation is supported
109             //
110 
111             LOG.info( "getting negotiable templates" );
112 
113             AgreementTemplateType[] negotiableTemplates = negotiation.getNegotiableTemplates();
114             assertNotNull( negotiableTemplates );
115             if ( LOG.isTraceEnabled() )
116             {
117                 LOG.trace( "negotiable templates:" );
118                 for ( int i = 0; i < negotiableTemplates.length; i++ )
119                 {
120                     LOG.trace( negotiableTemplates[i].xmlText() );
121                 }
122             }
123 
124             //
125             // negotiate an acceptable agreement offer
126             //
127 
128             LOG.info( "negotiating with negotiation instance" );
129 
130             AgreementTemplateType template = null;
131             for ( int i = 0; i < negotiableTemplates.length; i++ )
132             {
133                 AgreementTemplateType agreementTemplate = negotiableTemplates[i];
134                 if ( agreementTemplate.getName().equals( TEMPLATE_NAME ) )
135                 {
136                     template = agreementTemplate;
137                 }
138             }
139 
140             assertEquals( TEMPLATE_NAME, template.getName() );
141             if ( LOG.isTraceEnabled() )
142             {
143                 LOG.trace( "tempalte to negotiate: " + template.xmlText() );
144             }
145 
146             NegotiationOfferType negotiationOfferType = NegotiationOfferType.Factory.newInstance();
147             negotiationOfferType.setName( template.getContext().getTemplateName() );
148             negotiationOfferType.setAgreementId( template.getContext().getTemplateId() );
149             negotiationOfferType.addNewContext().set( template.getContext() );
150             negotiationOfferType.addNewTerms().set( template.getTerms() );
151 
152             NegotiationOfferContextType negOfferContext = NegotiationOfferContextType.Factory.newInstance();
153             negOfferContext.setExpirationTime( Calendar.getInstance() );
154             negOfferContext.setCreator( NegotiationRoleType.NEGOTIATION_INITIATOR );
155             NegotiationOfferStateType negOfferState = NegotiationOfferStateType.Factory.newInstance();
156             negOfferState.addNewAdvisory();
157             negOfferContext.setState( negOfferState );
158 
159             String templateId = template.getTemplateId() + "-" + template.getName();
160             negOfferContext.setCounterOfferTo( templateId );
161 
162             NegotiationConstraintSectionType constraints =
163                 NegotiationConstraintSectionType.Factory.newInstance();
164             constraints.addNewConstraint();
165 
166             negotiationOfferType.setNegotiationOfferContext( negOfferContext );
167             negotiationOfferType.setNegotiationConstraints( constraints );
168             negotiationOfferType.setOfferId( templateId );
169 
170             NegotiationOfferType[] negotiationOfferTypes = { negotiationOfferType };
171             if ( LOG.isTraceEnabled() )
172             {
173                 LOG.trace( "negotiation offer: " + negotiationOfferTypes[0].xmlText() );
174             }
175 
176             NegotiationOfferType[] counterOffers = negotiation.negotiate( negotiationOfferTypes );
177             assertNotNull( counterOffers );
178             assertEquals( 1, counterOffers.length );
179 
180             NegotiationOfferType counterOffer = counterOffers[0];
181             if ( LOG.isTraceEnabled() )
182             {
183                 LOG.trace( "counter offer: " + counterOffer.xmlText() );
184             }
185 
186             assertNotNull( counterOffer.getOfferId() );
187             assertFalse( counterOffer.getOfferId().equals( "" ) );
188             assertNotNull( counterOffer.getNegotiationOfferContext() );
189             assertNotNull( counterOffer.getNegotiationConstraints() );
190             assertNotNull( counterOffer.getContext() );
191             assertNotNull( counterOffer.getTerms() );
192             assertNotNull( negotiationOfferType.getOfferId(),
193                            counterOffer.getNegotiationOfferContext().getCounterOfferTo() );
194             assertNotNull( counterOffer.getAgreementId() );
195             assertEquals( template.getName(), counterOffer.getName() );
196 
197             NegotiationOfferType[] offers = negotiation.getNegotiationOffers();
198             assertNotNull( offers );
199             if ( LOG.isTraceEnabled() )
200             {
201                 LOG.trace( "valid offers[0]: " + offers[0].xmlText() );
202             }
203 
204             //
205             // create agreement offer and create agreement
206             //
207 
208             AgreementOffer offer = new AgreementOfferType( offers[0] );
209             AgreementClient agreement = getFactoryClient().createAgreement( offer );
210             assertNotNull( agreement );
211 
212             //
213             // finally terminate the negotiation process
214             //
215             LOG.info( "terminating agreement negotiation" );
216             negotiation.terminate();
217 
218         }
219         catch ( NegotiationException e )
220         {
221             fail( "NegotiationException: " + e.getMessage() );
222         }
223         catch ( ResourceUnavailableException e )
224         {
225             fail( "ResourceUnavailableException: " + e.getMessage() );
226         }
227         catch ( ResourceUnknownException e )
228         {
229             fail( "ResourceUnknownException: " + e.getMessage() );
230         }
231         catch ( Exception e )
232         {
233             fail( "negotiation test fails. Error: " + e.getMessage() );
234         }
235     }
236 
237     private NegotiationClient initiateNegotiation()
238     {
239         AgreementFactoryClient factory = null;
240         NegotiationClient negotiation = null;
241 
242         try
243         {
244 
245             factory = getFactoryClient();
246 
247             NegotiationContextDocument negContextDoc = NegotiationContextDocument.Factory.newInstance();
248             NegotiationContextType negContext = negContextDoc.addNewNegotiationContext();
249             negContext.setAgreementResponder( NegotiationRoleType.NEGOTIATION_RESPONDER );
250             negContext.setExpirationTime( new GregorianCalendar() );
251 
252             negContext.addNewNegotiationType().addNewNegotiation();
253 
254             negotiation = factory.initiateNegotiation( negContext );
255             assertNotNull( "the created negotiatin client is not null", negotiation );
256 
257             LOG.info( "negotiation instance is created successfully" );
258 
259         }
260         catch ( NegotiationFactoryException e )
261         {
262             fail( "NegotiationFactoryException: " + e.getMessage() );
263         }
264         catch ( ResourceUnavailableException e )
265         {
266             fail( "ResourceUnavailableException: " + e.getMessage() );
267         }
268         catch ( ResourceUnknownException e )
269         {
270             fail( "ResourceUnknownException: " + e.getMessage() );
271         }
272         catch ( Exception e )
273         {
274             fail( "Could not create negotiation client instance. Error: " + e.getMessage() );
275         }
276 
277         return negotiation;
278     }
279 
280     /**
281      * @return
282      * @throws ResourceUnknownException
283      * @throws ResourceUnavailableException
284      */
285     private AgreementFactoryClient getFactoryClient()
286         throws ResourceUnknownException, ResourceUnavailableException
287     {
288         AgreementFactoryClient factory;
289         //
290         // First create a negotiation instance. The instance is created
291         // based on a negotiation context that specifies the type of the
292         // negotiation process (NEGOTIATION) and the negotiation participators.
293         //
294         AgreementFactoryClient[] factories = getAgreementFactoryClients();
295         assertEquals( EXPECTED_FACTORIES, factories.length );
296 
297         if ( factories[0].getResourceId().equals( FACTORY_RESOURCE_ID ) )
298         {
299             factory = getAgreementFactoryClients()[0];
300         }
301         else
302         {
303             factory = getAgreementFactoryClients()[1];
304         }
305         return factory;
306     }
307 }