156e54b466c9afeadcf53f10f56553866572fee6
[aaf/authz.git] / cadi / shiro / src / test / java / org / onap / aaf / cadi / shiro / test / JU_AAFRealm.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.shiro.test;
22
23 import java.util.ArrayList;
24
25 import org.apache.shiro.authc.AuthenticationInfo;
26 import org.apache.shiro.authc.UsernamePasswordToken;
27 import org.apache.shiro.authz.AuthorizationInfo;
28 import org.apache.shiro.authz.Permission;
29 import org.apache.shiro.subject.PrincipalCollection;
30 import org.junit.Test;
31 import org.onap.aaf.cadi.aaf.AAFPermission;
32 import org.onap.aaf.cadi.config.Config;
33 import org.onap.aaf.cadi.shiro.AAFRealm;
34 import org.onap.aaf.cadi.shiro.AAFShiroPermission;
35
36 import junit.framework.Assert;
37
38 public class JU_AAFRealm {
39
40         @Test
41         public void test() {
42                 // NOTE This is a live test.  This JUnit needs to be built with "Mock"
43                 try {
44                         System.setProperty(Config.CADI_PROP_FILES, "/opt/app/osaaf/etc/org.osaaf.common.props");
45                         TestAAFRealm ar = new TestAAFRealm();
46                         
47                         UsernamePasswordToken upt = new UsernamePasswordToken("jonathan@people.osaaf.org", "new2You!");
48                         AuthenticationInfo ani = ar.authn(upt);
49                         
50                         AuthorizationInfo azi = ar.authz(ani.getPrincipals());
51                         // Change this to something YOU have, Sai...
52                         
53                         testAPerm(true,azi,"org.access","something","*");
54                         testAPerm(false,azi,"org.accessX","something","*");
55                 } catch (Throwable t) {
56                         t.printStackTrace();
57                         Assert.fail();
58                 }
59         }
60
61         private void testAPerm(boolean expect,AuthorizationInfo azi, String type, String instance, String action) {
62                 
63                 AAFShiroPermission testPerm = new AAFShiroPermission(new AAFPermission(type,instance,action,new ArrayList<String>()));
64
65                 boolean any = false;
66                 for(Permission p : azi.getObjectPermissions()) {
67                         if(p.implies(testPerm)) {
68                                 any = true;
69                         }
70                 }
71                 if(expect) {
72                         Assert.assertTrue(any);
73                 } else {
74                         Assert.assertFalse(any);
75                 }
76
77                 
78         }
79
80         /**
81          * Note, have to create a derived class, because "doGet"... are protected
82          */
83         private class TestAAFRealm extends AAFRealm {
84                 public AuthenticationInfo authn(UsernamePasswordToken upt) {
85                         return doGetAuthenticationInfo(upt);
86                 }
87                 public AuthorizationInfo authz(PrincipalCollection pc) {
88                         return doGetAuthorizationInfo(pc);
89                 }
90                 
91         }
92 }