02ab287be07d286e6752e005c7c1379a22dd45ca
[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.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;
55
56 public class MsoControllerTest {
57
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();
65
66     private MockMvc mockMvc;
67     private MsoBusinessLogic msoBusinessLogic;
68     private CloudOwnerService cloudService;
69
70     @Before
71     public void setUp() {
72         msoBusinessLogic = mock(MsoBusinessLogic.class);
73         cloudService = mock(CloudOwnerService.class);
74         MsoController msoController = new MsoController(msoBusinessLogic, cloudService);
75
76         mockMvc = MockMvcBuilders.standaloneSetup(msoController).build();
77     }
78
79     @Test
80     public void shouldDelegateNewServiceInstantiation() throws Exception {
81         // given
82         RequestDetails requestDetails = modelGenerator.nextObject(RequestDetails.class);
83
84         MsoResponseWrapper expectedResponse = new MsoResponseWrapper(200, "test");
85         given(msoBusinessLogic
86             .createSvcInstance(objectEqualTo(requestDetails)))
87             .willReturn(expectedResponse);
88
89         // when & then
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)));
95
96         then(cloudService).should(only()).enrichRequestWithCloudOwner(objectEqualTo(requestDetails));
97     }
98
99     @Test
100     public void shouldReturnOrchestrationRequestsForDashboard() throws Exception {
101         // given
102         List<Request> orchestrationRequests = modelGenerator
103             .objects(Request.class, 2)
104             .collect(Collectors.toList());
105
106         given(msoBusinessLogic.getOrchestrationRequestsForDashboard()).willReturn(orchestrationRequests);
107
108         // when & then
109         mockMvc.perform(get("/mso/mso_get_orch_reqs/dashboard"))
110             .andExpect(status().isOk())
111             .andExpect(content().json(objectMapper.writeValueAsString(orchestrationRequests)));
112
113         then(cloudService).shouldHaveZeroInteractions();
114     }
115
116     @Test
117     public void shouldReturnManualTasksById() throws Exception {
118         // given
119         List<Task> manualTasks = modelGenerator
120             .objects(Task.class, 2)
121             .collect(Collectors.toList());
122
123         String originalRequestId = "za1234d1-5a33-55df-13ab-12abad84e335";
124         given(msoBusinessLogic.getManualTasksByRequestId(originalRequestId)).willReturn(manualTasks);
125
126         // when & then
127         mockMvc.perform(get("/mso/mso_get_man_task/{id}", originalRequestId))
128             .andExpect(status().isOk())
129             .andExpect(content().json(objectMapper.writeValueAsString(manualTasks)));
130
131         then(cloudService).shouldHaveZeroInteractions();
132     }
133
134     @Test
135     public void shouldDelegateE2EServiceInstantiation() throws Exception {
136         // given
137         String requestDetails = "some request details";
138         Map<String, Object> payload = new LinkedHashMap<>();
139         payload.put("requestDetails", requestDetails);
140
141         MsoResponseWrapper wrapper = mock(MsoResponseWrapper.class);
142         given(wrapper.getResponse()).willReturn("some response");
143         given(msoBusinessLogic.createE2eSvcInstance(requestDetails)).willReturn(wrapper);
144
145         // when & then
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"));
151
152         then(cloudService).shouldHaveZeroInteractions();
153     }
154
155     @Test
156     public void shouldDelegateServiceInstantiation() throws Exception {
157         // given
158         RequestDetails requestDetails = modelGenerator.nextObject(RequestDetails.class);
159
160         MsoResponseWrapper wrapper = mock(MsoResponseWrapper.class);
161         given(wrapper.getResponse()).willReturn("some response");
162         given(msoBusinessLogic.createSvcInstance(objectEqualTo(requestDetails))).willReturn(wrapper);
163
164         // when & then
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"));
170
171         then(cloudService).should(only()).enrichRequestWithCloudOwner(objectEqualTo(requestDetails));
172     }
173
174     @Test
175     public void shouldDelegateVnfInstantiation() throws Exception {
176         // given
177         RequestDetails requestDetails = modelGenerator.nextObject(RequestDetails.class);
178         String serviceInstanceId = "bc305d54-75b4-431b-adb2-eb6b9e546014";
179
180         MsoResponseWrapper wrapper = mock(MsoResponseWrapper.class);
181         given(wrapper.getResponse()).willReturn("some response");
182         given(msoBusinessLogic.createVnf(objectEqualTo(requestDetails), eq(serviceInstanceId))).willReturn(wrapper);
183
184         // when & then
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"));
190
191         then(cloudService).should(only()).enrichRequestWithCloudOwner(objectEqualTo(requestDetails));
192     }
193
194     @Test
195     public void shouldDelegateNewInstanceCreation() throws Exception {
196         // given
197         RequestDetails requestDetails = modelGenerator.nextObject(RequestDetails.class);
198         String serviceInstanceId = "bc305d54-75b4-431b-adb2-eb6b9e546014";
199
200         MsoResponseWrapper wrapper = mock(MsoResponseWrapper.class);
201         given(wrapper.getResponse()).willReturn("some response");
202         given(msoBusinessLogic.createNwInstance(objectEqualTo(requestDetails), eq(serviceInstanceId))).willReturn(wrapper);
203
204         // when & then
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"));
210
211         then(cloudService).should(only()).enrichRequestWithCloudOwner(objectEqualTo(requestDetails));
212     }
213
214     @Test
215     public void shouldCompleteManualTask() throws Exception {
216         // given
217         RequestDetails requestDetails = modelGenerator.nextObject(RequestDetails.class);
218         String taskId = "bc305d54-75b4-431b-adb2-eb6b9e546014";
219
220         MsoResponseWrapper wrapper = mock(MsoResponseWrapper.class);
221         given(wrapper.getResponse()).willReturn("some response");
222         given(msoBusinessLogic.completeManualTask(objectEqualTo(requestDetails), eq(taskId))).willReturn(wrapper);
223
224         // when & then
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"));
230
231         then(cloudService).shouldHaveZeroInteractions();
232     }
233
234     private <T> String asJson(T value) {
235         try {
236             return objectMapper.writeValueAsString(value);
237         } catch (JsonProcessingException e) {
238             throw new RuntimeException(e);
239         }
240     }
241
242     private <T> T objectEqualTo(T expected) {
243         return argThat(given -> asJson(given).equals(asJson(expected)));
244     }
245 }