<commit_before>package org.gbif.dwc.record;

import org.gbif.dwc.terms.DwcTerm;
import org.gbif.dwc.terms.GbifTerm;

import java.util.Date;
import java.util.List;
import java.util.Set;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import org.apache.commons.beanutils.BeanUtils;
import org.junit.Test;

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

public class DarwinCoreTaxonTest {

  @Test
  public void testGetFullScientificName() {
    DarwinCoreTaxon dwc = new DarwinCoreTaxon("Festuca x brinkmannii A. Braun");
    dwc.setScientificNameAuthorship("<NAME>");
    assertEquals("Festuca x brinkmannii A. Braun", dwc.getFullScientificName());
    assertEquals(dwc.getFullScientificName(), dwc.getLowestScientificName());

    dwc = new DarwinCoreTaxon("Festuca x brinkmannii A. Braun");
    dwc.setScientificNameAuthorship("A.Braun");
    assertEquals("Festuca x brinkmannii A. Braun", dwc.getFullScientificName());
    assertEquals(dwc.getFullScientificName(), dwc.getLowestScientificName());

    dwc = new DarwinCoreTaxon("Festuca x brinkmannii");
    dwc.setScientificNameAuthorship("<NAME>");
    assertEquals("Festuca x brinkmannii A. Braun", dwc.getFullScientificName());
    assertEquals(dwc.getFullScientificName(), dwc.getLowestScientificName());

    dwc = new DarwinCoreTaxon("Festuca A. Braun brinkmannii subsp. brinkmannii");
    dwc.setScientificNameAuthorship("<NAME>");
    assertEquals("Festuca A. Braun brinkmannii subsp. brinkmannii", dwc.getFullScientificName());
    assertEquals(dwc.getFullScientificName(), dwc.getLowestScientificName());

    dwc = new DarwinCoreTaxon("Festuca brinkmannii");
    dwc.setScientificNameAuthorship("<NAME>");
    assertEquals("Festuca brinkmannii A. Braun", dwc.getFullScientificName());
    assertEquals(dwc.getFullScientificName(), dwc.getLowestScientificName());

    dwc = new DarwinCoreTaxon(" ");
    dwc.setScientificNameAuthorship("Rafinesque, 1795");
    dwc.setKingdom("Plantae");
    assertNull(dwc.getFullScientificName());
    assertEquals("Plantae", dwc.getLowestScientificName());

    dwc = new DarwinCoreTaxon(" ");
    dwc.setScientificNameAuthorship("L.");
    dwc.setKingdom("Plantae");
    dwc.setFamily("Asteraceae");
    assertNull(dwc.getFullScientificName());
    assertEquals("Asteraceae", dwc.getLowestScientificName());

    dwc = new DarwinCoreTaxon();
    dwc.setScientificNameAuthorship("L.");
    dwc.setKingdom("Plantae");
    dwc.setGenus("Poa");
    assertEquals("Poa", dwc.getFullScientificName());
    assertEquals(dwc.getFullScientificName(), dwc.getLowestScientificName());

    dwc.setSpecificEpithet("annua");
    assertEquals("Poa annua L.", dwc.getFullScientificName());
    assertEquals(dwc.getFullScientificName(), dwc.getLowestScientificName());

    dwc.setScientificNameAuthorship("");
    assertEquals("Poa annua", dwc.getFullScientificName());
    assertEquals(dwc.getFullScientificName(), dwc.getLowestScientificName());

    dwc.setScientificNameAuthorship("L.");
    dwc.setInfraspecificEpithet("alpina");
    assertEquals("Poa annua alpina L.", dwc.getFullScientificName());
    assertEquals(dwc.getFullScientificName(), dwc.getLowestScientificName());

    dwc.setVerbatimTaxonRank("var.");
    assertEquals("Poa annua alpina L.", dwc.getFullScientificName());
    assertEquals(dwc.getFullScientificName(), dwc.getLowestScientificName());

    // test not identical authorships
    dwc = new DarwinCoreTaxon();
    dwc.setScientificName("<NAME>, 2000");
    dwc.setGenus("Aagaardia");
    dwc.setSpecificEpithet("protensa");
    dwc.setScientificNameAuthorship("Saether 2000");
    assertEquals(dwc.getScientificName(), dwc.getFullScientificName());

    // test genus
    dwc = new DarwinCoreTaxon();
    dwc.setScientificName("<NAME>, 2000");
    dwc.setGenus("Aagaardia");
    dwc.setSpecificEpithet("protensa");
    dwc.setScientificNameAuthorship("Saether 2000");
    assertEquals(dwc.getScientificName(), dwc.getFullScientificName());

    // test authors with single letter that is part of the name
    dwc = new DarwinCoreTaxon();
    dwc.setScientificName("<NAME>");
    dwc.setGenus("Trifolium");
    dwc.setSpecificEpithet("repens");
    dwc.setScientificNameAuthorship("L.");
    assertEquals("Trifolium repens", dwc.getScientificName());
    assertEquals("Trifolium repens L.", dwc.getFullScientificName());

    dwc = new DarwinCoreTaxon();
    dwc.setScientificName("Trifolium thalii");
    dwc.setScientificNameAuthorship("Vill.");
    assertEquals("Trifolium thalii Vill.", dwc.getFullScientificName());

    dwc = new DarwinCoreTaxon();
    dwc.setScientificName("Trifolium alpestre");
    dwc.setScientificNameAuthorship("L.");
    assertEquals("Trifolium alpestre L.", dwc.getFullScientificName());

    // autonyms ???
    dwc = new DarwinCoreTaxon();
    dwc.setScientificName("Trifolium alpestre alpestre");
    dwc.setScientificNameAuthorship("L.");
    // this would be really correct for autonyms...
    //assertEquals("Trifolium alpestre L. alpestre", dwc.getFullScientificName());
    assertEquals("Trifolium alpestre alpestre L.", dwc.getFullScientificName());
  }

  @Test
  public void testInterpretedRank() {
    DarwinCoreTaxon dwc = new DarwinCoreTaxon("Festuca brinkmannii A. Braun");
    dwc.setKingdom("PLANTAE");
    dwc.setPhylum("Magnoliophyta");
    dwc.setFamily("Poaceae");
    dwc.setGenus("Festuca");
    dwc.setTaxonRank("species");
    assertEquals("species", dwc.getTaxonRankInterpreted());

    dwc.setSpecificEpithet("brinkmannii");
    assertEquals("species", dwc.getTaxonRankInterpreted());

    dwc.setInfraspecificEpithet("nonexistii");
    assertEquals("species", dwc.getTaxonRankInterpreted());

    dwc.setScientificName(null);
    assertEquals("species", dwc.getTaxonRankInterpreted());

    dwc.setTaxonRank(null);
    assertEquals("infraspecies", dwc.getTaxonRankInterpreted());

    dwc.setVerbatimTaxonRank("var.");
    assertEquals("var.", dwc.getTaxonRankInterpreted());

    dwc.setSpecificEpithet(null);
    assertEquals("genus", dwc.getTaxonRankInterpreted());

    dwc.setGenus(null);
    assertEquals("family", dwc.getTaxonRankInterpreted());
  }

  @Test
  public void testHigherClassification() {
    DarwinCoreTaxon dwc = new DarwinCoreTaxon("Festuca brinkmannii A. Braun");
    dwc.setKingdom("PLANTAE");
    dwc.setPhylum("Magnoliophyta");
    dwc.setFamily("Poaceae");
    dwc.setGenus("Festuca");
    assertEquals("Plantae;Magnoliophyta;Poaceae;Festuca", dwc.buildHigherClassification(null));
    assertEquals("Plantae;Magnoliophyta;Poaceae", dwc.buildHigherClassification('f'));
    assertEquals("Plantae;Magnoliophyta", dwc.buildHigherClassification('o'));
  }

  @Test
  public void testProperties() {<commit_msg>Amended the higherClassification seperator in tests to match ' | '.<commit_after>package org.gbif.dwc.record;

import org.gbif.dwc.terms.DwcTerm;
import org.gbif.dwc.terms.GbifTerm;

import java.util.Date;
import java.util.List;
import java.util.Set;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import org.apache.commons.beanutils.BeanUtils;
import org.junit.Test;

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

public class DarwinCoreTaxonTest {

  @Test
  public void testGetFullScientificName() {
    DarwinCoreTaxon dwc = new DarwinCoreTaxon("Festuca x brinkmannii A. Braun");
    dwc.setScientificNameAuthorship("A. Braun");
    assertEquals("Festuca x brinkmannii A. Braun", dwc.getFullScientificName());
    assertEquals(dwc.getFullScientificName(), dwc.getLowestScientificName());

    dwc = new DarwinCoreTaxon("Festuca x brinkmannii A. Braun");
    dwc.setScientificNameAuthorship("A.Braun");
    assertEquals("Festuca x brinkmannii A. Braun", dwc.getFullScientificName());
    assertEquals(dwc.getFullScientificName(), dwc.getLowestScientificName());

    dwc = new DarwinCoreTaxon("Festuca x brinkmannii");
    dwc.setScientificNameAuthorship("A. Braun");
    assertEquals("Festuca x brinkmannii A. Braun", dwc.getFullScientificName());
    assertEquals(dwc.getFullScientificName(), dwc.getLowestScientificName());

    dwc = new DarwinCoreTaxon("Festuca A. Braun brinkmannii subsp. brinkmannii");
    dwc.setScientificNameAuthorship("<NAME>");
    assertEquals("Festuca A. Braun brinkmannii subsp. brinkmannii", dwc.getFullScientificName());
    assertEquals(dwc.getFullScientificName(), dwc.getLowestScientificName());

    dwc = new DarwinCoreTaxon("Festuca brinkmannii");
    dwc.setScientificNameAuthorship("<NAME>");
    assertEquals("Festuca brinkmannii A. Braun", dwc.getFullScientificName());
    assertEquals(dwc.getFullScientificName(), dwc.getLowestScientificName());

    dwc = new DarwinCoreTaxon(" ");
    dwc.setScientificNameAuthorship("Rafinesque, 1795");
    dwc.setKingdom("Plantae");
    assertNull(dwc.getFullScientificName());
    assertEquals("Plantae", dwc.getLowestScientificName());

    dwc = new DarwinCoreTaxon(" ");
    dwc.setScientificNameAuthorship("L.");
    dwc.setKingdom("Plantae");
    dwc.setFamily("Asteraceae");
    assertNull(dwc.getFullScientificName());
    assertEquals("Asteraceae", dwc.getLowestScientificName());

    dwc = new DarwinCoreTaxon();
    dwc.setScientificNameAuthorship("L.");
    dwc.setKingdom("Plantae");
    dwc.setGenus("Poa");
    assertEquals("Poa", dwc.getFullScientificName());
    assertEquals(dwc.getFullScientificName(), dwc.getLowestScientificName());

    dwc.setSpecificEpithet("annua");
    assertEquals("Poa annua L.", dwc.getFullScientificName());
    assertEquals(dwc.getFullScientificName(), dwc.getLowestScientificName());

    dwc.setScientificNameAuthorship("");
    assertEquals("Poa annua", dwc.getFullScientificName());
    assertEquals(dwc.getFullScientificName(), dwc.getLowestScientificName());

    dwc.setScientificNameAuthorship("L.");
    dwc.setInfraspecificEpithet("alpina");
    assertEquals("Poa annua alpina L.", dwc.getFullScientificName());
    assertEquals(dwc.getFullScientificName(), dwc.getLowestScientificName());

    dwc.setVerbatimTaxonRank("var.");
    assertEquals("Poa annua alpina L.", dwc.getFullScientificName());
    assertEquals(dwc.getFullScientificName(), dwc.getLowestScientificName());

    // test not identical authorships
    dwc = new DarwinCoreTaxon();
    dwc.setScientificName("<NAME>, 2000");
    dwc.setGenus("Aagaardia");
    dwc.setSpecificEpithet("protensa");
    dwc.setScientificNameAuthorship("Saether 2000");
    assertEquals(dwc.getScientificName(), dwc.getFullScientificName());

    // test genus
    dwc = new DarwinCoreTaxon();
    dwc.setScientificName("<NAME>, 2000");
    dwc.setGenus("Aagaardia");
    dwc.setSpecificEpithet("protensa");
    dwc.setScientificNameAuthorship("Saether 2000");
    assertEquals(dwc.getScientificName(), dwc.getFullScientificName());

    // test authors with single letter that is part of the name
    dwc = new DarwinCoreTaxon();
    dwc.setScientificName("<NAME>");
    dwc.setGenus("Trifolium");
    dwc.setSpecificEpithet("repens");
    dwc.setScientificNameAuthorship("L.");
    assertEquals("Trifolium repens", dwc.getScientificName());
    assertEquals("Trifolium repens L.", dwc.getFullScientificName());

    dwc = new DarwinCoreTaxon();
    dwc.setScientificName("Trifolium thalii");
    dwc.setScientificNameAuthorship("Vill.");
    assertEquals("Trifolium thalii Vill.", dwc.getFullScientificName());

    dwc = new DarwinCoreTaxon();
    dwc.setScientificName("Trifolium alpestre");
    dwc.setScientificNameAuthorship("L.");
    assertEquals("Trifolium alpestre L.", dwc.getFullScientificName());

    // autonyms ???
    dwc = new DarwinCoreTaxon();
    dwc.setScientificName("Trifolium alpestre alpestre");
    dwc.setScientificNameAuthorship("L.");
    // this would be really correct for autonyms...
    //assertEquals("Trifolium alpestre L. alpestre", dwc.getFullScientificName());
    assertEquals("Trifolium alpestre alpestre L.", dwc.getFullScientificName());
  }

  @Test
  public void testInterpretedRank() {
    DarwinCoreTaxon dwc = new DarwinCoreTaxon("Festuca brinkmannii A. Braun");
    dwc.setKingdom("PLANTAE");
    dwc.setPhylum("Magnoliophyta");
    dwc.setFamily("Poaceae");
    dwc.setGenus("Festuca");
    dwc.setTaxonRank("species");
    assertEquals("species", dwc.getTaxonRankInterpreted());

    dwc.setSpecificEpithet("brinkmannii");
    assertEquals("species", dwc.getTaxonRankInterpreted());

    dwc.setInfraspecificEpithet("nonexistii");
    assertEquals("species", dwc.getTaxonRankInterpreted());

    dwc.setScientificName(null);
    assertEquals("species", dwc.getTaxonRankInterpreted());

    dwc.setTaxonRank(null);
    assertEquals("infraspecies", dwc.getTaxonRankInterpreted());

    dwc.setVerbatimTaxonRank("var.");
    assertEquals("var.", dwc.getTaxonRankInterpreted());

    dwc.setSpecificEpithet(null);
    assertEquals("genus", dwc.getTaxonRankInterpreted());

    dwc.setGenus(null);
    assertEquals("family", dwc.getTaxonRankInterpreted());
  }

  @Test
  public void testHigherClassification() {
    DarwinCoreTaxon dwc = new DarwinCoreTaxon("Festuca brinkmannii A. Braun");
    dwc.setKingdom("PLANTAE");
    dwc.setPhylum("Magnoliophyta");
    dwc.setFamily("Poaceae");
    dwc.setGenus("Festuca");
    assertEquals("Plantae | Magnoliophyta | Poaceae | Festuca", dwc.buildHigherClassification(null));
    assertEquals("Plantae | Magnoliophyta | Poaceae", dwc.buildHigherClassification('f'));
    assertEquals("Plantae | Magnoliophyta", dwc.buildHigherClassification('o'));
  }

  @Test
  public void testProperties() {
{'fraction_non_alphanumeric': 0.13597069597069597, 'fraction_numerical': 0.0029304029304029304, 'mean_word_length': 5.296586715867159, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '135141', 'n_tokens_mistral': 4814, 'n_tokens_neox': 4456, 'n_words': 676}