Create seed code of svnfm vnfmdriver
[vfc/nfvo/driver/vnfm/svnfm.git] / nokia / vnfmdriver / vfcadaptorservice / vfcadaptor / src / test / java / com / nokia / vfcadaptor / vnfmdriver / controller / BaseControllerTestCase.java
1 /*
2  * Copyright 2016-2017, Nokia Corporation
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
17 package com.nokia.vfcadaptor.vnfmdriver.controller;
18
19 import java.io.IOException;
20 import java.io.UnsupportedEncodingException;
21
22 import javax.net.ssl.SSLContext;
23 import javax.net.ssl.TrustManager;
24 import javax.net.ssl.X509TrustManager;
25
26 import org.apache.http.HttpEntity;
27 import org.apache.http.HttpResponse;
28 import org.apache.http.client.ClientProtocolException;
29 import org.apache.http.client.HttpClient;
30 import org.apache.http.client.methods.HttpGet;
31 import org.apache.http.client.methods.HttpPost;
32 import org.apache.http.conn.scheme.Scheme;
33 import org.apache.http.conn.scheme.SchemeRegistry;
34 import org.apache.http.conn.ssl.SSLSocketFactory;
35 import org.apache.http.entity.StringEntity;
36 import org.apache.http.impl.client.DefaultHttpClient;
37 import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
38 import org.apache.http.util.EntityUtils;
39 import org.junit.BeforeClass;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.http.MediaType;
43
44 public class BaseControllerTestCase {
45
46         private static final String CONTENT_TYPE = "Content-Type";
47         private static final String AUTH = "auth";
48
49         private static final String UTF_8 = "utf-8";
50
51         protected static String serviceUrl = "http://127.0.0.1:8080/AppSenseAnalysisSystem";
52
53         protected static String baseUrl;
54
55         protected static String pictureServerRootUrl = "http://localhost";
56
57         protected Logger log = LoggerFactory.getLogger(this.getClass());
58         protected boolean isHttpsProtocol = false;
59
60         @BeforeClass
61         public static void beforeClass() throws Exception {
62                 baseUrl = serviceUrl;
63         }
64         
65         public static org.apache.http.client.HttpClient wrapClient(org.apache.http.client.HttpClient base) {  
66         try {  
67             SSLContext ctx = SSLContext.getInstance("TLS");  
68             X509TrustManager tm = new X509TrustManager() {  
69                 public java.security.cert.X509Certificate[] getAcceptedIssuers() {  
70                     return null;  
71                 }  
72                 public void checkClientTrusted(  
73                         java.security.cert.X509Certificate[] chain,  
74                         String authType)  
75                         throws java.security.cert.CertificateException {  
76                     // TODO Auto-generated method stub  
77                       
78                 }  
79                 public void checkServerTrusted(  
80                         java.security.cert.X509Certificate[] chain,  
81                         String authType)  
82                         throws java.security.cert.CertificateException {  
83                     // TODO Auto-generated method stub  
84                       
85                 }  
86             };  
87             ctx.init(null, new TrustManager[] { tm }, null);  
88             SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);  
89             SchemeRegistry registry = new SchemeRegistry();  
90             registry.register(new Scheme("https", 8089, ssf));  
91             ThreadSafeClientConnManager mgr = new ThreadSafeClientConnManager(registry);  
92             return new DefaultHttpClient(mgr, base.getParams());  
93         } catch (Exception ex) {  
94             ex.printStackTrace();  
95             return null;  
96         }  
97     }
98         
99         protected String sendPostMsg(String message, String url) throws UnsupportedEncodingException,
100                         IOException, ClientProtocolException {
101             
102                 HttpClient httpclient = new DefaultHttpClient();
103                 if(isHttpsProtocol)
104                 {
105                     httpclient = wrapClient(httpclient);
106                 }
107                 HttpPost httppost = new HttpPost(url);
108                 StringEntity myEntity = new StringEntity(message, UTF_8);
109                 String auth = "";
110                 httppost.addHeader(AUTH, auth);
111                 httppost.addHeader(CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
112 //              httppost.addHeader(CONTENT_TYPE, MediaType.TEXT_XML_VALUE);
113                 httppost.setEntity(myEntity);
114                 HttpResponse response = httpclient.execute(httppost);
115                 HttpEntity resEntity = response.getEntity();
116                 String responseContent = "";
117                 if (resEntity != null) {
118                         responseContent = EntityUtils.toString(resEntity, "UTF-8");
119                         EntityUtils.consume(resEntity);
120                 }
121                 httpclient.getConnectionManager().shutdown();
122                 return responseContent;
123         }
124         
125         protected String sendGetMsg(String message, String url) throws UnsupportedEncodingException,
126         IOException, ClientProtocolException {
127
128       HttpClient httpclient = new DefaultHttpClient();
129      if(isHttpsProtocol)
130      {
131      httpclient = wrapClient(httpclient);
132      }
133      HttpGet  httpGet = new  HttpGet(url);
134      StringEntity myEntity = new StringEntity(message, UTF_8);
135      String auth = "";
136      httpGet.addHeader(AUTH, auth);
137      httpGet.addHeader(CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
138         //httppost.addHeader(CONTENT_TYPE, MediaType.TEXT_XML_VALUE);
139      //((HttpResponse) httpGet).setEntity(myEntity);
140   HttpResponse response = httpclient.execute(httpGet);
141   HttpEntity resEntity = response.getEntity();
142    String responseContent = "";
143    if (resEntity != null) {
144         responseContent = EntityUtils.toString(resEntity, "UTF-8");
145         responseContent.replaceAll("\r", "");//
146         EntityUtils.consume(resEntity);
147    }
148    httpclient.getConnectionManager().shutdown();
149    return responseContent;
150    }
151 }