ca0a8917a3c109910ff3b992304785ea258a0306
[aaf/authz.git] / auth / auth-cass / src / test / java / org / onap / aaf / auth / direct / test / JU_DirectAAFUserPass.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 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  */
21 package org.onap.aaf.auth.direct.test;
22
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertTrue;
25 import static org.mockito.Mockito.when;
26 import static org.mockito.MockitoAnnotations.initMocks;
27
28 import java.util.Date;
29
30 import javax.servlet.http.HttpServletRequest;
31
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.Mock;
36 import org.mockito.Mockito;
37 import org.mockito.runners.MockitoJUnitRunner;
38 import org.onap.aaf.auth.dao.DAOException;
39 import org.onap.aaf.auth.dao.hl.Question;
40 import org.onap.aaf.auth.direct.DirectAAFUserPass;
41 import org.onap.aaf.auth.env.AuthzEnv;
42 import org.onap.aaf.auth.env.AuthzTrans;
43 import org.onap.aaf.auth.layer.Result;
44 import org.onap.aaf.misc.env.LogTarget;
45
46
47 @RunWith(MockitoJUnitRunner.class) 
48 public class JU_DirectAAFUserPass {
49
50         @Mock
51         Question question;
52         
53         @Mock
54         AuthzEnv env;
55         
56         @Mock
57         AuthzTrans trans;
58         
59         @Mock
60         HttpServletRequest request;
61         
62         
63         @Before
64         public void setUp() throws Exception {
65                 initMocks(this);
66                 when(env.warn()).thenReturn(new LogTarget() {
67             
68             @Override
69             public void printf(String fmt, Object... vars) {}
70             
71             @Override
72             public void log(Throwable e, Object... msgs) {
73                 e.getMessage();
74                 e.printStackTrace();
75                 msgs.toString();
76                 
77             }
78             
79             @Override
80             public void log(Object... msgs) {
81             }
82             
83             @Override
84             public boolean isLoggable() {
85                 
86                 return true;
87             }
88         });
89                 when(env.error()).thenReturn(new LogTarget() {
90             
91             @Override
92             public void printf(String fmt, Object... vars) {}
93             
94             @Override
95             public void log(Throwable e, Object... msgs) {
96                 e.getMessage();
97                 e.printStackTrace();
98                 msgs.toString();
99                 
100             }
101             
102             @Override
103             public void log(Object... msgs) {
104             }
105             
106             @Override
107             public boolean isLoggable() {
108                 
109                 return true;
110             }
111         });
112         }
113         
114         @Test
115         public void testUserPass() {
116                 
117                 DirectAAFUserPass aafLocatorObj=null;
118                 aafLocatorObj = new DirectAAFUserPass(env, question);
119                 Result<Date> retVal1 = new Result<Date>(null,0,"",new String[0]);
120                 Mockito.doReturn(trans).when(env).newTransNoAvg();
121                 try {
122                         Mockito.doReturn(retVal1).when(question).doesUserCredMatch(trans, null, null);
123                 } catch (DAOException e) {
124                         e.printStackTrace();
125                 }
126                 boolean retVal = aafLocatorObj.validate(null, null, null, null);
127                 
128                 assertTrue(retVal);
129         }       
130         
131         @Test
132         public void testUserPassStateisRequest() {
133                 
134                 DirectAAFUserPass aafLocatorObj=null;
135                 aafLocatorObj = new DirectAAFUserPass(env, question);
136                 Result<Date> retVal1 = new Result<Date>(null,1,"",new String[0]);
137                 Mockito.doReturn(trans).when(env).newTransNoAvg();
138                 try {
139                         Mockito.doReturn(retVal1).when(question).doesUserCredMatch(trans, null, null);
140                 } catch (DAOException e) {
141                         e.printStackTrace();
142                 }
143                 boolean retVal = aafLocatorObj.validate(null, null, null, request);
144                 
145 //              System.out.println(retVal);
146                 assertFalse(retVal);
147         }
148         
149         @Test
150         public void testUserPassStateNotNull() {
151                 
152                 DirectAAFUserPass aafLocatorObj=null;
153                 aafLocatorObj = new DirectAAFUserPass(env, question);
154                 Result<Date> retVal1 = new Result<Date>(null,1,"",new String[0]);
155                 Mockito.doReturn(trans).when(env).newTransNoAvg();
156                 try {
157                         Mockito.doReturn(retVal1).when(question).doesUserCredMatch(trans, null, null);
158                 } catch (DAOException e) {
159                         e.printStackTrace();
160                 }
161                 boolean retVal = aafLocatorObj.validate(null, null, null, "test");
162                 
163 //              System.out.println(retVal);
164                 assertFalse(retVal);
165         }
166         
167         @Test
168         public void testUserPassTransChk() {
169                 
170                 DirectAAFUserPass aafLocatorObj=null;
171                 aafLocatorObj = new DirectAAFUserPass(env, question);
172                 Result<Date> retVal1 = new Result<Date>(null,1,"",new String[0]);
173                 Mockito.doReturn(trans).when(env).newTransNoAvg();
174                 try {
175                         Mockito.doReturn(retVal1).when(question).doesUserCredMatch(trans, null, null);
176                 } catch (DAOException e) {
177                         e.printStackTrace();
178                 }
179                 boolean retVal = aafLocatorObj.validate(null, null, null, trans);
180                 
181 //              System.out.println(retVal);
182                 assertFalse(retVal);
183         }
184         
185         @Test
186         public void testUserPassTransIpNotNull() {
187                 
188                 DirectAAFUserPass aafLocatorObj=null;
189                 aafLocatorObj = new DirectAAFUserPass(env, question);
190                 Result<Date> retVal1 = new Result<Date>(null,1,"",new String[0]);
191                 Mockito.doReturn("test").when(trans).ip();
192                 Mockito.doReturn(trans).when(env).newTransNoAvg();
193                 try {
194                         Mockito.doReturn(retVal1).when(question).doesUserCredMatch(trans, null, null);
195                 } catch (DAOException e) {
196                         e.printStackTrace();
197                 }
198                 boolean retVal = aafLocatorObj.validate(null, null, null, trans);
199                 
200 //              System.out.println(retVal);
201                 assertFalse(retVal);
202         }
203         
204         @Test
205         public void testUserExceptionChk() {
206                 
207                 DirectAAFUserPass aafLocatorObj=null;
208                 aafLocatorObj = new DirectAAFUserPass(env, question);
209                 Result<Date> retVal1 = new Result<Date>(null,1,"",new String[0]);
210                 Mockito.doReturn(trans).when(env).newTransNoAvg();
211                 try {
212                         Mockito.doThrow(DAOException.class).when(question).doesUserCredMatch(trans, null, null);
213                 } catch (DAOException e) {
214                         // TODO Auto-generated catch block
215 //                      e.printStackTrace();
216                 }
217                 boolean retVal = aafLocatorObj.validate(null, null, null, trans);
218                 
219 //              System.out.println(retVal);
220                 assertFalse(retVal);
221         }
222         
223 }