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.controller;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import com.google.common.collect.ImmutableList;
26 import com.google.common.collect.ImmutableMap;
27 import org.apache.log4j.BasicConfigurator;
28 import org.junit.Assert;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.ArgumentCaptor;
33 import org.mockito.Mock;
34 import org.mockito.junit.MockitoJUnitRunner;
35 import org.onap.vid.asdc.AsdcCatalogException;
36 import org.onap.vid.asdc.beans.SecureServices;
37 import org.onap.vid.asdc.beans.Service;
38 import org.onap.vid.asdc.beans.ServiceBuilder;
39 import org.onap.vid.model.*;
40 import org.onap.vid.model.PombaInstance.PombaRequest;
41 import org.onap.vid.model.PombaInstance.ServiceInstance;
42 import org.onap.vid.roles.RoleProvider;
43 import org.onap.vid.services.AaiService;
44 import org.onap.vid.services.PombaService;
45 import org.onap.vid.services.VidService;
46 import org.springframework.test.web.servlet.MockMvc;
47 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
49 import javax.ws.rs.core.MediaType;
50 import java.util.List;
52 import java.util.UUID;
53 import java.util.stream.IntStream;
55 import static java.util.stream.Collectors.toMap;
56 import static org.assertj.core.api.Assertions.assertThat;
57 import static org.hamcrest.CoreMatchers.is;
58 import static org.hamcrest.CoreMatchers.nullValue;
59 import static org.hamcrest.Matchers.not;
60 import static org.mockito.BDDMockito.given;
61 import static org.mockito.BDDMockito.then;
62 import static org.mockito.Mockito.times;
63 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
64 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
65 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
66 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
68 @RunWith(MockitoJUnitRunner.class)
69 public class VidControllerTest {
71 public static final String REST_MODELS_SERVICES = "/rest/models/services";
72 public static final String REST_MODELS_SERVICES_UUID = "/rest/models/services/{uuid}";
73 public static final String REST_MODELS_RESET = "/rest/models/reset";
74 public static final String REST_MODELS_SERVICES_VERIFY_SERVICE = "/rest/models/services/verifyService";
76 private VidService vidService;
78 private AaiService aaiService;
80 private RoleProvider roleProvider;
82 private PombaService pombaService;
84 private VidController vidController;
85 private MockMvc mockMvc;
86 private ObjectMapper objectMapper;
94 vidController = new VidController(vidService, aaiService, roleProvider, pombaService);
95 BasicConfigurator.configure();
96 mockMvc = MockMvcBuilders.standaloneSetup(vidController).build();
97 objectMapper = new ObjectMapper();
99 uuid1 = UUID.randomUUID().toString();
100 uuid2 = UUID.randomUUID().toString();
101 uuid3 = UUID.randomUUID().toString();
105 public void getServices_shouldReturnService_whenServiceExists() throws Exception {
106 List<Service> services = ImmutableList.of(createService(uuid1, 1), createService(uuid2, 2), createService(uuid3, 3));
108 given(aaiService.getServicesByDistributionStatus()).willReturn(services);
110 SecureServices secureServices = new SecureServices();
111 secureServices.setServices(services);
112 secureServices.setReadOnly(false);
114 mockMvc.perform(get(REST_MODELS_SERVICES)
115 .contentType(MediaType.APPLICATION_JSON))
116 .andExpect(status().isOk())
117 .andExpect(content().json(objectMapper.writeValueAsString(secureServices)));
121 public void getService_shouldReturnService_whenNoExceptionIsThrown() throws Exception {
122 ServiceModel model = expectedServiceModel(uuid1);
124 given(vidService.getService(uuid1)).willReturn(model);
126 mockMvc.perform(get(REST_MODELS_SERVICES_UUID, uuid1)
127 .contentType(MediaType.APPLICATION_JSON))
128 .andExpect(status().isOk())
129 .andExpect(content().json(objectMapper.writeValueAsString(model)));
133 public void getService_shouldThrow_whenAsdcCatalogExceptionIsThrown() throws Exception {
134 String testUuid = UUID.randomUUID().toString();
136 given(vidService.getService(testUuid)).willThrow(new AsdcCatalogException("error msg"));
138 mockMvc.perform(get(REST_MODELS_SERVICES_UUID, testUuid)
139 .contentType(MediaType.APPLICATION_JSON))
140 .andExpect(status().isServiceUnavailable());
144 public void invalidateServiceModelCache_shouldReturnAccepted() throws Exception {
145 mockMvc.perform(post(REST_MODELS_RESET)
146 .contentType(MediaType.APPLICATION_JSON))
147 .andExpect(status().isAccepted());
149 then(vidService).should(times(1)).invalidateServiceCache();
153 public void verifyServiceInstance_shouldReturnOk() throws Exception {
154 PombaRequest pombaRequest = new PombaRequest();
155 pombaRequest.serviceInstanceList = ImmutableList.of(new ServiceInstance());
157 mockMvc.perform(post(REST_MODELS_SERVICES_VERIFY_SERVICE)
158 .contentType(MediaType.APPLICATION_JSON)
159 .content(objectMapper.writeValueAsString(pombaRequest)))
160 .andExpect(status().isOk());
162 ArgumentCaptor<PombaRequest> argumentCaptor = ArgumentCaptor.forClass(PombaRequest.class);
163 then(pombaService).should(times(1)).verify(argumentCaptor.capture());
165 assertThat(pombaRequest).isEqualToComparingFieldByFieldRecursively(argumentCaptor.getValue());
168 private ServiceModel expectedServiceModel(String uuid) {
169 final ServiceModel serviceModel = getModelsByUuid().get(uuid);
170 Assert.assertThat(serviceModel, is(not(nullValue())));
174 private Service createService(String uuid, int i) {
175 return new ServiceBuilder().setUuid(uuid).setInvariantUUID("invariantUUID" + i)
176 .setCategory("category" + i).setVersion("version" + i).setName("name" + i)
177 .setDistributionStatus("distStatus" + i).setToscaModelURL("toscaModelUrl" + i).build();
180 private ServiceModel createServiceModel(int i) {
181 ServiceModel model = new ServiceModel();
183 model.setCollectionResource(ImmutableMap.of("resKey" + i, new CR()));
184 model.setNetworks(ImmutableMap.of("network" + i, new Network()));
185 model.setPnfs(ImmutableMap.of("pnf" + i, new Node()));
186 model.setServiceProxies(ImmutableMap.of("servProxy" + i, new ServiceProxy()));
187 model.setVfModules(ImmutableMap.of("vfmod" + i, new VfModule()));
188 model.setVnfs(ImmutableMap.of("vnf" + i, new VNF()));
189 model.setVolumeGroups(ImmutableMap.of("volgroup" + i, new VolumeGroup()));
190 model.setService(new org.onap.vid.model.Service());
194 private Map<String, ServiceModel> getModelsByUuid() {
195 ServiceModel serviceModel1 = createServiceModel(1);
196 ServiceModel serviceModel2 = createServiceModel(2);
197 ServiceModel serviceModel3 = createServiceModel(3);
199 List<ServiceModel> pseudoServiceModels = ImmutableList.of(serviceModel1, serviceModel2, serviceModel3);
200 List<String> uuids = ImmutableList.of(uuid1, uuid2, uuid3);
201 return IntStream.range(0, pseudoServiceModels.size()).boxed()
202 .collect(toMap(i -> uuids.get(i), i -> pseudoServiceModels.get(i)));