You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airavata.apache.org by sm...@apache.org on 2019/09/03 13:43:21 UTC
[airavata-custos] 26/45: addes user profile test cases
This is an automated email from the ASF dual-hosted git repository.
smarru pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/airavata-custos.git
commit 20e50106dba1251e62abb74073aa5f675874e5a6
Author: Aarushi <aa...@gmail.com>
AuthorDate: Mon Aug 19 21:27:55 2019 -0400
addes user profile test cases
---
.../src/main/resources/custos-server.properties | 3 +-
.../src/main/resources/META-INF/persistence.xml | 1 -
.../user-profile-service/pom.xml | 14 +++
.../user/handler/UserProfileServiceHandler.java | 18 ++--
.../test/java/TestUserProfileServiceHandler.java | 110 +++++++++++++++++++++
.../src/main/resources/custos-server.properties | 3 +-
.../src/main/resources/docker-compose.yml | 2 +-
pom.xml | 2 +
8 files changed, 139 insertions(+), 14 deletions(-)
diff --git a/ide-integration/src/main/resources/custos-server.properties b/custos-commons/src/main/resources/custos-server.properties
similarity index 98%
copy from ide-integration/src/main/resources/custos-server.properties
copy to custos-commons/src/main/resources/custos-server.properties
index 4d75666..d9d59c3 100644
--- a/ide-integration/src/main/resources/custos-server.properties
+++ b/custos-commons/src/main/resources/custos-server.properties
@@ -54,8 +54,9 @@ in.memory.cache.size=1000
# Profile Service Configuration
###########################################################################
profile.service.server.host=0.0.0.0
-profile.service.server.port=8963
+profile.service.server.port=9092
profile_service=org.apache.custos.profile.server.ProfileServiceServer
+
# MariaDB properties
profile.service.jdbc.url=jdbc:mariadb://localhost:13306/profile_service
profile.service.jdbc.user=root
diff --git a/custos-profile-service/profile-service-commons/src/main/resources/META-INF/persistence.xml b/custos-profile-service/profile-service-commons/src/main/resources/META-INF/persistence.xml
index f0a162e..4b92928 100644
--- a/custos-profile-service/profile-service-commons/src/main/resources/META-INF/persistence.xml
+++ b/custos-profile-service/profile-service-commons/src/main/resources/META-INF/persistence.xml
@@ -24,7 +24,6 @@
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<class>org.apache.custos.profile.commons.user.entities.UserProfileEntity</class>
<class>org.apache.custos.profile.commons.user.entities.NSFDemographicsEntity</class>
- <class>org.apache.custos.profile.commons.user.entities.CustomizedDashboardEntity</class>
<class>org.apache.custos.profile.commons.tenant.entities.GatewayEntity</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
diff --git a/custos-profile-service/user-profile-service/pom.xml b/custos-profile-service/user-profile-service/pom.xml
index 4afd95c..ec79614 100644
--- a/custos-profile-service/user-profile-service/pom.xml
+++ b/custos-profile-service/user-profile-service/pom.xml
@@ -34,6 +34,20 @@
<artifactId>iam-admin-services</artifactId>
<version>${project.version}</version>
</dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>${junit.version}</version>
+ <scope>test</scope>
+ </dependency>
+ <!-- https://mvnrepository.com/artifact/org.jmockit/jmockit -->
+ <dependency>
+ <groupId>org.jmockit</groupId>
+ <artifactId>jmockit</artifactId>
+ <version>${jmockit.version}</version>
+ <scope>test</scope>
+ </dependency>
+
</dependencies>
diff --git a/custos-profile-service/user-profile-service/src/main/java/org/apache/custos/profile/user/handler/UserProfileServiceHandler.java b/custos-profile-service/user-profile-service/src/main/java/org/apache/custos/profile/user/handler/UserProfileServiceHandler.java
index 317eb51..e29b39d 100644
--- a/custos-profile-service/user-profile-service/src/main/java/org/apache/custos/profile/user/handler/UserProfileServiceHandler.java
+++ b/custos-profile-service/user-profile-service/src/main/java/org/apache/custos/profile/user/handler/UserProfileServiceHandler.java
@@ -27,13 +27,13 @@ import org.apache.custos.commons.model.security.UserInfo;
import org.apache.custos.commons.utils.Constants;
import org.apache.custos.commons.utils.CustosUtils;
import org.apache.custos.commons.utils.ServerSettings;
+import org.apache.custos.profile.commons.repositories.UserProfileRepository;
import org.apache.custos.profile.iam.admin.services.client.CustosIamAdminServiceClient;
import org.apache.custos.profile.iam.admin.services.cpi.IamAdminServices;
import org.apache.custos.profile.iam.admin.services.cpi.exception.IamAdminServicesException;
import org.apache.custos.profile.model.user.Status;
import org.apache.custos.profile.model.user.UserProfile;
import org.apache.custos.profile.model.user.user_profile_modelConstants;
-import org.apache.custos.profile.commons.repositories.UserProfileRepository;
import org.apache.custos.profile.user.cpi.UserProfileService;
import org.apache.custos.profile.user.cpi.exception.UserProfileServiceException;
import org.apache.custos.security.manager.CustosSecurityManager;
@@ -62,7 +62,6 @@ public class UserProfileServiceHandler implements UserProfileService.Iface {
}
@Override
- //@SecurityCheck
public UserProfile initializeUserProfile(AuthzToken authzToken) throws UserProfileServiceException {
String gatewayId = authzToken.getClaimsMap().get(Constants.GATEWAY_ID);
try {
@@ -100,7 +99,6 @@ public class UserProfileServiceHandler implements UserProfileService.Iface {
}
@Override
- //@SecurityCheck
public UserProfile addUserProfile(AuthzToken authzToken, UserProfile userProfile) throws UserProfileServiceException {
try{
// Lowercase user id and internal id
@@ -122,7 +120,6 @@ public class UserProfileServiceHandler implements UserProfileService.Iface {
}
@Override
- //@SecurityCheck
public UserProfile updateUserProfile(AuthzToken authzToken, UserProfile userProfile) throws UserProfileServiceException {
try {
// After updating the user profile in the database but before committing the transaction, the
@@ -157,8 +154,7 @@ public class UserProfileServiceHandler implements UserProfileService.Iface {
}
@Override
- //@SecurityCheck
- public UserProfile getUserProfileById(AuthzToken authzToken, String userId, String gatewayId) throws UserProfileServiceException,TException {
+ public UserProfile getUserProfileById(AuthzToken authzToken, String userId, String gatewayId) throws UserProfileServiceException {
try{
UserProfile userProfile = userProfileRepository.getUserProfileByIdAndGateWay(userId, gatewayId);
if(userProfile != null)
@@ -174,8 +170,7 @@ public class UserProfileServiceHandler implements UserProfileService.Iface {
}
@Override
- //@SecurityCheck
- public UserProfile deleteUserProfile(AuthzToken authzToken, String userId, String gatewayId) throws UserProfileServiceException,TException {
+ public UserProfile deleteUserProfile(AuthzToken authzToken, String userId, String gatewayId) throws UserProfileServiceException {
UserProfile userProfile = null;
try{
// find user-profile
@@ -184,17 +179,20 @@ public class UserProfileServiceHandler implements UserProfileService.Iface {
// delete user
boolean deleteSuccess = userProfileRepository.delete(userId);
logger.info("Delete UserProfile with userId: " + userId + ", " + (deleteSuccess? "Success!" : "Failed!"));
+ if(deleteSuccess){
+ return userProfile;
+ }else {
+ throw new Exception("Deleting of user profile with userId: "+ userId + "was not successful");
+ }
} catch (Exception e) {
logger.error("Error while deleting user profile", e);
UserProfileServiceException exception = new UserProfileServiceException();
exception.setMessage("Error while deleting user profile. More info : " + e.getMessage());
throw exception;
}
- return userProfile;
}
@Override
- //@SecurityCheck
public List<UserProfile> getAllUserProfilesInGateway(AuthzToken authzToken, String gatewayId, int offset, int limit) throws UserProfileServiceException {
try{
List<UserProfile> usersInGateway = userProfileRepository.getAllUserProfilesInGateway(gatewayId, offset, limit);
diff --git a/custos-profile-service/user-profile-service/src/test/java/TestUserProfileServiceHandler.java b/custos-profile-service/user-profile-service/src/test/java/TestUserProfileServiceHandler.java
new file mode 100644
index 0000000..b88cee2
--- /dev/null
+++ b/custos-profile-service/user-profile-service/src/test/java/TestUserProfileServiceHandler.java
@@ -0,0 +1,110 @@
+import mockit.*;
+import mockit.integration.junit4.JMockit;
+import org.apache.custos.commons.exceptions.CustosSecurityException;
+import org.apache.custos.commons.model.security.AuthzToken;
+import org.apache.custos.commons.model.security.UserInfo;
+import org.apache.custos.commons.utils.Constants;
+import org.apache.custos.profile.model.user.UserProfile;
+import org.apache.custos.profile.user.cpi.exception.UserProfileServiceException;
+import org.apache.custos.profile.user.handler.UserProfileServiceHandler;
+import org.apache.custos.security.manager.CustosSecurityManager;
+import org.apache.custos.security.manager.SecurityManagerFactory;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import java.util.HashMap;
+import java.util.List;
+
+import static org.junit.Assert.*;
+
+@RunWith(JMockit.class)
+public class TestUserProfileServiceHandler {
+
+ private static String GATEWAY_ID = "test-gateway";
+ private static String USER_NAME = "default-user";
+ private static AuthzToken authzToken = new AuthzToken();
+ private static String NEW_USER_ID = "test-username";
+ private int NUMBER_OF_USERS = 1;
+ @Tested
+ private static UserProfileServiceHandler userProfileServiceHandler;
+ @Mocked
+ private static CustosSecurityManager mockedSecurityManager;
+
+ @Before
+ public void setUp() throws CustosSecurityException {
+ HashMap<String,String> map = new HashMap<>();
+ map.put(Constants.GATEWAY_ID, GATEWAY_ID);
+ map.put(Constants.USER_NAME, USER_NAME);
+ authzToken.setClaimsMap(map);
+ authzToken.setAccessToken("access token");
+ }
+ @After
+ public void cleanUp() {
+
+ }
+ @Test
+ public void testInitializeUserProfileWhenUserProfileDoesNotExist(@Mocked CustosSecurityManager mockedSecurityManager) throws UserProfileServiceException, CustosSecurityException {
+ UserInfo userInfo = new UserInfo();
+ userInfo.setUsername("test-username");
+ userInfo.setEmailAddress("test@test.com");
+ userInfo.setFirstName("test-first-name");
+ userInfo.setLastName("test-last-name");
+ new MockUp<SecurityManagerFactory>() {
+ @Mock
+ public CustosSecurityManager getSecurityManager(){return mockedSecurityManager;};
+ };
+ new Expectations() {{ mockedSecurityManager.getUserInfoFromAuthzToken(authzToken); result = userInfo;}};
+
+ UserProfile createdUserProfile = userProfileServiceHandler.initializeUserProfile(authzToken);
+ assertNotNull(createdUserProfile);
+ assertEquals(userInfo.getUsername().toLowerCase(), createdUserProfile.getUserId());
+ assertEquals(GATEWAY_ID,createdUserProfile.getGatewayId());
+ }
+ @Test
+ public void testInitializeUserProfileWhenUserProfileExists(@Mocked CustosSecurityManager mockedSecurityManager) throws UserProfileServiceException, CustosSecurityException{
+ UserInfo userInfo = new UserInfo();
+ userInfo.setUsername("test-username");
+ userInfo.setEmailAddress("some-other-test@test.com");
+ userInfo.setFirstName("someother-test-first-name");
+ userInfo.setLastName("someother-test-last-name");
+ String old_email_address = "test@test.com";
+ new MockUp<SecurityManagerFactory>() {
+ @Mock
+ public CustosSecurityManager getSecurityManager(){return mockedSecurityManager;};
+ };
+ new Expectations() {{ mockedSecurityManager.getUserInfoFromAuthzToken(authzToken); result = userInfo;}};
+
+ UserProfile createdUserProfile = userProfileServiceHandler.initializeUserProfile(authzToken);
+ assertNotNull(createdUserProfile);
+ assertEquals(old_email_address,createdUserProfile.getEmails().get(0));
+ assertEquals(userInfo.getUsername().toLowerCase(), createdUserProfile.getUserId());
+ assertEquals(GATEWAY_ID,createdUserProfile.getGatewayId());
+ }
+ @Test
+ public void testGetAllUserProfilesInGateway() throws UserProfileServiceException{
+ List<UserProfile> allGatewayUsers = userProfileServiceHandler.getAllUserProfilesInGateway(authzToken, GATEWAY_ID, 0, 0);
+ assertEquals(NUMBER_OF_USERS, allGatewayUsers.size());
+ }
+ @Test
+ public void testDoesUserExistWhenUserExists()throws UserProfileServiceException{
+ boolean shouldBeTrue = userProfileServiceHandler.doesUserExist(authzToken, "test-username", "test-gateway");
+ assertTrue(shouldBeTrue);
+ }
+ @Test
+ public void testDoesUserExistWhenUserDoesNotExist()throws UserProfileServiceException{
+ boolean shouldBeFalse = userProfileServiceHandler.doesUserExist(authzToken, "does-not-exist", "default");
+ assertFalse(shouldBeFalse);
+
+ }
+ @Test
+ //TODO: check
+ public void testDeleteUserProfile() throws UserProfileServiceException{
+ UserProfile userProfile = userProfileServiceHandler.deleteUserProfile(authzToken, NEW_USER_ID, GATEWAY_ID);
+ assertNotNull(userProfile);
+ assertEquals(NEW_USER_ID, userProfile.getUserId());
+ NUMBER_OF_USERS = 0;
+ //testGetAllUserProfilesInGateway();
+ }
+}
diff --git a/ide-integration/src/main/resources/custos-server.properties b/ide-integration/src/main/resources/custos-server.properties
index 4d75666..d9d59c3 100644
--- a/ide-integration/src/main/resources/custos-server.properties
+++ b/ide-integration/src/main/resources/custos-server.properties
@@ -54,8 +54,9 @@ in.memory.cache.size=1000
# Profile Service Configuration
###########################################################################
profile.service.server.host=0.0.0.0
-profile.service.server.port=8963
+profile.service.server.port=9092
profile_service=org.apache.custos.profile.server.ProfileServiceServer
+
# MariaDB properties
profile.service.jdbc.url=jdbc:mariadb://localhost:13306/profile_service
profile.service.jdbc.user=root
diff --git a/ide-integration/src/main/resources/docker-compose.yml b/ide-integration/src/main/resources/docker-compose.yml
index 7ede3bf..5ced9a8 100644
--- a/ide-integration/src/main/resources/docker-compose.yml
+++ b/ide-integration/src/main/resources/docker-compose.yml
@@ -23,5 +23,5 @@ services:
- ./database_scripts/init:/docker-entrypoint-initdb.d
- ./database_data:/var/lib/mysql
ports:
- - "13306:3307"
+ - "13306:3306"
command: ['mysqld', '--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci', '--sql_mode=']
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index 6979179..e0524c7 100644
--- a/pom.xml
+++ b/pom.xml
@@ -30,6 +30,8 @@
<log4j.version>1.2.17</log4j.version>
<thrift.version>0.12.0</thrift.version>
<openjpa.version>2.4.3</openjpa.version>
+ <junit.version>4.8.1</junit.version>
+ <jmockit.version>1.8</jmockit.version>
</properties>
<dependencies>
<dependency>