Advertisement
ManZzup

Apache CXF Integration Test

Aug 12th, 2016
846
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.07 KB | None | 0 0
  1. package org.wso2.carbon.identity.entitlement.endpoint.test;
  2.  
  3. import org.apache.cxf.endpoint.Server;
  4. import org.apache.cxf.jaxrs.client.WebClient;
  5. import org.testng.Assert;
  6. import org.testng.annotations.Test;
  7.  
  8.  
  9. import javax.ws.rs.core.Response;
  10. import java.io.File;
  11. import java.io.IOException;
  12. import java.net.URI;
  13. import java.net.URISyntaxException;
  14. import java.util.Scanner;
  15.  
  16. public class TestService extends Assert{
  17.     private final static String ENDPOINT_ADDRESS = "https://localhost:5000/rest";
  18.     private final static String WADL_ADDRESS = ENDPOINT_ADDRESS + "?_wadl";
  19.  
  20.     TestService(){
  21.         System.setProperty("javax.net.ssl.trustStore", "/home/manujith/Apps/wso2is-5.1.0/repository/resources/security/client-truststore.jks");
  22.     }
  23.  
  24.     private static void waitForWADL() throws Exception {
  25.         WebClient client = WebClient.create(WADL_ADDRESS);
  26.         // wait for 20 secs or so
  27.         for (int i = 0; i < 20; i++) {
  28.             Thread.currentThread().sleep(1000);
  29.             Response response = client.get();
  30.             if (response.getStatus() == 200) {
  31.                 break;
  32.             }
  33.         }
  34.         // no WADL is available yet - throw an exception or give tests a chance to run anyway
  35.     }
  36.  
  37.     private String readReource(String path){
  38.         StringBuilder result = new StringBuilder("");
  39.         try{
  40.             //Get file from resources folder
  41.             ClassLoader classLoader = getClass().getClassLoader();
  42.             URI filepath = new URI(classLoader.getResource(path).toString());
  43.  
  44.             File file = new File(filepath);
  45.  
  46.             Scanner scanner = new Scanner(file);
  47.  
  48.             while (scanner.hasNextLine()) {
  49.                 String line = scanner.nextLine();
  50.                 result.append(line).append("\n");
  51.             }
  52.  
  53.             scanner.close();
  54.  
  55.         } catch (IOException e) {
  56.             e.printStackTrace();
  57.         } catch(URISyntaxException e){
  58.             e.printStackTrace();
  59.         }
  60.  
  61.         return result.toString().replaceAll("\\n\\r|\\n|\\r|\\t|\\s{2,}","");
  62.     }
  63.  
  64.     @Test
  65.     public void testGetResponseXML(){
  66.  
  67.         WebClient client = WebClient.create(ENDPOINT_ADDRESS);
  68.  
  69.         client.type("application/xml");
  70.         client.accept("application/xml");
  71.  
  72.         client.path("getResponse");
  73.  
  74.  
  75.  
  76.         String request = readReource("xml/request-by-attrib-1.xml");
  77.         String response = readReource("xml/response-by-attrib-1.xml");
  78.  
  79.         String webRespose = client.post(request,String.class);
  80.  
  81.         assertEquals(response,webRespose);
  82.     }
  83.  
  84.     @Test
  85.     public void testGetResponseJSON(){
  86.  
  87.         WebClient client = WebClient.create(ENDPOINT_ADDRESS);
  88.  
  89.         client.type("application/json");
  90.         client.accept("application/json");
  91.  
  92.         client.path("getResponse");
  93.  
  94.  
  95.  
  96.         String request = readReource("json/request-by-attrib-1.json");
  97.         String response = readReource("json/response-by-attrib-1.json");
  98.  
  99.         String webRespose = client.post(request,String.class);
  100.  
  101.         assertEquals(response,webRespose);
  102.     }
  103.  
  104.  
  105.  
  106.  
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement