@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 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());
  }