View Javadoc
1   /*
2    * Copyright 2013 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    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.juddi.v2.tck;
17  
18  import java.util.List;
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  import org.uddi.api_v2.BindingTemplates;
22  import org.uddi.api_v2.BusinessEntity;
23  import org.uddi.api_v2.BusinessInfos;
24  import org.uddi.api_v2.BusinessList;
25  import org.uddi.api_v2.CategoryBag;
26  import org.uddi.api_v2.Contacts;
27  import org.uddi.api_v2.DeleteBusiness;
28  import org.uddi.api_v2.Description;
29  import org.uddi.api_v2.FindBusiness;
30  import org.uddi.api_v2.FindQualifiers;
31  import org.uddi.api_v2.FindService;
32  import org.uddi.api_v2.FindTModel;
33  import org.uddi.api_v2.KeyedReference;
34  import org.uddi.api_v2.Name;
35  import org.uddi.api_v2.ServiceInfos;
36  import org.uddi.api_v2.ServiceList;
37  import org.uddi.api_v2.TModelList;
38  import org.uddi.v2_service.*;
39  
40  
41  /**
42   * Common Utilities for TCK tests
43   *
44   * @author Alex O'Ree
45   */
46  public class TckCommon {
47  
48          private static Log logger = LogFactory.getLog(TckCommon.class);
49  //<editor-fold defaultstate="collapsed" desc="Some basic util functions to print out the data structure">
50  
51          /**
52           * Converts category bags of tmodels to a readable string
53           *
54           * @param categoryBag
55           * @return human readable category bag
56           */
57          public static String CatBagToString(CategoryBag categoryBag) {
58                  StringBuilder sb = new StringBuilder();
59                  if (categoryBag == null) {
60                          return "no data";
61                  }
62                  for (int i = 0; i < categoryBag.getKeyedReference().size(); i++) {
63                          sb.append(KeyedReferenceToString(categoryBag.getKeyedReference().get(i)));
64                  }
65                  return sb.toString();
66          }
67  
68          public static String KeyedReferenceToString(KeyedReference item) {
69                  StringBuilder sb = new StringBuilder();
70                  sb.append("Key Ref: Name=").
71                          append(item.getKeyName()).
72                          append(" Value=").
73                          append(item.getKeyValue()).
74                          append(" tModel=").
75                          append(item.getTModelKey()).
76                          append(System.getProperty("line.separator"));
77                  return sb.toString();
78          }
79  
80          public static void PrintContacts(Contacts contacts) {
81                  if (contacts == null) {
82                          return;
83                  }
84                  for (int i = 0; i < contacts.getContact().size(); i++) {
85                          System.out.println("Contact " + i + " type:" + contacts.getContact().get(i).getUseType());
86  
87                                  System.out.println("Name: " + contacts.getContact().get(i).getPersonName());
88                          
89                          for (int k = 0; k < contacts.getContact().get(i).getEmail().size(); k++) {
90                                  System.out.println("Email: " + contacts.getContact().get(i).getEmail().get(k).getValue());
91                          }
92                          for (int k = 0; k < contacts.getContact().get(i).getAddress().size(); k++) {
93                                  System.out.println("Address sort code " + contacts.getContact().get(i).getAddress().get(k).getSortCode());
94                                  System.out.println("Address use type " + contacts.getContact().get(i).getAddress().get(k).getUseType());
95                                  System.out.println("Address tmodel key " + contacts.getContact().get(i).getAddress().get(k).getTModelKey());
96                                  for (int x = 0; x < contacts.getContact().get(i).getAddress().get(k).getAddressLine().size(); x++) {
97                                          System.out.println("Address line value " + contacts.getContact().get(i).getAddress().get(k).getAddressLine().get(x).getValue());
98                                          System.out.println("Address line key name " + contacts.getContact().get(i).getAddress().get(k).getAddressLine().get(x).getKeyName());
99                                          System.out.println("Address line key value " + contacts.getContact().get(i).getAddress().get(k).getAddressLine().get(x).getKeyValue());
100                                 }
101                         }
102                         for (int k = 0; k < contacts.getContact().get(i).getDescription().size(); k++) {
103                                 System.out.println("Desc: " + contacts.getContact().get(i).getDescription().get(k).getValue());
104                         }
105                         for (int k = 0; k < contacts.getContact().get(i).getPhone().size(); k++) {
106                                 System.out.println("Phone: " + contacts.getContact().get(i).getPhone().get(k).getValue());
107                         }
108                 }
109 
110         }
111 
112         /**
113          * This function is useful for translating UDDI's somewhat complex data
114          * format to something that is more useful.
115          *
116          * @param bindingTemplates
117          */
118         public static void PrintBindingTemplates(BindingTemplates bindingTemplates) {
119                 if (bindingTemplates == null) {
120                         return;
121                 }
122                 for (int i = 0; i < bindingTemplates.getBindingTemplate().size(); i++) {
123                         System.out.println("Binding Key: " + bindingTemplates.getBindingTemplate().get(i).getBindingKey());
124 
125                         if (bindingTemplates.getBindingTemplate().get(i).getAccessPoint() != null) {
126                                 System.out.println("Access Point: " + bindingTemplates.getBindingTemplate().get(i).getAccessPoint().getValue() + " type " + bindingTemplates.getBindingTemplate().get(i).getAccessPoint().getURLType().value());
127                         }
128 
129                         if (bindingTemplates.getBindingTemplate().get(i).getHostingRedirector() != null) {
130                                 System.out.println("Hosting Redirection: " + bindingTemplates.getBindingTemplate().get(i).getHostingRedirector().getBindingKey());
131                         }
132                 }
133         }
134 
135         public static void PrintBusinessInfo(BusinessInfos businessInfos) {
136                 if (businessInfos == null) {
137                         System.out.println("No data returned");
138                 } else {
139                         for (int i = 0; i < businessInfos.getBusinessInfo().size(); i++) {
140                                 System.out.println("===============================================");
141                                 System.out.println("Business Key: " + businessInfos.getBusinessInfo().get(i).getBusinessKey());
142                                 System.out.println("Name: " + ListToString(businessInfos.getBusinessInfo().get(i).getName()));
143 
144                                 System.out.println("Name: " + ListToDescString(businessInfos.getBusinessInfo().get(i).getDescription()));
145                                 System.out.println("Services:");
146                                 PrintServiceInfo(businessInfos.getBusinessInfo().get(i).getServiceInfos());
147                         }
148                 }
149         }
150 
151         public static String ListToString(List<Name> name) {
152                 StringBuilder sb = new StringBuilder();
153                 for (int i = 0; i < name.size(); i++) {
154                         sb.append(name.get(i).getValue()).append(" ");
155                 }
156                 return sb.toString();
157         }
158 
159         public static String ListToDescString(List<Description> name) {
160                 StringBuilder sb = new StringBuilder();
161                 for (int i = 0; i < name.size(); i++) {
162                         sb.append(name.get(i).getValue()).append(" ");
163                 }
164                 return sb.toString();
165         }
166 
167         public static void PrintServiceInfo(ServiceInfos serviceInfos) {
168                 for (int i = 0; i < serviceInfos.getServiceInfo().size(); i++) {
169                         System.out.println("-------------------------------------------");
170                         System.out.println("Service Key: " + serviceInfos.getServiceInfo().get(i).getServiceKey());
171                         System.out.println("Owning Business Key: " + serviceInfos.getServiceInfo().get(i).getBusinessKey());
172                         System.out.println("Name: " + ListToString(serviceInfos.getServiceInfo().get(i).getName()));
173                 }
174         }
175 
176         public static void PrintBusinessDetails(List<BusinessEntity> businessDetail) {
177 
178 
179                 for (int i = 0; i < businessDetail.size(); i++) {
180                         System.out.println("Business Detail - key: " + businessDetail.get(i).getBusinessKey());
181                         System.out.println("Name: " + ListToString(businessDetail.get(i).getName()));
182                         System.out.println("CategoryBag: " + CatBagToString(businessDetail.get(i).getCategoryBag()));
183                         PrintContacts(businessDetail.get(i).getContacts());
184                 }
185         }
186 
187         /**
188          * use this for clean up actions after running tests. if an exception is
189          * raised, it will only be logged
190          *
191          * @param key
192          * @param authInfo
193          * @param publish
194          */
195         public static void DeleteBusiness(String key, String authInfo, Publish publish) {
196                 if (key == null) {
197                         return;
198                 }
199                 try {
200                         DeleteBusiness db = new DeleteBusiness();
201                         db.setGeneric("2.0");
202                         db.setAuthInfo(authInfo);
203                         db.getBusinessKey().add(key);
204                         publish.deleteBusiness(db);
205                 } catch (Exception ex) {
206                         logger.warn("failed to delete business " + key + " " + ex.getMessage());
207                         logger.debug("failed to delete business " + key + " " + ex.getMessage(), ex);
208                 }
209         }
210 
211         
212         /**
213          * returns true if the System Property "debug" is equal to "true"
214          * @return true/false
215          */
216         public static boolean isDebug() {
217                 boolean serialize = false;
218                 try {
219                         if (System.getProperty("debug") != null
220                                 && System.getProperty("debug").equalsIgnoreCase("true")) {
221                                 serialize = true;
222                         }
223                 } catch (Exception ex) {
224                 }
225                 return serialize;
226         }
227         
228         
229          public static String DumpAllServices(String authinfo, Inquire inquiry) {
230                 StringBuilder sb = new StringBuilder();
231                 FindService fs = new FindService();
232                 //fs.setAuthInfo(authinfo);
233                 //fs.setFindQualifiers(new FindQualifiers());
234                 //fs.getFindQualifiers().getFindQualifier().add("approximateMatch");
235                 fs.getName().add(new Name("%", null));
236                 try {
237                         ServiceList findService = inquiry.findService(fs);
238                         if (findService.getServiceInfos() == null) {
239                                 return ("NO SERVICES RETURNED!");
240                         } else {
241                                 for (int i = 0; i < findService.getServiceInfos().getServiceInfo().size(); i++) {
242                                         sb.append(findService.getServiceInfos().getServiceInfo().get(i).getName().get(0).getValue()).append(" lang=").append(findService.getServiceInfos().getServiceInfo().get(i).getName().get(0).getLang()).append(" ").append(findService.getServiceInfos().getServiceInfo().get(i).getServiceKey()).append(" ").append(findService.getServiceInfos().getServiceInfo().get(i).getBusinessKey()).append(
243                                                 System.getProperty("line.separator"));
244                                 }
245                         }
246                 } catch (Exception ex) {
247                         sb.append(ex.getMessage());
248                 }
249                 return sb.toString();
250         }
251 
252          
253         public static String DumpAllTModels(String authinfo, Inquire inquriy) {
254                 StringBuilder sb = new StringBuilder();
255                 FindTModel fs = new FindTModel();
256                 //fs.setAuthInfo(authinfo);
257                 //fs.setFindQualifiers(new FindQualifiers());
258                 //fs.getFindQualifiers().getFindQualifier().add("approximateMatch");
259                 fs.setName(new Name("%", null));
260                 fs.setGeneric("2.0");
261                 try {
262                         TModelList findService = inquriy.findTModel(fs);
263                         if (findService.getTModelInfos()== null) {
264                                 return ("NO TMODELS RETURNED!");
265                         } else {
266                                 for (int i = 0; i < findService.getTModelInfos().getTModelInfo().size(); i++) {
267                                         sb.append(findService.getTModelInfos().getTModelInfo().get(i).getName().getValue()).
268                                                 append(" lang=").append(findService.getTModelInfos().getTModelInfo().get(i).getName().getLang()).
269                                                 append(" ").append(findService.getTModelInfos().getTModelInfo().get(i).getTModelKey())
270                                                 .append(System.getProperty("line.separator"));
271                                 }
272                         }
273                 } catch (Exception ex) {
274                         return ex.getMessage();
275                 }
276                 return sb.toString();
277         }         
278         public static String DumpAllBusinesses(String authinfo, Inquire inquriy) {
279                 StringBuilder sb = new StringBuilder();
280                 FindBusiness fs = new FindBusiness();
281                 //fs.setAuthInfo(authinfo);
282                 //fs.setFindQualifiers(new FindQualifiers());
283                 //fs.getFindQualifiers().getFindQualifier().add("approximateMatch");
284                 fs.getName().add(new Name("%", null));
285                 fs.setGeneric("2.0");
286                 try {
287                         BusinessList findService = inquriy.findBusiness(fs);
288                         if (findService.getBusinessInfos() == null) {
289                                 return ("NO BUSINESSES RETURNED!");
290                         } else {
291                                 for (int i = 0; i < findService.getBusinessInfos().getBusinessInfo().size(); i++) {
292                                         sb.append(findService.getBusinessInfos().getBusinessInfo().get(i).getName().get(0).getValue()).
293                                                 append(" lang=").append(findService.getBusinessInfos().getBusinessInfo().get(i).getName().get(0).getLang()).
294                                                 append(" ").append(findService.getBusinessInfos().getBusinessInfo().get(i).getBusinessKey())
295                                                 .append(System.getProperty("line.separator"));
296                                 }
297                         }
298                 } catch (Exception ex) {
299                         return ex.getMessage();
300                 }
301                 return sb.toString();
302         }
303 
304 }