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.MsoRestClientNew;
50 import org.onap.vid.mso.rest.Request;
51 import org.onap.vid.mso.rest.RequestDetails;
52 import org.onap.vid.mso.rest.Task;
53 import org.onap.vid.services.CloudOwnerService;
54 import org.springframework.test.web.servlet.MockMvc;
55 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
57 public class MsoControllerTest {
59 private static final long STATIC_SEED = 5336L;
60 private EasyRandomParameters parameters = new EasyRandomParameters()
61 .randomize(Boolean.class, new BooleanRandomizer(STATIC_SEED))
62 .randomize(String.class, new StringRandomizer(4, 4, STATIC_SEED))
63 .collectionSizeRange(2, 3);
64 private EasyRandom modelGenerator = new EasyRandom(parameters);
65 private ObjectMapper objectMapper = new ObjectMapper();
67 private MockMvc mockMvc;
68 private MsoBusinessLogic msoBusinessLogic;
69 private CloudOwnerService cloudService;
70 private MsoRestClientNew msoRestClient;
74 msoBusinessLogic = mock(MsoBusinessLogic.class);
75 cloudService = mock(CloudOwnerService.class);
76 msoRestClient = mock(MsoRestClientNew.class);
77 MsoController msoController = new MsoController(msoBusinessLogic, msoRestClient, cloudService);
79 mockMvc = MockMvcBuilders.standaloneSetup(msoController).build();
83 public void shouldDelegateNewServiceInstantiation() throws Exception {
85 RequestDetails requestDetails = modelGenerator.nextObject(RequestDetails.class);
87 MsoResponseWrapper expectedResponse = new MsoResponseWrapper(200, "test");
88 given(msoBusinessLogic
89 .createSvcInstance(objectEqualTo(requestDetails)))
90 .willReturn(expectedResponse);
93 mockMvc.perform(post("/mso/mso_create_svc_instance")
94 .content(asJson(requestDetails))
95 .contentType(APPLICATION_JSON))
96 .andExpect(status().isOk())
97 .andExpect(content().json(asJson(expectedResponse)));
99 then(cloudService).should(only()).enrichRequestWithCloudOwner(objectEqualTo(requestDetails));
103 public void shouldReturnOrchestrationRequestsForDashboard() throws Exception {
105 List<Request> orchestrationRequests = modelGenerator
106 .objects(Request.class, 2)
107 .collect(Collectors.toList());
109 given(msoBusinessLogic.getOrchestrationRequestsForDashboard()).willReturn(orchestrationRequests);
112 mockMvc.perform(get("/mso/mso_get_orch_reqs/dashboard"))
113 .andExpect(status().isOk())
114 .andExpect(content().json(objectMapper.writeValueAsString(orchestrationRequests)));
116 then(cloudService).shouldHaveZeroInteractions();
120 public void shouldReturnManualTasksById() throws Exception {
122 List<Task> manualTasks = modelGenerator
123 .objects(Task.class, 2)
124 .collect(Collectors.toList());
126 String originalRequestId = "za1234d1-5a33-55df-13ab-12abad84e335";
127 given(msoBusinessLogic.getManualTasksByRequestId(originalRequestId)).willReturn(manualTasks);
130 mockMvc.perform(get("/mso/mso_get_man_task/{id}", originalRequestId))
131 .andExpect(status().isOk())
132 .andExpect(content().json(objectMapper.writeValueAsString(manualTasks)));
134 then(cloudService).shouldHaveZeroInteractions();
138 public void shouldDelegateE2EServiceInstantiation() throws Exception {
140 String requestDetails = "some request details";
141 Map<String, Object> payload = new LinkedHashMap<>();
142 payload.put("requestDetails", requestDetails);
144 MsoResponseWrapper wrapper = mock(MsoResponseWrapper.class);
145 given(wrapper.getResponse()).willReturn("some response");
146 given(msoBusinessLogic.createE2eSvcInstance(requestDetails)).willReturn(wrapper);
149 mockMvc.perform(post("/mso/mso_create_e2e_svc_instance")
150 .content(asJson(payload))
151 .contentType(APPLICATION_JSON))
152 .andExpect(status().isOk())
153 .andExpect(content().string("some response"));
155 then(cloudService).shouldHaveZeroInteractions();
159 public void shouldDelegateServiceInstantiation() throws Exception {
161 RequestDetails requestDetails = modelGenerator.nextObject(RequestDetails.class);
163 MsoResponseWrapper wrapper = mock(MsoResponseWrapper.class);
164 given(wrapper.getResponse()).willReturn("some response");
165 given(msoBusinessLogic.createSvcInstance(objectEqualTo(requestDetails))).willReturn(wrapper);
168 mockMvc.perform(post("/mso/mso_create_svc_instance")
169 .content(asJson(requestDetails))
170 .contentType(APPLICATION_JSON))
171 .andExpect(status().isOk())
172 .andExpect(content().string("some response"));
174 then(cloudService).should(only()).enrichRequestWithCloudOwner(objectEqualTo(requestDetails));
178 public void shouldDelegateVnfInstantiation() throws Exception {
180 RequestDetails requestDetails = modelGenerator.nextObject(RequestDetails.class);
181 String serviceInstanceId = "bc305d54-75b4-431b-adb2-eb6b9e546014";
183 MsoResponseWrapper wrapper = mock(MsoResponseWrapper.class);
184 given(wrapper.getResponse()).willReturn("some response");
185 given(msoBusinessLogic.createVnf(objectEqualTo(requestDetails), eq(serviceInstanceId))).willReturn(wrapper);
188 mockMvc.perform(post("/mso/mso_create_vnf_instance/" + serviceInstanceId)
189 .content(asJson(requestDetails))
190 .contentType(APPLICATION_JSON))
191 .andExpect(status().isOk())
192 .andExpect(content().string("some response"));
194 then(cloudService).should(only()).enrichRequestWithCloudOwner(objectEqualTo(requestDetails));
198 public void shouldDelegateNewInstanceCreation() throws Exception {
200 RequestDetails requestDetails = modelGenerator.nextObject(RequestDetails.class);
201 String serviceInstanceId = "bc305d54-75b4-431b-adb2-eb6b9e546014";
203 MsoResponseWrapper wrapper = mock(MsoResponseWrapper.class);
204 given(wrapper.getResponse()).willReturn("some response");
205 given(msoBusinessLogic.createNwInstance(objectEqualTo(requestDetails), eq(serviceInstanceId))).willReturn(wrapper);
208 mockMvc.perform(post("/mso/mso_create_nw_instance/" + serviceInstanceId)
209 .content(asJson(requestDetails))
210 .contentType(APPLICATION_JSON))
211 .andExpect(status().isOk())
212 .andExpect(content().string("some response"));
214 then(cloudService).should(only()).enrichRequestWithCloudOwner(objectEqualTo(requestDetails));
218 public void shouldCompleteManualTask() throws Exception {
220 RequestDetails requestDetails = modelGenerator.nextObject(RequestDetails.class);
221 String taskId = "bc305d54-75b4-431b-adb2-eb6b9e546014";
223 MsoResponseWrapper wrapper = mock(MsoResponseWrapper.class);
224 given(wrapper.getResponse()).willReturn("some response");
225 given(msoBusinessLogic.completeManualTask(objectEqualTo(requestDetails), eq(taskId))).willReturn(wrapper);
228 mockMvc.perform(post("/mso/mso_post_man_task/" + taskId)
229 .content(asJson(requestDetails))
230 .contentType(APPLICATION_JSON))
231 .andExpect(status().isOk())
232 .andExpect(content().string("some response"));
234 then(cloudService).shouldHaveZeroInteractions();
237 private <T> String asJson(T value) {
239 return objectMapper.writeValueAsString(value);
240 } catch (JsonProcessingException e) {
241 throw new RuntimeException(e);
245 private <T> T objectEqualTo(T expected) {
246 return argThat(given -> asJson(given).equals(asJson(expected)));