Merge "Support Multiple Realms for DefaultOrg"
[aaf/authz.git] / cadi / core / src / test / java / org / onap / aaf / cadi / test / config / JU_UsersDump.java
1 /*******************************************************************************
2  * * org.onap.aaf
3  * * ===========================================================================
4  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
5  * * ===========================================================================
6  * * Licensed under the Apache License, Version 2.0 (the "License");
7  * * you may not use this file except in compliance with the License.
8  * * You may obtain a copy of the License at
9  * *
10  *  *      http://www.apache.org/licenses/LICENSE-2.0
11  * *
12  *  * Unless required by applicable law or agreed to in writing, software
13  * * distributed under the License is distributed on an "AS IS" BASIS,
14  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * * See the License for the specific language governing permissions and
16  * * limitations under the License.
17  * * ============LICENSE_END====================================================
18  * *
19  * *
20  ******************************************************************************/
21
22 package org.onap.aaf.cadi.test.config;
23
24 import static org.junit.Assert.*;
25 import static org.hamcrest.CoreMatchers.*;
26 import static org.mockito.Mockito.*;
27 import org.junit.*;
28 import org.mockito.*;
29
30 import java.io.ByteArrayOutputStream;
31 import java.io.File;
32 import java.io.IOException;
33 import java.io.PrintStream;
34
35 import org.onap.aaf.cadi.AbsUserCache;
36 import org.onap.aaf.cadi.PropAccess;
37 import org.onap.aaf.cadi.config.UsersDump;
38 import org.onap.aaf.cadi.lur.LocalLur;
39 import org.onap.aaf.cadi.lur.LocalPermission;
40 import org.onap.aaf.cadi.util.Split;
41
42 public class JU_UsersDump {
43
44         private ByteArrayOutputStream outStream;
45         private ByteArrayOutputStream stdoutSuppressor;
46
47         private static final String expected = "<?xml version='1.0' encoding='utf-8'?>\n" +
48                 "<!--\n" +
49                 "    Code Generated Tomcat Users and Roles from AT&T LUR on ...\n" +
50                 "-->\n" +
51                 "<tomcat-users>\n" +
52                 "  <role rolename=\"suser\"/>\n" +
53                 "  <role rolename=\"admin\"/>\n" +
54                 "  <role rolename=\"groupB\"/>\n" +
55                 "  <role rolename=\"groupA\"/>\n" +
56                 "  \n" +
57                 "  <user username=\"yourname@none\" roles=\"admin\"/>\n" +
58                 "  <user username=\"m1234@none\" roles=\"suser\"/>\n" +
59                 "  <user username=\"hisname@none\" roles=\"suser\"/>\n" +
60                 "  <user username=\"hername@none\" roles=\"suser\"/>\n" +
61                 "  <user username=\"myname\" roles=\"groupB,groupA\"/>\n" +
62                 "  <user username=\"myname@none\" roles=\"admin\"/>\n" +
63                 "</tomcat-users>\n";
64
65         private final static String groups = "myname:groupA,groupB";
66         private final static String names = "admin:myname,yourname;suser:hisname,hername,m1234";
67
68         private AbsUserCache<LocalPermission> lur;
69                 
70         @Before
71         public void setup() throws IOException {
72                 outStream = new ByteArrayOutputStream();
73                 stdoutSuppressor = new ByteArrayOutputStream();
74
75                 System.setOut(new PrintStream(stdoutSuppressor));
76
77                 lur = new LocalLur(new PropAccess(), groups, names);
78         }
79
80         @After
81         public void tearDown() {
82                 System.setOut(System.out);
83         }
84
85         @Test
86         public void writeTest() throws IOException {
87                 UsersDump.write(outStream, lur);
88                 String[] actualLines = Split.splitTrim('\n', outStream.toString());
89                 String[] expectedLines = Split.splitTrim('\n', expected);
90
91                 assertThat(actualLines.length, is(expectedLines.length));
92
93                 // Check that the output starts with an XML tag
94                 assertThat(actualLines[0], is(expectedLines[0]));
95                 // Check that lines 2-4 are a comment
96                 assertThat(actualLines[1], is(expectedLines[1]));
97                 assertThat(actualLines[3], is(expectedLines[3]));
98
99                 // Check that the rest of the output matches the expected output
100                 for (int i = 4; i < actualLines.length; i++) {
101                         assertThat(actualLines[i], is(expectedLines[i]));
102                 }
103
104                 // Run the test again with outStream as a PrintStream (for coverage)
105                 outStream.reset();
106                 UsersDump.write(new PrintStream(outStream), lur);
107                 actualLines = Split.splitTrim('\n', outStream.toString());
108
109                 assertThat(actualLines.length, is(expectedLines.length));
110
111                 // Check that the output starts with an XML tag
112                 assertThat(actualLines[0], is(expectedLines[0]));
113                 // Check that lines 2-4 are a comment
114                 assertThat(actualLines[1], is(expectedLines[1]));
115                 assertThat(actualLines[3], is(expectedLines[3]));
116
117                 // Check that the rest of the output matches the expected output
118                 for (int i = 4; i < actualLines.length; i++) {
119                         assertThat(actualLines[i], is(expectedLines[i]));
120                 }
121         }
122
123         @Test
124         public void updateUsersTest() {
125                 String output;
126                 File outputFile = new File("src/test/resources/userdump.xml");
127                 assertThat(outputFile.exists(), is(false));
128
129                 output = UsersDump.updateUsers("src/test/resources/userdump.xml", (LocalLur) lur);
130                 assertThat(output, is(nullValue()));
131                 assertThat(outputFile.exists(), is(true));
132
133                 output = UsersDump.updateUsers("src/test/resources/userdump.xml", (LocalLur) lur);
134                 assertThat(output, is(nullValue()));
135                 assertThat(outputFile.exists(), is(true));
136
137                 outputFile.delete();
138         }
139
140 }