AT&T 2.0.19 Code drop, stage 3
[aaf/authz.git] / authz-cmd / src / test / java / org / onap / aaf / cmd / JU_AAFCli.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aaf\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * ===========================================================================\r
7  * * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * * you may not use this file except in compliance with the License.\r
9  * * You may obtain a copy of the License at\r
10  * * \r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * * \r
13  *  * Unless required by applicable law or agreed to in writing, software\r
14  * * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * * See the License for the specific language governing permissions and\r
17  * * limitations under the License.\r
18  * * ============LICENSE_END====================================================\r
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 package org.onap.aaf.cmd;\r
24 \r
25 import static org.junit.Assert.assertFalse;\r
26 import static org.junit.Assert.assertTrue;\r
27 import static org.mockito.Mockito.mock;\r
28 \r
29 import java.io.IOException;\r
30 import java.io.OutputStreamWriter;\r
31 import java.net.HttpURLConnection;\r
32 import java.security.GeneralSecurityException;\r
33 \r
34 import org.junit.BeforeClass;\r
35 import org.junit.Test;\r
36 import org.junit.runner.RunWith;\r
37 import org.mockito.runners.MockitoJUnitRunner;\r
38 import org.onap.aaf.authz.env.AuthzEnv;\r
39 import org.onap.aaf.cmd.AAFcli;\r
40 import org.onap.aaf.cadi.CadiException;\r
41 import org.onap.aaf.cadi.Locator;\r
42 import org.onap.aaf.cadi.LocatorException;\r
43 import org.onap.aaf.cadi.client.PropertyLocator;\r
44 import org.onap.aaf.cadi.config.Config;\r
45 import org.onap.aaf.cadi.config.SecurityInfo;\r
46 import org.onap.aaf.cadi.http.HBasicAuthSS;\r
47 import org.onap.aaf.cadi.http.HMangr;\r
48 import org.onap.aaf.inno.env.APIException;\r
49 \r
50 @RunWith(MockitoJUnitRunner.class)\r
51 public class JU_AAFCli {\r
52 \r
53         private static AAFcli cli;\r
54         private static int TIMEOUT = Integer.parseInt(Config.AAF_CONN_TIMEOUT_DEF);\r
55 \r
56         @BeforeClass\r
57         public static void setUp() throws Exception, Exception {\r
58                 cli = getAAfCli();\r
59         }\r
60 \r
61         @Test\r
62         public void eval() throws Exception {\r
63                 assertTrue(cli.eval("#startswith"));\r
64         }\r
65 \r
66         @Test\r
67         public void eval_empty() throws Exception {\r
68                 assertTrue(cli.eval(""));\r
69         }\r
70 \r
71         @Test\r
72         public void eval1() throws Exception {\r
73                 assertTrue(cli.eval("@[123"));\r
74         }\r
75 \r
76         @Test\r
77         public void eval2() throws Exception {\r
78                 assertFalse(cli.eval("as @[ 123"));\r
79         }\r
80 \r
81         @Test\r
82         public void eval3() throws Exception {\r
83                 try {\r
84                         cli.eval("expect @[ 123");\r
85                 } catch (Exception e) {\r
86                         // TODO Auto-generated catch block\r
87                         assertTrue(e instanceof CadiException);\r
88                 }\r
89         }\r
90 \r
91         public void eval31() throws Exception {\r
92                 try {\r
93                         cli.eval("expect 1 @[ 123");\r
94                 } catch (Exception e) {\r
95                         // TODO Auto-generated catch block\r
96                         assertTrue(e instanceof CadiException);\r
97                 }\r
98         }\r
99 \r
100         @Test\r
101         public void eval4() throws Exception {\r
102                 try {\r
103                         cli.eval("sleep @[ 123");\r
104                 } catch (Exception e) {\r
105                         assertTrue(e instanceof NumberFormatException);\r
106                 }\r
107         }\r
108 \r
109         @Test\r
110         public void eval41() throws Exception {\r
111                 assertTrue(cli.eval("sleep 1 @[ 123"));\r
112         }\r
113 \r
114         @Test\r
115         public void eval5() throws Exception {\r
116                 try {\r
117                         cli.eval("delay @[ 123");\r
118                 } catch (Exception e) {\r
119                         assertTrue(e instanceof NumberFormatException);\r
120                 }\r
121         }\r
122 \r
123         @Test\r
124         public void eval51() throws Exception {\r
125                 assertTrue(cli.eval("delay 1 @[ 123"));\r
126         }\r
127 \r
128         @Test\r
129         public void eval7() throws Exception {\r
130                 assertFalse(cli.eval("exit @[ 123"));\r
131         }\r
132 \r
133         @Test\r
134         public void eval8() throws Exception {\r
135                 assertTrue(cli.eval("REQUEST @[ 123"));\r
136         }\r
137 \r
138         @Test\r
139         public void eval9() throws Exception {\r
140                 assertTrue(cli.eval("FORCE @[ 123"));\r
141         }\r
142 \r
143         @Test\r
144         public void eval10() throws Exception {\r
145                 assertTrue(cli.eval("set @[ 123"));\r
146         }\r
147 \r
148         @Test\r
149         public void keyboardHelp() throws Exception {\r
150                 boolean noError=true;\r
151                 try {\r
152                         cli.keyboardHelp();\r
153                 } catch (Exception e) {\r
154                         noError=false;\r
155                 }\r
156                 assertTrue(noError);\r
157         }\r
158         \r
159 \r
160         \r
161         @Test\r
162         public void setProp() throws Exception {\r
163                 boolean noError=true;\r
164                 try {\r
165                         cli.keyboardHelp();\r
166                 } catch (Exception e) {\r
167                         noError=false;\r
168                 }\r
169                 assertTrue(noError);\r
170         }\r
171         \r
172         @Test\r
173         public void eval_randomString() throws Exception {\r
174                 assertTrue(cli.eval("Some random string @#&*& to check complete 100 coverage"));\r
175         }\r
176 \r
177         public static AAFcli getAAfCli() throws APIException, LocatorException, GeneralSecurityException, IOException {\r
178                 final AuthzEnv env = new AuthzEnv(System.getProperties());\r
179                 String aafUrl = "https://DME2RESOLVE";\r
180                 SecurityInfo si = new SecurityInfo(env);\r
181                 env.loadToSystemPropsStartsWith("AAF", "DME2");\r
182                 Locator loc;\r
183                 loc = new PropertyLocator(aafUrl);\r
184                 TIMEOUT = Integer.parseInt(env.getProperty(Config.AAF_CONN_TIMEOUT, Config.AAF_CONN_TIMEOUT_DEF));\r
185                 HMangr hman = new HMangr(env, loc).readTimeout(TIMEOUT).apiVersion("2.0");\r
186 \r
187                 // TODO: Consider requiring a default in properties\r
188                 env.setProperty(Config.AAF_DEFAULT_REALM,\r
189                                 System.getProperty(Config.AAF_DEFAULT_REALM, Config.getDefaultRealm()));\r
190                 HBasicAuthSS ss = mock(HBasicAuthSS.class);\r
191                 return new AAFcli(env, new OutputStreamWriter(System.out), hman, si, ss);\r
192         }\r
193 }\r