091a3aa6c04faad7251365d15f6b70fc42001361
[aaf/authz.git] / auth / auth-core / src / test / java / org / onap / aaf / auth / env / test / JU_AuthzTransOnlyFilter.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.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;
30
31 import java.lang.reflect.InvocationTargetException;
32 import java.lang.reflect.Method;
33
34 import javax.servlet.ServletRequest;
35
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;
50
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;
61
62     @Before
63     public void setUp(){
64         authzTransOnlyFilter = new AuthzTransOnlyFilter(authzEnvMock);
65     }
66
67     /*@Test
68     public void testProtected() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
69         Method newTransMethod = AuthzTransFilter.class.getDeclaredMethod("newTrans");
70         newTransMethod.setAccessible(true);
71
72         newTransMethod.invoke(authzTransFilter);
73     }*/
74
75     @Test
76     public void testStart() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
77         AuthzTransOnlyFilter aTF = new AuthzTransOnlyFilter(authzEnvMock);
78         Class c = aTF.getClass();
79         Class[] cArg = new Class[2];
80         cArg[0] = AuthzTrans.class;
81         cArg[1] = ServletRequest.class;        //Steps to test a protected method
82         Method startMethod = c.getDeclaredMethod("start", cArg);
83         startMethod.setAccessible(true);
84         //startMethod.invoke(aTF, authzTransMock, servletRequestMock);
85     }
86
87     @Test
88     public void testAuthenticated() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, CadiException {
89         TaggedPrincipal p = mock(TaggedPrincipal.class);
90         AuthzTransOnlyFilter aTF = new AuthzTransOnlyFilter(authzEnvMock);
91         Class c = aTF.getClass();
92         Class[] cArg = new Class[2];
93         cArg[0] = AuthzTrans.class;
94         cArg[1] = TaggedPrincipal.class;        //Steps to test a protected method
95         Method authenticatedMethod = c.getDeclaredMethod("authenticated", cArg);
96         authenticatedMethod.setAccessible(true);
97         authenticatedMethod.invoke(aTF,authzTransMock, null);
98     }
99
100     @Test
101     public void testTallyHo() throws CadiException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
102         AuthzTransOnlyFilter aTF = new AuthzTransOnlyFilter(authzEnvMock);
103         LogTarget log = mock(LogTarget.class);
104         Metric met = new Metric();
105         met.total = 199.33F;
106         met.entries = 15;
107         met.buckets = new float[] {199.33F,99.33F};
108         Class c = aTF.getClass();
109         Class[] cArg = new Class[1];
110         cArg[0] = AuthzTrans.class;        //Steps to test a protected method
111         StringBuilder sb = new StringBuilder("AuditTrail\n");
112         when(authzTransMock.auditTrail(anyInt(),(StringBuilder)any(),anyInt(),anyInt())).thenReturn(met);
113         when(authzTransMock.info()).thenReturn(log);
114         doNothing().when(log).log((StringBuilder)any());
115         Method tallyHoMethod = c.getDeclaredMethod("tallyHo", cArg);
116         tallyHoMethod.setAccessible(true);
117         tallyHoMethod.invoke(aTF,authzTransMock);
118     }
119
120 }