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