cff4b41450d40b85bc126bff2baa9fd49dabe6f7
[aaf/authz.git] / cadi / core / src / test / java / org / onap / aaf / cadi / util / test / JU_SubStandardConsole.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.util.test;
23
24 import static org.junit.Assert.*;
25 import static org.hamcrest.CoreMatchers.*;
26 import static org.mockito.Mockito.*;
27 import org.junit.*;
28
29 import java.io.BufferedReader;
30 import java.io.ByteArrayInputStream;
31 import java.io.ByteArrayOutputStream;
32 import java.io.IOException;
33 import java.io.PrintStream;
34 import java.lang.reflect.Field;
35
36 import org.onap.aaf.cadi.util.SubStandardConsole;
37
38 public class JU_SubStandardConsole {
39
40     private String inputString = "An input string";
41     private ByteArrayOutputStream outStream;
42     private ByteArrayOutputStream errStream;
43     private String lineSeparator = System.lineSeparator();
44
45     @Before
46     public void setup() {
47         outStream = new ByteArrayOutputStream();
48         errStream = new ByteArrayOutputStream();
49         System.setOut(new PrintStream(outStream));
50         System.setErr(new PrintStream(errStream));
51     }
52
53     @After
54     public void tearDown() {
55         System.setOut(System.out);
56         System.setErr(System.err);
57     }
58
59     @Test
60     public void readLineTest() {
61         byte[] input = inputString.getBytes();
62         System.setIn(new ByteArrayInputStream(input));
63         SubStandardConsole ssc = new SubStandardConsole();
64         String output = ssc.readLine("%s" + lineSeparator, ">>> ");
65         assertThat(output, is(inputString));
66         assertThat(outStream.toString(), is(">>> " + lineSeparator));
67     }
68
69     @Test
70     public void readLineTest2() {
71         byte[] input = inputString.getBytes();
72         System.setIn(new ByteArrayInputStream(input));
73         SubStandardConsole ssc = new SubStandardConsole();
74         String output = ssc.readLine("%s %s"  + lineSeparator, ">>> ", "Another argument for coverage");
75         assertThat(output, is(inputString));
76     }
77
78     @Test
79     public void readLineTest3() {
80         byte[] input = "\n".getBytes();
81         System.setIn(new ByteArrayInputStream(input));
82         SubStandardConsole ssc = new SubStandardConsole();
83         String output = ssc.readLine("%s" + lineSeparator, ">>> ");
84         assertThat(output, is(">>> "));
85         assertThat(outStream.toString(), is(">>> " + lineSeparator));
86     }
87
88     @Test
89     public void readPasswordTest() {
90         byte[] input = inputString.getBytes();
91         System.setIn(new ByteArrayInputStream(input));
92         SubStandardConsole ssc = new SubStandardConsole();
93         char[] output = ssc.readPassword("%s" + lineSeparator, ">>> ");
94         System.out.println(output);
95         assertThat(output, is(inputString.toCharArray()));
96         assertThat(outStream.toString(), is(">>> " + lineSeparator + "An input string"  + lineSeparator));
97     }
98
99     @Test
100     public void printfTest() {
101         byte[] input = inputString.getBytes();
102         System.setIn(new ByteArrayInputStream(input));
103         SubStandardConsole ssc = new SubStandardConsole();
104         ssc.printf("%s", "A format specifier");
105         assertThat(outStream.toString(), is("A format specifier"));
106     }
107
108     @Test
109     public void throwsTest() throws IOException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
110         BufferedReader brMock = mock(BufferedReader.class);
111         when(brMock.readLine()).thenThrow(new IOException());
112
113         SubStandardConsole ssc = new SubStandardConsole();
114
115         Field brField = SubStandardConsole.class.getDeclaredField("br");
116         brField.setAccessible(true);
117         brField.set(ssc, brMock);
118
119         assertThat(ssc.readLine(""), is(""));
120         assertThat(errStream.toString(), is("uh oh..." + lineSeparator));
121         errStream.reset();
122         assertThat(ssc.readPassword("").length, is(0));
123         assertThat(errStream.toString(), is("uh oh..." + lineSeparator));
124     }
125
126 }