/*
 * 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.md;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

import com.gooddata.AbstractGoodDataAT;
import com.gooddata.md.maintenance.PartialMdExport;
import com.gooddata.md.maintenance.PartialMdExportToken;
import com.gooddata.model.ModelDiff;
import com.gooddata.model.ModelService;
import com.gooddata.project.Environment;
import com.gooddata.project.Project;
import org.apache.commons.lang3.StringUtils;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.io.InputStreamReader;

/**
 * Metadata export/import acceptance tests.
 */
public class ExportImportServiceAT extends AbstractGoodDataAT {

    private Project importProject;
    private PartialMdExportToken partialMdExportToken;

    @BeforeClass
    public void setUp() throws Exception {
        final Project newProject = new Project(title + " - metadata import", projectToken);
        newProject.setEnvironment(Environment.TESTING);
        importProject = gd.getProjectService().createProject(newProject).get();

        final ModelService modelService = gd.getModelService();
        final ModelDiff projectModelDiff = modelService.getProjectModelDiff(importProject,
                new InputStreamReader(getClass().getResourceAsStream("/person.json"))).get();
        modelService.updateProjectModel(importProject, projectModelDiff).get();
    }

    @AfterClass
    public void tearDown() throws Exception {
        if (importProject != null) {
            gd.getProjectService().removeProject(importProject);
        }
    }

    @Test(groups = "exportimport", dependsOnGroups = "md")
    public void partialExportMetric() throws Exception {
        partialMdExportToken = gd.getExportImportService().partialExport(project, new PartialMdExport(metric.getUri())).get();
        assertThat(partialMdExportToken, notNullValue());
        assertThat(partialMdExportToken.getToken(), not(isEmptyOrNullString()));
        assertThat(partialMdExportToken.isImportAttributeProperties(), is(false));
    }

    @Test(groups = "exportimport", dependsOnMethods = "partialExportMetric")
    public void partialImportMetric() throws Exception {
        gd.getExportImportService().partialImport(importProject, partialMdExportToken).get();
        final Metric importedMetric = gd.getMetadataService().getObjById(importProject,
                StringUtils.substringAfterLast(metric.getUri(), "/"), Metric.class);

        assertThat(importedMetric, notNullValue());
    }
}

@@ -236,16 +236,70 @@
 ta.md;%0A%0A
+import static com.gooddata.md.Restriction.identifier;%0A
 import s
@@ -691,53 +691,8 @@
 ct;%0A
-import org.apache.commons.lang3.StringUtils;%0A
 impo
@@ -842,16 +842,45 @@
 mReader;
+%0Aimport java.util.Collection;
 %0A%0A/**%0A *
@@ -2579,32 +2579,33 @@
 rtToken).get();%0A
+%0A
         final Me
@@ -2606,168 +2606,254 @@
 nal 
-Metric importedMetric = gd.getMetadataService().getObjById(importProject,%0A                StringUtils.substringAfterLast(metric.getUri(), %22/%22), Metric.class
+Collection%3CEntry%3E collection = gd.getMetadataService()%0A                .find(importProject, Metric.class, identifier(metric.getIdentifier()));%0A%0A        assertThat(collection, hasSize(1));%0A        final Entry entry = collection.iterator().next(
 );%0A
-%0A
     
@@ -2871,39 +2871,48 @@
 hat(
-importedMetric, notNullValu
+entry.getTitle(), is(metric.getTitl
 e())
+)
 ;%0A  

check imported metric by id because uri is not stable