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 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.model.*;
39 import org.onap.vid.model.PombaInstance.PombaRequest;
40 import org.onap.vid.model.PombaInstance.ServiceInstance;
41 import org.onap.vid.roles.RoleProvider;
42 import org.onap.vid.services.AaiService;
43 import org.onap.vid.services.PombaService;
44 import org.onap.vid.services.VidService;
45 import org.springframework.test.web.servlet.MockMvc;
46 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
48 import javax.ws.rs.core.MediaType;
49 import java.util.List;
51 import java.util.UUID;
52 import java.util.stream.IntStream;
54 import static java.util.stream.Collectors.toMap;
55 import static org.assertj.core.api.Assertions.assertThat;
56 import static org.hamcrest.CoreMatchers.is;
57 import static org.hamcrest.CoreMatchers.nullValue;
58 import static org.hamcrest.Matchers.not;
59 import static org.mockito.BDDMockito.given;
60 import static org.mockito.BDDMockito.then;
61 import static org.mockito.Mockito.times;
62 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
63 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
64 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
65 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
67 @RunWith(MockitoJUnitRunner.class)
68 public class VidControllerTest {
70 public static final String REST_MODELS_SERVICES = "/rest/models/services";
71 public static final String REST_MODELS_SERVICES_UUID = "/rest/models/services/{uuid}";
72 public static final String REST_MODELS_RESET = "/rest/models/reset";
73 public static final String REST_MODELS_SERVICES_VERIFY_SERVICE = "/rest/models/services/verifyService";
75 private VidService vidService;
77 private AaiService aaiService;
79 private RoleProvider roleProvider;
81 private PombaService pombaService;
83 private VidController vidController;
84 private MockMvc mockMvc;
85 private ObjectMapper objectMapper;
93 vidController = new VidController(vidService, aaiService, roleProvider, pombaService);
94 BasicConfigurator.configure();
95 mockMvc = MockMvcBuilders.standaloneSetup(vidController).build();
96 objectMapper = new ObjectMapper();
98 uuid1 = UUID.randomUUID().toString();
99 uuid2 = UUID.randomUUID().toString();
100 uuid3 = UUID.randomUUID().toString();
104 public void getServices_shouldReturnService_whenServiceExists() throws Exception {
105 List<Service> services = ImmutableList.of(createService(uuid1, 1), createService(uuid2, 2), createService(uuid3, 3));
107 given(aaiService.getServicesByDistributionStatus()).willReturn(services);
109 SecureServices secureServices = new SecureServices();
110 secureServices.setServices(services);
111 secureServices.setReadOnly(false);
113 mockMvc.perform(get(REST_MODELS_SERVICES)
114 .contentType(MediaType.APPLICATION_JSON))
115 .andExpect(status().isOk())
116 .andExpect(content().json(objectMapper.writeValueAsString(secureServices)));
120 public void getService_shouldReturnService_whenNoExceptionIsThrown() throws Exception {
121 ServiceModel model = expectedServiceModel(uuid1);
123 given(vidService.getService(uuid1)).willReturn(model);
125 mockMvc.perform(get(REST_MODELS_SERVICES_UUID, uuid1)
126 .contentType(MediaType.APPLICATION_JSON))
127 .andExpect(status().isOk())
128 .andExpect(content().json(objectMapper.writeValueAsString(model)));
132 public void getService_shouldThrow_whenAsdcCatalogExceptionIsThrown() throws Exception {
133 String testUuid = UUID.randomUUID().toString();
135 given(vidService.getService(testUuid)).willThrow(new AsdcCatalogException("error msg"));
137 mockMvc.perform(get(REST_MODELS_SERVICES_UUID, testUuid)
138 .contentType(MediaType.APPLICATION_JSON))
139 .andExpect(status().isServiceUnavailable());
143 public void invalidateServiceModelCache_shouldReturnAccepted() throws Exception {
144 mockMvc.perform(post(REST_MODELS_RESET)
145 .contentType(MediaType.APPLICATION_JSON))
146 .andExpect(status().isAccepted());
148 then(vidService).should(times(1)).invalidateServiceCache();
152 public void verifyServiceInstance_shouldReturnOk() throws Exception {
153 PombaRequest pombaRequest = new PombaRequest(ImmutableList.of(new ServiceInstance()));
155 mockMvc.perform(post(REST_MODELS_SERVICES_VERIFY_SERVICE)
156 .contentType(MediaType.APPLICATION_JSON)
157 .content(objectMapper.writeValueAsString(pombaRequest)))
158 .andExpect(status().isOk());
160 ArgumentCaptor<PombaRequest> argumentCaptor = ArgumentCaptor.forClass(PombaRequest.class);
161 then(pombaService).should(times(1)).verify(argumentCaptor.capture());
163 assertThat(pombaRequest).isEqualToComparingFieldByFieldRecursively(argumentCaptor.getValue());
166 private ServiceModel expectedServiceModel(String uuid) {
167 final ServiceModel serviceModel = getModelsByUuid().get(uuid);
168 Assert.assertThat(serviceModel, is(not(nullValue())));
172 private Service createService(String uuid, int i) {
173 return new Service.ServiceBuilder().setUuid(uuid).setInvariantUUID("invariantUUID" + i)
174 .setCategory("category" + i).setVersion("version" + i).setName("name" + i)
175 .setDistributionStatus("distStatus" + i).setToscaModelURL("toscaModelUrl" + i).build();
178 private ServiceModel createServiceModel(int i) {
179 ServiceModel model = new ServiceModel();
181 model.setCollectionResources(ImmutableMap.of("resKey" + i, new CR()));
182 model.setNetworks(ImmutableMap.of("network" + i, new Network()));
183 model.setPnfs(ImmutableMap.of("pnf" + i, new Node()));
184 model.setServiceProxies(ImmutableMap.of("servProxy" + i, new ServiceProxy()));
185 model.setVfModules(ImmutableMap.of("vfmod" + i, new VfModule()));
186 model.setVnfs(ImmutableMap.of("vnf" + i, new VNF()));
187 model.setVolumeGroups(ImmutableMap.of("volgroup" + i, new VolumeGroup()));
188 model.setService(new org.onap.vid.model.Service());
192 private Map<String, ServiceModel> getModelsByUuid() {
193 ServiceModel serviceModel1 = createServiceModel(1);
194 ServiceModel serviceModel2 = createServiceModel(2);
195 ServiceModel serviceModel3 = createServiceModel(3);
197 List<ServiceModel> pseudoServiceModels = ImmutableList.of(serviceModel1, serviceModel2, serviceModel3);
198 List<String> uuids = ImmutableList.of(uuid1, uuid2, uuid3);
199 return IntStream.range(0, pseudoServiceModels.size()).boxed()
200 .collect(toMap(i -> uuids.get(i), i -> pseudoServiceModels.get(i)));