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 java.util.HashSet;
18  import java.util.Iterator;
19  import static junit.framework.Assert.assertEquals;
20  
21  import java.util.List;
22  import java.util.Set;
23  import javax.xml.bind.JAXB;
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.DeleteTModel;
30  import org.uddi.api_v2.FindTModel;
31  import org.uddi.api_v2.GetTModelDetail;
32  import org.uddi.api_v2.SaveTModel;
33  import org.uddi.api_v2.TModel;
34  import org.uddi.api_v2.TModelDetail;
35  import org.uddi.api_v2.TModelList;
36  import org.uddi.v2_service.Inquire;
37  import org.uddi.v2_service.Publish;
38  
39  /**
40   * @author <a href="mailto:kstam@apache.org">Kurt T Stam</a>
41   * @author <a href="mailto:jfaath@apache.org">Jeff Faath</a>
42   * @author <a href="mailto:alexoree@apache.org">Alex O'Ree</a>
43   */
44  public class TckTModel {
45  
46          public static final String JOE_PUBLISHER_TMODEL_XML = "uddi_data_v2/joepublisher/tModelKeyGen.xml";
47          
48          
49          final static String JOE_PUBLISHER_TMODEL_KEY = "uuid:c537997c-e082-461d-94e6-ec7935a3b18e";
50          final static String MARY_PUBLISHER_TMODEL_XML = "uddi_data_v2/marypublisher/tModelKeyGen.xml";
51          
52          final static String MARY_PUBLISHER_TMODEL_KEY = "uuid:333f6466-3de7-4532-849f-7354eb842e6a";
53          final static String SAM_SYNDICATOR_TMODEL_XML = "uddi_data_v2/samsyndicator/tModelKeyGen.xml";
54          
55          final static String SAM_SYNDICATOR_TMODEL_KEY = "uuid:c1e003d3-cb43-42f0-905f-aa8a92870bf6";
56          final static String TMODEL_PUBLISHER_TMODEL_XML = "uddi_data_v2/tmodels/tModelKeyGen.xml";
57          final static String TMODEL_PUBLISHER_TMODEL_KEY = "uuid:0ebf9f1d-4b81-4554-87b0-f4af21b6b569";
58          
59          
60          final static String FIND_TMODEL_XML = "uddi_data_v2/find/findTModel1.xml";
61          final static String FIND_TMODEL_XML_BY_CAT = "uddi_data_v2/find/findTModelByCategoryBag.xml";
62          public final static String TMODELS_XML = "uddi_data_v2/tmodels/tmodels.xml";
63          
64          private Log logger = LogFactory.getLog(this.getClass());
65          private Publish publication = null;
66          private Inquire inquiry = null;
67          private Set<String> keyscreated = new HashSet<String>();
68  
69          public TckTModel(Publish publication,
70               Inquire inquiry) {
71                  super();
72                  this.publication = publication;
73                  this.inquiry = inquiry;
74          }
75  
76          /**
77           * saves a tmodel using the tModelXml parameter as a file path
78           *
79           * @param authInfo
80           * @param tModelXml this is a relative file path
81           */
82          public void saveTModels(String authInfo, String tModelXml) {
83  
84                  // Add tModels
85                  try {
86                          SaveTModel st = (org.uddi.api_v2.SaveTModel) EntityCreator.buildFromDoc(tModelXml, "org.uddi.api_v2");
87                          st.setGeneric("2.0");
88  
89                          for (int i = 0; i < st.getTModel().size(); i++) {
90                                   saveTModel(authInfo, st.getTModel().get(i), false);
91                          }
92                          //st.setAuthInfo(authInfo);
93                          //publication.saveTModel(st);
94  
95                  } catch (Exception e) {
96                          logger.error(e.getMessage(), e);
97                          Assert.fail("No exception should be thrown");
98                  }
99          }
100 
101         private TModel saveTModel(String authInfo, TModel tmIn, boolean force) {
102                 boolean exists = false;
103                 GetTModelDetail gt1 = new GetTModelDetail();
104                 gt1.setGeneric("2.0");
105                 gt1.getTModelKey().add(tmIn.getTModelKey());
106                 try {
107                         TModelDetail td1 = inquiry.getTModelDetail(gt1);
108                         if (td1 != null && !td1.getTModel().isEmpty()) {
109                                         exists = true;
110                         }
111                 } catch (Exception ex) {
112                 }
113 
114                 if (!exists || force) // Add the tModel
115                 {
116                         try {
117                                 SaveTModel st = new SaveTModel();
118                                 st.setAuthInfo(authInfo);
119 
120                                 st.setGeneric("2.0");
121                                 st.getTModel().add(tmIn);
122                                 publication.saveTModel(st);
123 
124                                 keyscreated.add(tmIn.getTModelKey());
125                                 // Now get the entity and check the values
126                                 GetTModelDetail gt = new GetTModelDetail();
127                                 gt.setGeneric("2.0");
128                                 gt.getTModelKey().add(tmIn.getTModelKey());
129                                 TModelDetail td = inquiry.getTModelDetail(gt);
130                                 List<org.uddi.api_v2.TModel> tmOutList = td.getTModel();
131                                 org.uddi.api_v2.TModel tmOut = tmOutList.get(0);
132 
133                                 assertEquals(tmIn.getTModelKey().toLowerCase(), tmOut.getTModelKey());
134                                 assertEquals(tmIn.getName().getLang(), tmOut.getName().getLang());
135                                 assertEquals(tmIn.getName().getValue(), tmOut.getName().getValue());
136                                 TckValidator.checkDescriptions(tmIn.getDescription(), tmOut.getDescription());
137                                 TckValidator.checkCategories(tmIn.getCategoryBag(), tmOut.getCategoryBag());
138                                 
139                                         TckValidator.checkOverviewDocs(tmIn.getOverviewDoc(), tmOut.getOverviewDoc());
140                                 
141                                 logger.info("The TModel " + tmIn.getTModelKey() + " saved");
142 
143                                 if (TckCommon.isDebug()) {
144                                         JAXB.marshal(tmOut, System.out);
145                                 }
146                                 return tmOut;
147 
148                         } catch (Exception e) {
149                                 logger.error(e.getMessage(), e);
150                                 Assert.fail("No exception should be thrown");
151                         }
152 
153                 } else {
154                         logger.info("The TModel " + tmIn.getTModelKey() + " exists already, skipping");
155                 }
156                 return tmIn;
157         }
158 
159         public TModel saveTModel(String authInfo, String tModelXml, String tModelKey) {
160                 return saveTModel(authInfo, tModelXml, tModelKey, false);
161         }
162 
163         public TModel saveTModel(String authInfo, String tModelXml, String tModelKey, boolean force) {
164                 logger.info("Loading tModel from " + tModelXml);
165                 org.uddi.api_v2.TModel tmIn = null;
166                 try {
167                         tmIn = (org.uddi.api_v2.TModel) EntityCreator.buildFromDoc(tModelXml, "org.uddi.api_v2");
168                 } catch (Exception ex) {
169                         Assert.fail("unable to load tmodel from file!");
170                 }
171                 if (tmIn == null) {
172                         Assert.fail("unable to load tmodel from file!");
173                 }
174                 return saveTModel(authInfo, tmIn, force);
175         }
176 
177         public synchronized void deleteTModel(String authInfo, String tModelXml, String tModelKey, boolean force) {
178                 if (keyscreated.contains(tModelKey) || force) {
179                         try {
180                                 keyscreated.remove(tModelKey);
181                                 //Now deleting the TModel
182                                 // Delete the entity and make sure it is removed
183                                 DeleteTModel dt = new DeleteTModel();
184                                 dt.setGeneric("2.0");
185                                 dt.setAuthInfo(authInfo);
186 
187                                 logger.info("deleting tmodel " + tModelKey);
188                                 dt.getTModelKey().add(tModelKey);
189                                 publication.deleteTModel(dt);
190 
191                         } catch (Exception e) {
192                                 logger.error(e.getMessage(), e);
193                                 Assert.fail("No exception should be thrown");
194                         }
195                 } else {
196                         logger.info("skipping the deletion of tmodel " + tModelKey + " since it wasn't created by the tck");
197                 }
198         }
199 
200         public synchronized void deleteTModel(String authInfo, String tModelXml, String tModelKey) {
201 
202                 deleteTModel(authInfo, tModelXml, tModelKey, false);
203         }
204 
205         public TModelDetail getTModelDetail(String authInfo, String tModelXml, String tModelKey) {
206                 try {
207                         //Try to get the TModel
208                         GetTModelDetail tmodelDetail = new GetTModelDetail();
209                         tmodelDetail.setGeneric("2.0");
210                         //tmodelDetail.setAuthInfo(authInfo);
211                         tmodelDetail.getTModelKey().add(tModelKey);
212 
213                         return inquiry.getTModelDetail(tmodelDetail);
214 
215                 } catch (Exception e) {
216                         logger.error(e.getMessage(), e);
217                         Assert.fail("No exception should be thrown");
218                 }
219                 Assert.fail("We should already have returned");
220                 return null;
221         }
222 
223         public TModelList findJoeTModelDetail() {
224                 try {
225 
226                         FindTModel body = (FindTModel) EntityCreator.buildFromDoc(FIND_TMODEL_XML, "org.uddi.api_v2");
227                         body.setGeneric("2.0");
228                         TModelList result = inquiry.findTModel(body);
229 
230                         return result;
231 
232                 } catch (Exception e) {
233                         logger.error(e.getMessage(), e);
234                         Assert.fail("No exception should be thrown");
235                 }
236                 Assert.fail("We should already have returned");
237                 return null;
238         }
239 
240         public TModelList findJoeTModelDetailByCategoryBag() {
241                 try {
242 
243                         FindTModel body = (FindTModel) EntityCreator.buildFromDoc(FIND_TMODEL_XML_BY_CAT, "org.uddi.api_v2");
244                         TModelList result = inquiry.findTModel(body);
245 
246                         return result;
247 
248                 } catch (Exception e) {
249                         logger.error(e.getMessage(), e);
250                         Assert.fail("No exception should be thrown");
251                 }
252                 Assert.fail("We should already have returned");
253                 return null;
254         }
255 
256         public TModel saveJoePublisherTmodel(String authInfoJoe) {
257                 return saveTModel(authInfoJoe, JOE_PUBLISHER_TMODEL_XML, JOE_PUBLISHER_TMODEL_KEY, false);
258         }
259 
260         public TModel saveJoePublisherTmodel(String authInfoJoe, boolean force) {
261                 return saveTModel(authInfoJoe, JOE_PUBLISHER_TMODEL_XML, JOE_PUBLISHER_TMODEL_KEY, force);
262         }
263 
264         public void saveUDDIPublisherTmodel(String authInfoTM) {
265                 saveTModel(authInfoTM, TMODEL_PUBLISHER_TMODEL_XML, TMODEL_PUBLISHER_TMODEL_KEY, false);
266         }
267 
268         public void saveTmodels(String authInfoJoe) {
269                 saveTModels(authInfoJoe, TMODELS_XML);
270         }
271 
272         public void deleteJoePublisherTmodel(String authInfoJoe) {
273                 deleteTModel(authInfoJoe, JOE_PUBLISHER_TMODEL_XML, JOE_PUBLISHER_TMODEL_KEY);
274         }
275 
276         public TModelDetail getJoePublisherTmodel(String authInfoJoe) {
277                 return getTModelDetail(authInfoJoe, JOE_PUBLISHER_TMODEL_XML, JOE_PUBLISHER_TMODEL_KEY);
278         }
279 
280         public TModelList findJoePublisherTmodel(String authInfoJoe) {
281                 return findJoeTModelDetail();
282         }
283 
284         public TModel saveMaryPublisherTmodel(String authInfoMary) {
285                 return saveTModel(authInfoMary, MARY_PUBLISHER_TMODEL_XML, MARY_PUBLISHER_TMODEL_KEY, false);
286         }
287 
288         public void deleteMaryPublisherTmodel(String authInfoMary) {
289                 deleteTModel(authInfoMary, MARY_PUBLISHER_TMODEL_XML, MARY_PUBLISHER_TMODEL_KEY);
290         }
291 
292         public TModel saveSamSyndicatorTmodel(String authInfoSam) {
293                 return saveTModel(authInfoSam, SAM_SYNDICATOR_TMODEL_XML, SAM_SYNDICATOR_TMODEL_KEY, false);
294         }
295 
296         public void deleteSamSyndicatorTmodel(String authInfoSam) {
297                 deleteTModel(authInfoSam, SAM_SYNDICATOR_TMODEL_XML, SAM_SYNDICATOR_TMODEL_KEY);
298         }
299 
300         /**
301          * deletes at tmodels created usign the tck tool, tmodels that were
302          * previously present (before running) are not deleted no exception is
303          * thrown if an error occurs, but it will be logged
304          *
305          * @param authinfo
306          */
307         public void deleteCreatedTModels(String authinfo) {
308                 if (this.keyscreated != null) {
309                         Iterator<String> iterator = keyscreated.iterator();
310                         while (iterator.hasNext()) {
311                                 DeleteTModel dtm = new DeleteTModel();
312                                 dtm.setGeneric("2.0");
313                                 dtm.setAuthInfo(authinfo);
314                                 String s = iterator.next();
315                                 logger.info("cleanup tModel " + s);
316                                 dtm.getTModelKey().add(s);
317                                 try {
318                                         publication.deleteTModel(dtm);
319                                 } catch (Exception ex) {
320                                         logger.warn("failed to delete tmodel " + s + " " + ex.getMessage());
321                                         logger.debug("failed to delete tmodel " + s + " " + ex.getMessage(), ex);
322                                 }
323                         }
324                 }
325 
326         }
327 }
328