1374e0e2ac65f753be619cec250edd884da1eded
[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 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 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;
53
54 import java.util.Optional;
55
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;
67
68 /**
69  * @author
70  * @date 09/22/2020
71  * Mock Test cases for UserController
72  */
73 @WebMvcTest(UserController.class)
74 public class UserControllerTest {
75
76
77     @Autowired
78     private MockMvc mockMvc;
79
80     @MockBean
81     UserDetailsServiceImpl userDetailsService;
82
83     @MockBean
84     UserRepository userRepository;
85
86     @MockBean
87     MODUserDetailService modUserDetailService;
88
89     @MockBean
90     RoleRepository roleRepository;
91
92     @MockBean
93     AuthEntryPointJwt authEntryPointJwt;
94
95     @MockBean
96     JwtUtils jwtUtils;
97
98     @Mock
99     Authentication authentication;
100
101
102     @BeforeEach
103     void setUp() {
104     }
105
106     @WithMockUser(roles="ADMIN")
107     @Test
108     void test_getUsername() throws Exception {
109
110         when(userRepository.findByUsername(any())).thenReturn(Optional.of(new ModUser()));
111
112         MvcResult result =  mockMvc.perform(get("/api/users/" + userId)
113                 .contentType(MediaType.APPLICATION_JSON))
114                 .andExpect(status().isOk()).andReturn();
115
116         Assert.assertNotNull(result.getResponse().getContentAsString());
117         verify(userRepository, times(1)).findByUsername(any());
118     }
119
120     @Test
121     void test_getAllUsers() throws Exception {
122
123         when(modUserDetailService.findAll()).thenReturn(getUsers());
124
125         MvcResult result =  mockMvc.perform(get("/api/users/getAll")
126                 .contentType(MediaType.APPLICATION_JSON))
127                 .andExpect(status().isOk()).andReturn();
128
129         Assert.assertNotNull(result.getResponse().getContentAsString());
130         verify(modUserDetailService, times(1)).findAll();
131     }
132
133
134     @WithMockUser(roles="ADMIN")
135     @Test
136     void test_deleteUser() throws Exception {
137
138         doNothing().when(modUserDetailService).deleteUserByUsername(any(String.class));
139
140         MvcResult result =  mockMvc.perform(delete("/api/users/" + userId)
141                 .contentType(MediaType.APPLICATION_JSON))
142                 .andExpect(status().isOk()).andReturn();
143
144         Assert.assertNotNull(result.getResponse().getContentAsString());
145         verify(modUserDetailService, times(1)).deleteUserByUsername(any(String.class));
146     }
147
148
149     @WithMockUser(username="ADMIN")
150     @Test
151     void test_userUpdateOwnProfile_returnsSuccessResponse() throws Exception {
152         //arrange
153         UpdateUserRequest updateUserRequest = getUpdateUserRequest();
154
155         when(userDetailsService.adminUpdateUser(userId,updateUserRequest,"token")).thenReturn(getModUser());
156
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();
163
164         verify(userDetailsService, times(1)).adminUpdateUser(anyString(),updateUserRequest,anyString());
165     }
166
167
168 }