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.test.config;
24 import static org.junit.Assert.*;
25 import static org.hamcrest.CoreMatchers.*;
26 import static org.mockito.Mockito.*;
30 import java.io.ByteArrayOutputStream;
32 import java.io.IOException;
33 import java.io.PrintStream;
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;
42 public class JU_UsersDump {
44 private ByteArrayOutputStream outStream;
45 private ByteArrayOutputStream stdoutSuppressor;
47 private static final String expected = "<?xml version='1.0' encoding='utf-8'?>\n" +
49 " Code Generated Tomcat Users and Roles from AT&T LUR on ...\n" +
52 " <role rolename=\"suser\"/>\n" +
53 " <role rolename=\"admin\"/>\n" +
54 " <role rolename=\"groupB\"/>\n" +
55 " <role rolename=\"groupA\"/>\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" +
65 private final static String groups = "myname:groupA,groupB";
66 private final static String names = "admin:myname,yourname;suser:hisname,hername,m1234";
68 private AbsUserCache<LocalPermission> lur;
71 public void setup() throws IOException {
72 outStream = new ByteArrayOutputStream();
73 stdoutSuppressor = new ByteArrayOutputStream();
75 System.setOut(new PrintStream(stdoutSuppressor));
77 lur = new LocalLur(new PropAccess(), groups, names);
81 public void tearDown() {
82 System.setOut(System.out);
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);
91 assertThat(actualLines.length, is(expectedLines.length));
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]));
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]));
104 // Run the test again with outStream as a PrintStream (for coverage)
106 UsersDump.write(new PrintStream(outStream), lur);
107 actualLines = Split.splitTrim('\n', outStream.toString());
109 assertThat(actualLines.length, is(expectedLines.length));
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]));
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]));
124 public void updateUsersTest() {
126 File outputFile = new File("src/test/resources/userdump.xml");
127 assertThat(outputFile.exists(), is(false));
129 output = UsersDump.updateUsers("src/test/resources/userdump.xml", (LocalLur) lur);
130 assertThat(output, is(nullValue()));
131 assertThat(outputFile.exists(), is(true));
133 output = UsersDump.updateUsers("src/test/resources/userdump.xml", (LocalLur) lur);
134 assertThat(output, is(nullValue()));
135 assertThat(outputFile.exists(), is(true));