Sonar Fixes, Formatting
[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         res = mock(HttpServletResponse.class);
70         authzTransImpl.set(req,res);
71         when(req.getParameter("request")).thenReturn("NotNull");
72         authzTransImpl.set(req,res);
73         when(req.getParameter("request")).thenReturn("");
74         authzTransImpl.set(req,res);
75     }
76
77     @Test
78     public void testOrg() {
79         Organization result=null;
80         result = authzTransImpl.org();
81         OrganizationFactory test = mock(OrganizationFactory.class);
82         //result = OrganizationFactory.obtain(authzTransImpl.env(), authzTransImpl.user());
83         authzTransImpl.org();
84         //when(test).thenReturn(null);
85         //assertTrue(true);
86     }
87
88     @Mock
89     LogTarget logTargetMock;
90
91     @Test
92     public void testLogAuditTrail(){
93
94         when(logTargetMock.isLoggable()).thenReturn(false);
95         authzTransImpl.logAuditTrail(logTargetMock);
96         when(logTargetMock.isLoggable()).thenReturn(true);
97         Env delegate = mock(Env.class);
98         //when(logTargetMock.isLoggable()).thenReturn(true);//TODO: Figure this out
99         //authzTransImpl.logAuditTrail(logTargetMock);
100     }
101
102 //    @Test                            //TODO:Fix this AAF-111
103 //    public void testSetUser() {
104 //        Principal user = mock(Principal.class);
105 //        authzTransImpl.setUser(user);
106 //        Principal user1 = authzTransImpl.getUserPrincipal();
107 //        String username = user1.getName();
108 //        Assert.assertNotNull(user1);
109 //    }
110
111 //    @Test                            //TODO:Fix this AAF-111
112 //    public void testUser() {
113 //        Assert.assertEquals("n/a", authzTransImpl.user());
114 //        Principal user = mock(Principal.class); //Unsure how to modify name
115 //        when(user.toString()).thenReturn("name");
116 //        when(user.getName()).thenReturn("name");
117 //        authzTransImpl.setUser(user);
118 //        Assert.assertEquals("name", authzTransImpl.user());
119 //    }
120 //
121     @Test
122     public void testRequested() {
123         REQD_TYPE user = REQD_TYPE.move;
124         REQD_TYPE user1 = REQD_TYPE.future;
125         HttpServletRequest req = mock(HttpServletRequest.class);
126         String p = user1.name();
127         boolean boolUser = authzTransImpl.requested(user);
128         Assert.assertEquals(false, boolUser);
129         Assert.assertNotNull(p);
130         authzTransImpl.requested(user,true);
131         when(authzTransImpl.requested(user)).thenReturn(null);
132         Assert.assertEquals(true, authzTransImpl.requested(user));
133     /*    String p1 = req.getParameter(user1.name());  //unable to access private method call in all instances
134         when(req.getParameter(user1.name())).thenReturn("test");
135         authzTransImpl.requested(user,false);
136         */
137
138
139     }
140
141     @Test
142     public void testFish() {
143         mockAuthzTransImpl = mock(AuthzTransImpl.class);
144         Permission p = mock(Permission.class);
145         authzTransImpl.fish(p);
146         String str = "Test";
147         lur1.createPerm(str);
148         when(p.match(p)).thenReturn(true);
149         authzTransImpl.setLur(lur1);
150         authzTransImpl.fish(p);
151     }
152
153     @Test
154     public void testSetVariables() { //TODO: refactor this better
155         Assert.assertNull(authzTransImpl.agent());
156         Assert.assertNull(authzTransImpl.ip());
157         Assert.assertNull(authzTransImpl.path());
158         Assert.assertNotNull(authzTransImpl.port());
159         Assert.assertNull(authzTransImpl.meth());
160         Assert.assertNull(authzTransImpl.getUserPrincipal());
161         Assert.assertNotNull(authzTransImpl.user());
162     }
163
164     @Test
165     public void testNow() {
166         Date date = authzTransImpl.now();
167         Assert.assertEquals(date,authzTransImpl.now());
168         when(authzTransImpl.now()).thenReturn(null);
169     }
170
171 }