Integrate aai-schema-ingest library into 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  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.aai.util;
21
22 import static org.junit.Assert.assertTrue;
23
24 import java.io.ByteArrayOutputStream;
25 import java.io.PrintStream;
26 import java.util.regex.Matcher;
27 import java.util.regex.Pattern;
28
29 import org.junit.Test;
30
31 public class JettyObfuscationConversionCommandLineUtilTest {
32
33         private final ByteArrayOutputStream testOut = new ByteArrayOutputStream();
34
35         /**
36          * Test.
37          */
38         @Test
39         public void test() {
40                 //setup, this will catch main's print statements for evaluation
41                 PrintStream oldOutputStream = System.out;
42                 System.setOut(new PrintStream(testOut));
43
44                 /* ------ TEST OBFUSCATION ----*/
45                 JettyObfuscationConversionCommandLineUtil.main(new String[]{"-e", "hello world"});
46                 /*
47                  * testOut was also catching any logging statements which interfered with result checking.
48                  * This regex business was the workaround - it tries to find the expected value in
49                  * the results and asserts against that.
50                  */
51                 String obfResult = testOut.toString();
52                 String obfExpected = "OBF:1thf1ugo1x151wfw1ylz11tr1ymf1wg21x1h1uh21th7";
53                 Pattern obfExpectPat = Pattern.compile(obfExpected);
54                 Matcher obfMatch = obfExpectPat.matcher(obfResult);
55                 assertTrue(obfMatch.find());
56
57                 testOut.reset(); //clear out previous result
58
59                 /* ------ TEST DEOBFUSCATION ----- */
60                 JettyObfuscationConversionCommandLineUtil.main(new String[]{"-d", obfExpected});
61                 String deobfResult = testOut.toString();
62                 String deobfExpected = "hello world";
63                 Pattern deobfExpectPat = Pattern.compile(deobfExpected);
64                 Matcher deobfMatch = deobfExpectPat.matcher(deobfResult);
65                 assertTrue(deobfMatch.find());
66
67                 //clean up, resets to stdout
68                 System.setOut(oldOutputStream);
69         }
70
71 }