3 * * ============LICENSE_START=======================================================
5 * * ================================================================================
6 * * Copyright (c) 2020 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 org.apache.tools.ant.taskdefs.optional.extension.Specification;
26 import org.junit.Assert;
27 import org.junit.jupiter.api.BeforeEach;
28 import org.junit.jupiter.api.Test;
29 import org.junit.runner.Request;
30 import org.mockito.Mock;
31 import org.mockito.Mockito;
32 import org.onap.dcaegen2.platform.mod.controllers.RoleController;
33 import org.onap.dcaegen2.platform.mod.controllers.UserController;
34 import org.onap.dcaegen2.platform.mod.models.LoginRequest;
35 import org.onap.dcaegen2.platform.mod.models.ModUser;
36 import org.onap.dcaegen2.platform.mod.models.UpdateUserRequest;
37 import org.onap.dcaegen2.platform.mod.repositories.RoleRepository;
38 import org.onap.dcaegen2.platform.mod.repositories.UserRepository;
39 import org.onap.dcaegen2.platform.mod.security.jwt.AuthEntryPointJwt;
40 import org.onap.dcaegen2.platform.mod.security.jwt.JwtUtils;
41 import org.onap.dcaegen2.platform.mod.security.services.UserDetailsServiceImpl;
42 import org.onap.dcaegen2.platform.mod.services.MODUserDetailService;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
45 import org.springframework.boot.test.mock.mockito.MockBean;
46 import org.springframework.http.MediaType;
47 import org.springframework.security.core.Authentication;
48 import org.springframework.security.test.context.support.WithMockUser;
49 import org.springframework.test.web.client.RequestMatcher;
50 import org.springframework.test.web.servlet.MockMvc;
51 import org.springframework.test.web.servlet.MvcResult;
52 import org.springframework.test.web.servlet.ResultMatcher;
54 import java.util.Optional;
56 import static org.hamcrest.Matchers.notNullValue;
57 import static org.mockito.ArgumentMatchers.anyString;
58 import static org.mockito.Mockito.*;
59 import static org.onap.dcaegen2.platform.mod.objectmothers.AuthObjectMother.asJsonString;
60 import static org.onap.dcaegen2.platform.mod.objectmothers.AuthObjectMother.getModUser;
61 import static org.onap.dcaegen2.platform.mod.objectmothers.RoleObjectMother.getRoles;
62 import static org.onap.dcaegen2.platform.mod.objectmothers.UserObjectMother.*;
63 import static org.springframework.test.web.client.match.MockRestRequestMatchers.content;
64 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
65 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
66 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
71 * Mock Test cases for UserController
73 @WebMvcTest(UserController.class)
74 public class UserControllerTest {
78 private MockMvc mockMvc;
81 UserDetailsServiceImpl userDetailsService;
84 UserRepository userRepository;
87 MODUserDetailService modUserDetailService;
90 RoleRepository roleRepository;
93 AuthEntryPointJwt authEntryPointJwt;
99 Authentication authentication;
106 @WithMockUser(roles="ADMIN")
108 void test_getUsername() throws Exception {
110 when(userRepository.findByUsername(any())).thenReturn(Optional.of(new ModUser()));
112 MvcResult result = mockMvc.perform(get("/api/users/" + userId)
113 .contentType(MediaType.APPLICATION_JSON))
114 .andExpect(status().isOk()).andReturn();
116 Assert.assertNotNull(result.getResponse().getContentAsString());
117 verify(userRepository, times(1)).findByUsername(any());
121 void test_getAllUsers() throws Exception {
123 when(modUserDetailService.findAll()).thenReturn(getUsers());
125 MvcResult result = mockMvc.perform(get("/api/users/getAll")
126 .contentType(MediaType.APPLICATION_JSON))
127 .andExpect(status().isOk()).andReturn();
129 Assert.assertNotNull(result.getResponse().getContentAsString());
130 verify(modUserDetailService, times(1)).findAll();
134 @WithMockUser(roles="ADMIN")
136 void test_deleteUser() throws Exception {
138 doNothing().when(modUserDetailService).deleteUserByUsername(any(String.class));
140 MvcResult result = mockMvc.perform(delete("/api/users/" + userId)
141 .contentType(MediaType.APPLICATION_JSON))
142 .andExpect(status().isOk()).andReturn();
144 Assert.assertNotNull(result.getResponse().getContentAsString());
145 verify(modUserDetailService, times(1)).deleteUserByUsername(any(String.class));
149 @WithMockUser(username="ADMIN")
151 void test_userUpdateOwnProfile_returnsSuccessResponse() throws Exception {
153 UpdateUserRequest updateUserRequest = getUpdateUserRequest();
155 when(userDetailsService.adminUpdateUser(userId,updateUserRequest,"token")).thenReturn(getModUser());
157 mockMvc.perform(patch("/api/users/admin/" + userId)
158 //.header("Authorization", "token")
159 .contentType(MediaType.APPLICATION_JSON)
160 .content(asJsonString(updateUserRequest)).accept(MediaType.APPLICATION_JSON))
161 //.andExpect(jsonPath("$.message", notNullValue()))
162 .andExpect(status().isOk()).andReturn();
164 verify(userDetailsService, times(1)).adminUpdateUser(anyString(),updateUserRequest,anyString());