Increment code coverage for Application
[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     public void testGetAllAdapters() throws Exception {
86         when(deployer.getAll()).thenReturn(Collections.singletonList("mockResult"));
87
88         mockMvc.perform(get("/adapters/").secure(true).contentType(MediaType.APPLICATION_JSON)).andDo(print())
89                 .andExpect(status().isOk()).andExpect(content().string(containsString("mockResult")));
90     }
91
92     @Test
93     @WithMockUser(username = "admin", roles = "ADMIN")
94     public 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());
97     }
98
99     @Test
100     @WithMockUser(username = "admin", roles = "ADMIN")
101     public void testNotFound() throws Exception {
102         doThrow(NotFoundException.class).when(deployer).delete(anyString());
103
104         mockMvc.perform(delete("/adapters/adapter/172.10.55.3").secure(true).contentType(MediaType.APPLICATION_JSON))
105                 .andDo(print()).andExpect(status().isNotFound());
106     }
107
108     @Test
109     @WithMockUser(username = "admin", roles = "ADMIN")
110     public void testAddAdapter() throws Exception {
111
112         final Adapter adapter = new Adapter();
113         adapter.setHost("172.10.55.3");
114
115         final AdapterMechId mechId = new AdapterMechId();
116         mechId.username("admin");
117         mechId.password("somePass");
118         adapter.setMechId(mechId);
119
120         mockMvc.perform(post("/adapters/adapter").secure(true).contentType(MediaType.APPLICATION_JSON)
121                                 .content(GSON.toJson(adapter))).andDo(print()).andExpect(status().isOk());
122     }
123
124     @Test
125     @WithMockUser(username = "admin", roles = "ADMIN")
126     public void testAlreadyExist() throws Exception {
127
128         final Adapter adapter = new Adapter();
129         adapter.setHost("172.10.55.3");
130
131         final AdapterMechId mechId = new AdapterMechId();
132         mechId.username("admin");
133         mechId.password("somePass");
134         adapter.setMechId(mechId);
135
136         doThrow(AlreadyPresentException.class).when(deployer).create(anyString(), anyString(), anyString());
137
138         mockMvc.perform(post("/adapters/adapter").secure(true).contentType(MediaType.APPLICATION_JSON)
139                                 .content(GSON.toJson(adapter))).andDo(print()).andExpect(status().isBadRequest());
140     }
141
142     @Test
143     @WithMockUser(username = "admin", roles = "ADMIN")
144     public void testMissingArguments() throws Exception {
145
146         final Adapter adapter = new Adapter();
147         adapter.setHost("172.10.55.3");
148
149         final AdapterMechId mechId = new AdapterMechId();
150         mechId.username("admin");
151         adapter.setMechId(mechId);
152
153
154         mockMvc.perform(post("/adapters/adapter").secure(true).contentType(MediaType.APPLICATION_JSON)
155                                 .content(GSON.toJson(adapter))).andDo(print()).andExpect(status().isBadRequest());
156     }
157
158     @Test
159     public void test() {
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"));
163     }
164 }