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).contentType(MediaType.APPLICATION_JSON)).andDo(print())
89 .andExpect(status().isOk()).andExpect(content().string(containsString("mockResult")));
93 @WithMockUser(username = "admin", roles = "ADMIN")
94 void testDeleteAdapter() throws Exception {
95 mockMvc.perform(delete("/adapters/adapter/172.10.55.3").secure(true).contentType(MediaType.APPLICATION_JSON))
96 .andDo(print()).andExpect(status().isOk());
100 @WithMockUser(username = "admin", roles = "ADMIN")
101 void testNotFound() throws Exception {
102 doThrow(NotFoundException.class).when(deployer).delete(anyString());
104 mockMvc.perform(delete("/adapters/adapter/172.10.55.3").secure(true).contentType(MediaType.APPLICATION_JSON))
105 .andDo(print()).andExpect(status().isNotFound());
109 @WithMockUser(username = "admin", roles = "ADMIN")
110 void testAddAdapter() throws Exception {
112 final Adapter adapter = new Adapter();
113 adapter.setHost("172.10.55.3");
115 final AdapterMechId mechId = new AdapterMechId();
116 mechId.username("admin");
117 mechId.password("somePass");
118 adapter.setMechId(mechId);
120 mockMvc.perform(post("/adapters/adapter").secure(true).contentType(MediaType.APPLICATION_JSON)
121 .content(GSON.toJson(adapter))).andDo(print()).andExpect(status().isOk());
125 @WithMockUser(username = "admin", roles = "ADMIN")
126 void testAlreadyExist() throws Exception {
128 final Adapter adapter = new Adapter();
129 adapter.setHost("172.10.55.3");
131 final AdapterMechId mechId = new AdapterMechId();
132 mechId.username("admin");
133 mechId.password("somePass");
134 adapter.setMechId(mechId);
136 doThrow(AlreadyPresentException.class).when(deployer).create(anyString(), anyString(), anyString());
138 mockMvc.perform(post("/adapters/adapter").secure(true).contentType(MediaType.APPLICATION_JSON)
139 .content(GSON.toJson(adapter))).andDo(print()).andExpect(status().isBadRequest());
143 @WithMockUser(username = "admin", roles = "ADMIN")
144 void testMissingArguments() throws Exception {
146 final Adapter adapter = new Adapter();
147 adapter.setHost("172.10.55.3");
149 final AdapterMechId mechId = new AdapterMechId();
150 mechId.username("admin");
151 adapter.setMechId(mechId);
154 mockMvc.perform(post("/adapters/adapter").secure(true).contentType(MediaType.APPLICATION_JSON)
155 .content(GSON.toJson(adapter))).andDo(print()).andExpect(status().isBadRequest());
160 final ZoneId zoneId = ZoneId.of("+02:00");
161 when(deployer.getTimeZone("172.10.55.3")).thenReturn(zoneId);
162 assertEquals(zoneId, timeZoneServiceProvider.getTimeZone("172.10.55.3"));