2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.dmaap.datarouter.authz.impl;
23 import static org.mockito.Mockito.when;
25 import javax.persistence.EntityManager;
26 import javax.persistence.EntityManagerFactory;
27 import javax.persistence.Persistence;
28 import javax.servlet.http.HttpServletRequest;
29 import org.junit.AfterClass;
30 import org.junit.Assert;
31 import org.junit.Before;
32 import org.junit.BeforeClass;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.Mock;
36 import org.mockito.Mockito;
37 import org.onap.dmaap.datarouter.authz.AuthorizationResponse;
38 import org.onap.dmaap.datarouter.provisioning.StatisticsServlet;
39 import org.powermock.modules.junit4.PowerMockRunner;
41 @RunWith(PowerMockRunner.class)
42 public class ProvAuthTest {
45 private HttpServletRequest request;
48 private StatisticsServlet statisticsServlet;
50 private ProvAuthorizer provAuthorizer;
52 private static EntityManagerFactory emf;
53 private static EntityManager em;
56 public static void init() {
57 emf = Persistence.createEntityManagerFactory("dr-unit-tests");
58 em = emf.createEntityManager();
60 "org.onap.dmaap.datarouter.provserver.properties",
61 "src/test/resources/h2Database.properties");
65 public static void tearDownClass() {
72 public void setUp() throws Exception {
73 provAuthorizer = new ProvAuthorizer(statisticsServlet);
77 public void Validate_Prov_Auth_Check_Feed_Access() {
78 when(statisticsServlet.getFeedOwner(Mockito.anyString())).thenReturn("dr-admin");
79 when(statisticsServlet.getGroupByFeedGroupId(Mockito.anyString(), Mockito.anyString())).thenReturn("stub_auth_id");
80 when(request.getHeader("X-DMAAP-DR-ON-BEHALF-OF")).thenReturn("dr-admin");
81 when(request.getHeader("X-DMAAP-DR-ON-BEHALF-OF-GROUP")).thenReturn("stub_auth_id");
82 when(request.getMethod()).thenReturn("PUT");
83 when(request.getRequestURI()).thenReturn("http://the-request-uri:443/feed/1?1");
84 AuthorizationResponse authResp;
85 authResp = provAuthorizer.decide(request);
86 Assert.assertTrue(authResp.isAuthorized());
90 public void Validate_Prov_Auth_Check_Sub_Access() {
91 when(statisticsServlet.getSubscriptionOwner(Mockito.anyString())).thenReturn("dr-admin");
92 when(statisticsServlet.getGroupBySubGroupId(Mockito.anyString(), Mockito.anyString())).thenReturn("stub_auth_id");
93 when(request.getHeader("X-DMAAP-DR-ON-BEHALF-OF")).thenReturn("dr-admin");
94 when(request.getHeader("X-DMAAP-DR-ON-BEHALF-OF-GROUP")).thenReturn("stub_auth_id");
95 when(request.getMethod()).thenReturn("PUT");
96 when(request.getRequestURI()).thenReturn("http://the-request-uri:443/subs/1?1");
97 AuthorizationResponse authResp;
98 authResp = provAuthorizer.decide(request);
99 Assert.assertTrue(authResp.isAuthorized());
103 public void Validate_Prov_Auth_Check_Subs_Collection_Access() {
104 when(statisticsServlet.getSubscriptionOwner(Mockito.anyString())).thenReturn("dr-admin");
105 when(statisticsServlet.getGroupBySubGroupId(Mockito.anyString(), Mockito.anyString())).thenReturn("stub_auth_id");
106 when(request.getHeader("X-DMAAP-DR-ON-BEHALF-OF")).thenReturn("dr-admin");
107 when(request.getHeader("X-DMAAP-DR-ON-BEHALF-OF-GROUP")).thenReturn("stub_auth_id");
108 when(request.getMethod()).thenReturn("POST");
109 when(request.getRequestURI()).thenReturn("http://the-request-uri:443/subscribe/1?1");
110 AuthorizationResponse authResp;
111 authResp = provAuthorizer.decide(request);
112 Assert.assertTrue(authResp.isAuthorized());
116 public void Validate_Prov_Auth_Check_Feeds_Collection_Access() {
117 when(statisticsServlet.getFeedOwner(Mockito.anyString())).thenReturn("dr-admin");
118 when(statisticsServlet.getGroupByFeedGroupId(Mockito.anyString(), Mockito.anyString())).thenReturn("stub_auth_id");
119 when(request.getHeader("X-DMAAP-DR-ON-BEHALF-OF")).thenReturn("dr-admin");
120 when(request.getHeader("X-DMAAP-DR-ON-BEHALF-OF-GROUP")).thenReturn("stub_auth_id");
121 when(request.getMethod()).thenReturn("POST");
122 when(request.getRequestURI()).thenReturn("http://the-request-uri:443/");
123 AuthorizationResponse authResp;
124 authResp = provAuthorizer.decide(request);
125 Assert.assertTrue(authResp.isAuthorized());
126 Assert.assertNull(authResp.getAdvice());
127 Assert.assertNull(authResp.getObligations());