141d0ce5b637b35a99c6b77d1f192131f3ab9240
[moon.git] /
1 /*
2  * Copyright (c) 2015 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.aaa.shiro.web.env;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14
15 import java.io.File;
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;
23
24 /**
25  * @author Ryan Goulding (ryandgoulding@gmail.com)
26  */
27 public class KarafIniWebEnvironmentTest {
28     private static File iniFile;
29
30     @BeforeClass
31     public static void setup() throws IOException {
32         iniFile = createShiroIniFile();
33         assertTrue(iniFile.exists());
34     }
35
36     @AfterClass
37     public static void teardown() {
38         iniFile.delete();
39     }
40
41     private static String createFakeShiroIniContents() {
42         return "[users]\n" + "admin=admin, ROLE_ADMIN \n" + "[roles]\n" + "ROLE_ADMIN = *\n"
43                 + "[urls]\n" + "/** = authcBasic";
44     }
45
46     private static File createShiroIniFile() throws IOException {
47         File shiroIni = File.createTempFile("shiro", "ini");
48         FileWriter writer = new FileWriter(shiroIni);
49         writer.write(createFakeShiroIniContents());
50         writer.flush();
51         writer.close();
52         return shiroIni;
53     }
54
55     @Test
56     public void testCreateShiroIni() throws IOException {
57         Ini ini = KarafIniWebEnvironment.createShiroIni(iniFile.getAbsolutePath());
58         assertNotNull(ini);
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"));
66     }
67
68     @Test
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);
74     }
75
76 }