<commit_before>package org.ambraproject.wombat.freemarker;

import org.ambraproject.wombat.config.site.SiteSet;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.mockito.ArgumentCaptor;
import org.testng.annotations.Test;

import java.util.List;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.testng.Assert.assertEquals;

public class HtmlElementTransformationTest {

  @Test
  public void testApply(){

    SitePageContext sitePageContext = mock(SitePageContext.class);
    SiteSet siteset = mock(SiteSet.class);
    when(sitePageContext.buildLink(anyString())).thenReturn("path/to/an/internal/page");
    when(sitePageContext.buildLink(any(SiteSet.class), anyString(), anyString())).thenReturn("path/to/another/journal/page");

    String testHtml =  "<img data-lemur-key=\"content_1234\"/>" +
            "<a data-lemur-key=\"file_4321\"/>" +
            "<a data-lemur-link=\"s/lorum_ipsum\" data-lemur-link-suffix=\"#anchor_string\">Lorem Ipsum</a>" +
            "<a data-lemur-link=\"s/test-page\" data-lemur-link-journal=\"PLoSCompBiol\">test cross-journal link</a>" +
            "<a data-lemur-doi=\"10.1371/journal.pone.0008083\">article link</a>";

    Document document = Jsoup.parseBodyFragment(testHtml);

    HtmlElementTransformation.LINK.apply(sitePageContext, siteset, document);
    HtmlElementTransformation.ASSET.apply(sitePageContext, siteset, document);
    HtmlElementTransformation.IMAGE.apply(sitePageContext, siteset, document);
    HtmlElementTransformation.ARTICLE.apply(sitePageContext, siteset, document);

    ArgumentCaptor<String> pathArg = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<String> journalKeyArg = ArgumentCaptor.forClass(String.class);

    verify(sitePageContext, times(4)).buildLink(pathArg.capture());
    verify(sitePageContext).buildLink(any(SiteSet.class), journalKeyArg.capture(), pathArg.capture());

    List<String> pathArgs = pathArg.getAllValues();
    assertEquals(pathArgs.get(0), "s/lorum_ipsum");
    assertEquals(pathArgs.get(1), "indirect/file_4321");
    assertEquals(pathArgs.get(2), "indirect/content_1234");
    assertEquals(pathArgs.get(3), "article?id=10.1371/journal.pone.0008083");
    assertEquals(pathArgs.get(4), "s/test-page");
    assertEquals(journalKeyArg.getValue(), "PLoSCompBiol");

    assertEquals(document.toString(),
            "<html>\n" +
            " <head></head>\n" +
            " <body>\n" +
            "  <img src=\"path/to/an/internal/page\" />\n" +
            "  <a href=\"path/to/an/internal/page\"></a>\n" +
            "  <a href=\"path/to/an/internal/page#anchor_string\">Lorem Ipsum</a>\n" +
            "  <a href=\"path/to/another/journal/page\">test cross-journal link</a>\n" +
            "  <a href=\"path/to/an/internal/page\">article link</a>\n" +
            " </body>\n" +<commit_msg>DPRO-945: updated unit test<commit_after>package org.ambraproject.wombat.freemarker;

import org.ambraproject.wombat.config.site.SiteSet;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.mockito.ArgumentCaptor;
import org.testng.annotations.Test;

import java.util.List;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.testng.Assert.assertEquals;

public class HtmlElementTransformationTest {

  @Test
  public void testApply(){

    SitePageContext sitePageContext = mock(SitePageContext.class);
    SiteSet siteset = mock(SiteSet.class);
    when(sitePageContext.buildLink(anyString())).thenReturn("path/to/an/internal/page");
    when(sitePageContext.buildLink(any(SiteSet.class), anyString(), anyString())).thenReturn("path/to/another/journal/page");

    String testHtml =  "<img data-lemur-key=\"content_1234\"/>" +
            "<a data-lemur-key=\"file_4321\"/>" +
            "<a data-lemur-link=\"s/lorum_ipsum\" data-lemur-link-suffix=\"#anchor_string\">Lorem Ipsum</a>" +
            "<a data-lemur-link=\"s/test-page\" data-lemur-link-journal=\"PLoSCompBiol\">test cross-journal link</a>" +
            "<a data-lemur-doi=\"10.1371/journal.pone.0008083\">article link</a>";

    Document document = Jsoup.parseBodyFragment(testHtml);

    HtmlElementTransformation.LINK.apply(sitePageContext, siteset, document);
    HtmlElementTransformation.ASSET.apply(sitePageContext, siteset, document);
    HtmlElementTransformation.IMAGE.apply(sitePageContext, siteset, document);
    HtmlElementTransformation.ARTICLE.apply(sitePageContext, siteset, document);

    ArgumentCaptor<String> pathArg = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<String> journalKeyArg = ArgumentCaptor.forClass(String.class);

    verify(sitePageContext, times(4)).buildLink(pathArg.capture());
    verify(sitePageContext).buildLink(any(SiteSet.class), journalKeyArg.capture(), pathArg.capture());

    List<String> pathArgs = pathArg.getAllValues();
    assertEquals(pathArgs.get(0), "s/lorum_ipsum");
    assertEquals(pathArgs.get(1), "s/file?id=file_4321");
    assertEquals(pathArgs.get(2), "indirect/content_1234");
    assertEquals(pathArgs.get(3), "article?id=10.1371/journal.pone.0008083");
    assertEquals(pathArgs.get(4), "s/test-page");
    assertEquals(journalKeyArg.getValue(), "PLoSCompBiol");

    assertEquals(document.toString(),
            "<html>\n" +
            " <head></head>\n" +
            " <body>\n" +
            "  <img src=\"path/to/an/internal/page\" />\n" +
            "  <a href=\"path/to/an/internal/page\"></a>\n" +
            "  <a href=\"path/to/an/internal/page#anchor_string\">Lorem Ipsum</a>\n" +
            "  <a href=\"path/to/another/journal/page\">test cross-journal link</a>\n" +
            "  <a href=\"path/to/an/internal/page\">article link</a>\n" +
            " </body>\n" +
{'fraction_non_alphanumeric': 0.15884948521000164, 'fraction_numerical': 0.01617911423435202, 'mean_word_length': 4.012285012285012, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2896839', 'n_tokens_mistral': 2051, 'n_tokens_neox': 1975, 'n_words': 265}