1 /*******************************************************************************
2 * ============LICENSE_START====================================================
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
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 ******************************************************************************/
23 package org.onap.aaf.auth.env.test;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.when;
28 import java.security.Principal;
29 import java.util.Date;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
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;
49 import junit.framework.Assert;
51 @RunWith(PowerMockRunner.class)
52 public class JU_AuthzTransImpl {
54 AuthzTransImpl authzTransImpl;
56 AuthzEnv authzEnvMock;
57 AuthzTransImpl trans1;
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);
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);
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());
83 //when(test).thenReturn(null);
88 LogTarget logTargetMock;
91 public void testLogAuditTrail(){
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);
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);
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());
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);
141 public void testFish() {
142 mockAuthzTransImpl = mock(AuthzTransImpl.class);
143 Permission p = mock(Permission.class);
144 authzTransImpl.fish(p);
146 lur1.createPerm(str);
147 when(p.match(p)).thenReturn(true);
148 authzTransImpl.setLur(lur1);
149 authzTransImpl.fish(p);
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());
164 public void testNow() {
165 Date date = authzTransImpl.now();
166 Assert.assertEquals(date,authzTransImpl.now());
167 when(authzTransImpl.now()).thenReturn(null);