@Test
  public void testGetUserProfileNodeCreateFalse() {
    // get the orion credentials service
    IOrionCredentialsService orionCredentialsService =
        UserServiceHelper.getDefault().getUserStore();

    // particulars for the new user
    String login = "******";
    String name = "Existing Node";
    String email = "*****@*****.**";
    String password = "******";

    // create user pattern is to persist the new user in the metadata store first
    UserInfo userInfo = new UserInfo();
    userInfo.setUserName(login);
    userInfo.setFullName(name);
    try {
      OrionConfiguration.getMetaStore().createUser(userInfo);
    } catch (CoreException e) {
      fail("Could not create user in IMetaStore: " + e.getLocalizedMessage());
    }

    // create the new user using the IOrionCredentialsService
    User user = new User(userInfo.getUniqueId(), login, name, password);
    user.setEmail(email);
    User createdUser = orionCredentialsService.createUser(user);

    // get the profile node
    IOrionUserProfileService orionUserProfileService =
        UserServiceHelper.getDefault().getUserProfileService();
    IOrionUserProfileNode userProfileNode =
        orionUserProfileService.getUserProfileNode(createdUser.getUid(), false);
    assertNotNull(userProfileNode);
  }
  @Test
  public void testGetUserProfileNodeCreateTrue() {
    // get the orion credentials service
    IOrionCredentialsService orionCredentialsService =
        UserServiceHelper.getDefault().getUserStore();

    // particulars for the new user
    String login = "******";
    String name = "Create Node";
    String email = "*****@*****.**";
    String password = "******";

    // create user pattern is to persist the new user in the metadata store first
    UserInfo userInfo = new UserInfo();
    userInfo.setUserName(login);
    userInfo.setFullName(name);
    try {
      OrionConfiguration.getMetaStore().createUser(userInfo);
    } catch (CoreException e) {
      fail("Could not create user in IMetaStore: " + e.getLocalizedMessage());
    }

    // create the new user using the IOrionCredentialsService
    User user = new User(userInfo.getUniqueId(), login, name, password);
    user.setEmail(email);
    User createdUser = orionCredentialsService.createUser(user);

    // get the profile node
    IOrionUserProfileService orionUserProfileService =
        UserServiceHelper.getDefault().getUserProfileService();
    IOrionUserProfileNode userProfileNode =
        orionUserProfileService.getUserProfileNode(createdUser.getUid(), true);

    IOrionUserProfileNode generalProfilePart =
        userProfileNode.getUserProfileNode(IOrionUserProfileConstants.GENERAL_PROFILE_PART);
    try {
      generalProfilePart.put("GitMail", email, false);
      generalProfilePart.put("GitName", name, false);
      generalProfilePart.flush();
    } catch (CoreException e) {
      fail("Could not update the user profile node: " + e.getLocalizedMessage());
    }

    // read back the profile node
    IOrionUserProfileNode readProfileNode =
        orionUserProfileService.getUserProfileNode(
            createdUser.getUid(), IOrionUserProfileConstants.GENERAL_PROFILE_PART);
    try {
      String gitMail = readProfileNode.get("GitMail", null);
      assertNotNull(gitMail);
      assertEquals(email, gitMail);
      String gitName = readProfileNode.get("GitName", null);
      assertNotNull(gitName);
      assertEquals(name, gitName);
    } catch (CoreException e) {
      fail("Could not read the user profile node: " + e.getLocalizedMessage());
    }
  }
  private boolean sendNotification(
      HttpServletRequest request,
      HttpServletResponse response,
      Repository db,
      String login,
      String commit,
      String url,
      String authorName,
      String message)
      throws ServletException, URISyntaxException, IOException, JSONException, CoreException,
          Exception {
    UserEmailUtil util = UserEmailUtil.getUtil();
    if (!util.isEmailConfigured()) {
      return statusHandler.handleRequest(
          request,
          response,
          new ServerStatus(
              IStatus.ERROR,
              HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
              "Smpt server not configured",
              null));
    }
    IOrionCredentialsService userAdmin = UserServiceHelper.getDefault().getUserStore();
    User user = (User) userAdmin.getUser(UserConstants.KEY_LOGIN, login);
    try {
      if (reviewRequestEmail == null) {
        reviewRequestEmail = new EmailContent(EMAIL_REVIEW_REQUEST_FILE);
      }

      String emailAdress = user.getEmail();

      util.sendEmail(
          reviewRequestEmail.getTitle(),
          reviewRequestEmail
              .getContent()
              .replaceAll(EMAIL_COMMITER_NAME, authorName)
              .replaceAll(EMAIL_URL_LINK, url)
              .replaceAll(EMAIL_COMMIT_MESSAGE, message),
          emailAdress);

      JSONObject result = new JSONObject();
      result.put(GitConstants.KEY_RESULT, "Email sent");
      OrionServlet.writeJSONResponse(
          request, response, result, JsonURIUnqualificationStrategy.ALL_NO_GIT);
      return true;
    } catch (Exception e) {
      return statusHandler.handleRequest(
          request,
          response,
          new ServerStatus(
              IStatus.ERROR,
              HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
              "User doesn't exist",
              null));
    }
  };
  @Test
  public void testGetUserProfileNode() {
    // get the orion credentials service
    IOrionCredentialsService orionCredentialsService =
        UserServiceHelper.getDefault().getUserStore();

    // particulars for the new user
    String login = "******";
    String name = "Profile Node";
    String email = "*****@*****.**";
    String password = "******";

    // create user pattern is to persist the new user in the metadata store first
    UserInfo userInfo = new UserInfo();
    userInfo.setUserName(login);
    userInfo.setFullName(name);
    try {
      OrionConfiguration.getMetaStore().createUser(userInfo);
    } catch (CoreException e) {
      fail("Could not create user in IMetaStore: " + e.getLocalizedMessage());
    }

    // create the new user using the IOrionCredentialsService
    User user = new User(userInfo.getUniqueId(), login, name, password);
    user.setEmail(email);
    User createdUser = orionCredentialsService.createUser(user);

    // get the profile node
    IOrionUserProfileService orionUserProfileService =
        UserServiceHelper.getDefault().getUserProfileService();
    IOrionUserProfileNode userProfileNode =
        orionUserProfileService.getUserProfileNode(
            createdUser.getUid(), IOrionUserProfileConstants.GENERAL_PROFILE_PART);

    // set the last login timestamp
    String lastLogin = new Long(System.currentTimeMillis()).toString();
    try {
      userProfileNode.put(IOrionUserProfileConstants.LAST_LOGIN_TIMESTAMP, lastLogin, false);
      userProfileNode.flush();
    } catch (CoreException e) {
      fail("Could not put the user profile node: " + e.getLocalizedMessage());
    }

    // read back the profile node
    IOrionUserProfileNode readProfileNode =
        orionUserProfileService.getUserProfileNode(
            createdUser.getUid(), IOrionUserProfileConstants.GENERAL_PROFILE_PART);
    try {
      String readLastLogin =
          readProfileNode.get(IOrionUserProfileConstants.LAST_LOGIN_TIMESTAMP, null);
      assertNotNull(readLastLogin);
      assertEquals(lastLogin, readLastLogin);
    } catch (CoreException e) {
      fail("Could not read the user profile node: " + e.getLocalizedMessage());
    }
  }
 private User getUserForCredentials(String login, String password) {
   IOrionCredentialsService userAdmin = defaultUserAdmin;
   if (userAdmin == null) {
     LogHelper.log(
         new Status(
             IStatus.ERROR,
             Activator.PI_SERVER_AUTHENTICATION_BASIC,
             "User admin server is not available"));
     return null;
   }
   User user = userAdmin.getUser("login", login); // $NON-NLS-1$
   if (user != null && user.hasCredential("password", password)) { // $NON-NLS-1$
     return user;
   }
   return null;
 }
  public String getAuthenticatedUser(
      HttpServletRequest req, HttpServletResponse resp, Properties properties) throws IOException {
    String authHead = req.getHeader("Authorization"); // $NON-NLS-1$

    if (authHead != null && authHead.toUpperCase(Locale.ENGLISH).startsWith(getAuthType())) {
      String base64 = authHead.substring(6);
      String authString = new String(Base64.decode(base64.getBytes()));
      if (authString.indexOf(':') < 0) {
        return null;
      }

      String login = authString.substring(0, authString.indexOf(':'));
      String password = authString.substring(authString.indexOf(':') + 1);
      User user = getUserForCredentials(login, password);
      if (user != null) {
        Authorization authorization = defaultUserAdmin.getAuthorization(user);
        // TODO handle authorization
        return user.getUid();
      }
    }
    return null;
  }
  @Test
  public void testGetUserNames() {
    // get the orion credentials service
    IOrionCredentialsService orionCredentialsService =
        UserServiceHelper.getDefault().getUserStore();

    // particulars for the new user
    String login = "******";
    String name = "Test Names";
    String email = "*****@*****.**";
    String password = "******";

    // create user pattern is to persist the new user in the metadata store first
    UserInfo userInfo = new UserInfo();
    userInfo.setUserName(login);
    userInfo.setFullName(name);
    try {
      OrionConfiguration.getMetaStore().createUser(userInfo);
    } catch (CoreException e) {
      fail("Could not create user in IMetaStore: " + e.getLocalizedMessage());
    }

    // create the new user using the IOrionCredentialsService
    User user = new User(userInfo.getUniqueId(), login, name, password);
    user.setEmail(email);
    User createdUser = orionCredentialsService.createUser(user);
    assertNotNull(createdUser);

    // get the profile node
    IOrionUserProfileService orionUserProfileService =
        UserServiceHelper.getDefault().getUserProfileService();
    IOrionUserProfileNode userProfileNode =
        orionUserProfileService.getUserProfileNode(
            createdUser.getUid(), IOrionUserProfileConstants.GENERAL_PROFILE_PART);

    // set the last login timestamp
    String lastLogin = new Long(System.currentTimeMillis()).toString();
    try {
      userProfileNode.put(IOrionUserProfileConstants.LAST_LOGIN_TIMESTAMP, lastLogin, false);
      userProfileNode.flush();
    } catch (CoreException e) {
      fail("Could not put the user profile node: " + e.getLocalizedMessage());
    }

    // get the profile nodes
    orionUserProfileService = UserServiceHelper.getDefault().getUserProfileService();
    String[] userNames = orionUserProfileService.getUserNames();
    List<String> userNamesList = new ArrayList<String>(Arrays.asList(userNames));
    assertTrue(userNamesList.contains(userInfo.getUniqueId()));

    for (String userName : userNamesList) {
      if (userName.equals(userInfo.getUniqueId())) {
        IOrionUserProfileNode readUserProfileNode =
            orionUserProfileService.getUserProfileNode(userName, false);
        String[] childrenNames = readUserProfileNode.childrenNames();
        List<String> childrenNamesList = new ArrayList<String>(Arrays.asList(childrenNames));
        assertTrue(childrenNamesList.contains(IOrionUserProfileConstants.GENERAL_PROFILE_PART));
        assertEquals(1, childrenNamesList.size());
        for (String profilePart : childrenNamesList) {
          assertEquals(IOrionUserProfileConstants.GENERAL_PROFILE_PART, profilePart);
          IOrionUserProfileNode profilePartNode =
              readUserProfileNode.getUserProfileNode(profilePart);
          assertNotNull(profilePartNode);
          try {
            String readLastLogin =
                profilePartNode.get(IOrionUserProfileConstants.LAST_LOGIN_TIMESTAMP, null);
            assertEquals(lastLogin, readLastLogin);
          } catch (CoreException e) {
            fail("Could not read the user profile node: " + e.getLocalizedMessage());
          }
        }
      }
    }
  }