This project has retired. For details please refer to its Attic page.
API_091_RMISubscriptionListenerIntegrationTest 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.net.URI;
18  import java.rmi.AccessException;
19  import java.rmi.NotBoundException;
20  import java.rmi.RemoteException;
21  import java.rmi.registry.LocateRegistry;
22  import java.rmi.registry.Registry;
23  import java.util.Iterator;
24  import java.util.Map;
25  import java.util.Random;
26  
27  import org.apache.commons.configuration.ConfigurationException;
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.apache.juddi.api.util.SubscriptionListenerQuery;
31  import org.apache.juddi.v3.tck.TckBusiness;
32  import org.apache.juddi.v3.tck.TckBusinessService;
33  import org.apache.juddi.v3.tck.TckPublisher;
34  import org.apache.juddi.v3.tck.TckSecurity;
35  import org.apache.juddi.v3.tck.TckSubscriptionListener;
36  import org.apache.juddi.v3.tck.TckSubscriptionListenerRMI;
37  import org.apache.juddi.v3.tck.TckTModel;
38  import org.apache.juddi.v3.tck.UDDISubscriptionListenerImpl;
39  import org.junit.AfterClass;
40  import org.junit.Assert;
41  import org.junit.BeforeClass;
42  import org.junit.Test;
43  import org.uddi.v3_service.UDDISecurityPortType;
44  
45  /**
46   * @author <a href="mailto:tcunning@apache.org">Tom Cunningham</a>
47   */
48  public class API_091_RMISubscriptionListenerIntegrationTest
49  {
50  	
51  	private static Log logger = LogFactory.getLog(API_090_SubscriptionListenerIntegrationTest.class);
52  	private static API_010_PublisherTest api010      = new API_010_PublisherTest();
53  	private static TckTModel tckTModel               = new TckTModel(new UDDIPublicationImpl(), new UDDIInquiryImpl());
54  	private static TckBusiness tckBusiness           = new TckBusiness(new UDDIPublicationImpl(), new UDDIInquiryImpl());
55  	private static TckBusinessService tckBusinessService  = new TckBusinessService(new UDDIPublicationImpl(), new UDDIInquiryImpl());
56  	private static TckSubscriptionListenerRMI tckSubscriptionListenerRMI = new TckSubscriptionListenerRMI(new UDDISubscriptionImpl(), new UDDIPublicationImpl());
57  	
58  	private static String authInfoJoe = null;
59  	//private static UDDIClient manager;
60  	private static UDDISubscriptionListenerImpl rmiSubscriptionListenerService = null;
61  	private static Registry registry;
62  	private static String path = null;
63  	private static Integer randomPort = null;
64  
65  	@AfterClass
66  	public static void stopManager() throws ConfigurationException, AccessException, RemoteException, NotBoundException {
67  		//manager.stop();
68  		//shutting down the TCK SubscriptionListener
69  		registry.unbind(path);
70  		org.apache.juddi.Registry.stop();
71  	}
72  	
73  	@BeforeClass
74  	public static void startManager() throws ConfigurationException {
75  		org.apache.juddi.Registry.start();
76  		try {
77  			//random port
78  			randomPort = 19800 + new Random().nextInt(99);
79  			System.out.println("RMI Random port=" + randomPort);
80  			//bring up the RMISubscriptionListener
81  			URI rmiEndPoint = new URI("rmi://localhost:" + randomPort + "/tck/rmisubscriptionlistener");
82  			registry = LocateRegistry.createRegistry(rmiEndPoint.getPort());
83  			path = rmiEndPoint.getPath();
84  			
85  			//starting the service
86  			rmiSubscriptionListenerService = new UDDISubscriptionListenerImpl(0);
87  			//binding to the RMI Registry
88  			registry.bind(path,rmiSubscriptionListenerService);
89  			
90  			//double check that the service is bound in the local Registry
91  			Registry registry2 = LocateRegistry.getRegistry(rmiEndPoint.getHost(), rmiEndPoint.getPort());
92  			registry2.lookup(rmiEndPoint.getPath());
93             
94              
95  		} catch (Exception e2) {
96  			e2.printStackTrace();
97  			Assert.fail();
98  		}
99  
100 		//manager  = new UDDIClient();
101 		//manager.start();
102 		
103 		logger.debug("Getting auth tokens..");
104 		try {
105 			 
106 			 api010.saveJoePublisher();
107 			 UDDISecurityPortType security      = new UDDISecurityImpl();
108         	 authInfoJoe = TckSecurity.getAuthToken(security, TckPublisher.getJoePublisherId(),  TckPublisher.getJoePassword());
109         	 Assert.assertNotNull(authInfoJoe);	
110         	  
111 	     } catch (Exception e) {
112 	    	 logger.error(e.getMessage(), e);
113 				Assert.fail("Could not obtain authInfo token.");
114 	     } 
115 	}
116 	
117 	@Test
118 	public void joePublisher() {
119 		try {
120                         UDDISubscriptionListenerImpl.notifcationMap.clear();
121                         UDDISubscriptionListenerImpl.notificationCount=0;
122 			tckTModel.saveJoePublisherTmodel(authInfoJoe);
123 			tckBusiness.saveJoePublisherBusiness(authInfoJoe);
124 			tckBusinessService.saveJoePublisherService(authInfoJoe);
125 			//Saving the Listener Service
126 			tckSubscriptionListenerRMI.saveService(authInfoJoe, TckSubscriptionListener.LISTENER_RMI_SERVICE_XML, randomPort, "localhost");
127 			//Saving the Subscription
128 			tckSubscriptionListenerRMI.saveNotifierSubscription(authInfoJoe, TckSubscriptionListenerRMI.SUBSCRIPTION_XML_RMI);
129 			//Changing the service we subscribed to "JoePublisherService"
130 			tckBusinessService.updateJoePublisherService(authInfoJoe, "foo");
131 			
132             //waiting up to 100 seconds for the listener to notice the change.
133                         boolean found=API_090_SubscriptionListenerIntegrationTest.verifyDelivery("Service One");
134                         if (!found)
135                         {
136                                 logger.warn("The test failed, dumping any received notifications");
137                                 Iterator<Map.Entry<Integer, String>> iterator = UDDISubscriptionListenerImpl.notifcationMap.entrySet().iterator();
138                                 while (iterator.hasNext()){
139                                         logger.info("Notification RX: " +iterator.next().getValue());
140                                 }
141                         }
142                         Assert.assertTrue("Notification failed " +UDDISubscriptionListenerImpl.notificationCount + " items returned" , found);
143 			/*String test="";
144 			for (int i=0; i<200; i++) {
145 				Thread.sleep(500);
146 				System.out.print(".");
147 				if (UDDISubscriptionListenerImpl.notificationCount > 0) {
148 					break;
149 				} else {
150 					System.out.print(test);
151 				}
152 			}
153 			if (UDDISubscriptionListenerImpl.notificationCount == 0) {
154 				Assert.fail("No Notification was sent");
155 			}
156 			if (!UDDISubscriptionListenerImpl.notifcationMap.get(0).contains("foo")) {
157 				Assert.fail("Notification does not contain the correct service");
158 			}*/
159 			
160 		} catch (Exception e) {
161 			e.printStackTrace();
162 
163 			Assert.fail();
164 		} finally {
165 			
166 			tckSubscriptionListenerRMI.deleteNotifierSubscription(authInfoJoe, TckSubscriptionListenerRMI.SUBSCRIPTION_KEY_RMI);
167 			tckBusinessService.deleteJoePublisherService(authInfoJoe);
168 			tckBusiness.deleteJoePublisherBusiness(authInfoJoe);
169 			tckTModel.deleteJoePublisherTmodel(authInfoJoe);
170 		}
171 	}	
172     
173 }