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.assertj.core.api.Assertions.assertThat;
24 import static org.mockito.ArgumentMatchers.argThat;
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.List;
38 import java.util.stream.Collectors;
39 import org.jeasy.random.EasyRandom;
40 import org.jeasy.random.EasyRandomParameters;
41 import org.jeasy.random.randomizers.misc.BooleanRandomizer;
42 import org.jeasy.random.randomizers.text.StringRandomizer;
43 import org.junit.Before;
44 import org.junit.Test;
45 import org.mockito.ArgumentCaptor;
46 import org.onap.vid.mso.MsoBusinessLogic;
47 import org.onap.vid.mso.MsoResponseWrapper;
48 import org.onap.vid.mso.rest.Request;
49 import org.onap.vid.mso.rest.RequestDetails;
50 import org.onap.vid.mso.rest.Task;
51 import org.onap.vid.services.CloudOwnerService;
52 import org.springframework.test.web.servlet.MockMvc;
53 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
55 public class MsoControllerTest {
57 private static final long STATIC_SEED = 5336L;
58 private EasyRandomParameters parameters = new EasyRandomParameters()
59 .randomize(Boolean.class, new BooleanRandomizer(STATIC_SEED))
60 .randomize(String.class, new StringRandomizer(4, 4, STATIC_SEED))
61 .collectionSizeRange(2, 3);
62 private EasyRandom modelGenerator = new EasyRandom(parameters);
63 private ObjectMapper objectMapper = new ObjectMapper();
65 private MockMvc mockMvc;
66 private MsoBusinessLogic msoBusinessLogic;
67 private CloudOwnerService cloudService;
71 msoBusinessLogic = mock(MsoBusinessLogic.class);
72 cloudService = mock(CloudOwnerService.class);
73 MsoController msoController = new MsoController(msoBusinessLogic, cloudService);
75 mockMvc = MockMvcBuilders.standaloneSetup(msoController).build();
79 public void shouldDelegateNewInstanceCreation() throws Exception {
81 RequestDetails given = modelGenerator.nextObject(RequestDetails.class);
82 String payload = objectMapper.writeValueAsString(given);
84 MsoResponseWrapper expectedResponse = new MsoResponseWrapper(200, "test");
85 given(msoBusinessLogic
86 .createSvcInstance(argThat(request -> asJson(request).equals(payload))))
87 .willReturn(expectedResponse);
90 mockMvc.perform(post("/mso/mso_create_svc_instance")
92 .contentType(APPLICATION_JSON))
93 .andExpect(status().isOk())
94 .andExpect(content().json(asJson(expectedResponse)));
96 ArgumentCaptor<RequestDetails> captor = ArgumentCaptor.forClass(RequestDetails.class);
97 then(cloudService).should(only()).enrichRequestWithCloudOwner(captor.capture());
98 assertThat(captor.getValue()).matches(request -> asJson(request).equals(payload));
102 public void shouldReturnOrchestrationRequestsForDashboard() throws Exception {
104 List<Request> orchestrationRequests = modelGenerator
105 .objects(Request.class, 2)
106 .collect(Collectors.toList());
108 given(msoBusinessLogic.getOrchestrationRequestsForDashboard()).willReturn(orchestrationRequests);
111 mockMvc.perform(get("/mso/mso_get_orch_reqs/dashboard"))
112 .andExpect(status().isOk())
113 .andExpect(content().json(objectMapper.writeValueAsString(orchestrationRequests)));
115 then(cloudService).shouldHaveZeroInteractions();
119 public void shouldReturnManualTasksById() throws Exception {
121 List<Task> manualTasks = modelGenerator
122 .objects(Task.class, 2)
123 .collect(Collectors.toList());
125 String originalRequestId = "za1234d1-5a33-55df-13ab-12abad84e335";
126 given(msoBusinessLogic.getManualTasksByRequestId(originalRequestId)).willReturn(manualTasks);
129 mockMvc.perform(get("/mso/mso_get_man_task/{id}", originalRequestId))
130 .andExpect(status().isOk())
131 .andExpect(content().json(objectMapper.writeValueAsString(manualTasks)));
133 then(cloudService).shouldHaveZeroInteractions();
136 private <T> String asJson(T value) {
138 return objectMapper.writeValueAsString(value);
139 } catch (JsonProcessingException e) {
140 throw new RuntimeException(e);