Create seed code of svnfm vnfmdriver
[vfc/nfvo/driver/vnfm/svnfm.git] / nokia / vnfmdriver / vfcadaptorservice / vfcadaptor / src / test / java / com / nokia / vfcadaptor / http / client / HttpRequestProcessorTest.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.http.client;
18
19 import java.io.IOException;
20 import java.io.UnsupportedEncodingException;
21 import java.security.KeyManagementException;
22 import java.security.KeyStoreException;
23 import java.security.NoSuchAlgorithmException;
24
25 import org.apache.http.client.ClientProtocolException;
26 import org.apache.http.impl.client.HttpClientBuilder;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.http.MediaType;
32 import org.springframework.test.context.ContextConfiguration;
33 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
34 import org.springframework.test.context.web.WebAppConfiguration;
35 import org.springframework.test.web.servlet.MockMvc;
36 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
37 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
38 import org.springframework.web.bind.annotation.RequestMethod;
39 import org.springframework.web.context.WebApplicationContext;
40
41 import com.google.gson.Gson;
42 import com.nokia.vfcadaptor.vnfmdriver.bo.InstantiateVnfRequest;
43
44 @RunWith(SpringJUnit4ClassRunner.class)
45 @ContextConfiguration(locations={"file:src/main/webapp/WEB-INF/mvc-servlet.xml"})
46 @WebAppConfiguration
47 public class HttpRequestProcessorTest {
48         private String url;
49         private String basicUrl="http://localhost:8080/NvfmDriver/api/nokiavnfm/v1";
50     private String funcPath;
51     
52         private Gson gson = new Gson();
53     
54         @Autowired
55     private WebApplicationContext wac;
56     private MockMvc mockMvc;
57     @Before
58     public void setup() {
59         this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();   //¹¹ÔìMockMvc
60     }
61     
62     @Test
63     public void testP()
64     {
65         String message = "{\"vnfInstanceName\":\"vnfInstanceName_001\",\"vnfPackageId\":\"1\"}";
66         InstantiateVnfRequest request = gson.fromJson(message, InstantiateVnfRequest.class);
67         
68         System.out.println("vnfInstanceName = " + request.getVnfInstanceName());
69         
70         String result;
71                 try {
72                         result = mockMvc.perform(MockMvcRequestBuilders.post("/nokiavnfm/v1/vnfmId_001/vnfs")  
73                                 .contentType(MediaType.APPLICATION_JSON).content(message)  
74                                 .accept(MediaType.APPLICATION_JSON)) //Ö´ÐÐÇëÇó  
75                         .andReturn().getResponse().getContentAsString();
76                         System.out.println("result = " + result);
77                 } catch (UnsupportedEncodingException e) {
78                         // TODO Auto-generated catch block
79                         e.printStackTrace();
80                 } catch (Exception e) {
81                         // TODO Auto-generated catch block
82                         e.printStackTrace();
83                 }   
84         
85         
86     }
87 //    
88 //      @Test
89 //      public void testProcess() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException, ClientProtocolException, IOException
90 //      {
91 //              funcPath = "/vnfmId_001/vnfs";
92 //      url = basicUrl + funcPath;
93 //        String message = "{\"vnfInstanceName\":\"vnfInstanceName_001\",\"vnfPackageId\":\"1\"}";
94 //        
95 //              HttpClientBuilder httpClientBuilder = HttpClientUtils.createHttpClientBuilder();
96 //              HttpRequestProcessor processor = new HttpRequestProcessor(httpClientBuilder, RequestMethod.POST);
97 //              processor.addPostEntity(message);
98 //              String result = processor.process(url);
99 //              
100 //              System.out.println(result);
101 //      }
102 //      
103 //      @Test
104 //      public void testHttps() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException, ClientProtocolException, IOException
105 //      {
106 //              url = "https://www.baidu.com";
107 //              HttpClientBuilder httpClientBuilder = HttpClientUtils.createHttpClientBuilder();
108 //              HttpRequestProcessor processor = new HttpRequestProcessor(httpClientBuilder, RequestMethod.GET);
109 //              
110 //              String result = processor.process(url);
111 //              
112 //              System.out.println("The result is :" + result);
113 //      }
114 //      @Test
115 //      public void testHome() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException, ClientProtocolException, IOException
116 //      {
117 //              url = "http://172.24.208.168/";
118 //              HttpClientBuilder httpClientBuilder = HttpClientUtils.createHttpClientBuilder();
119 //              HttpRequestProcessor processor = new HttpRequestProcessor(httpClientBuilder, RequestMethod.GET);
120 //              
121 //              String result = processor.process(url);
122 //              
123 //              System.out.println("The result is :" + result);
124 //      }
125 }