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.v3.tck;
16  
17  import java.io.File;
18  import java.io.FileInputStream;
19  import java.net.URL;
20  import static junit.framework.Assert.assertEquals;
21  import static junit.framework.Assert.assertNotNull;
22  import static junit.framework.Assert.assertTrue;
23  
24  import java.rmi.RemoteException;
25  import java.security.KeyStore;
26  import java.security.PrivateKey;
27  import java.security.PublicKey;
28  import java.security.cert.Certificate;
29  import java.util.List;
30  import javax.xml.bind.JAXB;
31  import javax.xml.transform.dom.DOMResult;
32  import javax.xml.transform.dom.DOMSource;
33  
34  import org.apache.commons.logging.Log;
35  import org.apache.commons.logging.LogFactory;
36  import org.apache.juddi.jaxb.EntityCreator;
37  import org.junit.Assert;
38  import org.uddi.api_v3.BusinessDetail;
39  import org.uddi.api_v3.BusinessEntity;
40  import org.uddi.api_v3.DeleteBusiness;
41  import org.uddi.api_v3.Description;
42  import org.uddi.api_v3.GetBusinessDetail;
43  import org.uddi.api_v3.SaveBusiness;
44  import org.uddi.v3_service.UDDIInquiryPortType;
45  import org.uddi.v3_service.UDDIPublicationPortType;
46  import org.w3c.dom.Document;
47  import org.w3c.dom.Element;
48  
49  /**
50   * @author <a href="mailto:kstam@apache.org">Kurt T Stam</a>
51   * @author <a href="mailto:jfaath@apache.org">Jeff Faath</a>
52   */
53  public class TckBusiness {
54  
55          public final static String JOE_BUSINESS_XML = "uddi_data/joepublisher/businessEntity.xml";
56          public final static String JOE_BUSINESS_KEY = "uddi:uddi.joepublisher.com:businessone";
57          public final static String JOE_BUSINESS3_XML = "uddi_data/joepublisher/businessEntity3.xml";
58          public final static String JOE_BUSINESS3_KEY = "uddi:uddi.joepublisher.com:businessthree.com";
59          public final static String JOE_BUSINESS_MOVE_XML = "uddi_data/joepublisher/moveBusinessService1to3.xml";
60          public final static String MARY_BUSINESS_XML = "uddi_data/marypublisher/businessEntity.xml";
61          public final static String MARY_BUSINESS_KEY = "uddi:uddi.marypublisher.com:marybusinessone";
62          public final static String SAM_BUSINESS_XML = "uddi_data/samsyndicator/businessEntity.xml";
63          public final static String SAM_BUSINESS_WITHPROJECTION_XML = "uddi_data/samsyndicator/businessEntity_withProjection.xml";
64          public final static String SAM_BUSINESS_KEY = "uddi:www.samco.com:samco";
65          public final static String COMBINE_CATBAGS_BIZ_XML = "uddi_data/joepublisher/combineCatBagsBusinessServices.xml";
66          public final static String COMBINE_CATBAGS_BIZ_KEY = "uddi:uddi.joepublisher.com:business01";
67          public final static String RIFTSAW_BUSINESS_KEY = "uddi:riftsaw.jboss.org:business_redhat-jboss";
68          public final static String RIFTSAW_BUSINESS_XML = "uddi_data/bpel/riftsaw/businessEntity.xml";
69          final static String SIGNATURE_KEYSTORE = "uddi_data/signature/signature.jks";
70          final static String SIGNATURE_KEYSTORE_TYPE = "JKS";
71          final static String SIGNATURE_KEYSTORE_PASSWORD = "changeit";
72          final static String SIGNATURE_KEYSTORE_ALIAS = "mykey";
73          private Log logger = LogFactory.getLog(this.getClass());
74          private UDDIPublicationPortType publication = null;
75          private UDDIInquiryPortType inquiry = null;
76  
77          public TckBusiness(UDDIPublicationPortType publication,
78               UDDIInquiryPortType inquiry) {
79                  super();
80                  this.publication = publication;
81                  this.inquiry = inquiry;
82          }
83  
84          public BusinessEntity saveSamSyndicatorBusiness(String authInfoSam) {
85                  return saveBusiness(authInfoSam, SAM_BUSINESS_XML, SAM_BUSINESS_KEY);
86          }
87  
88          public void saveSamSyndicatorBusinesses(String authInfoSam, int numberOfCopies) {
89                  saveBusinesses(authInfoSam, SAM_BUSINESS_XML, SAM_BUSINESS_KEY, numberOfCopies);
90          }
91  
92          public BusinessEntity saveSamSyndicatorBusinessWithProjection(String authInfoSam) {
93                  return saveBusiness(authInfoSam, SAM_BUSINESS_WITHPROJECTION_XML, SAM_BUSINESS_KEY);
94          }
95  
96          public void deleteSamSyndicatorBusiness(String authInfoSam) {
97                  deleteBusiness(authInfoSam, SAM_BUSINESS_XML, SAM_BUSINESS_KEY);
98          }
99  
100         public void deleteSamSyndicatorBusinesses(String authInfoSam, int numberOfCopies) {
101                 deleteBusinesses(authInfoSam, SAM_BUSINESS_XML, SAM_BUSINESS_KEY, numberOfCopies);
102         }
103 
104         public void saveJoePublisherBusinessX509Signature(String authInfoJoe) {
105                 saveBusiness(authInfoJoe, JOE_BUSINESS_XML, JOE_BUSINESS_KEY);
106                 signBusiness(authInfoJoe, JOE_BUSINESS_KEY);
107                 verifyBusinessSignature(authInfoJoe, JOE_BUSINESS_KEY);
108         }
109 
110         public void saveJoePublisherBusiness(String authInfoJoe) {
111                 saveBusiness(authInfoJoe, JOE_BUSINESS_XML, JOE_BUSINESS_KEY, TckCommon.isDebug());
112         }
113 
114         public BusinessEntity saveCombineCatBagsPublisherBusiness(String authInfoJoe) {
115                 return saveBusiness(authInfoJoe, COMBINE_CATBAGS_BIZ_XML, COMBINE_CATBAGS_BIZ_KEY);
116         }
117 
118         public BusinessEntity saveJoePublisherBusiness3(String authInfoJoe) {
119                 return saveBusiness(authInfoJoe, JOE_BUSINESS3_XML, JOE_BUSINESS3_KEY);
120         }
121 
122         public BusinessEntity saveJoePublisherBusiness1to3(String authInfoJoe) {
123                 return saveBusiness(authInfoJoe, JOE_BUSINESS_MOVE_XML, JOE_BUSINESS3_KEY);
124         }
125 
126         public BusinessEntity saveMaryPublisherBusiness(String authInfoMary) {
127                 return saveBusiness(authInfoMary, MARY_BUSINESS_XML, MARY_BUSINESS_KEY);
128         }
129 
130         public void updateJoePublisherBusiness(String authInfoJoe) {
131                 updateBusiness(authInfoJoe, JOE_BUSINESS_XML, JOE_BUSINESS_KEY);
132         }
133 
134         public void saveJoePublisherBusinesses(String authInfoJoe, int numberOfCopies) {
135                 saveBusinesses(authInfoJoe, JOE_BUSINESS_XML, JOE_BUSINESS_KEY, numberOfCopies);
136         }
137 
138         public void deleteJoePublisherBusiness(String authInfoJoe) {
139                 deleteBusiness(authInfoJoe, JOE_BUSINESS_XML, JOE_BUSINESS_KEY);
140         }
141 
142         public void deleteJoePublisherBusiness3(String authInfoJoe) {
143                 deleteBusiness(authInfoJoe, JOE_BUSINESS3_XML, JOE_BUSINESS3_KEY);
144         }
145 
146         public void deleteMaryPublisherBusiness(String authInfoMary) {
147                 deleteBusiness(authInfoMary, MARY_BUSINESS_XML, MARY_BUSINESS_KEY);
148         }
149 
150         public void deleteJoePublisherBusinesses(String authInfoJoe, int numberOfCopies) {
151                 deleteBusinesses(authInfoJoe, JOE_BUSINESS_XML, JOE_BUSINESS_KEY, numberOfCopies);
152         }
153 
154         public void checkServicesBusinessOne(int expectedNumberOfServices) {
155                 checkNumberOfServices(JOE_BUSINESS_KEY, expectedNumberOfServices);
156         }
157 
158         public void checkServicesBusinessThree(int expectedNumberOfServices) {
159                 checkNumberOfServices(JOE_BUSINESS3_KEY, expectedNumberOfServices);
160         }
161 
162         public void saveBusinesses(String authInfo, String businessXML, String businessKey, int numberOfCopies) {
163                 try {
164                         BusinessEntity beIn = (BusinessEntity) EntityCreator.buildFromDoc(businessXML, "org.uddi.api_v3");
165                         String businessName = beIn.getName().get(0).getValue();
166                         for (int i = 0; i < numberOfCopies; i++) {
167                                 SaveBusiness sb = new SaveBusiness();
168                                 sb.setAuthInfo(authInfo);
169                                 beIn.getName().get(0).setValue(businessName + "-" + i);
170                                 beIn.setBusinessKey(businessKey + "-" + i);
171                                 sb.getBusinessEntity().add(beIn);
172                                /* if (beIn.getBusinessServices()!=null){
173                                         for (int x=0; x < beIn.getBusinessServices().getBusinessService().size(); x++){
174                                                 beIn.getBusinessServices().getBusinessService().get(x).setBusinessKey(businessKey + "-" + i);
175                                                 if (beIn.getBusinessServices().getBusinessService().get(x).getBindingTemplates()!=null){
176                                                         for (int y=0; y < beIn.getBusinessServices().getBusinessService().get(x).getBindingTemplates().getBindingTemplate().size(); y++){
177                                                                 beIn.getBusinessServices().getBusinessService().get(x).getBindingTemplates().getBindingTemplate().get(y).
178                                                         }
179                                                 }
180                                         }
181                                 }*/
182                                 publication.saveBusiness(sb);
183                                 logger.info("Saved business with key " + businessName + "-" + i);
184                         }
185 
186                 } catch (Throwable e) {
187                         logger.error(e.getMessage(), e);
188                         Assert.fail("No exception should be thrown " + e.getMessage());
189                 }
190         }
191 
192         public void checkNumberOfServices(String businessKey, int expectedServices) {
193 
194                 try {
195                         GetBusinessDetail gb = new GetBusinessDetail();
196                         gb.getBusinessKey().add(businessKey);
197                         BusinessDetail bd;
198                         bd = inquiry.getBusinessDetail(gb);
199                         List<BusinessEntity> beOutList = bd.getBusinessEntity();
200                         BusinessEntity beOut = beOutList.get(0);
201                         if (expectedServices > 0) {
202                                 assertEquals(expectedServices, beOut.getBusinessServices().getBusinessService().size());
203                         } else {
204                                 Assert.assertNull(beOut.getBusinessServices());
205                         }
206                 } catch (RemoteException e) {
207                         logger.error(e.getMessage(), e);
208                         Assert.fail("No exception should be thrown " + e.getMessage());
209                 }
210         }
211 
212         public void signBusiness(String authInfo, String businessKey) {
213                 try {
214                         GetBusinessDetail gb = new GetBusinessDetail();
215                         gb.getBusinessKey().add(businessKey);
216                         BusinessDetail bd = inquiry.getBusinessDetail(gb);
217                         List<BusinessEntity> beOutList = bd.getBusinessEntity();
218                         BusinessEntity bizEntity = beOutList.get(0);
219                         bizEntity.getSignature().clear();
220                         BusinessEntity bizEntitySigned = signJAXBObject(bizEntity);
221 
222                         SaveBusiness sb = new SaveBusiness();
223                         sb.setAuthInfo(authInfo);
224                         sb.getBusinessEntity().add(bizEntitySigned);
225                         publication.saveBusiness(sb);
226                 } catch (Throwable e) {
227                         logger.error(e.getMessage(), e);
228                         Assert.fail("No exception should be thrown " + e.getMessage());
229                 }
230         }
231 
232         public void verifyBusinessSignature(String authInfo, String businessKey) {
233                 try {
234                         GetBusinessDetail gb = new GetBusinessDetail();
235                         gb.getBusinessKey().add(businessKey);
236                         BusinessDetail bd = inquiry.getBusinessDetail(gb);
237                         List<BusinessEntity> beOutList = bd.getBusinessEntity();
238                         BusinessEntity bizEntity = beOutList.get(0);
239 
240                         boolean sigOk = verifySignedJAXBObject(bizEntity);
241                         assertTrue("Signature invalid!", sigOk);
242                 } catch (Throwable e) {
243                         logger.error(e.getMessage(), e);
244                         Assert.fail("No exception should be thrown " + e.getMessage());
245                 }
246         }
247 
248         private <T> T signJAXBObject(T jaxbObj) {
249                 DOMResult domResult = new DOMResult();
250                 JAXB.marshal(jaxbObj, domResult);
251                 Document doc = ((Document) domResult.getNode());
252                 Element docElement = doc.getDocumentElement();
253 
254                 try {
255                         KeyStore ks = KeyStore.getInstance(SIGNATURE_KEYSTORE_TYPE);
256                         URL url = Thread.currentThread().getContextClassLoader().getResource(SIGNATURE_KEYSTORE);
257                         ks.load(url.openStream(), SIGNATURE_KEYSTORE_PASSWORD.toCharArray());
258                         KeyStore.PrivateKeyEntry keyEntry = (KeyStore.PrivateKeyEntry) ks.getEntry(SIGNATURE_KEYSTORE_ALIAS, new KeyStore.PasswordProtection(SIGNATURE_KEYSTORE_PASSWORD.toCharArray()));
259                         PrivateKey privateKey = keyEntry.getPrivateKey();
260                         Certificate origCert = keyEntry.getCertificate();
261                         PublicKey validatingKey = origCert.getPublicKey();
262                         TckSigningUtil.signDOM(docElement, privateKey, origCert);
263 
264                         DOMSource domSource = new DOMSource(doc);
265                         T result = (T) JAXB.unmarshal(domSource, jaxbObj.getClass());
266                         return result;
267                 } catch (Exception e) {
268                         throw new RuntimeException("Signature failure due to: " + e.getMessage(), e);
269                 }
270         }
271 
272         private boolean verifySignedJAXBObject(Object obj) {
273                 try {
274                         DOMResult domResult = new DOMResult();
275                         JAXB.marshal(obj, domResult);
276                         Document doc = ((Document) domResult.getNode());
277                         Element docElement = doc.getDocumentElement();
278 
279                         KeyStore ks = KeyStore.getInstance(SIGNATURE_KEYSTORE_TYPE);
280                         URL url = Thread.currentThread().getContextClassLoader().getResource(SIGNATURE_KEYSTORE);
281                         ks.load(url.openStream(), SIGNATURE_KEYSTORE_PASSWORD.toCharArray());
282                         KeyStore.PrivateKeyEntry keyEntry = (KeyStore.PrivateKeyEntry) ks.getEntry(SIGNATURE_KEYSTORE_ALIAS, new KeyStore.PasswordProtection(SIGNATURE_KEYSTORE_PASSWORD.toCharArray()));
283                         PrivateKey privateKey = keyEntry.getPrivateKey();
284                         Certificate origCert = keyEntry.getCertificate();
285                         PublicKey validatingKey = origCert.getPublicKey();
286                         return TckSigningUtil.verifySignature(docElement, validatingKey);
287                 } catch (Exception e) {
288                         throw new RuntimeException(e);
289                 }
290         }
291 
292         public BusinessEntity saveBusiness(String authInfo, String businessXML, String businessKey) {
293                 return saveBusiness(authInfo, businessXML, businessKey, false);
294         }
295 
296         public BusinessEntity saveBusiness(String authInfo, String businessXML, String businessKey, boolean serialize) {
297                 logger.info("attempting to save business " + businessKey + " from " + businessXML);
298                 try {
299                         SaveBusiness sb = new SaveBusiness();
300                         sb.setAuthInfo(authInfo);
301                         
302                         BusinessEntity beIn = (BusinessEntity) EntityCreator.buildFromDoc(businessXML, "org.uddi.api_v3");
303                         if (beIn == null) {
304                                 throw new Exception("Unload to load source xml document from " + businessXML);
305                         }
306                         if (serialize) {
307                                 System.out.println("before saving");
308                                 JAXB.marshal(beIn, System.out);
309                         }
310                         sb.getBusinessEntity().add(beIn);
311                         BusinessDetail saveBusiness = publication.saveBusiness(sb);
312                         logger.info("Business saved with key " + saveBusiness.getBusinessEntity().get(0).getBusinessKey());
313                         if (serialize) {
314                                 System.out.println("after saving");
315                                 JAXB.marshal(saveBusiness, System.out);
316                         }
317 
318                         // Now get the entity and check the values
319                         GetBusinessDetail gb = new GetBusinessDetail();
320                         gb.getBusinessKey().add(businessKey);
321                         BusinessDetail bd = inquiry.getBusinessDetail(gb);
322                         List<BusinessEntity> beOutList = bd.getBusinessEntity();
323                         BusinessEntity beOut = beOutList.get(0);
324 
325                         if (serialize) {
326                                 JAXB.marshal(beOut, new File("target/aftersave.xml"));
327                         }
328 
329                         assertEquals(beIn.getBusinessKey(), beOut.getBusinessKey());
330 
331                         TckValidator.checkNames(beIn.getName(), beOut.getName());
332                         TckValidator.checkDescriptions(beIn.getDescription(), beOut.getDescription());
333                         TckValidator.checkDiscoveryUrls(beIn.getDiscoveryURLs(), beOut.getDiscoveryURLs());
334                         TckValidator.checkContacts(beIn.getContacts(), beOut.getContacts());
335                         TckValidator.checkCategories(beIn.getCategoryBag(), beOut.getCategoryBag());
336                         TckValidator.checkSignatures(beIn.getSignature(), beOut.getSignature());
337                         return beOut;
338                 } catch (Throwable e) {
339                         logger.error(e.getMessage(), e);
340                         Assert.fail("No exception should be thrown " + e.getMessage());
341                 }
342                 return null;
343 
344         }
345 
346         public void updateBusiness(String authInfo, String businessXML, String businessKey) {
347                 try {
348 
349                         // Now get the entity and check the values
350                         GetBusinessDetail gb = new GetBusinessDetail();
351                         gb.getBusinessKey().add(businessKey);
352                         BusinessDetail bd = inquiry.getBusinessDetail(gb);
353                         List<BusinessEntity> beOutList = bd.getBusinessEntity();
354                         BusinessEntity beOut = beOutList.get(0);
355                         //We are expecting 2 services
356                         assertEquals(2, beOut.getBusinessServices().getBusinessService().size());
357 
358                         //Now updating the business by adding another description
359                         SaveBusiness sb = new SaveBusiness();
360                         sb.setAuthInfo(authInfo);
361                         BusinessEntity beIn = beOut;
362                         Description desc2 = new Description();
363                         desc2.setLang("nl");
364                         desc2.setValue("Omschrijving");
365                         beIn.getDescription().add(desc2);
366                         sb.getBusinessEntity().add(beIn);
367                         publication.saveBusiness(sb);
368 
369                         // Now get the entity and check the values
370                         BusinessDetail bdnew = inquiry.getBusinessDetail(gb);
371                         List<BusinessEntity> beOutListNew = bdnew.getBusinessEntity();
372                         BusinessEntity beOutNew = beOutListNew.get(0);
373 
374                         assertEquals(beIn.getBusinessKey(), beOutNew.getBusinessKey());
375                         // After the update we still are supposed to see two services.
376                         assertNotNull(beOutNew.getBusinessServices());
377                         assertEquals(2, beOutNew.getBusinessServices().getBusinessService().size());
378 
379                 } catch (Throwable e) {
380                         logger.error(e.getMessage(), e);
381                         Assert.fail("No exception should be thrown " + e.getMessage());
382                 }
383 
384         }
385 
386         public void deleteBusinesses(String authInfo, String businessXML, String businessKey, int numberOfCopies) {
387                 try {
388                         for (int i = 0; i < numberOfCopies; i++) {
389                                 // Delete the entity and make sure it is removed
390                                 String key = businessKey + "-" + i;
391                                 DeleteBusiness db = new DeleteBusiness();
392                                 db.setAuthInfo(authInfo);
393                                 db.getBusinessKey().add(key);
394                                 publication.deleteBusiness(db);
395                                 logger.debug("Deleted business with key " + key);
396                         }
397 
398                 } catch (Exception e) {
399                         logger.error(e.getMessage(), e);
400                         Assert.fail("No exception should be thrown " + e.getMessage());
401                 }
402         }
403 
404         public void deleteBusiness(String authInfo, String businessXML, String businessKey) {
405                 try {
406                         // Delete the entity and make sure it is removed
407                         DeleteBusiness db = new DeleteBusiness();
408                         db.setAuthInfo(authInfo);
409                         db.getBusinessKey().add(businessKey);
410                         publication.deleteBusiness(db);
411 
412                 } catch (Exception e) {
413                         logger.error(e.getMessage(), e);
414                         Assert.fail("No exception should be thrown " + e.getMessage());
415                 }
416         }
417 }