This project has retired. For details please refer to its Attic page.
WADL2UDDITest xref
View Javadoc
1   /*
2    * Copyright 2013 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    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.juddi.v3.client.mapping.wsdl;
17  
18  import java.io.File;
19  import java.net.HttpURLConnection;
20  import java.net.MalformedURLException;
21  import java.net.URL;
22  import java.util.List;
23  import java.util.Properties;
24  import java.util.logging.Logger;
25  
26  import javax.xml.namespace.QName;
27  
28  import org.apache.juddi.jaxb.PrintUDDI;
29  import org.apache.juddi.v3.client.config.UDDIClerk;
30  import org.apache.juddi.v3.client.mapping.URLLocalizerDefaultImpl;
31  import org.apache.juddi.v3.client.mapping.wadl.Application;
32  import org.apache.juddi.v3.client.mapping.wadl.WADL2UDDI;
33  import org.junit.Assert;
34  import org.junit.Test;
35  import org.uddi.api_v3.BusinessService;
36  import org.uddi.api_v3.SaveTModel;
37  import org.uddi.api_v3.TModel;
38  
39  /**
40   *
41   * @author <a href="mailto:alexoree@apache.org">Alex O'Ree</a>
42   */
43  public class WADL2UDDITest {
44  
45      static final Logger log = Logger.getLogger(WADL2UDDITest.class.getCanonicalName());
46      static PrintUDDI<TModel> pTModel = new PrintUDDI<TModel>();
47      static Properties properties = new Properties();
48      static boolean serialize = false;
49  
50      public WADL2UDDITest() {
51          if (System.getProperty("debug") != null && System.getProperty("debug").equalsIgnoreCase("true")) {
52              serialize = true;
53          }
54      }
55  
56      /**
57       * tests loading a wadl from a file
58       *
59       * @throws Exception
60       */
61      @Test
62      public void Test1() throws Exception {
63          log.info("Test1 read from file");
64  
65          Application app = WADL2UDDI.parseWadl(new File("src/test/resources/wadl/cxf.wadl"));
66          Assert.assertNotNull(app);
67          Assert.assertNotNull(app);
68          List<URL> urls = WADL2UDDI.getBaseAddresses(app);
69          URL url = urls.get(0);
70          String domain = url.getHost();
71          PrintUDDI<TModel> tmodelPrinter = new PrintUDDI<TModel>();
72          TModel keygen = UDDIClerk.createKeyGenator("uddi:" + domain + ":keygenerator", domain, "en");
73          //save the keygen
74          SaveTModel stm = new SaveTModel();
75          stm.getTModel().add(keygen);
76          if (serialize) {
77              System.out.println(tmodelPrinter.print(keygen));
78          }
79  
80          properties.put("keyDomain", domain);
81          properties.put("businessName", domain);
82          properties.put("serverName", url.getHost());
83          properties.put("serverPort", url.getPort());
84          //wsdlURL = wsdlDefinition.getDocumentBaseURI();
85          WADL2UDDI wadl2UDDI = new WADL2UDDI(null, new URLLocalizerDefaultImpl(), properties);
86  
87          Assert.assertNotNull(wadl2UDDI);
88  
89          BusinessService businessServices = wadl2UDDI.createBusinessService(new QName("CXF.Sample.WADL.namespace", "Servicename"), app);
90  
91          Assert.assertNotNull(businessServices);
92          Assert.assertNotNull(businessServices.getBindingTemplates());
93          Assert.assertNotNull(businessServices.getName().get(0));
94          Assert.assertNotNull(businessServices.getBindingTemplates().getBindingTemplate().get(0));
95          Assert.assertNotNull(businessServices.getBindingTemplates().getBindingTemplate().get(0).getAccessPoint());
96          Assert.assertNotNull(businessServices.getBindingTemplates().getBindingTemplate().get(0).getAccessPoint().getValue());
97  
98  
99  
100 
101         PrintUDDI<BusinessService> servicePrinter = new PrintUDDI<BusinessService>();
102         if (serialize) {
103             System.out.println(servicePrinter.print(businessServices));
104         }
105 
106 
107 
108     }
109 
110     private static boolean IsReachable(String url) {
111         System.out.println("Testing connectivity to " + url);
112         try {
113             //make a URL to a known source
114             URL url2 = new URL(url);
115 
116             //open a connection to that source
117             HttpURLConnection urlConnect = (HttpURLConnection) url2.openConnection();
118 
119             //trying to retrieve data from the source. If there
120             //is no connection, this line will fail
121             Object objData = urlConnect.getContent();
122             urlConnect.disconnect();
123 
124         } catch (Exception e) {
125             System.out.println("Connectivity failed " + e.getMessage());
126             return false;
127         }
128         System.out.println("Connectivity passed");
129         return true;
130 
131     }
132 
133     /**
134      * normally, this test will work correctly if and only if you're connected
135      * to the big bad internet. if you happen to be offline, this test will
136      * fail.
137      */
138     @Test
139     public void Test2readFromURL() throws MalformedURLException, Exception {
140         log.info("Test2 read from URL");
141         boolean b = IsReachable("http://svn.apache.org/repos/asf/cxf/trunk/systests/jaxrs/src/test/resources/wadl/bookstoreImportResource.wadl");
142         if (!b) {
143             System.out.println("Skipping test for a remote WADL due to connectivity problems");
144         }
145 
146 
147         org.junit.Assume.assumeTrue(b);
148 
149 
150         Application app = WADL2UDDI.parseWadl(new URL("http://svn.apache.org/repos/asf/cxf/trunk/systests/jaxrs/src/test/resources/wadl/bookstoreImportResource.wadl"));
151         Assert.assertNotNull(app);
152         Assert.assertNotNull(app);
153         List<URL> urls = WADL2UDDI.getBaseAddresses(app);
154         URL url = urls.get(0);
155         String domain = url.getHost();
156         PrintUDDI<TModel> tmodelPrinter = new PrintUDDI<TModel>();
157         TModel keygen = UDDIClerk.createKeyGenator("uddi:" + domain + ":keygenerator", domain, "en");
158         //save the keygen
159         SaveTModel stm = new SaveTModel();
160         stm.getTModel().add(keygen);
161         if (serialize) {
162             System.out.println(tmodelPrinter.print(keygen));
163         }
164 
165         properties.put("keyDomain", domain);
166         properties.put("businessName", domain);
167         properties.put("serverName", url.getHost());
168         properties.put("serverPort", url.getPort());
169         //wsdlURL = wsdlDefinition.getDocumentBaseURI();
170         WADL2UDDI wadl2UDDI = new WADL2UDDI(null, new URLLocalizerDefaultImpl(), properties);
171 
172         Assert.assertNotNull(wadl2UDDI);
173 
174         BusinessService businessServices = wadl2UDDI.createBusinessService(new QName("CXF.Sample.WADL.namespace", "Servicename"), app);
175 
176         Assert.assertNotNull(businessServices);
177         Assert.assertNotNull(businessServices.getBindingTemplates());
178         Assert.assertNotNull(businessServices.getName().get(0));
179         Assert.assertNotNull(businessServices.getBindingTemplates().getBindingTemplate().get(0));
180         Assert.assertNotNull(businessServices.getBindingTemplates().getBindingTemplate().get(0).getAccessPoint());
181         Assert.assertNotNull(businessServices.getBindingTemplates().getBindingTemplate().get(0).getAccessPoint().getValue());
182 
183 
184 
185 
186         PrintUDDI<BusinessService> servicePrinter = new PrintUDDI<BusinessService>();
187         if (serialize) {
188             System.out.println(servicePrinter.print(businessServices));
189         }
190 
191 
192     }
193 }