2 * ============LICENSE_START=======================================================
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
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;
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;
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;
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;
74 @RunWith(MockitoJUnitRunner.class)
75 public class VidControllerTest {
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";
82 private VidService vidService;
84 private AaiService aaiService;
86 private RoleProvider roleProvider;
88 private PombaService pombaService;
90 private InstantiationTemplatesService instantiationTemplatesService;
92 private VidController vidController;
93 private MockMvc mockMvc;
94 private ObjectMapper objectMapper;
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();
107 uuid1 = UUID.randomUUID().toString();
108 uuid2 = UUID.randomUUID().toString();
109 uuid3 = UUID.randomUUID().toString();
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));
117 given(aaiService.getServicesByDistributionStatus())
118 .willReturn(services1);
120 given(instantiationTemplatesService.setOnEachServiceIsTemplateExists(services1))
121 .willReturn(services2);
123 SecureServices secureServices = new SecureServices();
124 secureServices.setServices(services2);
125 secureServices.setReadOnly(false);
127 mockMvc.perform(get(REST_MODELS_SERVICES)
128 .contentType(MediaType.APPLICATION_JSON))
129 .andExpect(status().isOk())
130 .andExpect(content().json(objectMapper.writeValueAsString(secureServices)));
134 public void getService_shouldReturnService_whenNoExceptionIsThrown() throws Exception {
135 ServiceModel model = expectedServiceModel(uuid1);
137 given(vidService.getService(uuid1)).willReturn(model);
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)));
146 public void getService_shouldThrow_whenAsdcCatalogExceptionIsThrown() throws Exception {
147 String testUuid = UUID.randomUUID().toString();
149 given(vidService.getService(testUuid)).willThrow(new AsdcCatalogException("error msg"));
151 mockMvc.perform(get(REST_MODELS_SERVICES_UUID, testUuid)
152 .contentType(MediaType.APPLICATION_JSON))
153 .andExpect(status().isServiceUnavailable());
157 public void invalidateServiceModelCache_shouldReturnAccepted() throws Exception {
158 mockMvc.perform(post(REST_MODELS_RESET)
159 .contentType(MediaType.APPLICATION_JSON))
160 .andExpect(status().isAccepted());
162 then(vidService).should(times(1)).invalidateServiceCache();
166 public void verifyServiceInstance_shouldReturnOk() throws Exception {
167 PombaRequest pombaRequest = new PombaRequest(ImmutableList.of(new ServiceInstance()));
169 mockMvc.perform(post(REST_MODELS_SERVICES_VERIFY_SERVICE)
170 .contentType(MediaType.APPLICATION_JSON)
171 .content(objectMapper.writeValueAsString(pombaRequest)))
172 .andExpect(status().isOk());
174 ArgumentCaptor<PombaRequest> argumentCaptor = ArgumentCaptor.forClass(PombaRequest.class);
175 then(pombaService).should(times(1)).verify(argumentCaptor.capture());
177 assertThat(pombaRequest).isEqualToComparingFieldByFieldRecursively(argumentCaptor.getValue());
180 private ServiceModel expectedServiceModel(String uuid) {
181 final ServiceModel serviceModel = getModelsByUuid().get(uuid);
182 Assert.assertThat(serviceModel, is(not(nullValue())));
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();
192 private ServiceModel createServiceModel(int i) {
193 ServiceModel model = new ServiceModel();
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());
206 private Map<String, ServiceModel> getModelsByUuid() {
207 ServiceModel serviceModel1 = createServiceModel(1);
208 ServiceModel serviceModel2 = createServiceModel(2);
209 ServiceModel serviceModel3 = createServiceModel(3);
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)));