Add unit test for vfc-nfvo-wfengine
[vfc/nfvo/wfengine.git] / activiti-extension / src / test / java / org / onap / workflow / activitiext / restservicetask / HighLevelRestApiTest.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.activitiext.restservicetask;
17
18 import static org.hamcrest.CoreMatchers.is;
19 import static org.hamcrest.MatcherAssert.assertThat;
20
21 import org.apache.commons.httpclient.HttpMethodBase;
22 import org.apache.commons.httpclient.NameValuePair;
23 import org.apache.commons.httpclient.methods.GetMethod;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.junit.Assert;
27 import org.junit.runner.RunWith;
28 import org.mockito.Mockito;
29 import org.onap.workflow.activitiext.common.RestInfo;
30 import org.powermock.api.mockito.PowerMockito;
31 import org.powermock.core.classloader.annotations.PrepareForTest;
32 import org.powermock.modules.junit4.PowerMockRunner;
33
34 @PrepareForTest({LowLevelRestApi.class})
35 @RunWith(PowerMockRunner.class)
36 public class HighLevelRestApiTest {
37
38   private HighLevelRestApi highLevelRestApi;
39
40   @Before
41   public void setUp() {
42     highLevelRestApi = new HighLevelRestApi();
43   }
44
45   @Test
46   public void testInvoke() {
47
48     try {
49       RestInfo info = new RestInfo();
50       info.setMethod("GET");
51       info.setName("name");
52       info.setPath("/catalog/v1");
53       info.setUrl("csars");
54       info.setRealUri("");
55       info.setVersion("v1");
56
57       HttpResponseMessage msg = new HttpResponseMessage();
58       msg.setStatusCode(200);
59       msg.setResponseBody(null);
60       PowerMockito.mockStatic(LowLevelRestApi.class);
61       PowerMockito.when(LowLevelRestApi.executeHttpMethod(Mockito.anyObject())).thenReturn(msg);
62
63       assertThat(HighLevelRestApi.invoke(info), is(msg));;
64     } catch (Exception e) {
65       assert (false);
66     }
67   }
68
69   @Test
70   public void testGet() {
71
72     try {
73       HttpResponseMessage msg = new HttpResponseMessage();
74       msg.setStatusCode(200);
75       msg.setResponseBody(null);
76       PowerMockito.mockStatic(LowLevelRestApi.class);
77       PowerMockito.when(LowLevelRestApi.executeHttpMethod(Mockito.anyObject())).thenReturn(msg);
78
79       String uri = "10.74.148.107/openoapi/catalog/v1/dbtest?id=5";
80       String acceptValue = "application/json";
81       String contentTypeValue = "application/json";
82
83       assertThat(HighLevelRestApi.Get(uri, acceptValue, contentTypeValue), is(msg));
84     } catch (Exception e) {
85       assert (false);
86     }
87   }
88
89   @Test
90   public void testPost() {
91
92     try {
93       HttpResponseMessage msg = new HttpResponseMessage();
94       msg.setStatusCode(200);
95       msg.setResponseBody(null);
96       PowerMockito.mockStatic(LowLevelRestApi.class);
97       PowerMockito.when(LowLevelRestApi.executeHttpMethod(Mockito.anyObject())).thenReturn(msg);
98
99       String uri = "10.74.148.107/openoapi/catalog/v1/dbtest?id=5";
100       String requestBody = "{'type':'NFAR'}";
101       String acceptValue = "application/json";
102       String contentTypeValue = "application/json";
103
104       assertThat(HighLevelRestApi.Post(uri, requestBody, acceptValue, contentTypeValue), is(msg));
105     } catch (Exception e) {
106       assert (false);
107     }
108   }
109
110   @Test
111   public void testDelete() {
112
113     try {
114       HttpResponseMessage msg = new HttpResponseMessage();
115       msg.setStatusCode(200);
116       msg.setResponseBody(null);
117       PowerMockito.mockStatic(LowLevelRestApi.class);
118       PowerMockito.when(LowLevelRestApi.executeHttpMethod(Mockito.anyObject())).thenReturn(msg);
119
120       String uri = "10.74.148.107/openoapi/catalog/v1/csars/aa1bc611c9fbc08247d5ea71fd67ec3f";
121       String acceptValue = "application/json";
122       String contentTypeValue = "application/json";
123
124       assertThat(HighLevelRestApi.Delete(uri, acceptValue, contentTypeValue), is(msg));
125     } catch (Exception e) {
126       assert (false);
127     }
128   }
129
130   @Test
131   public void testPut() {
132
133     try {
134       HttpResponseMessage msg = new HttpResponseMessage();
135       msg.setStatusCode(200);
136       msg.setResponseBody(null);
137       PowerMockito.mockStatic(LowLevelRestApi.class);
138       PowerMockito.when(LowLevelRestApi.executeHttpMethod(Mockito.anyObject())).thenReturn(msg);
139
140       String uri = "10.74.148.107/openoapi/catalog/v1/csars";
141       String requestBody = "{'type':'NFAR'}";
142       String acceptValue = "application/json";
143       String contentTypeValue = "application/json";
144
145       assertThat(HighLevelRestApi.Put(uri, requestBody, acceptValue, contentTypeValue), is(msg));
146     } catch (Exception e) {
147       assert (false);
148     }
149   }
150
151   @SuppressWarnings("static-access")
152   @Test
153   public void testCreateNameValuePairArrayFromQuery() {
154     NameValuePair[] pair = new NameValuePair[2];
155     NameValuePair p1 = new NameValuePair();
156     p1.setName("name");
157     p1.setValue("liuyao");
158     NameValuePair p2 = new NameValuePair();
159     p2.setName("pwd");
160     p2.setValue("zte");
161
162     pair[0] = p1;
163     pair[1] = p2;
164
165     String query = "name=liuyao&pwd=zte";
166
167     assertThat(highLevelRestApi.createNameValuePairArrayFromQuery(query), is(pair));
168   }
169
170   @Test
171   public void testSetAcceptHeader() {
172     try {
173       HttpMethodBase method = new GetMethod();
174       HighLevelRestApi.setAcceptHeader(method, "[application/json]");
175       Assert.assertTrue(true);
176     } catch (Exception e) {
177       Assert.assertTrue(false);
178     }
179   }
180
181   @Test
182   public void testSetContentTypeHeader() {
183
184     try {
185       HttpMethodBase method = new GetMethod();
186       HighLevelRestApi.setContentTypeHeader(method, "[application/json]");
187       Assert.assertTrue(true);
188     } catch (Exception e) {
189       Assert.assertTrue(false);
190     }
191   }
192
193 }