2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 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
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=========================================================
20 package org.onap.dmaap.dmf.mr.security;
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertTrue;
25 import static org.mockito.BDDMockito.given;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.Spy;
31 import org.mockito.runners.MockitoJUnitRunner;
32 import org.springframework.mock.web.MockHttpServletRequest;
34 @RunWith(MockitoJUnitRunner.class)
35 public class DMaaPAAFAuthenticatorImplTest {
37 private MockHttpServletRequest request;
39 private DMaaPAAFAuthenticatorImpl aafAuthorizer;
42 public void setUp() throws Exception {
43 request = new MockHttpServletRequest();
48 public void aafAuthentication_shouldSuccess_whenRequestIsConfiguredWithProperUserRole() {
50 String userRole = "org.onap.dmaap.mr.topic|:topic.org.onap.dmaap.mr.aSimpleTopic|sub";
51 request.addUserRole(userRole);
54 boolean isAuthorized = aafAuthorizer.aafAuthentication(request, userRole);
57 assertTrue(isAuthorized);
61 public void aafAuthentication_shouldFail_whenRequestIsConfiguredWithProperUserRole() {
63 String userRole = "org.onap.dmaap.mr.topic|:topic.org.onap.dmaap.mr.aSimpleTopic|pub";
66 boolean isAuthorized = aafAuthorizer.aafAuthentication(request, userRole);
69 assertFalse(isAuthorized);
73 public void getPermissionAsString_shouldReturnValidTopicPermission_whenTopicWithNamespace() throws Exception {
75 String topicPermission = "org.onap.dmaap.mr.topic|:topic.org.onap.dmaap.mr.aSimpleTopic|pub";
76 String topicName = "org.onap.dmaap.mr.aSimpleTopic";
77 String operation = "pub";
80 String resultPem = aafAuthorizer.aafPermissionString(topicName, operation);
83 assertEquals(topicPermission, resultPem);
87 public void getPermissionAsString_shouldReturnValidTopicPermission_whenTopicWithoutNamespace() throws Exception {
89 String topicPermission = "org.onap.dmaap.mr.topic|:topic.topicName|pub";
90 String topicName = "topicName";
91 String operation = "pub";
94 String resultPem = aafAuthorizer.aafPermissionString(topicName, operation);
97 assertEquals(topicPermission, resultPem);
101 public void getPermissionAsString_shouldReturnValidTopicPermission_whenNamespaceReadFromProperty() throws Exception {
103 String topicPermission = "com.custom.ns.topic|:topic.topicName|pub";
104 String topicName = "topicName";
105 String operation = "pub";
106 String customNamespace = "com.custom.ns";
107 given(aafAuthorizer.readNamespaceFromProperties()).willReturn(customNamespace);
110 String resultPem = aafAuthorizer.aafPermissionString(topicName, operation);
113 assertEquals(topicPermission, resultPem);