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