@Test
 public void testPersistNotExist() {
   Mockito.when(userRoleDAO.getUserRoleByUserIdAndRoleId(any(Long.class), any(Long.class)))
       .thenReturn(null);
   RoleDO role = new RoleDO();
   Long roleId = Long.valueOf(3);
   role.setRoleId(roleId);
   UserDO user = new UserDO();
   Mockito.when(roleDAO.getById(any(Long.class))).thenReturn(role);
   Mockito.when(userDAO.getById(any(Long.class))).thenReturn(user);
   Mockito.when(roleDAO.findDelegatableRoles(any(Long.class), any(Long.class)))
       .thenReturn(Lists.newArrayList(role));
   userRoleService.persist(new ServiceContext(), Long.valueOf(1), roleId);
   Mockito.verify(userRoleDAO, times(1)).persist(any(UserRoleDO.class));
 }
 @Test(expected = AuthorizationException.class)
 public void testPersistRoleNotConferrable() {
   Mockito.when(userRoleDAO.getUserRoleByUserIdAndRoleId(any(Long.class), any(Long.class)))
       .thenReturn(null);
   RoleDO role = new RoleDO();
   Long roleId = Long.valueOf(3);
   role.setRoleId(roleId);
   UserDO user = new UserDO();
   Mockito.when(roleDAO.getById(any(Long.class))).thenReturn(role);
   Mockito.when(userDAO.getById(any(Long.class))).thenReturn(user);
   Mockito.when(roleDAO.findDelegatableRoles(any(Long.class), any(Long.class)))
       .thenReturn(Lists.<RoleDO>newArrayList());
   try {
     userRoleService.persist(new ServiceContext(), Long.valueOf(1), roleId);
   } finally {
     Mockito.verify(userRoleDAO, Mockito.never()).persist(any(UserRoleDO.class));
   }
 }
  @Test(expected = ValidationServiceException.class)
  public void testMergeAddNotDelegatable() {
    RoleDO delegatable = new RoleDO();
    delegatable.setRoleId(Long.valueOf(2));
    delegatable.setName("delgatable");

    Mockito.when(roleDAO.findDelegatableRoles(any(Long.class), any(Long.class)))
        .thenReturn(Lists.<RoleDO>newArrayList());
    Mockito.when(roleDAO.findRolesByCode(any(Long.class), any(List.class)))
        .thenReturn(Lists.newArrayList(delegatable));
    Mockito.when(userDAO.getById(Long.valueOf(5))).thenReturn(new UserDO());
    try {
      ServiceContext context = new ServiceContext();
      User user = new User();
      user.setUserId(Long.valueOf(5));
      context.setUser(user);
      userRoleService.mergeUserRoles(context, Long.valueOf(1), Lists.<String>newArrayList("foo"));
    } finally {
      Mockito.verify(userRoleDAO, Mockito.never()).persist(any(UserRoleDO.class));
    }
  }