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.Item;
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 ItemQueryTest {
    private static final TcSession session = new TcSession();
    private static final String expectedItemID = "22U";
    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
        ItemQuery query = new ItemQuery(session);
        query.searchID(expectedItemID);

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

        // Parse the results
        log.info("Matching Items: ");
        for (Item item : result) {
            Assert.assertNotNull(item);
            log.info("ID = {}\t\tName = {}", item.get_item_id(), item.get_object_name());
            Assert.assertEquals(expectedItemID, item.get_item_id());
        }
    }

    @Test
    public void testSearchFakeID() {
        log.info("testSearchID--------------------------");

        // Create a query
        ItemQuery query = new ItemQuery(session);
        query.searchID("babar");

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