/*
 * Sonar ESQL Plugin
 * Copyright (C) 2013-2017 Thomas Pohl and EXXETA AG
 * http://www.exxeta.com
 *
 * 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 com.exxeta.iss.sonar.esql.api.tree;

import static com.exxeta.iss.sonar.esql.utils.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

import com.exxeta.iss.sonar.esql.api.tree.Tree.Kind;
import com.exxeta.iss.sonar.esql.api.tree.statement.ParseClauseTree;
import com.exxeta.iss.sonar.esql.tree.impl.statement.CallStatementTreeImpl;
import com.exxeta.iss.sonar.esql.tree.impl.statement.CreateStatementTreeImpl;
import com.exxeta.iss.sonar.esql.utils.EsqlTreeModelTest;


public class CreateStatementTest  extends EsqlTreeModelTest<CreateStatementTreeImpl>{
	@Test
	public void domainClause(){
		assertThat(Kind.REPEAT_CLAUSE)
		.matches("REPEAT");
	}	
	
	@Test
	public void valueClause(){
		assertThat(Kind.VALUES_CLAUSE)
		.matches("VALUE 'Element 2 Value'");
	}
	
	@Test
	public void createStatement(){
		assertThat(Kind.CREATE_STATEMENT)
		.matches("CREATE FIELD OutputRoot.XMLNS.Data;")
		.matches("CREATE LASTCHILD OF OutputRoot.XMLNS.TestCase.Root IDENTITY (XML.Element)NSpace1:Element1[2] VALUE 'Element 2 Value';")
		.matches("CREATE LASTCHILD OF OutputRoot DOMAIN('MRM');")
		.matches("CREATE LASTCHILD OF OutputRoot DOMAIN('MRM') PARSE(inBitStream, inEncoding, inCCSID, 'DP3UK14002001', 'TestCase', 'XML1', options);")
		;
	}
	
	@Test
	public void parseClause(){
		assertThat(Kind.PARSE_CLAUSE)
		.matches("PARSE(inBitStream, inEncoding, inCCSID, 'DP3UK14002001', 'TestCase', 'XML1', options)")
		;
	}
	
	@Test
	public void modelTest() throws Exception {
		CreateStatementTreeImpl tree = parse("CREATE LASTCHILD OF OutputRoot DOMAIN('MRM') PARSE(inBitStream, inEncoding, inCCSID, 'DP3UK14002001', 'TestCase', 'XML1', options);", Kind.CREATE_STATEMENT);
		assertNotNull(tree);
		assertNotNull(tree.createKeyword());
		assertEquals(tree.createKeyword().text(), "CREATE");
		assertNotNull(tree.qualifierName());
		assertEquals(tree.qualifierName().text(), "LASTCHILD");
		assertNotNull(tree.qualifierOfKeyword());
		assertEquals(tree.qualifierOfKeyword().text(), "OF");
		assertNotNull(tree.target());
		assertNull(tree.asKeyword());
		assertNull(tree.aliasFieldReference());
		assertNotNull(tree.domainKeyword());
		assertEquals(tree.domainKeyword().text(), "DOMAIN");
		assertNotNull(tree.domainExpression());
		assertTrue(tree.domainExpression().is(Kind.PARENTHESISED_EXPRESSION));
		assertNull(tree.repeatClause());
		assertNull(tree.valuesClause());
		assertNull(tree.fromClause());
		assertNotNull(tree.parseClause());
		assertNotNull(tree.semi());
		assertEquals(tree.semi().text(), ";");
		
		//ParseClause
		ParseClauseTree parseClause = tree.parseClause();
		assertNotNull(parseClause);
		assertNotNull(parseClause.parseKeyword());
		assertEquals(parseClause.parseKeyword().text(), "PARSE");
		assertNotNull(parseClause.openingParenthesis());
		assertEquals(parseClause.openingParenthesis().text(), "(");
		assertNotNull(parseClause.options());
		assertNull(parseClause.encodingKeyword());
		assertNull(parseClause.encoding());
		assertNull(parseClause.ccsidKeyword());
		assertNull(parseClause.ccsid());
		assertNull(parseClause.setKeyword());
		assertNull(parseClause.set());
		assertNull(parseClause.typeKeyword());
		assertNull(parseClause.type());
		assertNull(parseClause.formatKeyword());
		assertNull(parseClause.format());
		assertNotNull(parseClause.closingParenthesis());
		assertEquals(parseClause.closingParenthesis().text(), ")");
		
	}
	
}

@@ -1287,32 +1287,105 @@
 tementTreeImpl;%0A
+import com.exxeta.iss.sonar.esql.tree.impl.statement.FromClauseTreeImpl;%0A
 import com.exxet
@@ -2361,32 +2361,135 @@
 tions)%22)%0A%09%09;%0A%09%7D%0A
+%09@Test%0A%09public void fromClause()%7B%0A%09%09assertThat(Kind.FROM_CLAUSE)%0A%09%09.matches(%22FROM InputRoot.A%22)%0A%09%09;%0A%09%7D%0A
 %09%0A%09@Test%0A%09public
@@ -4421,16 +4421,292 @@
 ), %22)%22);
+%0A%0A%09%09tree = parse(%22CREATE LASTCHILD OF OutputRoot From InputRoot.A;%22, Kind.CREATE_STATEMENT);%0A%09%09%0A%09%09assertNotNull(tree.fromClause());%0A%09%09FromClauseTreeImpl fromClause = tree.fromClause();%0A%09%09assertNotNull(fromClause.fromKeyword());%0A%09%09assertNotNull(fromClause.fieldReference());%0A%09%09
 %0A%09%09%0A%09%7D%0A%09

Improve create statement test