Keep only clean TestCases, remove 2 license issues
[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 testAuthenticated() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, CadiException {
73                 Principal p = mock(Principal.class);
74                 AuthzTransFilter aTF = new AuthzTransFilter(authzEnvMock, connectorMock, trustCheckerMock, null);
75                 Class c = aTF.getClass();
76                 Class[] cArg = new Class[2];
77                 cArg[0] = AuthzTrans.class;
78                 cArg[1] = Principal.class;              //Steps to test a protected method
79                 Method authenticatedMethod = c.getDeclaredMethod("authenticated", cArg);
80                 authenticatedMethod.setAccessible(true);
81                 authenticatedMethod.invoke(aTF,authzTransMock, null);
82         }
83         
84         @Test
85         public void testTallyHo() throws CadiException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
86                 Slot specialLogSlot = authzEnvMock.slot("SPECIAL_LOG_SLOT");
87                 LogTarget lt = mock(LogTarget.class);
88                 AuthzTransFilter aTF = new AuthzTransFilter(authzEnvMock, connectorMock, trustCheckerMock, additionalTafLurs);
89                 TaggedPrincipal tPrin = mock(TaggedPrincipal.class);
90                 Metric met = new Metric();
91                 met.total = 199.33F;
92                 met.entries = 15;
93                 met.buckets = new float[] {199.33F,99.33F};
94                 Class c = aTF.getClass();
95                 Class[] cArg = new Class[1];
96                 cArg[0] = AuthzTrans.class;             //Steps to test a protected method
97                 Method tallyHoMethod = c.getDeclaredMethod("tallyHo", cArg);
98                 StringBuilder sb = new StringBuilder("AuditTrail\n");
99                 when(authzTransMock.auditTrail(((LogTarget)any()), anyInt(),(StringBuilder)any(),anyInt(),anyInt())).thenReturn(met);
100                 tallyHoMethod.setAccessible(true);
101                 when(authzTransMock.get(specialLogSlot, false)).thenReturn(false);
102                 when(authzTransMock.warn()).thenReturn(lt);
103                 when(authzTransMock.info()).thenReturn(lt);
104                 tallyHoMethod.invoke(aTF,authzTransMock);
105                 when(authzTransMock.getUserPrincipal()).thenReturn(tPrin);
106                 tallyHoMethod.invoke(aTF,authzTransMock);
107                 
108         }
109         
110         
111         
112         
113 }