package org.gbif.checklistbank.service.mybatis;

import org.gbif.api.model.checklistbank.ParsedName;
import org.gbif.api.vocabulary.NamePart;
import org.gbif.api.vocabulary.NameType;
import org.gbif.api.vocabulary.Rank;
import org.gbif.checklistbank.service.ParsedNameService;
import org.gbif.nameparser.NameParser;

import org.junit.Test;

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

public class ParsedNameServiceMyBatisIT extends MyBatisServiceITBase<ParsedNameService> {

  public ParsedNameServiceMyBatisIT() {
    super(ParsedNameService.class);
  }

  @Test
  public void testCreateOrGet() throws Exception {
    ParsedName pn = new ParsedName();
    pn.setScientificName("Abies alba Mill.");
    pn.setGenusOrAbove("Abies");
    pn.setAuthorship("Mill.");
    pn.setSpecificEpithet("alba");
    pn.setType(NameType.SCIENTIFIC);
    assertNull(pn.getKey());

    ParsedName pn2 = service.createOrGet(pn);
    assertNotNull(pn2.getKey());
    assertEquals("Abies alba Mill.", pn2.getScientificName());
    assertEquals("Abies alba", pn2.canonicalName());
    assertEquals("Abies", pn2.getGenusOrAbove());
    assertEquals("alba", pn2.getSpecificEpithet());
    assertEquals("Mill.", pn2.getAuthorship());

    final NameParser parser = new NameParser();
    pn = service.createOrGet(parser.parse("Abies alba Mill.", null));
    assertEquals("Abies alba Mill.", pn.getScientificName());
    assertEquals("Abies alba", pn.canonicalName());
    assertEquals("Abies", pn.getGenusOrAbove());
    assertEquals("alba", pn.getSpecificEpithet());
    assertEquals("Mill.", pn.getAuthorship());

    pn = service.createOrGet(parser.parse("Abies sp.", null));
    assertEquals("Abies sp.", pn.getScientificName());
    assertEquals("Abies spec.", pn.canonicalName());
    assertEquals("Abies", pn.getGenusOrAbove());
    assertEquals("sp.", pn.getRankMarker());
    assertEquals(Rank.SPECIES, pn.getRank());
    assertNull(pn.getSpecificEpithet());

    pn = service.createOrGet(parser.parse("×Abies Mill.", null));
    assertEquals("×Abies Mill.", pn.getScientificName());
    assertEquals("Abies", pn.canonicalName());
    assertEquals("Abies", pn.getGenusOrAbove());
    assertNull(pn.getRankMarker());
    assertNull(pn.getRank());
    assertNull(pn.getSpecificEpithet());
    assertEquals(NamePart.GENERIC, pn.getNotho());

    pn = service.createOrGet(parser.parse("? hostilis Gravenhorst, 1829", null));
    assertEquals("? hostilis Gravenhorst, 1829", pn.getScientificName());
    assertNull(pn.canonicalName());
    assertNull(pn.getGenusOrAbove());
    assertNull(pn.getRankMarker());
    assertNull(pn.getRank());
    assertNull(pn.getSpecificEpithet());
  }

  @Test
  public void testReparse() throws Exception {
    assertEquals(1150, service.reparseAll());
  }

  @Test
  public void testOrphaned() throws Exception {
    assertEquals(1107, ((ParsedNameServiceMyBatis) service).deleteOrphaned());
  }

}
@@ -308,16 +308,64 @@
 eParser;
+%0Aimport org.gbif.nameparser.UnparsableException;
 %0A%0Aimport
@@ -608,16 +608,65 @@
 ice%3E %7B%0A%0A
+  private NameParser parser = new NameParser();%0A%0A
   public
@@ -1397,56 +1397,8 @@
 );%0A%0A
-    final NameParser parser = new NameParser();%0A
     
@@ -1414,39 +1414,32 @@
 ice.createOrGet(
-parser.
 parse(%22Abies alb
@@ -1442,30 +1442,24 @@
  alba Mill.%22
-, null
 ));%0A    asse
@@ -1737,31 +1737,24 @@
 createOrGet(
-parser.
 parse(%22Abies
@@ -1758,22 +1758,16 @@
 ies sp.%22
-, null
 ));%0A    
@@ -2077,31 +2077,24 @@
 createOrGet(
-parser.
 parse(%22%C3%97Abie
@@ -2101,22 +2101,16 @@
 s Mill.%22
-, null
 ));%0A    
@@ -2447,23 +2447,16 @@
 teOrGet(
-parser.
 parse(%22?
@@ -2487,14 +2487,8 @@
 829%22
-, null
 ));%0A
@@ -2739,32 +2739,279 @@
 pithet());%0A  %7D%0A%0A
+  private ParsedName parse(String x) %7B%0A    try %7B%0A      return parser.parse(x, null);%0A    %7D catch (UnparsableException e) %7B%0A      ParsedName pn = new ParsedName();%0A      pn.setScientificName(x);%0A      pn.setType(e.type);%0A      return pn;%0A    %7D%0A  %7D%0A
   @Test%0A  public

fix test