abe4c34219115d03377e1ebe6bd3031e8b0ba6cc
[aaf/authz.git] / auth / auth-cmd / src / test / java / org / onap / aaf / auth / cmd / test / JU_AAFCli.java
1 /*******************************************************************************
2  * ============LICENSE_START====================================================
3  * * org.onap.aaf
4  * * ===========================================================================
5  * * Copyright © 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  ******************************************************************************/
22
23 package org.onap.aaf.auth.cmd.test;
24
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
28 import static org.mockito.Mockito.mock;
29
30 import java.io.IOException;
31 import java.io.OutputStreamWriter;
32 import java.net.HttpURLConnection;
33 import java.security.GeneralSecurityException;
34
35 import org.junit.Assert;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.mockito.runners.MockitoJUnitRunner;
40 import org.onap.aaf.auth.cmd.AAFcli;
41 import org.onap.aaf.auth.env.AuthzEnv;
42 import org.onap.aaf.cadi.CadiException;
43 import org.onap.aaf.cadi.Locator;
44 import org.onap.aaf.cadi.LocatorException;
45 import org.onap.aaf.cadi.config.Config;
46 import org.onap.aaf.cadi.config.SecurityInfo;
47 import org.onap.aaf.cadi.config.SecurityInfoC;
48 import org.onap.aaf.cadi.http.HBasicAuthSS;
49 import org.onap.aaf.cadi.http.HMangr;
50 import org.onap.aaf.cadi.locator.PropertyLocator;
51 import org.onap.aaf.misc.env.APIException;
52
53 @RunWith(MockitoJUnitRunner.class)
54 public class JU_AAFCli {
55
56     private static AAFcli cli;
57     private static int TIMEOUT = Integer.parseInt(Config.AAF_CONN_TIMEOUT_DEF);
58
59     @BeforeClass
60     public static void setUp() throws Exception, Exception {
61         cli = getAAfCli();
62     }
63
64     @Test
65     public void eval() throws Exception {
66         assertTrue(cli.eval("#startswith"));
67     }
68
69     @Test
70     public void eval_empty() throws Exception {
71         assertTrue(cli.eval(""));
72     }
73
74     @Test
75     public void eval1() throws Exception {
76         assertTrue(cli.eval("@[123"));
77     }
78
79 //    @Test
80 //    public void eval2() throws Exception {
81 //        assertFalse(cli.eval("as @[ 123"));
82 //    }
83
84     @Test
85     public void eval3() throws Exception {
86         try {
87             cli.eval("expect @[ 123");
88         } catch (Exception e) {
89             // TODO Auto-generated catch block
90             assertTrue(e instanceof CadiException);
91         }
92     }
93
94     public void eval31() throws Exception {
95         try {
96             cli.eval("expect 1 @[ 123");
97         } catch (Exception e) {
98             // TODO Auto-generated catch block
99             assertTrue(e instanceof CadiException);
100         }
101     }
102
103     @Test
104     public void eval4() throws Exception {
105         try {
106             cli.eval("sleep @[ 123");
107         } catch (Exception e) {
108             assertTrue(e instanceof NumberFormatException);
109         }
110     }
111
112     @Test
113     public void eval41() throws Exception {
114         assertTrue(cli.eval("sleep 1 @[ 123"));
115     }
116
117     @Test
118     public void eval5() throws Exception {
119         try {
120             cli.eval("delay @[ 123");
121         } catch (Exception e) {
122             assertTrue(e instanceof NumberFormatException);
123         }
124     }
125
126     @Test
127     public void eval51() throws Exception {
128         assertTrue(cli.eval("delay 1 @[ 123"));
129     }
130
131     @Test
132     public void eval7() throws Exception {
133         assertFalse(cli.eval("exit @[ 123"));
134     }
135
136     @Test
137     public void eval8() throws Exception {
138         assertTrue(cli.eval("REQUEST @[ 123"));
139     }
140
141     @Test
142     public void eval9() throws Exception {
143         assertTrue(cli.eval("FORCE @[ 123"));
144     }
145
146     @Test
147     public void eval10() throws Exception {
148         assertTrue(cli.eval("set @[ 123"));
149     }
150     
151     @Test
152     public void eval11() throws Exception {
153         assertTrue(cli.eval("DETAILS @[ 123"));
154     }
155     
156     @Test
157     public void eval12() throws Exception {
158         assertTrue(cli.eval(". |/, .\"0 \" "));
159     }
160
161     @Test
162     public void keyboardHelp() throws Exception {
163         boolean noError=true;
164         try {
165             cli.keyboardHelp();
166         } catch (Exception e) {
167             noError=false;
168         }
169         assertTrue(noError);
170     }
171
172     @Test
173     public void setProp() throws Exception {
174         boolean noError=true;
175         try {
176             cli.keyboardHelp();
177         } catch (Exception e) {
178             noError=false;
179         }
180         assertTrue(noError);
181     }
182     
183     @Test
184     public void eval_randomString() throws Exception {
185         assertTrue(cli.eval("Some random string @#&*& to check complete 100 coverage"));
186     }
187
188     public static AAFcli getAAfCli() throws APIException, LocatorException, GeneralSecurityException, IOException, CadiException {
189         final AuthzEnv env = new AuthzEnv(System.getProperties());
190         String aafUrl = "https://DME2RESOLVE";
191         SecurityInfoC<HttpURLConnection> si = mock(SecurityInfoC.class);
192         env.loadToSystemPropsStartsWith("AAF", "DME2");
193         Locator loc;
194         loc = new PropertyLocator(aafUrl);
195         TIMEOUT = Integer.parseInt(env.getProperty(Config.AAF_CONN_TIMEOUT, Config.AAF_CONN_TIMEOUT_DEF));
196         HMangr hman = new HMangr(env, loc).readTimeout(TIMEOUT).apiVersion(Config.AAF_DEFAULT_API_VERSION);
197
198         // TODO: Consider requiring a default in properties
199         env.setProperty(Config.AAF_DEFAULT_REALM,
200                 System.getProperty(Config.AAF_DEFAULT_REALM, Config.getDefaultRealm()));
201         
202         HBasicAuthSS ss = mock(HBasicAuthSS.class);
203         env.setProperty(Config.AAF_APPPASS, "test");
204         return new AAFcli(env, new OutputStreamWriter(System.out), hman, si, ss);
205     }
206     
207     @Test
208     public void testVerbose() {
209         cli.verbose(true);
210         cli.verbose(false);
211     }
212     
213     @Test
214     public void testClose() {
215         cli.close();
216     }
217     
218     @Test
219     public void testTimeout() {
220         Assert.assertNotNull(cli.timeout());
221     }
222     
223     @Test
224     public void testTest() {
225         Assert.assertNotNull(cli.isTest());
226     }
227     
228     @Test
229     public void testIsDetailed() {
230         Assert.assertNotNull(cli.isDetailed());
231     }
232     
233     @Test
234     public void testAddRequest() {
235         Assert.assertNotNull(cli.addRequest());
236     }
237     
238     @Test
239     public void testForceString() {
240         cli.clearSingleLineProperties();
241         Assert.assertNull(cli.forceString());
242     }
243     
244     @Test
245     public void testClearSingleLineProperties() {
246         cli.clearSingleLineProperties();
247     }
248     
249     @Test
250     public void testGui() {
251         cli.gui(true);
252         cli.gui(false);
253     }
254     
255     @Test
256     public void testMain() {
257         String[] strArr = {"\\*","test1"};
258         //cli.main(strArr);
259     }
260     
261 }