EventsService authorization refactor
[dmaap/messagerouter/msgrtr.git] / src / test / java / org / onap / dmaap / dmf / mr / security / DMaaPAAFAuthenticatorImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
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
10  * 
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 package org.onap.dmaap.dmf.mr.security;
21
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;
26
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;
33
34 @RunWith(MockitoJUnitRunner.class)
35 public class DMaaPAAFAuthenticatorImplTest {
36
37     private MockHttpServletRequest request;
38     @Spy
39     private DMaaPAAFAuthenticatorImpl aafAuthorizer;
40
41     @Before
42     public void setUp() throws Exception {
43         request = new MockHttpServletRequest();
44     }
45
46
47     @Test
48     public void aafAuthentication_shouldSuccess_whenRequestIsConfiguredWithProperUserRole() {
49         //given
50         String userRole = "org.onap.dmaap.mr.topic|:topic.org.onap.dmaap.mr.aSimpleTopic|sub";
51         request.addUserRole(userRole);
52
53         //when
54         boolean isAuthorized = aafAuthorizer.aafAuthentication(request, userRole);
55
56         //then
57         assertTrue(isAuthorized);
58     }
59
60     @Test
61     public void aafAuthentication_shouldFail_whenRequestIsConfiguredWithProperUserRole() {
62         //given
63         String userRole = "org.onap.dmaap.mr.topic|:topic.org.onap.dmaap.mr.aSimpleTopic|pub";
64
65         //when
66         boolean isAuthorized = aafAuthorizer.aafAuthentication(request, userRole);
67
68         //then
69         assertFalse(isAuthorized);
70     }
71
72     @Test
73     public void getPermissionAsString_shouldReturnValidTopicPermission_whenTopicWithNamespace() throws Exception {
74         //given
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";
78
79         //when
80         String resultPem = aafAuthorizer.aafPermissionString(topicName, operation);
81
82         //then
83         assertEquals(topicPermission, resultPem);
84     }
85
86     @Test
87     public void getPermissionAsString_shouldReturnValidTopicPermission_whenTopicWithoutNamespace() throws Exception {
88         //given
89         String topicPermission = "org.onap.dmaap.mr.topic|:topic.topicName|pub";
90         String topicName = "topicName";
91         String operation = "pub";
92
93         //when
94         String resultPem = aafAuthorizer.aafPermissionString(topicName, operation);
95
96         //then
97         assertEquals(topicPermission, resultPem);
98     }
99
100     @Test
101     public void getPermissionAsString_shouldReturnValidTopicPermission_whenNamespaceReadFromProperty() throws Exception {
102         //given
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);
108
109         //when
110         String resultPem = aafAuthorizer.aafPermissionString(topicName, operation);
111
112         //then
113         assertEquals(topicPermission, resultPem);
114     }
115
116
117 }