/*
 * Copyright 2012-2016 the original author or authors.
 *
 * 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.springframework.security.access.hierarchicalroles;

import org.junit.Test;

import java.util.*;

import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;

/**
 * Tests for {@link RoleHierarchyUtils}.
 *
 * @author Joe Grandja
 */
public class RoleHierarchyUtilsTests {

	@Test
	public void roleHierarchyFromMapWhenMapValidThenConvertsCorrectly() throws Exception {
		String expectedRoleHierarchy = "ROLE_A > ROLE_B\nROLE_A > ROLE_C\nROLE_B > ROLE_D\nROLE_C > ROLE_D\n";

		Map<String, List<String>> roleHierarchyMap = new TreeMap<String, List<String>>();
		roleHierarchyMap.put("ROLE_A", asList("ROLE_B", "ROLE_C"));
		roleHierarchyMap.put("ROLE_B", asList("ROLE_D"));
		roleHierarchyMap.put("ROLE_C", asList("ROLE_D"));

		String roleHierarchy = RoleHierarchyUtils.roleHierarchyFromMap(roleHierarchyMap);

		assertThat(roleHierarchy).isEqualTo(expectedRoleHierarchy);
	}

	@Test(expected = IllegalArgumentException.class)
	public void roleHierarchyFromMapWhenMapNullThenThrowsIllegalArgumentException() throws Exception {
		RoleHierarchyUtils.roleHierarchyFromMap(null);
	}

	@Test(expected = IllegalArgumentException.class)
	public void roleHierarchyFromMapWhenMapEmptyThenThrowsIllegalArgumentException() throws Exception {
		RoleHierarchyUtils.roleHierarchyFromMap(Collections.<String, List<String>>emptyMap());
	}

	@Test(expected = IllegalArgumentException.class)
	public void roleHierarchyFromMapWhenRoleNullThenThrowsIllegalArgumentException() throws Exception {
		Map<String, List<String>> roleHierarchyMap = new HashMap<String, List<String>>();
		roleHierarchyMap.put(null, asList("ROLE_B", "ROLE_C"));

		RoleHierarchyUtils.roleHierarchyFromMap(roleHierarchyMap);
	}

	@Test(expected = IllegalArgumentException.class)
	public void roleHierarchyFromMapWhenRoleEmptyThenThrowsIllegalArgumentException() throws Exception {
		Map<String, List<String>> roleHierarchyMap = new HashMap<String, List<String>>();
		roleHierarchyMap.put("", asList("ROLE_B", "ROLE_C"));

		RoleHierarchyUtils.roleHierarchyFromMap(roleHierarchyMap);
	}

	@Test(expected = IllegalArgumentException.class)
	public void roleHierarchyFromMapWhenImpliedRolesNullThenThrowsIllegalArgumentException() throws Exception {
		Map<String, List<String>> roleHierarchyMap = new HashMap<String, List<String>>();
		roleHierarchyMap.put("ROLE_A", null);

		RoleHierarchyUtils.roleHierarchyFromMap(roleHierarchyMap);
	}

	@Test(expected = IllegalArgumentException.class)
	public void roleHierarchyFromMapWhenImpliedRolesEmptyThenThrowsIllegalArgumentException() throws Exception {
		Map<String, List<String>> roleHierarchyMap = new HashMap<String, List<String>>();
		roleHierarchyMap.put("ROLE_A", Collections.<String>emptyList());

		RoleHierarchyUtils.roleHierarchyFromMap(roleHierarchyMap);
	}
}

@@ -934,16 +934,75 @@
 sTests %7B
+%0A%09private static final String EOL = System.lineSeparator();
 %0A%0A%09@Test
@@ -1090,16 +1090,36 @@
 ption %7B%0A
+%09%09// @formatter:off%0A
 %09%09String
@@ -1163,63 +1163,125 @@
 LE_B
-%5CnROLE_A %3E ROLE_C%5CnROLE_B %3E ROLE_D%5CnROLE_C %3E ROLE_D%5Cn%22;
+%22 + EOL +%0A%09%09%09%09%22ROLE_A %3E ROLE_C%22 + EOL +%0A%09%09%09%09%22ROLE_B %3E ROLE_D%22 + EOL +%0A%09%09%09%09%22ROLE_C %3E ROLE_D%22 + EOL;%0A%09%09// @formatter:on
 %0A%0A%09%09

Fix RoleHiearchyUtilsTests on Windows