Fixes/Refinements from Testing
[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         Method startMethod = c.getDeclaredMethod("start", new Class[] {AuthzTrans.class});
80         startMethod.setAccessible(true);
81         //startMethod.invoke(aTF, authzTransMock, servletRequestMock);
82     }
83
84     @Test
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);
95     }
96
97     @Test
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();
102         met.total = 199.33F;
103         met.entries = 15;
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);
115     }
116
117 }