Merge "Extend probe mechanism"
[vid.git] / vid-app-common / src / test / java / org / onap / vid / controller / AaiControllerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019 Nokia.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.vid.controller;
23
24 import static org.mockito.BDDMockito.given;
25 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
26 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
27 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
28
29 import com.fasterxml.jackson.databind.ObjectMapper;
30 import com.google.common.collect.ImmutableList;
31 import com.google.common.collect.ImmutableMap;
32 import com.google.common.collect.ImmutableMultimap;
33 import com.google.common.collect.Lists;
34 import com.google.common.collect.Multimap;
35 import java.util.Map;
36 import java.util.UUID;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mockito.Mock;
41 import org.mockito.junit.MockitoJUnitRunner;
42 import org.onap.vid.aai.AaiResponse;
43 import org.onap.vid.aai.AaiResponseTranslator.PortMirroringConfigData;
44 import org.onap.vid.aai.AaiResponseTranslator.PortMirroringConfigDataError;
45 import org.onap.vid.aai.AaiResponseTranslator.PortMirroringConfigDataOk;
46 import org.onap.vid.aai.model.AaiGetAicZone.AicZones;
47 import org.onap.vid.aai.model.AaiGetAicZone.Zone;
48 import org.onap.vid.aai.model.AaiGetPnfs.Pnf;
49 import org.onap.vid.aai.model.PortDetailsTranslator.PortDetails;
50 import org.onap.vid.aai.model.PortDetailsTranslator.PortDetailsError;
51 import org.onap.vid.aai.model.PortDetailsTranslator.PortDetailsOk;
52 import org.onap.vid.aai.util.AAIRestInterface;
53 import org.onap.vid.roles.RoleProvider;
54 import org.onap.vid.services.AaiService;
55 import org.onap.vid.utils.SystemPropertiesWrapper;
56 import org.springframework.http.HttpStatus;
57 import org.springframework.http.MediaType;
58 import org.springframework.test.web.servlet.MockMvc;
59 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
60
61 @RunWith(MockitoJUnitRunner.class)
62 public class AaiControllerTest {
63
64     private static final String ID_1 = "id1";
65     private static final String ID_2 = "id2";
66     private final ObjectMapper objectMapper = new ObjectMapper();
67     @Mock
68     private AaiService aaiService;
69     @Mock
70     private AAIRestInterface aaiRestInterface;
71     @Mock
72     private RoleProvider roleProvider;
73     @Mock
74     private SystemPropertiesWrapper systemPropertiesWrapper;
75     private MockMvc mockMvc;
76     private AaiController aaiController;
77
78     @Before
79     public void setUp() {
80         aaiController = new AaiController(aaiService, aaiRestInterface, roleProvider, systemPropertiesWrapper);
81         mockMvc = MockMvcBuilders.standaloneSetup(aaiController).build();
82     }
83
84     @Test
85     public void getPortMirroringConfigData_givenIds_shouldReturnConfigDataMappedById() throws Exception {
86         PortMirroringConfigDataOk okConfigData = new PortMirroringConfigDataOk("foo");
87         PortMirroringConfigDataError errorConfigData = new PortMirroringConfigDataError("bar", "{ baz: qux }");
88         Map<String, PortMirroringConfigData> expectedJson = ImmutableMap.of(
89             ID_1, okConfigData,
90             ID_2, errorConfigData);
91         given(aaiService.getPortMirroringConfigData(ID_1)).willReturn(okConfigData);
92         given(aaiService.getPortMirroringConfigData(ID_2)).willReturn(errorConfigData);
93
94         mockMvc
95             .perform(get("/aai_getPortMirroringConfigsData")
96                 .param("configurationIds", ID_1, ID_2)
97                 .contentType(MediaType.APPLICATION_JSON)
98                 .accept(MediaType.APPLICATION_JSON))
99             .andExpect(status().isOk())
100             .andExpect(content().json(objectMapper.writeValueAsString(expectedJson)));
101     }
102
103     @Test
104     public void getPortMirroringSourcePorts_givenIds_shouldReturnPortDetailsMappedById() throws Exception {
105         PortDetailsOk portDetailsOk = new PortDetailsOk("foo", "testInterface", true);
106         PortDetailsError portDetailsError = new PortDetailsError("bar", "{ baz: qux }");
107         Multimap<String, PortDetails> expectedJson = ImmutableMultimap.of(
108             ID_1, portDetailsOk,
109             ID_2, portDetailsError);
110         given(aaiService.getPortMirroringSourcePorts(ID_1)).willReturn(Lists.newArrayList(portDetailsOk));
111         given(aaiService.getPortMirroringSourcePorts(ID_2)).willReturn(Lists.newArrayList(portDetailsError));
112
113         mockMvc
114             .perform(get("/aai_getPortMirroringSourcePorts")
115                 .param("configurationIds", ID_1, ID_2)
116                 .contentType(MediaType.APPLICATION_JSON)
117                 .accept(MediaType.APPLICATION_JSON))
118             .andExpect(status().isOk())
119             .andExpect(content().json(objectMapper.writeValueAsString(expectedJson.asMap())));
120     }
121
122     @Test
123     public void getNodeTemplateInstances_givenParams_shouldReturnCorrectResponse() throws Exception {
124         String globalCustomerId = "testCustomerId";
125         String serviceType = "testServiceType";
126         String modelVersionId = UUID.nameUUIDFromBytes("modelVersionId".getBytes()).toString();
127         String modelInvariantId = UUID.nameUUIDFromBytes("modelInvariantId".getBytes()).toString();
128         String cloudRegion = "testRegion";
129         String urlTemplate = "/aai_get_vnf_instances/{globalCustomerId}/{serviceType}/{modelVersionId}/{modelInvariantId}/{cloudRegion}";
130         String expectedResponseBody = "myResponse";
131         AaiResponse<String> aaiResponse = new AaiResponse<>(expectedResponseBody, "", HttpStatus.OK.value());
132         given(aaiService
133             .getNodeTemplateInstances(globalCustomerId, serviceType, modelVersionId, modelInvariantId, cloudRegion))
134             .willReturn(aaiResponse);
135
136         mockMvc
137             .perform(get(urlTemplate, globalCustomerId, serviceType, modelVersionId, modelInvariantId, cloudRegion)
138                 .contentType(MediaType.APPLICATION_JSON)
139                 .accept(MediaType.APPLICATION_JSON))
140             .andExpect(status().isOk())
141             .andExpect(content().string(expectedResponseBody));
142     }
143
144     @Test
145     public void getAicZones_shouldReturnAaiZones_whenAaiHttpStatusIsOK() throws Exception {
146         AicZones aicZones = new AicZones(ImmutableList.of(new Zone("TEST_ZONE_ID", "TEST_ZONE_NAME")));
147         given(aaiService.getAaiZones()).willReturn(new AaiResponse(aicZones, "", HttpStatus.OK.value()));
148
149         mockMvc.perform(get("/aai_get_aic_zones")
150             .contentType(MediaType.APPLICATION_JSON)
151             .accept(MediaType.APPLICATION_JSON))
152             .andExpect(status().isOk())
153             .andExpect(content().json(objectMapper.writeValueAsString(aicZones)));
154     }
155
156     @Test
157     public void getAicZones_shouldReturnErrorResponse_whenAaiHttpStatusOtherThanOK() throws Exception {
158         String expectedErrorMessage = "Calling AAI Failed";
159         given(aaiService.getAaiZones())
160             .willReturn(new AaiResponse(null, expectedErrorMessage, HttpStatus.INTERNAL_SERVER_ERROR.value()));
161
162         mockMvc.perform(get("/aai_get_aic_zones")
163             .contentType(MediaType.APPLICATION_JSON)
164             .accept(MediaType.APPLICATION_JSON))
165             .andExpect(status().isInternalServerError())
166             .andExpect(content().string(expectedErrorMessage));
167     }
168
169     @Test
170     public void getSpecificPnf_shouldReturnPnfObjectForPnfId() throws Exception {
171         String pnfId = "MyPnfId";
172         Pnf pnf = createPnf(pnfId);
173         AaiResponse<Pnf> aaiResponse = new AaiResponse<>(pnf, "", HttpStatus.OK.value());
174         given(aaiService.getSpecificPnf(pnfId)).willReturn(aaiResponse);
175
176         mockMvc.perform(get("/aai_get_pnfs/pnf/{pnf_id}", pnfId)
177             .contentType(MediaType.APPLICATION_JSON)
178             .accept(MediaType.APPLICATION_JSON))
179             .andExpect(status().isOk())
180             .andExpect(content().json(objectMapper.writeValueAsString(pnf)));
181     }
182
183     @Test
184     public void getSpecificPnf_shouldHandleAAIServiceException() throws Exception {
185         String pnfId = "MyPnfId";
186         String expectedErrorMessage = "AAI Service Error";
187         given(aaiService.getSpecificPnf(pnfId)).willThrow(new RuntimeException(expectedErrorMessage));
188
189         mockMvc.perform(get("/aai_get_pnfs/pnf/{pnf_id}", pnfId)
190             .contentType(MediaType.APPLICATION_JSON)
191             .accept(MediaType.APPLICATION_JSON))
192             .andExpect(status().isInternalServerError())
193             .andExpect(content().string(expectedErrorMessage));
194     }
195
196     private Pnf createPnf(String pnfId) {
197         Pnf pnf = new Pnf();
198         pnf.setPnfId(pnfId);
199         pnf.setPnfName("TestPnf");
200         return pnf;
201     }
202
203     public void getPNFInstances_shouldReturnOKResponseFromAAIService() throws Exception {
204         String globalCustomerId = "testCustomerId";
205         String serviceType = "testServiceType";
206         String modelVersionId = UUID.nameUUIDFromBytes("modelVersionId".getBytes()).toString();
207         String modelInvariantId = UUID.nameUUIDFromBytes("modelInvariantId".getBytes()).toString();
208         String cloudRegion = "testRegion";
209         String equipVendor = "testVendor";
210         String equipModel = "model123";
211         String urlTemplate = "/aai_get_pnf_instances/{globalCustomerId}/{serviceType}/{modelVersionId}/{modelInvariantId}/{cloudRegion}/{equipVendor}/{equipModel}";
212         String expectedResponseBody = "myResponse";
213         AaiResponse<String> aaiResponse = new AaiResponse<>(expectedResponseBody, "", HttpStatus.OK.value());
214
215         given(aaiService
216             .getPNFData(globalCustomerId, serviceType, modelVersionId, modelInvariantId, cloudRegion, equipVendor,
217                 equipModel)).willReturn(aaiResponse);
218
219         mockMvc.perform(
220             get(urlTemplate, globalCustomerId, serviceType, modelVersionId,
221                 modelInvariantId, cloudRegion, equipVendor, equipModel)
222                 .contentType(MediaType.APPLICATION_JSON)
223                 .accept(MediaType.APPLICATION_JSON))
224             .andExpect(status().isOk())
225             .andExpect(content().string(expectedResponseBody));
226     }
227 }
228