AAI-1523 Batch reformat aai-core
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / util / JettyObfuscationConversionCommandLineUtilTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  *  Modifications Copyright © 2018 IBM.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *    http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.aai.util;
24
25 import static org.junit.Assert.assertTrue;
26
27 import java.io.ByteArrayOutputStream;
28 import java.io.PrintStream;
29 import java.util.regex.Matcher;
30 import java.util.regex.Pattern;
31
32 import org.junit.Test;
33
34 public class JettyObfuscationConversionCommandLineUtilTest {
35
36     private final ByteArrayOutputStream testOut = new ByteArrayOutputStream();
37
38     /**
39      * Test.
40      */
41     @Test
42     public void test() {
43         // setup, this will catch main's print statements for evaluation
44         PrintStream oldOutputStream = System.out;
45         System.setOut(new PrintStream(testOut));
46
47         /* ------ TEST OBFUSCATION ---- */
48         JettyObfuscationConversionCommandLineUtil.main(new String[] {"-e", "hello world"});
49         /*
50          * testOut was also catching any logging statements which interfered with result checking.
51          * This regex business was the workaround - it tries to find the expected value in
52          * the results and asserts against that.
53          */
54         String obfResult = testOut.toString();
55         String obfExpected = "OBF:1thf1ugo1x151wfw1ylz11tr1ymf1wg21x1h1uh21th7";
56         Pattern obfExpectPat = Pattern.compile(obfExpected);
57         Matcher obfMatch = obfExpectPat.matcher(obfResult);
58         assertTrue(obfMatch.find());
59
60         testOut.reset(); // clear out previous result
61
62         /* ------ TEST DEOBFUSCATION ----- */
63         JettyObfuscationConversionCommandLineUtil.main(new String[] {"-d", obfExpected});
64         String deobfResult = testOut.toString();
65         String deobfExpected = "hello world";
66         Pattern deobfExpectPat = Pattern.compile(deobfExpected);
67         Matcher deobfMatch = deobfExpectPat.matcher(deobfResult);
68         assertTrue(deobfMatch.find());
69
70         // clean up, resets to stdout
71         System.setOut(oldOutputStream);
72     }
73
74     /**
75      * Test.
76      */
77     @Test
78     public void testUsage() {
79         System.setOut(new PrintStream(testOut));
80
81         /* ------ TEST OBFUSCATION ---- */
82         JettyObfuscationConversionCommandLineUtil.main(new String[] {"-f", "hello world"});
83         /*
84          * testOut was also catching any logging statements which interfered with result checking.
85          * This regex business was the workaround - it tries to find the expected value in
86          * the results and asserts against that.
87          */
88         String obfResult = testOut.toString();
89         assertTrue(obfResult.startsWith("failed to parse input"));
90
91         testOut.reset(); // clear out previous result
92
93     }
94
95 }