@Test
  public void shouldRegisterUser() throws Exception {
    String email = "*****@*****.**";

    UserResult userResult;
    Client client = createClient();
    WebResource webResource = client.resource(ringringServerApi.getUrl() + "/user");

    // Create request hash with email address
    HashMap<String, String> requestHash = new HashMap<String, String>();
    requestHash.put("email", email);

    // First email registration
    userResult =
        webResource
            .type(MediaType.APPLICATION_JSON_TYPE)
            .accept(MediaType.APPLICATION_JSON_TYPE)
            .post(UserResult.class, requestHash);

    // Check if email sent to the user
    MimeMessage message = getLastMail().getMimeMessage();
    assertEquals(configManager.getSmtpFrom(), message.getFrom()[0].toString());
    assertEquals(email, message.getRecipients(Message.RecipientType.TO)[0].toString());

    // User needs to be registered
    assertEquals(Status.OKAY, userResult.getStatus());
    assertTrue(userResult.isSuccess());

    User registeredUser = userResult.getUser();
    assertEquals(email, registeredUser.getEmail());
    assertFalse(registeredUser.getIsActivated());
    assertFalse(registeredUser.getIsLoggedIn());

    // Try to register the same email again
    userResult =
        webResource
            .type(MediaType.APPLICATION_JSON_TYPE)
            .accept(MediaType.APPLICATION_JSON_TYPE)
            .post(UserResult.class, requestHash);

    // Should be failed with email already registered exception
    assertEquals(Status.EMAIL_ALREADY_REGISTERED, userResult.getStatus());
    assertFalse(userResult.isSuccess());

    assertNull(userResult.getUser());
  }
  @Test
  public void shouldNotActivateUser() throws Exception {
    String email = "*****@*****.**";

    UserResult userResult;
    Client client = createClient();
    WebResource webResource = client.resource(ringringServerApi.getUrl() + "/user");

    // Create request hash with email address
    HashMap<String, String> requestHash = new HashMap<String, String>();
    requestHash.put("email", email);

    // First email registration
    userResult =
        webResource
            .type(MediaType.APPLICATION_JSON_TYPE)
            .accept(MediaType.APPLICATION_JSON_TYPE)
            .post(UserResult.class, requestHash);

    // User needs to registered
    assertEquals(Status.OKAY, userResult.getStatus());
    assertTrue(userResult.isSuccess());

    // Check if email sent to the user
    MimeMessage message = getLastMail().getMimeMessage();
    assertEquals(configManager.getSmtpFrom(), message.getFrom()[0].toString());
    assertEquals(email, message.getRecipients(Message.RecipientType.TO)[0].toString());

    User registeredUser = userResult.getUser();
    assertEquals(email, registeredUser.getEmail());
    assertFalse(registeredUser.getIsActivated());
    assertFalse(registeredUser.getIsLoggedIn());

    // 1st try: email in the path is not matching to the user's email
    webResource = client.resource(ringringServerApi.getUrl() + "/user/[email protected]");
    StatusResult statusResult =
        webResource
            .type(MediaType.APPLICATION_JSON_TYPE)
            .accept(MediaType.APPLICATION_JSON_TYPE)
            .put(StatusResult.class, registeredUser);

    assertEquals(Status.INVALID_EMAIL, statusResult.getStatus());

    // 2nd try: Incorrect activation code
    registeredUser.setActivationCode("SOME_FALSE_ACTIVATION_CODE");

    webResource =
        client.resource(ringringServerApi.getUrl() + "/user/" + registeredUser.getEmail());
    statusResult =
        webResource
            .type(MediaType.APPLICATION_JSON_TYPE)
            .accept(MediaType.APPLICATION_JSON_TYPE)
            .put(StatusResult.class, registeredUser);

    assertEquals(Status.INVALID_ACTIVATION_CODE, statusResult.getStatus());
  }
  @Test
  public void shouldActivateUser() throws Exception {
    String email = "*****@*****.**";

    UserResult userResult;
    Client client = createClient();
    WebResource webResource = client.resource(ringringServerApi.getUrl() + "/user");

    // Create request hash with email address
    HashMap<String, String> requestHash = new HashMap<String, String>();
    requestHash.put("email", email);

    // First email registration
    userResult =
        webResource
            .type(MediaType.APPLICATION_JSON_TYPE)
            .accept(MediaType.APPLICATION_JSON_TYPE)
            .post(UserResult.class, requestHash);

    // Check if email sent to the user
    MimeMessage message = getLastMail().getMimeMessage();
    assertEquals(configManager.getSmtpFrom(), message.getFrom()[0].toString());
    assertEquals(email, message.getRecipients(Message.RecipientType.TO)[0].toString());

    // User needs to registered
    assertEquals(Status.OKAY, userResult.getStatus());
    assertTrue(userResult.isSuccess());

    User registeredUser = userResult.getUser();
    assertEquals(email, registeredUser.getEmail());
    assertFalse(registeredUser.getIsActivated());
    assertFalse(registeredUser.getIsLoggedIn());

    // The activation code is not sent in the REST response. Get it from the email
    String activationCode = getActivationCodeFromEmailContent(message.getContent().toString());
    registeredUser.setActivationCode(activationCode);

    // Activate the user
    webResource =
        client.resource(ringringServerApi.getUrl() + "/user/" + registeredUser.getEmail());
    StatusResult statusresult =
        webResource
            .type(MediaType.APPLICATION_JSON_TYPE)
            .accept(MediaType.APPLICATION_JSON_TYPE)
            .put(StatusResult.class, registeredUser);

    assertEquals(Status.OKAY, statusresult.getStatus());
  }
  @Test
  public void shouldGetUsersByEmailList() throws Exception {
    String email1 = "*****@*****.**";
    String email2 = "*****@*****.**";
    String email3 = "*****@*****.**";

    UserResult userResult;
    Client client = createClient();
    WebResource webResource = client.resource(ringringServerApi.getUrl() + "/user");

    // Create request hash with email address
    HashMap<String, String> requestHash = new HashMap<String, String>();
    requestHash.put("email", email1);

    // First email registration
    userResult =
        webResource
            .type(MediaType.APPLICATION_JSON_TYPE)
            .accept(MediaType.APPLICATION_JSON_TYPE)
            .post(UserResult.class, requestHash);
    assertEquals(Status.OKAY, userResult.getStatus());
    User user1 = userResult.getUser();

    // Get first activation code from email
    MimeMessage message1 = getLastMail().getMimeMessage();
    String activationCode1 = getActivationCodeFromEmailContent(message1.getContent().toString());
    user1.setActivationCode(activationCode1);

    // Second email registration
    requestHash.clear();
    requestHash.put("email", email2);

    userResult =
        webResource
            .type(MediaType.APPLICATION_JSON_TYPE)
            .accept(MediaType.APPLICATION_JSON_TYPE)
            .post(UserResult.class, requestHash);
    assertEquals(Status.OKAY, userResult.getStatus());
    User user2 = userResult.getUser();

    // Get second activation code from email
    MimeMessage message2 = getLastMail().getMimeMessage();
    String activationCode2 = getActivationCodeFromEmailContent(message2.getContent().toString());
    user2.setActivationCode(activationCode2);

    // Third email registration
    requestHash.clear();
    requestHash.put("email", email3);

    userResult =
        webResource
            .type(MediaType.APPLICATION_JSON_TYPE)
            .accept(MediaType.APPLICATION_JSON_TYPE)
            .post(UserResult.class, requestHash);
    assertEquals(Status.OKAY, userResult.getStatus());
    User user3 = userResult.getUser();

    // Get first activation code from email
    MimeMessage message3 = getLastMail().getMimeMessage();
    String activationCode3 = getActivationCodeFromEmailContent(message3.getContent().toString());
    user3.setActivationCode(activationCode3);

    // Activate two users from the three (1st and 3rd)
    webResource = client.resource(ringringServerApi.getUrl() + "/user/" + user1.getEmail());
    StatusResult statusResult =
        webResource
            .type(MediaType.APPLICATION_JSON_TYPE)
            .accept(MediaType.APPLICATION_JSON_TYPE)
            .put(StatusResult.class, user1);
    assertEquals(Status.OKAY, statusResult.getStatus());

    webResource = client.resource(ringringServerApi.getUrl() + "/user/" + user3.getEmail());
    statusResult =
        webResource
            .type(MediaType.APPLICATION_JSON_TYPE)
            .accept(MediaType.APPLICATION_JSON_TYPE)
            .put(StatusResult.class, user3);
    assertEquals(Status.OKAY, statusResult.getStatus());

    // Get the user list
    // Create request hash with email address
    HashMap<String, List<String>> emailsRequestHash = new HashMap<String, List<String>>();

    List<String> userEmails =
        Arrays.asList(
            "*****@*****.**",
            "*****@*****.**",
            "*****@*****.**",
            "*****@*****.**");
    emailsRequestHash.put("emails", userEmails);

    webResource = client.resource(ringringServerApi.getUrl() + "/user/list");
    UserListResult userListResult =
        webResource
            .type(MediaType.APPLICATION_JSON_TYPE)
            .accept(MediaType.APPLICATION_JSON_TYPE)
            .post(UserListResult.class, emailsRequestHash);

    // Should be only two activated users from all of them
    assertEquals(2, userListResult.getUsers().size());
  }
  @Test
  public void shouldGetUserByEmail() throws Exception {
    String email1 = "*****@*****.**";
    String email2 = "*****@*****.**";
    String email3 = "*****@*****.**";
    String email4 = "*****@*****.**";

    UserResult userResult;
    Client client = createClient();
    WebResource webResource = client.resource(ringringServerApi.getUrl() + "/user");

    // Create request hash with email address
    HashMap<String, String> requestHash = new HashMap<String, String>();
    requestHash.put("email", email1);

    // First email registration
    userResult =
        webResource
            .type(MediaType.APPLICATION_JSON_TYPE)
            .accept(MediaType.APPLICATION_JSON_TYPE)
            .post(UserResult.class, requestHash);
    assertEquals(Status.OKAY, userResult.getStatus());
    User user1 = userResult.getUser();

    // Get first activation code from email
    MimeMessage message1 = getLastMail().getMimeMessage();
    String activationCode1 = getActivationCodeFromEmailContent(message1.getContent().toString());
    user1.setActivationCode(activationCode1);

    // Second email registration
    requestHash.clear();
    requestHash.put("email", email2);

    userResult =
        webResource
            .type(MediaType.APPLICATION_JSON_TYPE)
            .accept(MediaType.APPLICATION_JSON_TYPE)
            .post(UserResult.class, requestHash);
    assertEquals(Status.OKAY, userResult.getStatus());
    User user2 = userResult.getUser();

    // Get second activation code from email
    MimeMessage message2 = getLastMail().getMimeMessage();
    String activationCode2 = getActivationCodeFromEmailContent(message2.getContent().toString());
    user2.setActivationCode(activationCode2);

    // Third email registration
    requestHash.clear();
    requestHash.put("email", email3);

    userResult =
        webResource
            .type(MediaType.APPLICATION_JSON_TYPE)
            .accept(MediaType.APPLICATION_JSON_TYPE)
            .post(UserResult.class, requestHash);
    assertEquals(Status.OKAY, userResult.getStatus());
    User user3 = userResult.getUser();

    // Get third activation code from email
    MimeMessage message3 = getLastMail().getMimeMessage();
    String activationCode3 = getActivationCodeFromEmailContent(message3.getContent().toString());
    user3.setActivationCode(activationCode3);

    // Activate two users from the three (1st and 3rd)
    webResource = client.resource(ringringServerApi.getUrl() + "/user/" + user1.getEmail());
    StatusResult statusResult =
        webResource
            .type(MediaType.APPLICATION_JSON_TYPE)
            .accept(MediaType.APPLICATION_JSON_TYPE)
            .put(StatusResult.class, user1);
    assertEquals(Status.OKAY, statusResult.getStatus());

    webResource = client.resource(ringringServerApi.getUrl() + "/user/" + user3.getEmail());
    statusResult =
        webResource
            .type(MediaType.APPLICATION_JSON_TYPE)
            .accept(MediaType.APPLICATION_JSON_TYPE)
            .put(StatusResult.class, user3);
    assertEquals(Status.OKAY, statusResult.getStatus());

    // Tyring to get all users one by one
    // 1st user should registered and activated
    webResource = client.resource(ringringServerApi.getUrl() + "/user/" + email1);
    userResult =
        webResource
            .type(MediaType.APPLICATION_JSON_TYPE)
            .accept(MediaType.APPLICATION_JSON_TYPE)
            .get(UserResult.class);
    assertEquals(Status.OKAY, userResult.getStatus());
    assertTrue(userResult.getUser().getIsActivated());

    // 2nd user should not be registered
    webResource = client.resource(ringringServerApi.getUrl() + "/user/" + email2);
    userResult =
        webResource
            .type(MediaType.APPLICATION_JSON_TYPE)
            .accept(MediaType.APPLICATION_JSON_TYPE)
            .get(UserResult.class);

    assertEquals(Status.USER_NOT_FOUND, userResult.getStatus());
    assertNull(userResult.getUser());

    // 3rd user should registered and activated
    webResource = client.resource(ringringServerApi.getUrl() + "/user/" + email3);
    userResult =
        webResource
            .type(MediaType.APPLICATION_JSON_TYPE)
            .accept(MediaType.APPLICATION_JSON_TYPE)
            .get(UserResult.class);
    assertEquals(Status.OKAY, userResult.getStatus());
    assertTrue(userResult.getUser().getIsActivated());

    // 4th user never existed in the system
    webResource = client.resource(ringringServerApi.getUrl() + "/user/" + email4);
    userResult =
        webResource
            .type(MediaType.APPLICATION_JSON_TYPE)
            .accept(MediaType.APPLICATION_JSON_TYPE)
            .get(UserResult.class);
    assertEquals(Status.USER_NOT_FOUND, userResult.getStatus());
    assertNull(userResult.getUser());
  }