package com.capgemini.framework.teamcenter.files;

import com.capgemini.framework.teamcenter.authent.TcSession;
import com.capgemini.framework.teamcenter.exceptions.ConnectionMisconguredException;
import com.capgemini.framework.teamcenter.exceptions.MalformedDataSetException;
import com.capgemini.framework.teamcenter.exceptions.NoTeamcenterConnectionException;
import com.capgemini.framework.teamcenter.files.FileManagement.FileType;
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.Test;

import java.io.File;
import java.io.IOException;
import java.util.List;

public class FileManagementTest {

    private static final String DESCRIPTION = "A nice description";
    private static final String DATASET_NAME = "aDatasetName";
    private static final String TEXT_FILE_NAME = "TextFile.txt";
    private static final String PDF_FILE_NAME = "jasperPdfPrinterTest.pdf";
    private static final String FOLDER = "src/test/resources/";

    @Test
    public void testFileManagementText()
            throws MalformedDataSetException, IOException, ConnectionMisconguredException, NoTeamcenterConnectionException {
        try (TcSession session = new TcSession()) {
            session.login();

            // Prepare dataset
            DatasetPreparator dataset = new DatasetPreparator(session, DATASET_NAME, DESCRIPTION);
            File targetFile = new File(FOLDER + TEXT_FILE_NAME);
            dataset.addFile(targetFile, FileType.TEXT_FILE);

            // Upload file (dataset will be uploaded too)
            FileManagement fileManager = new FileManagement(session);
            fileManager.addDataSet(dataset);
            fileManager.upload();

            List<GetDatasetWriteTicketsInputData> createdDatasets = fileManager.getDataSets();
            // Verify creation
            Assert.assertFalse(createdDatasets.isEmpty());

            // Delete all objects created
            ModelObject[] datasets = new ModelObject[createdDatasets.size()];
            int num = 0;
            for (GetDatasetWriteTicketsInputData data : createdDatasets) {
                datasets[num++] = data.dataset;
            }
            session.deleteObjects(datasets);
        }
    }

    @Test
    public void testFileManagementPDF()
            throws MalformedDataSetException, IOException, ConnectionMisconguredException, NoTeamcenterConnectionException {
        try (TcSession session = new TcSession()) {
            session.login();

            // Prepare dataset
            DatasetPreparator dataset = new DatasetPreparator(session, DATASET_NAME, DESCRIPTION);
            File targetFile = new File(FOLDER + PDF_FILE_NAME);
            dataset.addFile(targetFile, FileType.PDF);

            // Upload file (dataset will be uploaded too)
            FileManagement fileManager = new FileManagement(session);
            fileManager.addDataSet(dataset);
            try {
                // Perform the file upload
                fileManager.upload();

                // Verify creation
                List<GetDatasetWriteTicketsInputData> createdDatasets = fileManager.getDataSets();
                Assert.assertFalse(createdDatasets.isEmpty());
            } finally {
                if (dataset.isCreated()) {
                    // Delete The dataset
                    session.deleteObjects(dataset.getCreatedDataset().dataset);
                }
            }
        }
    }

    @Test(expected = MalformedDataSetException.class)
    public void testFileManagementNoDataset()
            throws InvalidCredentialsException, ServiceException, MalformedDataSetException, ConnectionMisconguredException {
        try (TcSession session = new TcSession()) {
            FileManagement fileManager = new FileManagement(session);
            fileManager.upload(); // Should fail because no datasets were provided
        }
    }

    @Test(expected = MalformedDataSetException.class)
    public void testFileManagementFailedDataset()
            throws InvalidCredentialsException, ServiceException, MalformedDataSetException, ConnectionMisconguredException {
        try (TcSession session = new TcSession()) {
            FileManagement fileManager = new FileManagement(session);
            fileManager.addDataSetWithSingleFile(DATASET_NAME, DESCRIPTION, null, FileType.FICHE_ARTICLE);
        }
    }
}
