Fix Sonar complains
[oam/nf-oam-adopter.git] / ves-nf-oam-adopter / ves-nf-oam-adopter-app / src / test / java / org / o / ran / oam / nf / oam / adopter / app / AdapterApplicationTest.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  O-RAN-SC
4  *  ================================================================================
5  *  Copyright © 2021 AT&T Intellectual Property. All rights reserved.
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  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.o.ran.oam.nf.oam.adopter.app;
21
22 import static org.hamcrest.Matchers.containsString;
23 import static org.junit.jupiter.api.Assertions.assertEquals;
24 import static org.mockito.ArgumentMatchers.anyString;
25 import static org.mockito.Mockito.doThrow;
26 import static org.mockito.Mockito.when;
27 import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
28 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
29 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
30 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
31 import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
32 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
33 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
34
35 import com.google.gson.Gson;
36 import java.time.ZoneId;
37 import java.util.Collections;
38 import org.junit.jupiter.api.BeforeEach;
39 import org.junit.jupiter.api.Test;
40 import org.o.ran.oam.nf.oam.adopter.app.controller.TimeZoneServiceProvider;
41 import org.o.ran.oam.nf.oam.adopter.model.Adapter;
42 import org.o.ran.oam.nf.oam.adopter.model.AdapterMechId;
43 import org.o.ran.oam.nf.oam.adopter.pm.rest.manager.PerformanceManagementMapperConfigProvider;
44 import org.o.ran.oam.nf.oam.adopter.pm.rest.manager.api.PerformanceManagementAdaptersDeployer;
45 import org.o.ran.oam.nf.oam.adopter.pm.rest.manager.exceptions.AlreadyPresentException;
46 import org.o.ran.oam.nf.oam.adopter.pm.rest.manager.exceptions.NotFoundException;
47 import org.o.ran.oam.nf.oam.adopter.snmp.manager.SnmpMappingConfigurationProvider;
48 import org.springframework.beans.factory.annotation.Autowired;
49 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
50 import org.springframework.boot.test.context.SpringBootTest;
51 import org.springframework.boot.test.mock.mockito.MockBean;
52 import org.springframework.http.MediaType;
53 import org.springframework.security.test.context.support.WithMockUser;
54 import org.springframework.test.web.servlet.MockMvc;
55 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
56 import org.springframework.web.context.WebApplicationContext;
57
58 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
59 @AutoConfigureMockMvc
60 class AdapterApplicationTest {
61
62     private static final Gson GSON = new Gson();
63
64     @Autowired
65     private MockMvc mockMvc;
66     @Autowired
67     private TimeZoneServiceProvider timeZoneServiceProvider;
68     @MockBean
69     private PerformanceManagementAdaptersDeployer deployer;
70     @MockBean
71     private SnmpMappingConfigurationProvider snmpProvider;
72     @MockBean
73     private PerformanceManagementMapperConfigProvider pmProvider;
74     @Autowired
75     private WebApplicationContext context;
76
77
78     @BeforeEach
79     public void applySecurity() {
80         mockMvc = MockMvcBuilders.webAppContextSetup(context).apply(springSecurity()).build();
81     }
82
83     @Test
84     @WithMockUser(username = "admin", roles = "ADMIN")
85     void testGetAllAdapters() throws Exception {
86         when(deployer.getAll()).thenReturn(Collections.singletonList("mockResult"));
87
88         mockMvc.perform(get("/adapters/").secure(true)
89                                 .contentType(MediaType.APPLICATION_JSON))
90                 .andDo(print())
91                 .andExpect(status().isOk()).andExpect(content().string(containsString("mockResult")));
92     }
93
94     @Test
95     @WithMockUser(username = "admin", roles = "ADMIN")
96     void testDeleteAdapter() throws Exception {
97         mockMvc.perform(delete("/adapters/adapter/172.10.55.3").secure(true)
98                                 .contentType(MediaType.APPLICATION_JSON))
99                 .andDo(print()).andExpect(status().isOk());
100     }
101
102     @Test
103     @WithMockUser(username = "admin", roles = "ADMIN")
104     void testNotFound() throws Exception {
105         doThrow(new NotFoundException("172.10.55.3"))
106                 .when(deployer)
107                 .delete(anyString());
108
109         mockMvc.perform(delete("/adapters/adapter/172.10.55.3").secure(true)
110                                 .contentType(MediaType.APPLICATION_JSON))
111                 .andDo(print())
112                 .andExpect(status().isNotFound())
113                 .andExpect(content().string(containsString("Adapter 172.10.55.3 is not present.")));
114     }
115
116     @Test
117     @WithMockUser(username = "admin", roles = "ADMIN")
118     void testAddAdapter() throws Exception {
119
120         final Adapter adapter = new Adapter();
121         adapter.setHost("172.10.55.3");
122
123         final AdapterMechId mechId = new AdapterMechId();
124         mechId.username("admin");
125         mechId.password("somePass");
126         adapter.setMechId(mechId);
127
128         mockMvc.perform(post("/adapters/adapter").secure(true)
129                                 .contentType(MediaType.APPLICATION_JSON)
130                                 .content(GSON.toJson(adapter)))
131                 .andDo(print())
132                 .andExpect(status().isOk());
133     }
134
135     @Test
136     @WithMockUser(username = "admin", roles = "ADMIN")
137     void testAlreadyExist() throws Exception {
138         final Adapter adapter = new Adapter();
139         adapter.setHost("172.10.55.3");
140
141         final AdapterMechId mechId = new AdapterMechId();
142         mechId.username("admin");
143         mechId.password("somePass");
144         adapter.setMechId(mechId);
145
146         doThrow(new AlreadyPresentException("172.10.55.3"))
147                 .when(deployer).create(anyString(), anyString(), anyString());
148
149         mockMvc.perform(post("/adapters/adapter")
150                                 .secure(true).contentType(MediaType.APPLICATION_JSON)
151                                 .content(GSON.toJson(adapter)))
152                 .andDo(print())
153                 .andExpect(status().isBadRequest())
154                 .andExpect(content().string(containsString("Adapter 172.10.55.3 already present.")));
155     }
156
157     @Test
158     @WithMockUser(username = "admin", roles = "ADMIN")
159     void testMissingArguments() throws Exception {
160         final Adapter adapter = new Adapter();
161         adapter.setHost("172.10.55.3");
162
163         final AdapterMechId mechId = new AdapterMechId();
164         mechId.username("admin");
165         adapter.setMechId(mechId);
166
167         mockMvc.perform(post("/adapters/adapter").secure(true)
168                                 .contentType(MediaType.APPLICATION_JSON)
169                                 .content(GSON.toJson(adapter)))
170                 .andDo(print())
171                 .andExpect(status().isBadRequest());
172     }
173
174     @Test
175     void testUnauthorized() throws Exception {
176         mockMvc.perform(post("/adapters/adapter").secure(true))
177                 .andDo(print())
178                 .andExpect(status().isUnauthorized());
179
180         mockMvc.perform(post("/adapters/adapter").secure(true))
181             .andDo(print())
182             .andExpect(status().isUnauthorized());
183
184         mockMvc.perform(delete("/adapters/adapter/172.10.55.3").secure(true))
185                 .andDo(print())
186                 .andExpect(status().isUnauthorized());
187
188     }
189
190     @Test
191     void test() {
192         final ZoneId zoneId = ZoneId.of("+02:00");
193         when(deployer.getTimeZone("172.10.55.3")).thenReturn(zoneId);
194         assertEquals(zoneId, timeZoneServiceProvider.getTimeZone("172.10.55.3"));
195     }
196 }