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_AuthzTransFilter.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
30 import static org.mockito.Matchers.any;
31 import static org.mockito.Matchers.anyInt;
32 import static org.mockito.Mockito.*;
33 import org.mockito.Mock;
34 import java.security.Principal;
35
36 import java.lang.reflect.InvocationTargetException;
37 import java.lang.reflect.Method;
38 import org.onap.aaf.auth.env.AuthzEnv;
39 import org.onap.aaf.auth.env.AuthzTrans;
40 import org.onap.aaf.auth.env.AuthzTransFilter;
41 import org.onap.aaf.auth.env.AuthzTransImpl;
42 import org.onap.aaf.cadi.CadiException;
43 import org.onap.aaf.cadi.Connector;
44 import org.onap.aaf.cadi.PropAccess;
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.Slot;
49 import org.onap.aaf.misc.env.Trans.Metric;
50 import org.powermock.api.mockito.PowerMockito;
51 import org.powermock.modules.junit4.PowerMockRunner;
52
53 @RunWith(PowerMockRunner.class)  
54 public class JU_AuthzTransFilter {
55 AuthzTransFilter authzTransFilter;
56 AuthzEnv authzEnvMock = mock(AuthzEnv.class);
57 Connector connectorMock = mock(Connector.class);
58 TrustChecker trustCheckerMock = mock(TrustChecker.class);
59 AuthzTrans authzTransMock = mock(AuthzTrans.class);
60 Object additionalTafLurs = mock(Object.class);
61
62         @Before
63         public void setUp() throws CadiException{
64                 when(authzEnvMock.access()).thenReturn(new PropAccess());
65                 //when(authzEnvMock.newTrans()).thenReturn(new AuthzTransImpl(authzEnvMock));
66                 authzTransFilter = new AuthzTransFilter(authzEnvMock, connectorMock, trustCheckerMock, additionalTafLurs);
67                 
68                 
69         }
70
71 /*      @Test
72         public void testTallyHo(){
73                 PowerMockito.when(authzTransMock.info().isLoggable()).thenReturn(true);
74                 //TODO: Gabe [JUnit] Not visible for junit
75                 //if(trans.info().isLoggable())
76                 //authzTransFilter.tallyHo(authzTransMock);
77                 
78         }*/
79
80         /*@Test
81         public void testProtected() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
82                 Method newTransMethod = AuthzTransFilter.class.getDeclaredMethod("newTrans");
83                 newTransMethod.setAccessible(true);
84                 
85                 newTransMethod.invoke(authzTransFilter);
86         }*/
87         
88         @Test
89         public void testAuthenticated() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, CadiException {
90                 Principal p = mock(Principal.class);
91                 AuthzTransFilter aTF = new AuthzTransFilter(authzEnvMock, connectorMock, trustCheckerMock, null);
92                 Class c = aTF.getClass();
93                 Class[] cArg = new Class[2];
94                 cArg[0] = AuthzTrans.class;
95                 cArg[1] = Principal.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                 Slot specialLogSlot = authzEnvMock.slot("SPECIAL_LOG_SLOT");
104                 LogTarget lt = mock(LogTarget.class);
105                 AuthzTransFilter aTF = new AuthzTransFilter(authzEnvMock, connectorMock, trustCheckerMock, additionalTafLurs);
106                 TaggedPrincipal tPrin = mock(TaggedPrincipal.class);
107                 Metric met = new Metric();
108                 met.total = 199.33F;
109                 met.entries = 15;
110                 met.buckets = new float[] {199.33F,99.33F};
111                 Class c = aTF.getClass();
112                 Class[] cArg = new Class[1];
113                 cArg[0] = AuthzTrans.class;             //Steps to test a protected method
114                 Method tallyHoMethod = c.getDeclaredMethod("tallyHo", cArg);
115                 StringBuilder sb = new StringBuilder("AuditTrail\n");
116                 when(authzTransMock.auditTrail(((LogTarget)any()), anyInt(),(StringBuilder)any(),anyInt(),anyInt())).thenReturn(met);
117                 tallyHoMethod.setAccessible(true);
118                 when(authzTransMock.get(specialLogSlot, false)).thenReturn(false);
119                 when(authzTransMock.warn()).thenReturn(lt);
120                 when(authzTransMock.info()).thenReturn(lt);
121                 tallyHoMethod.invoke(aTF,authzTransMock);
122                 when(authzTransMock.getUserPrincipal()).thenReturn(tPrin);
123                 tallyHoMethod.invoke(aTF,authzTransMock);
124                 
125         }
126         
127         
128         
129         
130 }