Fix the docker build error for vfc-nfvo-wfengine
[vfc/nfvo/wfengine.git] / wfenginemgrservice / src / test / java / org / onap / workflow / common / RestClientTest.java
index 418cff6..691577a 100644 (file)
-/**
- * Copyright 2017 ZTE 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 org.onap.workflow.common;
-
-import static org.powermock.api.mockito.PowerMockito.mock;
-
-import org.apache.http.HttpEntity;
-import org.apache.http.HttpHost;
-import org.apache.http.HttpRequest;
-import org.apache.http.client.methods.CloseableHttpResponse;
-import org.apache.http.impl.client.CloseableHttpClient;
-import org.apache.http.impl.client.HttpClients;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.Mockito;
-import org.onap.workflow.common.RestClient.HttpMethod;
-import org.onap.workflow.externalservice.entity.activitientitiy.ActivitiStartProcessRequest;
-import org.powermock.api.mockito.PowerMockito;
-import org.powermock.core.classloader.annotations.PowerMockIgnore;
-import org.powermock.core.classloader.annotations.PrepareForTest;
-import org.powermock.modules.junit4.PowerMockRunner;
-
-
-@PrepareForTest({RestClient.class, HttpClients.class})
-@RunWith(PowerMockRunner.class)
-@PowerMockIgnore("javax.net.ssl.*")
-public class RestClientTest {
-  private RestClient restClient;
-  @Before
-  public void setUp() throws Exception {
-    restClient = new RestClient();
-  }
-
-  @After
-  public void tearDown() throws Exception {}
-
-  @SuppressWarnings("static-access")
-  @Test
-  public final void testExecuteHttp() throws Exception {
-    HttpEntity httpEntity = mock(HttpEntity.class);
-    PowerMockito.mockStatic(HttpClients.class);
-    CloseableHttpClient tt = mock(CloseableHttpClient.class);
-    CloseableHttpResponse reponse = mock(CloseableHttpResponse.class);
-    
-    PowerMockito.when(HttpClients.createDefault()).thenReturn(tt);
-    PowerMockito.when(tt.execute(Mockito.any(HttpHost.class), Mockito.any(HttpRequest.class)))
-        .thenReturn(reponse);
-    PowerMockito.when(reponse.getEntity()).thenReturn(httpEntity);
-    
-    restClient.executeHttp(HttpMethod.POST, "127.0.0.1", 80, "test", httpEntity);
-  }
-
-  @SuppressWarnings("static-access")
-  @Test
-  public final void testExecuteHttpDeleteDeploy() throws Exception {
-    HttpEntity httpEntity = mock(HttpEntity.class);
-    PowerMockito.mockStatic(HttpClients.class);
-    CloseableHttpClient tt = mock(CloseableHttpClient.class);
-    CloseableHttpResponse reponse = mock(CloseableHttpResponse.class);
-    PowerMockito.when(HttpClients.createDefault()).thenReturn(tt);
-    PowerMockito.when(tt.execute(Mockito.any(HttpHost.class), Mockito.any(HttpRequest.class)))
-        .thenReturn(reponse);
-    PowerMockito.when(reponse.getEntity()).thenReturn(httpEntity);
-    restClient.executeHttpDeleteDeploy(HttpMethod.DELETE, "127.0.0.1", 80, "test");
-  }
-
-  @SuppressWarnings("static-access")
-  @Test
-  public final void testExecuteHttpStartIntance() throws Exception {
-
-    ActivitiStartProcessRequest activitiStartProcessRequest =
-        mock(ActivitiStartProcessRequest.class);
-
-    HttpEntity httpEntity = mock(HttpEntity.class);
-    PowerMockito.mockStatic(HttpClients.class);
-    CloseableHttpClient tt = mock(CloseableHttpClient.class);
-    CloseableHttpResponse reponse = mock(CloseableHttpResponse.class);
-    PowerMockito.when(HttpClients.createDefault()).thenReturn(tt);
-    PowerMockito.when(tt.execute(Mockito.any(HttpHost.class), Mockito.any(HttpRequest.class)))
-        .thenReturn(reponse);
-    PowerMockito.when(reponse.getEntity()).thenReturn(httpEntity);
-    restClient.executeHttpStartIntance(HttpMethod.POST, "127.0.0.1", 80, "test",activitiStartProcessRequest);
-
-  }
-}
+/**\r
+ * Copyright 2017 ZTE Corporation.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *     http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package org.onap.workflow.common;\r
+\r
+import static org.powermock.api.mockito.PowerMockito.mock;\r
+\r
+import java.io.IOException;\r
+\r
+import org.apache.http.HttpEntity;\r
+import org.apache.http.HttpHost;\r
+import org.apache.http.HttpRequest;\r
+import org.apache.http.StatusLine;\r
+import org.apache.http.client.ClientProtocolException;\r
+import org.apache.http.client.methods.CloseableHttpResponse;\r
+import org.apache.http.conn.ClientConnectionManager;\r
+import org.apache.http.impl.client.CloseableHttpClient;\r
+import org.apache.http.impl.client.HttpClients;\r
+import org.apache.http.params.HttpParams;\r
+import org.apache.http.protocol.HttpContext;\r
+import org.junit.Assert;\r
+import org.junit.Test;\r
+import org.junit.runner.RunWith;\r
+import org.mockito.Mockito;\r
+import org.onap.workflow.WorkflowAppConfig;\r
+import org.onap.workflow.common.RestClient.HttpMethod;\r
+import org.onap.workflow.entity.MsbClientConfig;\r
+import org.onap.workflow.externalservice.entity.activitientitiy.ActivitiStartProcessRequest;\r
+import org.powermock.api.mockito.PowerMockito;\r
+import org.powermock.core.classloader.annotations.PrepareForTest;\r
+import org.powermock.modules.junit4.PowerMockRunner;\r
+\r
+\r
+@PrepareForTest({HttpClients.class})\r
+@RunWith(PowerMockRunner.class)\r
+public class RestClientTest {\r
+  @Test\r
+  public void testExecuteHttp() throws Exception {\r
+    HttpEntity httpEntity = mock(HttpEntity.class);\r
+    PowerMockito.mockStatic(HttpClients.class);\r
+    CloseableHttpClient httpclient = mock(CloseableHttpClient.class);\r
+    PowerMockito.when(HttpClients.createDefault()).thenReturn(httpclient);\r
+    PowerMockito.when(httpclient.execute(Mockito.any(HttpHost.class), Mockito.any(HttpRequest.class)))\r
+        .thenThrow(new IOException());\r
+    try{\r
+    RestClient.executeHttp(HttpMethod.POST, "127.0.0.1", 80, "test", httpEntity);\r
+    }catch(IOException e){\r
+      Assert.assertTrue(true);\r
+    }\r
+  }\r
+\r
+  @Test\r
+  public void testExecuteHttpException() throws Exception {\r
+    HttpEntity httpEntity = mock(HttpEntity.class);\r
+    PowerMockito.mockStatic(HttpClients.class);\r
+    CloseableHttpClient tt = mock(CloseableHttpClient.class);\r
+    CloseableHttpResponse reponse = mock(CloseableHttpResponse.class);\r
+    StatusLine sl = mock(StatusLine.class);\r
+    PowerMockito.when(HttpClients.createDefault()).thenReturn(tt);\r
+    PowerMockito.when(tt.execute(Mockito.any(HttpHost.class), Mockito.any(HttpRequest.class)))\r
+        .thenReturn(reponse);\r
+    PowerMockito.when(reponse.getEntity()).thenReturn(httpEntity);\r
+    PowerMockito.when(reponse.getStatusLine()).thenReturn(sl);\r
+    RestClient.executeHttp(HttpMethod.POST, "127.0.0.1", 80, "test", httpEntity);\r
+    \r
+    WorkflowAppConfig workflowconfig = makeWorkFlowConfig("127.0.0.1", 80);\r
+    Config.setWorkflowAppConfig(workflowconfig);\r
+    RestClient.executeHttp(HttpMethod.POST, null, null, "test", httpEntity);\r
+  }\r
+  \r
+  private WorkflowAppConfig makeWorkFlowConfig(String msbSvrIp, int msbSvrPort) {\r
+    MsbClientConfig msbClientConfig = new MsbClientConfig();\r
+    msbClientConfig.setMsbSvrIp(msbSvrIp);\r
+    msbClientConfig.setMsbSvrPort(msbSvrPort);\r
+    WorkflowAppConfig workflowconfig = new WorkflowAppConfig();\r
+    workflowconfig.setMsbClientConfig(msbClientConfig);\r
+    return workflowconfig;\r
+  }\r
+  \r
+  @Test\r
+  public void testExecuteHttpDeleteDeploy() throws Exception {\r
+    HttpEntity httpEntity = mock(HttpEntity.class);\r
+    PowerMockito.mockStatic(HttpClients.class);\r
+    CloseableHttpClient tt = mock(CloseableHttpClient.class);\r
+    CloseableHttpResponse reponse = mock(CloseableHttpResponse.class);\r
+    PowerMockito.when(HttpClients.createDefault()).thenReturn(tt);\r
+    PowerMockito.when(tt.execute(Mockito.any(HttpHost.class), Mockito.any(HttpRequest.class)))\r
+        .thenReturn(reponse);\r
+    PowerMockito.when(reponse.getEntity()).thenReturn(httpEntity);\r
+    RestClient.executeHttpDeleteDeploy(HttpMethod.DELETE, "127.0.0.1", 80, "test");\r
+    \r
+    WorkflowAppConfig workflowconfig = makeWorkFlowConfig("127.0.0.1", 80);\r
+    Config.setWorkflowAppConfig(workflowconfig);\r
+    RestClient.executeHttpDeleteDeploy(HttpMethod.DELETE, null, null, "test");\r
+  }\r
+\r
+  @Test\r
+  public void testExecuteHttpStartIntance() throws Exception {\r
+    ActivitiStartProcessRequest activitiStartProcessRequest =\r
+        mock(ActivitiStartProcessRequest.class);\r
+\r
+    HttpEntity httpEntity = mock(HttpEntity.class);\r
+    PowerMockito.mockStatic(HttpClients.class);\r
+    CloseableHttpClient tt = mock(CloseableHttpClient.class);\r
+    CloseableHttpResponse reponse = mock(CloseableHttpResponse.class);\r
+    PowerMockito.when(HttpClients.createDefault()).thenReturn(tt);\r
+    PowerMockito.when(tt.execute(Mockito.any(HttpHost.class), Mockito.any(HttpRequest.class)))\r
+        .thenReturn(reponse);\r
+    PowerMockito.when(reponse.getEntity()).thenReturn(httpEntity);\r
+    RestClient.executeHttpStartIntance(HttpMethod.POST, "127.0.0.1", 80, "test",activitiStartProcessRequest);\r
+\r
+    WorkflowAppConfig workflowconfig = makeWorkFlowConfig("127.0.0.1", 80);\r
+    Config.setWorkflowAppConfig(workflowconfig);\r
+    RestClient.executeHttpStartIntance(HttpMethod.POST, null, null, "test", activitiStartProcessRequest);\r
+  }\r
+  \r
+  @Test\r
+  public void testgetRequest() {\r
+    HttpRequest request = RestClient.getRequest(HttpMethod.GET, "/test/", null);\r
+    Assert.assertTrue (request.getRequestLine().getMethod().equals(HttpMethod.GET.toString()));\r
+    request = RestClient.getRequest(HttpMethod.POST, "/test/", null);\r
+    Assert.assertTrue (request.getRequestLine().getMethod().equals(HttpMethod.POST.toString()));\r
+    request = RestClient.getRequest(HttpMethod.DELETE, "/test/", null);\r
+    Assert.assertTrue (request.getRequestLine().getMethod().equals(HttpMethod.DELETE.toString()));\r
+    request = RestClient.getRequest(HttpMethod.PUT, "/test/", null);\r
+    Assert.assertTrue (request.getRequestLine().getMethod().equals(HttpMethod.PUT.toString()));\r
+    try {\r
+      request = RestClient.getRequest(null, "/test/", null);\r
+    } catch (NullPointerException e) {\r
+      Assert.assertTrue (true);\r
+    }\r
+  }\r
+  \r
+  @Test\r
+  public void testPost() throws Exception {\r
+    HttpEntity httpEntity = mock(HttpEntity.class);\r
+    PowerMockito.mockStatic(HttpClients.class);\r
+    CloseableHttpClient tt = mock(CloseableHttpClient.class);\r
+    CloseableHttpResponse reponse = mock(CloseableHttpResponse.class);\r
+    \r
+    PowerMockito.when(HttpClients.createDefault()).thenReturn(tt);\r
+    PowerMockito.when(tt.execute(Mockito.any(HttpHost.class), Mockito.any(HttpRequest.class)))\r
+        .thenReturn(reponse);\r
+    PowerMockito.when(reponse.getEntity()).thenReturn(httpEntity);\r
+    WorkflowAppConfig workflowconfig = makeWorkFlowConfig("127.0.0.1", 80);\r
+    Config.setWorkflowAppConfig(workflowconfig);\r
+    RestClient.post("127.0.0.1", 80, "test", null);\r
+    RestClient.post("127.0.0.1", 80, "test");\r
+  }\r
+  \r
+  @Test\r
+  public void testCloseHttpClient(){\r
+    Assert.assertTrue(RestClient.closeHttpClient(mock(CloseableHttpClient.class)));\r
+    Assert.assertTrue(RestClient.closeHttpClient(null));\r
+    Assert.assertFalse(RestClient.closeHttpClient(new CloseableHttpClientTest()));\r
+  }\r
+  \r
+  public class CloseableHttpClientTest extends CloseableHttpClient{\r
+    @Override\r
+    public void close() throws IOException {\r
+      throw new IOException();\r
+    }\r
+\r
+    @Override\r
+    protected CloseableHttpResponse doExecute(HttpHost target, HttpRequest request,\r
+        HttpContext context) throws IOException, ClientProtocolException {\r
+      return null;\r
+    }\r
+\r
+    @SuppressWarnings("deprecation")\r
+    @Override\r
+    public HttpParams getParams() {\r
+      return null;\r
+    }\r
+\r
+    @SuppressWarnings("deprecation")\r
+    @Override\r
+    public ClientConnectionManager getConnectionManager() {\r
+      return null;\r
+    }\r
+  }\r
+}\r