e1fb24a3a2631e3006e2cbd231b1ed55cf62bed9
[aai/resources.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.openecomp.aai
4  * ================================================================================
5  * Copyright (C) 2017 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
21 package org.openecomp.aai.util;
22
23 import static org.junit.Assert.*;
24
25 import java.io.ByteArrayOutputStream;
26 import java.io.PrintStream;
27 import java.util.regex.Matcher;
28 import java.util.regex.Pattern;
29
30 import org.junit.Test;
31
32
33 public class JettyObfuscationConversionCommandLineUtilTest {
34         private final ByteArrayOutputStream testOut = new ByteArrayOutputStream();
35
36         /**
37          * Test.
38          */
39         @Test
40         public void test() {
41                 //setup, this will catch main's print statements for evaluation
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(null);
69         }
70
71 }