package com.capgemini.framework.teamcenter.statuating;

import com.capgemini.framework.teamcenter.authent.TcSession;
import com.capgemini.framework.teamcenter.exceptions.InvalidConfigurationException;
import com.capgemini.framework.teamcenter.exceptions.MalformedDataSetException;
import com.capgemini.framework.teamcenter.exceptions.NoTeamcenterConnectionException;
import com.capgemini.framework.teamcenter.exceptions.UnableToCreateStatusException;
import com.capgemini.framework.teamcenter.files.DatasetPreparator;
import com.capgemini.framework.teamcenter.files.FileManagement.FileType;
import com.capgemini.framework.teamcenter.policies.PolicyLocation;
import com.teamcenter.services.loose.core._2006_03.FileManagement.GetDatasetWriteTicketsInputData;
import com.teamcenter.soa.client.model.strong.ReleaseStatus;
import com.teamcenter.soa.client.model.strong.WorkspaceObject;
import junit.framework.Assert;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.File;

public class StatusSetterTest {
    private static final TcSession session = new TcSession();
    private static final String DESCRIPTION = "to perform a test, will be destroyed";
    private static final String DATASET_NAME = "aDatasetName";
    private static final String FILE_NAME = "jasperPdfPrinterTest.pdf";
    private static final String FOLDER = "src/test/resources/";
    private static final String TEST_STATUS_NAME = "J3B";
    private static final PolicyLocation XML_POLICY = PolicyLocation.make("test_policy", "/test_policy.xml");

    @BeforeClass
    public static void login() throws NoTeamcenterConnectionException, InvalidConfigurationException {
        session.login();
        session.setObjectPropPolicy(XML_POLICY);
    }

    @AfterClass
    public static void logout() {
        session.close();
    }


    @Test
    public void poseAndDeleteStatus() throws MalformedDataSetException, UnableToCreateStatusException {
        // Create the target Dataset
        DatasetPreparator datasetPreparator = new DatasetPreparator(session, DATASET_NAME, DESCRIPTION);
        datasetPreparator.addFile(new File(FOLDER + FILE_NAME), FileType.PDF);
        GetDatasetWriteTicketsInputData datasetTicket = datasetPreparator.prepare();

        // Perform the test: put a status on the brand new dataset
        StatusSetter setter = new StatusSetter(session);
        ReleaseStatus createdStatus = setter.putStatus((WorkspaceObject) datasetTicket.dataset, TEST_STATUS_NAME);
        Assert.assertNotNull(createdStatus);
        Assert.assertEquals(TEST_STATUS_NAME, createdStatus.get_name());

        // Status passed! now delete the dataset, with the and with it
        session.deleteObjects(datasetTicket.dataset);
    }
}
