2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 Nokia Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.onap.vid.controller;
22 import com.fasterxml.jackson.databind.ObjectMapper;
23 import io.joshworks.restclient.http.HttpResponse;
24 import io.joshworks.restclient.http.JsonMapper;
25 import org.apache.http.ProtocolVersion;
26 import org.apache.http.StatusLine;
27 import org.apache.http.message.BasicHttpResponse;
28 import org.apache.http.message.BasicStatusLine;
29 import org.mockito.Mock;
30 import org.onap.vid.changeManagement.UIWorkflowsRequest;
31 import org.onap.vid.changeManagement.WorkflowRequestDetail;
32 import org.onap.vid.mso.MsoUtil;
33 import org.onap.vid.mso.model.CloudConfiguration;
34 import org.onap.vid.services.ChangeManagementService;
35 import org.onap.vid.services.ExternalWorkflowsService;
36 import org.onap.vid.services.LocalWorkflowsService;
37 import org.springframework.test.web.servlet.MockMvc;
38 import org.springframework.test.web.servlet.ResultActions;
39 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
40 import org.testng.annotations.BeforeClass;
41 import org.testng.annotations.Test;
43 import javax.servlet.http.HttpServletRequest;
44 import javax.ws.rs.core.MediaType;
45 import java.util.ArrayList;
46 import java.util.HashMap;
47 import java.util.List;
49 import java.util.UUID;
51 import static org.mockito.ArgumentMatchers.any;
52 import static org.mockito.ArgumentMatchers.eq;
53 import static org.mockito.BDDMockito.given;
54 import static org.mockito.MockitoAnnotations.initMocks;
55 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
56 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
58 public class WorkflowsControllerTest {
60 private static final String VID_WORKFLOWS = "/workflows-management/{serviceInstanceId}/{vnfInstanceId}/{workflow_UUID}";
62 private MockMvc mockMvc;
64 private WorkflowsController workflowsController;
67 private ExternalWorkflowsService externalWorkflowsService;
70 private LocalWorkflowsService localWorkflowsService;
73 private ChangeManagementService changeManagementService;
78 workflowsController = new WorkflowsController(externalWorkflowsService,localWorkflowsService,changeManagementService);
79 mockMvc = MockMvcBuilders.standaloneSetup(workflowsController).build();
83 public void shouldProperlyReceivePostRequestFromUI() throws Exception {
85 HttpResponse<String> expectedResponse = createOkResponse();
86 ObjectMapper objectMapper = new ObjectMapper();
88 UIWorkflowsRequest uiWorkflowsRequest = new UIWorkflowsRequest();
90 WorkflowRequestDetail workflowRequestDetail = createWorkflowRequestDetail();
92 uiWorkflowsRequest.setRequestDetails(workflowRequestDetail);
94 UUID serviceInstanceId = new UUID(1,10);
95 UUID vnfInstanceId = new UUID(2,20);
96 UUID workflow_UUID = new UUID(3,30);
98 given(changeManagementService.invokeVnfWorkflow(
99 any(HttpServletRequest.class), eq(uiWorkflowsRequest.getRequestDetails()),eq(serviceInstanceId),eq(vnfInstanceId), eq(workflow_UUID)
100 )).willReturn(MsoUtil.wrapResponse(expectedResponse));
103 ResultActions response = mockMvc.perform( post(VID_WORKFLOWS,serviceInstanceId,vnfInstanceId,workflow_UUID)
104 .contentType(MediaType.APPLICATION_JSON)
105 .content(objectMapper.writeValueAsString(uiWorkflowsRequest)));
108 response.andExpect(status().isOk());
111 private WorkflowRequestDetail createWorkflowRequestDetail() {
112 WorkflowRequestDetail workflowRequestDetail = new WorkflowRequestDetail();
113 org.onap.vid.changeManagement.RequestParameters requestParameters = new org.onap.vid.changeManagement.RequestParameters();
114 HashMap<String,String> paramsMap = new HashMap<>();
115 paramsMap.put("testKey1","testValue1");
116 paramsMap.put("testKey2","testValue2");
118 List<Map<String,String>> mapArray= new ArrayList<>();
119 mapArray.add(paramsMap);
120 requestParameters.setUserParams(mapArray);
122 CloudConfiguration cloudConfiguration = new CloudConfiguration();
123 cloudConfiguration.setCloudOwner("testOwne");
124 cloudConfiguration.setTenantId("testId");
125 cloudConfiguration.setLcpCloudRegionId("testLcpCloudId");
127 workflowRequestDetail.setRequestParameters(requestParameters);
128 workflowRequestDetail.setCloudConfiguration(cloudConfiguration);
129 return workflowRequestDetail;
132 private HttpResponse<String> createOkResponse() {
133 StatusLine statusline = new BasicStatusLine(
134 new ProtocolVersion("http",1,1), 200, "acceptResponse");
136 org.apache.http.HttpResponse responseBase = new BasicHttpResponse(statusline);
138 return new HttpResponse<>(responseBase ,String.class, new JsonMapper());