1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
61
62
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
77
78
79 public AbstractAgreementNegotiationTest( String name )
80 {
81 super( name );
82 }
83
84
85
86
87
88
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
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
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
206
207
208 AgreementOffer offer = new AgreementOfferType( offers[0] );
209 AgreementClient agreement = getFactoryClient().createAgreement( offer );
210 assertNotNull( agreement );
211
212
213
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
282
283
284
285 private AgreementFactoryClient getFactoryClient()
286 throws ResourceUnknownException, ResourceUnavailableException
287 {
288 AgreementFactoryClient factory;
289
290
291
292
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 }