f9dd990a26513f7c6ec0e8671e0590d0006cbd0e
[vfc/nfvo/wfengine.git] / wfenginemgrservice / src / test / java / org / onap / workflow / externalservice / service / activitiservice / ActivitiServiceConsumerTest.java
1 /**
2  * Copyright 2017 ZTE Corporation.
3  * <p>
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  * <p>
8  * http://www.apache.org/licenses/LICENSE-2.0
9  * <p>
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.externalservice.service.activitiservice;
17
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;
32
33 import java.io.IOException;
34 import java.io.InputStream;
35
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;
42
43 @PrepareForTest({RestClient.class, Config.class})
44 @RunWith(PowerMockRunner.class)
45 public class ActivitiServiceConsumerTest {
46
47   @Before
48   public void setUp() throws Exception {
49     mockStatic(RestClient.class);
50   }
51
52   @Test
53   public void undeploybpmnfile() throws Exception {
54
55     RestResponse restResponse = mock(RestResponse.class);
56     when(RestClient.post(anyString(), any(Integer.class), anyString()))
57         .thenReturn(restResponse);
58
59     RestResponse response = ActivitiServiceConsumer.undeploybpmnfile("22");
60
61     assertThat(response, is(restResponse));
62   }
63
64   @Test
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);
70
71     RestResponse response = ActivitiServiceConsumer.startBpmnProcess(
72         mock(ActivitiStartProcessRequest.class));
73
74     assertThat(response, is(restResponse));
75   }
76
77   @Test
78   public void testDeleteDeployProcess() throws Exception {
79     RestResponse restResponse = mock(RestResponse.class);
80     when(RestClient.post(anyString(), any(Integer.class), anyString()))
81         .thenReturn(restResponse);
82
83     RestResponse response = ActivitiServiceConsumer.deleteDeployProcess("22");
84
85     assertThat(response, is(restResponse));
86   }
87
88   @Test
89   public void testDeleteDeployProcessReturnNull() throws Exception {
90     when(RestClient.post(anyString(), any(Integer.class), anyString()))
91         .thenThrow(new IOException());
92
93     RestResponse response = ActivitiServiceConsumer.deleteDeployProcess("22");
94
95     assertThat(response, is((RestResponse) null));
96   }
97
98   @Test
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);
104
105     RestResponse response = ActivitiServiceConsumer.startProcess(
106         mock(ActivitiStartProcessRequest.class));
107
108     assertThat(response, is(restResponse));
109   }
110
111   @Test
112   public void testStartProcessShouldReturnNull() throws Exception {
113     when(RestClient.post(anyString(), any(Integer.class), anyString(),
114         any(ActivitiStartProcessRequest.class)))
115         .thenThrow(new IOException());
116
117     RestResponse response = ActivitiServiceConsumer.startProcess(
118         mock(ActivitiStartProcessRequest.class));
119
120     assertThat(response, is((RestResponse) null));
121   }
122
123   @Test
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");
130
131     when(workflowAppConfig.getMsbClientConfig()).thenReturn(msbClientConfig);
132     when(Config.getWorkflowAppConfig()).thenReturn(workflowAppConfig);
133
134     RestResponse restResponse = mock(RestResponse.class);
135
136     ActivitiDeployResponse activitiDeployResponse = new ActivitiDeployResponse();
137     activitiDeployResponse.setId("2");
138     activitiDeployResponse.setUrl("xxxx");
139     activitiDeployResponse.setDeploymentTime("22");
140
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);
146
147     InputStream ins = mock(InputStream.class);
148     ActivitiDeployResponse result = ActivitiServiceConsumer.deploybpmnfile(ins, "result");
149
150     assertThat(result.getId(), is(activitiDeployResponse.getId()));
151   }
152
153 }