package com.google.sps.servlets;

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

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.EmbeddedEntity;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseToken;
import com.google.gson.Gson;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.Collections;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class GetUserRoleTest {

  private final LocalServiceTestHelper helper =
      new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig());

  private DatastoreService datastore;

  @Mock HttpServletRequest httpRequest;

  @Mock HttpServletResponse httpResponse;

  @Mock FirebaseAuth authInstance;

  @InjectMocks GetUserRole getUserRole;

  @Before
  public void setUp() {
    helper.setUp();
    datastore = DatastoreServiceFactory.getDatastoreService();
  }

  @After
  public void tearDown() {
    helper.tearDown();
  }

  @Test
  public void getTAName() throws Exception {
    Entity initClass = new Entity("Class");

    initClass.setProperty("owner", "ownerID");
    initClass.setProperty("name", "testClass");
    initClass.setProperty("beingHelped", new EmbeddedEntity());
    initClass.setProperty("studentQueue", Collections.emptyList());

    datastore.put(initClass);

    Entity initUser = new Entity("User");

    initUser.setProperty("userEmail", "user@google.com");
    initUser.setProperty("registeredClasses", Collections.emptyList());
    initUser.setProperty("ownedClasses", Collections.emptyList());
    initUser.setProperty("taClasses", Arrays.asList(initClass.getKey()));

    datastore.put(initUser);

    when(httpRequest.getParameter("idToken")).thenReturn("uID");

    FirebaseToken mockToken = mock(FirebaseToken.class);
    when(authInstance.verifyIdToken("uID")).thenReturn(mockToken);
    when(mockToken.getUid()).thenReturn("uID");
    when(mockToken.getEmail()).thenReturn("user@google.com");

    when(httpRequest.getParameter("classCode"))
        .thenReturn(KeyFactory.keyToString(initClass.getKey()));
    StringWriter stringWriter = new StringWriter();
    PrintWriter writer = new PrintWriter(stringWriter);
    when(httpResponse.getWriter()).thenReturn(writer);

    getUserRole.doGet(httpRequest, httpResponse);

    assertEquals(new Gson().toJson("TA"), stringWriter.toString());
  }

  @Test
  public void getOwnerName() throws Exception {
    Entity initClass = new Entity("Class");

    initClass.setProperty("owner", "ownerID");
    initClass.setProperty("name", "testClass");
    initClass.setProperty("beingHelped", new EmbeddedEntity());
    initClass.setProperty("studentQueue", Collections.emptyList());

    datastore.put(initClass);

    Entity initUser = new Entity("User");

    initUser.setProperty("userEmail", "user@google.com");
    initUser.setProperty("registeredClasses", Collections.emptyList());
    initUser.setProperty("ownedClasses", Arrays.asList(initClass.getKey()));
    initUser.setProperty("taClasses", Collections.emptyList());

    datastore.put(initUser);

    when(httpRequest.getParameter("idToken")).thenReturn("uID");

    FirebaseToken mockToken = mock(FirebaseToken.class);
    when(authInstance.verifyIdToken("uID")).thenReturn(mockToken);
    when(mockToken.getUid()).thenReturn("uID");
    when(mockToken.getEmail()).thenReturn("user@google.com");

    when(httpRequest.getParameter("classCode"))
        .thenReturn(KeyFactory.keyToString(initClass.getKey()));
    StringWriter stringWriter = new StringWriter();
    PrintWriter writer = new PrintWriter(stringWriter);
    when(httpResponse.getWriter()).thenReturn(writer);

    getUserRole.doGet(httpRequest, httpResponse);

    assertEquals(new Gson().toJson("owner"), stringWriter.toString());
  }
}

@@ -4529,11 +4529,1425 @@
 ());%0A  %7D
+%0A%0A  @Test%0A  public void getStudentName() throws Exception %7B%0A    Entity initClass = new Entity(%22Class%22);%0A%0A    initClass.setProperty(%22owner%22, %22ownerID%22);%0A    initClass.setProperty(%22name%22, %22testClass%22);%0A    initClass.setProperty(%22beingHelped%22, new EmbeddedEntity());%0A    initClass.setProperty(%22studentQueue%22, Collections.emptyList());%0A%0A    datastore.put(initClass);%0A%0A    Entity initUser = new Entity(%22User%22);%0A%0A    initUser.setProperty(%22userEmail%22, %22user@google.com%22);%0A    initUser.setProperty(%22registeredClasses%22, Arrays.asList(initClass.getKey()));%0A    initUser.setProperty(%22ownedClasses%22, Collections.emptyList());%0A    initUser.setProperty(%22taClasses%22, Collections.emptyList());%0A%0A    datastore.put(initUser);%0A%0A    when(httpRequest.getParameter(%22idToken%22)).thenReturn(%22uID%22);%0A%0A    FirebaseToken mockToken = mock(FirebaseToken.class);%0A    when(authInstance.verifyIdToken(%22uID%22)).thenReturn(mockToken);%0A    when(mockToken.getUid()).thenReturn(%22uID%22);%0A    when(mockToken.getEmail()).thenReturn(%22user@google.com%22);%0A%0A    when(httpRequest.getParameter(%22classCode%22))%0A        .thenReturn(KeyFactory.keyToString(initClass.getKey()));%0A    StringWriter stringWriter = new StringWriter();%0A    PrintWriter writer = new PrintWriter(stringWriter);%0A    when(httpResponse.getWriter()).thenReturn(writer);%0A%0A    getUserRole.doGet(httpRequest, httpResponse);%0A%0A    assertEquals(new Gson().toJson(%22student%22), stringWriter.toString());%0A  %7D
 %0A%7D%0A

add add'l test case