2 * Copyright 2017 ZTE Corporation.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onap.workflow.activitiext.restservicetask;
18 import static org.powermock.api.mockito.PowerMockito.mock;
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;
31 import com.alibaba.fastjson.JSON;
33 public class HttpUtilTest {
35 private HttpUtil httpUtil;
39 httpUtil = new HttpUtil();
43 public void testExecute() {
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);
57 public void testExecuteMethod() {
59 DelegateExecution executionEntity = mock(ExecutionEntity.class);
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);
73 public void testHandleParam1() {
76 DelegateExecution executionEntity = mock(ExecutionEntity.class);
77 Parameter param = new Parameter();
79 param.setPosition("path");
81 param.setValue("abc");
83 RestInfo info = new RestInfo();
84 info.setMethod("GET");
86 info.setPath("/catalog/v1");
89 info.setVersion("v1");
91 httpUtil.handleParam(executionEntity, param, info);
92 Assert.assertTrue(true);
93 } catch (ActivitiException e) {
94 Assert.assertTrue(true);
100 public void testHandleParam2() {
102 DelegateExecution executionEntity = mock(ExecutionEntity.class);
103 Parameter param = new Parameter();
105 param.setPosition("query");
107 param.setValue("abc");
109 RestInfo info = new RestInfo();
110 info.setMethod("GET");
111 info.setName("name");
112 info.setPath("/catalog/v1");
114 info.setUrl("csars");
115 info.setVersion("v1");
117 httpUtil.handleParam(executionEntity, param, info);
118 Assert.assertTrue(true);
119 } catch (ActivitiException e) {
120 Assert.assertTrue(true);
126 public void testGetValue() {
127 DelegateExecution executionEntity = mock(ExecutionEntity.class);
128 Expression expression = mock(Expression.class);
129 Object res = new String("result");
131 PowerMockito.when(expression.getValue(executionEntity)).thenReturn(res);
133 Assert.assertEquals(res, httpUtil.getValue(expression, executionEntity));
136 @SuppressWarnings("static-access")
138 public void testParsePlanExpression() {
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);
147 DelegateExecution executionEntity = mock(ExecutionEntity.class);
149 PowerMockito.when(executionEntity.getVariable("node0")).thenReturn(msg);
151 Assert.assertEquals("123", httpUtil.parsePlanExpression(exp, executionEntity));