private void updateResourcesTest(Authentication authentication, String requestedUsername, Map<String, Object> updates, boolean passwordAlreadyExists) throws Exception {
    Injector injector = createInjector();

    Capture<Request> requestCapture = newCapture(CaptureType.FIRST);
    Capture<Predicate> predicateCapture = newCapture(CaptureType.FIRST);
    boolean hasUpdates = false;

    ResourceProviderFactory factory = createMock(ResourceProviderFactory.class);

    UserEntity userEntity = createMock(UserEntity.class);
    expect(userEntity.getUserName()).andReturn(requestedUsername).anyTimes();
    expect(userEntity.getActive()).andReturn(true).anyTimes();
    expect(userEntity.getDisplayName()).andReturn(requestedUsername).anyTimes();
    expect(userEntity.getLocalUsername()).andReturn(requestedUsername).anyTimes();

    if (updates.containsKey(UserResourceProvider.USER_DISPLAY_NAME_PROPERTY_ID)) {
      userEntity.setDisplayName((String) updates.get(UserResourceProvider.USER_DISPLAY_NAME_PROPERTY_ID));
      expectLastCall().once();
      hasUpdates = true;
    }

    if (updates.containsKey(UserResourceProvider.USER_LOCAL_USERNAME_PROPERTY_ID)) {
      userEntity.setLocalUsername((String) updates.get(UserResourceProvider.USER_LOCAL_USERNAME_PROPERTY_ID));
      expectLastCall().once();
      hasUpdates = true;
    }

    if (updates.containsKey(UserResourceProvider.USER_ACTIVE_PROPERTY_ID)) {
      userEntity.setActive((Boolean) updates.get(UserResourceProvider.USER_ACTIVE_PROPERTY_ID));
      expectLastCall().once();
      hasUpdates = true;
    }

    UserDAO userDAO = injector.getInstance(UserDAO.class);
    expect(userDAO.findUserByName(requestedUsername)).andReturn(userEntity).once();

    if (hasUpdates) {
      expect(userDAO.merge(userEntity)).andReturn(userEntity).once();
    }

    if (updates.get(UserResourceProvider.USER_ADMIN_PROPERTY_ID) != null) {
      Boolean isAdmin = Boolean.TRUE.equals(updates.get(UserResourceProvider.USER_ADMIN_PROPERTY_ID));

      if (isAdmin) {
        PermissionEntity permissionEntity = createMock(PermissionEntity.class);
        PermissionDAO permissionDAO = injector.getInstance(PermissionDAO.class);
        expect(permissionDAO.findAmbariAdminPermission()).andReturn(permissionEntity).once();

        ResourceEntity resourceEntity = createMock(ResourceEntity.class);
        ResourceDAO resourceDAO = injector.getInstance(ResourceDAO.class);
        expect(resourceDAO.findAmbariResource()).andReturn(resourceEntity).once();

        PrivilegeDAO privilegeDAO = injector.getInstance(PrivilegeDAO.class);
        privilegeDAO.create(anyObject(PrivilegeEntity.class));
        expectLastCall().andAnswer(new IAnswer<Object>() {
          @Override
          public Object answer() throws Throwable {
            Object[] args = getCurrentArguments();

            ((PrivilegeEntity) args[0]).setId(1);
            return null;
          }
        }).once();

        PrincipalDAO principalDAO = injector.getInstance(PrincipalDAO.class);
        expect(principalDAO.merge(anyObject(PrincipalEntity.class))).andReturn(null).once();

        PrincipalEntity principalEntity = createMock(PrincipalEntity.class);
        expect(principalEntity.getPrivileges()).andReturn(new HashSet<PrivilegeEntity>()).anyTimes();

        expect(userEntity.getPrincipal()).andReturn(principalEntity).anyTimes();

        expect(userDAO.merge(anyObject(UserEntity.class))).andReturn(null).once();
      }
    }

    if (updates.containsKey(UserResourceProvider.USER_PASSWORD_PROPERTY_ID)) {
      if(passwordAlreadyExists) {
        UserAuthenticationEntity authenticationEntity = createMock(UserAuthenticationEntity.class);
        expect(authenticationEntity.getUserAuthenticationId()).andReturn(100L).anyTimes();
        expect(authenticationEntity.getAuthenticationType()).andReturn(UserAuthenticationType.LOCAL).anyTimes();

        expect(userEntity.getAuthenticationEntities()).andReturn(Collections.singletonList(authenticationEntity)).once();
      }
      else {
        expect(userEntity.getAuthenticationEntities()).andReturn(Collections.<UserAuthenticationEntity>emptyList()).once();
      }

      RequestStatus status = createMock(RequestStatus.class);

      ResourceProvider resourceProvider = createMock(ResourceProvider.class);

      if(passwordAlreadyExists) {
        expect(resourceProvider.updateResources(capture(requestCapture), capture(predicateCapture))).andReturn(status).once();
      }
      else {
        expect(resourceProvider.createResources(capture(requestCapture))).andReturn(status).once();
      }

      expect(factory.getUserAuthenticationSourceResourceProvider()).andReturn(resourceProvider).once();
    }

    AmbariEventPublisher publisher = createNiceMock(AmbariEventPublisher.class);

    replayAll();

    AmbariMetaInfo ambariMetaInfo = injector.getInstance(AmbariMetaInfo.class);
    ambariMetaInfo.init();

    ViewRegistry.initInstance(new ViewRegistry(publisher));
    AbstractControllerResourceProvider.init(factory);
    SecurityContextHolder.getContext().setAuthentication(authentication);

    ResourceProvider provider = getResourceProvider(injector);

    // create the request
    Request request = PropertyHelper.getUpdateRequest(updates, null);

    provider.updateResources(request, createPredicate(requestedUsername));

    verifyAll();

    if (updates.containsKey(UserResourceProvider.USER_PASSWORD_PROPERTY_ID)) {
      // Verify that the correct request was issued to update update the user's password...
      Request capturedRequest = requestCapture.getValue();
      Set<Map<String, Object>> capturedProperties = capturedRequest.getProperties();
      Map<String, Object> properties = capturedProperties.iterator().next();
      Assert.assertNotNull(capturedProperties);
      if(passwordAlreadyExists) {
        Assert.assertEquals(updates.get(UserResourceProvider.USER_PASSWORD_PROPERTY_ID), properties.get(UserAuthenticationSourceResourceProvider.AUTHENTICATION_KEY_PROPERTY_ID));
        Assert.assertEquals(updates.get(UserResourceProvider.USER_OLD_PASSWORD_PROPERTY_ID), properties.get(UserAuthenticationSourceResourceProvider.AUTHENTICATION_OLD_KEY_PROPERTY_ID));
      }
      else {
        Assert.assertEquals(updates.get(UserResourceProvider.USER_PASSWORD_PROPERTY_ID), properties.get(UserAuthenticationSourceResourceProvider.AUTHENTICATION_KEY_PROPERTY_ID));
        Assert.assertEquals(UserAuthenticationType.LOCAL.name(), properties.get(UserAuthenticationSourceResourceProvider.AUTHENTICATION_AUTHENTICATION_TYPE_PROPERTY_ID));
        Assert.assertEquals(requestedUsername, properties.get(UserAuthenticationSourceResourceProvider.AUTHENTICATION_USER_NAME_PROPERTY_ID));
      }

      if(passwordAlreadyExists) {
        Predicate capturedPredicate = predicateCapture.getValue();
        Assert.assertEquals(AndPredicate.class, capturedPredicate.getClass());
        AndPredicate andPredicate = (AndPredicate) capturedPredicate;
        Predicate[] predicates = andPredicate.getPredicates();
        Assert.assertEquals(2, predicates.length);
        for (Predicate p : predicates) {
          Assert.assertEquals(EqualsPredicate.class, p.getClass());
          EqualsPredicate equalsPredicate = (EqualsPredicate) p;

          if (UserAuthenticationSourceResourceProvider.AUTHENTICATION_USER_NAME_PROPERTY_ID.equals(equalsPredicate.getPropertyId())) {
            Assert.assertEquals(requestedUsername, equalsPredicate.getValue());
          } else if (UserAuthenticationSourceResourceProvider.AUTHENTICATION_AUTHENTICATION_SOURCE_ID_PROPERTY_ID.equals(equalsPredicate.getPropertyId())) {
            Assert.assertEquals("100", equalsPredicate.getValue());
          }
        }
      }
      else {
        Assert.assertFalse(predicateCapture.hasCaptured());
      }
    }
  }
Test give a user Ambari administrative rights by assigning the user to the AMBARI.ADMINISTRATOR role
when creating the account. This is for backward compatibility to maintain the REST API V1 contract.
ambari-server/src/test/java/org/apache/ambari/server/controller/internal/UserResourceProviderTest.java