/*
 * Copyright © 2012-2025 by Data Virtuality GmbH
 */
package org.teiid.query.metadata;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.teiid.api.exception.query.QueryMetadataException;
import org.teiid.api.exception.query.QueryResolverException;
import org.teiid.core.types.ResourceType;
import org.teiid.metadata.Table;
import org.teiid.query.resolver.util.ResolverUtil;
import org.teiid.query.sql.symbol.GroupSymbol;

import static org.teiid.core.types.ResourceType.UNDEFINED;

/**
 * @author Arseniy Kaleshnyk
 * @date 2025.03.17
 */
public class TestResourceTypeOfGroupSymbol {

    @ParameterizedTest()
    @MethodSource("org.teiid.util.TestingDataArguments#tableViews1")
    public void testGetResourceTypeOfGroupSymbol(String name, ResourceType type, QueryMetadataInterface metadata) throws Exception {
        TempMetadataAdapter tempMetadataAdapter = new TempMetadataAdapter(metadata, new TempMetadataStore());
        Assertions.assertEquals(
                type,
                tempMetadataAdapter.getResourceTypeOfGroupSymbol(name));
        Assertions.assertInstanceOf(
                Table.class,
                metadata.getGroupID(name));
    }

    @ParameterizedTest()
    @MethodSource("org.teiid.util.TestingDataArguments#invalidTableViews1")
    public void testGetResourceTypeOfGroupSymbolOfMissingObject(String name, ResourceType type, QueryMetadataInterface metadata) throws Exception {
        TempMetadataAdapter tempMetadataAdapter = new TempMetadataAdapter(metadata, new TempMetadataStore());
        Assertions.assertEquals(
                UNDEFINED,
                tempMetadataAdapter.getResourceTypeOfGroupSymbol(name));
        try {
            metadata.getGroupID(name);
            Assertions.fail("exception expected");
        } catch (QueryMetadataException qme) {
            // expected
        }
    }

    @ParameterizedTest()
    @MethodSource("org.teiid.util.TestingDataArguments#tableViews1")
    public void testResourceTypeOfResolvedGroupSymbol(String name, ResourceType type, QueryMetadataInterface metadata) throws Exception {
        GroupSymbol groupSymbol = new GroupSymbol(name);
        ResolverUtil.resolveGroup(groupSymbol, metadata);
        Assertions.assertTrue(groupSymbol.isResolved(), "not resolved");
        Assertions.assertEquals(type, groupSymbol.getResourceType());
    }

    @ParameterizedTest()
    @MethodSource("org.teiid.util.TestingDataArguments#invalidTableViews1")
    @Disabled // fails on "xmltest2.docA", returns VIEW instead of UNDEFINED
    public void testResourceTypeOfUnresolvedGroupSymbol(String name, ResourceType type, QueryMetadataInterface metadata) throws Exception {
        try {
            GroupSymbol groupSymbol = new GroupSymbol(name);
            ResolverUtil.resolveGroup(groupSymbol, metadata);
            Assertions.assertEquals(type, groupSymbol.getResourceType());
            Assertions.fail("exception expected");
        } catch (QueryResolverException qme) {
            // expected
        }
    }

}
