This project has retired. For details please refer to its Attic page.
JAXR030AssociationsTest xref
View Javadoc
1   /**
2    *
3    * Copyright 2004 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  package org.apache.ws.scout.registry.qa;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.fail;
21  
22  import java.util.ArrayList;
23  import java.util.Collection;
24  import java.util.Iterator;
25  
26  import javax.xml.registry.BulkResponse;
27  import javax.xml.registry.BusinessQueryManager;
28  import javax.xml.registry.FindQualifier;
29  import javax.xml.registry.JAXRException;
30  import javax.xml.registry.JAXRResponse;
31  import javax.xml.registry.RegistryService;
32  import javax.xml.registry.infomodel.Association;
33  import javax.xml.registry.infomodel.Concept;
34  import javax.xml.registry.infomodel.Key;
35  import javax.xml.registry.infomodel.Organization;
36  import javax.xml.registry.infomodel.RegistryObject;
37  
38  import junit.framework.JUnit4TestAdapter;
39  
40  import org.apache.ws.scout.BaseTestCase;
41  import org.apache.ws.scout.Creator;
42  import org.junit.After;
43  import org.junit.Assert;
44  import org.junit.Before;
45  import org.junit.Test;
46  
47  
48  /**
49   * Tests Publish, Delete (and indirectly, find) for associations.
50   * 
51   * You can comment out the deletion portion and use Open source UDDI Browser
52   * <http://www.uddibrowser.org> to check your intermediate results.
53   * 
54   * Based on query/publish tests written by <a href="mailto:anil@apache.org">Anil
55   * Saldhana</a>.
56   * 
57   * @author <a href="mailto:dbhole@redhat.com">Deepak Bhole</a>
58   * @author <a href="mailto:anil@apache.org">Anil Saldhana</a>
59   * 
60   * @since Sep 27, 2005
61   */
62  public class JAXR030AssociationsTest extends BaseTestCase {
63  
64  	private BusinessQueryManager bqm = null;
65  
66  	String associationType = "AssociationType/RelatedTo";
67  	private static String tempSrcOrgName = "Apache Source Org -- APACHE SCOUT TEST";
68  	private static String tempTgtOrgName = "Apache Target Org -- APACHE SCOUT TEST";
69  
70  	private Organization sOrg=null;
71  	private Organization tOrg=null;
72  
73      @Before
74  	public void setUp() {
75  		super.setUp();
76  	}
77  
78      @After
79  	public void tearDown() {
80  		super.tearDown();
81  	}
82  
83  	/**
84  	 * Tests publishing and deleting of associations.
85  	 * 
86  	 * Do not break this into testPublish(), testDelete(), etc. Order is
87  	 * important, and not all jvms can guarantee order since the JUnit framework
88  	 * uses getMethods() to gather test methods, and getMethods() does not
89  	 * guarantee order.
90  	 */
91      @Test
92  	public void testPublishFindAndDeleteAssociation() {
93  		login();
94  		
95  		try {
96  			
97  			RegistryService rs = connection.getRegistryService();
98  			bqm = rs.getBusinessQueryManager();
99  			blm = rs.getBusinessLifeCycleManager();
100 			
101 			//deleting any pre-exisiting organizations
102 			ArrayList<Organization> orgs = findTempOrgs();
103 			for (Organization organization : orgs) {
104 				Collection<Key> keys = new ArrayList<Key>();
105 				keys.add(organization.getKey());
106 				blm.deleteOrganizations(keys);
107 			}
108 			
109             Creatortor.html#Creator">Creator creator = new Creator(blm);
110 
111 			System.out.println("\nCreating temporary organizations...\n");
112             Organization org1 = creator.createOrganization(tempSrcOrgName);
113             Organization org2 = creator.createOrganization(tempTgtOrgName);
114             Collection<Organization> organizations = new ArrayList<Organization>();
115             organizations.add(org1);
116             organizations.add(org2);
117             blm.saveOrganizations(organizations);
118            
119 			System.out.println("\nSearching for newly created organizations...\n");
120 			ArrayList<Organization> newOrgs = findTempOrgs();
121                         Assert.assertEquals(2, newOrgs.size());
122 			sOrg = newOrgs.get(0);
123 			tOrg = newOrgs.get(1);
124 
125 			System.out.println("\nCreating association...\n");
126 			createAssociation(sOrg, tOrg);
127 
128 			// All created ... now try to delete.
129 			String associationID = findAndDeleteAssociation();
130 			
131 			//Let us look for associations now
132 			BulkResponse associationResp = 
133 				bqm.findCallerAssociations(null, Boolean.TRUE, Boolean.TRUE, null);
134 			
135 			if(associationResp.getExceptions() != null)
136 			{
137 				System.out.println(associationResp.getExceptions());
138 				fail("Association lookup failed");
139 			}
140 			else
141 			{
142 				Collection retAssocs = associationResp.getCollection();
143                 if (retAssocs.size() == 0)
144                 {
145                     //Pass
146                 } else
147                 {
148                    Iterator iterAss = retAssocs.iterator();
149                    while(iterAss.hasNext())
150                    {
151                       Association assc = (Association) iterAss.next();
152                       if(assc.getKey().getId().equals(associationID)) {
153                     	  System.out.println("found: " + associationID);
154                           fail("Deleted Association found");
155                       }
156                    }
157                 } 
158 			}
159 			 
160 		} catch (Exception e) {
161 			e.printStackTrace();
162 			fail(e.getMessage());
163 		} 
164 	}
165     
166 	private void createAssociation(Organization sOrg, Organization tOrg)
167 			throws JAXRException {
168 
169 		Concept type = bqm.findConceptByPath(associationType);
170 		Association association = blm.createAssociation(tOrg, type);
171 		sOrg.addAssociation(association);
172 
173 		ArrayList<Association> associations = new ArrayList<Association>();
174 		associations.add(association);
175 
176 		BulkResponse br = blm.saveAssociations(associations, true);
177 		if (br.getStatus() == JAXRResponse.STATUS_SUCCESS) {
178 			System.out.println("Association Saved");
179 			Collection coll = br.getCollection();
180 			Iterator iter = coll.iterator();
181 			while (iter.hasNext()) {
182 				System.out.println("Saved Key=" + iter.next());
183 			}// end while
184 		} else {
185 			System.err.println("JAXRExceptions " + "occurred during save:");
186 			Collection exceptions = br.getExceptions();
187 			Iterator iter = exceptions.iterator();
188 			while (iter.hasNext()) {
189 				Exception e = (Exception) iter.next();
190 				System.err.println(e.toString());
191 			}
192             deleteTempOrgs();
193 		}
194 	}
195 
196 	private String findAndDeleteAssociation() throws JAXRException {
197 
198 		String id = null;
199 		
200 		String sOrgID = sOrg.getKey().getId();
201 		String tOrgID = tOrg.getKey().getId();
202 
203 		Collection<String> findQualifiers = new ArrayList<String>();
204 		findQualifiers.add(FindQualifier.SORT_BY_NAME_ASC);
205 
206 		Concept type = bqm.findConceptByPath(associationType);
207 		ArrayList<Concept> conceptTypes = new ArrayList<Concept>(1);
208 		conceptTypes.add(type);
209 
210 		BulkResponse br = bqm.findAssociations(findQualifiers, sOrgID, tOrgID,
211 				conceptTypes);
212 		Collection associations = br.getCollection();
213 
214 		if (associations == null) {
215 			System.out.println("\n-- Matched 0 orgs");
216             fail("Expected 1 association");
217 
218 		} else {
219 			System.out.println("\n-- Matched " + associations.size()
220 					+ " associations --\n");
221             assertEquals(1,associations.size());
222 
223 			// then step through them
224 			for (Iterator conceptIter = associations.iterator(); conceptIter
225 					.hasNext();) {
226 				Association a = (Association) conceptIter.next();
227 
228 				System.out.println("Id: " + a.getKey().getId());
229 				System.out.println("Name: " + a.getName().getValue());
230 
231 				// Print spacer between messages
232 				System.out.println(" --- ");
233 
234 				id = a.getKey().getId();
235 				deleteAssociation(a.getKey());
236 
237 				System.out.println("\n ============================== \n");
238 			} 
239 		}
240 		return id;
241 	}
242 
243 	private void deleteAssociation(Key key) throws JAXRException {
244 
245 		String id = key.getId();
246 
247 		System.out.println("\nDeleting association with id " + id + "\n");
248 
249 		Collection<Key> keys = new ArrayList<Key>();
250 		keys.add(key);
251 		BulkResponse response = blm.deleteAssociations(keys);
252 
253         assertEquals(BulkResponse.STATUS_SUCCESS, response.getStatus());
254 		Collection exceptions = response.getExceptions();
255 		if (exceptions == null) {
256 			Collection retKeys = response.getCollection();
257 			Iterator keyIter = retKeys.iterator();
258 			javax.xml.registry.infomodel.Key orgKey = null;
259 			if (keyIter.hasNext()) {
260 				orgKey = (javax.xml.registry.infomodel.Key) keyIter.next();
261 				id = orgKey.getId();
262 				System.out
263 						.println("Association with ID=" + id + " was deleted");
264 			}
265 		}
266 	}
267 
268 	private ArrayList<Organization> findTempOrgs() throws JAXRException {
269 
270 		ArrayList<Organization> toReturn = new ArrayList<Organization>();
271 
272 		// Define find qualifiers and name patterns
273 		Collection<String> findQualifiers = new ArrayList<String>();
274 		findQualifiers.add(FindQualifier.SORT_BY_NAME_ASC);
275 		Collection<String> namePatterns = new ArrayList<String>();
276 		if ("3.0".equals(uddiversion)) {
277 			namePatterns.add(tempSrcOrgName);
278 			namePatterns.add(tempTgtOrgName);
279 		} else {
280 			namePatterns.add("%" + tempSrcOrgName + "%");
281 			namePatterns.add("%" + tempTgtOrgName + "%");
282 		}
283 		// Find based upon qualifier type and values
284 		System.out.println("\n-- searching the registry --\n");
285 		BulkResponse response = bqm.findOrganizations(findQualifiers,
286 				namePatterns, null, null, null, null);
287 
288 		// check how many organisation we have matched
289 		Collection orgs = response.getCollection();
290 		if (orgs == null) {
291 			System.out.println("\n-- Matched 0 orgs");
292 
293 		} else {
294 			System.out.println("\n-- Matched " + orgs.size()
295 					+ " organisations --\n");
296 
297 			// then step through them
298 			for (Iterator orgIter = orgs.iterator(); orgIter.hasNext();) {
299 				Organization org = (Organization) orgIter.next();
300 
301 				System.out.println("Org name: " + getName(org));
302 				System.out.println("Org description: " + getDescription(org));
303 				System.out.println("Org key id: " + getKey(org));
304 
305 				if (getName(org).indexOf(tempSrcOrgName) > -1) {
306 					toReturn.add(0, org);
307 				} else {
308 					toReturn.add(1, org);
309 				}
310 
311 				// Print spacer between organizations
312 				System.out.println("\n ============================== \n");
313 			}
314 		}// end else
315 
316 		return toReturn;
317 	}
318 
319 	private void deleteTempOrgs() {
320 
321 		try {
322 
323 			Key sOrgKey = sOrg.getKey();
324 			Key tOrgKey = tOrg.getKey();
325 
326 			System.out.println("\nDeleting temporary organizations with ids "
327 					+ sOrgKey + " and " + tOrgKey + "\n");
328 
329 			Collection<Key> keys = new ArrayList<Key>();
330 			keys.add(sOrgKey);
331 			keys.add(tOrgKey);
332 			BulkResponse response = blm.deleteOrganizations(keys);
333 
334 			Collection exceptions = response.getExceptions();
335 			if (exceptions == null) {
336 				Collection retKeys = response.getCollection();
337 				Iterator keyIter = retKeys.iterator();
338 				Key orgKey = null;
339 				while (keyIter.hasNext()) {
340 					orgKey = (javax.xml.registry.infomodel.Key) keyIter.next();
341 					String id = orgKey.getId();
342 					System.out.println("Organization with ID=" + id
343 							+ " was deleted");
344 				}
345 			}
346 		} catch (JAXRException jaxre) {
347 			jaxre.printStackTrace();
348 		}
349 	}
350 
351 	private static String getName(RegistryObject ro) throws JAXRException {
352 		if (ro != null && ro.getName() != null) {
353 			return ro.getName().getValue();
354 		}
355 		return "";
356 	}
357 
358 	private static String getDescription(RegistryObject ro)
359 			throws JAXRException {
360 		if (ro != null && ro.getDescription() != null) {
361 			return ro.getDescription().getValue();
362 		}
363 		return "";
364 	}
365 
366 	private static String getKey(RegistryObject ro) throws JAXRException {
367 		if (ro != null && ro.getKey() != null) {
368 			return ro.getKey().getId();
369 		}
370 		return "";
371 	}
372     
373     public static junit.framework.Test suite() {
374         return new JUnit4TestAdapter(JAXR030AssociationsTest.class);
375     }
376 }