@Test public void testGetResources() throws Exception { ViewEntity viewDefinition = ViewEntityTest.getViewEntity(); ViewInstanceEntity viewInstanceDefinition = ViewInstanceEntityTest.getViewInstanceEntity(); viewDefinition.addInstanceDefinition(viewInstanceDefinition); viewInstanceDefinition.setViewEntity(viewDefinition); viewDefinition.setStatus(ViewDefinition.ViewStatus.DEPLOYED); ViewRegistry registry = ViewRegistry.getInstance(); registry.addDefinition(viewDefinition); registry.addInstanceDefinition(viewDefinition, viewInstanceDefinition); List<PrivilegeEntity> privilegeEntities = new LinkedList<PrivilegeEntity>(); PrivilegeEntity privilegeEntity = createNiceMock(PrivilegeEntity.class); ResourceEntity resourceEntity = createNiceMock(ResourceEntity.class); UserEntity userEntity = createNiceMock(UserEntity.class); PrincipalEntity principalEntity = createNiceMock(PrincipalEntity.class); PrincipalTypeEntity principalTypeEntity = createNiceMock(PrincipalTypeEntity.class); PermissionEntity permissionEntity = createNiceMock(PermissionEntity.class); List<PrincipalEntity> principalEntities = new LinkedList<PrincipalEntity>(); principalEntities.add(principalEntity); List<UserEntity> userEntities = new LinkedList<UserEntity>(); userEntities.add(userEntity); privilegeEntities.add(privilegeEntity); expect(privilegeDAO.findAll()).andReturn(privilegeEntities); expect(privilegeEntity.getResource()).andReturn(resourceEntity).anyTimes(); expect(privilegeEntity.getPrincipal()).andReturn(principalEntity).anyTimes(); expect(privilegeEntity.getPermission()).andReturn(permissionEntity).anyTimes(); expect(resourceEntity.getId()).andReturn(20L).anyTimes(); expect(principalEntity.getId()).andReturn(20L).anyTimes(); expect(userEntity.getPrincipal()).andReturn(principalEntity).anyTimes(); expect(userEntity.getUserName()).andReturn("joe").anyTimes(); expect(permissionEntity.getPermissionName()).andReturn("VIEW.USER").anyTimes(); expect(permissionEntity.getPermissionLabel()).andReturn("View User").anyTimes(); expect(principalEntity.getPrincipalType()).andReturn(principalTypeEntity).anyTimes(); expect(principalTypeEntity.getName()).andReturn("USER").anyTimes(); expect(permissionDAO.findById(PermissionEntity.VIEW_USER_PERMISSION)).andReturn(permissionEntity); expect(userDAO.findUsersByPrincipal(principalEntities)).andReturn(userEntities); replay(privilegeDAO, userDAO, groupDAO, principalDAO, permissionDAO, resourceDAO, privilegeEntity, resourceEntity, userEntity, principalEntity, permissionEntity, principalTypeEntity); SecurityContextHolder.getContext().setAuthentication(TestAuthenticationFactory.createAdministrator("admin")); PrivilegeResourceProvider provider = new ViewPrivilegeResourceProvider(); Set<Resource> resources = provider.getResources(PropertyHelper.getReadRequest(), null); Assert.assertEquals(1, resources.size()); Resource resource = resources.iterator().next(); Assert.assertEquals("VIEW.USER", resource.getPropertyValue(AmbariPrivilegeResourceProvider.PERMISSION_NAME_PROPERTY_ID)); Assert.assertEquals("View User", resource.getPropertyValue(AmbariPrivilegeResourceProvider.PERMISSION_LABEL_PROPERTY_ID)); Assert.assertEquals("joe", resource.getPropertyValue(AmbariPrivilegeResourceProvider.PRINCIPAL_NAME_PROPERTY_ID)); Assert.assertEquals("USER", resource.getPropertyValue(AmbariPrivilegeResourceProvider.PRINCIPAL_TYPE_PROPERTY_ID)); verify(privilegeDAO, userDAO, groupDAO, principalDAO, permissionDAO, resourceDAO, privilegeEntity, resourceEntity, userEntity, principalEntity, permissionEntity, principalTypeEntity); }
@Override protected PermissionEntity getPermission(String permissionName, ResourceEntity resourceEntity) throws AmbariException { return (permissionName.equals(PermissionEntity.VIEW_USER_PERMISSION_NAME)) ? viewUsePermission : super.getPermission(permissionName, resourceEntity); }
ViewPrivilegeResourceProvider extends PrivilegeResourceProvider<ViewInstanceEntity> { @Override protected PermissionEntity getPermission(String permissionName, ResourceEntity resourceEntity) throws AmbariException { return (permissionName.equals(PermissionEntity.VIEW_USER_PERMISSION_NAME)) ? viewUsePermission : super.getPermission(permissionName, resourceEntity); } }
ViewPrivilegeResourceProvider extends PrivilegeResourceProvider<ViewInstanceEntity> { @Override protected PermissionEntity getPermission(String permissionName, ResourceEntity resourceEntity) throws AmbariException { return (permissionName.equals(PermissionEntity.VIEW_USER_PERMISSION_NAME)) ? viewUsePermission : super.getPermission(permissionName, resourceEntity); } ViewPrivilegeResourceProvider(); }
ViewPrivilegeResourceProvider extends PrivilegeResourceProvider<ViewInstanceEntity> { @Override protected PermissionEntity getPermission(String permissionName, ResourceEntity resourceEntity) throws AmbariException { return (permissionName.equals(PermissionEntity.VIEW_USER_PERMISSION_NAME)) ? viewUsePermission : super.getPermission(permissionName, resourceEntity); } ViewPrivilegeResourceProvider(); @Override Map<Resource.Type, String> getKeyPropertyIds(); @Override Map<Long, ViewInstanceEntity> getResourceEntities(Map<String, Object> properties); @Override Long getResourceEntityId(Predicate predicate); }
ViewPrivilegeResourceProvider extends PrivilegeResourceProvider<ViewInstanceEntity> { @Override protected PermissionEntity getPermission(String permissionName, ResourceEntity resourceEntity) throws AmbariException { return (permissionName.equals(PermissionEntity.VIEW_USER_PERMISSION_NAME)) ? viewUsePermission : super.getPermission(permissionName, resourceEntity); } ViewPrivilegeResourceProvider(); @Override Map<Resource.Type, String> getKeyPropertyIds(); @Override Map<Long, ViewInstanceEntity> getResourceEntities(Map<String, Object> properties); @Override Long getResourceEntityId(Predicate predicate); static final String PRIVILEGE_VIEW_NAME_PROPERTY_ID; static final String PRIVILEGE_VIEW_VERSION_PROPERTY_ID; static final String PRIVILEGE_INSTANCE_NAME_PROPERTY_ID; }