[DMAAP-KAFKA] Release img version 1.1.1
[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  *  Modification copyright (C) 2021 Nordix Foundation.
7  *  ================================================================================
8  *  Licensed under the Apache License, Version 2.0 (the "License");
9  *  you may not use this file except in compliance with the License.
10  *  You may obtain a copy of the License at
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.dmaap.commonauth.kafka.base.authorization;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNull;
28 import static org.junit.Assert.assertTrue;
29 import static org.mockito.Mockito.when;
30
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.Mock;
35 import org.mockito.MockitoAnnotations;
36 import org.onap.aaf.cadi.aaf.v2_0.AAFAuthn;
37 import org.powermock.core.classloader.annotations.PowerMockIgnore;
38 import org.powermock.modules.junit4.PowerMockRunner;
39
40
41 @RunWith(PowerMockRunner.class)
42 @PowerMockIgnore({"javax.net.ssl.*", "javax.security.auth.*", "jdk.internal.reflect.*"})
43 public class Cadi3AAFProviderTest {
44
45         public Cadi3AAFProvider cadi3AAFProvider;
46
47         @Mock
48         private static AAFAuthn<?> aafAuthn;
49
50         static {
51                 System.setProperty("CADI_PROPERTIES", "src/test/resources/cadi.properties");
52                 System.setProperty("enableCadi", "true");
53         }
54
55         @Before
56         public void setUp() {
57                 MockitoAnnotations.initMocks(this);
58                 cadi3AAFProvider = new Cadi3AAFProvider();
59         }
60
61         @Test
62         public void testHasPermission() {
63                 assertFalse(cadi3AAFProvider.hasPermission("userID", "permission", "instance", "action"));
64         }
65
66         @Test
67         public void testHasAdminPermission() {
68                 assertTrue(cadi3AAFProvider.hasPermission("admin", "permission", "instance", "action"));
69         }
70         
71         public void tesAuthenticate() throws Exception {
72                 when(aafAuthn.validate("userId", "password")).thenReturn("valid");
73                 assertEquals("valid", cadi3AAFProvider.authenticate("userId", "password"));
74         }
75
76         @Test
77         public void tesAuthenticateAdmin() throws Exception {
78                 assertNull(cadi3AAFProvider.authenticate("kafkaUsername", "apiKey"));
79         }
80         
81         @Test
82         public void tesAuthenticateAdminwtWrongCred() throws Exception {
83                 assertNotNull(cadi3AAFProvider.authenticate("kafkaUsername", "api"));
84         }
85 }