2 * Copyright (c) 2015 Brocade Communications Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.aaa.shiro.web.env;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
16 import java.io.FileWriter;
17 import java.io.IOException;
18 import org.apache.shiro.config.Ini;
19 import org.apache.shiro.config.Ini.Section;
20 import org.junit.AfterClass;
21 import org.junit.BeforeClass;
22 import org.junit.Test;
25 * @author Ryan Goulding (ryandgoulding@gmail.com)
27 public class KarafIniWebEnvironmentTest {
28 private static File iniFile;
31 public static void setup() throws IOException {
32 iniFile = createShiroIniFile();
33 assertTrue(iniFile.exists());
37 public static void teardown() {
41 private static String createFakeShiroIniContents() {
42 return "[users]\n" + "admin=admin, ROLE_ADMIN \n" + "[roles]\n" + "ROLE_ADMIN = *\n"
43 + "[urls]\n" + "/** = authcBasic";
46 private static File createShiroIniFile() throws IOException {
47 File shiroIni = File.createTempFile("shiro", "ini");
48 FileWriter writer = new FileWriter(shiroIni);
49 writer.write(createFakeShiroIniContents());
56 public void testCreateShiroIni() throws IOException {
57 Ini ini = KarafIniWebEnvironment.createShiroIni(iniFile.getAbsolutePath());
59 assertNotNull(ini.getSection("users"));
60 assertNotNull(ini.getSection("roles"));
61 assertNotNull(ini.getSection("urls"));
62 Section usersSection = ini.getSection("users");
63 assertTrue(usersSection.containsKey("admin"));
64 assertTrue(usersSection.get("admin").contains("admin"));
65 assertTrue(usersSection.get("admin").contains("ROLE_ADMIN"));
69 public void testCreateFileBasedIniPath() {
70 String testPath = "/shiro.ini";
71 String expectedFileBasedIniPath = KarafIniWebEnvironment.SHIRO_FILE_PREFIX + testPath;
72 String actualFileBasedIniPath = KarafIniWebEnvironment.createFileBasedIniPath(testPath);
73 assertEquals(expectedFileBasedIniPath, actualFileBasedIniPath);