package com.capgemini.framework.teamcenter.queries.saved.modelobject;

import com.capgemini.framework.teamcenter.authent.TcSession;
import com.capgemini.framework.teamcenter.exceptions.InvalidConfigurationException;
import com.capgemini.framework.teamcenter.exceptions.NoTeamcenterConnectionException;
import com.capgemini.framework.teamcenter.policies.PolicyLocation;
import com.teamcenter.soa.client.model.strong.ItemRevision;
import lombok.extern.slf4j.Slf4j;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

import java.util.List;

@Slf4j
public class ItemRevisionQueryTest {
    private static final TcSession session = new TcSession();
    private static final String expectedItemID = "22U";
    private static final String expectedObjectName = "GE90 M10 MOTEUR ASSEMBLE";
    private static final String expectedItemRevision = "C"; // <-- revision B also exists
    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 testSearchID() {
        log.info("testSearchID--------------------------");
        // Create a query
        ItemRevisionQuery query = new ItemRevisionQuery(session);
        query.searchID(expectedItemID);

        // Execute the query
        List<ItemRevision> result = query.findAll();
        Assert.assertNotNull(result);
        Assert.assertFalse(result.isEmpty());

        // Parse the results
        log.info("Matching ItemRevisions: ");
        for (ItemRevision itemRev : result) {
            log.info("ID = {}\tRev={}\tName = {}", itemRev.get_item_id(), itemRev.get_item_revision_id(), itemRev.get_object_name());
            Assert.assertEquals(expectedItemID, itemRev.get_item_id());
        }
    }

    @Test
    public void testSearchFakeID() {
        log.info("testSearchFakeID----------------------");
        // Create a query
        ItemRevisionQuery query = new ItemRevisionQuery(session);
        query.searchID("some ID that does not exist");

        // Execute the query
        List<ItemRevision> result = query.findAll();
        Assert.assertNotNull(result);
        Assert.assertTrue(result.isEmpty());
    }

    @Test
    public void testSearchIDAndRevision() {
        log.info("testSearchIDAndRevision---------------");
        // Create a query
        ItemRevisionQuery query = new ItemRevisionQuery(session);
        query.searchIDAndRevision(expectedItemID, expectedItemRevision);

        // Execute the query
        List<ItemRevision> result = query.findAll();
        Assert.assertNotNull(result);
        Assert.assertTrue(result.size() == 1);

        // Parse the results
        ItemRevision theItemRev = result.get(0);
        Assert.assertNotNull(theItemRev);
        Assert.assertEquals(expectedItemID, theItemRev.get_item_id());
        Assert.assertEquals(expectedItemRevision, theItemRev.get_item_revision_id());
        log.info("Matching ItemRevision: ");
        log.info("ID = {}\tRev={}\tName = {}", theItemRev.get_item_id(), theItemRev.get_item_revision_id(), theItemRev.get_object_name());
    }

    /**
     * JUnit test.
     */
    @Test
    public void testSearchIdAndFakeRevision() {
        log.info("testSearchIdAndFakeRevision-----------");
        // Create a query
        ItemRevisionQuery query = new ItemRevisionQuery(session);
        query.searchIDAndRevision(expectedItemID, "pouet pouet truc");

        // Execute the query
        List<ItemRevision> result = query.searchID(expectedItemID).findAll();
        Assert.assertNotNull(result);
        Assert.assertTrue(result.isEmpty());
    }

    @Test
    public void testSearchName() {
        log.info("testSearchName------------------------");
        // Create a query
        ItemRevisionQuery query = new ItemRevisionQuery(session);
        query.searchName(expectedObjectName);

        // Execute the query
        List<ItemRevision> result = query.findAll();
        Assert.assertNotNull(result);
        Assert.assertTrue(!result.isEmpty());

        // Parse the results
        log.info("Matching ItemRevisions: ");
        for (ItemRevision itemRev : result) {
            log.info("ID = {}\tRev={}", itemRev.get_item_id(), itemRev.get_item_revision_id());
            Assert.assertEquals(expectedObjectName, itemRev.get_object_name());
        }
    }

    /**
     * JUnit test.
     */
    @Test
    public void testSearchFakeName() {
        log.info("testSearchFakeName--------------------");
        // Create a query
        ItemRevisionQuery query = new ItemRevisionQuery(session);
        query.searchName("pouet pouet truc");

        // Execute the query
        List<ItemRevision> result = query.searchID(expectedItemID).findAll();
        Assert.assertNotNull(result);
        Assert.assertTrue(result.isEmpty());
    }
}
