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.Matchers.any;
26 import static org.mockito.Matchers.anyInt;
27 import static org.mockito.Mockito.doNothing;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.when;
31 import java.lang.reflect.InvocationTargetException;
32 import java.lang.reflect.Method;
34 import javax.servlet.ServletRequest;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
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.cadi.principal.TaggedPrincipal;
48 import org.onap.aaf.misc.env.LogTarget;
49 import org.onap.aaf.misc.env.Trans.Metric;
51 @RunWith(MockitoJUnitRunner.class)
52 public class JU_AuthzTransOnlyFilter {
53 AuthzTransFilter authzTransFilter;
54 AuthzEnv authzEnvMock = mock(AuthzEnv.class);
55 Connector connectorMock = mock(Connector.class);
56 TrustChecker trustCheckerMock = mock(TrustChecker.class);
57 AuthzTrans authzTransMock = mock(AuthzTrans.class);
58 Object additionalTafLurs = mock(Object.class);
59 ServletRequest servletRequestMock = mock(ServletRequest.class);
60 AuthzTransOnlyFilter authzTransOnlyFilter;
64 authzTransOnlyFilter = new AuthzTransOnlyFilter(authzEnvMock);
68 public void testProtected() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
69 Method newTransMethod = AuthzTransFilter.class.getDeclaredMethod("newTrans");
70 newTransMethod.setAccessible(true);
72 newTransMethod.invoke(authzTransFilter);
76 public void testStart() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
77 AuthzTransOnlyFilter aTF = new AuthzTransOnlyFilter(authzEnvMock);
78 Class c = aTF.getClass();
79 Method startMethod = c.getDeclaredMethod("start", new Class[] {AuthzTrans.class});
80 startMethod.setAccessible(true);
81 //startMethod.invoke(aTF, authzTransMock, servletRequestMock);
85 public void testAuthenticated() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, CadiException {
86 TaggedPrincipal p = mock(TaggedPrincipal.class);
87 AuthzTransOnlyFilter aTF = new AuthzTransOnlyFilter(authzEnvMock);
88 Class c = aTF.getClass();
89 Class[] cArg = new Class[2];
90 cArg[0] = AuthzTrans.class;
91 cArg[1] = TaggedPrincipal.class; //Steps to test a protected method
92 Method authenticatedMethod = c.getDeclaredMethod("authenticated", cArg);
93 authenticatedMethod.setAccessible(true);
94 authenticatedMethod.invoke(aTF,authzTransMock, null);
98 public void testTallyHo() throws CadiException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
99 AuthzTransOnlyFilter aTF = new AuthzTransOnlyFilter(authzEnvMock);
100 LogTarget log = mock(LogTarget.class);
101 Metric met = new Metric();
104 met.buckets = new float[] {199.33F,99.33F};
105 Class c = aTF.getClass();
106 Class[] cArg = new Class[1];
107 cArg[0] = AuthzTrans.class; //Steps to test a protected method
108 StringBuilder sb = new StringBuilder("AuditTrail\n");
109 when(authzTransMock.auditTrail(anyInt(),(StringBuilder)any(),anyInt(),anyInt())).thenReturn(met);
110 when(authzTransMock.info()).thenReturn(log);
111 doNothing().when(log).log((StringBuilder)any());
112 Method tallyHoMethod = c.getDeclaredMethod("tallyHo", cArg);
113 tallyHoMethod.setAccessible(true);
114 tallyHoMethod.invoke(aTF,authzTransMock);