2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.aai.util;
25 import static org.junit.Assert.assertTrue;
27 import java.io.ByteArrayOutputStream;
28 import java.io.PrintStream;
29 import java.util.regex.Matcher;
30 import java.util.regex.Pattern;
32 import org.junit.Test;
34 public class JettyObfuscationConversionCommandLineUtilTest {
36 private final ByteArrayOutputStream testOut = new ByteArrayOutputStream();
43 // setup, this will catch main's print statements for evaluation
44 PrintStream oldOutputStream = System.out;
45 System.setOut(new PrintStream(testOut));
47 /* ------ TEST OBFUSCATION ---- */
48 JettyObfuscationConversionCommandLineUtil.main(new String[] {"-e", "hello world"});
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.
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());
60 testOut.reset(); // clear out previous result
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());
70 // clean up, resets to stdout
71 System.setOut(oldOutputStream);
78 public void testUsage() {
79 System.setOut(new PrintStream(testOut));
81 /* ------ TEST OBFUSCATION ---- */
82 JettyObfuscationConversionCommandLineUtil.main(new String[] {"-f", "hello world"});
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.
88 String obfResult = testOut.toString();
89 assertTrue(obfResult.startsWith("failed to parse input"));
91 testOut.reset(); // clear out previous result