f4481ed499081ccfbf3ce0a2f9a554cd69e768c7
[aaf/authz.git] / auth / auth-core / src / test / java / org / onap / aaf / auth / env / test / JU_AuthzTransImpl.java
1 /*******************************************************************************
2  * ============LICENSE_START====================================================
3  * * org.onap.aaf
4  * * ===========================================================================
5  * * Copyright © 2017 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  ******************************************************************************/
22
23 package org.onap.aaf.auth.env.test;
24
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.when;
27
28 import java.security.Principal;
29 import java.util.Date;
30
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
33
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.Mock;
38 import org.onap.aaf.auth.env.AuthzEnv;
39 import org.onap.aaf.auth.env.AuthzTransImpl;
40 import org.onap.aaf.auth.env.AuthzTrans.REQD_TYPE;
41 import org.onap.aaf.auth.org.Organization;
42 import org.onap.aaf.auth.org.OrganizationFactory;
43 import org.onap.aaf.cadi.Lur;
44 import org.onap.aaf.cadi.Permission;
45 import org.onap.aaf.misc.env.Env;
46 import org.onap.aaf.misc.env.LogTarget;
47 import org.powermock.modules.junit4.PowerMockRunner;
48
49 import junit.framework.Assert;
50
51 @RunWith(PowerMockRunner.class)
52 public class JU_AuthzTransImpl {
53
54     AuthzTransImpl authzTransImpl;
55     @Mock
56     AuthzEnv authzEnvMock;
57     AuthzTransImpl trans1;
58     
59     private Organization org=null;
60     private AuthzTransImpl mockAuthzTransImpl;
61     private static HttpServletRequest req;
62     private static HttpServletResponse res;
63     private Lur lur1 = mock(Lur.class);
64     
65     @Before
66     public void setUp(){
67         authzTransImpl = new AuthzTransImpl(authzEnvMock);
68         req = mock(HttpServletRequest.class);
69         authzTransImpl.set(req);
70         when(req.getParameter("request")).thenReturn("NotNull");
71         authzTransImpl.set(req);
72         when(req.getParameter("request")).thenReturn("");
73         authzTransImpl.set(req);    
74     }
75     
76     @Test
77     public void testOrg() {
78         Organization result=null;
79         result = authzTransImpl.org();
80         OrganizationFactory test = mock(OrganizationFactory.class);
81         //result = OrganizationFactory.obtain(authzTransImpl.env(), authzTransImpl.user());
82         authzTransImpl.org();
83         //when(test).thenReturn(null);
84         //assertTrue(true);    
85     }
86     
87     @Mock
88     LogTarget logTargetMock;
89     
90     @Test
91     public void testLogAuditTrail(){
92         
93         when(logTargetMock.isLoggable()).thenReturn(false);
94         authzTransImpl.logAuditTrail(logTargetMock);
95         when(logTargetMock.isLoggable()).thenReturn(true);
96         Env delegate = mock(Env.class);
97         //when(logTargetMock.isLoggable()).thenReturn(true);//TODO: Figure this out
98         //authzTransImpl.logAuditTrail(logTargetMock);
99     }
100     
101 //    @Test                            //TODO:Fix this AAF-111
102 //    public void testSetUser() {
103 //        Principal user = mock(Principal.class);
104 //        authzTransImpl.setUser(user);
105 //        Principal user1 = authzTransImpl.getUserPrincipal();
106 //        String username = user1.getName();
107 //        Assert.assertNotNull(user1);
108 //    }
109     
110 //    @Test                            //TODO:Fix this AAF-111
111 //    public void testUser() {
112 //        Assert.assertEquals("n/a", authzTransImpl.user());
113 //        Principal user = mock(Principal.class); //Unsure how to modify name
114 //        when(user.toString()).thenReturn("name");
115 //        when(user.getName()).thenReturn("name");
116 //        authzTransImpl.setUser(user);
117 //        Assert.assertEquals("name", authzTransImpl.user());
118 //    }
119 //    
120     @Test
121     public void testRequested() {
122         REQD_TYPE user = REQD_TYPE.move;
123         REQD_TYPE user1 = REQD_TYPE.future;
124         HttpServletRequest req = mock(HttpServletRequest.class);
125         String p = user1.name();
126         boolean boolUser = authzTransImpl.requested(user);
127         Assert.assertEquals(false, boolUser);
128         Assert.assertNotNull(p);
129         authzTransImpl.requested(user,true);
130         when(authzTransImpl.requested(user)).thenReturn(null);
131         Assert.assertEquals(true, authzTransImpl.requested(user));
132     /*    String p1 = req.getParameter(user1.name());  //unable to access private method call in all instances
133         when(req.getParameter(user1.name())).thenReturn("test");
134         authzTransImpl.requested(user,false);
135         */
136         
137         
138     }
139     
140     @Test
141     public void testFish() {
142         mockAuthzTransImpl = mock(AuthzTransImpl.class);
143         Permission p = mock(Permission.class);
144         authzTransImpl.fish(p);
145         String str = "Test";
146         lur1.createPerm(str);
147         when(p.match(p)).thenReturn(true);
148         authzTransImpl.setLur(lur1);
149         authzTransImpl.fish(p);
150     }
151     
152     @Test
153     public void testSetVariables() { //TODO: refactor this better
154         Assert.assertNull(authzTransImpl.agent());
155         Assert.assertNull(authzTransImpl.ip());
156         Assert.assertNull(authzTransImpl.path());
157         Assert.assertNotNull(authzTransImpl.port());
158         Assert.assertNull(authzTransImpl.meth());
159         Assert.assertNull(authzTransImpl.getUserPrincipal());
160         Assert.assertNotNull(authzTransImpl.user());
161     }
162     
163     @Test
164     public void testNow() {
165         Date date = authzTransImpl.now();
166         Assert.assertEquals(date,authzTransImpl.now());
167         when(authzTransImpl.now()).thenReturn(null);
168     }
169     
170 }