001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.jaxr.scout;
023:
024: import junit.framework.TestCase;
025: import org.jboss.mx.util.ObjectNameFactory;
026: import org.jboss.test.JBossRMIAdaptorHelper;
027:
028: import javax.management.ObjectName;
029: import javax.naming.Context;
030: import javax.naming.InitialContext;
031: import javax.naming.NamingException;
032: import javax.xml.registry.BulkResponse;
033: import javax.xml.registry.BusinessLifeCycleManager;
034: import javax.xml.registry.BusinessQueryManager;
035: import javax.xml.registry.Connection;
036: import javax.xml.registry.ConnectionFactory;
037: import javax.xml.registry.FindQualifier;
038: import javax.xml.registry.JAXRException;
039: import javax.xml.registry.RegistryService;
040: import javax.xml.registry.infomodel.*;
041: import java.net.PasswordAuthentication;
042: import java.util.ArrayList;
043: import java.util.Collection;
044: import java.util.HashSet;
045: import java.util.Iterator;
046: import java.util.Locale;
047: import java.util.Properties;
048: import java.util.Set;
049:
050: /**
051: * Acts as the base class for Jaxr Test Cases
052: *
053: * @author <mailto:Anil.Saldhana@jboss.org>Anil Saldhana
054: * @since Dec 29, 2004
055: */
056: public class JaxrBaseTestCase extends TestCase {
057:
058: protected String userid = "jboss";
059: protected String passwd = "jboss";
060: protected BusinessLifeCycleManager blm = null;
061: protected RegistryService rs = null;
062: protected BusinessQueryManager bqm = null;
063: protected Connection connection = null;
064: protected BulkResponse br = null;
065: protected JBossRMIAdaptorHelper server = null;
066:
067: protected ConnectionFactory factory = null;
068:
069: protected static final ObjectName OBJECT_NAME = ObjectNameFactory
070: .create("jboss:service=juddi");
071:
072: //Debug ID
073: protected static String debugProp = System.getProperty(
074: "jaxr.debug", "true");
075:
076: /**
077: * Setup of the JUnit test
078: * We create the juddi tables on startup
079: *
080: * @throws Exception
081: */
082: protected void setUp() throws Exception {
083: //Change the createonstart setting for juddi service and restart it
084: server = new JBossRMIAdaptorHelper(this .getClientContext());
085: server.invokeOperation(OBJECT_NAME, "setCreateOnStart",
086: new Object[] { Boolean.TRUE },
087: new String[] { Boolean.TYPE.getName() });
088: server.invokeOperation(OBJECT_NAME, "stop", null, null);
089: server.invokeOperation(OBJECT_NAME, "start", null, null);
090:
091: //Ensure that the Jaxr Connection Factory class is setup
092: String factoryString = "javax.xml.registry.ConnectionFactoryClass";
093: String factoryClass = System.getProperty(factoryString);
094: if (factoryClass == null || factoryClass.length() == 0)
095: System
096: .setProperty(factoryString,
097: "org.apache.ws.scout.registry.ConnectionFactoryImpl");
098:
099: String queryurl = System.getProperty("jaxr.query.url",
100: "http://localhost:8080/juddi/inquiry");
101: String puburl = System.getProperty("jaxr.publish.url",
102: "http://localhost:8080/juddi/publish");
103:
104: Properties props = new Properties();
105: props.setProperty("javax.xml.registry.queryManagerURL",
106: queryurl);
107:
108: props.setProperty("javax.xml.registry.lifeCycleManagerURL",
109: puburl);
110:
111: String transportClass = System.getProperty(
112: "juddi.proxy.transportClass",
113: "org.jboss.jaxr.juddi.transport.SaajTransport");
114: System
115: .setProperty("juddi.proxy.transportClass",
116: transportClass);
117: try {
118: // Create the connection, passing it the configuration properties
119: factory = ConnectionFactory.newInstance();
120: factory.setProperties(props);
121: connection = factory.createConnection();
122: } catch (JAXRException e) {
123: fail("Setup failed" + e);
124: }
125: }
126:
127: /**
128: * Teardown of the junit test
129: * We discard all the tables created by the juddi service
130: *
131: * @throws Exception
132: */
133: protected void tearDown() throws Exception {
134: if (connection != null)
135: connection.close();
136: //stop the juddi service so that all the tables are dropped
137: server.invokeOperation(OBJECT_NAME, "setCreateOnStart",
138: new Object[] { Boolean.FALSE },
139: new String[] { Boolean.TYPE.getName() });
140: server.invokeOperation(OBJECT_NAME, "stop", null, null);
141: }
142:
143: public void testJaxrEssentials() {
144: assertNotNull(connection);
145: }
146:
147: /**
148: * Does authentication with the uddi registry
149: */
150: protected void login() {
151: PasswordAuthentication passwdAuth = new PasswordAuthentication(
152: userid, passwd.toCharArray());
153: Set creds = new HashSet();
154: creds.add(passwdAuth);
155:
156: try {
157: connection.setCredentials(creds);
158: } catch (JAXRException e) {
159: e.printStackTrace();
160: fail(e.getMessage());
161: }
162: }
163:
164: protected void getJAXREssentials() throws JAXRException {
165:
166: rs = connection.getRegistryService();
167: blm = rs.getBusinessLifeCycleManager();
168: bqm = rs.getBusinessQueryManager();
169: }
170:
171: public InternationalString getIString(String str)
172: throws JAXRException {
173: return blm.createInternationalString(str);
174: }
175:
176: /**
177: * Locale aware Search a business in the registry
178: *
179: * @param bizname
180: * @throws JAXRException
181: */
182: public void searchBusiness(String bizname) throws JAXRException {
183: try {
184: // Get registry service and business query manager
185: this .getJAXREssentials();
186:
187: // Define find qualifiers and name patterns
188: Collection findQualifiers = new ArrayList();
189: findQualifiers.add(FindQualifier.SORT_BY_NAME_ASC);
190: Collection namePatterns = new ArrayList();
191: String pattern = "%" + bizname + "%";
192: LocalizedString ls = blm.createLocalizedString(Locale
193: .getDefault(), pattern);
194: namePatterns.add(ls);
195:
196: // Find based upon qualifier type and values
197: BulkResponse response = bqm.findOrganizations(
198: findQualifiers, namePatterns, null, null, null,
199: null);
200:
201: // check how many organisation we have matched
202: Collection orgs = response.getCollection();
203: if (orgs == null) {
204: if ("true".equalsIgnoreCase(debugProp))
205: System.out.println(" -- Matched 0 orgs");
206:
207: } else {
208: if ("true".equalsIgnoreCase(debugProp))
209: System.out.println(" -- Matched " + orgs.size()
210: + " organizations -- ");
211:
212: // then step through them
213: for (Iterator orgIter = orgs.iterator(); orgIter
214: .hasNext();) {
215: Organization org = (Organization) orgIter.next();
216: if ("true".equalsIgnoreCase(debugProp)) {
217: System.out.println("Org name: " + getName(org));
218: System.out.println("Org description: "
219: + getDescription(org));
220: System.out
221: .println("Org key id: " + getKey(org));
222: }
223: checkUser(org);
224: checkServices(org);
225: }
226: }//end else
227: } catch (JAXRException e) {
228: e.printStackTrace();
229: fail(e.getMessage());
230: } finally {
231: connection.close();
232: }
233:
234: }
235:
236: protected RegistryService getRegistryService() throws JAXRException {
237: assertNotNull(connection);
238: return connection.getRegistryService();
239: }
240:
241: protected BusinessQueryManager getBusinessQueryManager()
242: throws JAXRException {
243: assertNotNull(connection);
244: if (rs == null)
245: rs = this .getRegistryService();
246: return rs.getBusinessQueryManager();
247: }
248:
249: protected BusinessLifeCycleManager getBusinessLifeCycleManager()
250: throws JAXRException {
251: assertNotNull(connection);
252: if (rs == null)
253: rs = this .getRegistryService();
254: return rs.getBusinessLifeCycleManager();
255: }
256:
257: private static void checkServices(Organization org)
258: throws JAXRException {
259: // Display service and binding information
260: Collection services = org.getServices();
261: for (Iterator svcIter = services.iterator(); svcIter.hasNext();) {
262: Service svc = (Service) svcIter.next();
263: if ("true".equalsIgnoreCase(debugProp)) {
264: System.out.println(" Service name: " + getName(svc));
265: System.out.println(" Service description: "
266: + getDescription(svc));
267: }
268: assertEquals("JBOSS JAXR Service", getName(svc));
269: assertEquals("Services of XML Registry",
270: getDescription(svc));
271:
272: Collection serviceBindings = svc.getServiceBindings();
273: for (Iterator sbIter = serviceBindings.iterator(); sbIter
274: .hasNext();) {
275: ServiceBinding sb = (ServiceBinding) sbIter.next();
276: if ("true".equalsIgnoreCase(debugProp)) {
277: System.out.println(" Binding Description: "
278: + getDescription(sb));
279: System.out.println(" Access URI: "
280: + sb.getAccessURI());
281: }
282: assertEquals("http://testjboss.org", sb.getAccessURI());
283: assertEquals("Test Service Binding", getDescription(sb));
284: }
285: }
286: }
287:
288: private static void checkUser(Organization org)
289: throws JAXRException {
290: // Display primary contact information
291: User pc = org.getPrimaryContact();
292: if (pc != null) {
293: PersonName pcName = pc.getPersonName();
294: System.out
295: .println(" Contact name: " + pcName.getFullName());
296: assertEquals("Anil S", pcName.getFullName());
297: Collection phNums = pc.getTelephoneNumbers(pc.getType());
298: for (Iterator phIter = phNums.iterator(); phIter.hasNext();) {
299: TelephoneNumber num = (TelephoneNumber) phIter.next();
300: System.out
301: .println(" Phone number: " + num.getNumber());
302: }
303: Collection eAddrs = pc.getEmailAddresses();
304: for (Iterator eaIter = eAddrs.iterator(); eaIter.hasNext();) {
305: System.out.println(" Email Address: "
306: + (EmailAddress) eaIter.next());
307: }
308: }
309: }
310:
311: private static String getName(RegistryObject ro)
312: throws JAXRException {
313: if (ro != null && ro.getName() != null) {
314: return ro.getName().getValue();
315: }
316: return "";
317: }
318:
319: private static String getDescription(RegistryObject ro)
320: throws JAXRException {
321: if (ro != null && ro.getDescription() != null) {
322: return ro.getDescription().getValue();
323: }
324: return "";
325: }
326:
327: private static String getKey(RegistryObject ro)
328: throws JAXRException {
329: if (ro != null && ro.getKey() != null) {
330: return ro.getKey().getId();
331: }
332: return "";
333: }
334:
335: /**
336: * Creates a Jaxr Organization with 1 or more services
337: *
338: * @return
339: * @throws JAXRException
340: */
341: protected Organization createOrganization(String orgname)
342: throws JAXRException {
343: Organization org = blm.createOrganization(getIString(orgname));
344: org.setDescription(getIString("JBoss Inc"));
345: Service service = blm
346: .createService(getIString("JBOSS JAXR Service"));
347: service.setDescription(getIString("Services of XML Registry"));
348: //Create serviceBinding
349: ServiceBinding serviceBinding = blm.createServiceBinding();
350: serviceBinding.setDescription(blm
351: .createInternationalString("Test Service Binding"));
352:
353: //Turn validation of URI off
354: serviceBinding.setValidateURI(false);
355: serviceBinding.setAccessURI("http://testjboss.org");
356:
357: // Add the serviceBinding to the service
358: service.addServiceBinding(serviceBinding);
359:
360: User user = blm.createUser();
361: org.setPrimaryContact(user);
362: PersonName personName = blm.createPersonName("Anil S");
363: TelephoneNumber telephoneNumber = blm.createTelephoneNumber();
364: telephoneNumber.setNumber("111-111-7777");
365: telephoneNumber.setType(null);
366: PostalAddress address = blm.createPostalAddress("111",
367: "My Drive", "BuckHead", "GA", "USA", "1111-111", "");
368: Collection postalAddresses = new ArrayList();
369: postalAddresses.add(address);
370: Collection emailAddresses = new ArrayList();
371: EmailAddress emailAddress = blm
372: .createEmailAddress("anil@apache.org");
373: emailAddresses.add(emailAddress);
374:
375: Collection numbers = new ArrayList();
376: numbers.add(telephoneNumber);
377: user.setPersonName(personName);
378: user.setPostalAddresses(postalAddresses);
379: user.setEmailAddresses(emailAddresses);
380: user.setTelephoneNumbers(numbers);
381:
382: ClassificationScheme cScheme = getClassificationScheme(
383: "ntis-gov:naics", "");
384: Key cKey = blm
385: .createKey("uuid:C0B9FE13-324F-413D-5A5B-2004DB8E5CC2");
386: cScheme.setKey(cKey);
387: Classification classification = blm.createClassification(
388: cScheme,
389: "Computer Systems Design and Related Services", "5415");
390: org.addClassification(classification);
391: ClassificationScheme cScheme1 = getClassificationScheme(
392: "D-U-N-S", "");
393: Key cKey1 = blm
394: .createKey("uuid:3367C81E-FF1F-4D5A-B202-3EB13AD02423");
395: cScheme1.setKey(cKey1);
396: ExternalIdentifier ei = blm.createExternalIdentifier(cScheme1,
397: "D-U-N-S number", "08-146-6849");
398: org.addExternalIdentifier(ei);
399: org.addService(service);
400: return org;
401: }
402:
403: /**
404: * Delete an Organization with a given key
405: *
406: * @param orgkey
407: * @throws Exception
408: */
409: protected void deleteOrganization(Key orgkey) throws Exception {
410: assertNotNull("Org Key is null?", orgkey);
411: if (blm == null)
412: blm = this .getBusinessLifeCycleManager();
413: Collection keys = new ArrayList();
414: keys.add(orgkey);
415:
416: BulkResponse response = blm.deleteOrganizations(keys);
417: Collection exceptions = response.getExceptions();
418: assertNull("Deleting Org with Key=" + orgkey, exceptions);
419: }
420:
421: private ClassificationScheme getClassificationScheme(String str1,
422: String str2) throws JAXRException {
423: ClassificationScheme cs = blm.createClassificationScheme(
424: getIString(str1), getIString(str2));
425: return cs;
426: }
427:
428: protected Connection loginSecondUser() {
429: Connection con = null;
430: try {
431: if (factory == null)
432: throw new IllegalStateException(
433: "ConnectionFactory is null");
434: con = factory.createConnection();
435: } catch (JAXRException e) {
436: e.printStackTrace();
437: }
438: PasswordAuthentication passwdAuth = new PasswordAuthentication(
439: "jbosscts", passwd.toCharArray());
440: Set creds = new HashSet();
441: creds.add(passwdAuth);
442:
443: try {
444: con.setCredentials(creds);
445: } catch (JAXRException e) {
446: e.printStackTrace();
447: fail(e.getMessage());
448: }
449: return con;
450: }
451:
452: protected Concept getAssociationConcept(String associationType) {
453: try {
454: BusinessQueryManager bqm = rs.getBusinessQueryManager();
455: ClassificationScheme associationTypes = bqm
456: .findClassificationSchemeByName(null,
457: "AssociationType");
458: Collection types = associationTypes.getChildrenConcepts();
459: Iterator iter = types.iterator();
460: Concept concept = null;
461: while (iter.hasNext()) {
462: concept = (Concept) iter.next();
463: if (concept.getName().getValue()
464: .equals(associationType)) {
465: return concept;
466: }
467: }
468: } catch (Exception e) {
469: e.printStackTrace();
470: return null;
471: }
472: return null;
473:
474: }// end of method
475:
476: protected InitialContext getClientContext() throws NamingException {
477: String hostname = System.getProperty("host.name", "localhost");
478: if (hostname == null)
479: throw new IllegalStateException(
480: "host.name system property not present");
481: Properties env = new Properties();
482: env.setProperty(Context.INITIAL_CONTEXT_FACTORY,
483: "org.jnp.interfaces.NamingContextFactory");
484: env.setProperty(Context.URL_PKG_PREFIXES,
485: "org.jboss.naming:org.jnp.interfaces");
486: env.setProperty(Context.PROVIDER_URL, "jnp://" + hostname
487: + ":1099");
488: return new InitialContext(env);
489: }
490:
491: }
|