/*
 * JBoss, Home of Professional Open Source.
 * See the COPYRIGHT.txt file distributed with this work for information
 * regarding copyright ownership.  Some portions may be licensed
 * to Red Hat, Inc. under one or more contributor license agreements.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 * Modified by
 * @author  Data Virtuality GmbH
 * @date    $Date$
 *
 */
// https://issues.jboss.org/browse/TEIID-2159
// https://issues.jboss.org/browse/TEIID-2390
// https://issues.redhat.com/browse/TEIID-5728
// https://issues.redhat.com/browse/TEIID-5499
package org.teiid.query.processor;

import static org.junit.jupiter.api.Assertions.*;
import static org.teiid.query.processor.TestProcessor.*;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.sql.rowset.serial.SerialBlob;

import org.junit.jupiter.api.Test;
import org.teiid.api.exception.query.ExpressionEvaluationException;
import org.teiid.core.types.BlobType;
import org.teiid.query.metadata.TransformationMetadata;
import org.teiid.core.types.DataTypeManager;
import org.teiid.query.optimizer.TestOptimizer;
import org.teiid.query.optimizer.capabilities.DefaultCapabilitiesFinder;
import org.teiid.query.resolver.TestResolver;
import org.teiid.query.sql.lang.Command;
import org.teiid.query.sql.symbol.Expression;
import org.teiid.query.unittest.RealMetadataFactory;

@SuppressWarnings("nls")
public class TestJSONProcessing {

	@Test public void testEmptyJSONObject() throws Exception {
		String sql = "select jsonObject()"; //$NON-NLS-1$

		List<?>[] expected = new List[] {
				Arrays.asList("{}"),
		};

		FakeDataManager fdm = new FakeDataManager();
		sampleData1(fdm);
		ProcessorPlan plan = helpGetPlan(sql, RealMetadataFactory.example1Cached());
		helpProcess(plan, fdm, expected);
	}

	@Test public void testJSONParseBlob() throws Exception {
		HardcodedDataManager dataManager = new HardcodedDataManager();
    	String sql = "select jsonParse(cast(? as blob), false) x"; //$NON-NLS-1$
    	String json = "{\"name\":123}";
    	
        List<?>[] expected = new List[] {
        		Arrays.asList(json),
        };    
    
        processPreparedStatement(sql, expected, dataManager, new DefaultCapabilitiesFinder(), RealMetadataFactory.example1Cached(), Arrays.asList(new BlobType(new SerialBlob(json.getBytes(Charset.forName("UTF-16BE"))))));
	}
	
	@Test public void testJSONArray() throws Exception {
		HardcodedDataManager dataManager = new HardcodedDataManager();
    	String sql = "select jsonArray(1, null, true, {d '2007-01-01'}, jsonParse('{\"name\":123}', true), unescape('\\t\\n?'))"; //$NON-NLS-1$
    	
        List<?>[] expected = new List[] {
        		Arrays.asList("[1,null,true,\"2007-01-01\",{\"name\":123},\"\\t\\n?\"]"),
        };    
    
        ProcessorPlan plan = helpGetPlan(sql, RealMetadataFactory.example1Cached());
        helpProcess(plan, dataManager, expected);
	}
	
	@Test public void testJSONArray_Agg() throws Exception {
		HardcodedDataManager dataManager = new HardcodedDataManager();
    	String sql = "select jsonArray_agg(e1 order by e1 desc) from pm1.g1"; //$NON-NLS-1$
    	
        List<?>[] expected = new List[] {
        		Arrays.asList("[\"d\",\"a\",\"\\\"b\"]"),
        };    
    
        dataManager.addData("SELECT pm1.g1.e1 FROM pm1.g1", new List<?>[] {Arrays.asList("a"), Arrays.asList("\"b"), Arrays.asList("d")});
        ProcessorPlan plan = helpGetPlan(sql, RealMetadataFactory.example1Cached());
		assertEquals(DataTypeManager.DefaultDataClasses.JSON, ((Expression)plan.getOutputElements().get(0)).getType());
        helpProcess(plan, dataManager, expected);
	}
	
	@Test public void testJSONObject() throws Exception {
    	String sql = "select jsonObject(e1, e2, 1) from pm1.g1 order by e1 limit 1"; //$NON-NLS-1$
    	
        List<?>[] expected = new List[] {
        		Arrays.asList("{\"e1\":null,\"e2\":1,\"expr3\":1}"),
        };    
        FakeDataManager fdm = new FakeDataManager();
        sampleData1(fdm);
        ProcessorPlan plan = helpGetPlan(sql, RealMetadataFactory.example1Cached());
        helpProcess(plan, fdm, expected);
	}

	@Test public void testJSONObjectWithNestedJson() throws Exception {
		String sql = "select jsonObject(jsonObject(e1), e2, jsonarray(e3, 2), 1) from pm1.g1 order by e1 limit 1"; //$NON-NLS-1$

		List<?>[] expected = new List[] {
				Arrays.asList("{\"expr1\":{\"e1\":null},\"e2\":1,\"expr3\":[false,2],\"expr4\":1}"),
		};
		FakeDataManager fdm = new FakeDataManager();
		sampleData1(fdm);
		ProcessorPlan plan = helpGetPlan(sql, RealMetadataFactory.example1Cached());
		helpProcess(plan, fdm, expected);
	}

	@Test public void testJSONObjectWithNestedJson1() throws Exception {
		String sql = "select jsonObject(jsonObject(e2, 1, jsonObject((select jsonArray_agg(e2) from pm1.g2 where e1 = pm1.g1.e1), e1))) from pm1.g1 order by e1 nulls last limit 1"; //$NON-NLS-1$

		List<?>[] expected = new List[] {
				Arrays.asList("{\"expr1\":{\"e2\":0,\"expr2\":1,\"expr3\":{\"expr1\":[0,3,0],\"e1\":\"a\"}}}"),
		};
		FakeDataManager fdm = new FakeDataManager();
		fdm.setBlockOnce();
		sampleData1(fdm);
		ProcessorPlan plan = helpGetPlan(sql, RealMetadataFactory.example1Cached());

		helpProcess(plan, fdm, expected);
	}

	@Test public void testJSONCasts() throws Exception {
		TransformationMetadata tm = RealMetadataFactory.fromDDL("create view json as select jsonParse('{}', true) as col", "x", "y");

		String sql = "select cast(jsonObject(col) as string), cast(cast(jsonParse(col, true) as clob) as string) from json"; //$NON-NLS-1$

		//note in the first results contains the nested json, not a nested string
		List<?>[] expected = new List[] {
				Arrays.asList("{\"col\":{}}", "{}"),
		};
		HardcodedDataManager hdm = new HardcodedDataManager();
		ProcessorPlan plan = helpGetPlan(sql, tm);
		helpProcess(plan, hdm, expected);

		//small breaking potential - if we explicitly expect clob results
		//tm = RealMetadataFactory.fromDDL("create view json (col clob) as select jsonParse('{}', true) as col", "x", "y");
	}

	@Test public void testJSONResolving() throws Exception {
		String sql = "select jsonArray(1, null, true, {d '2007-01-01'}), jsonParse('{\\\"name\\\":123}', true), jsonObject(1 as expr)"; //$NON-NLS-1$

		Command command = TestResolver.helpResolve(sql, RealMetadataFactory.example1Cached());
		command.getProjectedSymbols().stream().forEach(e -> assertEquals(
				DataTypeManager.DefaultDataClasses.JSON, e.getType()));
	}

	@Test
	public void testCastStringToJson() throws Exception {
		HardcodedDataManager dataManager = new HardcodedDataManager();
		String sql = "select cast('{\"name\":123}' as json)"; //$NON-NLS-1$
		String json = "{\"name\":123}";

		List<?>[] expected = new List[] {
				Arrays.asList(json),
		};

		ProcessorPlan plan = helpGetPlan(sql, RealMetadataFactory.example1Cached());
		helpProcess(plan, dataManager, expected);
	}

	@Test public void testCastStringToJsonFails() throws Exception {
		String sql = "select convert('{\"name\":?}', json)"; //$NON-NLS-1$

		assertThrows(ExpressionEvaluationException.class, () -> TestProcessor.helpGetPlan(
				TestOptimizer.helpGetCommand(sql,
						RealMetadataFactory.exampleBQTCached(), new ArrayList<>()),
				RealMetadataFactory.exampleBQTCached(),
				new DefaultCapabilitiesFinder(),
				TestProcessor.createCommandContext())
		);
	}
	
}
