2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Modifications Copyright 2018 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.controllers;
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.google.common.collect.ImmutableList;
38 import com.google.common.collect.ImmutableMap;
39 import java.util.List;
41 import java.util.UUID;
42 import java.util.stream.IntStream;
43 import javax.ws.rs.core.MediaType;
44 import org.apache.log4j.BasicConfigurator;
45 import org.codehaus.jackson.map.ObjectMapper;
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.runners.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.asdc.beans.ServiceBuilder;
57 import org.onap.vid.model.CR;
58 import org.onap.vid.model.Network;
59 import org.onap.vid.model.Node;
60 import org.onap.vid.model.PombaInstance.PombaRequest;
61 import org.onap.vid.model.PombaInstance.ServiceInstance;
62 import org.onap.vid.model.ServiceModel;
63 import org.onap.vid.model.ServiceProxy;
64 import org.onap.vid.model.VNF;
65 import org.onap.vid.model.VfModule;
66 import org.onap.vid.model.VolumeGroup;
67 import org.onap.vid.roles.RoleProvider;
68 import org.onap.vid.services.AaiService;
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 VidController vidController;
91 private MockMvc mockMvc;
92 private ObjectMapper objectMapper;
100 vidController = new VidController(vidService, aaiService, roleProvider, pombaService);
101 BasicConfigurator.configure();
102 mockMvc = MockMvcBuilders.standaloneSetup(vidController).build();
103 objectMapper = new ObjectMapper();
105 uuid1 = UUID.randomUUID().toString();
106 uuid2 = UUID.randomUUID().toString();
107 uuid3 = UUID.randomUUID().toString();
111 public void getServices_shouldReturnService_whenServiceExists() throws Exception {
112 List<Service> services = ImmutableList.of(createService(uuid1, 1), createService(uuid2, 2), createService(uuid3, 3));
114 given(aaiService.getServicesByDistributionStatus()).willReturn(services);
116 SecureServices secureServices = new SecureServices();
117 secureServices.setServices(services);
118 secureServices.setReadOnly(false);
120 mockMvc.perform(get(REST_MODELS_SERVICES)
121 .contentType(MediaType.APPLICATION_JSON))
122 .andExpect(status().isOk())
123 .andExpect(content().json(objectMapper.writeValueAsString(secureServices)));
127 public void getService_shouldReturnService_whenNoExceptionIsThrown() throws Exception {
128 ServiceModel model = expectedServiceModel(uuid1);
130 given(vidService.getService(uuid1)).willReturn(model);
132 mockMvc.perform(get(REST_MODELS_SERVICES_UUID, uuid1)
133 .contentType(MediaType.APPLICATION_JSON))
134 .andExpect(status().isOk())
135 .andExpect(content().json(objectMapper.writeValueAsString(model)));
139 public void getService_shouldThrow_whenAsdcCatalogExceptionIsThrown() throws Exception {
140 String testUuid = UUID.randomUUID().toString();
142 given(vidService.getService(testUuid)).willThrow(new AsdcCatalogException("error msg"));
144 mockMvc.perform(get(REST_MODELS_SERVICES_UUID, testUuid)
145 .contentType(MediaType.APPLICATION_JSON))
146 .andExpect(status().isServiceUnavailable());
150 public void invalidateServiceModelCache_shouldReturnAccepted() throws Exception {
151 mockMvc.perform(post(REST_MODELS_RESET)
152 .contentType(MediaType.APPLICATION_JSON))
153 .andExpect(status().isAccepted());
155 then(vidService).should(times(1)).invalidateServiceCache();
159 public void verifyServiceInstance_shouldReturnOk() throws Exception {
160 PombaRequest pombaRequest = new PombaRequest();
161 pombaRequest.serviceInstanceList = ImmutableList.of(new ServiceInstance());
163 mockMvc.perform(post(REST_MODELS_SERVICES_VERIFY_SERVICE)
164 .contentType(MediaType.APPLICATION_JSON)
165 .content(objectMapper.writeValueAsString(pombaRequest)))
166 .andExpect(status().isOk());
168 ArgumentCaptor<PombaRequest> argumentCaptor = ArgumentCaptor.forClass(PombaRequest.class);
169 then(pombaService).should(times(1)).verify(argumentCaptor.capture());
171 assertThat(pombaRequest).isEqualToComparingFieldByFieldRecursively(argumentCaptor.getValue());
174 private ServiceModel expectedServiceModel(String uuid) {
175 final ServiceModel serviceModel = getModelsByUuid().get(uuid);
176 Assert.assertThat(serviceModel, is(not(nullValue())));
180 private Service createService(String uuid, int i) {
181 return new ServiceBuilder().setUuid(uuid).setInvariantUUID("invariantUUID" + i)
182 .setCategory("category" + i).setVersion("version" + i).setName("name" + i)
183 .setDistributionStatus("distStatus" + i).setToscaModelURL("toscaModelUrl" + i).build();
186 private ServiceModel createServiceModel(int i) {
187 ServiceModel model = new ServiceModel();
189 model.setCollectionResource(ImmutableMap.of("resKey" + i, new CR()));
190 model.setNetworks(ImmutableMap.of("network" + i, new Network()));
191 model.setPnfs(ImmutableMap.of("pnf" + i, new Node()));
192 model.setServiceProxies(ImmutableMap.of("servProxy" + i, new ServiceProxy()));
193 model.setVfModules(ImmutableMap.of("vfmod" + i, new VfModule()));
194 model.setVnfs(ImmutableMap.of("vnf" + i, new VNF()));
195 model.setVolumeGroups(ImmutableMap.of("volgroup" + i, new VolumeGroup()));
196 model.setService(new org.onap.vid.model.Service());
200 private Map<String, ServiceModel> getModelsByUuid() {
201 ServiceModel serviceModel1 = createServiceModel(1);
202 ServiceModel serviceModel2 = createServiceModel(2);
203 ServiceModel serviceModel3 = createServiceModel(3);
205 List<ServiceModel> pseudoServiceModels = ImmutableList.of(serviceModel1, serviceModel2, serviceModel3);
206 List<String> uuids = ImmutableList.of(uuid1, uuid2, uuid3);
207 return IntStream.range(0, pseudoServiceModels.size()).boxed()
208 .collect(toMap(i -> uuids.get(i), i -> pseudoServiceModels.get(i)));