AT&T 2.0.19 Code drop, stage 3
[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 package org.onap.aaf.auth.env.test;
23
24 import static org.junit.Assert.*;
25
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;
34
35 import javax.servlet.ServletRequest;
36
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;
51
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;
62         
63         @Before
64         public void setUp(){
65                 authzTransOnlyFilter = new AuthzTransOnlyFilter(authzEnvMock);
66         }
67         
68         /*@Test
69         public void testProtected() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
70                 Method newTransMethod = AuthzTransFilter.class.getDeclaredMethod("newTrans");
71                 newTransMethod.setAccessible(true);
72                 
73                 newTransMethod.invoke(authzTransFilter);
74         }*/
75         
76         @Test
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);
86         }
87
88         @Test
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);
99         }
100         
101         @Test
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();
106                 met.total = 199.33F;
107                 met.entries = 15;
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);
119         }
120
121 }