Remove Tabs, per Jococo
[aaf/authz.git] / cadi / aaf / src / test / java / org / onap / aaf / cadi / aaf / v2_0 / JU_AAFAuthn.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.cadi.aaf.v2_0;
22
23 import static org.junit.Assert.assertNull;
24 import static org.junit.Assert.assertTrue;
25 import static org.mockito.MockitoAnnotations.initMocks;
26
27 import java.io.IOException;
28
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.mockito.Mock;
32 import org.mockito.Mockito;
33 import org.onap.aaf.cadi.AbsUserCache;
34 import org.onap.aaf.cadi.PropAccess;
35 import org.onap.aaf.cadi.User;
36 import org.onap.aaf.cadi.aaf.AAFPermission;
37 import org.onap.aaf.cadi.client.Future;
38 import org.onap.aaf.cadi.client.Rcli;
39 import org.onap.aaf.cadi.principal.BasicPrincipal;
40
41 public class JU_AAFAuthn {
42
43     @Mock
44     AAFCon con;
45     
46     @Mock
47     AbsUserCache<AAFPermission> cache;
48     
49     @Mock
50     PropAccess propaccess;
51     
52     @Before
53     public void setUp() {
54         initMocks(this);
55     }
56
57     @Test
58     public void testGetRealm() {
59         AAFAuthn authnObj = new AAFAuthn(con);
60         String realm = authnObj.getRealm();
61         assertNull(realm);
62     }
63     
64     @Test
65     public void testValidateFailure() {
66         AAFAuthnImplWithGetUserNull authnObj = new AAFAuthnImplWithGetUserNull(con, cache);
67         String realm="";
68         try {
69             Mockito.doReturn("test").when(propaccess).decrypt("test", false);
70             realm = authnObj.validate("test", "test");
71             assertNull(realm);
72         } catch (Exception e) {
73             // TODO Auto-generated catch block
74             assertNull( e.getLocalizedMessage());
75         }
76     }
77     
78     @Test
79     public void testValidate() {
80         AAFAuthnImpl authnObj = new AAFAuthnImpl(con);
81         String realm="";
82         try {
83             Mockito.doReturn("test").when(propaccess).decrypt("test", false);
84             Rcli rcliObj = Mockito.mock(Rcli.class);
85             Mockito.doReturn(rcliObj).when(con).client();
86             Mockito.doReturn(rcliObj).when(rcliObj).forUser(null);
87             Future<String> futureObj = Mockito.mock(Future.class);
88             Mockito.doReturn(futureObj).when(rcliObj).read( "/authn/basicAuth","text/plain");
89             realm = authnObj.validate("test", "test","test");
90             assertTrue(realm.contains("user/pass combo invalid"));
91         } catch (Exception e) {
92             // TODO Auto-generated catch block
93             e.printStackTrace();
94         }
95     }
96     
97     @Test
98     public void testValidateRevalidate() {
99         AAFAuthnImpl authnObj = new AAFAuthnImpl(con);
100         String realm="";
101         try {
102             Mockito.doReturn("test").when(propaccess).decrypt("test", false);
103             Rcli rcliObj = Mockito.mock(Rcli.class);
104             Mockito.doReturn(rcliObj).when(con).client();
105             Mockito.doReturn(rcliObj).when(rcliObj).forUser(null);
106             Future<String> futureObj = Mockito.mock(Future.class);
107             Mockito.doReturn(futureObj).when(rcliObj).read( "/authn/basicAuth","text/plain");
108             Mockito.doReturn(true).when(futureObj).get( 0);
109             realm = authnObj.validate("test", "test","test");
110             assertNull(realm);
111         } catch (Exception e) {
112             // TODO Auto-generated catch block
113             e.printStackTrace();
114         }
115     }
116     
117     @Test
118     public void testValidateValidUser() {
119         AAFAuthnImplWithGetUser authnObj = new AAFAuthnImplWithGetUser(con);
120         String realm="";
121         try {
122             Mockito.doReturn("test").when(propaccess).decrypt("test", false);
123             realm = authnObj.validate("test", "test","test");
124             assertTrue(realm.contains("User already denied"));
125         } catch (Exception e) {
126             // TODO Auto-generated catch block
127             e.printStackTrace();
128         }
129     }
130     
131     @Test
132     public void testValidateValidUserNull() {
133         AAFAuthnImplWithGetUserNull authnObj = new AAFAuthnImplWithGetUserNull(con);
134         String realm="";
135         try {
136             Mockito.doReturn("test").when(propaccess).decrypt("test", false);
137             realm = authnObj.validate("test", "test","test");
138             assertNull(realm);
139         } catch (Exception e) {
140             // TODO Auto-generated catch block
141             e.printStackTrace();
142         }
143     }
144     class AAFAuthnImpl extends AAFAuthn{
145         AAFAuthnImpl(AAFCon con) {
146             super(con);
147             this.access = propaccess;
148             // TODO Auto-generated constructor stub
149         }
150         
151         AAFAuthnImpl(AAFCon con, AbsUserCache cache) {
152             super(con, cache);
153             this.access = propaccess;
154             // TODO Auto-generated constructor stub
155         }
156         
157         
158     }
159     
160     class AAFAuthnImplWithGetUser extends AAFAuthn{
161         AAFAuthnImplWithGetUser(AAFCon con) {
162             super(con);
163             this.access = propaccess;
164             // TODO Auto-generated constructor stub
165         }
166         
167         AAFAuthnImplWithGetUser(AAFCon con, AbsUserCache cache) {
168             super(con, cache);
169             this.access = propaccess;
170             // TODO Auto-generated constructor stub
171         }
172         
173         @Override
174         protected User getUser(String user, byte[] cred) {
175             return new User<>("test",new byte[] {});
176         }
177     }
178     
179     class AAFAuthnImplWithGetUserNull extends AAFAuthn{
180         AAFAuthnImplWithGetUserNull(AAFCon con) {
181             super(con);
182             this.access = propaccess;
183             // TODO Auto-generated constructor stub
184         }
185         
186         AAFAuthnImplWithGetUserNull(AAFCon con, AbsUserCache cache) {
187             super(con, cache);
188             this.access = propaccess;
189             // TODO Auto-generated constructor stub
190         }
191         
192         @Override
193         protected User getUser(String user, byte[] cred) {
194             User user1 = null;
195             try {
196                 user1 = new User(new BasicPrincipal("test","test"));
197             } catch (IOException e) {
198                 // TODO Auto-generated catch block
199                 e.printStackTrace();
200             }
201             return user1;
202         }
203     }
204 }