Merge "aaiSubscriberController Test"
[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.ImmutableMap;
31 import com.google.common.collect.ImmutableMultimap;
32 import com.google.common.collect.Lists;
33 import com.google.common.collect.Multimap;
34 import java.util.Map;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.Mock;
39 import org.mockito.junit.MockitoJUnitRunner;
40 import org.onap.vid.aai.AaiResponseTranslator.PortMirroringConfigData;
41 import org.onap.vid.aai.AaiResponseTranslator.PortMirroringConfigDataError;
42 import org.onap.vid.aai.AaiResponseTranslator.PortMirroringConfigDataOk;
43 import org.onap.vid.aai.model.PortDetailsTranslator.PortDetails;
44 import org.onap.vid.aai.model.PortDetailsTranslator.PortDetailsError;
45 import org.onap.vid.aai.model.PortDetailsTranslator.PortDetailsOk;
46 import org.onap.vid.aai.util.AAIRestInterface;
47 import org.onap.vid.roles.RoleProvider;
48 import org.onap.vid.services.AaiService;
49 import org.onap.vid.utils.SystemPropertiesWrapper;
50 import org.springframework.http.MediaType;
51 import org.springframework.test.web.servlet.MockMvc;
52 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
53
54 @RunWith(MockitoJUnitRunner.class)
55 public class AaiControllerTest {
56
57     private static final String ID_1 = "id1";
58     private static final String ID_2 = "id2";
59     @Mock
60     private AaiService aaiService;
61     @Mock
62     private AAIRestInterface aaiRestInterface;
63     @Mock
64     private RoleProvider roleProvider;
65     @Mock
66     private SystemPropertiesWrapper systemPropertiesWrapper;
67
68     private MockMvc mockMvc;
69     private AaiController aaiController;
70
71     @Before
72     public void setUp() {
73         aaiController = new AaiController(aaiService, aaiRestInterface, roleProvider, systemPropertiesWrapper);
74         mockMvc = MockMvcBuilders.standaloneSetup(aaiController).build();
75     }
76
77     @Test
78     public void getPortMirroringConfigData_givenIds_shouldReturnConfigDataMappedById() throws Exception {
79         PortMirroringConfigDataOk okConfigData = new PortMirroringConfigDataOk("foo");
80         PortMirroringConfigDataError errorConfigData = new PortMirroringConfigDataError("bar", "{ baz: qux }");
81         Map<String, PortMirroringConfigData> expectedJson = ImmutableMap.of(
82             ID_1, okConfigData,
83             ID_2, errorConfigData);
84         given(aaiService.getPortMirroringConfigData(ID_1)).willReturn(okConfigData);
85         given(aaiService.getPortMirroringConfigData(ID_2)).willReturn(errorConfigData);
86
87         mockMvc
88             .perform(get("/aai_getPortMirroringConfigsData")
89                 .param("configurationIds", ID_1, ID_2)
90                 .contentType(MediaType.APPLICATION_JSON)
91                 .accept(MediaType.APPLICATION_JSON))
92             .andExpect(status().isOk())
93             .andExpect(content().json(new ObjectMapper().writeValueAsString(expectedJson)));
94     }
95
96     @Test
97     public void getPortMirroringSourcePorts_givenIds_shouldReturnPortDetailsMappedById() throws Exception {
98         PortDetailsOk portDetailsOk = new PortDetailsOk("foo", "testInterface", true);
99         PortDetailsError portDetailsError = new PortDetailsError("bar", "{ baz: qux }");
100         Multimap<String, PortDetails> expectedJson = ImmutableMultimap.of(
101             ID_1, portDetailsOk,
102             ID_2, portDetailsError);
103         given(aaiService.getPortMirroringSourcePorts(ID_1)).willReturn(Lists.newArrayList(portDetailsOk));
104         given(aaiService.getPortMirroringSourcePorts(ID_2)).willReturn(Lists.newArrayList(portDetailsError));
105
106         mockMvc
107             .perform(get("/aai_getPortMirroringSourcePorts")
108                 .param("configurationIds", ID_1, ID_2)
109                 .contentType(MediaType.APPLICATION_JSON)
110                 .accept(MediaType.APPLICATION_JSON))
111             .andExpect(status().isOk())
112             .andExpect(content().json(new ObjectMapper().writeValueAsString(expectedJson.asMap())));
113     }
114 }