package com.google.sps;

import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import com.google.appengine.tools.development.testing.LocalUserServiceTestConfig;
import com.google.sps.data.User;
import com.google.sps.servlets.UserProfileUpdateServlet;
import com.google.sps.utilities.CommonUtils;
import com.google.sps.utilities.SpannerClient;
import com.google.sps.utilities.SpannerTasks;
import com.google.sps.utilities.SpannerTestTasks;
import com.google.sps.utilities.TestUtils;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import javax.servlet.ServletContextEvent;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.Mockito;
import org.springframework.mock.web.MockServletContext;

/** Unit tests for DatabaseWrapper functionality related to Event class. */
@RunWith(JUnit4.class)
public class UserProfileUpdateTest {
  private static final String EMAIL = "test@example.com";
  private static final String DOMAIN = "example.com";
  private static final User EXPECTED_USER = TestUtils.newUser(EMAIL);

  private static final LocalServiceTestHelper authenticationHelper =
      new LocalServiceTestHelper(new LocalUserServiceTestConfig());

  private static final HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
  private static final HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
  private static final StringWriter stringWriter = new StringWriter();
  private static final PrintWriter printWriter = new PrintWriter(stringWriter);

  @BeforeClass
  public static void setUp() throws Exception {
    // Mock a request to trigger the SpannerClient setup to run
    MockServletContext mockServletContext = new MockServletContext();
    new SpannerClient().contextInitialized(new ServletContextEvent(mockServletContext));
    SpannerTestTasks.setup();
    authenticationHelper.setUp();
    Mockito.when(response.getWriter()).thenReturn(printWriter);
  }

  @AfterClass
  public static void tearDown() throws Exception {
    SpannerTestTasks.cleanup();
    authenticationHelper.tearDown();
  }

  @Test
  public void testGetUpdatedUser() throws Exception {
    SpannerTasks.insertOrUpdateUser(EXPECTED_USER);
    authenticationHelper
        .setEnvIsLoggedIn(true)
        .setEnvEmail(EMAIL)
        .setEnvAuthDomain(DOMAIN);

    new UserProfileUpdateServlet().doGet(request, response);
 
    Assert.assertEquals(
        CommonUtils.convertToJson(EXPECTED_USER), stringWriter.toString().trim());
  }

  @Test
  public void testPostUpdatedUser() throws Exception {
    Mockito.when(request.getParameter("name")).thenReturn(EXPECTED_USER.getName());
    Mockito.doReturn(EXPECTED_USER.getInterests().toString()).when(request).getParameter("interests");
    Mockito.doReturn(EXPECTED_USER.getSkills().toString()).when(request).getParameter("skills");
    authenticationHelper
        .setEnvIsLoggedIn(true)
        .setEnvEmail(EMAIL)
        .setEnvAuthDomain(DOMAIN);

    new UserProfileUpdateServlet().doPost(request, response);

    Mockito.verify(response).sendRedirect("/profile.html");
  }
}


@@ -1317,25 +1317,16 @@
 al User 
-EXPECTED_
 USER = T
@@ -2484,25 +2484,16 @@
 ateUser(
-EXPECTED_
 USER);%0A 
@@ -2491,25 +2491,28 @@
 (USER);%0A    
-a
+setA
 uthenticatio
@@ -2522,101 +2522,9 @@
 lper
-%0A        .setEnvIsLoggedIn(true)%0A        .setEnvEmail(EMAIL)%0A        .setEnvAuthDomain(DOMAIN
+(
 );%0A%0A
@@ -2645,25 +2645,16 @@
 tToJson(
-EXPECTED_
 USER), s
@@ -2807,33 +2807,24 @@
 .thenReturn(
-EXPECTED_
 USER.getName
@@ -2845,33 +2845,24 @@
 to.doReturn(
-EXPECTED_
 USER.getInte
@@ -2947,17 +2947,8 @@
 urn(
-EXPECTED_
 USER
@@ -3010,25 +3010,28 @@
 ills%22);%0A    
-a
+setA
 uthenticatio
@@ -3041,101 +3041,9 @@
 lper
-%0A        .setEnvIsLoggedIn(true)%0A        .setEnvEmail(EMAIL)%0A        .setEnvAuthDomain(DOMAIN
+(
 );%0A%0A
@@ -3168,12 +3168,187 @@
 l%22);%0A  %7D
+%0A%0A  private static void setAuthenticationHelper() %7B%0A    authenticationHelper%0A        .setEnvIsLoggedIn(true)%0A        .setEnvEmail(EMAIL)%0A        .setEnvAuthDomain(DOMAIN);%0A  %7D
 %0A%7D%0A%0A

Remove newline