Add unit test for vfc-nfvo-wfengine
[vfc/nfvo/wfengine.git] / wfenginemgrservice / src / test / java / org / onap / workflow / resources / RestClientTest.java
1 /**
2  * Copyright 2017 ZTE 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 package org.onap.workflow.resources;
17
18 import static org.powermock.api.mockito.PowerMockito.mock;
19
20 import org.apache.http.HttpEntity;
21 import org.apache.http.HttpHost;
22 import org.apache.http.HttpRequest;
23 import org.apache.http.client.methods.CloseableHttpResponse;
24 import org.apache.http.impl.client.CloseableHttpClient;
25 import org.apache.http.impl.client.HttpClients;
26 import org.junit.After;
27 import org.junit.AfterClass;
28 import org.junit.Before;
29 import org.junit.BeforeClass;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.Mockito;
33 import org.onap.workflow.common.RestClient;
34 import org.onap.workflow.common.RestClient.HttpMethod;
35 import org.onap.workflow.externalservice.entity.activitientitiy.ActivitiStartProcessRequest;
36 import org.powermock.api.mockito.PowerMockito;
37 import org.powermock.core.classloader.annotations.PowerMockIgnore;
38 import org.powermock.core.classloader.annotations.PrepareForTest;
39 import org.powermock.modules.junit4.PowerMockRunner;
40
41
42 @PrepareForTest({RestClient.class, HttpClients.class})
43 @RunWith(PowerMockRunner.class)
44 @PowerMockIgnore("javax.net.ssl.*")
45 public class RestClientTest {
46   private RestClient restClient;
47   @BeforeClass
48   public static void setUpBeforeClass() throws Exception {}
49
50   @AfterClass
51   public static void tearDownAfterClass() throws Exception {}
52
53   @Before
54   public void setUp() throws Exception {
55     restClient = new RestClient();
56
57   }
58
59   @After
60   public void tearDown() throws Exception {}
61
62   @SuppressWarnings("static-access")
63   @Test
64   public final void testExecuteHttp() throws Exception {
65     HttpEntity httpEntity = mock(HttpEntity.class);
66     PowerMockito.mockStatic(HttpClients.class);
67     CloseableHttpClient tt = mock(CloseableHttpClient.class);
68     CloseableHttpResponse reponse = mock(CloseableHttpResponse.class);
69     PowerMockito.when(HttpClients.createDefault()).thenReturn(tt);
70     PowerMockito.when(tt.execute(Mockito.any(HttpHost.class), Mockito.any(HttpRequest.class)))
71         .thenReturn(reponse);
72     PowerMockito.when(reponse.getEntity()).thenReturn(httpEntity);
73     restClient.executeHttp(HttpMethod.POST, "127.0.0.1", 80, "test", httpEntity);
74   }
75
76   @SuppressWarnings("static-access")
77   @Test
78   public final void testExecuteHttpDeleteDeploy() throws Exception {
79     HttpEntity httpEntity = mock(HttpEntity.class);
80     PowerMockito.mockStatic(HttpClients.class);
81     CloseableHttpClient tt = mock(CloseableHttpClient.class);
82     CloseableHttpResponse reponse = mock(CloseableHttpResponse.class);
83     PowerMockito.when(HttpClients.createDefault()).thenReturn(tt);
84     PowerMockito.when(tt.execute(Mockito.any(HttpHost.class), Mockito.any(HttpRequest.class)))
85         .thenReturn(reponse);
86     PowerMockito.when(reponse.getEntity()).thenReturn(httpEntity);
87     restClient.executeHttpDeleteDeploy(HttpMethod.DELETE, "127.0.0.1", 80, "test");
88   }
89
90   @SuppressWarnings("static-access")
91   @Test
92   public final void testExecuteHttpStartIntance() throws Exception {
93
94     ActivitiStartProcessRequest activitiStartProcessRequest =
95         mock(ActivitiStartProcessRequest.class);
96
97     HttpEntity httpEntity = mock(HttpEntity.class);
98     PowerMockito.mockStatic(HttpClients.class);
99     CloseableHttpClient tt = mock(CloseableHttpClient.class);
100     CloseableHttpResponse reponse = mock(CloseableHttpResponse.class);
101     PowerMockito.when(HttpClients.createDefault()).thenReturn(tt);
102     PowerMockito.when(tt.execute(Mockito.any(HttpHost.class), Mockito.any(HttpRequest.class)))
103         .thenReturn(reponse);
104     PowerMockito.when(reponse.getEntity()).thenReturn(httpEntity);
105     restClient.executeHttpStartIntance(HttpMethod.POST, "127.0.0.1", 80, "test",activitiStartProcessRequest);
106
107   }
108 }