2 * ============LICENSE_START=======================================================
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
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=========================================================
20 package org.o.ran.oam.nf.oam.adopter.app;
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;
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;
58 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
60 class AdapterApplicationTest {
62 private static final Gson GSON = new Gson();
65 private MockMvc mockMvc;
67 private TimeZoneServiceProvider timeZoneServiceProvider;
69 private PerformanceManagementAdaptersDeployer deployer;
71 private SnmpMappingConfigurationProvider snmpProvider;
73 private PerformanceManagementMapperConfigProvider pmProvider;
75 private WebApplicationContext context;
79 public void applySecurity() {
80 mockMvc = MockMvcBuilders.webAppContextSetup(context).apply(springSecurity()).build();
84 @WithMockUser(username = "admin", roles = "ADMIN")
85 void testGetAllAdapters() throws Exception {
86 when(deployer.getAll()).thenReturn(Collections.singletonList("mockResult"));
88 mockMvc.perform(get("/adapters/").secure(true)
89 .contentType(MediaType.APPLICATION_JSON))
91 .andExpect(status().isOk()).andExpect(content().string(containsString("mockResult")));
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());
103 @WithMockUser(username = "admin", roles = "ADMIN")
104 void testNotFound() throws Exception {
105 doThrow(new NotFoundException("172.10.55.3"))
107 .delete(anyString());
109 mockMvc.perform(delete("/adapters/adapter/172.10.55.3").secure(true)
110 .contentType(MediaType.APPLICATION_JSON))
112 .andExpect(status().isNotFound())
113 .andExpect(content().string(containsString("Adapter 172.10.55.3 is not present.")));
117 @WithMockUser(username = "admin", roles = "ADMIN")
118 void testAddAdapter() throws Exception {
120 final Adapter adapter = new Adapter();
121 adapter.setHost("172.10.55.3");
123 final AdapterMechId mechId = new AdapterMechId();
124 mechId.username("admin");
125 mechId.password("somePass");
126 adapter.setMechId(mechId);
128 mockMvc.perform(post("/adapters/adapter").secure(true)
129 .contentType(MediaType.APPLICATION_JSON)
130 .content(GSON.toJson(adapter)))
132 .andExpect(status().isOk());
136 @WithMockUser(username = "admin", roles = "ADMIN")
137 void testAlreadyExist() throws Exception {
138 final Adapter adapter = new Adapter();
139 adapter.setHost("172.10.55.3");
141 final AdapterMechId mechId = new AdapterMechId();
142 mechId.username("admin");
143 mechId.password("somePass");
144 adapter.setMechId(mechId);
146 doThrow(new AlreadyPresentException("172.10.55.3"))
147 .when(deployer).create(anyString(), anyString(), anyString());
149 mockMvc.perform(post("/adapters/adapter")
150 .secure(true).contentType(MediaType.APPLICATION_JSON)
151 .content(GSON.toJson(adapter)))
153 .andExpect(status().isBadRequest())
154 .andExpect(content().string(containsString("Adapter 172.10.55.3 already present.")));
158 @WithMockUser(username = "admin", roles = "ADMIN")
159 void testMissingArguments() throws Exception {
160 final Adapter adapter = new Adapter();
161 adapter.setHost("172.10.55.3");
163 final AdapterMechId mechId = new AdapterMechId();
164 mechId.username("admin");
165 adapter.setMechId(mechId);
167 mockMvc.perform(post("/adapters/adapter").secure(true)
168 .contentType(MediaType.APPLICATION_JSON)
169 .content(GSON.toJson(adapter)))
171 .andExpect(status().isBadRequest());
175 void testUnauthorized() throws Exception {
176 mockMvc.perform(post("/adapters/adapter").secure(true))
178 .andExpect(status().isUnauthorized());
180 mockMvc.perform(post("/adapters/adapter").secure(true))
182 .andExpect(status().isUnauthorized());
184 mockMvc.perform(delete("/adapters/adapter/172.10.55.3").secure(true))
186 .andExpect(status().isUnauthorized());
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"));