001: /**
002: *
003: * Copyright 2004 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.ws.scout.registry.qa;
017:
018: import static org.junit.Assert.fail;
019: import static org.junit.Assert.assertEquals;
020:
021: import java.util.ArrayList;
022: import java.util.Collection;
023: import java.util.Iterator;
024:
025: import javax.xml.registry.BulkResponse;
026: import javax.xml.registry.BusinessQueryManager;
027: import javax.xml.registry.FindQualifier;
028: import javax.xml.registry.JAXRException;
029: import javax.xml.registry.JAXRResponse;
030: import javax.xml.registry.RegistryService;
031: import javax.xml.registry.infomodel.Association;
032: import javax.xml.registry.infomodel.Concept;
033: import javax.xml.registry.infomodel.Key;
034: import javax.xml.registry.infomodel.Organization;
035: import javax.xml.registry.infomodel.RegistryObject;
036:
037: import junit.framework.JUnit4TestAdapter;
038:
039: import org.apache.ws.scout.BaseTestCase;
040: import org.apache.ws.scout.Creator;
041: import org.junit.After;
042: import org.junit.Before;
043: import org.junit.Test;
044:
045: /**
046: * Tests Publish, Delete (and indirectly, find) for associations.
047: *
048: * You can comment out the deletion portion and use Open source UDDI Browser
049: * <http://www.uddibrowser.org> to check your intermediate results.
050: *
051: * Based on query/publish tests written by <a href="mailto:anil@apache.org">Anil
052: * Saldhana</a>.
053: *
054: * @author <a href="mailto:dbhole@redhat.com">Deepak Bhole</a>
055: * @author <a href="mailto:anil@apache.org">Anil Saldhana</a>
056: *
057: * @since Sep 27, 2005
058: */
059: public class JAXR030AssociationsTest extends BaseTestCase {
060:
061: private BusinessQueryManager bqm = null;
062:
063: String associationType = "AssociationType/RelatedTo";
064: private static String tempSrcOrgName = "Apache Source Org -- APACHE SCOUT TEST";
065: private static String tempTgtOrgName = "Apache Target Org -- APACHE SCOUT TEST";
066:
067: Organization sOrg, tOrg;
068:
069: @Before
070: public void setUp() {
071: super .setUp();
072: }
073:
074: @After
075: public void tearDown() {
076: super .tearDown();
077: }
078:
079: /**
080: * Tests publishing and deleting of associations.
081: *
082: * Do not break this into testPublish(), testDelete(), etc. Order is
083: * important, and not all jvms can guarantee order since the JUnit framework
084: * uses getMethods() to gather test methods, and getMethods() does not
085: * guarantee order.
086: */
087: @Test
088: public void testPublishFindAndDeleteAssociation() {
089: login();
090: try {
091: RegistryService rs = connection.getRegistryService();
092: bqm = rs.getBusinessQueryManager();
093: blm = rs.getBusinessLifeCycleManager();
094: Creator creator = new Creator(blm);
095:
096: System.out
097: .println("\nCreating temporary organizations...\n");
098: Organization org1 = creator
099: .createOrganization(tempSrcOrgName);
100: Organization org2 = creator
101: .createOrganization(tempTgtOrgName);
102: Collection<Organization> organizations = new ArrayList<Organization>();
103: organizations.add(org1);
104: organizations.add(org2);
105: blm.saveOrganizations(organizations);
106:
107: System.out
108: .println("\nSearching for newly created organizations...\n");
109: ArrayList<Organization> orgs = findTempOrgs();
110: sOrg = orgs.get(0);
111: tOrg = orgs.get(1);
112:
113: System.out.println("\nCreating association...\n");
114: createAssociation(sOrg, tOrg);
115:
116: // All created ... now try to delete.
117: findAndDeleteAssociation();
118:
119: } catch (JAXRException e) {
120: e.printStackTrace();
121: fail(e.getMessage());
122: } finally {
123:
124: }
125: }
126:
127: private void createAssociation(Organization sOrg, Organization tOrg)
128: throws JAXRException {
129:
130: Concept type = bqm.findConceptByPath(associationType);
131: Association association = blm.createAssociation(tOrg, type);
132: sOrg.addAssociation(association);
133:
134: ArrayList<Association> associations = new ArrayList<Association>();
135: associations.add(association);
136:
137: BulkResponse br = blm.saveAssociations(associations, true);
138: if (br.getStatus() == JAXRResponse.STATUS_SUCCESS) {
139: System.out.println("Association Saved");
140: Collection coll = br.getCollection();
141: Iterator iter = coll.iterator();
142: while (iter.hasNext()) {
143: System.out.println("Saved Key=" + iter.next());
144: }// end while
145: } else {
146: System.err.println("JAXRExceptions "
147: + "occurred during save:");
148: Collection exceptions = br.getExceptions();
149: Iterator iter = exceptions.iterator();
150: while (iter.hasNext()) {
151: Exception e = (Exception) iter.next();
152: System.err.println(e.toString());
153: }
154: deleteTempOrgs();
155: }
156: }
157:
158: private void findAndDeleteAssociation() throws JAXRException {
159:
160: String sOrgID = sOrg.getKey().getId();
161: String tOrgID = tOrg.getKey().getId();
162:
163: Collection<String> findQualifiers = new ArrayList<String>();
164: findQualifiers.add(FindQualifier.SORT_BY_NAME_ASC);
165:
166: Concept type = bqm.findConceptByPath(associationType);
167: ArrayList<Concept> conceptTypes = new ArrayList<Concept>(1);
168: conceptTypes.add(type);
169:
170: BulkResponse br = bqm.findAssociations(findQualifiers, sOrgID,
171: tOrgID, conceptTypes);
172: Collection associations = br.getCollection();
173:
174: if (associations == null) {
175: System.out.println("\n-- Matched 0 orgs");
176: fail("Expected 1 association");
177:
178: } else {
179: System.out.println("\n-- Matched " + associations.size()
180: + " associations --\n");
181: assertEquals(1, associations.size());
182:
183: // then step through them
184: for (Iterator conceptIter = associations.iterator(); conceptIter
185: .hasNext();) {
186: Association a = (Association) conceptIter.next();
187:
188: System.out.println("Id: " + a.getKey().getId());
189: System.out.println("Name: " + a.getName().getValue());
190:
191: // Print spacer between messages
192: System.out.println(" --- ");
193:
194: deleteAssociation(a.getKey());
195:
196: System.out
197: .println("\n ============================== \n");
198: }
199: }
200: }
201:
202: private void deleteAssociation(Key key) throws JAXRException {
203:
204: String id = key.getId();
205:
206: System.out.println("\nDeleting association with id " + id
207: + "\n");
208:
209: Collection<Key> keys = new ArrayList<Key>();
210: keys.add(key);
211: BulkResponse response = blm.deleteAssociations(keys);
212:
213: assertEquals(BulkResponse.STATUS_SUCCESS, response.getStatus());
214: Collection exceptions = response.getExceptions();
215: if (exceptions == null) {
216: Collection retKeys = response.getCollection();
217: Iterator keyIter = retKeys.iterator();
218: javax.xml.registry.infomodel.Key orgKey = null;
219: if (keyIter.hasNext()) {
220: orgKey = (javax.xml.registry.infomodel.Key) keyIter
221: .next();
222: id = orgKey.getId();
223: System.out.println("Association with ID=" + id
224: + " was deleted");
225: }
226: }
227: }
228:
229: private ArrayList<Organization> findTempOrgs() throws JAXRException {
230:
231: ArrayList<Organization> toReturn = new ArrayList<Organization>(
232: 2);
233: toReturn.add(null);
234: toReturn.add(null);
235:
236: // Define find qualifiers and name patterns
237: Collection<String> findQualifiers = new ArrayList<String>();
238: findQualifiers.add(FindQualifier.SORT_BY_NAME_ASC);
239: Collection<String> namePatterns = new ArrayList<String>();
240: namePatterns.add("%" + tempSrcOrgName + "%");
241: namePatterns.add("%" + tempTgtOrgName + "%");
242:
243: // Find based upon qualifier type and values
244: System.out.println("\n-- searching the registry --\n");
245: BulkResponse response = bqm.findOrganizations(findQualifiers,
246: namePatterns, null, null, null, null);
247:
248: // check how many organisation we have matched
249: Collection orgs = response.getCollection();
250: if (orgs == null) {
251: System.out.println("\n-- Matched 0 orgs");
252:
253: } else {
254: System.out.println("\n-- Matched " + orgs.size()
255: + " organisations --\n");
256:
257: // then step through them
258: for (Iterator orgIter = orgs.iterator(); orgIter.hasNext();) {
259: Organization org = (Organization) orgIter.next();
260:
261: System.out.println("Org name: " + getName(org));
262: System.out.println("Org description: "
263: + getDescription(org));
264: System.out.println("Org key id: " + getKey(org));
265:
266: if (getName(org).indexOf(tempSrcOrgName) > -1) {
267: toReturn.add(0, org);
268: } else {
269: toReturn.add(1, org);
270: }
271:
272: // Print spacer between organizations
273: System.out
274: .println("\n ============================== \n");
275: }
276: }// end else
277:
278: return toReturn;
279: }
280:
281: private void deleteTempOrgs() {
282:
283: try {
284:
285: Key sOrgKey = sOrg.getKey();
286: Key tOrgKey = tOrg.getKey();
287:
288: System.out
289: .println("\nDeleting temporary organizations with ids "
290: + sOrgKey + " and " + tOrgKey + "\n");
291:
292: Collection<Key> keys = new ArrayList<Key>();
293: keys.add(sOrgKey);
294: keys.add(tOrgKey);
295: BulkResponse response = blm.deleteOrganizations(keys);
296:
297: Collection exceptions = response.getExceptions();
298: if (exceptions == null) {
299: Collection retKeys = response.getCollection();
300: Iterator keyIter = retKeys.iterator();
301: Key orgKey = null;
302: while (keyIter.hasNext()) {
303: orgKey = (javax.xml.registry.infomodel.Key) keyIter
304: .next();
305: String id = orgKey.getId();
306: System.out.println("Organization with ID=" + id
307: + " was deleted");
308: }
309: }
310: } catch (JAXRException jaxre) {
311: jaxre.printStackTrace();
312: }
313: }
314:
315: private static String getName(RegistryObject ro)
316: throws JAXRException {
317: if (ro != null && ro.getName() != null) {
318: return ro.getName().getValue();
319: }
320: return "";
321: }
322:
323: private static String getDescription(RegistryObject ro)
324: throws JAXRException {
325: if (ro != null && ro.getDescription() != null) {
326: return ro.getDescription().getValue();
327: }
328: return "";
329: }
330:
331: private static String getKey(RegistryObject ro)
332: throws JAXRException {
333: if (ro != null && ro.getKey() != null) {
334: return ro.getKey().getId();
335: }
336: return "";
337: }
338:
339: public static junit.framework.Test suite() {
340: return new JUnit4TestAdapter(JAXR030AssociationsTest.class);
341: }
342: }
|