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  
18  import static junit.framework.Assert.assertEquals;
19  
20  import java.util.List;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.juddi.jaxb.EntityCreator;
25  import org.junit.Assert;
26  import org.uddi.api_v2.*;
27  import org.uddi.v2_service.Inquire;
28  import org.uddi.v2_service.Publish;
29  /**
30   * @author <a href="mailto:kstam@apache.org">Kurt T Stam</a>
31   * @author <a href="mailto:jfaath@apache.org">Jeff Faath</a>
32   */
33  public class TckBusinessService 
34  {
35  	
36      public final static String JOE_SERVICE_XML = "uddi_data_v2/joepublisher/businessService.xml";
37      public final static String JOE_SERVICE_KEY = "b82739a9-f35c-4331-999a-6bfb2968ca5e";
38      public final static String JOE_SERVICE_XML_2 = "uddi_data_v2/joepublisher/businessService2.xml";
39      public final static String JOE_SERVICE_KEY_2 = "6687b480-c526-4175-b6bf-32a2a8734cec";
40      public final static String SAM_SERVICE_XML = "uddi_data_v2/samsyndicator/businessService.xml";
41      public final static String SAM_SERVICE_KEY = "bc9f7399-bb91-4693-8dc4-67ca38a4734f";
42      public final static String JOE_BINDING_KEY_1="c0af9342-f4f1-4625-b1f4-810fa35ae346";
43      private Log logger = LogFactory.getLog(this.getClass());
44      private Publish publication = null;
45      private Inquire inquiry = null;
46  	
47  	public TckBusinessService(Publish publication, 
48  				  Inquire inquiry) {
49  		super();
50  		this.publication = publication;
51  		this.inquiry = inquiry;
52  	}
53  
54  	public String saveJoePublisherService(String authInfoJoe) {
55  		return saveService(authInfoJoe, JOE_SERVICE_XML, JOE_SERVICE_KEY);
56  	}
57  	
58  	public void updateJoePublisherService(String authInfoJoe, String description) {
59  		saveService(authInfoJoe, JOE_SERVICE_XML, JOE_SERVICE_KEY, description);
60  	}
61  	
62  	public void saveJoePublisherService2(String authInfoJoe) {
63  		saveService2(authInfoJoe, JOE_SERVICE_KEY, JOE_SERVICE_XML_2, JOE_SERVICE_KEY_2);
64  	}
65  
66  	public void saveJoePublisherServices(String authInfoJoe, int businessInt, int numberOfCopies) {
67  		saveServices(authInfoJoe, businessInt, JOE_SERVICE_XML, JOE_SERVICE_KEY, numberOfCopies, TckBusiness.JOE_BUSINESS_KEY);
68  	}
69  	
70  	public void deleteJoePublisherService(String authInfoJoe) {
71  		deleteService(authInfoJoe, JOE_SERVICE_KEY);
72  	}
73  	
74  	public void deleteJoePublisherService2(String authInfoJoe) {
75  		deleteService(authInfoJoe, JOE_SERVICE_KEY_2);
76  	}
77  
78  	
79  	
80  	public void deleteJoePublisherServices(String authInfoJoe, int businessInt, int numberOfCopies) {
81  		deleteServices(authInfoJoe, businessInt,JOE_SERVICE_KEY, numberOfCopies);
82  	}
83  	
84  	public void saveSamSyndicatorService(String authInfoSam) {
85  		saveService(authInfoSam, SAM_SERVICE_XML, SAM_SERVICE_KEY);
86  	}
87  	
88  	public void saveSamSyndicatorServices(String authInfoSam, int businessInt, int numberOfCopies) {
89  		saveServices(authInfoSam, businessInt, SAM_SERVICE_XML, SAM_SERVICE_KEY, numberOfCopies, TckBusiness.SAM_BUSINESS_KEY);
90  	}
91  	
92  	public void deleteSamSyndicatorService(String authInfoSam) {
93  		deleteService(authInfoSam, SAM_SERVICE_KEY);
94  	}
95  	
96  	public void deleteSamSyndicatorServices(String authInfoSam, int businessInt, int numberOfCopies) {
97  		deleteServices(authInfoSam, businessInt, SAM_SERVICE_KEY, numberOfCopies);
98  	}
99  	
100 	public void saveServices(String authInfo, int businessInt, String serviceXML, String serviceKey, int numberOfCopies, String parentBusinessKey) {
101 		SaveService ss = null;
102 		try {
103 			org.uddi.api_v2.BusinessService bsIn = (org.uddi.api_v2.BusinessService)EntityCreator.buildFromDoc(serviceXML, "org.uddi.api_2");
104 			String serviceName = bsIn.getName().get(0).getValue();
105 			String bindingKey = bsIn.getBindingTemplates().getBindingTemplate().get(0).getBindingKey();
106 			for (int i=0; i<numberOfCopies; i++) {
107 			    // save the entity
108 				ss = new SaveService();
109                     ss.setGeneric("2.0");
110 				ss.setAuthInfo(authInfo);
111 				bsIn.getName().get(0).setValue(serviceName + "-" + i);
112 				bsIn.setBusinessKey(parentBusinessKey);
113 				bsIn.setServiceKey(serviceKey + "-" + businessInt + "-" + i);
114 				bsIn.getBindingTemplates().getBindingTemplate().get(0).setBindingKey(bindingKey + "-" + businessInt + "-" + i);
115 				bsIn.getBindingTemplates().getBindingTemplate().get(0).setServiceKey(serviceKey + "-" + businessInt + "-" + i);
116 				ss.getBusinessService().add(bsIn);
117 				
118 				BindingTemplate bt = bsIn.getBindingTemplates().getBindingTemplate().get(0);
119 				if (! bt.getServiceKey().equals(serviceKey + "-" + businessInt + "-" + i)) {
120 					System.out.println("not the same");
121 				}
122 				
123 				publication.saveService(ss);
124 				logger.info("Add service with key " + bsIn.getServiceKey());
125 			}
126 		}
127 		catch(Exception e) {
128 			logger.error(e.getMessage(), e);
129 			Assert.fail("No exception should be thrown.");
130 		}
131 	}
132 	
133 	public String saveService(String authInfo, String serviceXML, String serviceKey) {
134 		return saveService(authInfo, serviceXML, serviceKey, null);
135 	}
136 	
137 	public String saveService(String authInfo, String serviceXML, String serviceKey, String description) {
138 		try {
139                         logger.info("saving service key " + serviceKey + " from " + serviceXML);
140 			// First save the entity
141 			SaveService ss = new SaveService();
142 			ss.setAuthInfo(authInfo);
143 			ss.setGeneric("2.0");
144 			org.uddi.api_v2.BusinessService bsIn = (org.uddi.api_v2.BusinessService)EntityCreator.buildFromDoc(serviceXML, "org.uddi.api_v2");
145 			
146 			if (description!=null) bsIn.getDescription().get(0).setValue("updated description");
147 		    ss.getBusinessService().add(bsIn);
148 			publication.saveService(ss);
149 			
150 			// Now get the entity and check the values
151 			GetServiceDetail gs = new GetServiceDetail();
152 			gs.getServiceKey().add(serviceKey);
153 			ServiceDetail sd = inquiry.getServiceDetail(gs);
154 			List<BusinessService> bsOutList = sd.getBusinessService();
155 			BusinessService bsOut = bsOutList.get(0);
156 
157 			assertEquals(bsIn.getServiceKey().toLowerCase(), bsOut.getServiceKey());
158 			
159 			TckValidator.checkNames(bsIn.getName(), bsOut.getName());
160 			TckValidator.checkDescriptions(bsIn.getDescription(), bsOut.getDescription());
161 			TckValidator.checkBindingTemplates(bsIn.getBindingTemplates(), bsOut.getBindingTemplates());
162 			TckValidator.checkCategories(bsIn.getCategoryBag(), bsOut.getCategoryBag());
163                         if (bsOut.getBindingTemplates()!=null && !bsOut.getBindingTemplates().getBindingTemplate().isEmpty())
164                             return bsOut.getBindingTemplates().getBindingTemplate().get(0).getBindingKey();
165 		}
166 		catch(Exception e) {
167 			logger.error(e.getMessage(), e);
168 			Assert.fail("No exception should be thrown.");
169 		}
170 		return null;
171 	}
172 	
173 	public void saveService2(String authInfo, String serviceKey, String serviceXML2,  String serviceKey2) {
174 		try {
175 			// First save the entity
176 			SaveService ss = new SaveService();
177 			ss.setAuthInfo(authInfo);
178 			
179 			org.uddi.api_v2.BusinessService bsIn = (org.uddi.api_v2.BusinessService)EntityCreator.buildFromDoc(serviceXML2, "org.uddi.api_v2");
180 			ss.getBusinessService().add(bsIn);
181                ss.setGeneric("2.0");
182 			publication.saveService(ss);
183 			
184 			// Now get the entity and check the values
185 			GetServiceDetail gs = new GetServiceDetail();
186 			gs.getServiceKey().add(serviceKey);
187 			gs.getServiceKey().add(serviceKey2);
188 			ServiceDetail sd = inquiry.getServiceDetail(gs);
189 			List<BusinessService> bsOutList = sd.getBusinessService();
190 			int size = bsOutList.size();
191 
192 			assertEquals(2, size);
193 			
194 			
195 		}
196 		catch(Exception e) {
197 			logger.error(e.getMessage(), e);
198 			Assert.fail("No exception should be thrown.");
199 		}
200 		
201 	}
202 	
203 	public void deleteService(String authInfo, String serviceKey) {
204 		try {
205 			// Delete the entity and make sure it is removed
206 			DeleteService ds = new DeleteService();
207 			ds.setAuthInfo(authInfo);
208 			ds.setGeneric("2.0");
209 			ds.getServiceKey().add(serviceKey);
210 			publication.deleteService(ds);
211 		}
212 		catch(Exception e) {
213 			logger.error(e.getMessage(), e);
214 			Assert.fail("No exception should be thrown.");
215 		}
216 	}
217 	
218 	public void deleteServices(String authInfo, int businessInt, String serviceKey, int numberOfCopies) {
219 		try {
220 			for (int i=0; i<numberOfCopies; i++) {
221 				// Delete the entity and make sure it is removed
222 				DeleteService ds = new DeleteService();
223                     ds.setGeneric("2.0");
224 				ds.setAuthInfo(authInfo);
225 				ds.getServiceKey().add(serviceKey + "-" + businessInt + "-" + i);
226 				publication.deleteService(ds);
227 				logger.debug("Deleted Service with key " + businessInt + "-" + serviceKey + "-" + i);
228 			}
229 		}
230 		catch(Exception e) {
231 			logger.error(e.getMessage(), e);
232 			Assert.fail("No exception should be thrown.");
233 		}
234 	}
235 
236 }