1   /**
2    *
3    * Copyright 2004 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  package org.apache.ws.scout.registry.qa;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertNull;
21  import static org.junit.Assert.assertTrue;
22  import static org.junit.Assert.fail;
23  
24  import java.util.ArrayList;
25  import java.util.Collection;
26  import java.util.Iterator;
27  
28  import javax.xml.registry.BulkResponse;
29  import javax.xml.registry.JAXRException;
30  import javax.xml.registry.JAXRResponse;
31  import javax.xml.registry.RegistryService;
32  import javax.xml.registry.infomodel.Classification;
33  import javax.xml.registry.infomodel.ClassificationScheme;
34  import javax.xml.registry.infomodel.Key;
35  import javax.xml.registry.infomodel.Organization;
36  import javax.xml.registry.infomodel.Service;
37  import javax.xml.registry.infomodel.ServiceBinding;
38  
39  import junit.framework.JUnit4TestAdapter;
40  
41  import org.apache.ws.scout.BaseTestCase;
42  import org.apache.ws.scout.Creator;
43  import org.apache.ws.scout.Finder;
44  import org.apache.ws.scout.Printer;
45  import org.apache.ws.scout.Remover;
46  import org.junit.After;
47  import org.junit.Before;
48  import org.junit.Test;
49  
50  /**
51   * Test to check Jaxr Publish
52   * Open source UDDI Browser  <http://www.uddibrowser.org>
53   * can be used to check your results
54   * @author <mailto:anil@apache.org>Anil Saldhana
55   * @since Nov 20, 2004
56   */
57  public class JAXR010OrganizationTest extends BaseTestCase
58  {
59      @Before
60      public void setUp()
61      {
62          super.setUp();
63      }
64      
65      @After
66      public void tearDown()
67      {
68        super.tearDown();
69      }
70      
71      @Test 
72      public void publishClassificationScheme()
73      {
74          login();
75          try
76          {
77              RegistryService rs = connection.getRegistryService();
78              blm = rs.getBusinessLifeCycleManager();
79              Creator creator = new Creator(blm);
80              
81              Collection<ClassificationScheme> schemes = new ArrayList<ClassificationScheme>();
82              ClassificationScheme classificationScheme = creator.createClassificationScheme(this.getClass().getName());
83              schemes.add(classificationScheme);
84              
85              BulkResponse bulkResponse = blm.saveClassificationSchemes(schemes);
86              assertEquals(JAXRResponse.STATUS_SUCCESS,bulkResponse.getStatus());
87              
88              
89          } catch (JAXRException e) {
90              e.printStackTrace();
91              assertTrue(false);
92          }   
93      }
94      
95      @Test
96      public void publishOrganization()
97      {
98          BulkResponse response = null;
99          login();
100         try
101         {
102             RegistryService rs = connection.getRegistryService();
103             blm = rs.getBusinessLifeCycleManager();
104             bqm = rs.getBusinessQueryManager();
105             Creator creator = new Creator(blm);
106             Finder finder = new Finder(bqm, uddiversion);
107             
108             Collection<Organization> orgs = new ArrayList<Organization>();
109             Organization organization = creator.createOrganization(this.getClass().getName());
110 //          Add a Service
111             Service service = creator.createService(this.getClass().getName());
112             ServiceBinding serviceBinding = creator.createServiceBinding();
113             service.addServiceBinding(serviceBinding);
114             organization.addService(service);
115             //Add a classification
116             ClassificationScheme cs = finder.findClassificationSchemeByName(this.getClass().getName());
117             Classification classification = creator.createClassification(cs);
118             organization.addClassification(classification);
119             
120             orgs.add(organization);
121 
122             //Now save the Organization along with a Service, ServiceBinding and Classification
123             BulkResponse br = blm.saveOrganizations(orgs);
124             if (br.getStatus() == JAXRResponse.STATUS_SUCCESS)
125             {
126                 System.out.println("Organization Saved");
127                 Collection coll = br.getCollection();
128                 Iterator iter = coll.iterator();
129                 while (iter.hasNext())
130                 {
131                     Key key = (Key) iter.next();
132                     System.out.println("Saved Key=" + key.getId());
133                 }//end while
134             } else
135             {
136                 System.err.println("JAXRExceptions " +
137                         "occurred during save:");
138                 Collection exceptions = br.getExceptions();
139                 Iterator iter = exceptions.iterator();
140                 while (iter.hasNext())
141                 {
142                     Exception e = (Exception) iter.next();
143                     System.err.println(e.toString());
144                 }
145             }
146             
147         } catch (JAXRException e) {
148             e.printStackTrace();
149 			assertTrue(false);
150         }
151         assertNull(response);
152     }
153     
154     @Test
155     public void queryOrganization()
156     {
157         login();
158         try
159         {
160             // Get registry service and business query manager
161             RegistryService rs = connection.getRegistryService();
162             bqm = rs.getBusinessQueryManager();
163             System.out.println("We have the Business Query Manager");
164             Printer printer = new Printer();
165             Finder finder = new Finder(bqm, uddiversion);
166 
167             Collection orgs = finder.findOrganizationsByName(this.getClass().getName());
168             if (orgs == null) {
169                 fail("Only Expecting 1 Organization");
170             } else {
171                 assertEquals(1,orgs.size());
172                 // then step through them
173                 for (Iterator orgIter = orgs.iterator(); orgIter.hasNext();)
174                 {
175                     Organization org = (Organization) orgIter.next();
176                     System.out.println("Org name: " + printer.getName(org));
177                     System.out.println("Org description: " + printer.getDescription(org));
178                     System.out.println("Org key id: " + printer.getKey(org));
179 
180                     printer.printUser(org);
181                     printer.printServices(org);
182                     printer.printClassifications(org);
183                 }
184             }//end else
185         } catch (JAXRException e) {
186             e.printStackTrace();
187             fail(e.getMessage());
188         } 
189     }
190     
191     @Test
192     public void deleteOrganization()
193     {
194         login();
195         try
196         {
197             RegistryService rs = connection.getRegistryService();
198             blm = rs.getBusinessLifeCycleManager();
199     //      Get registry service and business query manager
200             bqm = rs.getBusinessQueryManager();
201             System.out.println("We have the Business Query Manager");
202             Finder finder = new Finder(bqm, uddiversion);
203             Remover remover = new Remover(blm);
204             Collection orgs = finder.findOrganizationsByName(this.getClass().getName());
205             for (Iterator orgIter = orgs.iterator(); orgIter.hasNext();)
206             {
207                 Organization org = (Organization) orgIter.next();
208                 remover.removeOrganization(org);
209             }
210             
211         } catch (Exception e) {
212             e.printStackTrace();
213             fail(e.getMessage());
214         }
215     }
216     
217     @Test
218     public void deleteClassificationScheme()
219     {
220         login();
221         try {
222             RegistryService rs = connection.getRegistryService();
223             bqm = rs.getBusinessQueryManager();
224             blm = rs.getBusinessLifeCycleManager();
225             System.out.println("We have the Business Query Manager");
226             Finder finder = new Finder(bqm, uddiversion);
227             Remover remover = new Remover(blm);
228             Collection schemes = finder.findClassificationSchemesByName(this.getClass().getName());
229             for (Iterator iter = schemes.iterator(); iter.hasNext();)
230             {
231                 ClassificationScheme scheme = (ClassificationScheme) iter.next();
232                 remover.removeClassificationScheme(scheme);
233             }
234             
235         } catch (Exception e) {
236             e.printStackTrace();
237             fail(e.getMessage());
238         }
239     }
240     
241     public static junit.framework.Test suite() {
242         return new JUnit4TestAdapter(JAXR010OrganizationTest.class);
243     }
244 }