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.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.UserRecord;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
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 GetQueueTest {

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

  private DatastoreService datastore;

  @Mock HttpServletRequest httpRequest;

  @Mock HttpServletResponse httpResponse;

  @Mock FirebaseAuth authInstance;

  @InjectMocks GetQueue queue;

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

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

  @Test
  public void shortQueue() throws Exception {
    Entity init = new Entity("Class");
    ArrayList<String> setQueue = new ArrayList<String>(Arrays.asList("uID1", "uID2"));

    init.setProperty("owner", "ownerID");
    init.setProperty("name", "testClass");
    init.setProperty("beingHelped", "");
    init.setProperty("studentQueue", setQueue);

    datastore.put(init);

    when(httpRequest.getParameter("classCode")).thenReturn(KeyFactory.keyToString(init.getKey()));

    UserRecord mockUser1 = mock(UserRecord.class);
    when(authInstance.getUser("uID1")).thenReturn(mockUser1);
    when(mockUser1.getEmail()).thenReturn("test1@google.com");

    UserRecord mockUser2 = mock(UserRecord.class);
    when(authInstance.getUser("uID2")).thenReturn(mockUser2);
    when(mockUser2.getEmail()).thenReturn("test2@google.com");

    StringWriter stringWriter = new StringWriter();
    PrintWriter writer = new PrintWriter(stringWriter);
    when(httpResponse.getWriter()).thenReturn(writer);

    queue.doGet(httpRequest, httpResponse);

    assertEquals(
        new ArrayList<String>(Arrays.asList("\"test1@google.com\"", "\"test2@google.com\"")),
        stringWriter.toString());
  }
}

// write json conversiton comment

@@ -639,16 +639,45 @@
 Record;%0A
+import com.google.gson.Gson;%0A
 import j
@@ -1424,16 +1424,36 @@
 tance;%0A%0A
+  @Mock Gson gson;%0A%0A
   @Injec
@@ -2494,24 +2494,184 @@
 gle.com%22);%0A%0A
+    when(gson.toJson(new ArrayList%3CString%3E(Arrays.asList(%22test1@google.com%22, %22test2@google.com%22))))%0A        .thenReturn(%22test1@google.com, test2@google.com%22);%0A%0A
     StringWr
@@ -2888,55 +2888,8 @@
 als(
-%0A        new ArrayList%3CString%3E(Arrays.asList(%22%5C
 %22tes
@@ -2905,16 +2905,10 @@
 .com
-%5C%22%22, %22%5C%22
+, 
 test
@@ -2923,22 +2923,10 @@
 .com
-%5C%22%22)),%0A       
+%22,
  str
@@ -2958,39 +2958,4 @@
 %7D%0A%7D%0A
-%0A// write json conversiton comment%0A

Fix test cases so return values match