summaryrefslogtreecommitdiffstats
path: root/mod2/auth-service/src/test/java/org
diff options
context:
space:
mode:
authorMantena, Ravi (rx908f) <rx908f@att.com>2020-09-03 17:14:41 -0400
committerRavi Mantena <rx908f@att.com>2020-09-22 12:54:22 -0400
commit1bebb005afdffa5fcdd54b217a3f8bff39a39c1c (patch)
tree8983a8afc01a78788e741eb4250b633c654fd146 /mod2/auth-service/src/test/java/org
parentb4287ad1fab97142df4d5260ec94d41bb782992a (diff)
New Auth Service in Mod2
Issue-ID: DCAEGEN2-2317 Change-Id: I34b08d7731cc23028f469376ef2147bb28a28b7f Signed-off-by: Ravi Mantena <rx908f@att.com>
Diffstat (limited to 'mod2/auth-service/src/test/java/org')
-rw-r--r--mod2/auth-service/src/test/java/org/onap/dcaegen2/platform/mod/objectmothers/AuthObjectMother.java85
-rw-r--r--mod2/auth-service/src/test/java/org/onap/dcaegen2/platform/mod/objectmothers/RoleObjectMother.java56
-rw-r--r--mod2/auth-service/src/test/java/org/onap/dcaegen2/platform/mod/objectmothers/UserObjectMother.java100
-rw-r--r--mod2/auth-service/src/test/java/org/onap/dcaegen2/platform/mod/util/TestUtil.java60
-rw-r--r--mod2/auth-service/src/test/java/org/onap/dcaegen2/platform/mod/web/AuthControllerTest.java149
-rw-r--r--mod2/auth-service/src/test/java/org/onap/dcaegen2/platform/mod/web/RoleControllerTest.java109
-rw-r--r--mod2/auth-service/src/test/java/org/onap/dcaegen2/platform/mod/web/UserControllerTest.java168
7 files changed, 727 insertions, 0 deletions
diff --git a/mod2/auth-service/src/test/java/org/onap/dcaegen2/platform/mod/objectmothers/AuthObjectMother.java b/mod2/auth-service/src/test/java/org/onap/dcaegen2/platform/mod/objectmothers/AuthObjectMother.java
new file mode 100644
index 0000000..3a22af3
--- /dev/null
+++ b/mod2/auth-service/src/test/java/org/onap/dcaegen2/platform/mod/objectmothers/AuthObjectMother.java
@@ -0,0 +1,85 @@
+/*
+ *
+ * * ============LICENSE_START=======================================================
+ * * org.onap.dcae
+ * * ================================================================================
+ * * Copyright (c) 2020 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.
+ * * You may obtain a copy of the License at
+ * *
+ * * http://www.apache.org/licenses/LICENSE-2.0
+ * *
+ * * Unless required by applicable law or agreed to in writing, software
+ * * distributed under the License is distributed on an "AS IS" BASIS,
+ * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * * See the License for the specific language governing permissions and
+ * * limitations under the License.
+ * * ============LICENSE_END=========================================================
+ *
+ */
+
+package org.onap.dcaegen2.platform.mod.objectmothers;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.onap.dcaegen2.platform.mod.models.LoginRequest;
+import org.onap.dcaegen2.platform.mod.models.ModUser;
+import org.onap.dcaegen2.platform.mod.models.Role;
+import org.onap.dcaegen2.platform.mod.models.SignupRequest;
+import org.onap.dcaegen2.platform.mod.security.services.UserDetailsImpl;
+import org.onap.dcaegen2.platform.mod.util.TestUtil;
+import org.springframework.security.core.authority.SimpleGrantedAuthority;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * @author
+ * @date 09/22/2020
+ * Mock for AuthenticationController Test Case
+ */
+
+public class AuthObjectMother {
+ public static final String LOGIN_REQUEST = "src/test/resources/http/requests/AuthLoginRequest.json";
+ public static final String SIGNUP_REQUEST = "src/test/resources/http/requests/AuthSignupRequest.json";
+
+ public static String asJsonString(final Object object) {
+ try {
+ return new ObjectMapper().writeValueAsString(object);
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public static LoginRequest getLoginRequest() {
+ return TestUtil.deserializeJsonFileToModel(LOGIN_REQUEST, LoginRequest.class);
+ }
+
+ public static SignupRequest getSignupRequest() {
+ return TestUtil.deserializeJsonFileToModel(SIGNUP_REQUEST, SignupRequest.class);
+ }
+
+ public static UserDetailsImpl getUserDetailsImpl() {
+ SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority("ROLE_ADMIN");
+ List authorities = new ArrayList();
+ authorities.add(simpleGrantedAuthority);
+ return new UserDetailsImpl("admin123", "admin123", "admin123", "admin123", authorities);
+ }
+
+ public static ModUser getModUser() {
+ ModUser user = new ModUser();
+ user.setUsername("test");
+ user.setFullName("test");
+ user.setPassword("password");
+ Set<Role> roles = new HashSet<>();
+ Role role = new Role();
+ role.setName("test");
+ role.setId("123");
+ roles.add(role);
+ user.setRoles(roles);
+ return user;
+ }
+}
diff --git a/mod2/auth-service/src/test/java/org/onap/dcaegen2/platform/mod/objectmothers/RoleObjectMother.java b/mod2/auth-service/src/test/java/org/onap/dcaegen2/platform/mod/objectmothers/RoleObjectMother.java
new file mode 100644
index 0000000..68002ac
--- /dev/null
+++ b/mod2/auth-service/src/test/java/org/onap/dcaegen2/platform/mod/objectmothers/RoleObjectMother.java
@@ -0,0 +1,56 @@
+/*
+ *
+ * * ============LICENSE_START=======================================================
+ * * org.onap.dcae
+ * * ================================================================================
+ * * Copyright (c) 2020 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.
+ * * You may obtain a copy of the License at
+ * *
+ * * http://www.apache.org/licenses/LICENSE-2.0
+ * *
+ * * Unless required by applicable law or agreed to in writing, software
+ * * distributed under the License is distributed on an "AS IS" BASIS,
+ * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * * See the License for the specific language governing permissions and
+ * * limitations under the License.
+ * * ============LICENSE_END=========================================================
+ *
+ */
+
+package org.onap.dcaegen2.platform.mod.objectmothers;
+
+
+import org.onap.dcaegen2.platform.mod.models.ModUser;
+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
+ * @date 09/22/2020
+ * Mock for RoleController Test Case
+ */
+public class RoleObjectMother {
+
+ public static List<Role> getRoles() {
+ List<Role> roles = new ArrayList();
+ Role role1 = new Role();
+ role1.setId("123");
+ role1.setName("Admin123");
+
+ Role role2 = new Role();
+ role2.setId("1234");
+ role2.setName("Admin1234");
+
+ roles.add(role1);
+ roles.add(role2);
+
+ return roles;
+ }
+
+}
diff --git a/mod2/auth-service/src/test/java/org/onap/dcaegen2/platform/mod/objectmothers/UserObjectMother.java b/mod2/auth-service/src/test/java/org/onap/dcaegen2/platform/mod/objectmothers/UserObjectMother.java
new file mode 100644
index 0000000..301b81f
--- /dev/null
+++ b/mod2/auth-service/src/test/java/org/onap/dcaegen2/platform/mod/objectmothers/UserObjectMother.java
@@ -0,0 +1,100 @@
+/*
+ *
+ * * ============LICENSE_START=======================================================
+ * * org.onap.dcae
+ * * ================================================================================
+ * * Copyright (c) 2020 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.
+ * * You may obtain a copy of the License at
+ * *
+ * * http://www.apache.org/licenses/LICENSE-2.0
+ * *
+ * * Unless required by applicable law or agreed to in writing, software
+ * * distributed under the License is distributed on an "AS IS" BASIS,
+ * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * * See the License for the specific language governing permissions and
+ * * limitations under the License.
+ * * ============LICENSE_END=========================================================
+ *
+ */
+
+package org.onap.dcaegen2.platform.mod.objectmothers;
+
+
+import org.onap.dcaegen2.platform.mod.models.ModUser;
+import org.onap.dcaegen2.platform.mod.models.Role;
+import org.onap.dcaegen2.platform.mod.models.UpdateUserRequest;
+import org.onap.dcaegen2.platform.mod.security.services.UserDetailsImpl;
+import org.springframework.security.core.authority.SimpleGrantedAuthority;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * @author
+ * @date 09/22/2020
+ * Mock for UserController Test Case
+ */
+public class UserObjectMother {
+
+ public static final String userId = "admin";
+
+ public static List<ModUser> getUsers() {
+ List<ModUser> users = new ArrayList();
+ ModUser modUser1 = new ModUser();
+ modUser1.set_id("123");
+ modUser1.setFullName("Admin123");
+ modUser1.setPassword("Admin123");
+ Set<Role> roles = new HashSet<>();
+ Role role = new Role();
+ role.setName("test");
+ role.setId("123");
+ roles.add(role);
+ modUser1.setRoles(roles);
+
+ ModUser modUser2 = new ModUser();
+ modUser2.set_id("1234");
+ modUser2.setFullName("Admin1234");
+ modUser2.setPassword("Admin1234");
+ Set<Role> roles1 = new HashSet<>();
+ Role role1 = new Role();
+ role.setName("test1");
+ role.setId("1234");
+ roles.add(role1);
+ modUser2.setRoles(roles1);
+
+ users.add(modUser1);
+ users.add(modUser2);
+
+ return users;
+ }
+
+ public static ModUser getModUser() {
+ ModUser user = new ModUser();
+ user.setUsername("test");
+ user.setFullName("test");
+ user.setPassword("password");
+ Set<Role> roles = new HashSet<>();
+ Role role = new Role();
+ role.setName("test");
+ role.setId("123");
+ roles.add(role);
+ user.setRoles(roles);
+ return user;
+ }
+
+ public static UpdateUserRequest getUpdateUserRequest(){
+ UpdateUserRequest user = new UpdateUserRequest();
+ user.setFullName("test");
+ user.setPassword("password");
+ Set<String> roles = new HashSet<>();
+ roles.add("ROLE_PST");
+ user.setRoles(roles);
+ return user;
+ }
+
+}
diff --git a/mod2/auth-service/src/test/java/org/onap/dcaegen2/platform/mod/util/TestUtil.java b/mod2/auth-service/src/test/java/org/onap/dcaegen2/platform/mod/util/TestUtil.java
new file mode 100644
index 0000000..111653f
--- /dev/null
+++ b/mod2/auth-service/src/test/java/org/onap/dcaegen2/platform/mod/util/TestUtil.java
@@ -0,0 +1,60 @@
+/*
+ *
+ * * ============LICENSE_START=======================================================
+ * * org.onap.dcae
+ * * ================================================================================
+ * * Copyright (c) 2020 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.
+ * * You may obtain a copy of the License at
+ * *
+ * * http://www.apache.org/licenses/LICENSE-2.0
+ * *
+ * * Unless required by applicable law or agreed to in writing, software
+ * * distributed under the License is distributed on an "AS IS" BASIS,
+ * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * * See the License for the specific language governing permissions and
+ * * limitations under the License.
+ * * ============LICENSE_END=========================================================
+ *
+ */
+
+package org.onap.dcaegen2.platform.mod.util;
+
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Map;
+
+/**
+ * @author
+ * @date 09/22/2020
+ * TestUtils for test cases
+ */
+public class TestUtil {
+
+ private static final ObjectMapper MAPPER = new ObjectMapper();
+
+ private TestUtil() {}
+
+ public static Map<String, Object> readJsonFileAsObjectMap(String filePath) {
+ try {
+ return MAPPER.readValue(new File(filePath), new TypeReference<Map<String, Object>>() {});
+ } catch (IOException e) {
+ e.printStackTrace();
+ throw new RuntimeException();
+ }
+ }
+
+ public static <T> T deserializeJsonFileToModel(String filePath, Class<T> modelClass) {
+ try {
+ return MAPPER.readValue(new File(filePath), modelClass);
+ } catch (IOException e) {
+ e.printStackTrace();
+ throw new RuntimeException();
+ }
+ }
+}
diff --git a/mod2/auth-service/src/test/java/org/onap/dcaegen2/platform/mod/web/AuthControllerTest.java b/mod2/auth-service/src/test/java/org/onap/dcaegen2/platform/mod/web/AuthControllerTest.java
new file mode 100644
index 0000000..2aad289
--- /dev/null
+++ b/mod2/auth-service/src/test/java/org/onap/dcaegen2/platform/mod/web/AuthControllerTest.java
@@ -0,0 +1,149 @@
+/*
+ *
+ * * ============LICENSE_START=======================================================
+ * * org.onap.dcae
+ * * ================================================================================
+ * * Copyright (c) 2020 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.
+ * * You may obtain a copy of the License at
+ * *
+ * * http://www.apache.org/licenses/LICENSE-2.0
+ * *
+ * * Unless required by applicable law or agreed to in writing, software
+ * * distributed under the License is distributed on an "AS IS" BASIS,
+ * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * * See the License for the specific language governing permissions and
+ * * limitations under the License.
+ * * ============LICENSE_END=========================================================
+ *
+ */
+
+package org.onap.dcaegen2.platform.mod.web;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.gson.Gson;
+import org.junit.Assert;
+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.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.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.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
+ * @date 09/22/2020
+ * Mock Test cases for AuthenticationController
+ */
+@WebMvcTest(AuthController.class)
+public class AuthControllerTest {
+
+ @Autowired
+ private MockMvc mockMvc;
+
+ @MockBean
+ JwtUtils jwtUtils;
+
+ @MockBean
+ AuthenticationManager authenticationManager;
+
+ @MockBean
+ UserRepository userRepository;
+
+ @MockBean
+ RoleRepository roleRepository;
+
+ @MockBean
+ PasswordEncoder passwordEncoder;
+
+ @MockBean
+ UserDetailsServiceImpl userDetailsService;
+
+ @MockBean
+ AuthEntryPointJwt authEntryPointJwt;
+
+ @Mock
+ Authentication authentication;
+
+ @BeforeEach
+ void setUp() {
+ }
+
+
+ @Test
+ void test_signin_returnsSuccessResponse() throws Exception {
+
+ LoginRequest loginRequest = getLoginRequest();
+
+ when(authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(loginRequest.getUsername(), loginRequest.getPassword()))).thenReturn(authentication);
+ when(authentication.getPrincipal()).thenReturn(getUserDetailsImpl());
+ when(jwtUtils.generateJwtToken(any())).thenReturn("Demo");
+
+ mockMvc.perform(post("/api/auth/signin")
+ .contentType(MediaType.APPLICATION_JSON)
+ .content(asJsonString(loginRequest)).accept(MediaType.APPLICATION_JSON))
+ .andExpect(jsonPath("$.token", notNullValue()))
+ .andExpect(status().isOk());
+
+ verify(authenticationManager, times(1)).authenticate(new UsernamePasswordAuthenticationToken(loginRequest.getUsername(), loginRequest.getPassword()));
+ verify(jwtUtils, times(1)).generateJwtToken(any());
+ }
+
+
+ @WithMockUser(roles="ADMIN")
+ @Test
+ void test_signup_returnsSuccessResponse() throws Exception {
+
+ SignupRequest signUpRequest = getSignupRequest();
+
+ when(userRepository.existsByUsername(signUpRequest.getUsername())).thenReturn(false);
+ when(passwordEncoder.encode(signUpRequest.getPassword())).thenReturn("password");
+ when(roleRepository.findByName(anyString())).thenReturn(Optional.of(new Role()));
+ when(userRepository.save(any())).thenReturn(getModUser());
+
+ mockMvc.perform(post("/api/auth/signup")
+ .contentType(MediaType.APPLICATION_JSON)
+ .content(asJsonString(signUpRequest)).accept(MediaType.APPLICATION_JSON))
+ .andExpect(jsonPath("$.message", notNullValue()))
+ .andExpect(status().isOk());
+
+ verify(userRepository, times(1)).existsByUsername(signUpRequest.getUsername());
+ verify(passwordEncoder, times(1)).encode(signUpRequest.getPassword());
+ verify(roleRepository, times(1)).findByName(anyString());
+ verify(userRepository, times(1)).save(any());
+ }
+
+
+}
diff --git a/mod2/auth-service/src/test/java/org/onap/dcaegen2/platform/mod/web/RoleControllerTest.java b/mod2/auth-service/src/test/java/org/onap/dcaegen2/platform/mod/web/RoleControllerTest.java
new file mode 100644
index 0000000..48b5d4b
--- /dev/null
+++ b/mod2/auth-service/src/test/java/org/onap/dcaegen2/platform/mod/web/RoleControllerTest.java
@@ -0,0 +1,109 @@
+/*
+ *
+ * * ============LICENSE_START=======================================================
+ * * org.onap.dcae
+ * * ================================================================================
+ * * Copyright (c) 2020 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.
+ * * You may obtain a copy of the License at
+ * *
+ * * http://www.apache.org/licenses/LICENSE-2.0
+ * *
+ * * Unless required by applicable law or agreed to in writing, software
+ * * distributed under the License is distributed on an "AS IS" BASIS,
+ * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * * See the License for the specific language governing permissions and
+ * * limitations under the License.
+ * * ============LICENSE_END=========================================================
+ *
+ */
+
+package org.onap.dcaegen2.platform.mod.web;
+
+import org.hamcrest.Matchers;
+import org.junit.Assert;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.onap.dcaegen2.platform.mod.controllers.RoleController;
+import org.onap.dcaegen2.platform.mod.models.ModUser;
+import org.onap.dcaegen2.platform.mod.models.Role;
+import org.onap.dcaegen2.platform.mod.repositories.RoleRepository;
+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.UserDetailsServiceImpl;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
+import org.springframework.boot.test.mock.mockito.MockBean;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.MediaType;
+import org.springframework.security.core.Authentication;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.MvcResult;
+import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
+import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.ResponseStatus;
+
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import static org.hamcrest.core.IsNull.notNullValue;
+import static org.onap.dcaegen2.platform.mod.objectmothers.AuthObjectMother.asJsonString;
+import static org.onap.dcaegen2.platform.mod.objectmothers.RoleObjectMother.getRoles;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
+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
+ * @date 09/22/2020
+ * Mock Test cases for RoleController
+ */
+@WebMvcTest(RoleController.class)
+public class RoleControllerTest {
+
+ @Autowired
+ private MockMvc mockMvc;
+
+ @MockBean
+ RoleRepository roleRepository;
+
+ @MockBean
+ UserDetailsServiceImpl userDetailsService;
+
+ @MockBean
+ AuthEntryPointJwt authEntryPointJwt;
+
+ @MockBean
+ JwtUtils jwtUtils;
+
+ @Mock
+ Authentication authentication;
+
+ @BeforeEach
+ void setUp() {
+ }
+
+
+ @Test
+ void test_getRoles() throws Exception {
+
+ Mockito.when(roleRepository.findAll()).thenReturn(getRoles());
+
+ MvcResult result = mockMvc.perform(get("/api/roles")
+ .contentType(MediaType.APPLICATION_JSON))
+ .andExpect(status().isOk()).andReturn();
+
+ Assert.assertNotNull(result.getResponse().getContentAsString());
+
+ }
+
+}
diff --git a/mod2/auth-service/src/test/java/org/onap/dcaegen2/platform/mod/web/UserControllerTest.java b/mod2/auth-service/src/test/java/org/onap/dcaegen2/platform/mod/web/UserControllerTest.java
new file mode 100644
index 0000000..1374e0e
--- /dev/null
+++ b/mod2/auth-service/src/test/java/org/onap/dcaegen2/platform/mod/web/UserControllerTest.java
@@ -0,0 +1,168 @@
+/*
+ *
+ * * ============LICENSE_START=======================================================
+ * * org.onap.dcae
+ * * ================================================================================
+ * * Copyright (c) 2020 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.
+ * * You may obtain a copy of the License at
+ * *
+ * * http://www.apache.org/licenses/LICENSE-2.0
+ * *
+ * * Unless required by applicable law or agreed to in writing, software
+ * * distributed under the License is distributed on an "AS IS" BASIS,
+ * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * * See the License for the specific language governing permissions and
+ * * limitations under the License.
+ * * ============LICENSE_END=========================================================
+ *
+ */
+
+package org.onap.dcaegen2.platform.mod.web;
+
+import org.apache.tools.ant.taskdefs.optional.extension.Specification;
+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.UserDetailsServiceImpl;
+import org.onap.dcaegen2.platform.mod.services.MODUserDetailService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
+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
+ * @date 09/22/2020
+ * Mock Test cases for UserController
+ */
+@WebMvcTest(UserController.class)
+public class UserControllerTest {
+
+
+ @Autowired
+ private MockMvc mockMvc;
+
+ @MockBean
+ UserDetailsServiceImpl userDetailsService;
+
+ @MockBean
+ UserRepository userRepository;
+
+ @MockBean
+ MODUserDetailService modUserDetailService;
+
+ @MockBean
+ RoleRepository roleRepository;
+
+ @MockBean
+ AuthEntryPointJwt authEntryPointJwt;
+
+ @MockBean
+ JwtUtils jwtUtils;
+
+ @Mock
+ Authentication authentication;
+
+
+ @BeforeEach
+ void setUp() {
+ }
+
+ @WithMockUser(roles="ADMIN")
+ @Test
+ void test_getUsername() throws Exception {
+
+ when(userRepository.findByUsername(any())).thenReturn(Optional.of(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());
+ }
+
+ @Test
+ void test_getAllUsers() throws Exception {
+
+ when(modUserDetailService.findAll()).thenReturn(getUsers());
+
+ MvcResult result = mockMvc.perform(get("/api/users/getAll")
+ .contentType(MediaType.APPLICATION_JSON))
+ .andExpect(status().isOk()).andReturn();
+
+ Assert.assertNotNull(result.getResponse().getContentAsString());
+ verify(modUserDetailService, times(1)).findAll();
+ }
+
+
+ @WithMockUser(roles="ADMIN")
+ @Test
+ void test_deleteUser() throws Exception {
+
+ doNothing().when(modUserDetailService).deleteUserByUsername(any(String.class));
+
+ MvcResult result = mockMvc.perform(delete("/api/users/" + userId)
+ .contentType(MediaType.APPLICATION_JSON))
+ .andExpect(status().isOk()).andReturn();
+
+ Assert.assertNotNull(result.getResponse().getContentAsString());
+ verify(modUserDetailService, times(1)).deleteUserByUsername(any(String.class));
+ }
+
+
+ @WithMockUser(username="ADMIN")
+ @Test
+ void test_userUpdateOwnProfile_returnsSuccessResponse() throws Exception {
+ //arrange
+ UpdateUserRequest updateUserRequest = getUpdateUserRequest();
+
+ when(userDetailsService.adminUpdateUser(userId,updateUserRequest,"token")).thenReturn(getModUser());
+
+ mockMvc.perform(patch("/api/users/admin/" + userId)
+ //.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());
+ }
+
+
+} \ No newline at end of file