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 ******************************************************************************/
22 package org.onap.aaf.auth.env.test;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.when;
27 import java.security.Principal;
28 import java.util.Date;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.Mock;
37 import org.onap.aaf.auth.env.AuthzEnv;
38 import org.onap.aaf.auth.env.AuthzTransImpl;
39 import org.onap.aaf.auth.env.AuthzTrans.REQD_TYPE;
40 import org.onap.aaf.auth.org.Organization;
41 import org.onap.aaf.auth.org.OrganizationFactory;
42 import org.onap.aaf.cadi.Lur;
43 import org.onap.aaf.cadi.Permission;
44 import org.onap.aaf.misc.env.Env;
45 import org.onap.aaf.misc.env.LogTarget;
46 import org.powermock.modules.junit4.PowerMockRunner;
48 import junit.framework.Assert;
50 @RunWith(PowerMockRunner.class)
51 public class JU_AuthzTransImpl {
53 AuthzTransImpl authzTransImpl;
55 AuthzEnv authzEnvMock;
56 AuthzTransImpl trans1;
58 private Organization org=null;
59 private AuthzTransImpl mockAuthzTransImpl;
60 private static HttpServletRequest req;
61 private static HttpServletResponse res;
62 private Lur lur1 = mock(Lur.class);
66 authzTransImpl = new AuthzTransImpl(authzEnvMock);
67 req = mock(HttpServletRequest.class);
68 authzTransImpl.set(req);
69 when(req.getParameter("request")).thenReturn("NotNull");
70 authzTransImpl.set(req);
71 when(req.getParameter("request")).thenReturn("");
72 authzTransImpl.set(req);
76 public void testOrg() {
77 Organization result=null;
78 result = authzTransImpl.org();
79 OrganizationFactory test = mock(OrganizationFactory.class);
80 //result = OrganizationFactory.obtain(authzTransImpl.env(), authzTransImpl.user());
82 //when(test).thenReturn(null);
87 LogTarget logTargetMock;
90 public void testLogAuditTrail(){
92 when(logTargetMock.isLoggable()).thenReturn(false);
93 authzTransImpl.logAuditTrail(logTargetMock);
94 when(logTargetMock.isLoggable()).thenReturn(true);
95 Env delegate = mock(Env.class);
96 //when(logTargetMock.isLoggable()).thenReturn(true);//TODO: Figure this out
97 //authzTransImpl.logAuditTrail(logTargetMock);
100 // @Test //TODO:Fix this AAF-111
101 // public void testSetUser() {
102 // Principal user = mock(Principal.class);
103 // authzTransImpl.setUser(user);
104 // Principal user1 = authzTransImpl.getUserPrincipal();
105 // String username = user1.getName();
106 // Assert.assertNotNull(user1);
109 // @Test //TODO:Fix this AAF-111
110 // public void testUser() {
111 // Assert.assertEquals("n/a", authzTransImpl.user());
112 // Principal user = mock(Principal.class); //Unsure how to modify name
113 // when(user.toString()).thenReturn("name");
114 // when(user.getName()).thenReturn("name");
115 // authzTransImpl.setUser(user);
116 // Assert.assertEquals("name", authzTransImpl.user());
120 public void testRequested() {
121 REQD_TYPE user = REQD_TYPE.move;
122 REQD_TYPE user1 = REQD_TYPE.future;
123 HttpServletRequest req = mock(HttpServletRequest.class);
124 String p = user1.name();
125 boolean boolUser = authzTransImpl.requested(user);
126 Assert.assertEquals(false, boolUser);
127 Assert.assertNotNull(p);
128 authzTransImpl.requested(user,true);
129 when(authzTransImpl.requested(user)).thenReturn(null);
130 Assert.assertEquals(true, authzTransImpl.requested(user));
131 /* String p1 = req.getParameter(user1.name()); //unable to access private method call in all instances
132 when(req.getParameter(user1.name())).thenReturn("test");
133 authzTransImpl.requested(user,false);
140 public void testFish() {
141 mockAuthzTransImpl = mock(AuthzTransImpl.class);
142 Permission p = mock(Permission.class);
143 authzTransImpl.fish(p);
145 lur1.createPerm(str);
146 when(p.match(p)).thenReturn(true);
147 authzTransImpl.setLur(lur1);
148 authzTransImpl.fish(p);
152 public void testSetVariables() { //TODO: refactor this better
153 Assert.assertNull(authzTransImpl.agent());
154 Assert.assertNull(authzTransImpl.ip());
155 Assert.assertNull(authzTransImpl.path());
156 Assert.assertNotNull(authzTransImpl.port());
157 Assert.assertNull(authzTransImpl.meth());
158 Assert.assertNull(authzTransImpl.getUserPrincipal());
159 Assert.assertNotNull(authzTransImpl.user());
163 public void testNow() {
164 Date date = new Date();
165 Assert.assertEquals(date,authzTransImpl.now());
166 when(authzTransImpl.now()).thenReturn(null);