View Javadoc
1   /*
2    * Copyright 2001-2009 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *      http://www.apache.org/licenses/LICENSE-2.0
8    * 
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.apache.juddi.v2.tck;
16  
17  import static junit.framework.Assert.assertEquals;
18  import static junit.framework.Assert.assertTrue;
19  import static junit.framework.Assert.assertNotNull;
20  
21  import java.util.HashSet;
22  import java.util.List;
23  import java.util.Set;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.juddi.jaxb.EntityCreator;
28  import org.junit.Assert;
29  import org.uddi.api_v2.*;
30  import org.uddi.v2_service.Inquire;
31  import org.uddi.v2_service.Publish;
32  
33  
34  /**
35   * @author <a href="mailto:kstam@apache.org">Kurt T Stam</a>
36   * @author <a href="mailto:jfaath@apache.org">Jeff Faath</a>
37   */
38  public class TckFindEntity 
39  {
40  	final static String FIND_BUSINESS_XML             = "uddi_data_v2/find/findBusiness1.xml";
41  	final static String FIND_ALL_BUSINESSES_XML       = "uddi_data_v2/find/findAllBusinesses.xml";
42  	
43  	final static String FIND_RELATED_BUSINESS_SORT_BY_NAME_XML = "uddi_data_v2/find/findRelatedBusinesses_sortByName.xml";
44  	final static String FIND_RELATED_BUSINESS_FROM_KEY= "uddi_data_v2/find/findRelatedBusinesses_fromKey.xml";
45  	final static String FIND_RELATED_BUSINESS_TO_KEY  = "uddi_data_v2/find/findRelatedBusinesses_toKey.xml";
46  	final static String FIND_SERVICE_XML              = "uddi_data_v2/find/findService1.xml";
47  	final static String FIND_BINDING_XML              = "uddi_data_v2/find/findBinding1.xml";
48  	final static String FIND_TMODEL_XML               = "uddi_data_v2/find/findTModel1.xml";
49  	final static String COMBINE_CAT_FIND_SERVICES     = "uddi_data_v2/joepublisher/combineCatBagsFindServices.xml";
50  	   
51     
52  	private Log logger = LogFactory.getLog(this.getClass());
53  	Inquire inquiry =null;
54  	
55  	public TckFindEntity(Inquire inquiry) {
56  		super();
57  		this.inquiry = inquiry;
58  	}
59  
60  	public void findBusiness() {
61  		try {
62  			FindBusiness body = (FindBusiness)EntityCreator.buildFromDoc(FIND_BUSINESS_XML, "org.uddi.api_v2");
63                 body.setGeneric("2.0");
64  			BusinessList result = inquiry.findBusiness(body);
65  			if (result == null)
66  				Assert.fail("Null result from find business operation");
67  			BusinessInfos bInfos = result.getBusinessInfos();
68  			if (bInfos == null)
69  				Assert.fail("No result from find business operation");
70  			List<BusinessInfo> biList = bInfos.getBusinessInfo();
71  			if (biList == null || biList.size() == 0)
72  				Assert.fail("No result from find business operation");
73  			BusinessInfo biOut = biList.get(0);
74  			
75  			BusinessEntity beIn = (BusinessEntity)EntityCreator.buildFromDoc(TckBusiness.JOE_BUSINESS_XML, "org.uddi.api_v2");
76  			
77  			assertEquals(beIn.getBusinessKey(), biOut.getBusinessKey());
78  			
79  			TckValidator.checkNames(beIn.getName(), biOut.getName());
80  			TckValidator.checkDescriptions(beIn.getDescription(), biOut.getDescription());
81  		}
82  		catch(Exception e) {
83  			logger.error(e.getMessage(), e);
84  			Assert.fail("No exception should be thrown.");
85  		}
86  	}
87  	
88  	public void getNonExitingBusiness() {
89  		String nonExistingKey = "nonexistingKey";
90  		try {
91  			GetBusinessDetail body = new GetBusinessDetail();
92                 body.setGeneric("2.0");
93  			body.getBusinessKey().add(nonExistingKey);
94  			BusinessDetail  result = inquiry.getBusinessDetail(body);
95  			Assert.fail("No business should be found");
96  			System.out.println(result.getBusinessEntity().size());
97  		} catch (Exception e) {
98  			try {
99  				System.out.println("(Expected) Error message = " + e.getMessage());
100 				DispositionReport report = org.uddi.v2_service.DispositionReport.getDispositionReport(e);
101 				assertNotNull(report);
102 				assertTrue(report.countainsErrorCode(DispositionReport.E_INVALID_KEY_PASSED));
103 			} catch (Exception e1) {
104 				Assert.fail("We only expect DispositionReportFaultMessage, not " + e1.getClass());
105 				logger.error(e.getMessage(), e1);
106 			}
107 		}
108 	}
109 	
110 	public void findAllBusiness() {
111 		try {
112 			FindBusiness body = (FindBusiness)EntityCreator.buildFromDoc(FIND_ALL_BUSINESSES_XML, "org.uddi.api_v2");
113                body.setGeneric("2.0");
114 			BusinessList result = inquiry.findBusiness(body);
115 			if (result == null)
116 				Assert.fail("Null result from find business operation");
117 			BusinessInfos bInfos = result.getBusinessInfos();
118 			if (bInfos == null)
119 				Assert.fail("No result from find business operation");
120 			List<BusinessInfo> biList = bInfos.getBusinessInfo();
121 			if (biList == null || biList.size() == 0)
122 				Assert.fail("No result from find business operation");
123 			//expecting more than 2 businesses
124 			Assert.assertTrue(biList.size()>1);
125 			
126 		}
127 		catch(Exception e) {
128 			logger.error(e.getMessage(), e);
129 			Assert.fail("No exception should be thrown.");
130 		}
131 	}
132 	
133 	
134 	public void findRelatedBusiness_sortByName(boolean isOneSided) {
135 		try {
136 			FindRelatedBusinesses body = (FindRelatedBusinesses)EntityCreator.buildFromDoc(FIND_RELATED_BUSINESS_SORT_BY_NAME_XML, "org.uddi.api_v2");
137                body.setGeneric("2.0");
138 			RelatedBusinessesList result = inquiry.findRelatedBusinesses(body);
139 			if (result == null)
140 				Assert.fail("Null result from find related business operation");
141 			RelatedBusinessInfos bInfos = result.getRelatedBusinessInfos();
142 		
143 			//both parties need to register the assertion for it to be live.
144 			if (isOneSided) {
145 				Assert.assertTrue(bInfos==null || bInfos.getRelatedBusinessInfo().isEmpty());
146 			} else {
147 				if (bInfos == null)
148 					Assert.fail("No result from find related business operation");
149 				List<RelatedBusinessInfo> biList = bInfos.getRelatedBusinessInfo();
150 				if (biList == null || biList.size() == 0)
151 					Assert.fail("No result from find related business operation");
152 				Set<String> keys = new HashSet<String>();
153 				for (RelatedBusinessInfo relatedBusinessInfo : biList) {
154 					keys.add(relatedBusinessInfo.getBusinessKey());
155 				}
156 				Assert.assertTrue(keys.contains(TckBusiness.SAM_BUSINESS_KEY));
157 				Assert.assertTrue(keys.contains(TckBusiness.MARY_BUSINESS_KEY));
158 			}
159 		}
160 		catch(Exception e) {
161 			logger.error(e.getMessage(), e);
162 			Assert.fail("No exception should be thrown.");
163 		}
164 	}
165 	
166 	public void findRelatedBusinessToKey(boolean isOneSided) {
167 		try {
168 			FindRelatedBusinesses body = (FindRelatedBusinesses)EntityCreator.buildFromDoc(FIND_RELATED_BUSINESS_TO_KEY, "org.uddi.api_v2");
169                body.setGeneric("2.0");
170 			RelatedBusinessesList result = inquiry.findRelatedBusinesses(body);
171 			if (result == null)
172 				Assert.fail("Null result from find related business operation");
173 			RelatedBusinessInfos bInfos = result.getRelatedBusinessInfos();
174 		
175 			//both parties need to register the assertion for it to be live.
176 			if (isOneSided) {
177 				Assert.assertTrue(bInfos==null || bInfos.getRelatedBusinessInfo().isEmpty());
178 			} else {
179 				if (bInfos == null)
180 					Assert.fail("No result from find related business operation");
181 				List<RelatedBusinessInfo> biList = bInfos.getRelatedBusinessInfo();
182 				if (biList == null || biList.size() == 0)
183 					Assert.fail("No result from find related business operation");
184 				Set<String> keys = new HashSet<String>();
185 				for (RelatedBusinessInfo relatedBusinessInfo : biList) {
186 					keys.add(relatedBusinessInfo.getBusinessKey());
187 				}
188 				Assert.assertTrue(keys.contains(TckBusiness.JOE_BUSINESS_KEY));
189 			}
190 		}
191 		catch(Exception e) {
192 			logger.error(e.getMessage(), e);
193 			Assert.fail("No exception should be thrown.");
194 		}
195 	}
196 	
197 	public void findRelatedBusinessFromKey(boolean isOneSided) {
198 		try {
199 			FindRelatedBusinesses body = (FindRelatedBusinesses)EntityCreator.buildFromDoc(FIND_RELATED_BUSINESS_FROM_KEY, "org.uddi.api_v2");
200                body.setGeneric("2.0");
201 			RelatedBusinessesList result = inquiry.findRelatedBusinesses(body);
202 			if (result == null)
203 				Assert.fail("Null result from find related business operation");
204 			RelatedBusinessInfos bInfos = result.getRelatedBusinessInfos();
205 		
206 			//both parties need to register the assertion for it to be live.
207 			if (isOneSided) {
208 				Assert.assertNull(bInfos);
209 			} else {
210 				if (bInfos == null)
211 					Assert.fail("No result from find related business operation");
212 				List<RelatedBusinessInfo> biList = bInfos.getRelatedBusinessInfo();
213 				if (biList == null || biList.size() == 0)
214 					Assert.fail("No result from find related business operation");
215 				Set<String> keys = new HashSet<String>();
216 				for (RelatedBusinessInfo relatedBusinessInfo : biList) {
217 					keys.add(relatedBusinessInfo.getBusinessKey());
218 				}
219 				Assert.assertTrue(keys.contains(TckBusiness.SAM_BUSINESS_KEY));
220 			}
221 		}
222 		catch(Exception e) {
223 			logger.error(e.getMessage(), e);
224 			Assert.fail("No exception should be thrown.");
225 		}
226 	}
227 	
228 	public String findService(String findQualifier) {
229 		String serviceKey = null;
230 		try {
231 			FindService body = (FindService)EntityCreator.buildFromDoc(FIND_SERVICE_XML, "org.uddi.api_v2");
232                         body.setGeneric("2.0");
233 			if (findQualifier!=null) body.getFindQualifiers().getFindQualifier().add(findQualifier);
234 			ServiceList result = inquiry.findService(body);
235 			if (result == null)
236 				Assert.fail("Null result from find service operation");
237 			ServiceInfos sInfos = result.getServiceInfos();
238 			if (sInfos == null)
239 				Assert.fail("No result from find service operation");
240 			List<ServiceInfo> siList = sInfos.getServiceInfo();
241 			if (siList == null || siList.size() == 0)
242 				Assert.fail("No result from find service operation");
243 			ServiceInfo siOut = siList.get(0);
244 			
245 			BusinessService bsIn = (BusinessService)EntityCreator.buildFromDoc(TckBusinessService.JOE_SERVICE_XML, "org.uddi.api_v2");
246 			
247 			assertEquals(bsIn.getServiceKey(), siOut.getServiceKey());
248 			
249 			TckValidator.checkNames(bsIn.getName(), siOut.getName());
250 			serviceKey = siOut.getServiceKey();
251 		}
252 		catch(Exception e) {
253 			logger.error(e.getMessage(), e);
254 			Assert.fail("No exception should be thrown.");
255 		}
256 		return serviceKey;
257 	}
258 	
259 	public String findService_CombinedCatBag() {
260 		String serviceKey = null;
261 		try {
262 			FindService body = (FindService)EntityCreator.buildFromDoc(COMBINE_CAT_FIND_SERVICES, "org.uddi.api_v2");
263                body.setGeneric("2.0");
264 			ServiceList result = inquiry.findService(body);
265 			if (result == null)
266 				Assert.fail("Null result from find service operation");
267 			ServiceInfos sInfos = result.getServiceInfos();
268 			if (sInfos == null)
269 				Assert.fail("No result from find service operation");
270 			List<ServiceInfo> siList = sInfos.getServiceInfo();
271 			if (siList == null || siList.size() == 0)
272 				Assert.fail("No result from find service operation");
273 			ServiceInfo siOut = siList.get(0);
274 			
275 			serviceKey = siOut.getServiceKey();
276 		}
277 		catch(Exception e) {
278 			logger.error(e.getMessage(), e);
279 			Assert.fail("No exception should be thrown.");
280 		}
281 		return serviceKey;
282 	}
283 
284 	public void findServiceDetail(String serviceKey) {
285 		try {
286 			GetServiceDetail getServiceDetail = new GetServiceDetail();
287                         getServiceDetail.setGeneric("2.0");
288 			getServiceDetail.getServiceKey().add(serviceKey);
289 			
290 			
291 			ServiceDetail result = inquiry.getServiceDetail(getServiceDetail);
292 			
293 			if (result == null)
294 				Assert.fail("Null result from find service operation");
295 			
296 			BindingTemplates templates = result.getBusinessService().get(0).getBindingTemplates();
297 			if (templates!=null && templates.getBindingTemplate()!=null) {
298 				System.out.println(templates.getBindingTemplate().size());
299 				System.out.println("key=" + templates.getBindingTemplate().get(0).getBindingKey());
300 			}
301 //			ServiceInfos sInfos = result.getServiceInfos();
302 //			if (sInfos == null)
303 //				Assert.fail("No result from find service operation");
304 //			List<ServiceInfo> siList = sInfos.getServiceInfo();
305 //			if (siList == null || siList.size() == 0)
306 //				Assert.fail("No result from find service operation");
307 //			ServiceInfo siOut = siList.get(0);
308 //			
309 //			BusinessService bsIn = (BusinessService)EntityCreator.buildFromDoc(TckBusinessService.JOE_SERVICE_XML, "org.uddi.api_v2");
310 //			
311 //			assertEquals(bsIn.getServiceKey(), siOut.getServiceKey());
312 //			
313 //			TckValidator.checkNames(bsIn.getName(), siOut.getName());
314 		}
315 		catch(Exception e) {
316 			logger.error(e.getMessage(), e);
317 			Assert.fail("No exception should be thrown.");
318 		}
319 	}
320 	public void findBinding(String findQualifier) {
321 		try {
322 			FindBinding body = (FindBinding)EntityCreator.buildFromDoc(FIND_BINDING_XML, "org.uddi.api_v2");
323 			if (findQualifier!=null) body.getFindQualifiers().getFindQualifier().add(findQualifier);
324                         body.setGeneric("2.0");
325 			BindingDetail result = inquiry.findBinding(body);
326 			if (result == null)
327 				Assert.fail("Null result from find binding operation");
328 			List<BindingTemplate> btList = result.getBindingTemplate();
329 			if (btList == null || btList.size() == 0)
330 				Assert.fail("No result from find binding operation");
331 			BindingTemplate btOut = btList.get(0);
332 			
333 			BindingTemplate btIn = (BindingTemplate)EntityCreator.buildFromDoc(TckBindingTemplate.JOE_BINDING_XML, "org.uddi.api_v2");
334 			
335 			assertEquals(btIn.getServiceKey(), btOut.getServiceKey());
336 			assertEquals(btIn.getBindingKey(), btOut.getBindingKey());
337 			
338 			TckValidator.checkDescriptions(btIn.getDescription(), btOut.getDescription());
339 		}
340 		catch(Exception e) {
341 			logger.error(e.getMessage(), e);
342 			Assert.fail("No exception should be thrown:  " + e.getMessage());
343 		}
344 	}
345 	
346 	public void findTModel(String findQualifier) {
347 		try {
348 			FindTModel body = (FindTModel)EntityCreator.buildFromDoc(FIND_TMODEL_XML, "org.uddi.api_v2");
349 			if (findQualifier!=null) body.getFindQualifiers().getFindQualifier().add(findQualifier);
350                body.setGeneric("2.0");
351 			TModelList result = inquiry.findTModel(body);
352 			if (result == null)
353 				Assert.fail("Null result from find tModel operation");
354 			TModelInfos tInfos = result.getTModelInfos();
355 			if (tInfos == null)
356 				Assert.fail("No result from find tModel operation");
357 			List<TModelInfo> tiList = tInfos.getTModelInfo();
358 			if (tiList == null || tiList.size() == 0)
359 				Assert.fail("No result from find tModel operation");
360 			TModelInfo tiOut = tiList.get(0);
361 			
362 			TModel tmIn = (TModel)EntityCreator.buildFromDoc(TckTModel.JOE_PUBLISHER_TMODEL_XML, "org.uddi.api_v2");
363 			
364 			assertEquals(tmIn.getTModelKey(), tiOut.getTModelKey());
365 			assertEquals(tmIn.getName().getLang(), tiOut.getName().getLang());
366 			assertEquals(tmIn.getName().getValue(), tiOut.getName().getValue());
367 
368 		}
369 		catch(Exception e) {
370 			logger.error(e.getMessage(), e);
371 			Assert.fail("No exception should be thrown.");
372 		}
373 	}
374 }