/*
 * Copyright Red Hat, Inc. and/or its affiliates
 * and other contributors as indicated by the @author tags and
 * the COPYRIGHT.txt file distributed with this work.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.teiid.translator.odata4;

import org.junit.jupiter.api.Test;
import org.teiid.core.util.ObjectConverterUtil;
import org.teiid.core.util.UnitTestUtil;
import org.teiid.translator.ResultSetExecution;

import java.io.FileReader;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
 * @author Muhammad Imran Ansari
 * @date 2024.10.30
 */

public class TestODataFilterOnGuidColumnDvcore8603 extends TestBaseODataMockExecution {

    @Test
    public void testGuidBasedFilter() throws Exception {
        String query = "SELECT ShareId FROM Me_Trips LIMIT 1";
        String expectedURL = "Me?$select=UserName&$expand=Trips($select=ShareId)&$top=1";

        FileReader reader = new FileReader(UnitTestUtil.getTestDataFile("me-trips.json"));
        ResultSetExecution execution = helpQueryExecute(TestODataMetadataProcessor.tripPinMetadata(),
                query, ObjectConverterUtil.convertToString(reader), expectedURL);

        Object shareId = execution.next().toArray(new Object[1])[0];
        assertEquals("testShareID", shareId);
        execution.close();
        query = String.format("SELECT ShareId FROM Me_Trips WHERE ShareId = '%s' LIMIT 1", shareId);
        expectedURL = "Me?$select=UserName&$expand=Trips($select=ShareId;$filter=ShareId eq testShareID)&$top=1";
        reader = new FileReader(UnitTestUtil.getTestDataFile("me-trips.json"));
        execution = helpQueryExecute(TestODataMetadataProcessor.tripPinMetadata(),
                query, ObjectConverterUtil.convertToString(reader), expectedURL);
        shareId = execution.next().toArray(new Object[1])[0];
        assertEquals("testShareID", shareId);
    }
}
