View Javadoc
1   package org.kuali.ole.deliver.service;
2   
3   import org.apache.log4j.Logger;
4   import org.junit.Test;
5   import org.springframework.core.io.ClassPathResource;
6   import org.springframework.core.io.Resource;
7   import org.apache.xml.serialize.OutputFormat;
8   import org.apache.xml.serialize.XMLSerializer;
9   import org.w3c.dom.Document;
10  import org.xml.sax.InputSource;
11  import org.xml.sax.SAXException;
12  
13  import javax.xml.parsers.DocumentBuilder;
14  import javax.xml.parsers.DocumentBuilderFactory;
15  import javax.xml.parsers.ParserConfigurationException;
16  import java.io.IOException;
17  import java.io.StringReader;
18  import java.io.StringWriter;
19  import java.io.Writer;
20  
21  import javax.xml.namespace.QName;
22  import javax.xml.soap.*;
23  import javax.xml.transform.OutputKeys;
24  import javax.xml.transform.Transformer;
25  import javax.xml.transform.TransformerFactory;
26  import javax.xml.transform.dom.DOMSource;
27  import javax.xml.transform.stream.StreamResult;
28  import java.io.*;
29  import java.net.HttpURLConnection;
30  import java.net.URL;
31  
32  /**
33   * Created by sheiksalahudeenm on 5/5/15.
34   */
35  public class OLEPatronService_UT {
36  
37      private static final Logger LOG = Logger.getLogger(OLEPatronService_UT.class);
38      private static String APPLICATION_URL  = "http://192.168.55.223:8080/olefs";
39  
40      @Test
41      public void createPatronTest(){
42          try {
43              String requestContent  =readFileContent("org/kuali/ole/deliver/patron/createPatronWsdlXml");
44              String resposneString = OLESOAPService.sendSoapRequest(APPLICATION_URL + "/remoting/olePatronService",requestContent);
45              System.out.println("Response : \n" +formatContentForPretty(resposneString));
46  
47          } catch (Exception ex) {
48              ex.printStackTrace();
49          }
50      }
51  
52      public String readFileContent(String path) throws IOException {
53          BufferedReader br=new BufferedReader(new FileReader(getFilePath(path)));
54          String line=null;
55          String fullContent = "";
56          while ((line=br.readLine())!=null)
57          {
58              fullContent += line;
59          }
60          return fullContent;
61      }
62  
63      public String getFilePath(String classpathRelativePath)  {
64          try {
65              Resource rsrc = new ClassPathResource(classpathRelativePath);
66              return rsrc.getFile().getAbsolutePath();
67          } catch(Exception e){
68              LOG.error("Error : while accessing file "+e);
69          }
70          return null;
71      }
72  
73      private String formatContentForPretty(String content){
74          try {
75              final Document document = parseXmlFile(content);
76              OutputFormat format = new OutputFormat(document);
77              format.setLineWidth(65);
78              format.setIndenting(true);
79              format.setIndent(2);
80              Writer out = new StringWriter();
81              XMLSerializer serializer = new XMLSerializer(out, format);
82              serializer.serialize(document);
83              return out.toString();
84          } catch (IOException e) {
85              throw new RuntimeException(e);
86          }
87  
88      }
89  
90      private Document parseXmlFile(String in) {
91          try {
92              DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
93              DocumentBuilder db = dbf.newDocumentBuilder();
94              InputSource is = new InputSource(new StringReader(in));
95              return db.parse(is);
96          } catch (ParserConfigurationException e) {
97              throw new RuntimeException(e);
98          } catch (SAXException e) {
99              throw new RuntimeException(e);
100         } catch (IOException e) {
101             throw new RuntimeException(e);
102         }
103     }
104 
105 }