aboutsummaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/test/java/org/onap/vid/controller/AaiControllerTest.java
blob: 30110139997875e1fd7b20177bb56b508851b047 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*-
 * ============LICENSE_START=======================================================
 * VID
 * ================================================================================
 * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
 * Modifications Copyright (C) 2019 Nokia.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.vid.controller;

import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import java.util.Map;
import java.util.UUID;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.onap.vid.aai.AaiResponse;
import org.onap.vid.aai.AaiResponseTranslator.PortMirroringConfigData;
import org.onap.vid.aai.AaiResponseTranslator.PortMirroringConfigDataError;
import org.onap.vid.aai.AaiResponseTranslator.PortMirroringConfigDataOk;
import org.onap.vid.aai.model.AaiGetAicZone.AicZones;
import org.onap.vid.aai.model.AaiGetAicZone.Zone;
import org.onap.vid.aai.model.AaiGetPnfs.Pnf;
import org.onap.vid.aai.model.PortDetailsTranslator.PortDetails;
import org.onap.vid.aai.model.PortDetailsTranslator.PortDetailsError;
import org.onap.vid.aai.model.PortDetailsTranslator.PortDetailsOk;
import org.onap.vid.aai.util.AAIRestInterface;
import org.onap.vid.roles.RoleProvider;
import org.onap.vid.services.AaiService;
import org.onap.vid.utils.SystemPropertiesWrapper;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

@RunWith(MockitoJUnitRunner.class)
public class AaiControllerTest {

    private static final String ID_1 = "id1";
    private static final String ID_2 = "id2";
    private final ObjectMapper objectMapper = new ObjectMapper();
    @Mock
    private AaiService aaiService;
    @Mock
    private AAIRestInterface aaiRestInterface;
    @Mock
    private RoleProvider roleProvider;
    @Mock
    private SystemPropertiesWrapper systemPropertiesWrapper;
    private MockMvc mockMvc;
    private AaiController aaiController;

    @Before
    public void setUp() {
        aaiController = new AaiController(aaiService, aaiRestInterface, roleProvider, systemPropertiesWrapper);
        mockMvc = MockMvcBuilders.standaloneSetup(aaiController).build();
    }

    @Test
    public void getPortMirroringConfigData_givenIds_shouldReturnConfigDataMappedById() throws Exception {
        PortMirroringConfigDataOk okConfigData = new PortMirroringConfigDataOk("foo");
        PortMirroringConfigDataError errorConfigData = new PortMirroringConfigDataError("bar", "{ baz: qux }");
        Map<String, PortMirroringConfigData> expectedJson = ImmutableMap.of(
            ID_1, okConfigData,
            ID_2, errorConfigData);
        given(aaiService.getPortMirroringConfigData(ID_1)).willReturn(okConfigData);
        given(aaiService.getPortMirroringConfigData(ID_2)).willReturn(errorConfigData);

        mockMvc
            .perform(get("/aai_getPortMirroringConfigsData")
                .param("configurationIds", ID_1, ID_2)
                .contentType(MediaType.APPLICATION_JSON)
                .accept(MediaType.APPLICATION_JSON))
            .andExpect(status().isOk())
            .andExpect(content().json(objectMapper.writeValueAsString(expectedJson)));
    }

    @Test
    public void getPortMirroringSourcePorts_givenIds_shouldReturnPortDetailsMappedById() throws Exception {
        PortDetailsOk portDetailsOk = new PortDetailsOk("foo", "testInterface", true);
        PortDetailsError portDetailsError = new PortDetailsError("bar", "{ baz: qux }");
        Multimap<String, PortDetails> expectedJson = ImmutableMultimap.of(
            ID_1, portDetailsOk,
            ID_2, portDetailsError);
        given(aaiService.getPortMirroringSourcePorts(ID_1)).willReturn(Lists.newArrayList(portDetailsOk));
        given(aaiService.getPortMirroringSourcePorts(ID_2)).willReturn(Lists.newArrayList(portDetailsError));

        mockMvc
            .perform(get("/aai_getPortMirroringSourcePorts")
                .param("configurationIds", ID_1, ID_2)
                .contentType(MediaType.APPLICATION_JSON)
                .accept(MediaType.APPLICATION_JSON))
            .andExpect(status().isOk())
            .andExpect(content().json(objectMapper.writeValueAsString(expectedJson.asMap())));
    }

    @Test
    public void getNodeTemplateInstances_givenParams_shouldReturnCorrectResponse() throws Exception {
        String globalCustomerId = "testCustomerId";
        String serviceType = "testServiceType";
        String modelVersionId = UUID.nameUUIDFromBytes("modelVersionId".getBytes()).toString();
        String modelInvariantId = UUID.nameUUIDFromBytes("modelInvariantId".getBytes()).toString();
        String cloudRegion = "testRegion";
        String urlTemplate = "/aai_get_vnf_instances/{globalCustomerId}/{serviceType}/{modelVersionId}/{modelInvariantId}/{cloudRegion}";
        String expectedResponseBody = "myResponse";
        AaiResponse<String> aaiResponse = new AaiResponse<>(expectedResponseBody, "", HttpStatus.OK.value());
        given(aaiService
            .getNodeTemplateInstances(globalCustomerId, serviceType, modelVersionId, modelInvariantId, cloudRegion))
            .willReturn(aaiResponse);

        mockMvc
            .perform(get(urlTemplate, globalCustomerId, serviceType, modelVersionId, modelInvariantId, cloudRegion)
                .contentType(MediaType.APPLICATION_JSON)
                .accept(MediaType.APPLICATION_JSON))
            .andExpect(status().isOk())
            .andExpect(content().string(expectedResponseBody));
    }

    @Test
    public void getAicZones_shouldReturnAaiZones_whenAaiHttpStatusIsOK() throws Exception {
        AicZones aicZones = new AicZones(ImmutableList.of(new Zone("TEST_ZONE_ID", "TEST_ZONE_NAME")));
        given(aaiService.getAaiZones()).willReturn(new AaiResponse(aicZones, "", HttpStatus.OK.value()));

        mockMvc.perform(get("/aai_get_aic_zones")
            .contentType(MediaType.APPLICATION_JSON)
            .accept(MediaType.APPLICATION_JSON))
            .andExpect(status().isOk())
            .andExpect(content().json(objectMapper.writeValueAsString(aicZones)));
    }

    @Test
    public void getAicZones_shouldReturnErrorResponse_whenAaiHttpStatusOtherThanOK() throws Exception {
        String expectedErrorMessage = "Calling AAI Failed";
        given(aaiService.getAaiZones())
            .willReturn(new AaiResponse(null, expectedErrorMessage, HttpStatus.INTERNAL_SERVER_ERROR.value()));

        mockMvc.perform(get("/aai_get_aic_zones")
            .contentType(MediaType.APPLICATION_JSON)
            .accept(MediaType.APPLICATION_JSON))
            .andExpect(status().isInternalServerError())
            .andExpect(content().string(expectedErrorMessage));
    }

    @Test
    public void getSpecificPnf_shouldReturnPnfObjectForPnfId() throws Exception {
        String pnfId = "MyPnfId";
        Pnf pnf = createPnf(pnfId);
        AaiResponse<Pnf> aaiResponse = new AaiResponse<>(pnf, "", HttpStatus.OK.value());
        given(aaiService.getSpecificPnf(pnfId)).willReturn(aaiResponse);

        mockMvc.perform(get("/aai_get_pnfs/pnf/{pnf_id}", pnfId)
            .contentType(MediaType.APPLICATION_JSON)
            .accept(MediaType.APPLICATION_JSON))
            .andExpect(status().isOk())
            .andExpect(content().json(objectMapper.writeValueAsString(pnf)));
    }

    @Test
    public void getSpecificPnf_shouldHandleAAIServiceException() throws Exception {
        String pnfId = "MyPnfId";
        String expectedErrorMessage = "AAI Service Error";
        given(aaiService.getSpecificPnf(pnfId)).willThrow(new RuntimeException(expectedErrorMessage));

        mockMvc.perform(get("/aai_get_pnfs/pnf/{pnf_id}", pnfId)
            .contentType(MediaType.APPLICATION_JSON)
            .accept(MediaType.APPLICATION_JSON))
            .andExpect(status().isInternalServerError())
            .andExpect(content().string(expectedErrorMessage));
    }

    private Pnf createPnf(String pnfId) {
        Pnf pnf = new Pnf();
        pnf.setPnfId(pnfId);
        pnf.setPnfName("TestPnf");
        return pnf;
    }

    public void getPNFInstances_shouldReturnOKResponseFromAAIService() throws Exception {
        String globalCustomerId = "testCustomerId";
        String serviceType = "testServiceType";
        String modelVersionId = UUID.nameUUIDFromBytes("modelVersionId".getBytes()).toString();
        String modelInvariantId = UUID.nameUUIDFromBytes("modelInvariantId".getBytes()).toString();
        String cloudRegion = "testRegion";
        String equipVendor = "testVendor";
        String equipModel = "model123";
        String urlTemplate = "/aai_get_pnf_instances/{globalCustomerId}/{serviceType}/{modelVersionId}/{modelInvariantId}/{cloudRegion}/{equipVendor}/{equipModel}";
        String expectedResponseBody = "myResponse";
        AaiResponse<String> aaiResponse = new AaiResponse<>(expectedResponseBody, "", HttpStatus.OK.value());

        given(aaiService
            .getPNFData(globalCustomerId, serviceType, modelVersionId, modelInvariantId, cloudRegion, equipVendor,
                equipModel)).willReturn(aaiResponse);

        mockMvc.perform(
            get(urlTemplate, globalCustomerId, serviceType, modelVersionId,
                modelInvariantId, cloudRegion, equipVendor, equipModel)
                .contentType(MediaType.APPLICATION_JSON)
                .accept(MediaType.APPLICATION_JSON))
            .andExpect(status().isOk())
            .andExpect(content().string(expectedResponseBody));
    }
}