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.authz.impl.ProvAuthorizer;
39 import org.onap.dmaap.datarouter.provisioning.StatisticsServlet;
40 import org.onap.dmaap.datarouter.provisioning.utils.DB;
41 import org.powermock.modules.junit4.PowerMockRunner;
43 @RunWith(PowerMockRunner.class)
44 public class ProvAuthTest {
47 private HttpServletRequest request;
50 private StatisticsServlet statisticsServlet;
52 private ProvAuthorizer provAuthorizer;
54 private static EntityManagerFactory emf;
55 private static EntityManager em;
59 public static void init() {
60 emf = Persistence.createEntityManagerFactory("dr-unit-tests");
61 em = emf.createEntityManager();
63 "org.onap.dmaap.datarouter.provserver.properties",
64 "src/test/resources/h2Database.properties");
68 public static void tearDownClass() {
75 public void setUp() throws Exception {
77 provAuthorizer = new ProvAuthorizer(statisticsServlet);
81 public void Validate_Prov_Auth_Check_Feed_Access() {
82 when(statisticsServlet.getFeedOwner(Mockito.anyString())).thenReturn("dr-admin");
83 when(statisticsServlet.getGroupByFeedGroupId(Mockito.anyString(), Mockito.anyString())).thenReturn("stub_auth_id");
84 when(request.getHeader("X-DMAAP-DR-ON-BEHALF-OF")).thenReturn("dr-admin");
85 when(request.getHeader("X-DMAAP-DR-ON-BEHALF-OF-GROUP")).thenReturn("stub_auth_id");
86 when(request.getMethod()).thenReturn("PUT");
87 when(request.getRequestURI()).thenReturn("http://the-request-uri:443/feed/1?1");
88 AuthorizationResponse authResp;
89 authResp = provAuthorizer.decide(request);
90 Assert.assertTrue(authResp.isAuthorized());
94 public void Validate_Prov_Auth_Check_Sub_Access() {
95 when(statisticsServlet.getSubscriptionOwner(Mockito.anyString())).thenReturn("dr-admin");
96 when(statisticsServlet.getGroupBySubGroupId(Mockito.anyString(), Mockito.anyString())).thenReturn("stub_auth_id");
97 when(request.getHeader("X-DMAAP-DR-ON-BEHALF-OF")).thenReturn("dr-admin");
98 when(request.getHeader("X-DMAAP-DR-ON-BEHALF-OF-GROUP")).thenReturn("stub_auth_id");
99 when(request.getMethod()).thenReturn("PUT");
100 when(request.getRequestURI()).thenReturn("http://the-request-uri:443/subs/1?1");
101 AuthorizationResponse authResp;
102 authResp = provAuthorizer.decide(request);
103 Assert.assertTrue(authResp.isAuthorized());
107 public void Validate_Prov_Auth_Check_Subs_Collection_Access() {
108 when(statisticsServlet.getSubscriptionOwner(Mockito.anyString())).thenReturn("dr-admin");
109 when(statisticsServlet.getGroupBySubGroupId(Mockito.anyString(), Mockito.anyString())).thenReturn("stub_auth_id");
110 when(request.getHeader("X-DMAAP-DR-ON-BEHALF-OF")).thenReturn("dr-admin");
111 when(request.getHeader("X-DMAAP-DR-ON-BEHALF-OF-GROUP")).thenReturn("stub_auth_id");
112 when(request.getMethod()).thenReturn("POST");
113 when(request.getRequestURI()).thenReturn("http://the-request-uri:443/subscribe/1?1");
114 AuthorizationResponse authResp;
115 authResp = provAuthorizer.decide(request);
116 Assert.assertTrue(authResp.isAuthorized());
120 public void Validate_Prov_Auth_Check_Feeds_Collection_Access() {
121 when(statisticsServlet.getFeedOwner(Mockito.anyString())).thenReturn("dr-admin");
122 when(statisticsServlet.getGroupByFeedGroupId(Mockito.anyString(), Mockito.anyString())).thenReturn("stub_auth_id");
123 when(request.getHeader("X-DMAAP-DR-ON-BEHALF-OF")).thenReturn("dr-admin");
124 when(request.getHeader("X-DMAAP-DR-ON-BEHALF-OF-GROUP")).thenReturn("stub_auth_id");
125 when(request.getMethod()).thenReturn("POST");
126 when(request.getRequestURI()).thenReturn("http://the-request-uri:443/");
127 AuthorizationResponse authResp;
128 authResp = provAuthorizer.decide(request);
129 Assert.assertTrue(authResp.isAuthorized());
130 Assert.assertNull(authResp.getAdvice());
131 Assert.assertNull(authResp.getObligations());