This project has retired. For details please refer to its Attic page.
JAXR050ServiceBindingTest xref
View Javadoc
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.fail;
20  
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.Iterator;
24  
25  import javax.xml.registry.BulkResponse;
26  import javax.xml.registry.BusinessLifeCycleManager;
27  import javax.xml.registry.FindQualifier;
28  import javax.xml.registry.JAXRException;
29  import javax.xml.registry.JAXRResponse;
30  import javax.xml.registry.RegistryService;
31  import javax.xml.registry.infomodel.ClassificationScheme;
32  import javax.xml.registry.infomodel.Concept;
33  import javax.xml.registry.infomodel.ExternalLink;
34  import javax.xml.registry.infomodel.InternationalString;
35  import javax.xml.registry.infomodel.Key;
36  import javax.xml.registry.infomodel.Organization;
37  import javax.xml.registry.infomodel.Service;
38  import javax.xml.registry.infomodel.ServiceBinding;
39  import javax.xml.registry.infomodel.SpecificationLink;
40  
41  import junit.framework.JUnit4TestAdapter;
42  
43  import org.apache.ws.scout.BaseTestCase;
44  import org.apache.ws.scout.Finder;
45  import org.apache.ws.scout.Remover;
46  import org.junit.After;
47  import static org.junit.Assert.assertEquals;
48  import static org.junit.Assert.fail;
49  import org.junit.Before;
50  import org.junit.Test;
51  
52  /**
53  Tests Publish, Delete (and indirectly, find) for service bindings.
54   * 
55   * You can comment out the deletion portion and use 
56   * Open source UDDI Browser  <http://www.uddibrowser.org>
57   * to check your intermediate results
58   *
59   * Based on query/publish tests written by 
60   * <a href="mailto:anil@apache.org">Anil Saldhana</a>.
61   *
62   * @author <a href="mailto:dbhole@redhat.com">Deepak Bhole</a>
63   * @author <a href="mailto:anil@apache.org">Anil Saldhana</a>
64   *
65   * @since Sep 27, 2005
66   */
67  public class JAXR050ServiceBindingTest extends BaseTestCase
68  {
69      
70      String serviceBindingName = "Apache JAXR Service Binding -- APACHE SCOUT TEST";
71      String serviceName = "Apache JAXR Service -- APACHE SCOUT TEST";
72  	String tempOrgName = "Apache JAXR Service Org -- APACHE SCOUT TEST";
73  
74      @Before
75      public void setUp() {
76          super.setUp();
77          login();
78          try {
79              RegistryService rs = connection.getRegistryService();
80              bqm = rs.getBusinessQueryManager();
81              blm = rs.getBusinessLifeCycleManager();
82              ClassificationScheme cScheme = blm.createClassificationScheme("org.jboss.soa.esb.:testcategory", "JBossESB Classification Scheme");
83              ArrayList<ClassificationScheme> cSchemes = new ArrayList<ClassificationScheme>();
84              cSchemes.add(cScheme);
85              BulkResponse br = blm.saveClassificationSchemes(cSchemes);
86              assertEquals(JAXRResponse.STATUS_SUCCESS, br.getStatus());
87          } catch (Exception je) {
88              je.printStackTrace();
89              fail(je.getMessage());
90          }
91      }
92  
93      @After
94      public void tearDown() {
95          super.tearDown();
96          login();
97          try {
98              RegistryService rs = connection.getRegistryService();
99              bqm = rs.getBusinessQueryManager();
100             blm = rs.getBusinessLifeCycleManager();
101             Collection<String> findQualifiers = new ArrayList<String>();
102             findQualifiers.add(FindQualifier.AND_ALL_KEYS);
103             //findQualifiers.add(FindQualifier.SORT_BY_NAME_DESC);
104             ClassificationScheme cScheme = bqm.findClassificationSchemeByName(findQualifiers, "org.jboss.soa.esb.:testcategory");
105             Removerver.html#Remover">Remover remover = new Remover(blm);
106             remover.removeClassificationScheme(cScheme);
107         } catch (Exception je) {
108             je.printStackTrace();
109             fail(je.getMessage());
110         }
111     }
112 
113 	/**
114 	 * Tests publishing and deleting of service bindings.
115 	 * 
116 	 * Do not break this into testPublish(), testDelete(), etc. Order is
117 	 * important, and not all jvms can guarantee order since the JUnit framework
118 	 * uses getMethods() to gather test methods, and getMethods() does not
119 	 * guarantee order.
120 	 */
121     @Test
122     public void testPublishFindAndDeleteServiceBinding()
123     {
124         login();
125         try
126         {
127             RegistryService rs = connection.getRegistryService();
128             blm = rs.getBusinessLifeCycleManager();
129             bqm = rs.getBusinessQueryManager();
130             Findernder.html#Finder">Finder finder = new Finder(bqm, uddiversion);
131 
132             System.out.println("\nCreating temporary organization...\n");
133             Organization tmpOrg = createTempOrg();
134             Key tmpOrgKey = tmpOrg.getKey();
135             
136             System.out.println("\nCreating service...\n");
137             Service tmpSvc = createTempService(tmpOrg);
138             Key tmpSvcKey = tmpSvc.getKey();
139             
140             System.out.println("\nCreating service binding...\n");
141             Key sbKey = createServiceBinding(tmpSvc);
142                        
143             
144             SpecificationLink specLink = blm.createSpecificationLink();            
145             /*Concept concept = null;
146             if ("3.0".equals(uddiversion)) {
147             	concept = (Concept)bqm.getRegistryObject("uddi:uddi.org:findqualifier:orlikekeys", BusinessLifeCycleManager.CONCEPT);
148             } else {
149             	concept = (Concept)bqm.getRegistryObject("uuid:AD61DE98-4DB8-31B2-A299-A2373DC97212",BusinessLifeCycleManager.CONCEPT);
150             }
151             specLink.setSpecificationObject(concept);*/
152             
153             //find serviceBinding
154             Collection<ServiceBinding> serviceBindings2 = finder.findServiceBindings(tmpSvcKey, specLink);
155             @SuppressWarnings("unused")
156 			ServiceBinding serviceBinding2 = serviceBindings2.iterator().next();
157             
158             // All created ... now try to delete.
159             deleteServiceBinding(sbKey);
160             deleteTempService(tmpSvcKey);
161             deleteTempOrg(tmpOrgKey);
162             // No find service binding.. search by name is not currently supported. 
163             
164         } catch (JAXRException e)
165         {
166             e.printStackTrace();
167             fail(e.getMessage());
168         }
169     }
170 
171 
172     private Key createServiceBinding(Service tmpSvc) throws JAXRException {
173     	Key key = null;
174         ServiceBinding serviceBinding = blm.createServiceBinding();
175         serviceBinding.setName(getIString(serviceBindingName));
176         serviceBinding.setDescription(getIString("UDDI service binding"));
177         tmpSvc.addServiceBinding(serviceBinding);
178         
179         SpecificationLink specLink = blm.createSpecificationLink();
180         ExternalLink externalLink = blm.createExternalLink("http://localhost:8080/jmx-console", "Scout test");
181         Collection<ExternalLink> externalLinks = new ArrayList<ExternalLink>();
182         externalLinks.add(externalLink);
183         specLink.setExternalLinks(externalLinks);
184         
185         RegistryService rs = connection.getRegistryService();
186         bqm = rs.getBusinessQueryManager();
187         Concept concept = null;
188         if ("3.0".equals(uddiversion)) {        
189         	concept = (Concept)bqm.getRegistryObject("uddi:uddi.org:findqualifier:orlikekeys", BusinessLifeCycleManager.CONCEPT);
190         } else {
191         	concept = (Concept)bqm.getRegistryObject("uuid:AD61DE98-4DB8-31B2-A299-A2373DC97212",BusinessLifeCycleManager.CONCEPT);
192         }
193         
194         specLink.setSpecificationObject(concept);
195         
196         serviceBinding.addSpecificationLink(specLink);
197         ArrayList<ServiceBinding> serviceBindings = new ArrayList<ServiceBinding>();
198         serviceBindings.add(serviceBinding);
199 
200         BulkResponse br = blm.saveServiceBindings(serviceBindings);
201         if (br.getStatus() == JAXRResponse.STATUS_SUCCESS)
202         {
203             System.out.println("Service Binding Saved");
204             key = (Key) br.getCollection().iterator().next();
205             System.out.println("Saved Key=" + key.getId());
206         } else
207         {
208             System.err.println("JAXRExceptions " +
209                     "occurred during save:");
210             Collection exceptions = br.getExceptions();
211             Iterator iter = exceptions.iterator();
212             while (iter.hasNext())
213             {
214                 Exception e = (Exception) iter.next();
215                 System.err.println(e.toString());
216                 fail(e.toString());
217             }
218         }
219         
220         return key;
221     }
222 
223     private void deleteServiceBinding(Key key) throws JAXRException {
224 
225     	String id = key.getId();
226 
227     	System.out.println("\nDeleting service binding with id " + id + "\n");
228 
229     	Collection<Key> keys = new ArrayList<Key>();
230     	keys.add(key);
231     	BulkResponse response = blm.deleteServiceBindings(keys);
232 
233     	Collection exceptions = response.getExceptions();
234     	if (exceptions == null) {
235     		Collection retKeys = response.getCollection();
236     		Iterator keyIter = retKeys.iterator();
237     		javax.xml.registry.infomodel.Key orgKey = null;
238     		if (keyIter.hasNext()) {
239     			orgKey = 
240     				(javax.xml.registry.infomodel.Key) keyIter.next();
241     			id = orgKey.getId();
242     			System.out.println("Service binding with ID=" + id + " was deleted");
243     		}
244     	}
245     }
246 
247     private Service createTempService(Organization tmpOrg) throws JAXRException {
248     	
249         Service service = blm.createService(getIString(serviceName));
250         service.setDescription(getIString("Services in UDDI Registry"));
251         service.setProvidingOrganization(tmpOrg);
252 
253         ArrayList<Service> services = new ArrayList<Service>();
254         services.add(service);
255 
256         BulkResponse br = blm.saveServices(services);
257         if (br.getStatus() == JAXRResponse.STATUS_SUCCESS)
258         {
259             System.out.println("Service Saved");
260             Key key = (Key) br.getCollection().iterator().next();
261             System.out.println("Saved Key=" + key.getId());
262             service.setKey(key);
263         } else
264         {
265             System.err.println("JAXRExceptions " +
266                     "occurred during save:");
267             Collection exceptions = br.getExceptions();
268             Iterator iter = exceptions.iterator();
269             while (iter.hasNext())
270             {
271                 Exception e = (Exception) iter.next();
272                 System.err.println(e.toString());
273             }
274         }
275         
276         return service;
277     }
278 
279     private void deleteTempService(Key key) throws JAXRException {
280 
281     	String id = key.getId();
282 
283     	System.out.println("\nDeleting service with id " + id + "\n");
284 
285     	Collection<Key> keys = new ArrayList<Key>();
286     	keys.add(key);
287     	BulkResponse response = blm.deleteServices(keys);
288 
289     	Collection exceptions = response.getExceptions();
290     	if (exceptions == null) {
291     		Collection retKeys = response.getCollection();
292     		Iterator keyIter = retKeys.iterator();
293     		javax.xml.registry.infomodel.Key orgKey = null;
294     		if (keyIter.hasNext()) {
295     			orgKey = 
296     				(javax.xml.registry.infomodel.Key) keyIter.next();
297     			id = orgKey.getId();
298     			System.out.println("Service with ID=" + id + " was deleted");
299     		}
300     	}
301     }
302     
303     private Organization createTempOrg() throws JAXRException {
304 
305         Key orgKey = null;
306         Organization org = blm.createOrganization(getIString(tempOrgName));
307         org.setDescription(getIString("Temporary organization to test saveService()"));
308 
309         Collection<Organization> orgs = new ArrayList<Organization>();
310         orgs.add(org);
311         BulkResponse br = blm.saveOrganizations(orgs);
312         
313         if (br.getStatus() == JAXRResponse.STATUS_SUCCESS)
314         {
315         	orgKey = (Key) br.getCollection().iterator().next();
316             System.out.println("Temporary Organization Created with id=" + orgKey.getId());
317             org.setKey(orgKey);
318         }  else
319         {
320             System.err.println("JAXRExceptions " +
321                     "occurred during creation of temporary organization:");
322             
323             Iterator iter = br.getCollection().iterator();
324             
325             while (iter.hasNext()) {
326             	Exception e = (Exception) iter.next();
327             	System.err.println(e.toString());
328             }
329             
330             fail();
331         }
332         
333         return org;
334     }
335     
336     private void deleteTempOrg(Key key) throws JAXRException {
337 
338     	if (key == null) {
339     		return;
340     	}
341     	
342     	String id = key.getId();
343 
344     	System.out.println("\nDeleting temporary organization with id " + id + "\n");
345 
346     	Collection<Key> keys = new ArrayList<Key>();
347     	keys.add(key);
348     	BulkResponse response = blm.deleteOrganizations(keys);
349 
350     	Collection exceptions = response.getExceptions();
351     	if (exceptions == null) {
352     		Collection retKeys = response.getCollection();
353     		Iterator keyIter = retKeys.iterator();
354     		Key orgKey = null;
355     		if (keyIter.hasNext()) {
356     			orgKey = 
357     				(javax.xml.registry.infomodel.Key) keyIter.next();
358     			id = orgKey.getId();
359     			System.out.println("Organization with ID=" + id + " was deleted");
360     		}
361     	}
362     }
363     
364     private InternationalString getIString(String str)
365     throws JAXRException
366     {
367         return blm.createInternationalString(str);
368     }
369     
370     public static junit.framework.Test suite() {
371         return new JUnit4TestAdapter(JAXR050ServiceBindingTest.class);
372     }
373 }