Fix for Penetration test _ Session and cookie management
[vid.git] / vid-app-common / src / test / java / org / onap / vid / controller / VidControllerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Modifications Copyright 2018 - 2019 Nokia
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
24 import static java.util.stream.Collectors.toMap;
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.hamcrest.CoreMatchers.is;
27 import static org.hamcrest.CoreMatchers.nullValue;
28 import static org.hamcrest.Matchers.not;
29 import static org.mockito.BDDMockito.given;
30 import static org.mockito.BDDMockito.then;
31 import static org.mockito.Mockito.times;
32 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
33 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
34 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
35 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
36
37 import com.fasterxml.jackson.databind.ObjectMapper;
38 import com.google.common.collect.ImmutableList;
39 import com.google.common.collect.ImmutableMap;
40 import java.util.List;
41 import java.util.Map;
42 import java.util.UUID;
43 import java.util.stream.IntStream;
44 import javax.ws.rs.core.MediaType;
45 import org.apache.log4j.BasicConfigurator;
46 import org.junit.Assert;
47 import org.junit.Before;
48 import org.junit.Test;
49 import org.junit.runner.RunWith;
50 import org.mockito.ArgumentCaptor;
51 import org.mockito.Mock;
52 import org.mockito.junit.MockitoJUnitRunner;
53 import org.onap.vid.asdc.AsdcCatalogException;
54 import org.onap.vid.asdc.beans.SecureServices;
55 import org.onap.vid.asdc.beans.Service;
56 import org.onap.vid.model.CR;
57 import org.onap.vid.model.Network;
58 import org.onap.vid.model.Node;
59 import org.onap.vid.model.PombaInstance.PombaRequest;
60 import org.onap.vid.model.PombaInstance.ServiceInstance;
61 import org.onap.vid.model.ServiceModel;
62 import org.onap.vid.model.ServiceProxy;
63 import org.onap.vid.model.VNF;
64 import org.onap.vid.model.VfModule;
65 import org.onap.vid.model.VolumeGroup;
66 import org.onap.vid.roles.RoleProvider;
67 import org.onap.vid.services.AaiService;
68 import org.onap.vid.services.InstantiationTemplatesService;
69 import org.onap.vid.services.PombaService;
70 import org.onap.vid.services.VidService;
71 import org.springframework.test.web.servlet.MockMvc;
72 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
73
74 @RunWith(MockitoJUnitRunner.class)
75 public class VidControllerTest {
76
77     public static final String REST_MODELS_SERVICES = "/rest/models/services";
78     public static final String REST_MODELS_SERVICES_UUID = "/rest/models/services/{uuid}";
79     public static final String REST_MODELS_RESET = "/rest/models/reset";
80     public static final String REST_MODELS_SERVICES_VERIFY_SERVICE = "/rest/models/services/verifyService";
81     @Mock
82     private VidService vidService;
83     @Mock
84     private AaiService aaiService;
85     @Mock
86     private RoleProvider roleProvider;
87     @Mock
88     private PombaService pombaService;
89     @Mock
90     private InstantiationTemplatesService instantiationTemplatesService;
91
92     private VidController vidController;
93     private MockMvc mockMvc;
94     private ObjectMapper objectMapper;
95
96     private String uuid1;
97     private String uuid2;
98     private String uuid3;
99
100     @Before
101     public void setUp() {
102         vidController = new VidController(vidService, aaiService, roleProvider, pombaService, instantiationTemplatesService);
103         BasicConfigurator.configure();
104         mockMvc = MockMvcBuilders.standaloneSetup(vidController).build();
105         objectMapper = new ObjectMapper();
106
107         uuid1 = UUID.randomUUID().toString();
108         uuid2 = UUID.randomUUID().toString();
109         uuid3 = UUID.randomUUID().toString();
110     }
111
112     @Test
113     public void getServices_shouldReturnService_whenServiceExists() throws Exception {
114         List<Service> services1 = ImmutableList.of(createService(uuid1, 1), createService(uuid2, 2), createService(uuid3, 3));
115         List<Service> services2 = ImmutableList.of(createService(uuid1, 4), createService(uuid2, 5), createService(uuid3, 6));
116
117         given(aaiService.getServicesByDistributionStatus())
118             .willReturn(services1);
119
120         given(instantiationTemplatesService.setOnEachServiceIsTemplateExists(services1))
121             .willReturn(services2);
122
123         SecureServices secureServices = new SecureServices();
124         secureServices.setServices(services2);
125         secureServices.setReadOnly(false);
126
127         mockMvc.perform(get(REST_MODELS_SERVICES)
128             .contentType(MediaType.APPLICATION_JSON))
129             .andExpect(status().isOk())
130             .andExpect(content().json(objectMapper.writeValueAsString(secureServices)));
131     }
132
133     @Test
134     public void getService_shouldReturnService_whenNoExceptionIsThrown() throws Exception {
135         ServiceModel model = expectedServiceModel(uuid1);
136
137         given(vidService.getService(uuid1)).willReturn(model);
138
139         mockMvc.perform(get(REST_MODELS_SERVICES_UUID, uuid1)
140             .contentType(MediaType.APPLICATION_JSON))
141             .andExpect(status().isOk())
142             .andExpect(content().json(objectMapper.writeValueAsString(model)));
143     }
144
145     @Test
146     public void getService_shouldThrow_whenAsdcCatalogExceptionIsThrown() throws Exception {
147         String testUuid = UUID.randomUUID().toString();
148
149         given(vidService.getService(testUuid)).willThrow(new AsdcCatalogException("error msg"));
150
151         mockMvc.perform(get(REST_MODELS_SERVICES_UUID, testUuid)
152             .contentType(MediaType.APPLICATION_JSON))
153             .andExpect(status().isServiceUnavailable());
154     }
155
156     @Test
157     public void invalidateServiceModelCache_shouldReturnAccepted() throws Exception {
158         mockMvc.perform(post(REST_MODELS_RESET)
159             .contentType(MediaType.APPLICATION_JSON))
160             .andExpect(status().isAccepted());
161
162         then(vidService).should(times(1)).invalidateServiceCache();
163     }
164
165     @Test
166     public void verifyServiceInstance_shouldReturnOk() throws Exception {
167         PombaRequest pombaRequest = new PombaRequest(ImmutableList.of(new ServiceInstance()));
168
169         mockMvc.perform(post(REST_MODELS_SERVICES_VERIFY_SERVICE)
170             .contentType(MediaType.APPLICATION_JSON)
171             .content(objectMapper.writeValueAsString(pombaRequest)))
172             .andExpect(status().isOk());
173
174         ArgumentCaptor<PombaRequest> argumentCaptor = ArgumentCaptor.forClass(PombaRequest.class);
175         then(pombaService).should(times(1)).verify(argumentCaptor.capture());
176
177         assertThat(pombaRequest).isEqualToComparingFieldByFieldRecursively(argumentCaptor.getValue());
178     }
179
180     private ServiceModel expectedServiceModel(String uuid) {
181         final ServiceModel serviceModel = getModelsByUuid().get(uuid);
182         Assert.assertThat(serviceModel, is(not(nullValue())));
183         return serviceModel;
184     }
185
186     private Service createService(String uuid, int i) {
187         return new Service.ServiceBuilder().setUuid(uuid).setInvariantUUID("invariantUUID" + i)
188             .setCategory("category" + i).setVersion("version" + i).setName("name" + i)
189             .setDistributionStatus("distStatus" + i).setToscaModelURL("toscaModelUrl" + i).build();
190     }
191
192     private ServiceModel createServiceModel(int i) {
193         ServiceModel model = new ServiceModel();
194
195         model.setCollectionResources(ImmutableMap.of("resKey" + i, new CR()));
196         model.setNetworks(ImmutableMap.of("network" + i, new Network()));
197         model.setPnfs(ImmutableMap.of("pnf" + i, new Node()));
198         model.setServiceProxies(ImmutableMap.of("servProxy" + i, new ServiceProxy()));
199         model.setVfModules(ImmutableMap.of("vfmod" + i, new VfModule()));
200         model.setVnfs(ImmutableMap.of("vnf" + i, new VNF()));
201         model.setVolumeGroups(ImmutableMap.of("volgroup" + i, new VolumeGroup()));
202         model.setService(new org.onap.vid.model.Service());
203         return model;
204     }
205
206     private Map<String, ServiceModel> getModelsByUuid() {
207         ServiceModel serviceModel1 = createServiceModel(1);
208         ServiceModel serviceModel2 = createServiceModel(2);
209         ServiceModel serviceModel3 = createServiceModel(3);
210
211         List<ServiceModel> pseudoServiceModels = ImmutableList.of(serviceModel1, serviceModel2, serviceModel3);
212         List<String> uuids = ImmutableList.of(uuid1, uuid2, uuid3);
213         return IntStream.range(0, pseudoServiceModels.size()).boxed()
214             .collect(toMap(i -> uuids.get(i), i -> pseudoServiceModels.get(i)));
215     }
216 }