updating test cases for more coverage
[dmaap/kafka11aaf.git] / src / test / java / org / onap / dmaap / kafkaAuthorize / KafkaCustomAuthorizerTest.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.kafkaAuthorize;
22
23 import static org.junit.Assert.assertTrue;
24
25 import org.apache.kafka.common.security.auth.KafkaPrincipal;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.mockito.Mock;
30 import org.mockito.MockitoAnnotations;
31 import org.onap.dmaap.commonauth.kafka.base.authorization.AuthorizationProvider;
32 import org.onap.dmaap.commonauth.kafka.base.authorization.AuthorizationProviderFactory;
33 import org.powermock.api.mockito.PowerMockito;
34 import org.powermock.core.classloader.annotations.PowerMockIgnore;
35 import org.powermock.core.classloader.annotations.PrepareForTest;
36 import org.powermock.modules.junit4.PowerMockRunner;
37
38 import kafka.network.RequestChannel.Session;
39 import kafka.security.auth.Operation;
40 import kafka.security.auth.Resource;
41 import kafka.security.auth.ResourceType;
42
43 @RunWith(PowerMockRunner.class)
44 @PowerMockIgnore("javax.net.ssl.*")
45 @PrepareForTest({ AuthorizationProviderFactory.class })
46 public class KafkaCustomAuthorizerTest {
47         @Mock
48         Session arg0;
49         @Mock
50         Operation arg1;
51         @Mock
52         Resource arg2;
53         @Mock
54         KafkaPrincipal principal;
55         @Mock
56         ResourceType resourceType;
57         @Mock
58         AuthorizationProviderFactory factory;
59         @Mock
60         AuthorizationProvider provider;
61
62         KafkaCustomAuthorizer authorizer;
63         
64         static {
65                 System.setProperty("CADI_PROPERTIES", "src/test/resources/cadi.properties");
66         }
67
68         @Before
69         public void setUp() throws Exception {
70
71                 MockitoAnnotations.initMocks(this);
72                 PowerMockito.when(principal.getName()).thenReturn("fullName");
73                 PowerMockito.when(arg0.principal()).thenReturn(principal);
74                 PowerMockito.when(arg1.name()).thenReturn("Write");
75                 PowerMockito.when(resourceType.name()).thenReturn("Topic");
76                 PowerMockito.when(arg2.resourceType()).thenReturn(resourceType);
77                 PowerMockito.when(arg2.name()).thenReturn("namespace.Topic");
78                 PowerMockito.mockStatic(AuthorizationProviderFactory.class);
79                 PowerMockito.when(AuthorizationProviderFactory.getProviderFactory()).thenReturn(factory);
80                 PowerMockito.when(factory.getProvider()).thenReturn(provider);
81
82         }
83
84         @Test
85         public void testAuthorizerSuccess() {
86
87
88                 PowerMockito.when(provider.hasPermission("fullName", "namespace.topic", ":topic.namespace.Topic", "pub"))
89                                 .thenReturn(true);
90                 authorizer = new KafkaCustomAuthorizer();
91                 assertTrue(authorizer.authorize(arg0, arg1, arg2));
92
93         }
94
95         @Test
96         public void testAuthorizerFailure() {
97                 System.setProperty("CADI_PROPERTIES", "src/test/resources/cadi.properties");
98                 PowerMockito.when(provider.hasPermission("fullName", "namespace.topic", ":topic.namespace.Topic", "pub"))
99                                 .thenReturn(false);
100                 authorizer = new KafkaCustomAuthorizer();
101                 try {
102                         authorizer.authorize(arg0, arg1, arg2);
103                 } catch (Exception e) {
104                         assertTrue(true);
105                 }
106
107         }
108
109 }