Auth Service Errors Fix
[dcaegen2/platform.git] / mod2 / auth-service / src / test / java / org / onap / dcaegen2 / platform / mod / web / UserControllerTest.java
1 /*
2  *
3  *  * ============LICENSE_START=======================================================
4  *  *  org.onap.dcae
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
11  *  *
12  *  *       http://www.apache.org/licenses/LICENSE-2.0
13  *  *
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=========================================================
20  *
21  */
22
23 package org.onap.dcaegen2.platform.mod.web;
24
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;
39
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;
62
63 /**
64  * @author
65  * @date 09/22/2020
66  * Mock Test cases for UserController
67  */
68 @WebMvcTest(UserController.class)
69 public class UserControllerTest {
70
71
72     @Autowired
73     private MockMvc mockMvc;
74
75     @MockBean
76     UserDetailsServiceImpl userDetailsService;
77
78     @MockBean
79     UserRepository userRepository;
80
81     @MockBean
82     MODUserDetailService modUserDetailService;
83
84     @MockBean
85     RoleRepository roleRepository;
86
87     @MockBean
88     AuthEntryPointJwt authEntryPointJwt;
89
90     @MockBean
91     JwtUtils jwtUtils;
92
93     @Mock
94     Authentication authentication;
95
96
97     @BeforeEach
98     void setUp() {
99     }
100
101     @WithMockUser(roles="ADMIN")
102     @Test
103     void test_getUsername() throws Exception {
104
105         when(userDetailsService.loadUserByUsername(userId)).thenReturn(UserDetailsImpl.build(new ModUser()));
106
107         MvcResult result =  mockMvc.perform(get("/api/users/" + userId)
108                 .contentType(MediaType.APPLICATION_JSON))
109                 .andExpect(status().isOk()).andReturn();
110
111         Assert.assertNotNull(result.getResponse().getContentAsString());
112         verify(userDetailsService, times(1)).loadUserByUsername(userId);
113     }
114
115     @WithMockUser(roles="ADMIN")
116     @Test
117     void test_getAllUsers() throws Exception {
118
119         when(modUserDetailService.findAll()).thenReturn(getUsers());
120
121         MvcResult result =  mockMvc.perform(get("/api/users/getAll")
122                 .contentType(MediaType.APPLICATION_JSON))
123                 .andExpect(status().isOk()).andReturn();
124
125         Assert.assertNotNull(result.getResponse().getContentAsString());
126         verify(modUserDetailService, times(1)).findAll();
127     }
128
129
130     @WithMockUser(roles="ADMIN")
131     @Test
132     void test_deleteUser() throws Exception {
133
134         doNothing().when(modUserDetailService).deleteUserByUsername(any(String.class));
135
136         MvcResult result =  mockMvc.perform(delete("/api/users/" + userId)
137                 .contentType(MediaType.APPLICATION_JSON))
138                 .andExpect(status().isOk()).andReturn();
139
140         Assert.assertNotNull(result.getResponse().getContentAsString());
141         verify(modUserDetailService, times(1)).deleteUserByUsername(any(String.class));
142     }
143
144
145     @WithMockUser(roles="ADMIN")
146     @Test
147     void test_userUpdateOwnProfile_returnsSuccessResponse() throws Exception {
148         //arrange
149         UpdateUserRequest updateUserRequest = getUpdateUserRequest();
150
151         when(userDetailsService.adminUpdateUser(userId,updateUserRequest,"token")).thenReturn(getModUser());
152
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();
159
160         verify(userDetailsService, times(1)).adminUpdateUser(userId,updateUserRequest,"token");
161     }
162
163
164 }