2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2019 AT&T 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=========================================================
21 package org.onap.vid.controller;
23 import static org.mockito.ArgumentMatchers.argThat;
24 import static org.mockito.ArgumentMatchers.eq;
25 import static org.mockito.BDDMockito.given;
26 import static org.mockito.BDDMockito.then;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.only;
29 import static org.springframework.http.MediaType.APPLICATION_JSON;
30 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
31 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
32 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
33 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
35 import com.fasterxml.jackson.core.JsonProcessingException;
36 import com.fasterxml.jackson.databind.ObjectMapper;
37 import java.util.LinkedHashMap;
38 import java.util.List;
40 import java.util.stream.Collectors;
41 import org.jeasy.random.EasyRandom;
42 import org.jeasy.random.EasyRandomParameters;
43 import org.jeasy.random.randomizers.misc.BooleanRandomizer;
44 import org.jeasy.random.randomizers.text.StringRandomizer;
45 import org.junit.Before;
46 import org.junit.Test;
47 import org.onap.vid.mso.MsoBusinessLogic;
48 import org.onap.vid.mso.MsoResponseWrapper;
49 import org.onap.vid.mso.rest.Request;
50 import org.onap.vid.mso.rest.RequestDetails;
51 import org.onap.vid.mso.rest.Task;
52 import org.onap.vid.services.CloudOwnerService;
53 import org.springframework.test.web.servlet.MockMvc;
54 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
56 public class MsoControllerTest {
58 private static final long STATIC_SEED = 5336L;
59 private EasyRandomParameters parameters = new EasyRandomParameters()
60 .randomize(Boolean.class, new BooleanRandomizer(STATIC_SEED))
61 .randomize(String.class, new StringRandomizer(4, 4, STATIC_SEED))
62 .collectionSizeRange(2, 3);
63 private EasyRandom modelGenerator = new EasyRandom(parameters);
64 private ObjectMapper objectMapper = new ObjectMapper();
66 private MockMvc mockMvc;
67 private MsoBusinessLogic msoBusinessLogic;
68 private CloudOwnerService cloudService;
72 msoBusinessLogic = mock(MsoBusinessLogic.class);
73 cloudService = mock(CloudOwnerService.class);
74 MsoController msoController = new MsoController(msoBusinessLogic, cloudService);
76 mockMvc = MockMvcBuilders.standaloneSetup(msoController).build();
80 public void shouldDelegateNewServiceInstantiation() throws Exception {
82 RequestDetails requestDetails = modelGenerator.nextObject(RequestDetails.class);
84 MsoResponseWrapper expectedResponse = new MsoResponseWrapper(200, "test");
85 given(msoBusinessLogic
86 .createSvcInstance(objectEqualTo(requestDetails)))
87 .willReturn(expectedResponse);
90 mockMvc.perform(post("/mso/mso_create_svc_instance")
91 .content(asJson(requestDetails))
92 .contentType(APPLICATION_JSON))
93 .andExpect(status().isOk())
94 .andExpect(content().json(asJson(expectedResponse)));
96 then(cloudService).should(only()).enrichRequestWithCloudOwner(objectEqualTo(requestDetails));
100 public void shouldReturnOrchestrationRequestsForDashboard() throws Exception {
102 List<Request> orchestrationRequests = modelGenerator
103 .objects(Request.class, 2)
104 .collect(Collectors.toList());
106 given(msoBusinessLogic.getOrchestrationRequestsForDashboard()).willReturn(orchestrationRequests);
109 mockMvc.perform(get("/mso/mso_get_orch_reqs/dashboard"))
110 .andExpect(status().isOk())
111 .andExpect(content().json(objectMapper.writeValueAsString(orchestrationRequests)));
113 then(cloudService).shouldHaveZeroInteractions();
117 public void shouldReturnManualTasksById() throws Exception {
119 List<Task> manualTasks = modelGenerator
120 .objects(Task.class, 2)
121 .collect(Collectors.toList());
123 String originalRequestId = "za1234d1-5a33-55df-13ab-12abad84e335";
124 given(msoBusinessLogic.getManualTasksByRequestId(originalRequestId)).willReturn(manualTasks);
127 mockMvc.perform(get("/mso/mso_get_man_task/{id}", originalRequestId))
128 .andExpect(status().isOk())
129 .andExpect(content().json(objectMapper.writeValueAsString(manualTasks)));
131 then(cloudService).shouldHaveZeroInteractions();
135 public void shouldDelegateE2EServiceInstantiation() throws Exception {
137 String requestDetails = "some request details";
138 Map<String, Object> payload = new LinkedHashMap<>();
139 payload.put("requestDetails", requestDetails);
141 MsoResponseWrapper wrapper = mock(MsoResponseWrapper.class);
142 given(wrapper.getResponse()).willReturn("some response");
143 given(msoBusinessLogic.createE2eSvcInstance(requestDetails)).willReturn(wrapper);
146 mockMvc.perform(post("/mso/mso_create_e2e_svc_instance")
147 .content(asJson(payload))
148 .contentType(APPLICATION_JSON))
149 .andExpect(status().isOk())
150 .andExpect(content().string("some response"));
152 then(cloudService).shouldHaveZeroInteractions();
156 public void shouldDelegateServiceInstantiation() throws Exception {
158 RequestDetails requestDetails = modelGenerator.nextObject(RequestDetails.class);
160 MsoResponseWrapper wrapper = mock(MsoResponseWrapper.class);
161 given(wrapper.getResponse()).willReturn("some response");
162 given(msoBusinessLogic.createSvcInstance(objectEqualTo(requestDetails))).willReturn(wrapper);
165 mockMvc.perform(post("/mso/mso_create_svc_instance")
166 .content(asJson(requestDetails))
167 .contentType(APPLICATION_JSON))
168 .andExpect(status().isOk())
169 .andExpect(content().string("some response"));
171 then(cloudService).should(only()).enrichRequestWithCloudOwner(objectEqualTo(requestDetails));
175 public void shouldDelegateVnfInstantiation() throws Exception {
177 RequestDetails requestDetails = modelGenerator.nextObject(RequestDetails.class);
178 String serviceInstanceId = "bc305d54-75b4-431b-adb2-eb6b9e546014";
180 MsoResponseWrapper wrapper = mock(MsoResponseWrapper.class);
181 given(wrapper.getResponse()).willReturn("some response");
182 given(msoBusinessLogic.createVnf(objectEqualTo(requestDetails), eq(serviceInstanceId))).willReturn(wrapper);
185 mockMvc.perform(post("/mso/mso_create_vnf_instance/" + serviceInstanceId)
186 .content(asJson(requestDetails))
187 .contentType(APPLICATION_JSON))
188 .andExpect(status().isOk())
189 .andExpect(content().string("some response"));
191 then(cloudService).should(only()).enrichRequestWithCloudOwner(objectEqualTo(requestDetails));
195 public void shouldDelegateNewInstanceCreation() throws Exception {
197 RequestDetails requestDetails = modelGenerator.nextObject(RequestDetails.class);
198 String serviceInstanceId = "bc305d54-75b4-431b-adb2-eb6b9e546014";
200 MsoResponseWrapper wrapper = mock(MsoResponseWrapper.class);
201 given(wrapper.getResponse()).willReturn("some response");
202 given(msoBusinessLogic.createNwInstance(objectEqualTo(requestDetails), eq(serviceInstanceId))).willReturn(wrapper);
205 mockMvc.perform(post("/mso/mso_create_nw_instance/" + serviceInstanceId)
206 .content(asJson(requestDetails))
207 .contentType(APPLICATION_JSON))
208 .andExpect(status().isOk())
209 .andExpect(content().string("some response"));
211 then(cloudService).should(only()).enrichRequestWithCloudOwner(objectEqualTo(requestDetails));
215 public void shouldCompleteManualTask() throws Exception {
217 RequestDetails requestDetails = modelGenerator.nextObject(RequestDetails.class);
218 String taskId = "bc305d54-75b4-431b-adb2-eb6b9e546014";
220 MsoResponseWrapper wrapper = mock(MsoResponseWrapper.class);
221 given(wrapper.getResponse()).willReturn("some response");
222 given(msoBusinessLogic.completeManualTask(objectEqualTo(requestDetails), eq(taskId))).willReturn(wrapper);
225 mockMvc.perform(post("/mso/mso_post_man_task/" + taskId)
226 .content(asJson(requestDetails))
227 .contentType(APPLICATION_JSON))
228 .andExpect(status().isOk())
229 .andExpect(content().string("some response"));
231 then(cloudService).shouldHaveZeroInteractions();
234 private <T> String asJson(T value) {
236 return objectMapper.writeValueAsString(value);
237 } catch (JsonProcessingException e) {
238 throw new RuntimeException(e);
242 private <T> T objectEqualTo(T expected) {
243 return argThat(given -> asJson(given).equals(asJson(expected)));