package com.google.launchpod;

import static com.google.appengine.api.datastore.FetchOptions.Builder.withLimit;
import static org.junit.Assert.assertEquals;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import com.google.cloud.translate.Translate;
import com.google.cloud.translate.Translate.TranslateOption;
import com.google.cloud.translate.Translation;
import com.google.cloud.translate.testing.RemoteTranslateHelper;
import com.google.launchpod.data.Item;
import com.google.launchpod.data.RSS;
import com.google.launchpod.servlets.TranslationServlet;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

/**
 * Runs unit tests for the FormHandlerServlet that contains doPost(), doGet(),
 * and xmlString() methods.
 */
@RunWith(JUnit4.class)
public class TranslationServletTest extends Mockito {

  @InjectMocks
  private TranslationServlet servlet = new TranslationServlet();

  @Mock
  HttpServletRequest request;

  @Mock
  HttpServletResponse response;

  @Rule
  public ExpectedException thrown = ExpectedException.none();

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

  // keys
  private static final String USER_FEED = "UserFeed";
  private static final String LANGUAGE = "language";
  private static final String RSS_FEED_LINK = "rssFeedLink";
  private static final String TEST_NAME = "hello";
  private static final String TEST_PODCAST_TITLE = "American Life";
  private static final String TEST_CATEGORY = "lifestyle";
  private static final String TEST_TARGET_LANGUAGE = "es";
  private static final String TEST_LANGUAGE = "en";
  private static final String TEST_DESCRIPTION = "Episode generated by launchpod";
  private static final String TEST_MP3_LINK = "this-is-a-test.mp3";
  private static final String TEST_ID = "123456";
  private static final String TEST_EMAIL = "123@abc.com";
  private static final String BASE_URL = "https://launchpod-step18-2020.appspot.com/rss-feed?id=";
  private static final RSS TEST_RSS_FEED = new RSS(TEST_NAME, TEST_EMAIL, TEST_PODCAST_TITLE, TEST_MP3_LINK, TEST_CATEGORY);
  private RemoteTranslateHelper translateHelper = RemoteTranslateHelper
      .create("AIzaSyBlu9s7xFLjlHAdvlVuUISq_MbyELuCRZo");
  private Translate translateMock = translateHelper.getOptions().getService();

  @Before
  public void setUp() {
    MockitoAnnotations.initMocks(this);
    helper.setUp();
  }

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

  //TODO: Implement Translation Constructor
  /**
   * Test translate helper to ensure it translates the given string
   */
  /*
  @Test
  public void doPost_TranslateHelperWorks() {
    String sourceLang = translateMock.detect(TEST_DESCRIPTION).getLanguage();
    Translation translation = translateMock.translate(TEST_DESCRIPTION, TranslateOption.sourceLanguage(sourceLang),
        TranslateOption.targetLanguage(TEST_TARGET_LANGUAGE));

    String translatedText = translation.getTranslatedText().toLowerCase();
    assertEquals("episodio generado por launchpod", translatedText);
  }

  /**
   * test to ensure that when rss feed is translated the RSS object is modified as
   * well
   * 
   * @throws JsonProcessingException
   */
  /*
  @Test
  public void translate_translatesRSSFieldsCorrectly() throws JsonProcessingException {
    RSS rssTranslatedFeed = TEST_RSS_FEED;
    // Test channel description translation
    Translation translation = translateMock.translate(rssTranslatedFeed.getChannel().getDescription(),
        TranslateOption.targetLanguage(TEST_TARGET_LANGUAGE));
    rssTranslatedFeed.getChannel().setDescription(translation.getTranslatedText());
    assertEquals("launchpod generó rss", rssTranslatedFeed.getChannel().getDescription().toLowerCase());

    // Test item
    for (Item item : rssTranslatedFeed.getChannel().getItems()) {
      translation = translateMock.translate(item.getTitle(), TranslateOption.targetLanguage(TEST_TARGET_LANGUAGE));
      item.setTitle(translation.getTranslatedText());
      assertEquals("vida americana", item.getTitle().toLowerCase());

      // set item description
      translation = translateMock.translate(item.getDescription(),
          TranslateOption.targetLanguage(TEST_TARGET_LANGUAGE));
      item.setDescription(translation.getTranslatedText());
      assertEquals("episodio generado por launchpod", item.getDescription().toLowerCase());
    }
  }

  /**
   * Expect doPost to throw IOException when language field is empty
   * 
   * @throws IOException
   */

  @Test
  public void doPost_ThrowsException_WhenLanguageInputEmpty() throws IOException {
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

    when(request.getParameter(LANGUAGE)).thenReturn("");
    when(request.getParameter(RSS_FEED_LINK)).thenReturn(BASE_URL + TEST_ID);

    thrown.expect(IOException.class);
    thrown.expectMessage("Please give valid language.");

    servlet.doPost(request, response);

    assertEquals(0, datastore.prepare(new Query(USER_FEED)).countEntities(withLimit(5)));
  }

  /**
   * Expect doPost to throw IOException when rssFeedLink field is empty
   * 
   * @throws IOException
   */
  @Test
  public void doPost_ThrowsException_WhenRssFeedLinkInputEmpty() throws IOException {
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

    when(request.getParameter(LANGUAGE)).thenReturn(TEST_LANGUAGE);
    when(request.getParameter(RSS_FEED_LINK)).thenReturn("");

    thrown.expect(IOException.class);
    thrown.expectMessage("Please give valid link.");

    servlet.doPost(request, response);

    assertEquals(0, datastore.prepare(new Query(USER_FEED)).countEntities(withLimit(5)));
  }

  /**
   * Assert that getIdFromUrl() obtains the correct input id from the rssFeedLink
   * in the form
   */
  @Test
  public void getIdFromUrl_GeneratesCorrectId() throws IOException {
    String testId = TranslationServlet.getIdFromUrl(BASE_URL + TEST_ID);

    assertEquals(TEST_ID, testId);
  }
}
@@ -3266,1893 +3266,8 @@
 ctor
-%0A  /**%0A   * Test translate helper to ensure it translates the given string%0A   */%0A  /*%0A  @Test%0A  public void doPost_TranslateHelperWorks() %7B%0A    String sourceLang = translateMock.detect(TEST_DESCRIPTION).getLanguage();%0A    Translation translation = translateMock.translate(TEST_DESCRIPTION, TranslateOption.sourceLanguage(sourceLang),%0A        TranslateOption.targetLanguage(TEST_TARGET_LANGUAGE));%0A%0A    String translatedText = translation.getTranslatedText().toLowerCase();%0A    assertEquals(%22episodio generado por launchpod%22, translatedText);%0A  %7D%0A%0A  /**%0A   * test to ensure that when rss feed is translated the RSS object is modified as%0A   * well%0A   * %0A   * @throws JsonProcessingException%0A   */%0A  /*%0A  @Test%0A  public void translate_translatesRSSFieldsCorrectly() throws JsonProcessingException %7B%0A    RSS rssTranslatedFeed = TEST_RSS_FEED;%0A    // Test channel description translation%0A    Translation translation = translateMock.translate(rssTranslatedFeed.getChannel().getDescription(),%0A        TranslateOption.targetLanguage(TEST_TARGET_LANGUAGE));%0A    rssTranslatedFeed.getChannel().setDescription(translation.getTranslatedText());%0A    assertEquals(%22launchpod gener%C3%B3 rss%22, rssTranslatedFeed.getChannel().getDescription().toLowerCase());%0A%0A    // Test item%0A    for (Item item : rssTranslatedFeed.getChannel().getItems()) %7B%0A      translation = translateMock.translate(item.getTitle(), TranslateOption.targetLanguage(TEST_TARGET_LANGUAGE));%0A      item.setTitle(translation.getTranslatedText());%0A      assertEquals(%22vida americana%22, item.getTitle().toLowerCase());%0A%0A      // set item description%0A      translation = translateMock.translate(item.getDescription(),%0A          TranslateOption.targetLanguage(TEST_TARGET_LANGUAGE));%0A      item.setDescription(translation.getTranslatedText());%0A      assertEquals(%22episodio generado por launchpod%22, item.getDescription().toLowerCase());%0A    %7D%0A  %7D
 %0A%0A  
@@ -4865,9 +4865,10 @@
 d);%0A  %7D%0A
-
 %7D
+%0A

Remove commented code, add todo