package com.gooddata.model;

import com.gooddata.AbstractGoodDataIT;
import com.gooddata.gdc.AsyncTask;
import com.gooddata.project.Project;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import static com.gooddata.model.ModelDiff.UpdateScript;
import static net.jadler.Jadler.onRequest;

public class ModelServiceIT extends AbstractGoodDataIT {

    private static final String DIFF_URI = "/gdc/projects/PROJECT_ID/model/diff";
    private static final String DIFF_POLL_URI = DIFF_URI + "/123";
    private static final String LDM_MANAGE2 = "/gdc/md/PROJECT_ID/ldm/manage2";
    private static final String STATUS_URI = "/gdc/md/PROJECT_ID/tasks/123/status";

    private Project project;

    @BeforeClass
    public void setUp() throws Exception {
        project = MAPPER.readValue(readResource("/project/project.json"), Project.class);
    }

    @Test
    public void shouldCreateDiff() throws Exception {
        onRequest()
                .havingMethodEqualTo("POST")
                .havingPathEqualTo(DIFF_URI)
            .respond()
                .withStatus(202)
                .withBody(MAPPER.writeValueAsString(new AsyncTask(DIFF_POLL_URI)));
        onRequest()
                .havingMethodEqualTo("GET")
                .havingPathEqualTo(DIFF_POLL_URI)
            .respond()
                .withStatus(202)
                .withBody(MAPPER.writeValueAsString(new AsyncTask(DIFF_POLL_URI)))
            .thenRespond()
                .withStatus(200)
                .withBody(readResource("/model/modelDiff.json"))
        ;

        gd.getModelService().getProjectModelDiff(project, "xxx").get();
    }

    @Test
    public void shouldUpdateModel() throws Exception {
        onRequest()
                .havingMethodEqualTo("POST")
                .havingPathEqualTo(LDM_MANAGE2)
            .respond()
                .withStatus(202)
                .withBody(readResource("/model/maqlDdlLinks.json"));
        onRequest()
                .havingMethodEqualTo("GET")
                .havingPathEqualTo(STATUS_URI)
            .respond()
                .withStatus(202)
                .withBody(MAPPER.writeValueAsString(new MaqlDdlTaskStatus("RUNNING", STATUS_URI)))
            .thenRespond()
                .withStatus(200)
                .withBody(MAPPER.writeValueAsString(new MaqlDdlTaskStatus("OK", STATUS_URI)))
        ;

        final ModelDiff diff = new ModelDiff(new UpdateScript(true, false,
                "synchronize {dataset.person} preserve data",
                "synchronize {dataset.person} preserve data"
        ));
        gd.getModelService().updateProjectModel(project, diff).get();
    }
}
@@ -313,16 +313,167 @@
 Request;
+%0Aimport static org.hamcrest.CoreMatchers.is;%0Aimport static org.hamcrest.CoreMatchers.notNullValue;%0Aimport static org.hamcrest.MatcherAssert.assertThat;
 %0A%0Apublic
@@ -1727,32 +1727,55 @@
       ;%0A%0A       
+ final ModelDiff diff =
  gd.getModelServ
@@ -1818,24 +1818,412 @@
 xxx%22).get();
+%0A%0A        assertThat(diff, is(notNullValue()));%0A        assertThat(diff.getUpdateMaql().get(0), is(%22CREATE FOLDER %7Bffld.employee%7D VISUAL(TITLE %5C%22Employee%5C%22) TYPE FACT;%5CnCREATE FACT %7Bfact.employee.age%7D VISUAL(TITLE %5C%22Employee Age%5C%22, FOLDER %7Bffld.employee%7D) AS %7Bf_employee.f_age%7D;%5CnALTER DATASET %7Bdataset.employee%7D ADD %7Bfact.employee.age%7D;%5CnSYNCHRONIZE %7Bdataset.employee%7D PRESERVE DATA;%22));
 %0A    %7D%0A%0A    
@@ -3057,38 +3057,38 @@
 ronize %7Bdataset.
-person
+chunk1
 %7D preserve data%22
@@ -3131,14 +3131,14 @@
 set.
-person
+chunk2
 %7D pr

add asserts to ModelServiceIT