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 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
55
56
57
58
59 public abstract class AbstractPendingAgreementFactoryTest extends AbstractIntegrationTest
60 {
61
62
63
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
72
73
74 public AbstractPendingAgreementFactoryTest( String name )
75 {
76 super( name );
77 }
78
79
80
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
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
160
161
162
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
193
194
195
196
197 LOG.info( "Creating new Agreements" );
198 AgreementTemplateType template =
199 factory.getTemplate( "SAMPLE5-PENDING-AGREEMENT-WITH-NOTIFICATION", "1" );
200
201
202
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
218
219
220
221
222
223
224
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
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 }