This project has retired. For details please refer to its Attic page.
API_010_PublisherTest xref
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.api.impl;
16  
17  import static junit.framework.Assert.assertEquals;
18  
19  import java.rmi.RemoteException;
20  import java.util.ArrayList;
21  import java.util.Calendar;
22  import java.util.GregorianCalendar;
23  import java.util.List;
24  import javax.xml.datatype.DatatypeFactory;
25  import javax.xml.ws.Holder;
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.Registry;
31  import org.apache.juddi.api_v3.DeletePublisher;
32  import org.apache.juddi.api_v3.GetPublisherDetail;
33  import org.apache.juddi.api_v3.Publisher;
34  import org.apache.juddi.api_v3.PublisherDetail;
35  import org.apache.juddi.api_v3.SavePublisher;
36  import org.apache.juddi.config.AppConfig;
37  import org.apache.juddi.config.Property;
38  import org.apache.juddi.jaxb.EntityCreator;
39  import org.apache.juddi.v3.client.UDDIConstants;
40  import org.apache.juddi.v3.error.InvalidKeyPassedException;
41  import org.apache.juddi.v3.error.UserMismatchException;
42  import org.apache.juddi.v3.tck.TckPublisher;
43  import org.apache.juddi.v3.tck.TckSecurity;
44  import org.apache.juddi.v3.tck.TckSubscription;
45  import org.junit.AfterClass;
46  import org.junit.Assert;
47  import org.junit.BeforeClass;
48  import org.junit.Test;
49  import org.uddi.api_v3.FindBusiness;
50  import org.uddi.api_v3.FindQualifiers;
51  import org.uddi.api_v3.Name;
52  import org.uddi.api_v3.TModel;
53  import org.uddi.sub_v3.Subscription;
54  import org.uddi.sub_v3.SubscriptionFilter;
55  import org.uddi.v3_service.DispositionReportFaultMessage;
56  import org.uddi.v3_service.UDDISecurityPortType;
57  
58  /**
59   * This test is jUDDI specific, as the publisher methods are an extension to the
60   * UDDI api.
61   *
62   * @author <a href="mailto:jfaath@apache.org">Jeff Faath</a>
63   * @author <a href="mailto:kstam@apache.org">Kurt T Stam</a>
64   * @author <a href="mailto:alexoree@apache.org">Alex O'Ree</a>
65   */
66  public class API_010_PublisherTest {
67  
68          private static Log logger = LogFactory.getLog(API_010_PublisherTest.class);
69          private JUDDIApiImpl publisher = new JUDDIApiImpl();
70          private UDDISecurityPortType security = new UDDISecurityImpl();
71          private static TckSubscription tckSubscription = new TckSubscription(new UDDISubscriptionImpl(), new UDDISecurityImpl(), new UDDIInquiryImpl());
72  
73          @BeforeClass
74          public static void startRegistry() throws ConfigurationException {
75                  Registry.start();
76          }
77  
78          @AfterClass
79          public static void stopRegistry() throws ConfigurationException {
80                  Registry.stop();
81          }
82  
83          @Test
84          public void testJoePublisher() {
85                  //We can only test this if the publisher is not there already.
86                  //If it already there is probably has foreign key relationships.
87                  //This test should really only run on an empty database. Seed
88                  //data will be added if the root publisher is missing.
89                  if (!isExistPublisher(TckPublisher.getJoePublisherId())) {
90                          saveJoePublisher();
91                          deleteJoePublisher();
92                  }
93          }
94  
95          @Test
96          public void testSamSyndicator() {
97                  //We can only test this if the publisher is not there already.
98                  if (!isExistPublisher(TckPublisher.getSamPublisherId())) {
99                          saveSamSyndicator();
100                         deleteSamSyndicator();
101                 }
102         }
103 
104         /**
105          * Persists Joe Publisher to the database.
106          *
107          * @return - true if the published did not exist already, - false in all
108          * other cases.
109          */
110         public boolean saveJoePublisher() {
111                 if (!isExistPublisher(TckPublisher.getJoePublisherId())) {
112                         savePublisher(TckPublisher.getJoePublisherId(), TckPublisher.JOE_PUBLISHER_XML);
113                         return true;
114                 } else {
115                         return false;
116                 }
117         }
118 
119         /**
120          * Persists Mary Publisher to the database.
121          *
122          * @return - true if the published did not exist already, - false in all
123          * other cases.
124          */
125         public boolean saveMaryPublisher() {
126                 if (!isExistPublisher(TckPublisher.getMaryPublisherId())) {
127                         savePublisher(TckPublisher.getMaryPublisherId(), TckPublisher.MARY_PUBLISHER_XML);
128                         return true;
129                 } else {
130                         return false;
131                 }
132         }
133 
134         /**
135          * Removes Joe Publisher from the database, this will fail if there are
136          * child objects attached; think Services etc.
137          */
138         public void deleteJoePublisher() {
139                 deletePublisher(TckPublisher.getJoePublisherId());
140         }
141 
142         /**
143          * Persists Sam Syndicator to the database.
144          *
145          * @return publisherId
146          */
147         public String saveSamSyndicator() {
148                 if (!isExistPublisher(TckPublisher.getSamPublisherId())) {
149                         savePublisher(TckPublisher.getSamPublisherId(), TckPublisher.SAM_SYNDICATOR_XML);
150                 }
151                 return TckPublisher.getSamPublisherId();
152         }
153 
154         /**
155          * Removes Sam Syndicator from the database, this will fail if there are
156          * child objects attached; think Services etc.
157          */
158         public void deleteSamSyndicator() {
159                 deletePublisher(TckPublisher.getSamPublisherId());
160         }
161 
162         private void savePublisher(String publisherId, String publisherXML) {
163                 try {
164                         String rootPublisherStr = AppConfig.getConfiguration().getString(Property.JUDDI_ROOT_PUBLISHER);
165                         logger.info("savePublisher as " + rootPublisherStr);
166                         String authInfo = TckSecurity.getAuthToken(security, rootPublisherStr, "");
167                         logger.debug("Saving new publisher: " + publisherXML);
168                         SavePublisher sp = new SavePublisher();
169                         sp.setAuthInfo(authInfo);
170                         Publisher pubIn = (Publisher) EntityCreator.buildFromDoc(publisherXML, EntityCreator.JUDDIv3_Package);
171                         sp.getPublisher().add(pubIn);
172                         publisher.savePublisher(sp);
173 
174                         // Now get the entity and check the values
175                         GetPublisherDetail gp = new GetPublisherDetail();
176                         gp.getPublisherId().add(publisherId);
177                         gp.setAuthInfo(authInfo);
178                         PublisherDetail pd = publisher.getPublisherDetail(gp);
179                         List<Publisher> pubOutList = pd.getPublisher();
180                         Publisher pubOut = pubOutList.get(0);
181 
182                         assertEquals(pubIn.getAuthorizedName(), pubOut.getAuthorizedName());
183                         assertEquals(pubIn.getPublisherName(), pubOut.getPublisherName());
184                         assertEquals(pubIn.getEmailAddress(), pubOut.getEmailAddress());
185                         assertEquals(pubIn.isIsAdmin(), pubOut.isIsAdmin());
186                         assertEquals(pubIn.isIsEnabled(), pubOut.isIsEnabled());
187                         assertEquals(pubIn.getMaxBindingsPerService(), pubOut.getMaxBindingsPerService());
188                         assertEquals(pubIn.getMaxBusinesses(), pubOut.getMaxBusinesses());
189                         assertEquals(pubIn.getMaxServicePerBusiness(), pubOut.getMaxServicePerBusiness());
190                         assertEquals(pubIn.getMaxTModels(), pubOut.getMaxTModels());
191 
192                         logger.debug("Querying for publisher: " + publisherXML);
193                         //Querying for this publisher to make sure it's really gone
194                         //We're expecting a invalid Key exception at this point.
195                         PublisherDetail pdBeforeDelete = null;
196                         try {
197                                 pdBeforeDelete = publisher.getPublisherDetail(gp);
198                                 Assert.assertNotNull(pdBeforeDelete);
199                         } catch (InvalidKeyPassedException e) {
200                                 Assert.fail("We expected to find publisher " + publisherXML);
201                         }
202 
203                 } catch (Exception e) {
204                         logger.error(e.getMessage(), e);
205                         Assert.fail("No exception should be thrown");
206                 }
207         }
208 
209         private void deletePublisher(String publisherId) {
210                 try {
211                         String rootPublisherStr = AppConfig.getConfiguration().getString(Property.JUDDI_ROOT_PUBLISHER);
212                         String authInfo = TckSecurity.getAuthToken(security, rootPublisherStr, "");
213                         logger.debug("Delete publisher: " + publisherId);
214                         //Now deleting this publisher
215                         DeletePublisher dp = new DeletePublisher();
216                         dp.setAuthInfo(authInfo);
217                         dp.getPublisherId().add(publisherId);
218                         publisher.deletePublisher(dp);
219 
220                         logger.info("Querying for publisher: " + publisherId + " after deletion.");
221                         //Querying for this publisher to make sure it's really gone
222                         //We're expecting a invalid Key exception at this point.
223                         GetPublisherDetail gp = new GetPublisherDetail();
224                         gp.getPublisherId().add(publisherId);
225                         gp.setAuthInfo(authInfo);
226                         PublisherDetail pdAfterDelete = null;
227                         try {
228                                 pdAfterDelete = publisher.getPublisherDetail(gp);
229                                 Assert.fail("We did not expect to find this publisher anymore.");
230                         } catch (InvalidKeyPassedException e) {
231                                 Assert.assertNull(pdAfterDelete);
232                         }
233 
234                 } catch (Exception e) {
235                         logger.error(e.getMessage(), e);
236                         Assert.fail("No exception should be thrown");
237                 }
238         }
239 
240         private boolean isExistPublisher(String publisherId) {
241                 GetPublisherDetail gp = new GetPublisherDetail();
242                 gp.getPublisherId().add(publisherId);
243                 try {
244                         publisher.getPublisherDetail(gp);
245                         return true;
246                 } catch (Exception e) {
247                         return false;
248                 }
249         }
250 
251         protected String authInfoJoe() throws RemoteException, DispositionReportFaultMessage {
252                 return TckSecurity.getAuthToken(security, TckPublisher.getJoePublisherId(), TckPublisher.getJoePassword());
253         }
254 
255         protected String authInfoSam() throws RemoteException, DispositionReportFaultMessage {
256                 return TckSecurity.getAuthToken(security, TckPublisher.getSamPublisherId(), TckPublisher.getSamPassword());
257         }
258 
259         /**
260          * Joe saves a subscription on Sam's behalf
261          *
262          * @throws Exception
263          */
264         @Test
265         public void testAdminSaveSubscriptionAuthorized() throws Exception {
266                 saveJoePublisher();
267                 saveSamSyndicator();
268                 DatatypeFactory fac = DatatypeFactory.newInstance();
269                 List<Subscription> subs = new ArrayList<Subscription>();
270                 Subscription s = new Subscription();
271 
272                 s.setMaxEntities(10);
273                 s.setBrief(false);
274                 GregorianCalendar gcal = new GregorianCalendar();
275                 gcal.setTimeInMillis(System.currentTimeMillis());
276                 gcal.add(Calendar.HOUR, 1);
277                 s.setExpiresAfter(fac.newXMLGregorianCalendar(gcal));
278                 s.setSubscriptionFilter(new SubscriptionFilter());
279                 s.getSubscriptionFilter().setFindBusiness(new FindBusiness());
280                 s.getSubscriptionFilter().getFindBusiness().setFindQualifiers(new FindQualifiers());
281                 s.getSubscriptionFilter().getFindBusiness().getFindQualifiers().getFindQualifier().add(UDDIConstants.APPROXIMATE_MATCH);
282                 s.getSubscriptionFilter().getFindBusiness().getName().add(new Name(UDDIConstants.WILDCARD, null));
283                 subs.add(s);
284                 Holder<List<Subscription>> items = new Holder<List<Subscription>>();
285                 items.value = subs;
286                 publisher.adminSaveSubscription(authInfoJoe(), TckPublisher.getSamPublisherId(), items);
287                 for (int i = 0; i < items.value.size(); i++) {
288                         tckSubscription.deleteSubscription(authInfoSam(), items.value.get(i).getSubscriptionKey());
289                 }
290 
291                 deleteJoePublisher();
292                 deleteSamSyndicator();
293 
294         }
295 
296         /**
297          * Sam saves a subscription on Sam's behalf (not authorized
298          *
299          * @throws Exception
300          */
301         @Test(expected = UserMismatchException.class)
302         public void testAdminSaveSubscriptionNotAuthorized() throws Exception {
303                 saveJoePublisher();
304                 saveSamSyndicator();
305                 DatatypeFactory fac = DatatypeFactory.newInstance();
306                 List<Subscription> subs = new ArrayList<Subscription>();
307                 Subscription s = new Subscription();
308 
309                 s.setMaxEntities(10);
310                 s.setBrief(false);
311                 GregorianCalendar gcal = new GregorianCalendar();
312                 gcal.setTimeInMillis(System.currentTimeMillis());
313                 gcal.add(Calendar.HOUR, 1);
314                 s.setExpiresAfter(fac.newXMLGregorianCalendar(gcal));
315                 s.setSubscriptionFilter(new SubscriptionFilter());
316                 s.getSubscriptionFilter().setFindBusiness(new FindBusiness());
317                 s.getSubscriptionFilter().getFindBusiness().setFindQualifiers(new FindQualifiers());
318                 s.getSubscriptionFilter().getFindBusiness().getFindQualifiers().getFindQualifier().add(UDDIConstants.APPROXIMATE_MATCH);
319                 s.getSubscriptionFilter().getFindBusiness().getName().add(new Name(UDDIConstants.WILDCARD, null));
320                 subs.add(s);
321                 Holder<List<Subscription>> items = new Holder<List<Subscription>>();
322                 items.value = subs;
323                 publisher.adminSaveSubscription(authInfoSam(), TckPublisher.getJoePublisherId(), items);
324                 deleteJoePublisher();
325                 deleteSamSyndicator();
326 
327         }
328 
329         @Test
330         public void testAdminSaveTModel() throws Exception {
331                 saveJoePublisher();
332                 saveSamSyndicator();
333                 List<org.apache.juddi.api_v3.AdminSaveTModelWrapper> values = new ArrayList<org.apache.juddi.api_v3.AdminSaveTModelWrapper>();
334                 org.apache.juddi.api_v3.AdminSaveTModelWrapper x = new org.apache.juddi.api_v3.AdminSaveTModelWrapper();
335                 x.setPublisherID(TckPublisher.getSamPublisherId());
336                 TModel tm = new TModel();
337                 tm.setName(new Name("testAdminSaveTModel joe on sam's behalf", null));
338                 
339                 x.getTModel().add(tm);
340                 publisher.adminSaveTModel(authInfoJoe(), values);
341                 deleteJoePublisher();
342                 deleteSamSyndicator();
343         }
344         
345 }