package com.google.sps.servlets;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.After;

import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import static com.google.sps.tool.Tool.compareJson;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.*;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.IOException;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import com.google.appengine.tools.development.testing.LocalUserServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import static com.google.appengine.api.datastore.FetchOptions.Builder.withLimit;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Entity;

import com.google.gson.Gson;
import com.google.common.collect.ImmutableMap;
import com.google.sps.data.Folder;
import com.google.sps.tool.EntityTestingTool;
import java.util.List; 
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;


@RunWith(JUnit4.class)
public final class EditFolderServletTest {

  private final LocalServiceTestHelper helper = 
    new LocalServiceTestHelper(
      new LocalDatastoreServiceTestConfig()
        .setDefaultHighRepJobPolicyUnappliedJobPercentage(0),
      new LocalUserServiceTestConfig())
      .setEnvIsAdmin(true).setEnvIsLoggedIn(true)
      .setEnvEmail("test@gmail.com").setEnvAuthDomain("gmail.com");

  private static final Folder CURRENT_FOLDER = new Folder("FIRSTFOLDER", "en");
  private static final Folder EDITED_FOLDER = new Folder("EDITEDFOLDER", "es_edited");
  private HttpServletRequest mockRequest;
  private HttpServletResponse mockResponse;
  private StringWriter responseWriter;
  private EditFolderServlet servlet;
  private DatastoreService datastore;
    
  @Before
  public void setUp() throws Exception {
    helper.setUp();
    servlet = new EditFolderServlet();
    mockRequest = mock(HttpServletRequest.class);
    mockResponse = mock(HttpServletResponse.class);
    
    // Set up a fake HTTP response 
    responseWriter = new StringWriter();
    when(mockResponse.getWriter()).thenReturn(new PrintWriter(responseWriter));

    // Initialize datastore
    datastore = DatastoreServiceFactory.getDatastoreService();
  }

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

  @Test
  public void EditFolder() throws Exception {
    
    // Generate testing User
    Entity USER_A = new Entity("User", "testId");
    String USERKEY = KeyFactory.keyToString(USER_A.getKey());

    Folder folderInDatastore = EntityTestingTool.populateDatastoreWithAFolder(CURRENT_FOLDER, datastore, USERKEY);
    String folderKey = folderInDatastore.getFolderKey();

    // Make sure the expected Folder has the same key
    EDITED_FOLDER.setFolderKey(folderKey);

    when(mockRequest.getParameter("folderName")).thenReturn("EDITEDFOLDER");
    when(mockRequest.getParameter("folderDefaultLanguage")).thenReturn("es_edited");
    when(mockRequest.getParameter("folderKey")).thenReturn(folderKey);

    servlet.doPut(mockRequest, mockResponse);

    Entity editedFolder = datastore.get(KeyFactory.stringToKey(folderKey));

    String response = new Gson().toJson(new Folder(editedFolder));
    String expectedResponse = new Gson().toJson(EDITED_FOLDER);

    assertEquals(response, expectedResponse);
  }

}
@@ -2130,175 +2130,8 @@
 );%0A%0A
-  private static final Folder CURRENT_FOLDER = new Folder(%22FIRSTFOLDER%22, %22en%22);%0A  private static final Folder EDITED_FOLDER = new Folder(%22EDITEDFOLDER%22, %22es_edited%22);%0A
   pr
@@ -2932,24 +2932,154 @@
  Exception %7B
+%0A%0A    Folder currentFolder = new Folder(%22FIRSTFOLDER%22, %22en%22);%0A    Folder expectedFolder = new Folder(%22EDITEDFOLDER%22, %22es_edited%22);
 %0A    %0A    //
@@ -3112,22 +3112,20 @@
  Entity 
-USER_A
+user
  = new E
@@ -3160,23 +3160,23 @@
  String 
-USERKEY
+userKey
  = KeyFa
@@ -3197,14 +3197,12 @@
 ing(
-USER_A
+user
 .get
@@ -3292,22 +3292,21 @@
 der(
-CURRENT_FOLDER
+currentFolder
 , da
@@ -3318,15 +3318,15 @@
 re, 
-USERKEY
+userKey
 );%0A 
@@ -3440,29 +3440,30 @@
 key%0A    
-EDITED_FOLDER
+expectedFolder
 .setFold
@@ -3958,21 +3958,22 @@
 son(
-EDITED_FOLDER
+expectedFolder
 );%0A%0A

Fix naming styling issue in EditFolderServletTest.java