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