/*
 * Copyright (c) 2013-2015 Cinchapi, Inc.
 *
 * 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.cinchapi.concourse.shell;

import java.util.List;
import java.util.Set;

import org.junit.Assert;
import org.junit.Test;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

/**
 * Unit tests for the {@link SyntaxTools} methods.
 * 
 * @author Jeff Nelson
 */
public class SyntaxToolsTest {

    @Test
    public void testParseSingleInvokedMethod() {
        Set<String> methods = SyntaxTools.parseShortInvokedMethods("foo()");
        Assert.assertEquals(1, methods.size());
        Assert.assertTrue(methods.contains("foo"));
    }

    @Test
    public void testParseSingleInvokedConcourseMethod() {
        Set<String> methods = SyntaxTools
                .parseShortInvokedMethods("concourse.foo()");
        Assert.assertEquals(0, methods.size());
        Assert.assertFalse(methods.contains("concourse.foo"));
    }

    @Test
    public void testParseNestedInvokvedMethods() {
        Set<String> methods = SyntaxTools
                .parseShortInvokedMethods("foo(bar(x))");
        Assert.assertEquals(methods, Sets.newHashSet("foo", "bar"));
    }

    @Test
    public void testParseSingleInvokedBlacklistedMethod() {
        Set<String> methods = SyntaxTools.parseShortInvokedMethods("time(x)");
        Assert.assertEquals(0, methods.size());
        Assert.assertFalse(methods.contains("time"));
    }

    @Test
    public void testParseNestedInvokedBlacklistedMethod() {
        Set<String> methods = SyntaxTools
                .parseShortInvokedMethods("foo(time(x, y))");
        Assert.assertEquals(methods, Sets.newHashSet("foo"));
    }

    @Test
    public void testParseNestedInvokedConcourseMethod() {
        Set<String> methods = SyntaxTools
                .parseShortInvokedMethods("foo(concourse.bar(x, y))");
        Assert.assertEquals(methods, Sets.newHashSet("foo"));
    }

    @Test
    public void testParseNestedNestedInvokvedMethods() {
        Set<String> methods = SyntaxTools
                .parseShortInvokedMethods("foo(bar(x, baz(time(z))))");
        Assert.assertEquals(methods, Sets.newHashSet("foo", "bar", "baz"));
    }

    @Test
    public void testHandleShortSyntaxTimeWithNoArg() {
        Assert.assertEquals("time \"now\"", SyntaxTools.handleShortSyntax(
                "time",
                Lists.newArrayList(ConcourseShell.getAccessibleApiMethods())));
    }

    @Test
    public void testHandleShortSyntaxDateWithNoArg() {
        Assert.assertEquals("date \"now\"", SyntaxTools.handleShortSyntax(
                "date",
                Lists.newArrayList(ConcourseShell.getAccessibleApiMethods())));

    }

    @Test
    public void testHandleShortSyntaxTimeWithArg() {
        Assert.assertEquals(
                "time \"yesterday\"",
                SyntaxTools.handleShortSyntax("time \"yesterday\"", Lists
                        .newArrayList(ConcourseShell.getAccessibleApiMethods())));
    }

    @Test
    public void testHandleShortSyntaxDateWithArg() {
        Assert.assertEquals(
                "date \"yesterday\"",
                SyntaxTools.handleShortSyntax("date \"yesterday\"", Lists
                        .newArrayList(ConcourseShell.getAccessibleApiMethods())));
    }

    @Test
    public void testHandleShortSyntaxSingleMethodNoParens() {
        String line = "add \"foo\", \"bar\", 1";
        String expected = "concourse.add \"foo\", \"bar\", 1";
        Assert.assertEquals(expected,
                SyntaxTools.handleShortSyntax(line, options()));
    }

    @Test
    public void testHandleShortSyntaxSingleMethodWithParens() {
        String line = "add(\"foo\", \"bar\", 1)";
        String expected = "concourse.add(\"foo\", \"bar\", 1)";
        Assert.assertEquals(expected,
                SyntaxTools.handleShortSyntax(line, options()));
    }

    @Test
    public void testHandleShortSyntaxNestedMethods() {
        String line = "get(describe(1), 1)";
        String expected = "concourse.get(concourse.describe(1), 1)";
        Assert.assertEquals(expected,
                SyntaxTools.handleShortSyntax(line, options()));
    }

    @Test
    public void testHandleShortSyntaxNestedMethodsSomeBlacklisted() {
        String line = "get(describe(1), 1, time(\"yesterday\"))";
        String expected = "concourse.get(concourse.describe(1), 1, time(\"yesterday\"))";
        Assert.assertEquals(expected,
                SyntaxTools.handleShortSyntax(line, options()));
    }

    @Test
    public void testHandleShortSyntaxNestedNestedMethods() {
        String line = "get(describe(1), find(\"name\", eq, \"foo\"))";
        String expected = "concourse.get(concourse.describe(1), concourse.find(\"name\", eq, \"foo\"))";
        Assert.assertEquals(expected,
                SyntaxTools.handleShortSyntax(line, options()));
    }

    /**
     * A convenience method to return the accessible API methods in CaSH as a
     * list instead of an array
     * 
     * @return the list of accessible API methods
     */
    private List<String> options() {
        return Lists.newArrayList(ConcourseShell.getAccessibleApiMethods());
    }

}

@@ -3323,24 +3323,34 @@
            %22
+concourse.
 time %5C%22yeste

update test to relfect the new time function in the thrift API