Sonar Cloud migration changes
[dmaap/kafka11aaf.git] / src / test / java / org / onap / dmaap / commonauth / kafka / base / authorization / Cadi3AAFProviderTest.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  org.onap.dmaap
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  *        http://www.apache.org/licenses/LICENSE-2.0
11 *  
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *  ============LICENSE_END=========================================================
18  *  
19  *  
20  *******************************************************************************/
21 package org.onap.dmaap.commonauth.kafka.base.authorization;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertNull;
27 import static org.mockito.Mockito.when;
28
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.Mock;
33 import org.mockito.MockitoAnnotations;
34 import org.onap.aaf.cadi.PropAccess;
35 import org.onap.aaf.cadi.aaf.AAFPermission;
36 import org.onap.aaf.cadi.aaf.v2_0.AAFAuthn;
37 import org.onap.aaf.cadi.aaf.v2_0.AAFConHttp;
38 import org.onap.aaf.cadi.aaf.v2_0.AbsAAFLur;
39 import org.powermock.core.classloader.annotations.PowerMockIgnore;
40 import org.powermock.modules.junit4.PowerMockRunner;
41
42
43 @RunWith(PowerMockRunner.class)
44 @PowerMockIgnore({"javax.net.ssl.*", "javax.security.auth.*"})
45 public class Cadi3AAFProviderTest {
46
47         public Cadi3AAFProvider cadi3AAFProvider;
48
49         @Mock
50         private static AAFAuthn<?> aafAuthn;
51         
52         @Mock
53         private static AAFConHttp aafCon;
54         
55         @Mock
56         private static AbsAAFLur<AAFPermission> aafLur;
57
58         @Mock
59         private static PropAccess access;
60
61         @Before
62         public void setUp() throws Exception {
63                 MockitoAnnotations.initMocks(this);
64                 System.setProperty("enableCadi", "true");
65                 System.setProperty("CADI_PROPERTIES", "src/test/resources/cadi.properties");
66                 cadi3AAFProvider = new Cadi3AAFProvider();
67         }
68
69         @Test
70         public void testHasPermission() {
71                 assertFalse(cadi3AAFProvider.hasPermission("userID", "permission", "instance", "action"));
72         }
73
74         @Test
75         public void testHasAdminPermission() {
76                 assertEquals(cadi3AAFProvider.hasPermission("admin", "permission", "instance", "action"), true);
77         }
78         
79         @Test(expected = NullPointerException.class)
80         public void tesAuthenticate() throws Exception {
81                 System.setProperty("enableCadi", "true");
82                 when(aafAuthn.validate("userId", "password")).thenReturn("valid");
83                 assertEquals(cadi3AAFProvider.authenticate("userId", "password"), "valid");
84         }
85
86         @Test
87         public void tesAuthenticateAdmin() throws Exception {
88                 assertNull(cadi3AAFProvider.authenticate("kafkaUsername", "apiKey"));
89         }
90         
91         @Test
92         public void tesAuthenticateAdminwtWrongCred() throws Exception {
93                 assertNotNull(cadi3AAFProvider.authenticate("kafkaUsername", "api"));
94         }
95         
96 }