package com.capgemini.framework.teamcenter.files;

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.files.FileManagement.FileType;
import com.capgemini.framework.teamcenter.policies.PolicyLocation;
import com.teamcenter.schemas.soa._2006_03.exceptions.InvalidCredentialsException;
import com.teamcenter.schemas.soa._2006_03.exceptions.ServiceException;
import com.teamcenter.services.loose.core._2006_03.FileManagement.GetDatasetWriteTicketsInputData;
import com.teamcenter.soa.client.model.ModelObject;
import junit.framework.Assert;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.File;
import java.util.NoSuchElementException;

public class DatasetPreparatorTest {
    private static final String DESCRIPTION = "A nice description";
    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 PolicyLocation XML_POLICY = PolicyLocation.make("test_policy", "/test_policy.xml");
    private static final TcSession session = new TcSession();

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

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

    @Test
    public void testAddFile() throws MalformedDataSetException, InvalidCredentialsException, ServiceException, NoTeamcenterConnectionException {
        DatasetPreparator datapreparator = new DatasetPreparator(session, DATASET_NAME, DESCRIPTION);
        addValidfile(datapreparator);
    }

    @Test(expected = MalformedDataSetException.class)
    public void testDatasetPreparatorNoName() throws MalformedDataSetException {
        DatasetPreparator datasetPreparator = new DatasetPreparator(session, "", DESCRIPTION);
        Assert.fail("'" + datasetPreparator.getDatasetName() + "' should not be accepted as a dataset name");
    }

    @Test(expected = MalformedDataSetException.class)
    public void testDatasetPreparatorNullName() throws MalformedDataSetException {
        DatasetPreparator datasetPreparator = new DatasetPreparator(session, null, DESCRIPTION);
        datasetPreparator.isEmpty();
        Assert.fail("NULL should not be accepted as a dataset name");
    }

    @Test(expected = MalformedDataSetException.class)
    public void testDatasetPreparatorNoFile()
            throws MalformedDataSetException, InvalidCredentialsException, ServiceException, NoTeamcenterConnectionException {
        DatasetPreparator datapreparator = new DatasetPreparator(session, DATASET_NAME, "");
        datapreparator.prepare(); // Fails here
    }

    @Test
    public void testPrepare() throws MalformedDataSetException, InvalidCredentialsException, ServiceException, NoTeamcenterConnectionException {
        DatasetPreparator datapreparator = new DatasetPreparator(session, DATASET_NAME, DESCRIPTION);
        addValidfile(datapreparator);
        GetDatasetWriteTicketsInputData createdDataset = datapreparator.prepare();

        // Cleanup
        ModelObject asModelObject = createdDataset.dataset;
        session.deleteObjects(asModelObject);
    }

    @Test
    public void testIsCreated() throws MalformedDataSetException, InvalidCredentialsException, ServiceException, NoTeamcenterConnectionException {
        DatasetPreparator datapreparator = new DatasetPreparator(session, DATASET_NAME, DESCRIPTION);
        Assert.assertFalse(datapreparator.isCreated());
        addValidfile(datapreparator);
        Assert.assertFalse(datapreparator.isCreated());
        GetDatasetWriteTicketsInputData createdDataset = datapreparator.prepare();
        Assert.assertTrue(datapreparator.isCreated());

        // Cleanup
        ModelObject asModelObject = createdDataset.dataset;
        session.deleteObjects(asModelObject);
    }

    // Helper function
    private void addValidfile(DatasetPreparator datapreparator) throws MalformedDataSetException {
        File theFile = new File(FOLDER + FILE_NAME);
        if (!theFile.exists() || theFile.isDirectory()) {
            throw new NoSuchElementException("Missing test file " + FOLDER + FILE_NAME); // This is the failure of the test-class, not of the class being tested
        }
        datapreparator.addFile(theFile, FileType.FICHE_ARTICLE);
    }
}
