3 * * ============LICENSE_START=======================================================
5 * * ================================================================================
6 * * Copyright (c) 2020 - 2021 AT&T Intellectual Property. All rights reserved.
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
12 * * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.dcaegen2.platform.mod.web;
25 import static org.mockito.Mockito.any;
26 import static org.mockito.Mockito.doNothing;
27 import static org.mockito.Mockito.times;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
30 import static org.onap.dcaegen2.platform.mod.objectmothers.AuthObjectMother.asJsonString;
31 import static org.onap.dcaegen2.platform.mod.objectmothers.AuthObjectMother.getModUser;
32 import static org.onap.dcaegen2.platform.mod.objectmothers.UserObjectMother.getUpdateUserRequest;
33 import static org.onap.dcaegen2.platform.mod.objectmothers.UserObjectMother.getUsers;
34 import static org.onap.dcaegen2.platform.mod.objectmothers.UserObjectMother.userId;
35 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
36 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
37 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
38 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
40 import org.junit.Assert;
41 import org.junit.jupiter.api.BeforeEach;
42 import org.junit.jupiter.api.Test;
43 import org.mockito.Mock;
44 import org.onap.dcaegen2.platform.mod.controllers.UserController;
45 import org.onap.dcaegen2.platform.mod.models.ModUser;
46 import org.onap.dcaegen2.platform.mod.models.UpdateUserRequest;
47 import org.onap.dcaegen2.platform.mod.repositories.RoleRepository;
48 import org.onap.dcaegen2.platform.mod.repositories.UserRepository;
49 import org.onap.dcaegen2.platform.mod.security.jwt.AuthEntryPointJwt;
50 import org.onap.dcaegen2.platform.mod.security.jwt.JwtUtils;
51 import org.onap.dcaegen2.platform.mod.security.services.UserDetailsImpl;
52 import org.onap.dcaegen2.platform.mod.security.services.UserDetailsServiceImpl;
53 import org.onap.dcaegen2.platform.mod.services.MODUserDetailService;
54 import org.springframework.beans.factory.annotation.Autowired;
55 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
56 import org.springframework.boot.test.mock.mockito.MockBean;
57 import org.springframework.http.MediaType;
58 import org.springframework.security.core.Authentication;
59 import org.springframework.security.test.context.support.WithMockUser;
60 import org.springframework.test.web.servlet.MockMvc;
61 import org.springframework.test.web.servlet.MvcResult;
66 * Mock Test cases for UserController
68 @WebMvcTest(UserController.class)
69 public class UserControllerTest {
73 private MockMvc mockMvc;
76 UserDetailsServiceImpl userDetailsService;
79 UserRepository userRepository;
82 MODUserDetailService modUserDetailService;
85 RoleRepository roleRepository;
88 AuthEntryPointJwt authEntryPointJwt;
94 Authentication authentication;
101 @WithMockUser(roles="ADMIN")
103 void test_getUsername() throws Exception {
105 when(userDetailsService.loadUserByUsername(userId)).thenReturn(UserDetailsImpl.build(new ModUser()));
107 MvcResult result = mockMvc.perform(get("/api/users/" + userId)
108 .contentType(MediaType.APPLICATION_JSON))
109 .andExpect(status().isOk()).andReturn();
111 Assert.assertNotNull(result.getResponse().getContentAsString());
112 verify(userDetailsService, times(1)).loadUserByUsername(userId);
115 @WithMockUser(roles="ADMIN")
117 void test_getAllUsers() throws Exception {
119 when(modUserDetailService.findAll()).thenReturn(getUsers());
121 MvcResult result = mockMvc.perform(get("/api/users/getAll")
122 .contentType(MediaType.APPLICATION_JSON))
123 .andExpect(status().isOk()).andReturn();
125 Assert.assertNotNull(result.getResponse().getContentAsString());
126 verify(modUserDetailService, times(1)).findAll();
130 @WithMockUser(roles="ADMIN")
132 void test_deleteUser() throws Exception {
134 doNothing().when(modUserDetailService).deleteUserByUsername(any(String.class));
136 MvcResult result = mockMvc.perform(delete("/api/users/" + userId)
137 .contentType(MediaType.APPLICATION_JSON))
138 .andExpect(status().isOk()).andReturn();
140 Assert.assertNotNull(result.getResponse().getContentAsString());
141 verify(modUserDetailService, times(1)).deleteUserByUsername(any(String.class));
145 @WithMockUser(roles="ADMIN")
147 void test_userUpdateOwnProfile_returnsSuccessResponse() throws Exception {
149 UpdateUserRequest updateUserRequest = getUpdateUserRequest();
151 when(userDetailsService.adminUpdateUser(userId,updateUserRequest,"token")).thenReturn(getModUser());
153 mockMvc.perform(patch("/api/users/admin/" + userId)
154 .header("Authorization", "token")
155 .contentType(MediaType.APPLICATION_JSON)
156 .content(asJsonString(updateUserRequest)).accept(MediaType.APPLICATION_JSON))
157 //.andExpect(jsonPath("$.message", notNullValue()))
158 .andExpect(status().isOk()).andReturn();
160 verify(userDetailsService, times(1)).adminUpdateUser(userId,updateUserRequest,"token");