/*
 * This file is part of Sponge, licensed under the MIT License (MIT).
 *
 * Copyright (c) SpongePowered.org <http://www.spongepowered.org>
 * Copyright (c) contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package ninja.leaping.permissionsex.util;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.spongepowered.api.util.Tristate;

import java.util.HashMap;
import java.util.Map;

public class NodeTreeTest {

    @Test
    public void testAsMap() {
        final Map<String, Integer> testPermissions = new HashMap<>();
        testPermissions.put("generate.rainbow", 1);
        testPermissions.put("generate.sunset", -1);
        testPermissions.put("generate", 1);
        testPermissions.put("generate.thunderstorm.explosive", -1);

        NodeTree oldTree = NodeTree.of(testPermissions);

        assertEquals(testPermissions, oldTree.asMap());
    }

    @Test
    public void testWithValue() throws Exception {
        final Map<String, Integer> testPermissions = new HashMap<>();
        testPermissions.put("generate.rainbow", 1);
        testPermissions.put("generate.sunset", -1);
        testPermissions.put("generate", 1);
        testPermissions.put("generate.thunderstorm.explosive", -1);

        NodeTree oldTree = NodeTree.of(testPermissions);
        assertEquals(-1, oldTree.get("generate.thunderstorm.explosive"));
        NodeTree newTree = oldTree.withValue("generate.thunderstorm.explosive", 1);
        assertEquals(-1, oldTree.get("generate.thunderstorm.explosive"));
        assertEquals(1, newTree.get("generate.thunderstorm.explosive"));
    }

    @Test
    public void testWithAll() throws Exception {
        final Map<String, Integer> testPermissions = new HashMap<>();
        testPermissions.put("generate.rainbow", 1);
        testPermissions.put("generate.sunset", -1);
        testPermissions.put("generate", 1);
        testPermissions.put("generate.thunderstorm.explosive", -1);

        NodeTree oldTree = NodeTree.of(testPermissions);

        final Map<String, Integer> newPermissions = new HashMap<>();
        newPermissions.put("generate.sunset.red", 1);
        newPermissions.put("generate.thunderstorm.explosive", 0);
        newPermissions.put("something.new", -1);

        NodeTree newTree = oldTree.withAll(newPermissions);

        assertEquals(-1, oldTree.get("generate.sunset.red"));
        assertEquals(1, newTree.get("generate.sunset.red"));

        assertEquals(-1, oldTree.get("generate.thunderstorm.explosive"));
        assertEquals(0, newTree.get("generate.thunderstorm.explosive"));

        assertEquals(0, oldTree.get("something.new"));
        assertEquals(-1, newTree.get("something.new"));
    }

    @Test
    public void testCreateFromValues() throws Exception {
        final Map<String, Integer> testPermissions = new HashMap<>();
        testPermissions.put("generate.rainbow", 1);
        testPermissions.put("generate.sunset", -1);
        testPermissions.put("generate", 1);
        testPermissions.put("generate.thunderstorm.explosive", -1);

        NodeTree nodes = NodeTree.of(testPermissions, 0);

        assertEquals(1, nodes.get("generate.rainbow"));
        assertEquals(1, nodes.get("generate.rainbow.double"));
        assertEquals(-1, nodes.get("generate.sunset"));
        assertEquals(-1, nodes.get("generate.sunset.east"));
        assertEquals(1, nodes.get("generate.thunderstorm"));
        assertEquals(-1, nodes.get("generate.thunderstorm.explosive"));
        assertEquals(0, nodes.get("random.perm"));
    }
}

@@ -1,79 +1,24 @@
 /*
+*
 %0A * 
-This file is part of Sponge, licensed under the MIT License (MIT).%0A *
+PermissionsEx
 %0A * 
@@ -32,719 +32,424 @@
 ht (
-c) SpongePowered.org %3Chttp://www.spongepowered.org%3E%0A * Copyright (c) contributors%0A *%0A * Permission is hereby granted, free of charge, to any person obtaining a copy%0A * of this software and associated documentation files (the %22Software%22), to deal%0A * in the Software without restriction, including without limitation the rights%0A * to use, copy, modify, merge, publish, distribute, subl
+C) zml and PermissionsEx contributors%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the L
 icense
-,
  a
-nd/or sell%0A * copies of the Software, and to permit persons to whom the Software is%0A * furnished to do so, subject to the following conditions:%0A *%0A * The above copyright notice and this permission notice shall be included in%0A * all copies or substantial portions of the Software.%0A *%0A * THE SOFTWARE IS PROVIDED %22
+t%0A *%0A *    http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 B
 AS
- 
 IS
-%22
 ,
+%0A *
  WIT
@@ -464,436 +464,169 @@
 RANT
-Y OF ANY KIND, EXPRESS OR%0A * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,%0A * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE%0A * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER%0A * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,%0A * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN%0A * THE SOFTWARE
+IES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License
 .%0A *

Fix license header in NodeTreeTest