<commit_before>package edu.harvard.iq.dataverse;

import org.junit.jupiter.api.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import static org.junit.Assert.assertEquals;

/**
 * Testing DataCitation class
 * @author <EMAIL>
 */
public class DataCitationTest {

    /**
     * Test that bibtex data export contains a closing bracket
     * @throws ParseException
     * @throws IOException
     */
    @Test
    void testWriteAsBibtexCitation() throws ParseException, IOException {
        DatasetVersion datasetVersion = createATestDatasetVersion(true, true);

        DataCitation dataCitation = new DataCitation(datasetVersion);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        dataCitation.writeAsBibtexCitation(os);
        String out = new String(os.toByteArray(), "UTF-8");
        assertEquals(
           "@data{LK0D1H_1955,\r\n" +
              "author = {First Last},\r\n" +
              "publisher = {LibraScholar},\r\n" +
              "title = {Dataset Title},\r\n" +
              "year = {1955},\r\n" +
              "doi = {10.5072/FK2/LK0D1H},\r\n" +
              "url = {https://doi.org/10.5072/FK2/LK0D1H}\r\n" +
              "}\r\n",
           out
        );
    }

    /**
     * Test that bibtex data export contains a closing bracket
     * @throws ParseException
     */
    @Test
    void testToBibtexString() throws ParseException {
        DatasetVersion datasetVersion = createATestDatasetVersion(true, true);
        DataCitation dataCitation = new DataCitation(datasetVersion);
        assertEquals(
           "@data{LK0D1H_1955,\r\n" +
              "author = {First Last},\r\n" +
              "publisher = {LibraScholar},\r\n" +
              "title = {Dataset Title},\r\n" +
              "year = {1955},\r\n" +
              "doi = {10.5072/FK2/LK0D1H},\r\n" +
              "url = {https://doi.org/10.5072/FK2/LK0D1H}\r\n" +
              "}\r\n",
           dataCitation.toBibtexString()
        );
    }

    /**
     * Test that bibtex data export contains an empty author if no author is specified
     * @throws ParseException
     */
    @Test
    void testToBibtexString_withoutAuthor() throws ParseException {
        DatasetVersion datasetVersion = createATestDatasetVersion(true, false);
        DataCitation dataCitation = new DataCitation(datasetVersion);
        assertEquals(
           "@data{LK0D1H_1955,\r\n" +
              "author = {},\r\n" +
              "publisher = {LibraScholar},\r\n" +
              "title = {Dataset Title},\r\n" +
              "year = {1955},\r\n" +
              "doi = {10.5072/FK2/LK0D1H},\r\n" +
              "url = {https://doi.org/10.5072/FK2/LK0D1H}\r\n" +
              "}\r\n",
           dataCitation.toBibtexString()
        );
    }

    /**
     * Test that bibtex data export contains an empty title if no title is specified
     * @throws ParseException
     */
    @Test
    void testToBibtexString_withoutTitle() throws ParseException {
        DatasetVersion datasetVersion = createATestDatasetVersion(false, true);
        DataCitation dataCitation = new DataCitation(datasetVersion);
        assertEquals(
           "@data{LK0D1H_1955,\r\n" +
              "author = {First Last},\r\n" +
              "publisher = {LibraScholar},\r\n" +
              "title = {},\r\n" +
              "year = {1955},\r\n" +
              "doi = {10.5072/FK2/LK0D1H},\r\n" +
              "url = {https://doi.org/10.5072/FK2/LK0D1H}\r\n" +
              "}\r\n",
           dataCitation.toBibtexString()
        );
    }

    /**
     * Test that bibtex data export contains an empty author and title if no author, nor title is specified
     * @throws ParseException
     */
    @Test
    void testToBibtexString_withoutTitleAndAuthor() throws ParseException, IOException {
        DatasetVersion datasetVersion = createATestDatasetVersion(false, false);
        DataCitation dataCitation = new DataCitation(datasetVersion);
        assertEquals(
           "@data{LK0D1H_1955,\r\n" +
              "author = {},\r\n" +
              "publisher = {LibraScholar},\r\n" +
              "title = {},\r\n" +
              "year = {1955},\r\n" +
              "doi = {10.5072/FK2/LK0D1H},\r\n" +
              "url = {https://doi.org/10.5072/FK2/LK0D1H}\r\n" +
              "}\r\n",
           dataCitation.toBibtexString()
        );
    }

    private DatasetVersion createATestDatasetVersion(boolean withTitle, boolean withAuthor) throws ParseException {
        Dataverse dataverse = new Dataverse();
        dataverse.setName("LibraScholar");

        Dataset dataset = new Dataset();
        dataset.setProtocol("doi");<commit_msg>issue #5265: adding tests to every public methods.<commit_after>package edu.harvard.iq.dataverse;

import org.apache.commons.lang.StringUtils;
import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import static junit.framework.TestCase.assertNull;
import static org.junit.Assert.assertEquals;

/**
 * Testing DataCitation class
 * @author <EMAIL>
 */
public class DataCitationTest {

    /**
     * Test the public properties of DataCitation class via their getters
     * @throws ParseException
     */
    @Test
    public void testProperties() throws ParseException {
        DataCitation dataCitation = new DataCitation(createATestDatasetVersion(true, true));
        assertEquals("First Last", dataCitation.getAuthorsString());
        assertNull(dataCitation.getFileTitle());
        assertEquals("doi:10.5072/FK2/LK0D1H", dataCitation.getPersistentId().asString());
        assertEquals("LibraScholar", dataCitation.getPublisher());
        assertEquals("Dataset Title", dataCitation.getTitle());
        assertNull(dataCitation.getUNF());
        assertEquals("V1", dataCitation.getVersion());
        assertEquals("1955", dataCitation.getYear());
    }

    /**
     * Test DataCite metadata
     * @throws ParseException
     */
    @Test
    public void testGetDataCiteMetadata() throws ParseException {
        DataCitation dataCitation = new DataCitation(createATestDatasetVersion(true, true));
        Map<String, String> properties = dataCitation.getDataCiteMetadata();
        assertEquals(4, properties.size());
        assertEquals(
           "datacite.creator, datacite.publisher, datacite.title, datacite.publicationyear",
           StringUtils.join(properties.keySet(), ", ")
        );
        assertEquals("First Last", properties.get("datacite.creator"));
        assertEquals("LibraScholar", properties.get("datacite.publisher"));
        assertEquals("Dataset Title", properties.get("datacite.title"));
        assertEquals("1955", properties.get("datacite.publicationyear"));
    }

    /**
     * Test that bibtex data export contains a closing bracket
     * @throws ParseException
     * @throws IOException
     */
    @Test
    public void testWriteAsBibtexCitation() throws ParseException, IOException {
        DatasetVersion datasetVersion = createATestDatasetVersion(true, true);

        DataCitation dataCitation = new DataCitation(datasetVersion);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        dataCitation.writeAsBibtexCitation(os);
        String out = new String(os.toByteArray(), "UTF-8");
        assertEquals(
           "@data{LK0D1H_1955,\r\n" +
           "author = {<NAME>},\r\n" +
           "publisher = {LibraScholar},\r\n" +
           "title = {Dataset Title},\r\n" +
           "year = {1955},\r\n" +
           "doi = {10.5072/FK2/LK0D1H},\r\n" +
           "url = {https://doi.org/10.5072/FK2/LK0D1H}\r\n" +
           "}\r\n",
           out
        );
    }

    /**
     * Test that bibtex data export contains a closing bracket
     * @throws ParseException
     */
    @Test
    public void testToBibtexString() throws ParseException {
        DatasetVersion datasetVersion = createATestDatasetVersion(true, true);
        DataCitation dataCitation = new DataCitation(datasetVersion);
        assertEquals(
           "@data{LK0D1H_1955,\r\n" +
           "author = {<NAME>},\r\n" +
           "publisher = {LibraScholar},\r\n" +
           "title = {Dataset Title},\r\n" +
           "year = {1955},\r\n" +
           "doi = {10.5072/FK2/LK0D1H},\r\n" +
           "url = {https://doi.org/10.5072/FK2/LK0D1H}\r\n" +
           "}\r\n",
           dataCitation.toBibtexString()
        );
    }

    /**
     * Test that bibtex data export contains an empty author if no author is specified
     * @throws ParseException
     */
    @Test
    public void testToBibtexString_withoutAuthor() throws ParseException {
        DatasetVersion datasetVersion = createATestDatasetVersion(true, false);
        DataCitation dataCitation = new DataCitation(datasetVersion);
        assertEquals(
           "@data{LK0D1H_1955,\r\n" +
           "author = {},\r\n" +
           "publisher = {LibraScholar},\r\n" +
           "title = {Dataset Title},\r\n" +
           "year = {1955},\r\n" +
           "doi = {10.5072/FK2/LK0D1H},\r\n" +
           "url = {https://doi.org/10.5072/FK2/LK0D1H}\r\n" +
           "}\r\n",
           dataCitation.toBibtexString()
        );
    }

    /**
     * Test that bibtex data export contains an empty title if no title is specified
     * @throws ParseException
     */
    @Test
    public void testToBibtexString_withoutTitle() throws ParseException {
        DatasetVersion datasetVersion = createATestDatasetVersion(false, true);
        DataCitation dataCitation = new DataCitation(datasetVersion);
        assertEquals(
           "@data{LK0D1H_1955,\r\n" +
           "author = {First Last},\r\n" +
           "publisher = {LibraScholar},\r\n" +
           "title = {},\r\n" +
           "year = {1955},\r\n" +
           "doi = {10.5072/FK2/LK0D1H},\r\n" +
           "url = {https://doi.org/10.5072/FK2/LK0D1H}\r\n" +
           "}\r\n",
           dataCitation.toBibtexString()
        );
    }

    /**
     * Test that bibtex data export contains an empty author and title if no author, nor title is specified
     * @throws ParseException
     */
    @Test
    public void testToBibtexString_withoutTitleAndAuthor() throws ParseException {
        DatasetVersion datasetVersion = createATestDatasetVersion(false, false);
        DataCitation dataCitation = new DataCitation(datasetVersion);
        assertEquals(
           "@data{LK0D1H_1955,\r\n" +
           "author = {},\r\n" +
           "publisher = {LibraScholar},\r\n" +
           "title = {},\r\n" +
           "year = {1955},\r\n" +
           "doi = {10.5072/FK2/LK0D1H},\r\n" +
           "url = {https://doi.org/10.5072/FK2/LK0D1H}\r\n" +
           "}\r\n",
           dataCitation.toBibtexString()
        );
    }

    @Test
    public void testToRISString_withTitleAndAuthor() throws ParseException {
        DatasetVersion datasetVersion = createATestDatasetVersion(true, true);
        DataCitation dataCitation = new DataCitation(datasetVersion);
        assertEquals(
           "Provider: LibraScholar\r\n" +
           "Content: text/plain; charset=\"utf-8\"\r\n" +
           "TY  - DATA\r\n" +
           "T1  - Dataset Title\r\n" +
           "AU  - First Last\r\n" +
           "DO  - doi:10.5072/FK2/LK0D1H\r\n" +
           "ET  - V1\r\n" +
           "PY  - 1955\r\n" +
           "SE  - 1955-11-05 00:00:00.0\r\n" +
           "UR  - https://doi.org/10.5072/FK2/LK0D1H\r\n" +
           "PB  - LibraScholar\r\n" +
           "ER  - \r\n",
           dataCitation.toRISString()
        );
    }

    @Test
    public void testToRISString_withoutTitleAndAuthor() throws ParseException {
        DatasetVersion datasetVersion = createATestDatasetVersion(false, false);
        DataCitation dataCitation = new DataCitation(datasetVersion);
        assertEquals(
           "Provider: LibraScholar\r\n" +
           "Content: text/plain; charset=\"utf-8\"\r\n" +
           "TY  - DATA\r\n" +
           "T1  - \r\n" +
           "DO  - doi:10.5072/FK2/LK0D1H\r\n" +
           "ET  - V1\r\n" +
           "PY  - 1955\r\n" +
           "SE  - 1955-11-05 00:00:00.0\r\n" +
           "UR  - https://doi.org/10.5072/FK2/LK0D1H\r\n" +
           "PB  - LibraScholar\r\n" +
           "ER  - \r\n",
           dataCitation.toRISString()
        );
    }

    @Test
    public void testToEndNoteString_withTitleAndAuthor() throws ParseException {
        DatasetVersion datasetVersion = createATestDatasetVersion(true, true);
        DataCitation dataCitation = new DataCitation(datasetVersion);
        assertEquals(
           "<?xml version='1.0' encoding='UTF-8'?>" +
           "<xml>" +
           "<records>" +
           "<record>" +
           "<ref-type name=\"Dataset\">59</ref-type>" +
           "<contributors>" +
           "<authors><author>First Last</author></authors>" +
           "</contributors>" +
           "<titles><title>Dataset Title</title></titles>" +
           "<section>1955-11-05</section>" +
           "<abstract />" +
           "<dates><year>1955</year></dates>" +
           "<edition>V1</edition>" +
           "<publisher>LibraScholar</publisher>" +
           "<urls><related-urls><url>https://doi.org/10.5072/FK2/LK0D1H</url></related-urls></urls>" +
           "<electronic-resource-num>doi/10.5072/FK2/LK0D1H</electronic-resource-num>" +
           "</record>" +
           "</records>" +
           "</xml>",
           dataCitation.toEndNoteString()
        );
    }

    @Test
    public void testToEndNoteString_withoutTitleAndAuthor() throws ParseException {
        DatasetVersion datasetVersion = createATestDatasetVersion(false, false);
        DataCitation dataCitation = new DataCitation(datasetVersion);
        assertEquals(
           "<?xml version='1.0' encoding='UTF-8'?>" +
           "<xml>" +
           "<records>" +
           "<record>" +
           "<ref-type name=\"Dataset\">59</ref-type>" +
           "<contributors />" +
           "<titles><title></title></titles>" +
           "<section>1955-11-05</section>" +
           "<abstract />" +
           "<dates><year>1955</year></dates>" +
           "<edition>V1</edition>" +
           "<publisher>LibraScholar</publisher>" +
           "<urls><related-urls><url>https://doi.org/10.5072/FK2/LK0D1H</url></related-urls></urls>" +
           "<electronic-resource-num>doi/10.5072/FK2/LK0D1H</electronic-resource-num>" +
           "</record>" +
           "</records>" +
           "</xml>",
           dataCitation.toEndNoteString()
        );
    }

    @Test
    public void testToString_withTitleAndAuthor() throws ParseException {
        DatasetVersion datasetVersion = createATestDatasetVersion(true, true);
        DataCitation dataCitation = new DataCitation(datasetVersion);
        assertEquals(
           "First Last, 1955, \"Dataset Title\", https://doi.org/10.5072/FK2/LK0D1H, LibraScholar, V1",
           dataCitation.toString()
        );
    }

    @Test
    public void testToString_withoutTitleAndAuthor() throws ParseException {
        DatasetVersion datasetVersion = createATestDatasetVersion(false, false);
        DataCitation dataCitation = new DataCitation(datasetVersion);
        assertEquals(
           "1955, https://doi.org/10.5072/FK2/LK0D1H, LibraScholar, V1",
           dataCitation.toString()
        );
    }

    @Test
    public void testToHtmlString_withTitleAndAuthor() throws ParseException {
        DatasetVersion datasetVersion = createATestDatasetVersion(true, true);
        DataCitation dataCitation = new DataCitation(datasetVersion);
        assertEquals(
           "First Last, 1955, \"Dataset Title\"," +
           " <a href=\"https://doi.org/10.5072/FK2/LK0D1H\" target=\"_blank\">https://doi.org/10.5072/FK2/LK0D1H</a>," +
           " LibraScholar, V1",
           dataCitation.toString(true)
        );
    }

    @Test
    public void testToHtmlString_withoutTitleAndAuthor() throws ParseException {
        DatasetVersion datasetVersion = createATestDatasetVersion(false, false);
        DataCitation dataCitation = new DataCitation(datasetVersion);
        assertEquals(
           "1955," +
           " <a href=\"https://doi.org/10.5072/FK2/LK0D1H\" target=\"_blank\">https://doi.org/10.5072/FK2/LK0D1H</a>," +
           " LibraScholar, V1",
           dataCitation.toString(true)
        );
    }

    private DatasetVersion createATestDatasetVersion(boolean withTitle, boolean withAuthor) throws ParseException {
        Dataverse dataverse = new Dataverse();
        dataverse.setName("LibraScholar");

        Dataset dataset = new Dataset();
        dataset.setProtocol("doi");
{'fraction_non_alphanumeric': 0.1359961959106039, 'fraction_numerical': 0.0315620542082739, 'mean_word_length': 2.2065942443300934, 'pattern_counts': {'":': 0, '<': 83, '<?xml version=': 2, '>': 83, 'https://': 20, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, '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': '5449918', 'n_tokens_mistral': 5545, 'n_tokens_neox': 5150, 'n_words': 910}