Fix the docker build error for vfc-nfvo-wfengine
[vfc/nfvo/wfengine.git] / activiti-extension / src / test / java / org / onap / workflow / activitiext / restservicetask / HttpUtilTest.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.powermock.api.mockito.PowerMockito.mock;\r
19 \r
20 import org.activiti.engine.ActivitiException;\r
21 import org.activiti.engine.delegate.DelegateExecution;\r
22 import org.activiti.engine.delegate.Expression;\r
23 import org.activiti.engine.impl.persistence.entity.ExecutionEntity;\r
24 import org.junit.Assert;\r
25 import org.junit.Before;\r
26 import org.junit.Test;\r
27 import org.onap.workflow.activitiext.common.Parameter;\r
28 import org.onap.workflow.activitiext.common.RestInfo;\r
29 import org.powermock.api.mockito.PowerMockito;\r
30 \r
31 import com.alibaba.fastjson.JSON;\r
32 \r
33 public class HttpUtilTest {\r
34 \r
35   private HttpUtil httpUtil;\r
36 \r
37   @Before\r
38   public void setUp() {\r
39     httpUtil = new HttpUtil();\r
40   }\r
41 \r
42   @Test\r
43   public void testExecuteMethod() {\r
44 \r
45     DelegateExecution executionEntity = mock(ExecutionEntity.class);\r
46     try {\r
47       PowerMockito.when(executionEntity.getCurrentActivityId()).thenReturn("1111");\r
48       HttpResponseMessage msg = new HttpResponseMessage();\r
49       msg.setStatusCode(200);\r
50       msg.setResponseBody(null);\r
51       httpUtil.execute(executionEntity);\r
52       Assert.assertTrue(false);\r
53     } catch (Exception e) {\r
54       Assert.assertTrue(true);\r
55     }\r
56   }\r
57 \r
58   @Test\r
59   public void testHandleParam1() {\r
60 \r
61     try {\r
62       DelegateExecution executionEntity = mock(ExecutionEntity.class);\r
63       Parameter param = new Parameter();\r
64       param.setName("id");\r
65       param.setPosition("path");\r
66       param.setType("");\r
67       param.setValue("abc");\r
68 \r
69       RestInfo info = new RestInfo();\r
70       info.setMethod("GET");\r
71       info.setName("name");\r
72       info.setPath("/catalog/v1");\r
73       info.setRealUri("");\r
74       info.setUrl("csars");\r
75       info.setVersion("v1");\r
76 \r
77       httpUtil.handleParam(executionEntity, param, info);\r
78       Assert.assertTrue(true);\r
79     } catch (ActivitiException e) {\r
80       Assert.assertTrue(true);\r
81     }\r
82 \r
83   }\r
84 \r
85   @Test\r
86   public void testHandleParam2() {\r
87     try {\r
88       DelegateExecution executionEntity = mock(ExecutionEntity.class);\r
89       Parameter param = new Parameter();\r
90       param.setName("id");\r
91       param.setPosition("query");\r
92       param.setType("");\r
93       param.setValue("abc");\r
94 \r
95       RestInfo info = new RestInfo();\r
96       info.setMethod("GET");\r
97       info.setName("name");\r
98       info.setPath("/catalog/v1");\r
99       info.setRealUri("");\r
100       info.setUrl("csars");\r
101       info.setVersion("v1");\r
102 \r
103       httpUtil.handleParam(executionEntity, param, info);\r
104       Assert.assertTrue(true);\r
105     } catch (ActivitiException e) {\r
106       Assert.assertTrue(true);\r
107     }\r
108   }\r
109   \r
110   @Test\r
111   public void testHandleParam3() {\r
112     try {\r
113       DelegateExecution executionEntity = mock(ExecutionEntity.class);\r
114       Parameter param = new Parameter();\r
115       param.setName("id");\r
116       param.setPosition("body");\r
117       param.setType("");\r
118       param.setValue("{'value':{'progress':{'value':'60','valueSource':'String'},'errcode':{'value':'200','valueSource':'Variable'},'desc':{'value':'','valueSource':'String'}},'valueSource':'Definition'}");\r
119 \r
120       RestInfo info = new RestInfo();\r
121       info.setMethod("GET");\r
122       info.setName("name");\r
123       info.setPath("/catalog/v1");\r
124       info.setRealUri("");\r
125       info.setUrl("csars");\r
126       info.setVersion("v1");\r
127 \r
128       httpUtil.handleParam(executionEntity, param, info);\r
129       Assert.assertTrue(true);\r
130     } catch (ActivitiException e) {\r
131       Assert.assertTrue(true);\r
132     }\r
133   }\r
134 \r
135   @Test\r
136   public void testGetValue() {\r
137     DelegateExecution executionEntity = mock(ExecutionEntity.class);\r
138     Expression expression = mock(Expression.class);\r
139     Object res = new String("result");\r
140 \r
141     PowerMockito.when(expression.getValue(executionEntity)).thenReturn(res);\r
142 \r
143     Assert.assertEquals(res, httpUtil.getValue(expression, executionEntity));\r
144   }\r
145 \r
146   @SuppressWarnings("static-access")\r
147   @Test\r
148   public void testParsePlanExpression() {\r
149 \r
150     String exp = "[node0].[responseBody].[id]";\r
151     String json = "{'id':'123'}";\r
152     JSON obj = (JSON) JSON.parse(json);\r
153     HttpResponseMessage msg = new HttpResponseMessage();\r
154     msg.setStatusCode(200);\r
155     msg.setResponseBody(obj);\r
156 \r
157     DelegateExecution executionEntity = mock(ExecutionEntity.class);\r
158 \r
159     PowerMockito.when(executionEntity.getVariable("node0")).thenReturn(msg);\r
160 \r
161     Assert.assertEquals("123", httpUtil.parsePlanExpression(exp, executionEntity));\r
162   }\r
163 }\r