Search in sources :

Example 1 with UserNamespaceAuthorizationEntity

use of org.finra.herd.model.jpa.UserNamespaceAuthorizationEntity in project herd by FINRAOS.

the class UserNamespaceAuthorizationHelperTest method testBuildNamespaceAuthorizationsAssertAuthLookupByUserId.

@Test
public void testBuildNamespaceAuthorizationsAssertAuthLookupByUserId() {
    ApplicationUser applicationUser = new ApplicationUser(getClass());
    String userId = "userId";
    applicationUser.setUserId(userId);
    when(configurationHelper.getBooleanProperty(any())).thenReturn(true);
    List<UserNamespaceAuthorizationEntity> userNamespaceAuthorizationEntities = new ArrayList<>();
    UserNamespaceAuthorizationEntity userNamespaceAuthorizationEntity = new UserNamespaceAuthorizationEntity();
    userNamespaceAuthorizationEntity.setUserId("userNamespaceAuthorizationEntityUserId");
    NamespaceEntity namespaceEntity = new NamespaceEntity();
    namespaceEntity.setCode("namespace");
    userNamespaceAuthorizationEntity.setNamespace(namespaceEntity);
    userNamespaceAuthorizationEntities.add(userNamespaceAuthorizationEntity);
    when(userNamespaceAuthorizationDao.getUserNamespaceAuthorizationsByUserId(any())).thenReturn(userNamespaceAuthorizationEntities);
    userNamespaceAuthorizationHelper.buildNamespaceAuthorizations(applicationUser);
    assertEquals(1, applicationUser.getNamespaceAuthorizations().size());
    NamespaceAuthorization namespaceAuthorization = IterableUtils.get(applicationUser.getNamespaceAuthorizations(), 0);
    assertEquals(namespaceEntity.getCode(), namespaceAuthorization.getNamespace());
    verify(userNamespaceAuthorizationDao).getUserNamespaceAuthorizationsByUserId(eq(userId));
    verify(userNamespaceAuthorizationDao).getUserNamespaceAuthorizationsByUserIdStartsWith(eq(WildcardHelper.WILDCARD_TOKEN));
    verifyNoMoreInteractions(userNamespaceAuthorizationDao, wildcardHelper);
}
Also used : ApplicationUser(org.finra.herd.model.dto.ApplicationUser) NamespaceEntity(org.finra.herd.model.jpa.NamespaceEntity) ArrayList(java.util.ArrayList) UserNamespaceAuthorizationEntity(org.finra.herd.model.jpa.UserNamespaceAuthorizationEntity) NamespaceAuthorization(org.finra.herd.model.api.xml.NamespaceAuthorization) Test(org.junit.Test)

Example 2 with UserNamespaceAuthorizationEntity

use of org.finra.herd.model.jpa.UserNamespaceAuthorizationEntity in project herd by FINRAOS.

the class UserNamespaceAuthorizationHelperTest method testBuildNamespaceAuthorizationsAssertWildcardEntityNotAddedIfMatchFails.

@Test
public void testBuildNamespaceAuthorizationsAssertWildcardEntityNotAddedIfMatchFails() {
    ApplicationUser applicationUser = new ApplicationUser(getClass());
    String userId = "userId";
    applicationUser.setUserId(userId);
    when(configurationHelper.getBooleanProperty(any())).thenReturn(true);
    List<UserNamespaceAuthorizationEntity> wildcardEntities = new ArrayList<>();
    UserNamespaceAuthorizationEntity wildcardEntity = new UserNamespaceAuthorizationEntity();
    wildcardEntity.setUserId("wildcardEntityUserId");
    NamespaceEntity namespaceEntity = new NamespaceEntity();
    namespaceEntity.setCode("namespace");
    wildcardEntity.setNamespace(namespaceEntity);
    wildcardEntities.add(wildcardEntity);
    when(userNamespaceAuthorizationDao.getUserNamespaceAuthorizationsByUserIdStartsWith(any())).thenReturn(wildcardEntities);
    when(wildcardHelper.matches(any(), any())).thenReturn(false);
    userNamespaceAuthorizationHelper.buildNamespaceAuthorizations(applicationUser);
    assertEquals(0, applicationUser.getNamespaceAuthorizations().size());
    verify(userNamespaceAuthorizationDao).getUserNamespaceAuthorizationsByUserId(eq(userId));
    verify(userNamespaceAuthorizationDao).getUserNamespaceAuthorizationsByUserIdStartsWith(eq(WildcardHelper.WILDCARD_TOKEN));
    verify(wildcardHelper).matches(eq(userId.toUpperCase()), eq(wildcardEntity.getUserId().toUpperCase()));
    verifyNoMoreInteractions(userNamespaceAuthorizationDao, wildcardHelper);
}
Also used : ApplicationUser(org.finra.herd.model.dto.ApplicationUser) NamespaceEntity(org.finra.herd.model.jpa.NamespaceEntity) ArrayList(java.util.ArrayList) UserNamespaceAuthorizationEntity(org.finra.herd.model.jpa.UserNamespaceAuthorizationEntity) Test(org.junit.Test)

Example 3 with UserNamespaceAuthorizationEntity

use of org.finra.herd.model.jpa.UserNamespaceAuthorizationEntity in project herd by FINRAOS.

the class UserNamespaceAuthorizationDaoTest method testGetUserNamespaceAuthorizationsByUserId.

@Test
public void testGetUserNamespaceAuthorizationsByUserId() {
    // Create user namespace authorization keys. The keys are listed out of order to validate the order by logic.
    List<UserNamespaceAuthorizationKey> keys = Arrays.asList(new UserNamespaceAuthorizationKey(USER_ID_2, NAMESPACE_2), new UserNamespaceAuthorizationKey(USER_ID_2, NAMESPACE), new UserNamespaceAuthorizationKey(USER_ID, NAMESPACE_2), new UserNamespaceAuthorizationKey(USER_ID, NAMESPACE));
    // Create and persist the relative database entities.
    List<UserNamespaceAuthorizationEntity> userNamespaceAuthorizationEntities = new ArrayList<>();
    for (UserNamespaceAuthorizationKey key : keys) {
        userNamespaceAuthorizationEntities.add(userNamespaceAuthorizationDaoTestHelper.createUserNamespaceAuthorizationEntity(key, SUPPORTED_NAMESPACE_PERMISSIONS));
    }
    // Get user namespace authorizations by user id.
    assertEquals(Arrays.asList(userNamespaceAuthorizationEntities.get(3), userNamespaceAuthorizationEntities.get(2)), userNamespaceAuthorizationDao.getUserNamespaceAuthorizationsByUserId(USER_ID));
    // Test case insensitivity of the user id input parameter.
    assertEquals(Arrays.asList(userNamespaceAuthorizationEntities.get(3), userNamespaceAuthorizationEntities.get(2)), userNamespaceAuthorizationDao.getUserNamespaceAuthorizationsByUserId(USER_ID.toUpperCase()));
    assertEquals(Arrays.asList(userNamespaceAuthorizationEntities.get(3), userNamespaceAuthorizationEntities.get(2)), userNamespaceAuthorizationDao.getUserNamespaceAuthorizationsByUserId(USER_ID.toLowerCase()));
    // Try to retrieve user namespace authorization using a non-existing user id.
    assertEquals(new ArrayList<>(), userNamespaceAuthorizationDao.getUserNamespaceAuthorizationsByUserId(USER_ID_3));
}
Also used : UserNamespaceAuthorizationKey(org.finra.herd.model.api.xml.UserNamespaceAuthorizationKey) ArrayList(java.util.ArrayList) UserNamespaceAuthorizationEntity(org.finra.herd.model.jpa.UserNamespaceAuthorizationEntity) Test(org.junit.Test)

Example 4 with UserNamespaceAuthorizationEntity

use of org.finra.herd.model.jpa.UserNamespaceAuthorizationEntity in project herd by FINRAOS.

the class UserNamespaceAuthorizationDaoTestHelper method createUserNamespaceAuthorizationEntity.

/**
 * Creates and persists a new user namespace authorization entity.
 *
 * @param userId the user id
 * @param namespaceEntity the namespace entity
 * @param namespacePermissions the list of namespace permissions
 *
 * @return the newly created user namespace authorization entity
 */
public UserNamespaceAuthorizationEntity createUserNamespaceAuthorizationEntity(String userId, NamespaceEntity namespaceEntity, List<NamespacePermissionEnum> namespacePermissions) {
    UserNamespaceAuthorizationEntity userNamespaceAuthorizationEntity = new UserNamespaceAuthorizationEntity();
    userNamespaceAuthorizationEntity.setUserId(userId);
    userNamespaceAuthorizationEntity.setNamespace(namespaceEntity);
    userNamespaceAuthorizationEntity.setReadPermission(namespacePermissions.contains(NamespacePermissionEnum.READ));
    userNamespaceAuthorizationEntity.setWritePermission(namespacePermissions.contains(NamespacePermissionEnum.WRITE));
    userNamespaceAuthorizationEntity.setExecutePermission(namespacePermissions.contains(NamespacePermissionEnum.EXECUTE));
    userNamespaceAuthorizationEntity.setGrantPermission(namespacePermissions.contains(NamespacePermissionEnum.GRANT));
    userNamespaceAuthorizationEntity.setWriteDescriptiveContentPermission(namespacePermissions.contains(NamespacePermissionEnum.WRITE_DESCRIPTIVE_CONTENT));
    return userNamespaceAuthorizationDao.saveAndRefresh(userNamespaceAuthorizationEntity);
}
Also used : UserNamespaceAuthorizationEntity(org.finra.herd.model.jpa.UserNamespaceAuthorizationEntity)

Example 5 with UserNamespaceAuthorizationEntity

use of org.finra.herd.model.jpa.UserNamespaceAuthorizationEntity in project herd by FINRAOS.

the class UserNamespaceAuthorizationServiceImpl method getUserNamespaceAuthorization.

@NamespacePermission(fields = "#key?.namespace", permissions = NamespacePermissionEnum.READ)
@Override
public UserNamespaceAuthorization getUserNamespaceAuthorization(UserNamespaceAuthorizationKey key) {
    // Validate and trim the key.
    validateUserNamespaceAuthorizationKey(key);
    // Retrieve and ensure that a user namespace authorization exists with the specified key.
    UserNamespaceAuthorizationEntity userNamespaceAuthorizationEntity = getUserNamespaceAuthorizationEntity(key);
    // Create and return the user namespace authorization object from the persisted entity.
    return createUserNamespaceAuthorizationFromEntity(userNamespaceAuthorizationEntity);
}
Also used : UserNamespaceAuthorizationEntity(org.finra.herd.model.jpa.UserNamespaceAuthorizationEntity) NamespacePermission(org.finra.herd.model.annotation.NamespacePermission)

Aggregations

UserNamespaceAuthorizationEntity (org.finra.herd.model.jpa.UserNamespaceAuthorizationEntity)38 Test (org.junit.Test)25 UserNamespaceAuthorizationKey (org.finra.herd.model.api.xml.UserNamespaceAuthorizationKey)22 UserNamespaceAuthorization (org.finra.herd.model.api.xml.UserNamespaceAuthorization)18 NamespaceEntity (org.finra.herd.model.jpa.NamespaceEntity)12 UserNamespaceAuthorizations (org.finra.herd.model.api.xml.UserNamespaceAuthorizations)8 ArrayList (java.util.ArrayList)7 NamespacePermission (org.finra.herd.model.annotation.NamespacePermission)5 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)4 Predicate (javax.persistence.criteria.Predicate)4 UserNamespaceAuthorizationUpdateRequest (org.finra.herd.model.api.xml.UserNamespaceAuthorizationUpdateRequest)4 NamespaceAuthorization (org.finra.herd.model.api.xml.NamespaceAuthorization)3 ApplicationUser (org.finra.herd.model.dto.ApplicationUser)3 Order (javax.persistence.criteria.Order)2 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 AlreadyExistsException (org.finra.herd.model.AlreadyExistsException)1