1 /*******************************************************************************
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
10 * * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
20 ******************************************************************************/
22 package org.onap.aaf.cadi.config.test;
24 import static org.hamcrest.CoreMatchers.is;
25 import static org.hamcrest.CoreMatchers.nullValue;
26 import static org.junit.Assert.assertThat;
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Test;
32 import java.io.ByteArrayOutputStream;
34 import java.io.IOException;
35 import java.io.PrintStream;
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;
44 public class JU_UsersDump {
46 private ByteArrayOutputStream outStream;
47 private ByteArrayOutputStream stdoutSuppressor;
49 private static final String expected = "<?xml version='1.0' encoding='utf-8'?>\n" +
51 " Code Generated Tomcat Users and Roles from AT&T LUR on ...\n" +
54 " <role rolename=\"suser\"/>\n" +
55 " <role rolename=\"admin\"/>\n" +
56 " <role rolename=\"groupB\"/>\n" +
57 " <role rolename=\"groupA\"/>\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" +
67 private final static String groups = "myname:groupA,groupB";
68 private final static String names = "admin:myname,yourname;suser:hisname,hername,m1234";
70 private AbsUserCache<LocalPermission> lur;
73 public void setup() throws IOException {
74 outStream = new ByteArrayOutputStream();
75 stdoutSuppressor = new ByteArrayOutputStream();
77 System.setOut(new PrintStream(stdoutSuppressor));
79 lur = new LocalLur(new PropAccess(), groups, names);
83 public void tearDown() {
84 System.setOut(System.out);
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);
96 assertThat(actualLines.length, is(expectedLines.length));
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]));
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]));
109 // Run the test again with outStream as a PrintStream (for coverage)
111 UsersDump.write(new PrintStream(outStream), lur);
112 actualLines = Split.splitTrim('\n', outStream.toString());
114 assertThat(actualLines.length, is(expectedLines.length));
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]));
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]));
129 public void updateUsersTest() {
131 File outputFile = new File("src/test/resources/userdump.xml");
132 assertThat(outputFile.exists(), is(false));
134 output = UsersDump.updateUsers("src/test/resources/userdump.xml", (LocalLur) lur);
135 assertThat(output, is(nullValue()));
136 assertThat(outputFile.exists(), is(true));
138 output = UsersDump.updateUsers("src/test/resources/userdump.xml", (LocalLur) lur);
139 assertThat(output, is(nullValue()));
140 assertThat(outputFile.exists(), is(true));