package org.jboss.test.jaxr.common;
import org.jboss.logging.Logger;
import javax.xml.registry.*;
import javax.xml.registry.infomodel.*;
import java.net.*;
import java.util.*;
import junit.framework.TestCase;
public class JaxrTestCase extends TestCase{
private static final Logger log = Logger.getLogger(JaxrTestCase.class);
protected Connection connection = null;
protected RegistryService registryservice = null;
protected BusinessLifeCycleManager blcm = null;
protected BusinessQueryManager bqm = null;
protected String queryurl = "";
protected String publishurl = "";
protected String username = "";
protected String password = "";
protected Service service = null;
protected ServiceBinding binding = null;
public JaxrTestCase(){}
public JaxrTestCase(String name) {
super(name);
queryurl = System.getProperty("jaxr.queryurl");
publishurl = System.getProperty("jaxr.publishurl");
getLog().debug("queryurl::"+queryurl);
getLog().debug("publishurl::"+publishurl);
}
public void setUp() throws Exception{
if( connection == null ) connection = this.getConnection();
if( blcm == null ) blcm = this.getBusinessLifeCycleManager();
}
public void tearDown() throws Exception{
if( connection != null) this.releaseConnection();
}
public void testAuthentication()
throws Exception
{
getLog().debug("Enter testAuthentication");
if(connection == null ) connection = getConnection();
assertNotNull("Jaxr Connection is Not Null?",connection);
authenticate();
releaseConnection();
getLog().debug("Exit testAuthentication");
}
public void testJaxrConnection()
throws Exception
{
getLog().debug("Enter testJaxrConnection");
if(connection == null ) connection = getConnection();
assertNotNull("Jaxr Connection is Not Null?",connection);
releaseConnection();
getLog().debug("Exit testJaxrConnection");
}
public void testRegistryService()
throws Exception
{
getLog().debug("Enter testRegistryService");
if(registryservice == null) registryservice = getRegistryService();
assertNotNull("RegistryService is Not Null?",registryservice);
releaseConnection();
getLog().debug("Exit testRegistryService");
}
protected Connection getConnection()
throws Exception
{
if( connection == null ){
Properties props = new Properties();
props.setProperty("javax.xml.registry.queryManagerURL", queryurl);
props.setProperty("javax.xml.registry.lifeCycleManagerURL", publishurl);
assertNotNull("QueryUrl is Not Null?",queryurl);
assertNotNull("PublishUrl is Not Null?",publishurl);
ConnectionFactory factory = ConnectionFactory.newInstance();
factory.setProperties(props);
connection = factory.createConnection();
}
return connection;
}
protected RegistryService getRegistryService()
throws Exception
{
if(connection == null ) connection = getConnection();
assertNotNull("Jaxr Connection is Not Null?",connection);
return connection.getRegistryService();
}
protected BusinessLifeCycleManager getBusinessLifeCycleManager()
throws Exception
{
if(registryservice == null ) registryservice = getRegistryService();
assertNotNull("registryservice is Not Null?",registryservice);
return blcm = registryservice.getBusinessLifeCycleManager();
}
protected BusinessQueryManager getBusinessQueryManager()
throws Exception
{
if(registryservice == null ) registryservice = getRegistryService();
assertNotNull("registryservice is Not Null?",registryservice);
return bqm = registryservice.getBusinessQueryManager();
}
protected void releaseConnection()
throws Exception
{
if (connection != null) connection.close();
}
protected void authenticate()
throws Exception{
if(connection == null ) connection = getConnection();
assertNotNull("Jaxr Connection is Not Null?",connection);
assertNotNull("Username is Not Null?",username);
assertNotNull("Password is Not Null?",password);
PasswordAuthentication passwdAuth =
new PasswordAuthentication(username, password.toCharArray());
Set creds = new HashSet();
creds.add(passwdAuth);
connection.setCredentials(creds);
}
protected Logger getLog() { return log; }
protected String createOrganization()
throws Exception {
authenticate();
if( blcm == null ) blcm = this.getBusinessLifeCycleManager();
assertNotNull("BusinessLifeCycleManager is Not Null?",blcm);
Organization org = this.getOrganization("JBoss Inc", "Testing Jaxr Org");
User user = this.getPrimaryContact("Marc Fluery");
getLog().debug("Created User="+user.getPersonName().getFullName());
Collection emailadds = new ArrayList();
emailadds.add(this.getEmailAddress("marc@jboss.org"));
user.setEmailAddresses(emailadds);
Collection tels = new ArrayList();
tels.add(this.getTelephoneNumber("121-121-1111"));
user.setTelephoneNumbers(tels);
org.setPrimaryContact(user);
Collection classifications = new ArrayList();
classifications.add(this.getClassification("ntis-gov:naics:1997",
"Software Publishers",
"511210"));
org.addClassifications(classifications);
getLog().debug("Created Classification");
Collection services = new ArrayList();
if( service == null)
service = getService("Consulting Service","JBoss Inc provides Consulting");
getLog().debug("Created Service");
Collection servicebindings = new ArrayList();
if( binding == null)
binding = getServiceBinding("Consulting Arm",false,
"http://www.jboss.org" );
servicebindings.add(binding);
getLog().debug("Created ServiceBinding");
getLog().debug("Adding ServiceBindings to Service");
service.addServiceBindings(servicebindings);
getLog().debug("Added ServiceBindings to Service");
getLog().debug("Adding Services to Org");
services.add(service);
org.addServices(services);
getLog().debug("Added Services to Org");
Collection orgs = new ArrayList();
orgs.add(org);
getLog().debug("Going to add the Org into the registry");
BulkResponse response = blcm.saveOrganizations(orgs);
Collection exceptions = response.getExceptions();
assertNull("Org Saving to Registry Successful?", exceptions );
Collection keys = response.getCollection();
Iterator keyIter = keys.iterator();
String id = null;
if (keyIter.hasNext()) {
Key orgKey = (Key) keyIter.next();
id = orgKey.getId();
System.out.println("Organization key is " + id);
}
return id;
}
protected void deleteOrganization( Key orgkey)
throws Exception {
assertNotNull("Org Key is null?", orgkey);
authenticate();
if( blcm == null ) blcm = this.getBusinessLifeCycleManager();
Collection keys = new ArrayList();
keys.add(orgkey);
BulkResponse response = blcm.deleteOrganizations(keys);
Collection exceptions = response.getExceptions();
assertNull("Deleting Org with Key="+orgkey,exceptions);
}
protected void deleteOrganizations( Collection keys)
throws Exception {
assertNotNull("Org Key is null?", keys);
authenticate();
if( blcm == null ) blcm = this.getBusinessLifeCycleManager();
BulkResponse response = blcm.deleteOrganizations(keys);
Collection exceptions = response.getExceptions();
assertNull("Deleting Orgs ",exceptions);
}
protected InternationalString getIS(String str)
throws Exception {
if( blcm == null) blcm = this.getBusinessLifeCycleManager();
return blcm.createInternationalString(str);
}
protected Classification getClassification( String schemename,
String name, String value)
throws Exception {
if( bqm == null ) bqm = this.getBusinessQueryManager();
ClassificationScheme cscheme = bqm.findClassificationSchemeByName(null, schemename);
return blcm.createClassification( cscheme,name,value );
}
protected Organization getOrganization(String name, String desc)
throws Exception {
if( blcm == null) blcm = this.getBusinessLifeCycleManager();
Organization org = blcm.createOrganization(name);
org.setDescription(getIS(desc));
return org;
}
protected User getPrimaryContact(String name)
throws Exception {
if( blcm == null) blcm = this.getBusinessLifeCycleManager();
User primaryContact = blcm.createUser();
PersonName personname = blcm.createPersonName(name);
primaryContact.setPersonName(personname);
return primaryContact;
}
protected TelephoneNumber getTelephoneNumber(String telnumb )
throws Exception {
if( blcm == null) blcm = this.getBusinessLifeCycleManager();
TelephoneNumber tnum = blcm.createTelephoneNumber();
tnum.setNumber(telnumb);
return tnum;
}
protected EmailAddress getEmailAddress(String email )
throws Exception {
if( blcm == null) blcm = this.getBusinessLifeCycleManager();
EmailAddress emailaddress =
blcm.createEmailAddress(email);
return emailaddress;
}
protected Service getService(String servicename, String servicedesc)
throws Exception {
if( blcm == null) blcm = this.getBusinessLifeCycleManager();
if(service == null) {
service = blcm.createService(servicename);
service.setDescription(getIS(servicedesc));
}
return service;
}
protected ServiceBinding getServiceBinding(String descr, boolean validateurl,
String url)
throws Exception {
if( blcm == null) blcm = this.getBusinessLifeCycleManager();
if( binding == null ){
binding = blcm.createServiceBinding();
binding.setDescription(getIS(descr));
binding.setValidateURI(validateurl);
binding.setAccessURI(url);
}
return binding;
}
protected void clearOrganizations( String name)
throws Exception {
if(bqm == null ) bqm = this.getBusinessQueryManager();
Collection findQualifiers = new ArrayList();
findQualifiers.add(FindQualifier.CASE_SENSITIVE_MATCH);
Collection namePatterns = new ArrayList();
namePatterns.add("%" + name + "%");
BulkResponse response =
bqm.findOrganizations(findQualifiers, namePatterns, null,
null, null, null);
Collection orgs = response.getCollection();
System.out.println("There are "+orgs.size()+" orgs to delete");
Collection keys = new ArrayList();
Iterator iter = orgs.iterator();
while( iter.hasNext()){
Organization org = (Organization)iter.next();
Key key = org.getKey();
keys.add(key);
}
if( keys != null ) this.deleteOrganizations(keys);
}
}