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