/*
 * Copyright 2014, Kaazing Corporation. All rights reserved.
 *
 * 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.kaazing.netx.http.internal.auth;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.kaazing.netx.http.internal.auth.DefaultDispatchChallengeHandler;

import java.util.Arrays;

import static org.kaazing.netx.http.internal.auth.NodeTest.NodeType.TEST;

public class NodeTest {

    private DefaultDispatchChallengeHandler.Node<String,NodeType> root;

    static enum NodeType {
        TEST
    }

    @Before
    public void setUp() throws Exception {
        root = new DefaultDispatchChallengeHandler.Node<String, NodeType>();
    }

    @Test
    public void testAccessRootNode() throws Exception {
        Assert.assertNull(root.getName());
        Assert.assertNull(root.getParent());
        Assert.assertTrue(root.isRootNode());
    }

    @Test
    public void testMakeChild() throws Exception {
        root.addChild("foo", TEST);
        DefaultDispatchChallengeHandler.Node fooNode = root.getChild("foo");
        Assert.assertEquals("foo", fooNode.getName());
        Assert.assertSame(root, fooNode.getParent());
    }

    @Test
    public void testFullyQualifiedName() throws Exception {
        Assert.assertEquals("", root.getFullyQualifiedName());
        root.addChild("foo", TEST);
        DefaultDispatchChallengeHandler.Node foo = root.getChild("foo");
        Assert.assertEquals("foo", foo.getFullyQualifiedName());

        foo.addChild("bar", TEST);
        DefaultDispatchChallengeHandler.Node bar = foo.getChild("bar");
        Assert.assertEquals("foo.bar", bar.getFullyQualifiedName());
    }

    @Test
    public void testCannotFindChildWithWrongName() throws Exception {
        Assert.assertNull(root.getChild("bar"));
        root.addChild("foo", TEST);
        Assert.assertNull(root.getChild("bar"));

    }

    @Test
    public void testWildcard() throws Exception {
        Assert.assertFalse(root.isWildcard());
        root.addChild("foo", TEST);
        DefaultDispatchChallengeHandler.Node foo = root.getChild("foo");
        Assert.assertFalse(foo.isWildcard());
    }


    @Test(expected = IllegalArgumentException.class)
    public void testSetValueOnRootNodeIllegal() throws Exception {
        root.appendValues("VALUE");
    }

    @Test
    public void testSetValue() {
        root.addChild("foo", TEST);
        DefaultDispatchChallengeHandler.Node<String, NodeType> foo = root.getChild("foo");
        foo.appendValues("VALUE");
        Assert.assertEquals(Arrays.asList("VALUE"),foo.getValues());
    }

    @Test
    public void testGetParent() throws Exception {
        root.addChild("foo", TEST);
        DefaultDispatchChallengeHandler.Node foo = root.getChild("foo");
        foo.addChild("bar", TEST);
        DefaultDispatchChallengeHandler.Node bar = foo.getChild("bar");

        Assert.assertNull(root.getParent());
        Assert.assertEquals(root, foo.getParent());
        Assert.assertEquals(foo, bar.getParent());
    }

    @Test
    public void testHasChildren() throws Exception {
        Assert.assertFalse(root.hasChildren());
        root.addChild("foo", TEST);
        Assert.assertTrue(root.hasChildren());
    }

    @Test
    public void testIsWildcard() throws Exception {
        Assert.assertFalse(root.isWildcard());
        root.addChild("foo", TEST);
        Assert.assertFalse(root.isWildcard());
        DefaultDispatchChallengeHandler.Node foo = root.getChild("foo");
        Assert.assertFalse(foo.isWildcard());

        foo.addChild(DefaultDispatchChallengeHandler.Node.getWildcardChar(), TEST);
        Assert.assertFalse(foo.isWildcard());
        Assert.assertTrue(foo.getChild(DefaultDispatchChallengeHandler.Node.getWildcardChar()).isWildcard());
    }

    @Test
    public void testHasWildcardDefined() throws Exception {
        root.addChild("foo", TEST);
        DefaultDispatchChallengeHandler.Node foo = root.getChild("foo");
        foo.addChild(DefaultDispatchChallengeHandler.Node.getWildcardChar(), TEST);
        Assert.assertTrue(foo.hasWildcardChild());
        Assert.assertTrue(foo.getChild(DefaultDispatchChallengeHandler.Node.getWildcardChar()).isWildcard());
        Assert.assertFalse(foo.isWildcard());
        Assert.assertFalse(foo.getParent().isWildcard());
    }

    @Test
    public void testGetWildcard() throws Exception {
        root.addChild("foo", TEST);
        DefaultDispatchChallengeHandler.Node foo = root.getChild("foo");
        foo.addChild(DefaultDispatchChallengeHandler.Node.getWildcardChar(), TEST);
        DefaultDispatchChallengeHandler.Node fooChild = foo.getChild(DefaultDispatchChallengeHandler.Node.getWildcardChar());
        // Wildcard nodes know they are wildcards.
        Assert.assertTrue(fooChild.isWildcard());
        // Parents of wildcards know they have wildcard children.
        Assert.assertTrue(foo.hasWildcardChild());

    }



    @Test(expected = IllegalArgumentException.class)
    public void testCannotCreateANullNamedNode() throws Exception {
        root.addChild(null, TEST);
    }

    @Test(expected = IllegalArgumentException.class)
    public void testCannotCreateAnEmptyNamedNode() throws Exception {
        root.addChild("", TEST);
    }
}

@@ -662,16 +662,117 @@
 .auth;%0A%0A
+import static org.kaazing.netx.http.internal.auth.NodeTest.NodeType.TEST;%0A%0Aimport java.util.Arrays;%0A%0A
 import o
@@ -914,109 +914,13 @@
 dler
-;%0A%0Aimport java.util.Arrays;%0A%0Aimport static org.kaazing.netx.http.internal.auth.NodeTest.NodeType.TEST
+.Node
 ;%0A%0Ap
@@ -1518,44 +1518,30 @@
     
-DefaultDispatchChallengeHandler.Node
+Node%3CString, NodeType%3E
  foo
@@ -1866,44 +1866,30 @@
     
-DefaultDispatchChallengeHandler.Node
+Node%3CString, NodeType%3E
  foo
@@ -2026,44 +2026,30 @@
     
-DefaultDispatchChallengeHandler.Node
+Node%3CString, NodeType%3E
  bar
@@ -2525,44 +2525,30 @@
     
-DefaultDispatchChallengeHandler.Node
+Node%3CString, NodeType%3E
  foo
@@ -3179,44 +3179,30 @@
     
-DefaultDispatchChallengeHandler.Node
+Node%3CString, NodeType%3E
  foo
@@ -3273,44 +3273,30 @@
     
-DefaultDispatchChallengeHandler.Node
+Node%3CString, NodeType%3E
  bar
@@ -3880,44 +3880,30 @@
     
-DefaultDispatchChallengeHandler.Node
+Node%3CString, NodeType%3E
  foo
@@ -4339,44 +4339,30 @@
     
-DefaultDispatchChallengeHandler.Node
+Node%3CString, NodeType%3E
  foo
@@ -4853,44 +4853,30 @@
     
-DefaultDispatchChallengeHandler.Node
+Node%3CString, NodeType%3E
  foo
@@ -4996,44 +4996,30 @@
     
-DefaultDispatchChallengeHandler.Node
+Node%3CString, NodeType%3E
  foo

Eliminate warnings