Implant vid-app-common org.onap.vid.job (main and test)
[vid.git] / vid-app-common / src / test / java / org / onap / vid / controller / MsoControllerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
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
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
21 package org.onap.vid.controller;
22
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;
34
35 import com.fasterxml.jackson.core.JsonProcessingException;
36 import com.fasterxml.jackson.databind.ObjectMapper;
37 import java.util.LinkedHashMap;
38 import java.util.List;
39 import java.util.Map;
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;
56
57 public class MsoControllerTest {
58
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();
66
67     private MockMvc mockMvc;
68     private MsoBusinessLogic msoBusinessLogic;
69     private CloudOwnerService cloudService;
70     private MsoRestClientNew msoRestClient;
71
72     @Before
73     public void setUp() {
74         msoBusinessLogic = mock(MsoBusinessLogic.class);
75         cloudService = mock(CloudOwnerService.class);
76         msoRestClient = mock(MsoRestClientNew.class);
77         MsoController msoController = new MsoController(msoBusinessLogic, msoRestClient, cloudService);
78
79         mockMvc = MockMvcBuilders.standaloneSetup(msoController).build();
80     }
81
82     @Test
83     public void shouldDelegateNewServiceInstantiation() throws Exception {
84         // given
85         RequestDetails requestDetails = modelGenerator.nextObject(RequestDetails.class);
86
87         MsoResponseWrapper expectedResponse = new MsoResponseWrapper(200, "test");
88         given(msoBusinessLogic
89             .createSvcInstance(objectEqualTo(requestDetails)))
90             .willReturn(expectedResponse);
91
92         // when & then
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)));
98
99         then(cloudService).should(only()).enrichRequestWithCloudOwner(objectEqualTo(requestDetails));
100     }
101
102     @Test
103     public void shouldReturnOrchestrationRequestsForDashboard() throws Exception {
104         // given
105         List<Request> orchestrationRequests = modelGenerator
106             .objects(Request.class, 2)
107             .collect(Collectors.toList());
108
109         given(msoBusinessLogic.getOrchestrationRequestsForDashboard()).willReturn(orchestrationRequests);
110
111         // when & then
112         mockMvc.perform(get("/mso/mso_get_orch_reqs/dashboard"))
113             .andExpect(status().isOk())
114             .andExpect(content().json(objectMapper.writeValueAsString(orchestrationRequests)));
115
116         then(cloudService).shouldHaveZeroInteractions();
117     }
118
119     @Test
120     public void shouldReturnManualTasksById() throws Exception {
121         // given
122         List<Task> manualTasks = modelGenerator
123             .objects(Task.class, 2)
124             .collect(Collectors.toList());
125
126         String originalRequestId = "za1234d1-5a33-55df-13ab-12abad84e335";
127         given(msoBusinessLogic.getManualTasksByRequestId(originalRequestId)).willReturn(manualTasks);
128
129         // when & then
130         mockMvc.perform(get("/mso/mso_get_man_task/{id}", originalRequestId))
131             .andExpect(status().isOk())
132             .andExpect(content().json(objectMapper.writeValueAsString(manualTasks)));
133
134         then(cloudService).shouldHaveZeroInteractions();
135     }
136
137     @Test
138     public void shouldDelegateE2EServiceInstantiation() throws Exception {
139         // given
140         String requestDetails = "some request details";
141         Map<String, Object> payload = new LinkedHashMap<>();
142         payload.put("requestDetails", requestDetails);
143
144         MsoResponseWrapper wrapper = mock(MsoResponseWrapper.class);
145         given(wrapper.getResponse()).willReturn("some response");
146         given(msoBusinessLogic.createE2eSvcInstance(requestDetails)).willReturn(wrapper);
147
148         // when & then
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"));
154
155         then(cloudService).shouldHaveZeroInteractions();
156     }
157
158     @Test
159     public void shouldDelegateServiceInstantiation() throws Exception {
160         // given
161         RequestDetails requestDetails = modelGenerator.nextObject(RequestDetails.class);
162
163         MsoResponseWrapper wrapper = mock(MsoResponseWrapper.class);
164         given(wrapper.getResponse()).willReturn("some response");
165         given(msoBusinessLogic.createSvcInstance(objectEqualTo(requestDetails))).willReturn(wrapper);
166
167         // when & then
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"));
173
174         then(cloudService).should(only()).enrichRequestWithCloudOwner(objectEqualTo(requestDetails));
175     }
176
177     @Test
178     public void shouldDelegateVnfInstantiation() throws Exception {
179         // given
180         RequestDetails requestDetails = modelGenerator.nextObject(RequestDetails.class);
181         String serviceInstanceId = "bc305d54-75b4-431b-adb2-eb6b9e546014";
182
183         MsoResponseWrapper wrapper = mock(MsoResponseWrapper.class);
184         given(wrapper.getResponse()).willReturn("some response");
185         given(msoBusinessLogic.createVnf(objectEqualTo(requestDetails), eq(serviceInstanceId))).willReturn(wrapper);
186
187         // when & then
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"));
193
194         then(cloudService).should(only()).enrichRequestWithCloudOwner(objectEqualTo(requestDetails));
195     }
196
197     @Test
198     public void shouldDelegateNewInstanceCreation() throws Exception {
199         // given
200         RequestDetails requestDetails = modelGenerator.nextObject(RequestDetails.class);
201         String serviceInstanceId = "bc305d54-75b4-431b-adb2-eb6b9e546014";
202
203         MsoResponseWrapper wrapper = mock(MsoResponseWrapper.class);
204         given(wrapper.getResponse()).willReturn("some response");
205         given(msoBusinessLogic.createNwInstance(objectEqualTo(requestDetails), eq(serviceInstanceId))).willReturn(wrapper);
206
207         // when & then
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"));
213
214         then(cloudService).should(only()).enrichRequestWithCloudOwner(objectEqualTo(requestDetails));
215     }
216
217     @Test
218     public void shouldCompleteManualTask() throws Exception {
219         // given
220         RequestDetails requestDetails = modelGenerator.nextObject(RequestDetails.class);
221         String taskId = "bc305d54-75b4-431b-adb2-eb6b9e546014";
222
223         MsoResponseWrapper wrapper = mock(MsoResponseWrapper.class);
224         given(wrapper.getResponse()).willReturn("some response");
225         given(msoBusinessLogic.completeManualTask(objectEqualTo(requestDetails), eq(taskId))).willReturn(wrapper);
226
227         // when & then
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"));
233
234         then(cloudService).shouldHaveZeroInteractions();
235     }
236
237     private <T> String asJson(T value) {
238         try {
239             return objectMapper.writeValueAsString(value);
240         } catch (JsonProcessingException e) {
241             throw new RuntimeException(e);
242         }
243     }
244
245     private <T> T objectEqualTo(T expected) {
246         return argThat(given -> asJson(given).equals(asJson(expected)));
247     }
248 }