1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
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
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=========================================================
21 *******************************************************************************/
22 package org.onap.dmaap.commonauth.kafka.base.authorization;
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;
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;
41 @RunWith(PowerMockRunner.class)
42 @PowerMockIgnore({"javax.net.ssl.*", "javax.security.auth.*", "jdk.internal.reflect.*"})
43 public class Cadi3AAFProviderTest {
45 public Cadi3AAFProvider cadi3AAFProvider;
48 private static AAFAuthn<?> aafAuthn;
51 System.setProperty("CADI_PROPERTIES", "src/test/resources/cadi.properties");
52 System.setProperty("enableCadi", "true");
57 MockitoAnnotations.initMocks(this);
58 cadi3AAFProvider = new Cadi3AAFProvider();
62 public void testHasPermission() {
63 assertFalse(cadi3AAFProvider.hasPermission("userID", "permission", "instance", "action"));
67 public void testHasAdminPermission() {
68 assertTrue(cadi3AAFProvider.hasPermission("admin", "permission", "instance", "action"));
71 public void tesAuthenticate() throws Exception {
72 when(aafAuthn.validate("userId", "password")).thenReturn("valid");
73 assertEquals("valid", cadi3AAFProvider.authenticate("userId", "password"));
77 public void tesAuthenticateAdmin() throws Exception {
78 assertNull(cadi3AAFProvider.authenticate("kafkaUsername", "apiKey"));
82 public void tesAuthenticateAdminwtWrongCred() throws Exception {
83 assertNotNull(cadi3AAFProvider.authenticate("kafkaUsername", "api"));