View Javadoc
1   package org.apache.juddi.v3.tck;
2   
3   /*
4    * Copyright 2001-2009 The Apache Software Foundation.
5    * 
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  import java.util.ArrayList;
18  import java.util.List;
19  
20  import javax.xml.ws.Holder;
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_v3.BindingTemplate;
27  import org.uddi.api_v3.DeleteBinding;
28  import org.uddi.api_v3.SaveBinding;
29  import org.uddi.api_v3.SaveService;
30  import org.uddi.api_v3.ServiceDetail;
31  import org.uddi.sub_v3.DeleteSubscription;
32  import org.uddi.sub_v3.Subscription;
33  import org.uddi.v3_service.UDDIPublicationPortType;
34  import org.uddi.v3_service.UDDISubscriptionPortType;
35  
36  /**
37   * @author <a href="mailto:tcunning@apache.org">Tom Cunningham</a>
38   */
39  public class TckSubscriptionListener {
40  
41          /**
42           * uddi_data/subscriptionnotifier/listenerService.xml
43           */
44          public static final String LISTENER_HTTP_SERVICE_XML = "uddi_data/subscriptionnotifier/listenerService.xml";
45          public static final String LISTENER_SMTP_SERVICE_XML = "uddi_data/subscriptionnotifier/listenerServiceSMTP.xml";
46          public static final String LISTENER_SMTP_SERVICE_EXTERNAL_XML = "uddi_data/subscriptionnotifier/listenerServiceSMTP-external.xml";
47          public static final String LISTENER_RMI_SERVICE_XML = "uddi_data/subscriptionnotifier/listenerServiceRMI.xml";
48          public static final String LISTENER_SERVICE_KEY = "uddi:uddi.joepublisher.com:listenerone";
49          /**
50           * find service http note that the subscription1.xml contains the
51           * binding template for the UDDI server to call back into
52           * "uddi_data/subscriptionnotifier/subscription1.xml"
53           */
54          public static final String SUBSCRIPTION_XML = "uddi_data/subscriptionnotifier/subscription1.xml";
55          /**
56           * find business http note that the subscription1.xml contains the
57           * binding template for the UDDI server to call back into
58           */
59          public static final String SUBSCRIPTION2_XML = "uddi_data/subscriptionnotifier/subscription2.xml";
60          /**
61           * find business smtp
62           */
63          public static final String SUBSCRIPTION2_SMTP_XML = "uddi_data/subscriptionnotifier/subscription2SMTP.xml";
64          /**
65           * find service smtp
66           * "uddi_data/subscriptionnotifier/subscription1SMTP.xml"
67           */
68          public static final String SUBSCRIPTION_SMTP_XML = "uddi_data/subscriptionnotifier/subscription1SMTP.xml";
69          /**
70           * find tmodel http note that the subscription1.xml contains the binding
71           * template for the UDDI server to call back into
72           */
73          public static final String SUBSCRIPTION3_XML = "uddi_data/subscriptionnotifier/subscription3.xml";
74          /**
75           * find tmodel smtp
76           */
77          public static final String SUBSCRIPTION3_SMTP_XML = "uddi_data/subscriptionnotifier/subscription3SMTP.xml";
78          public static final String SUBSCRIPTION_KEY = "uddi:uddi.joepublisher.com:subscriptionone";
79          public static final String SUBSCRIPTION_SMTP_KEY = "uddi:uddi.joepublisher.com:subscriptiononesmtp";
80          private Log logger = LogFactory.getLog(this.getClass());
81          private UDDIPublicationPortType publication = null;
82          private UDDISubscriptionPortType subscription = null;
83          private SaveService ss = null;
84  
85          public TckSubscriptionListener(
86               UDDISubscriptionPortType subscription,
87               UDDIPublicationPortType publication) {
88                  super();
89                  this.subscription = subscription;
90                  this.publication = publication;
91          }
92  
93          public void saveNotifierBinding(String authInfo, String bindingXML, String bindingKey, Integer port) {
94                  try {
95                          SaveBinding sb = new SaveBinding();
96                          sb.setAuthInfo(authInfo);
97                          BindingTemplate btIn = (BindingTemplate) EntityCreator.buildFromDoc(bindingXML, "org.uddi.api_v3");
98                          String value = btIn.getAccessPoint().getValue();
99                          value = value.replace("{randomPort}", port.toString());
100                         sb.getBindingTemplate().add(btIn);
101                         publication.saveBinding(sb);
102                 } catch (Exception e) {
103                         logger.error(e.getMessage(), e);
104                         Assert.fail("No exception should be thrown: " + e.getMessage());
105                 }
106         }
107 
108         public void deleteBinding(String authInfo, String bindingKey) {
109                 try {
110                         // Delete the entity and make sure it is removed
111                         DeleteBinding db = new DeleteBinding();
112                         db.setAuthInfo(authInfo);
113 
114                         db.getBindingKey().add(bindingKey);
115                         publication.deleteBinding(db);
116                 } catch (Exception e) {
117                         logger.error(e.getMessage(), e);
118                         Assert.fail("No exception should be thrown.");
119                 }
120 
121         }
122 
123         /**
124          *
125          * @param authInfo
126          * @param listenerService
127          * @param port
128          * @param hostname
129          * @return the first binding key if it exists
130          */
131         public String saveService(String authInfo, String listenerService, Integer port, String hostname) {
132                 try {
133                         // First save the entity
134                         ss = new SaveService();
135                         ss.setAuthInfo(authInfo);
136 
137                         org.uddi.api_v3.BusinessService bsIn = (org.uddi.api_v3.BusinessService) EntityCreator.buildFromDoc(listenerService, "org.uddi.api_v3");
138                         // if (port > 0) {
139                         for (BindingTemplate btIn : bsIn.getBindingTemplates().getBindingTemplate()) {
140                                 String value = btIn.getAccessPoint().getValue();
141                                 value = value.replace("{randomPort}", port.toString());
142                                 value = value.replace("{hostname}", hostname);
143                                 btIn.getAccessPoint().setValue(value);
144                                 logger.info("service " + bsIn.getServiceKey() + " access point " + btIn.getAccessPoint().getValue() + " key:" + btIn.getBindingKey());
145                         }
146                         //}
147                         ss.getBusinessService().add(bsIn);
148                         ServiceDetail bsOut = publication.saveService(ss);
149                         Assert.assertNotNull(bsOut);
150                         Assert.assertEquals(bsOut.getBusinessService().get(0).getBusinessKey(), bsIn.getBusinessKey());
151                         if (bsIn.getBindingTemplates() != null) {
152                                 Assert.assertNotNull(bsOut.getBusinessService().get(0).getBindingTemplates());
153                                 Assert.assertEquals(bsOut.getBusinessService().get(0).getBindingTemplates().getBindingTemplate().size(), bsIn.getBindingTemplates().getBindingTemplate().size());
154                                 for (int i = 0; i < bsIn.getBindingTemplates().getBindingTemplate().size(); i++) {
155                                         Assert.assertEquals(bsIn.getBindingTemplates().getBindingTemplate().get(i).getAccessPoint().getValue(),
156                                              bsOut.getBusinessService().get(0).getBindingTemplates().getBindingTemplate().get(i).getAccessPoint().getValue());
157 
158                                 }
159                         }
160                         Assert.assertEquals(bsOut.getBusinessService().get(0).getServiceKey(), bsIn.getServiceKey());
161                         logger.info("service " + bsIn.getServiceKey() + " saved");
162                         if (bsOut.getBusinessService().get(0).getBindingTemplates() != null && !bsOut.getBusinessService().get(0).getBindingTemplates().getBindingTemplate().isEmpty()) {
163                                 return bsOut.getBusinessService().get(0).getBindingTemplates().getBindingTemplate().get(0).getBindingKey();
164                         }
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         /**
174          * returns subscription key
175          *
176          * @param authInfo
177          * @param subscriptionXML
178          * @return
179          */
180         public String saveNotifierSubscription(String authInfo, String subscriptionXML) {
181                 return saveSubscription(authInfo, subscriptionXML, SUBSCRIPTION_KEY);
182         }
183 
184         public void deleteNotifierSubscription(String authInfo, String subscriptionKey) {
185                 deleteSubscription(authInfo, subscriptionKey);
186         }
187 
188         /**
189          * returns subscription key
190          *
191          * @param authInfo
192          * @param subscriptionXML
193          * @param subscriptionKey
194          * @return
195          */
196         public String saveSubscription(String authInfo, String subscriptionXML, String subscriptionKey) {
197                 try {
198                         Subscription subIn = (Subscription) EntityCreator.buildFromDoc(subscriptionXML, "org.uddi.sub_v3");
199                         logger.info("saving subscription " + subscriptionKey + " " + subscriptionXML + " " + subIn.getBindingKey());
200                         if (subscriptionKey!=null && !subscriptionKey.equalsIgnoreCase(subIn.getSubscriptionKey()))
201                                 logger.warn("subscription key mismatch");
202                         List<Subscription> subscriptionList = new ArrayList<Subscription>();
203                         subscriptionList.add(subIn);
204                         Holder<List<Subscription>> subscriptionHolder = new Holder<List<Subscription>>();
205                         subscriptionHolder.value = subscriptionList;
206 
207                         subscription.saveSubscription(authInfo, subscriptionHolder);
208 
209                         Subscription subDirectOut = subscriptionHolder.value.get(0);
210                         Assert.assertEquals(subIn.getSubscriptionKey(), subDirectOut.getSubscriptionKey());
211 
212                         List<Subscription> outSubscriptionList = subscription.getSubscriptions(authInfo);
213                         Assert.assertNotNull(outSubscriptionList);
214                         Subscription subOut = outSubscriptionList.get(0);
215 
216                         Assert.assertEquals(subDirectOut.getExpiresAfter().getMonth(), subOut.getExpiresAfter().getMonth());
217                         Assert.assertEquals(subDirectOut.getExpiresAfter().getDay(), subOut.getExpiresAfter().getDay());
218                         Assert.assertEquals(subDirectOut.getExpiresAfter().getYear(), subOut.getExpiresAfter().getYear());
219 
220                         return subDirectOut.getSubscriptionKey();
221                         //assertEquals(subIn.getSubscriptionFilter().getFindService().getName().get(0).getValue(), 
222                         //			 subOut.getSubscriptionFilter().getFindService().getName().get(0).getValue());
223 
224                 } catch (Exception e) {
225                         logger.error(e.getMessage(), e);
226                         Assert.fail("No exception should be thrown");
227                 }
228                 return null;
229         }
230 
231         public void deleteSubscription(String authInfo, String subscriptionKey) {
232                 try {
233                         // Delete the entity and make sure it is removed
234                         DeleteSubscription ds = new DeleteSubscription();
235                         ds.setAuthInfo(authInfo);
236 
237                         ds.getSubscriptionKey().add(subscriptionKey);
238                         subscription.deleteSubscription(ds);
239                 } catch (Exception e) {
240                         logger.error(e.getMessage(), e);
241                         Assert.fail("No exception should be thrown.");
242                 }
243         }
244 }