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("test@test.com", false, "verificationToken"), "password", "", "", "");
  private final PilotUser verifiedMockUser =
      new PilotUser(new Email("test@test.com", true, "verificationToken"), "password", "", "", "");
  private final PilotUser nullDatabaseTokenMockUser =
      new PilotUser(new Email("test@test.com", false, null), "password", "", "", "");
  private final PilotUser mismatchedTokenMockUser =
      new PilotUser(new Email("test@test.com", 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, "test@test.com", null);

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

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

    Response response = resource.verifyEmail(key, "test@test.com", "verificationToken");

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

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

    Response response = resource.verifyEmail(key, "test@test.com", "verificationToken");

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

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

    Response response = resource.verifyEmail(key, "test@test.com", "verificationToken");
    PilotUser result = (PilotUser) response.getEntity();

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

@@ -127,16 +127,114 @@
 on.Key;%0A
+import com.sanction.thunder.dao.DatabaseError;%0Aimport com.sanction.thunder.dao.DatabaseException;%0A
 import c
@@ -1933,32 +1933,384 @@
 _REQUEST);%0A  %7D%0A%0A
+  @Test%0A  public void testVerifyEmailFindUserException() %7B%0A    when(usersDao.findByEmail(anyString()))%0A        .thenThrow(new DatabaseException(DatabaseError.DATABASE_DOWN));%0A%0A    Response response = resource.verifyEmail(key, %22test@test.com%22, %22verificationToken%22);%0A%0A    assertEquals(response.getStatusInfo(), Response.Status.SERVICE_UNAVAILABLE);%0A  %7D%0A%0A
   @Test%0A  public
@@ -2921,32 +2921,510 @@
 _REQUEST);%0A  %7D%0A%0A
+  @Test%0A  public void testVerifyEmailUpdateUserException() %7B%0A    when(usersDao.findByEmail(%22test@test.com%22)).thenReturn(unverifiedMockUser);%0A    when(usersDao.update(unverifiedMockUser.getEmail().getAddress(), verifiedMockUser))%0A        .thenThrow(new DatabaseException(DatabaseError.DATABASE_DOWN));%0A%0A    Response response = resource.verifyEmail(key, %22test@test.com%22, %22verificationToken%22);%0A%0A    assertEquals(response.getStatusInfo(), Response.Status.SERVICE_UNAVAILABLE);%0A  %7D%0A%0A
   @Test%0A  public

Add a few more unit tests