package life.catalogue.resources;

import java.time.LocalDate;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.Response;

import life.catalogue.api.model.Dataset;
import life.catalogue.api.model.Page;
import life.catalogue.api.model.ResultPage;
import life.catalogue.api.search.DatasetSearchRequest;
import life.catalogue.api.vocab.DataFormat;
import life.catalogue.api.vocab.DatasetOrigin;
import life.catalogue.api.vocab.DatasetType;
import life.catalogue.api.vocab.Frequency;
import life.catalogue.db.mapper.TestDataRule;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;

import static life.catalogue.api.TestEntityGenerator.nullifyUserDate;
import static life.catalogue.dw.ApiUtils.*;
import static org.junit.Assert.*;

public class DatasetResourceTest extends ResourceTestBase {
  
  static GenericType<ResultPage<Dataset>> RESULT_PAGE = new GenericType<ResultPage<Dataset>>() {};
  
  @Rule
  public TestDataRule testDataRule = TestDataRule.datasets(RULE.getSqlSessionFactory());

  public DatasetResourceTest() {
    super("/dataset");
  }
  
  @Test
  public void list() {
    Page page = new Page(0,10);
    ResultPage<Dataset> resp = applyPage(base, page).request().get(RESULT_PAGE);
    
    assertEquals(10, resp.size());
    assertTrue(resp.getTotal() > 200);
    for (Dataset d : resp) {
      assertNotNull(d);
    }
  
    DatasetSearchRequest req = DatasetSearchRequest.byQuery("Catalogue");
    req.setSortBy(DatasetSearchRequest.SortBy.TITLE);
    resp = applySearch(base, req, page).request().get(RESULT_PAGE);
  
    assertEquals(10, resp.size());
    assertEquals("A World Catalogue of Centipedes (Chilopoda) for the Web", resp.getResult().get(0).getTitle());
  
    req.setFormat(DataFormat.DWCA);
    resp = applySearch(base, req, page).request().get(RESULT_PAGE);
  
    assertEquals(5, resp.size());
    assertEquals("Catalogue of Afrotropical Bees", resp.getResult().get(0).getTitle());
  }
  
  @Test
  public void create() {
    Dataset d = new Dataset();
    d.setTitle("s3s3derftg");
    d.setType(DatasetType.OTHER);
    d.setOrigin(DatasetOrigin.UPLOADED);
    d.setContact("me");
    d.setReleased(LocalDate.now());
    d.setImportFrequency(Frequency.MONTHLY);
    Integer key = editorCreds(base).post(json(d), Integer.class);
    d.setKey(key);
    
    Dataset d2 = base.path(key.toString()).request().get(Dataset.class);
    
    assertEquals(nullifyUserDate(d2), nullifyUserDate(d));
  }
  
  @Test
  public void get() {
    Dataset d = base.path("1008").request().get(Dataset.class);
    assertNotNull(d);
    assertNull(d.getDeleted());
    assertEquals("The Reptile Database", d.getTitle());
  }
  
  @Test
  @Ignore
  public void update() {
  }
  
  @Test
  public void delete() {
    Response resp = editorCreds(base.path("2035")).delete();
    assertEquals(204, resp.getStatus());
  
    Dataset d = base.path("2035").request().get(Dataset.class);
    assertNotNull(d.getDeleted());
  }
  
  
  
  @Test
  @Ignore
  public void getImports() {
  }
  
  @Test
  @Ignore
  public void logo() {
  }
  
  @Test
  @Ignore
  public void uploadLogo() {
  }
  
  @Test
  @Ignore
  public void deleteLogo() {
  }
}
@@ -32,108 +32,8 @@
 s;%0A%0A
-import java.time.LocalDate;%0Aimport javax.ws.rs.core.GenericType;%0Aimport javax.ws.rs.core.Response;%0A%0A
 impo
@@ -69,16 +69,16 @@
 ataset;%0A
+
 import l
@@ -503,16 +503,157 @@
 .Test;%0A%0A
+import javax.ws.rs.ClientErrorException;%0Aimport javax.ws.rs.core.GenericType;%0Aimport javax.ws.rs.core.Response;%0Aimport java.time.LocalDate;%0A%0A
 import s
@@ -2264,16 +2264,358 @@
 NTHLY);%0A
+    try %7B%0A      editorCreds(base).post(json(d), Integer.class);%0A      fail(%22Expected validation error%22);%0A    %7D catch (ClientErrorException e) %7B%0A      // expect a 422 validation error!%0A      if (e.getResponse().getStatus() != 422) %7B%0A        fail(%22Expected HTTP 422%22);%0A      %7D%0A    %7D%0A    // add data format%0A    d.setDataFormat(DataFormat.ACEF);%0A
     Inte
@@ -3342,21 +3342,15 @@
 ));%0A
+
   %7D%0A  %0A
-  %0A  %0A
   @T

Extend test for dataset new validation