Add VidController junits
[vid.git] / vid-app-common / src / test / java / org / onap / vid / controllers / VidControllerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
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
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.controllers;
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.google.common.collect.ImmutableList;
38 import com.google.common.collect.ImmutableMap;
39 import java.util.List;
40 import java.util.Map;
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;
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
90     private VidController vidController;
91     private MockMvc mockMvc;
92     private ObjectMapper objectMapper;
93
94     private String uuid1;
95     private String uuid2;
96     private String uuid3;
97
98     @Before
99     public void setUp() {
100         vidController = new VidController(vidService, aaiService, roleProvider, pombaService);
101         BasicConfigurator.configure();
102         mockMvc = MockMvcBuilders.standaloneSetup(vidController).build();
103         objectMapper = new ObjectMapper();
104
105         uuid1 = UUID.randomUUID().toString();
106         uuid2 = UUID.randomUUID().toString();
107         uuid3 = UUID.randomUUID().toString();
108     }
109
110     @Test
111     public void getServices_shouldReturnService_whenServiceExists() throws Exception {
112         List<Service> services = ImmutableList.of(createService(uuid1, 1), createService(uuid2, 2), createService(uuid3, 3));
113
114         given(aaiService.getServicesByDistributionStatus()).willReturn(services);
115
116         SecureServices secureServices = new SecureServices();
117         secureServices.setServices(services);
118         secureServices.setReadOnly(false);
119
120         mockMvc.perform(get(REST_MODELS_SERVICES)
121             .contentType(MediaType.APPLICATION_JSON))
122             .andExpect(status().isOk())
123             .andExpect(content().json(objectMapper.writeValueAsString(secureServices)));
124     }
125
126     @Test
127     public void getService_shouldReturnService_whenNoExceptionIsThrown() throws Exception {
128         ServiceModel model = expectedServiceModel(uuid1);
129
130         given(vidService.getService(uuid1)).willReturn(model);
131
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)));
136     }
137
138     @Test
139     public void getService_shouldThrow_whenAsdcCatalogExceptionIsThrown() throws Exception {
140         String testUuid = UUID.randomUUID().toString();
141
142         given(vidService.getService(testUuid)).willThrow(new AsdcCatalogException("error msg"));
143
144         mockMvc.perform(get(REST_MODELS_SERVICES_UUID, testUuid)
145             .contentType(MediaType.APPLICATION_JSON))
146             .andExpect(status().isServiceUnavailable());
147     }
148
149     @Test
150     public void invalidateServiceModelCache_shouldReturnAccepted() throws Exception {
151         mockMvc.perform(post(REST_MODELS_RESET)
152             .contentType(MediaType.APPLICATION_JSON))
153             .andExpect(status().isAccepted());
154
155         then(vidService).should(times(1)).invalidateServiceCache();
156     }
157
158     @Test
159     public void verifyServiceInstance_shouldReturnOk() throws Exception {
160         PombaRequest pombaRequest = new PombaRequest();
161         pombaRequest.serviceInstanceList = ImmutableList.of(new ServiceInstance());
162
163         mockMvc.perform(post(REST_MODELS_SERVICES_VERIFY_SERVICE)
164             .contentType(MediaType.APPLICATION_JSON)
165             .content(objectMapper.writeValueAsString(pombaRequest)))
166             .andExpect(status().isOk());
167
168         ArgumentCaptor<PombaRequest> argumentCaptor = ArgumentCaptor.forClass(PombaRequest.class);
169         then(pombaService).should(times(1)).verify(argumentCaptor.capture());
170
171         assertThat(pombaRequest).isEqualToComparingFieldByFieldRecursively(argumentCaptor.getValue());
172     }
173
174     private ServiceModel expectedServiceModel(String uuid) {
175         final ServiceModel serviceModel = getModelsByUuid().get(uuid);
176         Assert.assertThat(serviceModel, is(not(nullValue())));
177         return serviceModel;
178     }
179
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();
184     }
185
186     private ServiceModel createServiceModel(int i) {
187         ServiceModel model = new ServiceModel();
188
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());
197         return model;
198     }
199
200     private Map<String, ServiceModel> getModelsByUuid() {
201         ServiceModel serviceModel1 = createServiceModel(1);
202         ServiceModel serviceModel2 = createServiceModel(2);
203         ServiceModel serviceModel3 = createServiceModel(3);
204
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)));
209     }
210 }