2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
21 package org.onap.aaf.cadi.aaf.v2_0;
23 import static org.junit.Assert.assertNull;
24 import static org.junit.Assert.assertTrue;
25 import static org.mockito.MockitoAnnotations.initMocks;
27 import java.io.IOException;
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;
41 public class JU_AAFAuthn {
47 AbsUserCache<AAFPermission> cache;
50 PropAccess propaccess;
58 public void testGetRealm() {
59 AAFAuthn authnObj = new AAFAuthn(con);
60 String realm = authnObj.getRealm();
65 public void testValidateFailure() {
66 AAFAuthnImplWithGetUserNull authnObj = new AAFAuthnImplWithGetUserNull(con, cache);
69 Mockito.doReturn("test").when(propaccess).decrypt("test", false);
70 realm = authnObj.validate("test", "test");
72 } catch (Exception e) {
73 // TODO Auto-generated catch block
74 assertNull( e.getLocalizedMessage());
79 public void testValidate() {
80 AAFAuthnImpl authnObj = new AAFAuthnImpl(con);
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
98 public void testValidateRevalidate() {
99 AAFAuthnImpl authnObj = new AAFAuthnImpl(con);
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");
111 } catch (Exception e) {
112 // TODO Auto-generated catch block
118 public void testValidateValidUser() {
119 AAFAuthnImplWithGetUser authnObj = new AAFAuthnImplWithGetUser(con);
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
132 public void testValidateValidUserNull() {
133 AAFAuthnImplWithGetUserNull authnObj = new AAFAuthnImplWithGetUserNull(con);
136 Mockito.doReturn("test").when(propaccess).decrypt("test", false);
137 realm = authnObj.validate("test", "test","test");
139 } catch (Exception e) {
140 // TODO Auto-generated catch block
144 class AAFAuthnImpl extends AAFAuthn{
145 AAFAuthnImpl(AAFCon con) {
147 this.access = propaccess;
148 // TODO Auto-generated constructor stub
151 AAFAuthnImpl(AAFCon con, AbsUserCache cache) {
153 this.access = propaccess;
154 // TODO Auto-generated constructor stub
160 class AAFAuthnImplWithGetUser extends AAFAuthn{
161 AAFAuthnImplWithGetUser(AAFCon con) {
163 this.access = propaccess;
164 // TODO Auto-generated constructor stub
167 AAFAuthnImplWithGetUser(AAFCon con, AbsUserCache cache) {
169 this.access = propaccess;
170 // TODO Auto-generated constructor stub
174 protected User getUser(String user, byte[] cred) {
175 return new User<>("test",new byte[] {});
179 class AAFAuthnImplWithGetUserNull extends AAFAuthn{
180 AAFAuthnImplWithGetUserNull(AAFCon con) {
182 this.access = propaccess;
183 // TODO Auto-generated constructor stub
186 AAFAuthnImplWithGetUserNull(AAFCon con, AbsUserCache cache) {
188 this.access = propaccess;
189 // TODO Auto-generated constructor stub
193 protected User getUser(String user, byte[] cred) {
196 user1 = new User(new BasicPrincipal("test","test"));
197 } catch (IOException e) {
198 // TODO Auto-generated catch block