Changed to unmaintained
[aaf/cadi.git] / 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.Assert;
31 import org.junit.Test;
32 import org.onap.aaf.cadi.aaf.AAFPermission;
33 import org.onap.aaf.cadi.config.Config;
34 import org.onap.aaf.cadi.shiro.AAFRealm;
35 import org.onap.aaf.cadi.shiro.AAFShiroPermission;
36
37
38
39 public class JU_AAFRealm {
40
41 //@Test
42  public void test() {
43         // NOTE This is a live test.  This JUnit needs to be built with "Mock" before it can be 
44         // an official JUNIT
45         
46         try {
47                 System.setProperty(Config.CADI_PROP_FILES, "/opt/app/osaaf/local/org.onap.aai.props");
48                 TestAAFRealm ar = new TestAAFRealm();
49                 
50                 //UsernamePasswordToken upt = new UsernamePasswordToken("demo@people.osaaf.org", "demo123456!");
51                 /// UsernamePasswordToken upt = new UsernamePasswordToken("AAI", "AAI");
52                 UsernamePasswordToken upt = new UsernamePasswordToken("admin","Kp8bJ4SXszM0WXlhak3eHlcse2gAw84vaoGGmJvUy2U");
53                 
54                 AuthenticationInfo ani = ar.authn(upt);
55                 
56                 AuthorizationInfo azi = ar.authz(ani.getPrincipals());
57                 // Change this to something YOU have, Sai...
58                 
59                 testAPerm(true,azi,"org.onap.aai","resources","something","get");
60                 testAPerm(false,azi,"org.osaaf.nons","resources","something","get");
61  //             testAPerm(true,azi,"name","org.access","something","*");
62  //             testAPerm(false,azi,"org.accessX","something","*");
63                 
64                 Assert.assertEquals(true,ar.supports(upt));
65         } catch (Throwable t) {
66                 t.printStackTrace();
67                 Assert.fail();
68         }
69  }
70
71         private void testAPerm(boolean expect, AuthorizationInfo azi, String ns, String type, String instance, String action) {
72                 
73                 AAFShiroPermission testPerm = new AAFShiroPermission(new AAFPermission(ns,type,instance,action,new ArrayList<String>()));
74
75                 boolean any = false;
76                 for(Permission p : azi.getObjectPermissions()) {
77                         if(p.implies(testPerm)) {
78                                 any = true;
79                         }
80                 }
81                 if(expect) {
82                         Assert.assertTrue(any);
83                 } else {
84                         Assert.assertFalse(any);
85                 }
86
87                 
88         }
89
90         /**
91          * Note, have to create a derived class, because "doGet"... are protected
92          */
93         private class TestAAFRealm extends AAFRealm {
94                 public AuthenticationInfo authn(UsernamePasswordToken upt) {
95                         return doGetAuthenticationInfo(upt);
96                 }
97                 public AuthorizationInfo authz(PrincipalCollection pc) {
98                         return doGetAuthorizationInfo(pc);
99                 }
100                 
101         }
102 }