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.query.infomodel;
023:
024: import org.jboss.test.jaxr.scout.JaxrBaseTestCase;
025: import org.jboss.test.jaxr.scout.util.ScoutUtil;
026:
027: import javax.xml.registry.BulkResponse;
028: import javax.xml.registry.BusinessLifeCycleManager;
029: import javax.xml.registry.BusinessQueryManager;
030: import javax.xml.registry.Connection;
031: import javax.xml.registry.JAXRException;
032: import javax.xml.registry.LifeCycleManager;
033: import javax.xml.registry.RegistryService;
034: import javax.xml.registry.infomodel.Association;
035: import javax.xml.registry.infomodel.Concept;
036: import javax.xml.registry.infomodel.Key;
037: import javax.xml.registry.infomodel.Organization;
038: import java.util.ArrayList;
039: import java.util.Collection;
040: import java.util.Iterator;
041:
042: /**
043: * Tests FindAssociation using JAXR API
044: * @author <mailto:Anil.Saldhana@jboss.org>Anil Saldhana
045: * @since Mar 9, 2005
046: */
047: public class JaxrFindAssociationsTestCase extends JaxrBaseTestCase {
048: public void testFindAssociations() throws JAXRException {
049:
050: String orgSource = "Source Organization";
051: String type = "Implements";
052:
053: Key savekey = null;
054: Key assockey = null;
055:
056: BusinessQueryManager bqm2 = null;
057: BusinessLifeCycleManager blm2 = null;
058: Collection associationKeys = null;
059: Collection sourceKeys = null;
060: Collection targetKeys = null;
061: String targetId = null;
062: String sourceId = null;
063: String orgTarget = "Target Organization";
064:
065: try {
066: login();
067: getJAXREssentials();
068: // Authenticate second user
069: Connection con2 = loginSecondUser();
070: RegistryService rs2 = con2.getRegistryService();
071: blm2 = rs2.getBusinessLifeCycleManager();
072: bqm2 = rs2.getBusinessQueryManager();
073:
074: Organization source = blm.createOrganization(blm
075: .createInternationalString(orgSource));
076: Organization target = blm2.createOrganization(blm
077: .createInternationalString(orgTarget));
078:
079: // publish the source organization
080: Collection orgs = new ArrayList();
081: orgs.add(source);
082: br = blm.saveOrganizations(orgs);
083: if (br.getExceptions() != null) {
084: fail(" Source:Save Orgs failed");
085: }
086:
087: sourceKeys = br.getCollection();
088: Iterator iter = sourceKeys.iterator();
089: while (iter.hasNext()) {
090: savekey = (Key) iter.next();
091: }
092: sourceId = savekey.getId();
093: String objectType = LifeCycleManager.ORGANIZATION;
094: Organization pubSource = (Organization) bqm
095: .getRegistryObject(sourceId, objectType);
096: assertNotNull(pubSource.getName().getValue());
097:
098: // publish the target
099: orgs.clear();
100: orgs.add(target);
101: br = blm2.saveOrganizations(orgs);
102: if (br.getExceptions() != null) {
103: fail(" Target:Save Orgs failed");
104: }
105: targetKeys = br.getCollection();
106: iter = targetKeys.iterator();
107: while (iter.hasNext()) {
108: savekey = (Key) iter.next();
109: }
110: targetId = savekey.getId();
111: Organization pubTarget = (Organization) bqm2
112: .getRegistryObject(targetId, objectType);
113:
114: Concept associationType = getAssociationConcept(type);
115: if (associationType == null)
116: fail(" getAssociationConcept returned null associationType");
117:
118: Association a = blm.createAssociation(pubTarget,
119: associationType);
120: a.setSourceObject(pubSource);
121:
122: blm.confirmAssociation(a); //First user
123: blm2.confirmAssociation(a); //Second user
124:
125: // publish Association
126: Collection associations = new ArrayList();
127: associations.add(a);
128: // Second user saves the association.
129: br = blm2.saveAssociations(associations, false);
130:
131: if (br.getExceptions() != null) {
132: fail("Error:Save Associations failed \n");
133: }
134: BulkResponse targetAssoc = bqm.findCallerAssociations(null,
135: new Boolean(true), new Boolean(true), null);
136:
137: if (targetAssoc.getExceptions() == null) {
138: Collection targetCol = targetAssoc.getCollection();
139: if (targetCol.size() > 0) {
140: iter = targetCol.iterator();
141: while (iter.hasNext()) {
142: Association a1 = (Association) iter.next();
143: Organization o = (Organization) a1
144: .getSourceObject();
145: o = (Organization) a1.getTargetObject();
146: Concept atype = a1.getAssociationType();
147: assertNotNull(
148: "Concept Type stored in Association",
149: atype);
150: }
151: }
152: }
153:
154: br = null;
155: Collection associationTypes = new ArrayList();
156: associationTypes.add(type);
157: br = bqm.findAssociations(null, sourceId, targetId, null);
158: if (br.getExceptions() != null) {
159: fail("Error: findAssociations failed ");
160: }
161: associations = null;
162: associations = br.getCollection();
163: if (associations.size() > 0) {
164: iter = associations.iterator();
165: while (iter.hasNext()) {
166: Association a1 = (Association) iter.next();
167: assockey = a1.getKey();
168: ScoutUtil.validateAssociation(a1, orgSource);
169: }
170: }
171: } catch (Exception e) {
172: e.printStackTrace();
173: fail(" Test failed ");
174: } finally {
175: //Clean Up
176: try {
177: if (assockey != null) {
178: associationKeys = new ArrayList();
179: associationKeys.add(assockey);
180: blm.deleteAssociations(associationKeys);
181: }
182: blm2.deleteOrganizations(targetKeys);
183: blm.deleteOrganizations(sourceKeys);
184: } catch (JAXRException ex) {
185: ex.printStackTrace();
186: fail("Error: Cleanup failed");
187: }
188: }
189:
190: } // end of method
191:
192: }
|