This project has retired. For details please refer to its Attic page.
API_090_SubscriptionListenerIntegrationTest xref
View Javadoc
1   package org.apache.juddi.api.impl;
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.Iterator;
18  import java.util.Random;
19  
20  import javax.xml.ws.Endpoint;
21  
22  import org.apache.commons.configuration.ConfigurationException;
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.apache.juddi.Registry;
26  import org.apache.juddi.config.Property;
27  import org.apache.juddi.v3.tck.TckBusiness;
28  import org.apache.juddi.v3.tck.TckBusinessService;
29  import org.apache.juddi.v3.tck.TckPublisher;
30  import org.apache.juddi.v3.tck.TckSecurity;
31  import org.apache.juddi.v3.tck.TckSubscriptionListener;
32  import org.apache.juddi.v3.tck.TckTModel;
33  import org.apache.juddi.v3.tck.UDDISubscriptionListenerImpl;
34  import org.junit.AfterClass;
35  import org.junit.Assert;
36  import org.junit.BeforeClass;
37  import org.junit.Test;
38  import org.uddi.v3_service.UDDISecurityPortType;
39  
40  import com.dumbster.smtp.SimpleSmtpServer;
41  import com.dumbster.smtp.SmtpMessage;
42  import java.util.ArrayList;
43  import java.util.List;
44  import javax.xml.datatype.DatatypeFactory;
45  import javax.xml.ws.Holder;
46  import org.apache.juddi.v3.client.UDDIConstants;
47  import org.apache.juddi.v3.tck.TckCommon;
48  import org.junit.Assume;
49  import org.junit.Ignore;
50  import org.uddi.api_v3.AddPublisherAssertions;
51  import org.uddi.api_v3.BusinessEntity;
52  import org.uddi.api_v3.CompletionStatus;
53  import org.uddi.api_v3.DeletePublisherAssertions;
54  import org.uddi.api_v3.GetAssertionStatusReport;
55  import org.uddi.api_v3.KeyedReference;
56  import org.uddi.api_v3.PublisherAssertion;
57  import org.uddi.sub_v3.DeleteSubscription;
58  import org.uddi.sub_v3.Subscription;
59  import org.uddi.sub_v3.SubscriptionFilter;
60  import org.uddi.v3_service.UDDIInquiryPortType;
61  import org.uddi.v3_service.UDDIPublicationPortType;
62  import org.uddi.v3_service.UDDISubscriptionPortType;
63  
64  /**
65   * @author <a href="mailto:tcunning@apache.org">Tom Cunningham</a>
66   */
67  public class API_090_SubscriptionListenerIntegrationTest {
68  
69          private static final Log logger = LogFactory.getLog(API_090_SubscriptionListenerIntegrationTest.class);
70          private static API_010_PublisherTest api010 = new API_010_PublisherTest();
71          private static TckTModel tckTModel = new TckTModel(new UDDIPublicationImpl(), new UDDIInquiryImpl());
72          static UDDIPublicationPortType publication = new UDDIPublicationImpl();
73          static UDDISubscriptionPortType subscription = new UDDISubscriptionImpl();
74          private static TckBusiness tckBusiness = new TckBusiness(new UDDIPublicationImpl(), new UDDIInquiryImpl());
75          private static TckBusinessService tckBusinessService = new TckBusinessService(new UDDIPublicationImpl(), new UDDIInquiryImpl());
76          private static TckSubscriptionListener tckSubscriptionListener = new TckSubscriptionListener(new UDDISubscriptionImpl(), new UDDIPublicationImpl());
77          private static Endpoint endPoint;
78          private static String authInfoJoe = null;
79          private static String authInfoMary = null;
80          private static SimpleSmtpServer mailServer;
81  
82          private static Integer smtpPort = 25;
83          private static Integer httpPort = 80;
84  
85          @AfterClass
86          public static void stopManager() throws ConfigurationException {
87                  //manager.stop();
88                  //shutting down the TCK SubscriptionListener
89                  endPoint.stop();
90                  Registry.stop();
91                  mailServer.stop();
92          }
93          static String httpEndpoint = null;
94  
95          @BeforeClass
96          public static void startManager() throws ConfigurationException {
97                  Registry.start();
98                  try {
99                          smtpPort = 9700 + new Random().nextInt(99);
100                         httpPort = 9600 + new Random().nextInt(99);
101                         System.setProperty(Property.DEFAULT_JUDDI_EMAIL_PREFIX + "mail.smtp.host", "localhost");
102                         System.setProperty(Property.DEFAULT_JUDDI_EMAIL_PREFIX + "mail.smtp.port", String.valueOf(smtpPort));
103                         System.setProperty(Property.DEFAULT_JUDDI_EMAIL_PREFIX + "mail.smtp.from", "jUDDI@example.org");
104                         mailServer = SimpleSmtpServer.start(smtpPort);
105                         //bring up the TCK HTTP SubscriptionListener
106                         httpEndpoint = "http://localhost:" + httpPort + "/tcksubscriptionlistener";
107                         System.out.println("Bringing up SubscriptionListener endpoint at " + httpEndpoint);
108                         endPoint = Endpoint.publish(httpEndpoint, new UDDISubscriptionListenerImpl());
109                         logger.debug("Getting auth tokens..");
110 
111                         api010.saveJoePublisher();
112                         api010.saveMaryPublisher();
113                         UDDISecurityPortType security = new UDDISecurityImpl();
114                         authInfoJoe = TckSecurity.getAuthToken(security, TckPublisher.getJoePublisherId(), TckPublisher.getJoePassword());
115                         Assert.assertNotNull(authInfoJoe);
116 
117                         authInfoMary = TckSecurity.getAuthToken(security, TckPublisher.getMaryPublisherId(), TckPublisher.getMaryPassword());
118                         Assert.assertNotNull(authInfoMary);
119 
120                 } catch (Exception e) {
121                         logger.error(e.getMessage(), e);
122                         Assert.fail(e.getMessage());
123                 }
124         }
125 
126         @Test
127         public void joePublisherUpdateService_HTTP() {
128                 try {
129                         tckTModel.saveJoePublisherTmodel(authInfoJoe);
130                         tckBusiness.saveJoePublisherBusiness(authInfoJoe);
131                         //Saving the binding template that will be called by the server for a subscription event
132                         tckBusinessService.saveJoePublisherService(authInfoJoe);
133                         //Saving the HTTP Listener Service
134                         tckSubscriptionListener.saveService(authInfoJoe, TckSubscriptionListener.LISTENER_HTTP_SERVICE_XML, httpPort, "localhost");
135                         //Saving the HTTP Subscription
136                         tckSubscriptionListener.saveNotifierSubscription(authInfoJoe, TckSubscriptionListener.SUBSCRIPTION_XML);
137                         //Changing the service we subscribed to "JoePublisherService"
138                         Thread.sleep(1000);
139                         logger.info("Updating Service ********** ");
140                         tckBusinessService.updateJoePublisherService(authInfoJoe, "foo");
141 
142                         //waiting up to 100 seconds for the listener to notice the change.
143                         for (int i = 0; i < 200; i++) {
144                                 Thread.sleep(500);
145                                 System.out.print(".");
146                                 if (UDDISubscriptionListenerImpl.notificationCount > 0) {
147                                         logger.info("Received HTTP Notification");
148                                         break;
149                                 }
150                         }
151                         if (UDDISubscriptionListenerImpl.notificationCount == 0) {
152                                 Assert.fail("No HttpNotification was sent");
153                         }
154                         if (!UDDISubscriptionListenerImpl.notifcationMap.get(0).contains("<name xml:lang=\"en\">Service One</name>")) {
155                                 Assert.fail("Notification does not contain the correct service");
156                         }
157 
158                 } catch (Exception e) {
159                         logger.error("No exceptions please.");
160                         e.printStackTrace();
161 
162                         Assert.fail();
163                 } finally {
164                         tckSubscriptionListener.deleteNotifierSubscription(authInfoJoe, TckSubscriptionListener.SUBSCRIPTION_KEY);
165                         tckBusinessService.deleteJoePublisherService(authInfoJoe);
166                         tckBusiness.deleteJoePublisherBusiness(authInfoJoe);
167                         tckTModel.deleteJoePublisherTmodel(authInfoJoe);
168                 }
169         }
170 
171         @Test
172         public void joePublisherUpdateService_SMTP() {
173                 try {
174                         tckTModel.saveJoePublisherTmodel(authInfoJoe);
175                         tckBusiness.saveJoePublisherBusiness(authInfoJoe);
176                         //Saving the binding template that will be called by the server for a subscription event
177                         tckBusinessService.saveJoePublisherService(authInfoJoe);
178                         //Saving the SMTP Listener Service
179                         tckSubscriptionListener.saveService(authInfoJoe, TckSubscriptionListener.LISTENER_SMTP_SERVICE_XML, 0, "localhost");
180                         //Saving the SMTP Subscription
181                         tckSubscriptionListener.saveNotifierSubscription(authInfoJoe, TckSubscriptionListener.SUBSCRIPTION_SMTP_XML);
182                         //Changing the service we subscribed to "JoePublisherService"
183                         Thread.sleep(1000);
184                         logger.info("Updating Service ********** ");
185                         tckBusinessService.updateJoePublisherService(authInfoJoe, "foo");
186 
187                         //waiting up to 100 seconds for the listener to notice the change.
188                         for (int i = 0; i < 200; i++) {
189                                 Thread.sleep(500);
190                                 System.out.print(".");
191                                 if (mailServer.getReceivedEmailSize() > 0) {
192                                         logger.info("Received Email Notification");
193                                         break;
194                                 }
195                         }
196                         if (mailServer.getReceivedEmailSize() == 0) {
197                                 Assert.fail("No SmtpNotification was sent");
198                         }
199                         @SuppressWarnings("rawtypes")
200                         Iterator emailIter = mailServer.getReceivedEmail();
201                         SmtpMessage email = (SmtpMessage) emailIter.next();
202                         System.out.println("Subject:" + email.getHeaderValue("Subject"));
203                         System.out.println("Body:" + email.getBody());
204 
205                         if (!email.getBody().contains("Service One")) {
206                                 Assert.fail("Notification does not contain the correct service");
207                         }
208 
209                 } catch (Exception e) {
210                         logger.error("No exceptions please.");
211                         e.printStackTrace();
212 
213                         Assert.fail();
214                 } finally {
215                         tckSubscriptionListener.deleteNotifierSubscription(authInfoJoe, TckSubscriptionListener.SUBSCRIPTION_SMTP_KEY);
216                         tckBusinessService.deleteJoePublisherService(authInfoJoe);
217                         tckBusiness.deleteJoePublisherBusiness(authInfoJoe);
218                         tckTModel.deleteJoePublisherTmodel(authInfoJoe);
219                 }
220         }
221 
222         @Test
223         public void joePublisherUpdateBusiness_HTTP() {
224                 try {
225                         tckTModel.saveJoePublisherTmodel(authInfoJoe);
226                         tckBusiness.saveJoePublisherBusiness(authInfoJoe);
227                         tckBusinessService.saveJoePublisherService(authInfoJoe);
228                         //Saving the Listener Service
229                         tckSubscriptionListener.saveService(authInfoJoe, TckSubscriptionListener.LISTENER_HTTP_SERVICE_XML, httpPort, "localhost");
230                         //Saving the Subscription
231                         tckSubscriptionListener.saveNotifierSubscription(authInfoJoe, TckSubscriptionListener.SUBSCRIPTION_XML);
232                         //Changing the service we subscribed to "JoePublisherService"
233                         Thread.sleep(1000);
234                         logger.info("Deleting Business ********** ");
235                         tckBusiness.updateJoePublisherBusiness(authInfoJoe);
236 
237                         //waiting up to 100 seconds for the listener to notice the change.
238                         String test = "";
239                         for (int i = 0; i < 200; i++) {
240                                 Thread.sleep(500);
241                                 System.out.print(".");
242                                 if (UDDISubscriptionListenerImpl.notificationCount > 0) {
243                                         logger.info("Received Notification");
244                                         break;
245                                 } else {
246                                         System.out.print(test);
247                                 }
248                         }
249                         if (UDDISubscriptionListenerImpl.notificationCount == 0) {
250                                 Assert.fail("No Notification was sent");
251                         }
252                         if (!UDDISubscriptionListenerImpl.notifcationMap.get(0).contains("<name xml:lang=\"en\">Service One</name>")) {
253                                 Assert.fail("Notification does not contain the correct service");
254                         }
255 
256                 } catch (Exception e) {
257                         logger.error("No exceptions please.");
258                         e.printStackTrace();
259 
260                         Assert.fail();
261                 } finally {
262                         tckSubscriptionListener.deleteNotifierSubscription(authInfoJoe, TckSubscriptionListener.SUBSCRIPTION_KEY);
263                         tckTModel.deleteJoePublisherTmodel(authInfoJoe);
264                 }
265         }
266 
267         /**
268          * PUBLISHERASSERTION tests joe want's updates on when someone asserts
269          * that they own his business
270          *
271          * @throws Exception
272          */
273         @Test
274         //@Ignore
275         public void joePublisherUpdate_PUBLISHERASSERTION_DETAIL_TO() throws Exception {
276 
277                 logger.info("joePublisherUpdate_HTTP_PUBLISHERASSERTION_DETAIL_TO");
278 
279                 UDDISubscriptionListenerImpl.notifcationMap.clear();
280                 UDDISubscriptionListenerImpl.notificationCount=0;
281                 Holder<List<Subscription>> holder = null;
282                 try {
283 
284                         tckTModel.saveJoePublisherTmodel(authInfoJoe);
285                         //tckTModelJoe.saveTModels(authInfoJoe, TckTModel.JOE_PUBLISHER_TMODEL_XML_SUBSCRIPTION3);
286 
287                         tckTModel.saveMaryPublisherTmodel(authInfoMary);
288                         BusinessEntity saveMaryPublisherBusiness = tckBusiness.saveMaryPublisherBusiness(authInfoMary);
289 
290                         tckBusiness.saveJoePublisherBusiness(authInfoJoe);
291                         tckBusinessService.saveJoePublisherService(authInfoJoe);
292                         //Saving the Listener Service
293                         String bindingkey = tckSubscriptionListener.saveService(authInfoJoe, TckSubscriptionListener.LISTENER_HTTP_SERVICE_XML, httpPort, "localhost");
294 
295                         //Saving the Subscription
296                         holder = new Holder<List<Subscription>>();
297                         holder.value = new ArrayList<Subscription>();
298                         Subscription sub = new Subscription();
299                         sub.setBindingKey(bindingkey);
300                         sub.setNotificationInterval(DatatypeFactory.newInstance().newDuration(5000));
301                         sub.setSubscriptionFilter(new SubscriptionFilter());
302                         sub.getSubscriptionFilter().setGetAssertionStatusReport(new GetAssertionStatusReport());
303                         sub.getSubscriptionFilter().getGetAssertionStatusReport().setCompletionStatus(CompletionStatus.STATUS_FROM_KEY_INCOMPLETE);
304 
305                         holder.value.add(sub);
306                         subscription.saveSubscription(authInfoJoe, holder);
307                         logger.info("subscription saved for " + holder.value.get(0).getSubscriptionKey());
308                         //tckSubscriptionListenerJoe.saveNotifierSubscription(authInfoJoe, TckSubscriptionListener.SUBSCRIPTION3_XML);
309                         //Changing the service we subscribed to "JoePublisherService"
310                         Thread.sleep(1000);
311                         logger.info("saving Mary's publisher assertion********** ");
312                         AddPublisherAssertions pa = new AddPublisherAssertions();
313                         pa.setAuthInfo(authInfoMary);
314                         PublisherAssertion pas = new PublisherAssertion();
315                         pas.setToKey(TckBusiness.MARY_BUSINESS_KEY);
316                         pas.setFromKey(TckBusiness.JOE_BUSINESS_KEY);
317                         pas.setKeyedReference(new KeyedReference(UDDIConstants.RELATIONSHIPS, "parent", "parent-child"));
318                         pa.getPublisherAssertion().add(pas);
319 
320                         publication.addPublisherAssertions(pa);
321                         boolean found = verifyDelivery(TckBusiness.MARY_BUSINESS_KEY);
322 
323                         DeletePublisherAssertions deletePublisherAssertions = new DeletePublisherAssertions();
324                         deletePublisherAssertions.setAuthInfo(authInfoMary);
325                         deletePublisherAssertions.getPublisherAssertion().add(pas);
326                         publication.deletePublisherAssertions(deletePublisherAssertions);
327                         if (!found) {
328 
329                                 Assert.fail("Notification does not contain the correct service.");
330                         }
331 
332                 } catch (Exception e) {
333                         logger.error("No exceptions please.");
334                         e.printStackTrace();
335 
336                         Assert.fail();
337                 } finally {
338                         //tckSubscriptionListenerJoe.deleteNotifierSubscription(authInfoJoe, TckSubscriptionListener.SUBSCRIPTION_KEY);
339                         DeleteSubscription ds = new DeleteSubscription();
340                         ds.setAuthInfo(authInfoJoe);
341                         ds.getSubscriptionKey().add(holder.value.get(0).getSubscriptionKey());
342                         subscription.deleteSubscription(ds);
343                         tckBusiness.deleteMaryPublisherBusiness(authInfoMary);
344                         tckTModel.deleteMaryPublisherTmodel(authInfoMary);
345 
346                         tckBusinessService.deleteJoePublisherService(authInfoJoe);
347                         tckBusiness.deleteJoePublisherBusiness(authInfoJoe);
348                         tckTModel.deleteJoePublisherTmodel(authInfoJoe);
349                         tckTModel.deleteTModel(authInfoJoe, TckTModel.JOE_PUBLISHER_TMODEL_SUBSCRIPTION3_TMODEL_KEY, TckTModel.JOE_PUBLISHER_TMODEL_XML_SUBSCRIPTION3);
350 
351                 }
352         }
353 
354         public static boolean  verifyDelivery(String findMe) {
355                 for (int i = 0; i < TckPublisher.getSubscriptionTimeout(); i++) {
356                         try {
357                                 if (UDDISubscriptionListenerImpl.notificationCount > 0) {
358                                         Iterator<String> it = UDDISubscriptionListenerImpl.notifcationMap.values().iterator();
359                                      
360                                         while (it.hasNext()) {
361                                                 String test = it.next();
362                                                 if (test.toLowerCase().contains(findMe.toLowerCase())) {
363                                                          return true;
364                                                 }
365                                         }
366                                 }
367                                 Thread.sleep(1000);
368                         } catch (Exception ex) {
369                                 ex.printStackTrace();
370                         }
371                         System.out.print(".");
372                         //if (UDDISubscriptionListenerImpl.notificationCount > 0) {                        }
373                 }
374                 logger.info("RX " + UDDISubscriptionListenerImpl.notificationCount + " notifications");
375                 Iterator<String> it = UDDISubscriptionListenerImpl.notifcationMap.values().iterator();
376                 boolean found = false;
377 
378                 while (it.hasNext()) {
379                         String test = it.next();
380                         if (TckCommon.isDebug()) {
381                                 logger.info("Notification: " + test);
382                         }
383                         if (test.toLowerCase().contains(findMe.toLowerCase())) {
384                                 found = true;
385                         }
386                 }
387                 return found;
388         }
389 
390 }