/*
 * ModeShape (http://www.modeshape.org)
 *
 * 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.modeshape.sequencer.ddl.node;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import org.junit.Before;
import org.junit.Test;
import org.modeshape.jcr.api.JcrConstants;
import org.modeshape.sequencer.ddl.StandardDdlLexicon;

/**
 *
 */
public class AstNodeFactoryTest {
    private AstNodeFactory nodeFactory;
    private AstNode rootNode;

    @Before
    public void beforeEach() {
        nodeFactory = new AstNodeFactory();
        rootNode = nodeFactory.node("testRootNode");
        rootNode.setProperty(JcrConstants.JCR_MIXIN_TYPES, StandardDdlLexicon.STATEMENTS_CONTAINER);
        rootNode.setProperty(JcrConstants.JCR_PRIMARY_TYPE, JcrConstants.NT_UNSTRUCTURED);
    }

    @Test
    public void shouldCreateChildNode() {
        String name = "myNodeName";

        nodeFactory.node(name, rootNode, StandardDdlLexicon.TYPE_CREATE_TABLE_STATEMENT);

        assertThat(rootNode.getChildCount(), is(1));
        assertEquals(rootNode.getProperty(JcrConstants.JCR_MIXIN_TYPES), StandardDdlLexicon.STATEMENTS_CONTAINER);
        assertEquals(rootNode.getChild(0).getProperty(JcrConstants.JCR_MIXIN_TYPES),
                     StandardDdlLexicon.TYPE_CREATE_TABLE_STATEMENT);
        assertThat(rootNode.getChild(0).getName(), is(name));
    }

    @Test
    public void shouldHaveMixinTypeForNode() {
        String name = "myNodeName";
        nodeFactory.node(name, rootNode, StandardDdlLexicon.TYPE_CREATE_TABLE_STATEMENT);

        assertThat(rootNode.getChildCount(), is(1));
        assertThat(nodeFactory.hasMixinType(rootNode, StandardDdlLexicon.STATEMENTS_CONTAINER), is(true));
        assertThat(nodeFactory.hasMixinType(rootNode.getChild(0), StandardDdlLexicon.TYPE_CREATE_TABLE_STATEMENT), is(true));
    }

    @Test
    public void shouldFindChildForNameAndType() {
        String name = "myNodeName";
        nodeFactory.node(name, rootNode, StandardDdlLexicon.TYPE_CREATE_TABLE_STATEMENT);

        AstNode node = nodeFactory.getChildforNameAndType(rootNode, name, StandardDdlLexicon.TYPE_CREATE_TABLE_STATEMENT);

        assertThat(rootNode.getChildCount(), is(1));
        assertThat(nodeFactory.hasMixinType(rootNode, StandardDdlLexicon.STATEMENTS_CONTAINER), is(true));

        assertNotNull(node);
    }

    @Test
    public void shouldNotFindChildForRightNameWrongType() {
        String name = "myNodeName";
        nodeFactory.node(name, rootNode, StandardDdlLexicon.TYPE_CREATE_TABLE_STATEMENT);

        AstNode node = nodeFactory.getChildforNameAndType(rootNode, name, StandardDdlLexicon.TYPE_CREATE_VIEW_STATEMENT);

        assertThat(rootNode.getChildCount(), is(1));
        assertThat(nodeFactory.hasMixinType(rootNode, StandardDdlLexicon.STATEMENTS_CONTAINER), is(true));

        assertNull(node);
    }

    @Test
    public void shouldNotFindChildForWrongNameRightType() {
        String name = "myNodeName";
        nodeFactory.node(name, rootNode, StandardDdlLexicon.TYPE_CREATE_TABLE_STATEMENT);

        AstNode node = nodeFactory.getChildforNameAndType(rootNode, "wrongName", StandardDdlLexicon.TYPE_CREATE_TABLE_STATEMENT);

        assertNull(node);
    }

    @Test
    public void shouldSetTypeOnNode() {
        String name = "myNodeName";
        AstNode node = nodeFactory.node(name);
        assertNotNull(node);
        nodeFactory.setType(node, StandardDdlLexicon.TYPE_CREATE_TABLE_STATEMENT);
        assertThat(nodeFactory.hasMixinType(node, StandardDdlLexicon.TYPE_CREATE_TABLE_STATEMENT), is(true));
    }
}
