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.ogf.graap.wsag.api.AgreementOffer;
39  import org.ogf.graap.wsag.api.client.AgreementClient;
40  import org.ogf.graap.wsag.api.client.AgreementFactoryClient;
41  import org.ogf.graap.wsag.api.exceptions.AgreementFactoryException;
42  import org.ogf.graap.wsag.api.exceptions.ResourceUnavailableException;
43  import org.ogf.graap.wsag.api.exceptions.ResourceUnknownException;
44  import org.ogf.graap.wsag.api.types.AgreementOfferType;
45  import org.ogf.schemas.graap.wsAgreement.AgreementTemplateType;
46  
47  /**
48   * AbstractAgreementFactoryTest
49   * 
50   * @author Oliver Waeldrich
51   * 
52   */
53  public abstract class AbstractAgreementRegistryTest extends AbstractIntegrationTest
54  {
55  
56      @SuppressWarnings( "unused" )
57      private static final Logger LOG = Logger.getLogger( AbstractAgreementRegistryTest.class );
58  
59      /**
60       * 
61       * @param name
62       *            the test name
63       */
64      public AbstractAgreementRegistryTest( String name )
65      {
66          super( name );
67      }
68  
69      /**
70       * Tests adding and removing agreements from/to the agreement service group.
71       */
72      public void testRegistryAddRemove()
73      {
74          try
75          {
76              AgreementFactoryClient[] factories = getAgreementFactoryClients();
77              assertEquals( EXPECTED_FACTORIES, factories.length );
78  
79              assertTrue( "There are at least 2 agreement factories configured", EXPECTED_FACTORIES >= 2 );
80  
81              AgreementFactoryClient factory1 = factories[0];
82              AgreementFactoryClient factory2 = factories[1];
83  
84              assertEquals( 0, factory1.listAgreements().length );
85              assertEquals( 0, factory2.listAgreements().length );
86  
87              AgreementTemplateType[] templates1 = factory1.getTemplates();
88              AgreementTemplateType[] templates2 = factory2.getTemplates();
89  
90              AgreementOffer offer1 = new AgreementOfferType( templates1[0] );
91              AgreementOffer offer2 = new AgreementOfferType( templates2[0] );
92  
93              factory1.createAgreement( offer1 );
94              assertEquals( 1, factory1.listAgreements().length );
95              assertEquals( 0, factory2.listAgreements().length );
96  
97              factory1.createAgreement( offer1 );
98              assertEquals( 2, factory1.listAgreements().length );
99              assertEquals( 0, factory2.listAgreements().length );
100 
101             factory2.createAgreement( offer2 );
102             assertEquals( 2, factory1.listAgreements().length );
103             assertEquals( 1, factory2.listAgreements().length );
104 
105             factory2.createAgreement( offer2 );
106             assertEquals( 2, factory1.listAgreements().length );
107             assertEquals( 2, factory2.listAgreements().length );
108 
109             factory1.listAgreements()[0].destroy();
110             assertEquals( 1, factory1.listAgreements().length );
111             assertEquals( 2, factory2.listAgreements().length );
112 
113             factory2.listAgreements()[0].destroy();
114             assertEquals( 1, factory1.listAgreements().length );
115             assertEquals( 1, factory2.listAgreements().length );
116 
117             factory1.listAgreements()[0].destroy();
118             assertEquals( 0, factory1.listAgreements().length );
119             assertEquals( 1, factory2.listAgreements().length );
120 
121             factory2.listAgreements()[0].destroy();
122             assertEquals( 0, factory1.listAgreements().length );
123             assertEquals( 0, factory2.listAgreements().length );
124 
125         }
126         catch ( AgreementFactoryException e )
127         {
128             fail( "test failed due to a AgreementFactoryException\n" + e.getMessage() );
129         }
130         catch ( ResourceUnknownException e )
131         {
132             fail( "test failed due to a ResourceUnknownException\n" + e.getMessage() );
133         }
134         catch ( ResourceUnavailableException e )
135         {
136             fail( "test failed due to a ResourceUnavailableException\n" + e.getMessage() );
137         }
138         catch ( Exception e )
139         {
140             fail( "AgreementFactoryLocator.getFactory() failed due to a Exception\n" + e.getMessage() );
141         }
142 
143     }
144 
145     /**
146      * Tests listing agreement instances registered in an agreement service group.
147      */
148     public void testListAgreements()
149     {
150         try
151         {
152             AgreementFactoryClient[] factories = getAgreementFactoryClients();
153             assertEquals( EXPECTED_FACTORIES, factories.length );
154 
155             AgreementFactoryClient factory = factories[0];
156             AgreementClient[] agreements = factory.listAgreements();
157             // END SNIPPET: ListAgreements
158 
159             AgreementTemplateType[] templates = factory.getTemplates();
160 
161             assertNotNull( "the registered agreements must not be <null>", agreements );
162             assertEquals( "the number of registered agreements <0>", 0, agreements.length );
163 
164             AgreementOffer offer = new AgreementOfferType( templates[0] );
165             AgreementClient agreement = factory.createAgreement( offer );
166 
167             assertNotNull( "the created agreement must not be <null>", agreement );
168 
169             agreements = factory.listAgreements();
170             assertNotNull( "the registered agreements must not be <null>", agreements );
171             assertEquals( "the number of registered agreements must be <1>", 1, agreements.length );
172 
173             assertEquals( "The name of the agreement must match <SampleAgreement>", "SampleAgreement",
174                           agreements[0].getName() );
175 
176             offer = new AgreementOfferType( templates[0] );
177             agreement = factory.createAgreement( offer );
178 
179             assertNotNull( "the created agreement must not be <null>", agreement );
180 
181             agreements = factory.listAgreements();
182             assertNotNull( "the registered agreements must not be <null>", agreements );
183             assertEquals( "the number of registered agreements must be <2>", 2, agreements.length );
184 
185             assertEquals( "The name of the agreement must match <SampleAgreement>", "SampleAgreement",
186                           agreements[0].getName() );
187             assertEquals( "The name of the agreement must match <SampleAgreement>", "SampleAgreement",
188                           agreements[1].getName() );
189 
190         }
191         catch ( AgreementFactoryException e )
192         {
193             fail( "test failed due to a AgreementFactoryException\n" + e.getMessage() );
194         }
195         catch ( ResourceUnknownException e )
196         {
197             fail( "test failed due to a ResourceUnknownException\n" + e.getMessage() );
198         }
199         catch ( ResourceUnavailableException e )
200         {
201             fail( "test failed due to a ResourceUnavailableException\n" + e.getMessage() );
202         }
203         catch ( Exception e )
204         {
205             fail( "AgreementFactoryLocator.getFactory() failed due to a Exception\n" + e.getMessage() );
206         }
207     }
208 }