Auth Service Errors Fix 71/120371/2
authorRavi Mantena <rx908f@att.com>
Fri, 9 Apr 2021 15:53:04 +0000 (11:53 -0400)
committerRavi Mantena <rx908f@att.com>
Fri, 9 Apr 2021 18:05:05 +0000 (14:05 -0400)
Issue-ID: DCAEGEN2-2727
Change-Id: Ibc2ffe24d2f41fc085c251acafc2bfe6c58c39d4
Signed-off-by: Ravi Mantena <rx908f@att.com>
mod2/auth-service/pom.xml
mod2/auth-service/src/test/java/org/onap/dcaegen2/platform/mod/objectmothers/RoleObjectMother.java
mod2/auth-service/src/test/java/org/onap/dcaegen2/platform/mod/web/AuthControllerTest.java
mod2/auth-service/src/test/java/org/onap/dcaegen2/platform/mod/web/UserControllerTest.java

index 3723584..b1135c5 100644 (file)
                                </exclusion>
                        </exclusions>
                </dependency>
-               <dependency>
-                       <groupId>org.springframework.security</groupId>
-                       <artifactId>spring-security-test</artifactId>
-                       <scope>test</scope>
-               </dependency>
                <dependency>
                        <groupId>com.squareup.okhttp3</groupId>
                        <artifactId>mockwebserver</artifactId>
                        <artifactId>jaxb-api</artifactId>
                        <version>2.3.1</version>
                </dependency>
-               <dependency>
+<!--           <dependency>
                        <groupId>org.testng</groupId>
                        <artifactId>testng</artifactId>
                        <version>RELEASE</version>
                        <scope>test</scope>
-               </dependency>
+               </dependency>-->
        </dependencies>
 
        <dependencyManagement>
index 68002ac..a249744 100644 (file)
@@ -3,7 +3,7 @@
  *  * ============LICENSE_START=======================================================
  *  *  org.onap.dcae
  *  *  ================================================================================
- *  *  Copyright (c) 2020 AT&T Intellectual Property. All rights reserved.
+ *  *  Copyright (c) 2020 - 2021 AT&T Intellectual Property. All rights reserved.
  *  *  ================================================================================
  *  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  *  you may not use this file except in compliance with the License.
 package org.onap.dcaegen2.platform.mod.objectmothers;
 
 
-import org.onap.dcaegen2.platform.mod.models.ModUser;
+import java.util.ArrayList;
+import java.util.List;
 import org.onap.dcaegen2.platform.mod.models.Role;
-import org.onap.dcaegen2.platform.mod.repositories.RoleRepository;
-
-import java.util.*;
-import java.util.stream.Collectors;
 
 /**
  * @author
index 2aad289..7ec1b89 100644 (file)
@@ -3,7 +3,7 @@
  *  * ============LICENSE_START=======================================================
  *  *  org.onap.dcae
  *  *  ================================================================================
- *  *  Copyright (c) 2020 AT&T Intellectual Property. All rights reserved.
+ *  *  Copyright (c) 2020 - 2021 AT&T Intellectual Property. All rights reserved.
  *  *  ================================================================================
  *  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  *  you may not use this file except in compliance with the License.
 
 package org.onap.dcaegen2.platform.mod.web;
 
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.google.gson.Gson;
-import org.junit.Assert;
+import static org.hamcrest.core.IsNull.notNullValue;
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.anyString;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import static org.onap.dcaegen2.platform.mod.objectmothers.AuthObjectMother.asJsonString;
+import static org.onap.dcaegen2.platform.mod.objectmothers.AuthObjectMother.getLoginRequest;
+import static org.onap.dcaegen2.platform.mod.objectmothers.AuthObjectMother.getModUser;
+import static org.onap.dcaegen2.platform.mod.objectmothers.AuthObjectMother.getSignupRequest;
+import static org.onap.dcaegen2.platform.mod.objectmothers.AuthObjectMother.getUserDetailsImpl;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
+
+import java.util.Optional;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.extension.ExtendWith;
 import org.mockito.Mock;
 import org.onap.dcaegen2.platform.mod.controllers.AuthController;
-import org.onap.dcaegen2.platform.mod.models.*;
+import org.onap.dcaegen2.platform.mod.models.LoginRequest;
+import org.onap.dcaegen2.platform.mod.models.Role;
+import org.onap.dcaegen2.platform.mod.models.SignupRequest;
 import org.onap.dcaegen2.platform.mod.repositories.RoleRepository;
 import org.onap.dcaegen2.platform.mod.repositories.UserRepository;
 import org.onap.dcaegen2.platform.mod.security.jwt.AuthEntryPointJwt;
@@ -38,29 +52,14 @@ import org.onap.dcaegen2.platform.mod.security.jwt.JwtUtils;
 import org.onap.dcaegen2.platform.mod.security.services.UserDetailsServiceImpl;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
-import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.boot.test.mock.mockito.MockBean;
-import org.springframework.http.*;
+import org.springframework.http.MediaType;
 import org.springframework.security.authentication.AuthenticationManager;
 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
 import org.springframework.security.core.Authentication;
 import org.springframework.security.crypto.password.PasswordEncoder;
 import org.springframework.security.test.context.support.WithMockUser;
-import org.springframework.test.context.junit.jupiter.SpringExtension;
 import org.springframework.test.web.servlet.MockMvc;
-import org.springframework.web.client.RestTemplate;
-import org.testng.annotations.BeforeTest;
-
-import java.io.IOException;
-import java.util.Optional;
-
-import static org.hamcrest.core.IsNull.notNullValue;
-import static org.mockito.Mockito.*;
-import static org.onap.dcaegen2.platform.mod.objectmothers.AuthObjectMother.*;
-import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
-import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
-import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
-import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
 
 /**
  * @author
index 1374e0e..2e0057b 100644 (file)
@@ -3,7 +3,7 @@
  *  * ============LICENSE_START=======================================================
  *  *  org.onap.dcae
  *  *  ================================================================================
- *  *  Copyright (c) 2020 AT&T Intellectual Property. All rights reserved.
+ *  *  Copyright (c) 2020 - 2021 AT&T Intellectual Property. All rights reserved.
  *  *  ================================================================================
  *  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  *  you may not use this file except in compliance with the License.
 
 package org.onap.dcaegen2.platform.mod.web;
 
-import org.apache.tools.ant.taskdefs.optional.extension.Specification;
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import static org.onap.dcaegen2.platform.mod.objectmothers.AuthObjectMother.asJsonString;
+import static org.onap.dcaegen2.platform.mod.objectmothers.AuthObjectMother.getModUser;
+import static org.onap.dcaegen2.platform.mod.objectmothers.UserObjectMother.getUpdateUserRequest;
+import static org.onap.dcaegen2.platform.mod.objectmothers.UserObjectMother.getUsers;
+import static org.onap.dcaegen2.platform.mod.objectmothers.UserObjectMother.userId;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
+
 import org.junit.Assert;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
-import org.junit.runner.Request;
 import org.mockito.Mock;
-import org.mockito.Mockito;
-import org.onap.dcaegen2.platform.mod.controllers.RoleController;
 import org.onap.dcaegen2.platform.mod.controllers.UserController;
-import org.onap.dcaegen2.platform.mod.models.LoginRequest;
 import org.onap.dcaegen2.platform.mod.models.ModUser;
 import org.onap.dcaegen2.platform.mod.models.UpdateUserRequest;
 import org.onap.dcaegen2.platform.mod.repositories.RoleRepository;
 import org.onap.dcaegen2.platform.mod.repositories.UserRepository;
 import org.onap.dcaegen2.platform.mod.security.jwt.AuthEntryPointJwt;
 import org.onap.dcaegen2.platform.mod.security.jwt.JwtUtils;
+import org.onap.dcaegen2.platform.mod.security.services.UserDetailsImpl;
 import org.onap.dcaegen2.platform.mod.security.services.UserDetailsServiceImpl;
 import org.onap.dcaegen2.platform.mod.services.MODUserDetailService;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -46,24 +57,8 @@ import org.springframework.boot.test.mock.mockito.MockBean;
 import org.springframework.http.MediaType;
 import org.springframework.security.core.Authentication;
 import org.springframework.security.test.context.support.WithMockUser;
-import org.springframework.test.web.client.RequestMatcher;
 import org.springframework.test.web.servlet.MockMvc;
 import org.springframework.test.web.servlet.MvcResult;
-import org.springframework.test.web.servlet.ResultMatcher;
-
-import java.util.Optional;
-
-import static org.hamcrest.Matchers.notNullValue;
-import static org.mockito.ArgumentMatchers.anyString;
-import static org.mockito.Mockito.*;
-import static org.onap.dcaegen2.platform.mod.objectmothers.AuthObjectMother.asJsonString;
-import static org.onap.dcaegen2.platform.mod.objectmothers.AuthObjectMother.getModUser;
-import static org.onap.dcaegen2.platform.mod.objectmothers.RoleObjectMother.getRoles;
-import static org.onap.dcaegen2.platform.mod.objectmothers.UserObjectMother.*;
-import static org.springframework.test.web.client.match.MockRestRequestMatchers.content;
-import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
-import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
-import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
 
 /**
  * @author
@@ -107,16 +102,17 @@ public class UserControllerTest {
     @Test
     void test_getUsername() throws Exception {
 
-        when(userRepository.findByUsername(any())).thenReturn(Optional.of(new ModUser()));
+        when(userDetailsService.loadUserByUsername(userId)).thenReturn(UserDetailsImpl.build(new ModUser()));
 
         MvcResult result =  mockMvc.perform(get("/api/users/" + userId)
                 .contentType(MediaType.APPLICATION_JSON))
                 .andExpect(status().isOk()).andReturn();
 
         Assert.assertNotNull(result.getResponse().getContentAsString());
-        verify(userRepository, times(1)).findByUsername(any());
+        verify(userDetailsService, times(1)).loadUserByUsername(userId);
     }
 
+    @WithMockUser(roles="ADMIN")
     @Test
     void test_getAllUsers() throws Exception {
 
@@ -146,7 +142,7 @@ public class UserControllerTest {
     }
 
 
-    @WithMockUser(username="ADMIN")
+    @WithMockUser(roles="ADMIN")
     @Test
     void test_userUpdateOwnProfile_returnsSuccessResponse() throws Exception {
         //arrange
@@ -155,13 +151,13 @@ public class UserControllerTest {
         when(userDetailsService.adminUpdateUser(userId,updateUserRequest,"token")).thenReturn(getModUser());
 
         mockMvc.perform(patch("/api/users/admin/" + userId)
-                //.header("Authorization", "token")
+                .header("Authorization", "token")
                 .contentType(MediaType.APPLICATION_JSON)
                 .content(asJsonString(updateUserRequest)).accept(MediaType.APPLICATION_JSON))
                 //.andExpect(jsonPath("$.message", notNullValue()))
                 .andExpect(status().isOk()).andReturn();
 
-        verify(userDetailsService, times(1)).adminUpdateUser(anyString(),updateUserRequest,anyString());
+        verify(userDetailsService, times(1)).adminUpdateUser(userId,updateUserRequest,"token");
     }