Sending workflow data from UI to SO
[vid.git] / vid-app-common / src / test / java / org / onap / vid / controller / WorkflowsControllerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.onap.vid.controller;
21
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;
42
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;
48 import java.util.Map;
49 import java.util.UUID;
50
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;
57
58 public class WorkflowsControllerTest {
59
60     private static final String VID_WORKFLOWS = "/workflows-management/{serviceInstanceId}/{vnfInstanceId}/{workflow_UUID}";
61
62     private MockMvc mockMvc;
63
64     private WorkflowsController workflowsController;
65
66     @Mock
67     private ExternalWorkflowsService externalWorkflowsService;
68
69     @Mock
70     private LocalWorkflowsService localWorkflowsService;
71
72     @Mock
73     private ChangeManagementService changeManagementService;
74
75     @BeforeClass
76     public  void setUp(){
77         initMocks(this);
78         workflowsController = new WorkflowsController(externalWorkflowsService,localWorkflowsService,changeManagementService);
79         mockMvc = MockMvcBuilders.standaloneSetup(workflowsController).build();
80     }
81
82     @Test
83     public void shouldProperlyReceivePostRequestFromUI() throws Exception {
84         //  given
85         HttpResponse<String> expectedResponse = createOkResponse();
86         ObjectMapper objectMapper = new ObjectMapper();
87
88         UIWorkflowsRequest uiWorkflowsRequest = new UIWorkflowsRequest();
89
90         WorkflowRequestDetail workflowRequestDetail = createWorkflowRequestDetail();
91
92         uiWorkflowsRequest.setRequestDetails(workflowRequestDetail);
93
94         UUID serviceInstanceId = new UUID(1,10);
95         UUID vnfInstanceId = new UUID(2,20);
96         UUID workflow_UUID = new UUID(3,30);
97
98         given(changeManagementService.invokeVnfWorkflow(
99                 any(HttpServletRequest.class), eq(uiWorkflowsRequest.getRequestDetails()),eq(serviceInstanceId),eq(vnfInstanceId), eq(workflow_UUID)
100                 )).willReturn(MsoUtil.wrapResponse(expectedResponse));
101
102         //  when
103         ResultActions response = mockMvc.perform( post(VID_WORKFLOWS,serviceInstanceId,vnfInstanceId,workflow_UUID)
104                 .contentType(MediaType.APPLICATION_JSON)
105                 .content(objectMapper.writeValueAsString(uiWorkflowsRequest)));
106
107         //then
108         response.andExpect(status().isOk());
109     }
110
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");
117
118         List<Map<String,String>> mapArray= new ArrayList<>();
119         mapArray.add(paramsMap);
120         requestParameters.setUserParams(mapArray);
121
122         CloudConfiguration cloudConfiguration = new CloudConfiguration();
123         cloudConfiguration.setCloudOwner("testOwne");
124         cloudConfiguration.setTenantId("testId");
125         cloudConfiguration.setLcpCloudRegionId("testLcpCloudId");
126
127         workflowRequestDetail.setRequestParameters(requestParameters);
128         workflowRequestDetail.setCloudConfiguration(cloudConfiguration);
129         return workflowRequestDetail;
130     }
131
132     private HttpResponse<String> createOkResponse() {
133         StatusLine statusline = new BasicStatusLine(
134                 new ProtocolVersion("http",1,1), 200, "acceptResponse");
135
136         org.apache.http.HttpResponse responseBase = new BasicHttpResponse(statusline);
137
138         return new HttpResponse<>(responseBase ,String.class, new JsonMapper());
139     }
140 }