package com.capgemini.reports.changeobjects;

import com.capgemini.framework.teamcenter.exceptions.NoTeamcenterConnectionException;
import com.capgemini.framework.teamcenter.object.pom.status.TcReleaseStatus;
import com.capgemini.framework.teamcenter.policies.PolicyLocation;
import com.capgemini.framework.teamcenter.queries.saved.modelobject.ItemRevisionQuery;
import com.capgemini.reports.AbstractTeamcenterTest;
import com.capgemini.reports.changeobjects.change.TcChangeNoticeRevision;
import com.capgemini.reports.changeobjects.change.TcChangeRequestRevision;
import com.capgemini.reports.snecmaobjects.generic.AbstractGeneric;
import com.capgemini.reports.teamcenter.queries.saved.ChangeRequestQuery;
import com.capgemini.reports.workflows.Task;
import com.teamcenter.schemas.soa._2006_03.exceptions.ServiceException;
import com.teamcenter.soa.client.model.strong.ItemRevision;
import junit.framework.Assert;
import org.junit.Test;

import java.util.List;


public class AbstractChangeObjectTest extends AbstractTeamcenterTest {

    @Test
    public void testLigneProduit() throws ServiceException, NoTeamcenterConnectionException {
        ItemRevisionQuery query = new ItemRevisionQuery(getSession());
        query.searchID("MOD200281");
        query.searchType("PV4_ETU_ANTICIPRevision"); // Making sure we don't obtain the (deprecated) EngChange with the same name
        ItemRevision MOD200281 = query.findAll().get(0);
        TcChangeNoticeRevision changeRequest = new TcChangeNoticeRevision(getSession(), MOD200281);
        Assert.assertNotNull("pv4_ligne_produit was not loaded on the Change!", changeRequest.getLignesProduit());
        Assert.assertFalse("pv4_ligne_poduit was empty", changeRequest.getLignesProduit().isEmpty());
        Assert.assertEquals("pv4_ligne_poduit did not contain the expected element", "ATAR 08K50 ETAT", changeRequest.getLignesProduit().get(0));
    }

    @Test
    public void testActorStatusOnTask() throws ServiceException, NoTeamcenterConnectionException {
        ChangeRequestQuery query = new ChangeRequestQuery(getSession());
        query.searchID("MOD200411");
        ItemRevision crMOD20411_AA = query.findAll().get(0);
        TcChangeRequestRevision changeRequest = new TcChangeRequestRevision(getSession(), crMOD20411_AA);
        Assert.assertNotNull(changeRequest.getWkfList());
        Assert.assertFalse(changeRequest.getWkfList().isEmpty()); // Should be 1 workflow with 1 active task
        Task wkf = changeRequest.getWkfList().get(0);
        Assert.assertNotNull(wkf);
        Assert.assertFalse(wkf.getActiveChildTasks().isEmpty());
    }

    @Test
    public void testChangeWithOrWithoutRupture() throws ServiceException, NoTeamcenterConnectionException {
        ItemRevisionQuery query = new ItemRevisionQuery(getSession()).searchID("Change952");
        ItemRevision r201_051 = query.findAll().get(0);
        TcChangeRequestRevision changeRequest = new TcChangeRequestRevision(getSession(), r201_051);
        List<AbstractGeneric> without = changeRequest.getListOfGenericRevWithOrWithoutRupture();
        Assert.assertNotNull(without);
        Assert.assertFalse(without.isEmpty());
        Assert.assertNotNull(without.get(0));
    }

    @Test
    public void testReleaseStatusWithMaturity() throws ServiceException, NoTeamcenterConnectionException {
        ChangeRequestQuery query = new ChangeRequestQuery(getSession());
        query.searchID("MOD200497");
        ItemRevision crMOD200497 = query.findAll().get(0);
        TcChangeRequestRevision changeRequest = new TcChangeRequestRevision(getSession(), crMOD200497);
        Assert.assertFalse(changeRequest.getReleaseStatusContainingEffectivity().isEmpty());
        for (TcReleaseStatus status : changeRequest.getReleaseStatusContainingEffectivity()) {
            Assert.assertNotNull(status);
            Assert.assertTrue(status.isEffectivity());
            Assert.assertFalse(status.isChangeMaturity());
        }
        Assert.assertFalse(changeRequest.getReleaseStatusContainingMaturity().isEmpty());
        for (TcReleaseStatus status : changeRequest.getReleaseStatusContainingMaturity()) {
            Assert.assertNotNull(status);
            Assert.assertTrue(status.isChangeMaturity());
            Assert.assertFalse(status.isEffectivity());
        }
    }

    @Test
    public void testReleaseStatusWithInterfaceTODO() throws ServiceException, NoTeamcenterConnectionException {
        ChangeRequestQuery query = new ChangeRequestQuery(getSession());
        query.searchID("MOD200330");
        ItemRevision crMOD200497 = query.findAll().get(0);
        TcChangeRequestRevision changeRequest = new TcChangeRequestRevision(getSession(), crMOD200497);
        Assert.assertFalse(changeRequest.getReleaseStatusContainingEffectivity().isEmpty());
        for (TcReleaseStatus status : changeRequest.getReleaseStatusContainingEffectivity()) {
            Assert.assertNotNull(status);
            Assert.assertTrue(status.isEffectivity());
            Assert.assertFalse(status.isChangeMaturity());
        }
        Assert.assertFalse(changeRequest.getReleaseStatusContainingMaturity().isEmpty());
        for (TcReleaseStatus status : changeRequest.getReleaseStatusContainingMaturity()) {
            Assert.assertNotNull(status);
            Assert.assertTrue(status.isChangeMaturity());
            Assert.assertFalse(status.isEffectivity());
        }
    }

    @Override
    public PolicyLocation getPropertyPolicyIdentifier() {
        return R13_POLICY_FULL;
    }
}
