View Javadoc

1   /* 
2    * Copyright (c) 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.doc;
36  
37  import javax.security.auth.login.LoginContext;
38  
39  import org.ggf.schemas.jsdl.x2005.x11.jsdl.JobDescriptionDocument;
40  import org.ggf.schemas.jsdl.x2005.x11.jsdl.JobDescriptionType;
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.AgreementFactoryRegistryClient;
45  import org.ogf.graap.wsag.api.exceptions.AgreementFactoryException;
46  import org.ogf.graap.wsag.api.types.AgreementOfferType;
47  import org.ogf.graap.wsag.client.AgreementFactoryRegistryLocator;
48  import org.ogf.graap.wsag.it.AbstractIntegrationTest;
49  import org.ogf.graap.wsag.security.core.KeystoreProperties;
50  import org.ogf.graap.wsag.security.core.keystore.KeystoreLoginContext;
51  import org.ogf.schemas.graap.wsAgreement.AgreementTemplateType;
52  import org.ogf.schemas.graap.wsAgreement.ServiceDescriptionTermType;
53  import org.ogf.schemas.graap.wsAgreement.TerminateInputType;
54  import org.w3.x2005.x08.addressing.EndpointReferenceType;
55  
56  /**
57   * Code snippets used in documentation.
58   * 
59   * @author Karl Catewicz
60   */
61  public class AbstractAgreementIntegrationSnippets
62  {
63  
64      /**
65       * number of factories configured in the framework
66       */
67      public static final int EXPECTED_FACTORIES = 2;
68  
69      /**
70       * number of templates at factory 1
71       */
72      public static final int EXPECTED_TEMPLATES_FACTORY_1 = 4;
73  
74      /**
75       * Returns the login context used in the unit tests.
76       * 
77       * @return the client login context
78       * 
79       * @throws Exception
80       *             indicates a failure during test case execution
81       */
82      public static LoginContext getLoginContext() throws Exception
83      {
84          /*
85           * create a keystore login context
86           */
87          // START SNIPPET: CreateLoginContext
88          KeystoreProperties properties = new KeystoreProperties();
89          properties.setKeyStoreAlias( "wsag4j-user" );
90          properties.setPrivateKeyPassword( "user@wsag4j" );
91  
92          properties.setKeyStoreType( "JKS" );
93          properties.setKeystoreFilename( "/wsag4j-client-keystore.jks" );
94          properties.setKeystorePassword( "user@wsag4j" );
95  
96          properties.setTruststoreType( "JKS" );
97          properties.setTruststoreFilename( "/wsag4j-client-keystore.jks" );
98          properties.setTruststorePassword( "user@wsag4j" );
99  
100         LoginContext loginContext = new KeystoreLoginContext( properties );
101         loginContext.login();
102         // END SNIPPET: CreateLoginContext
103 
104         return loginContext;
105     }
106 
107     /**
108      * Code snippet to illustrate how templates are retrieved from an agreement factory.
109      * 
110      * @throws Exception
111      *             indicates a failure during test case execution
112      */
113     @SuppressWarnings( "unused" )
114     public void testGetTemplate() throws Exception
115     {
116 
117         AgreementFactoryClient[] factories = getAgreementFactoryClients();
118         //
119         // We have 2 factories installed. One returns 1 template, the other 2.
120         //
121 
122         AgreementFactoryClient factory = null;
123         for ( int i = 0; i < factories.length; i++ )
124         {
125             AgreementTemplateType[] templates = factories[i].getTemplates();
126             if ( templates.length == EXPECTED_TEMPLATES_FACTORY_1 )
127             {
128                 factory = factories[i];
129             }
130         }
131         // START SNIPPET: GetTemplate
132         AgreementTemplateType[] templates = factory.getTemplates();
133         // END SNIPPET: GetTemplate
134     }
135 
136     /**
137      * Code snippet to illustrate how agreements are created with an agreement factory.
138      * 
139      * @throws Exception
140      *             indicates a failure during test case execution
141      */
142     public void testCreateAgreement() throws Exception
143     {
144 
145         AgreementFactoryClient[] factories = getAgreementFactoryClients();
146 
147         //
148         // lookup factory with resource id "SAMPLE-INSTANCE-1"
149         //
150         // START SNIPPET: LookupFactoryWithID
151         AgreementFactoryClient factory = null;
152         for ( int i = 0; i < factories.length; i++ )
153         {
154             if ( "SAMPLE-INSTANCE-1".equals( factories[i].getResourceId() ) )
155             {
156                 factory = factories[i];
157                 break;
158             }
159         }
160         // END SNIPPET: LookupFactoryWithID
161         //
162         // retrieve template with name "SAMPLE1" and id "1" from the factory and print it on the console
163         //
164         // START SNIPPET: RetrieveTemplateWithID
165         AgreementTemplateType template = factory.getTemplate( "SAMPLE1", "1" );
166         // END SNIPPET: RetrieveTemplateWithID
167 
168         // START SNIPPET: CreateAgreement
169 
170         //
171         // create the agreement offer based on the selected template
172         //
173         AgreementOffer offer = new AgreementOfferType( template );
174 
175         //
176         // get the service description term from the offer
177         //
178         ServiceDescriptionTermType sdt = offer.getTerms().getAll().getServiceDescriptionTermArray( 0 );
179 
180         //
181         // select the service description
182         //
183         // The JobDescriptionDocument is defined as a document element. We use the
184         // JobDescriptionDocument QName to select the JobDescriptionDocument from the SDT.
185         //
186         JobDescriptionType job =
187             (JobDescriptionType) sdt.selectChildren( JobDescriptionDocument.type.getDocumentElementName() )[0];
188 
189         //
190         // changes to the job object are automatically reflected in the offer
191         //
192         job.getResources().addNewIndividualCPUCount().addNewExact().setDoubleValue( 4 );
193 
194         //
195         // now create the agreement instance
196         //
197         AgreementClient agreement = factory.createAgreement( offer );
198 
199         // END SNIPPET: CreateAgreement
200 
201         agreement.destroy();
202     }
203 
204     /**
205      * Code snippet to illustrate exception handling during agreements creation.
206      * 
207      * @throws Exception
208      *             indicates a failure during test case execution
209      */
210     @SuppressWarnings( "unused" )
211     public void testCreateAgreementException() throws Exception
212     {
213 
214         AgreementFactoryClient[] factories = getAgreementFactoryClients();
215 
216         AgreementFactoryClient factory = factories[0];
217         AgreementTemplateType[] templates = factory.getTemplates();
218 
219         AgreementOffer offer = new AgreementOfferType( templates[0] );
220         offer.getTerms().getAll().getServiceDescriptionTermArray( 0 ).setName( "TEST_EXCEPTION" );
221 
222         // START SNIPPET: CreateAgreementException
223         AgreementClient agreement = null;
224         AgreementFactoryException exception = null;
225         try
226         {
227             agreement = factory.createAgreement( offer );
228         }
229         catch ( AgreementFactoryException e )
230         {
231             exception = e;
232         }
233         // END SNIPPET: CreateAgreementException
234     }
235 
236     /**
237      * Code snippet to illustrate agreements termination.
238      * 
239      * @throws Exception
240      *             indicates a failure during test case execution
241      */
242     public void testCreateTerminateAgreement() throws Exception
243     {
244 
245         AgreementFactoryClient[] factories = getAgreementFactoryClients();
246 
247         AgreementFactoryClient factory = factories[1];
248         AgreementTemplateType[] templates = factory.getTemplates();
249 
250         AgreementOffer offer = new AgreementOfferType( templates[0] );
251         AgreementClient agreement = factory.createAgreement( offer );
252 
253         // START SNIPPET: TerminateAgreement
254         agreement.terminate( TerminateInputType.Factory.newInstance() );
255         // END SNIPPET: TerminateAgreement
256 
257         agreement.destroy();
258     }
259 
260     /**
261      * Returns the factory clients for this tests.
262      * 
263      * @return the factory clients
264      * 
265      * @throws Exception
266      *             indicates an error while generating the agreement factory client list
267      */
268     public static AgreementFactoryClient[] getAgreementFactoryClients() throws Exception
269     {
270         AgreementFactoryClient[] factories = null;
271 
272         LoginContext loginContext = AbstractIntegrationTest.getLoginContext();
273 
274         // START SNIPPET: ListAgreementFactories
275         EndpointReferenceType epr = EndpointReferenceType.Factory.newInstance();
276         epr.addNewAddress().setStringValue( "http://127.0.0.1:8080/wsag4j" );
277 
278         AgreementFactoryRegistryClient registry =
279             AgreementFactoryRegistryLocator.getFactoryRegistry( epr, loginContext );
280         factories = registry.listAgreementFactories();
281         // END SNIPPET: ListAgreementFactories
282 
283         return factories;
284     }
285 
286 }