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.junit.Assert.*;
25 import static org.hamcrest.CoreMatchers.*;
28 import java.io.ByteArrayOutputStream;
30 import java.io.IOException;
31 import java.io.PrintStream;
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;
40 public class JU_UsersDump {
42 private ByteArrayOutputStream outStream;
43 private ByteArrayOutputStream stdoutSuppressor;
45 private static final String expected = "<?xml version='1.0' encoding='utf-8'?>\n" +
47 " Code Generated Tomcat Users and Roles from AT&T LUR on ...\n" +
50 " <role rolename=\"suser\"/>\n" +
51 " <role rolename=\"admin\"/>\n" +
52 " <role rolename=\"groupB\"/>\n" +
53 " <role rolename=\"groupA\"/>\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" +
63 private final static String groups = "myname:groupA,groupB";
64 private final static String names = "admin:myname,yourname;suser:hisname,hername,m1234";
66 private AbsUserCache<LocalPermission> lur;
69 public void setup() throws IOException {
70 outStream = new ByteArrayOutputStream();
71 stdoutSuppressor = new ByteArrayOutputStream();
73 System.setOut(new PrintStream(stdoutSuppressor));
75 lur = new LocalLur(new PropAccess(), groups, names);
79 public void tearDown() {
80 System.setOut(System.out);
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);
89 assertThat(actualLines.length, is(expectedLines.length));
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]));
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]));
102 // Run the test again with outStream as a PrintStream (for coverage)
104 UsersDump.write(new PrintStream(outStream), lur);
105 actualLines = Split.splitTrim('\n', outStream.toString());
107 assertThat(actualLines.length, is(expectedLines.length));
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]));
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]));
122 public void updateUsersTest() {
124 File outputFile = new File("src/test/resources/userdump.xml");
125 assertThat(outputFile.exists(), is(false));
127 output = UsersDump.updateUsers("src/test/resources/userdump.xml", (LocalLur) lur);
128 assertThat(output, is(nullValue()));
129 assertThat(outputFile.exists(), is(true));
131 output = UsersDump.updateUsers("src/test/resources/userdump.xml", (LocalLur) lur);
132 assertThat(output, is(nullValue()));
133 assertThat(outputFile.exists(), is(true));