Merge "Initialize parameters maps before reading params"
[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.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;
34
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;
54
55 public class MsoControllerTest {
56
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();
64
65     private MockMvc mockMvc;
66     private MsoBusinessLogic msoBusinessLogic;
67     private CloudOwnerService cloudService;
68
69     @Before
70     public void setUp() {
71         msoBusinessLogic = mock(MsoBusinessLogic.class);
72         cloudService = mock(CloudOwnerService.class);
73         MsoController msoController = new MsoController(msoBusinessLogic, cloudService);
74
75         mockMvc = MockMvcBuilders.standaloneSetup(msoController).build();
76     }
77
78     @Test
79     public void shouldDelegateNewInstanceCreation() throws Exception {
80         // given
81         RequestDetails given = modelGenerator.nextObject(RequestDetails.class);
82         String payload = objectMapper.writeValueAsString(given);
83
84         MsoResponseWrapper expectedResponse = new MsoResponseWrapper(200, "test");
85         given(msoBusinessLogic
86             .createSvcInstance(argThat(request -> asJson(request).equals(payload))))
87             .willReturn(expectedResponse);
88
89         // when & then
90         mockMvc.perform(post("/mso/mso_create_svc_instance")
91             .content(payload)
92             .contentType(APPLICATION_JSON))
93             .andExpect(status().isOk())
94             .andExpect(content().json(asJson(expectedResponse)));
95
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));
99     }
100
101     @Test
102     public void shouldReturnOrchestrationRequestsForDashboard() throws Exception {
103         // given
104         List<Request> orchestrationRequests = modelGenerator
105             .objects(Request.class, 2)
106             .collect(Collectors.toList());
107
108         given(msoBusinessLogic.getOrchestrationRequestsForDashboard()).willReturn(orchestrationRequests);
109
110         // when & then
111         mockMvc.perform(get("/mso/mso_get_orch_reqs/dashboard"))
112             .andExpect(status().isOk())
113             .andExpect(content().json(objectMapper.writeValueAsString(orchestrationRequests)));
114
115         then(cloudService).shouldHaveZeroInteractions();
116     }
117
118     @Test
119     public void shouldReturnManualTasksById() throws Exception {
120         // given
121         List<Task> manualTasks = modelGenerator
122             .objects(Task.class, 2)
123             .collect(Collectors.toList());
124
125         String originalRequestId = "za1234d1-5a33-55df-13ab-12abad84e335";
126         given(msoBusinessLogic.getManualTasksByRequestId(originalRequestId)).willReturn(manualTasks);
127
128         // when & then
129         mockMvc.perform(get("/mso/mso_get_man_task/{id}", originalRequestId))
130             .andExpect(status().isOk())
131             .andExpect(content().json(objectMapper.writeValueAsString(manualTasks)));
132
133         then(cloudService).shouldHaveZeroInteractions();
134     }
135
136     private <T> String asJson(T value) {
137         try {
138             return objectMapper.writeValueAsString(value);
139         } catch (JsonProcessingException e) {
140             throw new RuntimeException(e);
141         }
142     }
143 }