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