This project has retired. For details please refer to its Attic page.
TckBusiness xref
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 org.apache.juddi.v2.tck.*;
18  import java.io.File;
19  import static junit.framework.Assert.assertEquals;
20  import static junit.framework.Assert.assertNotNull;
21  import java.util.List;
22  import javax.xml.bind.JAXB;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.juddi.jaxb.EntityCreator;
27  import org.junit.Assert;
28  import org.uddi.api_v2.BusinessDetail;
29  import org.uddi.api_v2.BusinessEntity;
30  import org.uddi.api_v2.DeleteBusiness;
31  import org.uddi.api_v2.Description;
32  import org.uddi.api_v2.GetBusinessDetail;
33  import org.uddi.api_v2.SaveBusiness;
34  import org.uddi.v2_service.Inquire;
35  import org.uddi.v2_service.Publish;
36  
37  /**
38   * @author <a href="mailto:kstam@apache.org">Kurt T Stam</a>
39   * @author <a href="mailto:jfaath@apache.org">Jeff Faath</a>
40   * @author <a href="mailto:alexoree@apache.org">Alex O'Ree</a>
41   */
42  public class TckBusiness {
43  
44          public final static String JOE_BUSINESS_XML = "uddi_data_v2/joepublisher/businessEntity.xml";
45          public final static String JOE_BUSINESS_KEY = "920465ac-07dd-4756-8212-5c4c300faf36";
46          public final static String JOE_BUSINESS3_XML = "uddi_data_v2/joepublisher/businessEntity3.xml";
47          
48          public final static String JOE_BUSINESS3_KEY = "d04d1a31-bce1-46dc-8da9-8c7c16c46932";
49          public final static String JOE_BUSINESS_MOVE_XML = "uddi_data_v2/joepublisher/moveBusinessService1to3.xml";
50          public final static String MARY_BUSINESS_XML = "uddi_data_v2/marypublisher/businessEntity.xml";
51          
52          public final static String MARY_BUSINESS_KEY = "e509bb52-daeb-46b1-abb7-d488b10baf19";
53          public final static String SAM_BUSINESS_XML = "uddi_data_v2/samsyndicator/businessEntity.xml";
54          public final static String SAM_BUSINESS_WITHPROJECTION_XML = "uddi_data_v2/samsyndicator/businessEntity_withProjection.xml";
55          
56          public final static String SAM_BUSINESS_KEY = "49417f41-d2b5-4749-b459-55bd785a00d2";
57          public final static String COMBINE_CATBAGS_BIZ_XML = "uddi_data_v2/joepublisher/combineCatBagsBusinessServices.xml";
58          
59          public final static String COMBINE_CATBAGS_BIZ_KEY = "26dcd995-b5be-4050-8cdc-453d62c7fcaa";
60          
61          private Log logger = LogFactory.getLog(this.getClass());
62          private Publish publication = null;
63          private Inquire inquiry = null;
64  
65          public TckBusiness(Publish publication,
66               Inquire inquiry) {
67                  super();
68                  this.publication = publication;
69                  this.inquiry = inquiry;
70          }
71  
72          public BusinessEntity saveSamSyndicatorBusiness(String authInfoSam) {
73                  return saveBusiness(authInfoSam, SAM_BUSINESS_XML, SAM_BUSINESS_KEY);
74          }
75  
76          public void saveSamSyndicatorBusinesses(String authInfoSam, int numberOfCopies) {
77                  saveBusinesses(authInfoSam, SAM_BUSINESS_XML, SAM_BUSINESS_KEY, numberOfCopies);
78          }
79  
80          public BusinessEntity saveSamSyndicatorBusinessWithProjection(String authInfoSam) {
81                  return saveBusiness(authInfoSam, SAM_BUSINESS_WITHPROJECTION_XML, SAM_BUSINESS_KEY);
82          }
83  
84          public void deleteSamSyndicatorBusiness(String authInfoSam) {
85                  deleteBusiness(authInfoSam, SAM_BUSINESS_XML, SAM_BUSINESS_KEY);
86          }
87  
88          public void deleteSamSyndicatorBusinesses(String authInfoSam, int numberOfCopies) {
89                  deleteBusinesses(authInfoSam, SAM_BUSINESS_XML, SAM_BUSINESS_KEY, numberOfCopies);
90          }
91  
92  
93          public void saveJoePublisherBusiness(String authInfoJoe) {
94                  saveBusiness(authInfoJoe, JOE_BUSINESS_XML, JOE_BUSINESS_KEY, TckCommon.isDebug());
95          }
96  
97          public BusinessEntity saveCombineCatBagsPublisherBusiness(String authInfoJoe) {
98                  return saveBusiness(authInfoJoe, COMBINE_CATBAGS_BIZ_XML, COMBINE_CATBAGS_BIZ_KEY);
99          }
100 
101         public BusinessEntity saveJoePublisherBusiness3(String authInfoJoe) {
102                 return saveBusiness(authInfoJoe, JOE_BUSINESS3_XML, JOE_BUSINESS3_KEY);
103         }
104 
105         public BusinessEntity saveJoePublisherBusiness1to3(String authInfoJoe) {
106                 return saveBusiness(authInfoJoe, JOE_BUSINESS_MOVE_XML, JOE_BUSINESS3_KEY);
107         }
108 
109         public BusinessEntity saveMaryPublisherBusiness(String authInfoMary) {
110                 return saveBusiness(authInfoMary, MARY_BUSINESS_XML, MARY_BUSINESS_KEY);
111         }
112 
113         public void updateJoePublisherBusiness(String authInfoJoe) {
114                 updateBusiness(authInfoJoe, JOE_BUSINESS_XML, JOE_BUSINESS_KEY);
115         }
116 
117         public void saveJoePublisherBusinesses(String authInfoJoe, int numberOfCopies) {
118                 saveBusinesses(authInfoJoe, JOE_BUSINESS_XML, JOE_BUSINESS_KEY, numberOfCopies);
119         }
120 
121         public void deleteJoePublisherBusiness(String authInfoJoe) {
122                 deleteBusiness(authInfoJoe, JOE_BUSINESS_XML, JOE_BUSINESS_KEY);
123         }
124 
125         public void deleteJoePublisherBusiness3(String authInfoJoe) {
126                 deleteBusiness(authInfoJoe, JOE_BUSINESS3_XML, JOE_BUSINESS3_KEY);
127         }
128 
129         public void deleteMaryPublisherBusiness(String authInfoMary) {
130                 deleteBusiness(authInfoMary, MARY_BUSINESS_XML, MARY_BUSINESS_KEY);
131         }
132 
133         public void deleteJoePublisherBusinesses(String authInfoJoe, int numberOfCopies) {
134                 deleteBusinesses(authInfoJoe, JOE_BUSINESS_XML, JOE_BUSINESS_KEY, numberOfCopies);
135         }
136 
137         public void checkServicesBusinessOne(int expectedNumberOfServices) {
138                 checkNumberOfServices(JOE_BUSINESS_KEY, expectedNumberOfServices);
139         }
140 
141         public void checkServicesBusinessThree(int expectedNumberOfServices) {
142                 checkNumberOfServices(JOE_BUSINESS3_KEY, expectedNumberOfServices);
143         }
144 
145         public void saveBusinesses(String authInfo, String businessXML, String businessKey, int numberOfCopies) {
146                 try {
147                         BusinessEntity beIn = (BusinessEntity) EntityCreator.buildFromDoc(businessXML, "org.uddi.api_v2");
148                         String businessName = beIn.getName().get(0).getValue();
149                         for (int i = 0; i < numberOfCopies; i++) {
150                                 SaveBusiness sb = new SaveBusiness();
151                                 sb.setGeneric("2.0");
152                                 sb.setAuthInfo(authInfo);
153                                 beIn.getName().get(0).setValue(businessName + "-" + i);
154                                 beIn.setBusinessKey(businessKey + "-" + i);
155                                 sb.getBusinessEntity().add(beIn);
156                                 publication.saveBusiness(sb);
157                                 logger.info("Saved business with key " + businessName + "-" + i);
158                         }
159 
160                 } catch (Throwable e) {
161                         logger.error(e.getMessage(), e);
162                         Assert.fail("No exception should be thrown");
163                 }
164         }
165 
166         public void checkNumberOfServices(String businessKey, int expectedServices) {
167 
168                 try {
169                         GetBusinessDetail gb = new GetBusinessDetail();
170                         gb.setGeneric("2.0");
171                         gb.getBusinessKey().add(businessKey);
172                         BusinessDetail bd;
173                         bd = inquiry.getBusinessDetail(gb);
174                         List<BusinessEntity> beOutList = bd.getBusinessEntity();
175                         BusinessEntity beOut = beOutList.get(0);
176                         if (expectedServices > 0) {
177                                 assertEquals(expectedServices, beOut.getBusinessServices().getBusinessService().size());
178                         } else {
179                                 Assert.assertNull(beOut.getBusinessServices());
180                         }
181                 } catch (Exception e) {
182                         logger.error(e.getMessage(), e);
183                         Assert.fail("No exception should be thrown");
184                 }
185         }
186         public BusinessEntity saveBusiness(String authInfo, String businessXML, String businessKey) {
187                 return saveBusiness(authInfo, businessXML, businessKey, false);
188         }
189 
190         public BusinessEntity saveBusiness(String authInfo, String businessXML, String businessKey, boolean serialize) {
191                 logger.info("attempting to save business " + businessKey + " from " + businessXML);
192                 try {
193                         SaveBusiness sb = new SaveBusiness();
194                         sb.setGeneric("2.0");
195                         sb.setAuthInfo(authInfo);
196 
197                         BusinessEntity beIn = (BusinessEntity) EntityCreator.buildFromDoc(businessXML, "org.uddi.api_v2");
198                         if (beIn == null) {
199                                 throw new Exception("Unload to load source xml document from " + businessXML);
200                         }
201                         sb.getBusinessEntity().add(beIn);
202                         sb.setGeneric("2.0");
203                         BusinessDetail saveBusiness = publication.saveBusiness(sb);
204                         logger.info("Business saved with key " + saveBusiness.getBusinessEntity().get(0).getBusinessKey());
205                         if (serialize) {
206                                 JAXB.marshal(saveBusiness, System.out);
207                         }
208 
209                         // Now get the entity and check the values
210                         GetBusinessDetail gb = new GetBusinessDetail();
211                         gb.setGeneric("2.0");
212                         gb.getBusinessKey().add(businessKey);
213                         BusinessDetail bd = inquiry.getBusinessDetail(gb);
214                         List<BusinessEntity> beOutList = bd.getBusinessEntity();
215                         BusinessEntity beOut = beOutList.get(0);
216 
217                         if (serialize) {
218                                 JAXB.marshal(beOut, new File("target/aftersave.xml"));
219                         }
220 
221                         assertEquals(beIn.getBusinessKey(), beOut.getBusinessKey());
222 
223                         TckValidator.checkNames(beIn.getName(), beOut.getName());
224                         TckValidator.checkDescriptions(beIn.getDescription(), beOut.getDescription());
225                         TckValidator.checkDiscoveryUrls(beIn.getDiscoveryURLs(), beOut.getDiscoveryURLs());
226                         TckValidator.checkContacts(beIn.getContacts(), beOut.getContacts());
227                         TckValidator.checkCategories(beIn.getCategoryBag(), beOut.getCategoryBag());
228 
229                         return beOut;
230                 } catch (Throwable e) {
231                         logger.error(e.getMessage(), e);
232                         Assert.fail("No exception should be thrown");
233                 }
234                 return null;
235 
236         }
237 
238         public void updateBusiness(String authInfo, String businessXML, String businessKey) {
239                 try {
240 
241                         // Now get the entity and check the values
242                         GetBusinessDetail gb = new GetBusinessDetail();
243                         gb.setGeneric("2.0");
244                         gb.getBusinessKey().add(businessKey);
245                         BusinessDetail bd = inquiry.getBusinessDetail(gb);
246                         List<BusinessEntity> beOutList = bd.getBusinessEntity();
247                         BusinessEntity beOut = beOutList.get(0);
248                         //We are expecting 2 services
249                         assertEquals(2, beOut.getBusinessServices().getBusinessService().size());
250 
251                         //Now updating the business by adding another description
252                         SaveBusiness sb = new SaveBusiness();
253                         sb.setGeneric("2.0");
254                         sb.setAuthInfo(authInfo);
255                         BusinessEntity beIn = beOut;
256                         Description desc2 = new Description();
257                         desc2.setLang("nl");
258                         desc2.setValue("Omschrijving");
259                         beIn.getDescription().add(desc2);
260                         sb.getBusinessEntity().add(beIn);
261                         sb.setGeneric("2.0");
262                         publication.saveBusiness(sb);
263 
264                         // Now get the entity and check the values
265                         BusinessDetail bdnew = inquiry.getBusinessDetail(gb);
266                         List<BusinessEntity> beOutListNew = bdnew.getBusinessEntity();
267                         BusinessEntity beOutNew = beOutListNew.get(0);
268 
269                         assertEquals(beIn.getBusinessKey(), beOutNew.getBusinessKey());
270                         // After the update we still are supposed to see two services.
271                         assertNotNull(beOutNew.getBusinessServices());
272                         assertEquals(2, beOutNew.getBusinessServices().getBusinessService().size());
273 
274                 } catch (Throwable e) {
275                         logger.error(e.getMessage(), e);
276                         Assert.fail("No exception should be thrown");
277                 }
278 
279         }
280 
281         public void deleteBusinesses(String authInfo, String businessXML, String businessKey, int numberOfCopies) {
282                 try {
283                         for (int i = 0; i < numberOfCopies; i++) {
284                                 // Delete the entity and make sure it is removed
285                                 String key = businessKey + "-" + i;
286                                 DeleteBusiness db = new DeleteBusiness();
287                                 db.setGeneric("2.0");
288                                 db.setAuthInfo(authInfo);
289                                 db.getBusinessKey().add(key);
290                                 publication.deleteBusiness(db);
291                                 logger.debug("Deleted business with key " + key);
292                         }
293 
294                 } catch (Exception e) {
295                         logger.error(e.getMessage(), e);
296                         Assert.fail("No exception should be thrown");
297                 }
298         }
299 
300         public void deleteBusiness(String authInfo, String businessXML, String businessKey) {
301                 try {
302                         // Delete the entity and make sure it is removed
303                         DeleteBusiness db = new DeleteBusiness();
304                         db.setGeneric("2.0");
305                         db.setAuthInfo(authInfo);
306                         db.getBusinessKey().add(businessKey);
307                         publication.deleteBusiness(db);
308 
309                 } catch (Exception e) {
310                         logger.error(e.getMessage(), e);
311                         Assert.fail("No exception should be thrown");
312                 }
313         }
314 }