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