diff options
Diffstat (limited to 'nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/test')
4 files changed, 506 insertions, 0 deletions
diff --git a/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/test/java/com/nokia/vfcadaptor/http/client/HttpRequestProcessorTest.java b/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/test/java/com/nokia/vfcadaptor/http/client/HttpRequestProcessorTest.java new file mode 100644 index 00000000..61367505 --- /dev/null +++ b/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/test/java/com/nokia/vfcadaptor/http/client/HttpRequestProcessorTest.java @@ -0,0 +1,125 @@ +/* + * Copyright 2016-2017, Nokia Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.nokia.vfcadaptor.http.client; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.security.KeyManagementException; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; + +import org.apache.http.client.ClientProtocolException; +import org.apache.http.impl.client.HttpClientBuilder; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.context.WebApplicationContext; + +import com.google.gson.Gson; +import com.nokia.vfcadaptor.vnfmdriver.bo.InstantiateVnfRequest; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations={"file:src/main/webapp/WEB-INF/mvc-servlet.xml"}) +@WebAppConfiguration +public class HttpRequestProcessorTest { + private String url; + private String basicUrl="http://localhost:8080/NvfmDriver/api/nokiavnfm/v1"; + private String funcPath; + + private Gson gson = new Gson(); + + @Autowired + private WebApplicationContext wac; + private MockMvc mockMvc; + @Before + public void setup() { + this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); //¹¹ÔìMockMvc + } + + @Test + public void testP() + { + String message = "{\"vnfInstanceName\":\"vnfInstanceName_001\",\"vnfPackageId\":\"1\"}"; + InstantiateVnfRequest request = gson.fromJson(message, InstantiateVnfRequest.class); + + System.out.println("vnfInstanceName = " + request.getVnfInstanceName()); + + String result; + try { + result = mockMvc.perform(MockMvcRequestBuilders.post("/nokiavnfm/v1/vnfmId_001/vnfs") + .contentType(MediaType.APPLICATION_JSON).content(message) + .accept(MediaType.APPLICATION_JSON)) //Ö´ÐÐÇëÇó + .andReturn().getResponse().getContentAsString(); + System.out.println("result = " + result); + } catch (UnsupportedEncodingException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + + } +// +// @Test +// public void testProcess() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException, ClientProtocolException, IOException +// { +// funcPath = "/vnfmId_001/vnfs"; +// url = basicUrl + funcPath; +// String message = "{\"vnfInstanceName\":\"vnfInstanceName_001\",\"vnfPackageId\":\"1\"}"; +// +// HttpClientBuilder httpClientBuilder = HttpClientUtils.createHttpClientBuilder(); +// HttpRequestProcessor processor = new HttpRequestProcessor(httpClientBuilder, RequestMethod.POST); +// processor.addPostEntity(message); +// String result = processor.process(url); +// +// System.out.println(result); +// } +// +// @Test +// public void testHttps() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException, ClientProtocolException, IOException +// { +// url = "https://www.baidu.com"; +// HttpClientBuilder httpClientBuilder = HttpClientUtils.createHttpClientBuilder(); +// HttpRequestProcessor processor = new HttpRequestProcessor(httpClientBuilder, RequestMethod.GET); +// +// String result = processor.process(url); +// +// System.out.println("The result is :" + result); +// } +// @Test +// public void testHome() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException, ClientProtocolException, IOException +// { +// url = "http://172.24.208.168/"; +// HttpClientBuilder httpClientBuilder = HttpClientUtils.createHttpClientBuilder(); +// HttpRequestProcessor processor = new HttpRequestProcessor(httpClientBuilder, RequestMethod.GET); +// +// String result = processor.process(url); +// +// System.out.println("The result is :" + result); +// } +} diff --git a/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/test/java/com/nokia/vfcadaptor/vnfmdriver/controller/BaseControllerTestCase.java b/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/test/java/com/nokia/vfcadaptor/vnfmdriver/controller/BaseControllerTestCase.java new file mode 100644 index 00000000..025949c0 --- /dev/null +++ b/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/test/java/com/nokia/vfcadaptor/vnfmdriver/controller/BaseControllerTestCase.java @@ -0,0 +1,151 @@ +/* + * Copyright 2016-2017, Nokia Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.nokia.vfcadaptor.vnfmdriver.controller; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; + +import javax.net.ssl.SSLContext; +import javax.net.ssl.TrustManager; +import javax.net.ssl.X509TrustManager; + +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.conn.scheme.Scheme; +import org.apache.http.conn.scheme.SchemeRegistry; +import org.apache.http.conn.ssl.SSLSocketFactory; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager; +import org.apache.http.util.EntityUtils; +import org.junit.BeforeClass; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.http.MediaType; + +public class BaseControllerTestCase { + + private static final String CONTENT_TYPE = "Content-Type"; + private static final String AUTH = "auth"; + + private static final String UTF_8 = "utf-8"; + + protected static String serviceUrl = "http://127.0.0.1:8080/AppSenseAnalysisSystem"; + + protected static String baseUrl; + + protected static String pictureServerRootUrl = "http://localhost"; + + protected Logger log = LoggerFactory.getLogger(this.getClass()); + protected boolean isHttpsProtocol = false; + + @BeforeClass + public static void beforeClass() throws Exception { + baseUrl = serviceUrl; + } + + public static org.apache.http.client.HttpClient wrapClient(org.apache.http.client.HttpClient base) { + try { + SSLContext ctx = SSLContext.getInstance("TLS"); + X509TrustManager tm = new X509TrustManager() { + public java.security.cert.X509Certificate[] getAcceptedIssuers() { + return null; + } + public void checkClientTrusted( + java.security.cert.X509Certificate[] chain, + String authType) + throws java.security.cert.CertificateException { + // TODO Auto-generated method stub + + } + public void checkServerTrusted( + java.security.cert.X509Certificate[] chain, + String authType) + throws java.security.cert.CertificateException { + // TODO Auto-generated method stub + + } + }; + ctx.init(null, new TrustManager[] { tm }, null); + SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); + SchemeRegistry registry = new SchemeRegistry(); + registry.register(new Scheme("https", 8089, ssf)); + ThreadSafeClientConnManager mgr = new ThreadSafeClientConnManager(registry); + return new DefaultHttpClient(mgr, base.getParams()); + } catch (Exception ex) { + ex.printStackTrace(); + return null; + } + } + + protected String sendPostMsg(String message, String url) throws UnsupportedEncodingException, + IOException, ClientProtocolException { + + HttpClient httpclient = new DefaultHttpClient(); + if(isHttpsProtocol) + { + httpclient = wrapClient(httpclient); + } + HttpPost httppost = new HttpPost(url); + StringEntity myEntity = new StringEntity(message, UTF_8); + String auth = ""; + httppost.addHeader(AUTH, auth); + httppost.addHeader(CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); +// httppost.addHeader(CONTENT_TYPE, MediaType.TEXT_XML_VALUE); + httppost.setEntity(myEntity); + HttpResponse response = httpclient.execute(httppost); + HttpEntity resEntity = response.getEntity(); + String responseContent = ""; + if (resEntity != null) { + responseContent = EntityUtils.toString(resEntity, "UTF-8"); + EntityUtils.consume(resEntity); + } + httpclient.getConnectionManager().shutdown(); + return responseContent; + } + + protected String sendGetMsg(String message, String url) throws UnsupportedEncodingException, + IOException, ClientProtocolException { + + HttpClient httpclient = new DefaultHttpClient(); + if(isHttpsProtocol) + { + httpclient = wrapClient(httpclient); + } + HttpGet httpGet = new HttpGet(url); + StringEntity myEntity = new StringEntity(message, UTF_8); + String auth = ""; + httpGet.addHeader(AUTH, auth); + httpGet.addHeader(CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); + //httppost.addHeader(CONTENT_TYPE, MediaType.TEXT_XML_VALUE); + //((HttpResponse) httpGet).setEntity(myEntity); + HttpResponse response = httpclient.execute(httpGet); + HttpEntity resEntity = response.getEntity(); + String responseContent = ""; + if (resEntity != null) { + responseContent = EntityUtils.toString(resEntity, "UTF-8"); + responseContent.replaceAll("\r", "");// + EntityUtils.consume(resEntity); + } + httpclient.getConnectionManager().shutdown(); + return responseContent; + } +} diff --git a/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/test/java/com/nokia/vfcadaptor/vnfmdriver/controller/VnfmDriverAdaptorControllerTest.java b/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/test/java/com/nokia/vfcadaptor/vnfmdriver/controller/VnfmDriverAdaptorControllerTest.java new file mode 100644 index 00000000..69a5c853 --- /dev/null +++ b/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/test/java/com/nokia/vfcadaptor/vnfmdriver/controller/VnfmDriverAdaptorControllerTest.java @@ -0,0 +1,99 @@ +/* + * Copyright 2016-2017, Nokia Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.nokia.vfcadaptor.vnfmdriver.controller; + +import java.io.IOException; +import java.net.URISyntaxException; + +import org.apache.http.client.ClientProtocolException; +import org.junit.Before; +import org.junit.Test; + +public class VnfmDriverAdaptorControllerTest extends BaseControllerTestCase{ + + private String url; + private String basicUrl="http://localhost:8080/NvfmDriver/api/nokiavnfm/v1"; + private String funcPath; + + @Before + public void setUp() + { + isHttpsProtocol = false; + } + + @Test + public void testInstantiateVnf() throws URISyntaxException, ClientProtocolException, IOException { + funcPath = "/vnfmId_001/vnfs"; + url = basicUrl + funcPath; + String message = "{\"vnfInstanceName\":\"vnfInstanceName_001\",\"vnfPackageId\":\"1\"}"; + + String responseContent = sendPostMsg(message, url); + System.out.println("-------------------------------"); + System.out.println( " Initiate Response is " + responseContent); + } + + @Test + public void testTerminateVnf() throws URISyntaxException, ClientProtocolException, IOException { + funcPath = "/vnfmId_001/vnfs/vnfInstanceId_001/terminate"; + url = basicUrl + funcPath; + String message = "{\"terminationType\":\"graceful\"}"; + String responseContent = sendPostMsg(message, url); + System.out.println("-------------------------------"); + System.out.println("Terminate Response is " + responseContent); + } + + @Test + public void testQueryVnf() throws URISyntaxException, ClientProtocolException, IOException { + funcPath = "/vnfmId_001/vnfs/vnfInstanceId_001"; + url = basicUrl + funcPath; + String message = "{\"vnfInfo\":\"{\"nfInstanceId\":\"1\",\"vnfInstanceName\":\"vFW\",\"vnfInstanceDescription\":\"vFW in Nanjing TIC Edge\",\"vnfdId\":\"1\"}\"}"; + String responseContent = sendGetMsg(message, url); + + System.out.println("-------------------------------"); + System.out.println("QueryResponse is " + responseContent); + } + + @Test + public void testOperStatusVnf() throws URISyntaxException, ClientProtocolException, IOException { + funcPath = "/vnfmId_001/jobs/jobId_001&responseId=responseId_001"; + url = basicUrl + funcPath; + String message = "{\"jobId\":\"12345\",\"responseDescriptor\":\"{\"progress\":\"40\",\"status\":\"proccessing\"}\"}"; + String responseContent = sendGetMsg(message, url); + System.out.println("-------------------------------"); + System.out.println(" operStatus Response is " + responseContent); + } + + @Test + public void testScaleVnf() throws URISyntaxException, ClientProtocolException, IOException { + funcPath = "/vnfmId_001/vnfs/vnfInstanceId_001/scale"; + url = basicUrl + funcPath; + String message = "{\"type\":\"12345\",\"aspectId\":\"145\"}"; + String responseContent = sendPostMsg(message, url); + System.out.println("-------------------------------"); + System.out.println(" Scale Response is " + responseContent); + } + + @Test + public void testHealVnf() throws URISyntaxException, ClientProtocolException, IOException { + funcPath = "/vnfmId_001/vnfs/vnfInstanceId_001/heal"; + url = basicUrl + funcPath; + String message = "{\"action\":\"12345\"}"; + String responseContent = sendPostMsg(message, url); + System.out.println("-------------------------------"); + System.out.println(" Heal Response is " + responseContent); + } + +} diff --git a/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/test/java/com/nokia/vfcadaptor/vnfmdriver/controller/VnfmDriverControllerTest.java b/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/test/java/com/nokia/vfcadaptor/vnfmdriver/controller/VnfmDriverControllerTest.java new file mode 100644 index 00000000..cd6d93eb --- /dev/null +++ b/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/test/java/com/nokia/vfcadaptor/vnfmdriver/controller/VnfmDriverControllerTest.java @@ -0,0 +1,131 @@ +/* + * Copyright 2016-2017, Nokia Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.nokia.vfcadaptor.vnfmdriver.controller; + + +import static org.mockito.Mockito.when; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import org.apache.http.HttpStatus; +import org.json.JSONObject; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; + +import com.nokia.vfcadaptor.exception.VnfmDriverException; +import com.nokia.vfcadaptor.vnfmdriver.bo.InstantiateVnfRequest; +import com.nokia.vfcadaptor.vnfmdriver.bo.InstantiateVnfResponse; +import com.nokia.vfcadaptor.vnfmdriver.bo.TerminateVnfRequest; +import com.nokia.vfcadaptor.vnfmdriver.bo.TerminateVnfResponse; +import com.nokia.vfcadaptor.vnfmdriver.inf.VnfmDriverMgmrInf; + +//@RunWith(SpringJUnit4ClassRunner.class) +//@ContextConfiguration(locations={"file:src/main/webapp/WEB-INF/mvc-servlet.xml"}) +//@WebAppConfiguration(value = "src/main/webapp") +public class VnfmDriverControllerTest { + + @Mock + private VnfmDriverMgmrInf vnfmDriverMgmr; + + @InjectMocks + private VnfmDriverController controller; + + private MockMvc mockMvc; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + mockMvc = MockMvcBuilders.standaloneSetup(controller).build(); + } + + @Test + public void testInstantiateVnf() throws Exception + { + InstantiateVnfResponse mockResponse = new InstantiateVnfResponse(); + mockResponse.setJobId("job_001"); + mockResponse.setVnfInstanceId("vnfInstanceId_001"); + String jsonString = "{\"vnfInstanceName\":\"vnfInstanceName_001\",\"vnfPackageId\":\"1\"}"; + when(vnfmDriverMgmr.instantiateVnf(Mockito.any(InstantiateVnfRequest.class), Mockito.anyString())).thenReturn(mockResponse); + + String responseString = mockMvc.perform( + post("/nokiavnfm/v1/vnfmId_001/vnfs"). + characterEncoding("UTF-8"). + accept(MediaType.APPLICATION_JSON). + contentType(MediaType.APPLICATION_JSON). + content(jsonString)) + .andDo(print()) + .andExpect(status().isCreated()) + .andReturn().getResponse().getContentAsString(); + + JSONObject jsonObj = new JSONObject(responseString); + Assert.assertEquals("jobId is ", mockResponse.getJobId(), jsonObj.get("jobId")); + Assert.assertEquals("vnfInstanceId is ", mockResponse.getVnfInstanceId(), jsonObj.get("vnfInstanceId")); + } + + @Test + public void testTerminateVnfSuccess() throws Exception + { + TerminateVnfResponse mockResponse = new TerminateVnfResponse(); + mockResponse.setJobId("job_002"); + String jsonString = "{\"vnfInstanceId\":\"vnfInstanceId_001\"}"; + when(vnfmDriverMgmr.terminateVnf(Mockito.any(TerminateVnfRequest.class), Mockito.anyString(), Mockito.anyString())).thenReturn(mockResponse); + + String responseString = mockMvc.perform( + post("/nokiavnfm/v1/vnfmId_001/vnfs/vnfInstanceId_001/terminate"). + characterEncoding("UTF-8"). + accept(MediaType.APPLICATION_JSON). + contentType(MediaType.APPLICATION_JSON). + content(jsonString)) + .andDo(print()) + .andExpect(status().isCreated()) + .andReturn().getResponse().getContentAsString(); + + JSONObject jsonObj = new JSONObject(responseString); + Assert.assertEquals("jobId is ", mockResponse.getJobId(), jsonObj.get("jobId")); + } + @Test + public void testTerminateVnfException() throws Exception + { + TerminateVnfResponse mockResponse = new TerminateVnfResponse(); + mockResponse.setJobId("job_002"); + String jsonString = "{\"vnfInstanceId\":\"vnfInstanceId_001\"}"; + VnfmDriverException exception = new VnfmDriverException(HttpStatus.SC_BAD_REQUEST, "vnfInstanceId is wrong"); + when(vnfmDriverMgmr.terminateVnf(Mockito.any(TerminateVnfRequest.class), Mockito.anyString(), Mockito.anyString())).thenThrow(exception); + + String erroMsg = mockMvc.perform( + post("/nokiavnfm/v1/vnfmId_001/vnfs/vnfInstanceId_001/terminate"). + characterEncoding("UTF-8"). + accept(MediaType.APPLICATION_JSON). + contentType(MediaType.APPLICATION_JSON). + content(jsonString)) + .andDo(print()) + .andExpect(status().isBadRequest()) + .andReturn().getResponse().getErrorMessage() + ; + Assert.assertEquals("Error Message is ", exception.getMessage(), erroMsg); + } + +} |