Fix the docker build error for vfc-nfvo-wfengine
[vfc/nfvo/wfengine.git] / wfenginemgrservice / src / test / java / org / onap / workflow / common / RestClientTest.java
1 /**\r
2  * Copyright 2017 ZTE Corporation.\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 package org.onap.workflow.common;\r
17 \r
18 import static org.powermock.api.mockito.PowerMockito.mock;\r
19 \r
20 import java.io.IOException;\r
21 \r
22 import org.apache.http.HttpEntity;\r
23 import org.apache.http.HttpHost;\r
24 import org.apache.http.HttpRequest;\r
25 import org.apache.http.StatusLine;\r
26 import org.apache.http.client.ClientProtocolException;\r
27 import org.apache.http.client.methods.CloseableHttpResponse;\r
28 import org.apache.http.conn.ClientConnectionManager;\r
29 import org.apache.http.impl.client.CloseableHttpClient;\r
30 import org.apache.http.impl.client.HttpClients;\r
31 import org.apache.http.params.HttpParams;\r
32 import org.apache.http.protocol.HttpContext;\r
33 import org.junit.Assert;\r
34 import org.junit.Test;\r
35 import org.junit.runner.RunWith;\r
36 import org.mockito.Mockito;\r
37 import org.onap.workflow.WorkflowAppConfig;\r
38 import org.onap.workflow.common.RestClient.HttpMethod;\r
39 import org.onap.workflow.entity.MsbClientConfig;\r
40 import org.onap.workflow.externalservice.entity.activitientitiy.ActivitiStartProcessRequest;\r
41 import org.powermock.api.mockito.PowerMockito;\r
42 import org.powermock.core.classloader.annotations.PrepareForTest;\r
43 import org.powermock.modules.junit4.PowerMockRunner;\r
44 \r
45 \r
46 @PrepareForTest({HttpClients.class})\r
47 @RunWith(PowerMockRunner.class)\r
48 public class RestClientTest {\r
49   @Test\r
50   public void testExecuteHttp() throws Exception {\r
51     HttpEntity httpEntity = mock(HttpEntity.class);\r
52     PowerMockito.mockStatic(HttpClients.class);\r
53     CloseableHttpClient httpclient = mock(CloseableHttpClient.class);\r
54     PowerMockito.when(HttpClients.createDefault()).thenReturn(httpclient);\r
55     PowerMockito.when(httpclient.execute(Mockito.any(HttpHost.class), Mockito.any(HttpRequest.class)))\r
56         .thenThrow(new IOException());\r
57     try{\r
58     RestClient.executeHttp(HttpMethod.POST, "127.0.0.1", 80, "test", httpEntity);\r
59     }catch(IOException e){\r
60       Assert.assertTrue(true);\r
61     }\r
62   }\r
63 \r
64   @Test\r
65   public void testExecuteHttpException() throws Exception {\r
66     HttpEntity httpEntity = mock(HttpEntity.class);\r
67     PowerMockito.mockStatic(HttpClients.class);\r
68     CloseableHttpClient tt = mock(CloseableHttpClient.class);\r
69     CloseableHttpResponse reponse = mock(CloseableHttpResponse.class);\r
70     StatusLine sl = mock(StatusLine.class);\r
71     PowerMockito.when(HttpClients.createDefault()).thenReturn(tt);\r
72     PowerMockito.when(tt.execute(Mockito.any(HttpHost.class), Mockito.any(HttpRequest.class)))\r
73         .thenReturn(reponse);\r
74     PowerMockito.when(reponse.getEntity()).thenReturn(httpEntity);\r
75     PowerMockito.when(reponse.getStatusLine()).thenReturn(sl);\r
76     RestClient.executeHttp(HttpMethod.POST, "127.0.0.1", 80, "test", httpEntity);\r
77     \r
78     WorkflowAppConfig workflowconfig = makeWorkFlowConfig("127.0.0.1", 80);\r
79     Config.setWorkflowAppConfig(workflowconfig);\r
80     RestClient.executeHttp(HttpMethod.POST, null, null, "test", httpEntity);\r
81   }\r
82   \r
83   private WorkflowAppConfig makeWorkFlowConfig(String msbSvrIp, int msbSvrPort) {\r
84     MsbClientConfig msbClientConfig = new MsbClientConfig();\r
85     msbClientConfig.setMsbSvrIp(msbSvrIp);\r
86     msbClientConfig.setMsbSvrPort(msbSvrPort);\r
87     WorkflowAppConfig workflowconfig = new WorkflowAppConfig();\r
88     workflowconfig.setMsbClientConfig(msbClientConfig);\r
89     return workflowconfig;\r
90   }\r
91   \r
92   @Test\r
93   public void testExecuteHttpDeleteDeploy() throws Exception {\r
94     HttpEntity httpEntity = mock(HttpEntity.class);\r
95     PowerMockito.mockStatic(HttpClients.class);\r
96     CloseableHttpClient tt = mock(CloseableHttpClient.class);\r
97     CloseableHttpResponse reponse = mock(CloseableHttpResponse.class);\r
98     PowerMockito.when(HttpClients.createDefault()).thenReturn(tt);\r
99     PowerMockito.when(tt.execute(Mockito.any(HttpHost.class), Mockito.any(HttpRequest.class)))\r
100         .thenReturn(reponse);\r
101     PowerMockito.when(reponse.getEntity()).thenReturn(httpEntity);\r
102     RestClient.executeHttpDeleteDeploy(HttpMethod.DELETE, "127.0.0.1", 80, "test");\r
103     \r
104     WorkflowAppConfig workflowconfig = makeWorkFlowConfig("127.0.0.1", 80);\r
105     Config.setWorkflowAppConfig(workflowconfig);\r
106     RestClient.executeHttpDeleteDeploy(HttpMethod.DELETE, null, null, "test");\r
107   }\r
108 \r
109   @Test\r
110   public void testExecuteHttpStartIntance() throws Exception {\r
111     ActivitiStartProcessRequest activitiStartProcessRequest =\r
112         mock(ActivitiStartProcessRequest.class);\r
113 \r
114     HttpEntity httpEntity = mock(HttpEntity.class);\r
115     PowerMockito.mockStatic(HttpClients.class);\r
116     CloseableHttpClient tt = mock(CloseableHttpClient.class);\r
117     CloseableHttpResponse reponse = mock(CloseableHttpResponse.class);\r
118     PowerMockito.when(HttpClients.createDefault()).thenReturn(tt);\r
119     PowerMockito.when(tt.execute(Mockito.any(HttpHost.class), Mockito.any(HttpRequest.class)))\r
120         .thenReturn(reponse);\r
121     PowerMockito.when(reponse.getEntity()).thenReturn(httpEntity);\r
122     RestClient.executeHttpStartIntance(HttpMethod.POST, "127.0.0.1", 80, "test",activitiStartProcessRequest);\r
123 \r
124     WorkflowAppConfig workflowconfig = makeWorkFlowConfig("127.0.0.1", 80);\r
125     Config.setWorkflowAppConfig(workflowconfig);\r
126     RestClient.executeHttpStartIntance(HttpMethod.POST, null, null, "test", activitiStartProcessRequest);\r
127   }\r
128   \r
129   @Test\r
130   public void testgetRequest() {\r
131     HttpRequest request = RestClient.getRequest(HttpMethod.GET, "/test/", null);\r
132     Assert.assertTrue (request.getRequestLine().getMethod().equals(HttpMethod.GET.toString()));\r
133     request = RestClient.getRequest(HttpMethod.POST, "/test/", null);\r
134     Assert.assertTrue (request.getRequestLine().getMethod().equals(HttpMethod.POST.toString()));\r
135     request = RestClient.getRequest(HttpMethod.DELETE, "/test/", null);\r
136     Assert.assertTrue (request.getRequestLine().getMethod().equals(HttpMethod.DELETE.toString()));\r
137     request = RestClient.getRequest(HttpMethod.PUT, "/test/", null);\r
138     Assert.assertTrue (request.getRequestLine().getMethod().equals(HttpMethod.PUT.toString()));\r
139     try {\r
140       request = RestClient.getRequest(null, "/test/", null);\r
141     } catch (NullPointerException e) {\r
142       Assert.assertTrue (true);\r
143     }\r
144   }\r
145   \r
146   @Test\r
147   public void testPost() throws Exception {\r
148     HttpEntity httpEntity = mock(HttpEntity.class);\r
149     PowerMockito.mockStatic(HttpClients.class);\r
150     CloseableHttpClient tt = mock(CloseableHttpClient.class);\r
151     CloseableHttpResponse reponse = mock(CloseableHttpResponse.class);\r
152     \r
153     PowerMockito.when(HttpClients.createDefault()).thenReturn(tt);\r
154     PowerMockito.when(tt.execute(Mockito.any(HttpHost.class), Mockito.any(HttpRequest.class)))\r
155         .thenReturn(reponse);\r
156     PowerMockito.when(reponse.getEntity()).thenReturn(httpEntity);\r
157     WorkflowAppConfig workflowconfig = makeWorkFlowConfig("127.0.0.1", 80);\r
158     Config.setWorkflowAppConfig(workflowconfig);\r
159     RestClient.post("127.0.0.1", 80, "test", null);\r
160     RestClient.post("127.0.0.1", 80, "test");\r
161   }\r
162   \r
163   @Test\r
164   public void testCloseHttpClient(){\r
165     Assert.assertTrue(RestClient.closeHttpClient(mock(CloseableHttpClient.class)));\r
166     Assert.assertTrue(RestClient.closeHttpClient(null));\r
167     Assert.assertFalse(RestClient.closeHttpClient(new CloseableHttpClientTest()));\r
168   }\r
169   \r
170   public class CloseableHttpClientTest extends CloseableHttpClient{\r
171     @Override\r
172     public void close() throws IOException {\r
173       throw new IOException();\r
174     }\r
175 \r
176     @Override\r
177     protected CloseableHttpResponse doExecute(HttpHost target, HttpRequest request,\r
178         HttpContext context) throws IOException, ClientProtocolException {\r
179       return null;\r
180     }\r
181 \r
182     @SuppressWarnings("deprecation")\r
183     @Override\r
184     public HttpParams getParams() {\r
185       return null;\r
186     }\r
187 \r
188     @SuppressWarnings("deprecation")\r
189     @Override\r
190     public ClientConnectionManager getConnectionManager() {\r
191       return null;\r
192     }\r
193   }\r
194 }\r