From a96a3e49cd472aa902c22143358b87562603d47c Mon Sep 17 00:00:00 2001 From: "Kishore Reddy, Gujja (kg811t)" Date: Mon, 9 Jul 2018 13:41:00 -0400 Subject: Adding User Auth and permission aaf services Issue-ID: PORTAL-334 Change-Id: I2826f2a06f7d818d918ae5f45b500a8da78cec42 Signed-off-by: Kishore Reddy, Gujja (kg811t) --- .../service/UserApiServiceImplTest.java | 46 ++++++++++++++++++++-- 1 file changed, 43 insertions(+), 3 deletions(-) (limited to 'ecomp-sdk/epsdk-aaf/src/test/java/org/onap/portalsdk/external/authorization/service/UserApiServiceImplTest.java') diff --git a/ecomp-sdk/epsdk-aaf/src/test/java/org/onap/portalsdk/external/authorization/service/UserApiServiceImplTest.java b/ecomp-sdk/epsdk-aaf/src/test/java/org/onap/portalsdk/external/authorization/service/UserApiServiceImplTest.java index d93b03c1..9836c5f1 100644 --- a/ecomp-sdk/epsdk-aaf/src/test/java/org/onap/portalsdk/external/authorization/service/UserApiServiceImplTest.java +++ b/ecomp-sdk/epsdk-aaf/src/test/java/org/onap/portalsdk/external/authorization/service/UserApiServiceImplTest.java @@ -37,6 +37,7 @@ */ package org.onap.portalsdk.external.authorization.service; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; @@ -83,6 +84,7 @@ import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.RestTemplate; import com.fasterxml.jackson.databind.ObjectMapper; @@ -132,7 +134,7 @@ public class UserApiServiceImplTest { PowerMockito.mockStatic(SystemProperties.class); Mockito.when(EcompExternalAuthProperties.getProperty(EcompExternalAuthProperties.EXTERNAL_AUTH_NAMESPACE)) .thenReturn("com.test.app2"); - Mockito.when(EcompExternalAuthUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(new HttpHeaders()); + Mockito.when(EcompExternalAuthUtils.base64encodeKeyForAAFBasicAuth(Matchers.anyString(), Matchers.anyString())).thenReturn(new HttpHeaders()); Mockito.when(EcompExternalAuthProperties.getProperty(EcompExternalAuthProperties.EXTERNAL_AUTH_USER_DOMAIN)) .thenReturn("@test.com"); MockitoAnnotations.initMocks(this); @@ -286,6 +288,15 @@ public class UserApiServiceImplTest { @Test public void getRoleFunctionsTest() throws Exception { + JSONObject mockJsonObjectPerms = mockUserPerms(); + ResponseEntity response = new ResponseEntity<>(mockJsonObjectPerms.toString(), HttpStatus.OK); + Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET), + Matchers.>any(), Matchers.eq(String.class))).thenReturn(response); + List actual = UserApiServiceImpl.getRoleFunctions("test123"); + assertNotNull(actual); + } + + private JSONObject mockUserPerms() { JSONObject mockJsonObjectPerms = new JSONObject(); JSONObject mockJsonObjectPerm1 = new JSONObject(); JSONObject mockJsonObjectPerm2 = new JSONObject(); @@ -304,10 +315,39 @@ public class UserApiServiceImplTest { permsList.add(mockJsonObjectPerm1); permsList.add(mockJsonObjectPerm2); mockJsonObjectPerms.put("perm", permsList); + return mockJsonObjectPerms; + } + + @Test + public void checkUserExistsTest() throws Exception { + ResponseEntity response = new ResponseEntity<>(HttpStatus.OK); + Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST), + Matchers.>any(), Matchers.eq(String.class))).thenReturn(response); + ResponseEntity actual = UserApiServiceImpl.checkUserExists("test", "test"); + assertNotNull(actual); + } + + @Test(expected = HttpClientErrorException.class) + public void checkUserExistsExceptionTest() throws Exception { + Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST), + Matchers.>any(), Matchers.eq(String.class))).thenThrow(new HttpClientErrorException(HttpStatus.UNAUTHORIZED)); + UserApiServiceImpl.checkUserExists("test", "test"); + } + + @Test + public void getIfUserPermsExistsTest() throws Exception { + JSONObject mockJsonObjectPerms = mockUserPerms(); ResponseEntity response = new ResponseEntity<>(mockJsonObjectPerms.toString(), HttpStatus.OK); Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET), Matchers.>any(), Matchers.eq(String.class))).thenReturn(response); - List actual = UserApiServiceImpl.getRoleFunctions("test123"); - assertNotNull(actual); + UserApiServiceImpl.getIfUserPermsExists("test123@test.com"); + } + + @Test(expected = HttpClientErrorException.class) + public void getIfUserPermsExistsInvalidUserTest() throws Exception { + Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET), + Matchers.>any(), Matchers.eq(String.class))).thenThrow(new HttpClientErrorException(HttpStatus.UNAUTHORIZED)); + UserApiServiceImpl.getIfUserPermsExists("test1"); } + } -- cgit 1.2.3-korg