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.externalservice.service.activitiservice;
18 import com.google.gson.Gson;
19 import org.apache.http.HttpEntity;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.onap.workflow.WorkflowAppConfig;
24 import org.onap.workflow.common.Config;
25 import org.onap.workflow.common.RestClient;
26 import org.onap.workflow.common.RestResponse;
27 import org.onap.workflow.entity.MsbClientConfig;
28 import org.onap.workflow.externalservice.entity.activitientitiy.ActivitiDeployResponse;
29 import org.onap.workflow.externalservice.entity.activitientitiy.ActivitiStartProcessRequest;
30 import org.powermock.core.classloader.annotations.PrepareForTest;
31 import org.powermock.modules.junit4.PowerMockRunner;
33 import java.io.IOException;
34 import java.io.InputStream;
36 import static org.hamcrest.core.Is.is;
37 import static org.junit.Assert.assertThat;
38 import static org.mockito.Matchers.*;
39 import static org.mockito.Mockito.mock;
40 import static org.powermock.api.mockito.PowerMockito.mockStatic;
41 import static org.powermock.api.mockito.PowerMockito.when;
43 @PrepareForTest({RestClient.class, Config.class})
44 @RunWith(PowerMockRunner.class)
45 public class ActivitiServiceConsumerTest {
48 public void setUp() throws Exception {
49 mockStatic(RestClient.class);
53 public void undeploybpmnfile() throws Exception {
55 RestResponse restResponse = mock(RestResponse.class);
56 when(RestClient.post(anyString(), any(Integer.class), anyString()))
57 .thenReturn(restResponse);
59 RestResponse response = ActivitiServiceConsumer.undeploybpmnfile("22");
61 assertThat(response, is(restResponse));
65 public void startBpmnProcess() throws Exception {
66 RestResponse restResponse = mock(RestResponse.class);
67 when(RestClient.post(anyString(), any(Integer.class), anyString(),
68 any(ActivitiStartProcessRequest.class)))
69 .thenReturn(restResponse);
71 RestResponse response = ActivitiServiceConsumer.startBpmnProcess(
72 mock(ActivitiStartProcessRequest.class));
74 assertThat(response, is(restResponse));
78 public void testDeleteDeployProcess() throws Exception {
79 RestResponse restResponse = mock(RestResponse.class);
80 when(RestClient.post(anyString(), any(Integer.class), anyString()))
81 .thenReturn(restResponse);
83 RestResponse response = ActivitiServiceConsumer.deleteDeployProcess("22");
85 assertThat(response, is(restResponse));
89 public void testDeleteDeployProcessReturnNull() throws Exception {
90 when(RestClient.post(anyString(), any(Integer.class), anyString()))
91 .thenThrow(new IOException());
93 RestResponse response = ActivitiServiceConsumer.deleteDeployProcess("22");
95 assertThat(response, is((RestResponse) null));
99 public void testStartProcessShouldReturnResponse() throws Exception {
100 RestResponse restResponse = mock(RestResponse.class);
101 when(RestClient.post(anyString(), any(Integer.class), anyString(),
102 any(ActivitiStartProcessRequest.class)))
103 .thenReturn(restResponse);
105 RestResponse response = ActivitiServiceConsumer.startProcess(
106 mock(ActivitiStartProcessRequest.class));
108 assertThat(response, is(restResponse));
112 public void testStartProcessShouldReturnNull() throws Exception {
113 when(RestClient.post(anyString(), any(Integer.class), anyString(),
114 any(ActivitiStartProcessRequest.class)))
115 .thenThrow(new IOException());
117 RestResponse response = ActivitiServiceConsumer.startProcess(
118 mock(ActivitiStartProcessRequest.class));
120 assertThat(response, is((RestResponse) null));
124 public void deploybpmnfile() throws Exception {
125 mockStatic(Config.class);
126 WorkflowAppConfig workflowAppConfig = mock(WorkflowAppConfig.class);
127 MsbClientConfig msbClientConfig = new MsbClientConfig();
128 msbClientConfig.setMsbSvrPort(2);
129 msbClientConfig.setMsbSvrIp("127.0.0.1");
131 when(workflowAppConfig.getMsbClientConfig()).thenReturn(msbClientConfig);
132 when(Config.getWorkflowAppConfig()).thenReturn(workflowAppConfig);
134 RestResponse restResponse = mock(RestResponse.class);
136 ActivitiDeployResponse activitiDeployResponse = new ActivitiDeployResponse();
137 activitiDeployResponse.setId("2");
138 activitiDeployResponse.setUrl("xxxx");
139 activitiDeployResponse.setDeploymentTime("22");
141 when(restResponse.getStatusCode()).thenReturn(200);
142 when(restResponse.getResult()).thenReturn(new Gson().toJson(activitiDeployResponse));
143 when(RestClient.post(anyString(), anyInt(), anyString(),
144 any(HttpEntity.class)))
145 .thenReturn(restResponse);
147 InputStream ins = mock(InputStream.class);
148 ActivitiDeployResponse result = ActivitiServiceConsumer.deploybpmnfile(ins, "result");
150 assertThat(result.getId(), is(activitiDeployResponse.getId()));