/*
 * Copyright (C) 2004-2017, GoodData(R) Corporation. All rights reserved.
 * This source code is licensed under the BSD-style license found in the
 * LICENSE.txt file in the root directory of this source tree.
 */
package com.gooddata.dataset;

import static com.gooddata.util.ResourceUtils.readFromResource;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.Matchers.greaterThan;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.fail;

import com.gooddata.AbstractGoodDataAT;
import static org.hamcrest.MatcherAssert.assertThat;

import org.testng.annotations.Test;

import java.util.Collection;

/**
 * Dataset acceptance tests.
 */
public class DatasetServiceAT extends AbstractGoodDataAT {

    @Test(groups = "dataset", dependsOnGroups = {"md", "datastore"})
    public void loadDataset() throws Exception {
        final DatasetService datasetService = gd.getDatasetService();

        final DatasetManifest manifest = datasetService.getDatasetManifest(project, "dataset.person");
        datasetService.loadDataset(project, manifest, readFromResource("/person.csv")).get();
    }

    @Test(groups = "dataset", dependsOnMethods = {"loadDataset"})
    public void loadDatasetBatch() throws Exception {
        final DatasetService datasetService = gd.getDatasetService();

        final DatasetManifest personManifest = datasetService.getDatasetManifest(project, "dataset.person");
        personManifest.setSource(readFromResource("/person.csv"));
        final DatasetManifest cityManifest = datasetService.getDatasetManifest(project, "dataset.city");
        cityManifest.setSource(readFromResource("/city.csv"));

        datasetService.loadDatasets(project, personManifest, cityManifest).get();
    }

    @Test(groups = "dataset", dependsOnMethods = "loadDatasetBatch")
    public void updateData() {
        final DatasetService datasetService = gd.getDatasetService();
        datasetService.updateProjectData(project, "DELETE FROM {attr.person.name} WHERE {label.person.name} = \"not exists\";");
    }

    @Test(groups = "dataset", dependsOnGroups = {"md", "datastore"})
    public void loadDatasetFail(){
        final DatasetService datasetService = gd.getDatasetService();
        final DatasetManifest manifest = datasetService.getDatasetManifest(project, "dataset.person");
        try {
            datasetService.loadDataset(project, manifest, readFromResource("/corruptedPerson.csv")).get();
            fail();
        } catch (DatasetException ex){
            assertThat(ex.getMessage(),is(equalTo("Load datasets [dataset.person] failed: [Number of columns doesn't correspond in dataset.person.csv at line 3]")));
        }
    }

    @Test(groups = "dataset", dependsOnMethods = {"loadDataset"})
    public void loadDatasetBatchFail() throws Exception {
        final DatasetService datasetService = gd.getDatasetService();

        final DatasetManifest personManifest = datasetService.getDatasetManifest(project, "dataset.person");
        personManifest.setSource(readFromResource("/corruptedPerson.csv"));
        final DatasetManifest cityManifest = datasetService.getDatasetManifest(project, "dataset.city");
        cityManifest.setSource(readFromResource("/city.csv"));

        try {
            datasetService.loadDatasets(project, personManifest, cityManifest).get();
            fail();
        } catch (DatasetException ex){
            assertThat(ex.getMessage(),is(equalTo("Load datasets [dataset.person, dataset.city] failed: [Number of columns doesn't correspond in dataset.person.csv at line 3]")));
        }
    }

    @Test(groups = "dataset", dependsOnMethods = {"loadDataset"})
    public void listUploadsForDataset() throws Exception {
        final Collection<Upload> uploads = gd.getDatasetService().listUploadsForDataset(project, "dataset.person");

        assertThat(uploads, notNullValue());
        assertFalse(uploads.isEmpty());
    }

    @Test(groups = "dataset", dependsOnMethods = {"loadDataset"})
    public void getLastUploadForDataset() throws Exception {
        final Upload lastUpload = gd.getDatasetService().getLastUploadForDataset(project, "dataset.person");

        assertThat(lastUpload, notNullValue());
    }

    @Test(groups = "dataset", dependsOnMethods = {"loadDataset", "loadDatasetFail"})
    public void getUploadStatistics() throws Exception {
        final UploadStatistics uploadStatistics = gd.getDatasetService().getUploadStatistics(project);

        assertThat(uploadStatistics, notNullValue());
        assertThat(uploadStatistics.getUploadsCount("OK"), greaterThan(0));
        assertThat(uploadStatistics.getUploadsCount("ERROR"), greaterThan(0));
    }
}

@@ -237,24 +237,131 @@
 a.dataset;%0A%0A
+import com.gooddata.AbstractGoodDataAT;%0Aimport org.testng.annotations.Test;%0A%0Aimport java.util.Collection;%0A%0A
 import stati
@@ -452,23 +452,28 @@
 atchers.
-equalTo
+notNullValue
 ;%0Aimport
@@ -497,23 +497,32 @@
 est.
-Core
 Matcher
-s.is
+Assert.assertThat
 ;%0Aim
@@ -538,36 +538,32 @@
 ic org.hamcrest.
-Core
 Matchers.notNull
@@ -551,36 +551,35 @@
 st.Matchers.
-notNullValue
+greaterThan
 ;%0Aimport sta
@@ -595,35 +595,49 @@
 amcrest.
+text.
 Matche
-rs.gre
+sP
 at
+t
 er
-Tha
+n.matchesPatter
 n;%0Aimpor
@@ -719,264 +719,277 @@
 l;%0A%0A
-import com.gooddata.AbstractGoodDataAT;%0Aimport static org.hamcrest.MatcherAssert.assertThat;%0A%0Aimport org.testng.annotations.Test;%0A%0Aimport java.util.Collection;%0A%0A/**%0A * Dataset acceptance tests.%0A */%0Apublic class DatasetServiceAT extends AbstractGoodDataAT %7B
+/**%0A * Dataset acceptance tests.%0A */%0Apublic class DatasetServiceAT extends AbstractGoodDataAT %7B%0A%0A    private static final String FAILED_LOAD_PATTERN = %22Number of columns does(n't%7C not) co%5Br%5D%7B1,2%7Despond (in dataset.person.csv at line 3%7Con line 3 in dataset.person.csv)%22;
 %0A%0A  
@@ -2810,131 +2810,43 @@
 e(),
-is(equalTo(%22Load datasets %5Bdataset.person%5D failed: %5BNumber of columns doesn't correspond in dataset.person.csv at line 3%5D%22)
+ matchesPattern(FAILED_LOAD_PATTERN
 ));%0A
@@ -3613,18 +3613,23 @@
 e(),
-is(equalTo
+ matchesPattern
 (%22Lo
@@ -3684,81 +3684,39 @@
 d: %5B
-Number of columns doesn't correspond in dataset.person.csv at line 3
+%22 + FAILED_LOAD_PATTERN + %22
 %5D%22))
-)
 ;%0A  

make match failed dataset load message less strict