<commit_before>package com.sanction.thunder.resources;

import com.codahale.metrics.MetricRegistry;

import com.sanction.thunder.authentication.Key;
import com.sanction.thunder.dao.PilotUsersDao;
import com.sanction.thunder.models.Email;
import com.sanction.thunder.models.PilotUser;

import javax.ws.rs.core.Response;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class VerificationResourceTest {
  private final PilotUsersDao usersDao = mock(PilotUsersDao.class);
  private final MetricRegistry metrics = new MetricRegistry();
  private final Key key = mock(Key.class);

  private final PilotUser unverifiedMockUser =
      new PilotUser(new Email("<EMAIL>", false, "verificationToken"), "password", "", "", "");
  private final PilotUser verifiedMockUser =
      new PilotUser(new Email("<EMAIL>", true, "verificationToken"), "password", "", "", "");
  private final PilotUser nullDatabaseTokenMockUser =
      new PilotUser(new Email("<EMAIL>", false, null), "password", "", "", "");
  private final PilotUser mismatchedTokenMockUser =
      new PilotUser(new Email("<EMAIL>", false, "mismatchedToken"), "password", "", "", "");

  private final VerificationResource resource = new VerificationResource(usersDao, metrics);


  /* Verify Email Tests */
  @Test
  public void testVerifyEmailWithNullEmail() {
    Response response =
        resource.verifyEmail(key, null, "verificationToken");

    assertEquals(response.getStatusInfo(), Response.Status.BAD_REQUEST);
  }

  @Test
  public void testVerifyEmailWithNullToken() {
    Response response = resource.verifyEmail(key, "<EMAIL>", null);

    assertEquals(response.getStatusInfo(), Response.Status.BAD_REQUEST);
  }

  @Test
  public void testVerifyEmailWithNullDatabaseToken() {
    when(usersDao.findByEmail(anyString())).thenReturn(nullDatabaseTokenMockUser);

    Response response = resource.verifyEmail(key, "<EMAIL>", "verificationToken");

    assertEquals(response.getStatusInfo(), Response.Status.INTERNAL_SERVER_ERROR);
  }

  @Test
  public void testVerifyEmailWithMismatchedToken() {
    when(usersDao.findByEmail(anyString())).thenReturn(mismatchedTokenMockUser);

    Response response = resource.verifyEmail(key, "<EMAIL>", "verificationToken");

    assertEquals(response.getStatusInfo(), Response.Status.BAD_REQUEST);
  }

  @Test
  public void testVerifyEmailSuccess() {
    when(usersDao.findByEmail("<EMAIL>")).thenReturn(unverifiedMockUser);
    when(usersDao.update(unverifiedMockUser.getEmail().getAddress(), verifiedMockUser))
        .thenReturn(verifiedMockUser);

    Response response = resource.verifyEmail(key, "<EMAIL>", "verificationToken");
    PilotUser result = (PilotUser) response.getEntity();

    assertEquals(response.getStatusInfo(), Response.Status.OK);
    assertEquals(verifiedMockUser, result);
  }
}
<commit_msg>Add a few more unit tests<commit_after>package com.sanction.thunder.resources;

import com.codahale.metrics.MetricRegistry;

import com.sanction.thunder.authentication.Key;
import com.sanction.thunder.dao.DatabaseError;
import com.sanction.thunder.dao.DatabaseException;
import com.sanction.thunder.dao.PilotUsersDao;
import com.sanction.thunder.models.Email;
import com.sanction.thunder.models.PilotUser;

import javax.ws.rs.core.Response;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class VerificationResourceTest {
  private final PilotUsersDao usersDao = mock(PilotUsersDao.class);
  private final MetricRegistry metrics = new MetricRegistry();
  private final Key key = mock(Key.class);

  private final PilotUser unverifiedMockUser =
      new PilotUser(new Email("<EMAIL>", false, "verificationToken"), "password", "", "", "");
  private final PilotUser verifiedMockUser =
      new PilotUser(new Email("<EMAIL>", true, "verificationToken"), "password", "", "", "");
  private final PilotUser nullDatabaseTokenMockUser =
      new PilotUser(new Email("<EMAIL>", false, null), "password", "", "", "");
  private final PilotUser mismatchedTokenMockUser =
      new PilotUser(new Email("<EMAIL>", false, "mismatchedToken"), "password", "", "", "");

  private final VerificationResource resource = new VerificationResource(usersDao, metrics);


  /* Verify Email Tests */
  @Test
  public void testVerifyEmailWithNullEmail() {
    Response response =
        resource.verifyEmail(key, null, "verificationToken");

    assertEquals(response.getStatusInfo(), Response.Status.BAD_REQUEST);
  }

  @Test
  public void testVerifyEmailWithNullToken() {
    Response response = resource.verifyEmail(key, "<EMAIL>", null);

    assertEquals(response.getStatusInfo(), Response.Status.BAD_REQUEST);
  }

  @Test
  public void testVerifyEmailFindUserException() {
    when(usersDao.findByEmail(anyString()))
        .thenThrow(new DatabaseException(DatabaseError.DATABASE_DOWN));

    Response response = resource.verifyEmail(key, "<EMAIL>", "verificationToken");

    assertEquals(response.getStatusInfo(), Response.Status.SERVICE_UNAVAILABLE);
  }

  @Test
  public void testVerifyEmailWithNullDatabaseToken() {
    when(usersDao.findByEmail(anyString())).thenReturn(nullDatabaseTokenMockUser);

    Response response = resource.verifyEmail(key, "<EMAIL>", "verificationToken");

    assertEquals(response.getStatusInfo(), Response.Status.INTERNAL_SERVER_ERROR);
  }

  @Test
  public void testVerifyEmailWithMismatchedToken() {
    when(usersDao.findByEmail(anyString())).thenReturn(mismatchedTokenMockUser);

    Response response = resource.verifyEmail(key, "<EMAIL>", "verificationToken");

    assertEquals(response.getStatusInfo(), Response.Status.BAD_REQUEST);
  }

  @Test
  public void testVerifyEmailUpdateUserException() {
    when(usersDao.findByEmail("<EMAIL>")).thenReturn(unverifiedMockUser);
    when(usersDao.update(unverifiedMockUser.getEmail().getAddress(), verifiedMockUser))
        .thenThrow(new DatabaseException(DatabaseError.DATABASE_DOWN));

    Response response = resource.verifyEmail(key, "<EMAIL>", "verificationToken");

    assertEquals(response.getStatusInfo(), Response.Status.SERVICE_UNAVAILABLE);
  }

  @Test
  public void testVerifyEmailSuccess() {
    when(usersDao.findByEmail("<EMAIL>")).thenReturn(unverifiedMockUser);
    when(usersDao.update(unverifiedMockUser.getEmail().getAddress(), verifiedMockUser))
        .thenReturn(verifiedMockUser);

    Response response = resource.verifyEmail(key, "<EMAIL>", "verificationToken");
    PilotUser result = (PilotUser) response.getEntity();

    assertEquals(response.getStatusInfo(), Response.Status.OK);
    assertEquals(verifiedMockUser, result);
  }
}

{'fraction_non_alphanumeric': 0.12449098312972659, 'fraction_numerical': 0.0, 'mean_word_length': 5.6252408477842, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4234387', 'n_tokens_mistral': 1969, 'n_tokens_neox': 1958, 'n_words': 367}