2 * Copyright 2017 ZTE Corporation.
\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
8 * http://www.apache.org/licenses/LICENSE-2.0
\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
16 package org.onap.workflow.activitiext.restservicetask;
\r
18 import static org.powermock.api.mockito.PowerMockito.mock;
\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
31 import com.alibaba.fastjson.JSON;
\r
33 public class HttpUtilTest {
\r
35 private HttpUtil httpUtil;
\r
38 public void setUp() {
\r
39 httpUtil = new HttpUtil();
\r
43 public void testExecuteMethod() {
\r
45 DelegateExecution executionEntity = mock(ExecutionEntity.class);
\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
59 public void testHandleParam1() {
\r
62 DelegateExecution executionEntity = mock(ExecutionEntity.class);
\r
63 Parameter param = new Parameter();
\r
64 param.setName("id");
\r
65 param.setPosition("path");
\r
67 param.setValue("abc");
\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
77 httpUtil.handleParam(executionEntity, param, info);
\r
78 Assert.assertTrue(true);
\r
79 } catch (ActivitiException e) {
\r
80 Assert.assertTrue(true);
\r
86 public void testHandleParam2() {
\r
88 DelegateExecution executionEntity = mock(ExecutionEntity.class);
\r
89 Parameter param = new Parameter();
\r
90 param.setName("id");
\r
91 param.setPosition("query");
\r
93 param.setValue("abc");
\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
103 httpUtil.handleParam(executionEntity, param, info);
\r
104 Assert.assertTrue(true);
\r
105 } catch (ActivitiException e) {
\r
106 Assert.assertTrue(true);
\r
111 public void testHandleParam3() {
\r
113 DelegateExecution executionEntity = mock(ExecutionEntity.class);
\r
114 Parameter param = new Parameter();
\r
115 param.setName("id");
\r
116 param.setPosition("body");
\r
118 param.setValue("{'value':{'progress':{'value':'60','valueSource':'String'},'errcode':{'value':'200','valueSource':'Variable'},'desc':{'value':'','valueSource':'String'}},'valueSource':'Definition'}");
\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
128 httpUtil.handleParam(executionEntity, param, info);
\r
129 Assert.assertTrue(true);
\r
130 } catch (ActivitiException e) {
\r
131 Assert.assertTrue(true);
\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
141 PowerMockito.when(expression.getValue(executionEntity)).thenReturn(res);
\r
143 Assert.assertEquals(res, httpUtil.getValue(expression, executionEntity));
\r
146 @SuppressWarnings("static-access")
\r
148 public void testParsePlanExpression() {
\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
157 DelegateExecution executionEntity = mock(ExecutionEntity.class);
\r
159 PowerMockito.when(executionEntity.getVariable("node0")).thenReturn(msg);
\r
161 Assert.assertEquals("123", httpUtil.parsePlanExpression(exp, executionEntity));
\r