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.junit.Assert.*;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.mockito.Mock;
30 import static org.mockito.Mockito.*;
31 import javax.servlet.http.HttpServletRequest;
32 import java.lang.reflect.InvocationTargetException;
33 import java.lang.reflect.Method;
35 import javax.servlet.ServletRequest;
37 import org.onap.aaf.cadi.principal.TaggedPrincipal;
38 import org.onap.aaf.misc.env.Env;
39 import org.mockito.runners.MockitoJUnitRunner;
40 import org.onap.aaf.auth.env.AuthzEnv;
41 import org.onap.aaf.auth.env.AuthzTrans;
42 import org.onap.aaf.auth.env.AuthzTransFilter;
43 import org.onap.aaf.auth.env.AuthzTransOnlyFilter;
44 import org.onap.aaf.cadi.CadiException;
45 import org.onap.aaf.cadi.Connector;
46 import org.onap.aaf.cadi.TrustChecker;
47 import org.onap.aaf.misc.env.LogTarget;
48 import org.onap.aaf.misc.env.Slot;
49 import org.onap.aaf.misc.env.Trans;
50 import org.onap.aaf.misc.env.Trans.Metric;
52 @RunWith(MockitoJUnitRunner.class)
53 public class JU_AuthzTransOnlyFilter {
54 AuthzTransFilter authzTransFilter;
55 AuthzEnv authzEnvMock = mock(AuthzEnv.class);
56 Connector connectorMock = mock(Connector.class);
57 TrustChecker trustCheckerMock = mock(TrustChecker.class);
58 AuthzTrans authzTransMock = mock(AuthzTrans.class);
59 Object additionalTafLurs = mock(Object.class);
60 ServletRequest servletRequestMock = mock(ServletRequest.class);
61 AuthzTransOnlyFilter authzTransOnlyFilter;
65 authzTransOnlyFilter = new AuthzTransOnlyFilter(authzEnvMock);
69 public void testProtected() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
70 Method newTransMethod = AuthzTransFilter.class.getDeclaredMethod("newTrans");
71 newTransMethod.setAccessible(true);
73 newTransMethod.invoke(authzTransFilter);
77 public void testStart() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
78 AuthzTransOnlyFilter aTF = new AuthzTransOnlyFilter(authzEnvMock);
79 Class c = aTF.getClass();
80 Class[] cArg = new Class[2];
81 cArg[0] = AuthzTrans.class;
82 cArg[1] = ServletRequest.class; //Steps to test a protected method
83 Method startMethod = c.getDeclaredMethod("start", cArg);
84 startMethod.setAccessible(true);
85 //startMethod.invoke(aTF, authzTransMock, servletRequestMock);
89 public void testAuthenticated() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, CadiException {
90 TaggedPrincipal p = mock(TaggedPrincipal.class);
91 AuthzTransOnlyFilter aTF = new AuthzTransOnlyFilter(authzEnvMock);
92 Class c = aTF.getClass();
93 Class[] cArg = new Class[2];
94 cArg[0] = AuthzTrans.class;
95 cArg[1] = TaggedPrincipal.class; //Steps to test a protected method
96 Method authenticatedMethod = c.getDeclaredMethod("authenticated", cArg);
97 authenticatedMethod.setAccessible(true);
98 authenticatedMethod.invoke(aTF,authzTransMock, null);
102 public void testTallyHo() throws CadiException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
103 AuthzTransOnlyFilter aTF = new AuthzTransOnlyFilter(authzEnvMock);
104 LogTarget log = mock(LogTarget.class);
105 Metric met = new Metric();
108 met.buckets = new float[] {199.33F,99.33F};
109 Class c = aTF.getClass();
110 Class[] cArg = new Class[1];
111 cArg[0] = AuthzTrans.class; //Steps to test a protected method
112 StringBuilder sb = new StringBuilder("AuditTrail\n");
113 when(authzTransMock.auditTrail(anyInt(),(StringBuilder)any(),anyInt(),anyInt())).thenReturn(met);
114 when(authzTransMock.info()).thenReturn(log);
115 doNothing().when(log).log((StringBuilder)any());
116 Method tallyHoMethod = c.getDeclaredMethod("tallyHo", cArg);
117 tallyHoMethod.setAccessible(true);
118 tallyHoMethod.invoke(aTF,authzTransMock);