1   /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2006, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */
22  package org.apache.ws.scout.registry.qa;
23  
24  import static org.junit.Assert.assertEquals;
25  import static org.junit.Assert.assertTrue;
26  import static org.junit.Assert.fail;
27  
28  import java.util.ArrayList;
29  import java.util.Collection;
30  import java.util.Iterator;
31  
32  import javax.xml.registry.BulkResponse;
33  import javax.xml.registry.FindQualifier;
34  import javax.xml.registry.JAXRException;
35  import javax.xml.registry.JAXRResponse;
36  import javax.xml.registry.RegistryService;
37  import javax.xml.registry.infomodel.Classification;
38  import javax.xml.registry.infomodel.ClassificationScheme;
39  import javax.xml.registry.infomodel.Organization;
40  import javax.xml.registry.infomodel.Service;
41  import javax.xml.registry.infomodel.ServiceBinding;
42  import javax.xml.registry.infomodel.User;
43  
44  import junit.framework.JUnit4TestAdapter;
45  
46  import org.apache.ws.scout.BaseTestCase;
47  import org.apache.ws.scout.Creator;
48  import org.apache.ws.scout.Finder;
49  import org.apache.ws.scout.Remover;
50  import org.junit.After;
51  import org.junit.Before;
52  import org.junit.Test;
53  
54  /**
55   * Testing the registry.
56   * 
57   * @author kstam
58   *
59   */
60  public class JAXR060RegistryTest extends BaseTestCase
61  {
62      @Before
63      public void setUp() {
64          super.setUp();
65          login();
66          try
67          {
68              RegistryService rs = connection.getRegistryService();
69              bqm = rs.getBusinessQueryManager();
70              blm = rs.getBusinessLifeCycleManager();
71              ClassificationScheme cScheme = blm.createClassificationScheme("org.jboss.soa.esb.:testcategory", "JBossESB Classification Scheme");
72              ArrayList<ClassificationScheme> cSchemes = new ArrayList<ClassificationScheme>();
73              cSchemes.add(cScheme);
74              BulkResponse br = blm.saveClassificationSchemes(cSchemes);
75              assertEquals(JAXRResponse.STATUS_SUCCESS, br.getStatus());
76          } catch (JAXRException je) {
77              fail(je.getMessage());
78          }
79      }
80  
81      @After
82      public void tearDown() {
83          super.tearDown();
84          login();
85          try {
86              RegistryService rs = connection.getRegistryService();
87              bqm = rs.getBusinessQueryManager();
88              blm = rs.getBusinessLifeCycleManager();
89              Collection<String> findQualifiers = new ArrayList<String>();
90              findQualifiers.add(FindQualifier.AND_ALL_KEYS);
91              findQualifiers.add(FindQualifier.SORT_BY_NAME_DESC);
92              ClassificationScheme cScheme = bqm.findClassificationSchemeByName(findQualifiers, "org.jboss.soa.esb.:testcategory");
93              Remover remover = new Remover(blm);
94              remover.removeClassificationScheme(cScheme);
95          } catch (JAXRException je) {
96              fail(je.getMessage());
97          }
98      }
99     
100 	/**
101 	 * Tests the successful creation of the RED HAT/JBossESB Organization.
102 	 */
103 	@SuppressWarnings("unchecked")
104     @Test
105 	public void publishCheckAndDelete() 
106 	{
107         login();
108         try
109         {
110             RegistryService rs = connection.getRegistryService();
111             blm = rs.getBusinessLifeCycleManager();
112             Creator creator = new Creator(blm);
113             
114             Collection<Organization> organizations = new ArrayList<Organization>();
115             Organization organization = creator.createOrganization("Red Hat/JBossESB");
116             organizations.add(organization);
117             BulkResponse br = blm.saveOrganizations(organizations);
118 			assertEquals(BulkResponse.STATUS_SUCCESS, br.getStatus());
119 		} catch (JAXRException je) {
120 			je.printStackTrace();
121 			assertTrue(false);
122 		}
123 	    //find organization
124         try
125         {
126             RegistryService rs = connection.getRegistryService();
127             bqm = rs.getBusinessQueryManager();
128             Finder finder = new Finder(bqm, uddiversion);
129 			Collection<Organization> orgs = finder.findOrganizationsByName("Red Hat/JBossESB");
130 			Organization org = orgs.iterator().next();
131 			assertEquals("Red Hat/JBossESB", org.getName().getValue());
132 		} catch (JAXRException je) {
133 			fail(je.getMessage());
134 		}
135 		try {
136             RegistryService rs = connection.getRegistryService();
137             bqm = rs.getBusinessQueryManager();
138             Finder finder = new Finder(bqm, uddiversion);
139             Collection<Organization> orgs = finder.findOrganizationsByName("Not Existing Org");
140 			assertEquals(0, orgs.size());
141 		} catch (JAXRException je) {
142 			fail(je.getMessage());
143 		}
144         //Publish a service
145 		try {
146             RegistryService rs = connection.getRegistryService();
147             bqm = rs.getBusinessQueryManager();
148             Finder finder = new Finder(bqm, uddiversion);
149             Collection<Organization> orgs = finder.findOrganizationsByName("Red Hat/JBossESB");
150             Organization organization = orgs.iterator().next();
151             
152             blm = rs.getBusinessLifeCycleManager();
153             //Adding the category as prefix for the name
154             Service service = blm.createService(blm.createInternationalString("Registry Test ServiceName"));
155             service.setDescription(blm.createInternationalString("Registry Test Service Description"));
156             Collection<String> findQualifiers = new ArrayList<String>();
157             findQualifiers.add(FindQualifier.AND_ALL_KEYS);
158             findQualifiers.add(FindQualifier.SORT_BY_NAME_DESC);
159             ClassificationScheme cScheme = bqm.findClassificationSchemeByName(findQualifiers, "org.jboss.soa.esb.:testcategory");
160             Classification classification = blm.createClassification(cScheme, "category", "registry");
161             service.addClassification(classification);
162             organization.addService(service);
163             Collection<Service> services = new ArrayList<Service>();
164             services.add(service);
165             BulkResponse br = blm.saveServices(services);
166             assertEquals(BulkResponse.STATUS_SUCCESS, br.getStatus());
167 		} catch (JAXRException je) {
168 			fail(je.getMessage());
169 		}
170 	    //find Service
171         try
172         {
173             RegistryService rs = connection.getRegistryService();
174             bqm = rs.getBusinessQueryManager();
175             blm = rs.getBusinessLifeCycleManager();
176             Finder finder = new Finder(bqm, uddiversion);
177             //Find the service
178             Service service = finder.findService("registry","Registry Test ServiceName", blm);
179             assertEquals("Registry Test ServiceName", service.getName().getValue());
180         } catch (JAXRException je) {
181             fail(je.getMessage());
182         }
183         //publish serviceBinding
184         try {
185             RegistryService rs = connection.getRegistryService();
186             bqm = rs.getBusinessQueryManager();
187             blm = rs.getBusinessLifeCycleManager();
188             Finder finder = new Finder(bqm, uddiversion);
189             //Find the service
190             Service service = finder.findService("registry","Registry Test ServiceName", blm);
191             
192             ServiceBinding serviceBinding = blm.createServiceBinding();
193             serviceBinding.setDescription(blm.createInternationalString("eprDescription"));
194             String xml = "<epr>epr uri</epr>";
195             serviceBinding.setAccessURI(xml);
196         
197             ArrayList<ServiceBinding> serviceBindings = new ArrayList<ServiceBinding>();
198             serviceBindings.add(serviceBinding);
199             service.addServiceBindings(serviceBindings);
200             Collection<String> findQualifiers = new ArrayList<String>();
201             findQualifiers.add(FindQualifier.AND_ALL_KEYS);
202             findQualifiers.add(FindQualifier.SORT_BY_NAME_DESC);
203             ClassificationScheme cScheme = bqm.findClassificationSchemeByName(findQualifiers, "org.jboss.soa.esb.:testcategory");
204             Classification classification = blm.createClassification(cScheme, "category", "registry");
205             service.addClassification(classification);
206            
207             BulkResponse br  = blm.saveServiceBindings(serviceBindings);
208             assertEquals(BulkResponse.STATUS_SUCCESS, br.getStatus());
209             BulkResponse br2  = blm.saveServiceBindings(serviceBindings); //Save one more
210             assertEquals(BulkResponse.STATUS_SUCCESS, br2.getStatus());
211            
212             //Delete one binding            
213             Collection<ServiceBinding> serviceBindings2 = finder.findServiceBindings(service.getKey(),classification);
214             if ((serviceBindings2 != null) && (serviceBindings2.iterator() != null) 
215             		&& (serviceBindings2.iterator().hasNext())) {
216             	ServiceBinding serviceBinding2 = serviceBindings2.iterator().next();
217             	Remover remover = new Remover(blm);
218             	remover.removeServiceBinding(serviceBinding2);
219             }
220 		} catch (JAXRException re) {
221 			fail(re.getMessage());
222 		}
223         //find all services for this organization
224         try
225         {
226             RegistryService rs = connection.getRegistryService();
227             bqm = rs.getBusinessQueryManager();
228             Finder finder = new Finder(bqm, uddiversion);
229 			Collection<Organization> orgs = finder.findOrganizationsByName("Red Hat/JBossESB");
230             Organization org = orgs.iterator().next();
231 			//Listing out the services and their Bindings
232 			System.out.println("-------------------------------------------------");
233             System.out.println("Organization name: " + org.getName().getValue());
234             System.out.println("Description: " + org.getDescription().getValue());
235             System.out.println("Key id: " + org.getKey().getId());
236 			User primaryContact = org.getPrimaryContact();
237             System.out.println("Primary Contact: " + primaryContact.getPersonName().getFullName());
238 			Collection services = org.getServices();
239 			for (Iterator serviceIter = services.iterator();serviceIter.hasNext();) {
240 				Service service = (Service) serviceIter.next();
241                 System.out.println("- Service Name: " + service.getName().getValue());
242                 System.out.println("  Service Key : " + service.getKey().getId());
243 				Collection serviceBindings = service.getServiceBindings();
244 				for (Iterator serviceBindingIter = serviceBindings.iterator();serviceBindingIter.hasNext();){
245 					ServiceBinding serviceBinding = (ServiceBinding) serviceBindingIter.next();
246                     System.out.println("  ServiceBinding Description: " + serviceBinding.getDescription().getValue());
247 					String xml = serviceBinding.getAccessURI();
248                     System.out.println("  ServiceBinding URI: " + xml);
249 					assertEquals("<epr>epr uri</epr>",xml);
250 				}
251 			}
252             System.out.println("-------------------------------------------------");
253 	    } catch (Exception je) {
254 	    	fail(je.getMessage());
255 		}
256         //delete the service
257         try
258         {
259             RegistryService rs = connection.getRegistryService();
260             bqm = rs.getBusinessQueryManager();
261             blm = rs.getBusinessLifeCycleManager();
262             Finder finder = new Finder(bqm, uddiversion);
263             //Find the service
264             Service service = finder.findService("registry","Registry Test ServiceName", blm);
265             Remover remover = new Remover(blm);
266             remover.removeService(service);
267         } catch (JAXRException je) {
268             fail(je.getMessage());
269         }
270         //delete the organization
271         try
272         {
273             RegistryService rs = connection.getRegistryService();
274             bqm = rs.getBusinessQueryManager();
275             blm = rs.getBusinessLifeCycleManager();
276             Finder finder = new Finder(bqm, uddiversion);
277             Collection<Organization> orgs = finder.findOrganizationsByName("Red Hat/JBossESB");
278             Organization org = orgs.iterator().next();
279             Remover remover = new Remover(blm);
280             remover.removeOrganization(org);
281         } catch (JAXRException je) {
282             fail(je.getMessage());
283         }
284     }
285     
286   
287 
288 	public static junit.framework.Test suite() {
289 		return new JUnit4TestAdapter(JAXR060RegistryTest.class);
290 	}
291 
292 }