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