7d7ca77c62b42214f8953c53e7bb40ab1b53e76d
[aaf/authz.git] / cadi / core / src / test / java / org / onap / aaf / cadi / config / test / 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.config.test;
23
24 import static org.hamcrest.CoreMatchers.is;
25 import static org.hamcrest.CoreMatchers.nullValue;
26 import static org.junit.Assert.assertThat;
27
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Test;
31
32 import java.io.ByteArrayOutputStream;
33 import java.io.File;
34 import java.io.IOException;
35 import java.io.PrintStream;
36
37 import org.onap.aaf.cadi.AbsUserCache;
38 import org.onap.aaf.cadi.PropAccess;
39 import org.onap.aaf.cadi.config.UsersDump;
40 import org.onap.aaf.cadi.lur.LocalLur;
41 import org.onap.aaf.cadi.lur.LocalPermission;
42 import org.onap.aaf.cadi.util.Split;
43
44 public class JU_UsersDump {
45
46         private ByteArrayOutputStream outStream;
47         private ByteArrayOutputStream stdoutSuppressor;
48
49         private static final String expected = "<?xml version='1.0' encoding='utf-8'?>\n" +
50                 "<!--\n" +
51                 "    Code Generated Tomcat Users and Roles from AT&T LUR on ...\n" +
52                 "-->\n" +
53                 "<tomcat-users>\n" +
54                 "  <role rolename=\"suser\"/>\n" +
55                 "  <role rolename=\"admin\"/>\n" +
56                 "  <role rolename=\"groupB\"/>\n" +
57                 "  <role rolename=\"groupA\"/>\n" +
58                 "  \n" +
59                 "  <user username=\"yourname@none\" roles=\"admin\"/>\n" +
60                 "  <user username=\"m1234@none\" roles=\"suser\"/>\n" +
61                 "  <user username=\"hisname@none\" roles=\"suser\"/>\n" +
62                 "  <user username=\"hername@none\" roles=\"suser\"/>\n" +
63                 "  <user username=\"myname\" roles=\"groupB,groupA\"/>\n" +
64                 "  <user username=\"myname@none\" roles=\"admin\"/>\n" +
65                 "</tomcat-users>\n";
66
67         private final static String groups = "myname:groupA,groupB";
68         private final static String names = "admin:myname,yourname;suser:hisname,hername,m1234";
69
70         private AbsUserCache<LocalPermission> lur;
71
72         @Before
73         public void setup() throws IOException {
74                 outStream = new ByteArrayOutputStream();
75                 stdoutSuppressor = new ByteArrayOutputStream();
76
77                 System.setOut(new PrintStream(stdoutSuppressor));
78
79                 lur = new LocalLur(new PropAccess(), groups, names);
80         }
81
82         @After
83         public void tearDown() {
84                 System.setOut(System.out);
85         }
86
87         @Test
88         public void writeTest() throws IOException {
89                 UsersDump.write(outStream, lur);
90                 String[] actualLines = Split.splitTrim('\n', outStream.toString());
91                 String[] expectedLines = Split.splitTrim('\n', expected);
92                 for (String s : actualLines) {
93                         System.out.println(s);
94                 }
95
96                 assertThat(actualLines.length, is(expectedLines.length));
97
98                 // Check that the output starts with an XML tag
99                 assertThat(actualLines[0], is(expectedLines[0]));
100                 // Check that lines 2-4 are a comment
101                 assertThat(actualLines[1], is(expectedLines[1]));
102                 assertThat(actualLines[3], is(expectedLines[3]));
103
104                 // Check that the rest of the output matches the expected output
105                 for (int i = 4; i < actualLines.length; i++) {
106                         assertThat(actualLines[i], is(expectedLines[i]));
107                 }
108
109                 // Run the test again with outStream as a PrintStream (for coverage)
110                 outStream.reset();
111                 UsersDump.write(new PrintStream(outStream), lur);
112                 actualLines = Split.splitTrim('\n', outStream.toString());
113
114                 assertThat(actualLines.length, is(expectedLines.length));
115
116                 // Check that the output starts with an XML tag
117                 assertThat(actualLines[0], is(expectedLines[0]));
118                 // Check that lines 2-4 are a comment
119                 assertThat(actualLines[1], is(expectedLines[1]));
120                 assertThat(actualLines[3], is(expectedLines[3]));
121
122                 // Check that the rest of the output matches the expected output
123                 for (int i = 4; i < actualLines.length; i++) {
124                         assertThat(actualLines[i], is(expectedLines[i]));
125                 }
126         }
127
128         @Test
129         public void updateUsersTest() {
130                 String output;
131                 File outputFile = new File("src/test/resources/userdump.xml");
132                 assertThat(outputFile.exists(), is(false));
133
134                 output = UsersDump.updateUsers("src/test/resources/userdump.xml", (LocalLur) lur);
135                 assertThat(output, is(nullValue()));
136                 assertThat(outputFile.exists(), is(true));
137
138                 output = UsersDump.updateUsers("src/test/resources/userdump.xml", (LocalLur) lur);
139                 assertThat(output, is(nullValue()));
140                 assertThat(outputFile.exists(), is(true));
141
142                 outputFile.delete();
143         }
144
145 }