2  * ============LICENSE_START=======================================================
\r 
   3  *  Copyright (C) 2019 Nordix Foundation.
\r 
   4  * ================================================================================
\r 
   5  * Licensed under the Apache License, Version 2.0 (the "License");
\r 
   6  * you may not use this file except in compliance with the License.
\r 
   7  * You may obtain a copy of the License at
\r 
   9  *      http://www.apache.org/licenses/LICENSE-2.0
\r 
  11  * Unless required by applicable law or agreed to in writing, software
\r 
  12  * distributed under the License is distributed on an "AS IS" BASIS,
\r 
  13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r 
  14  * See the License for the specific language governing permissions and
\r 
  15  * limitations under the License.
\r 
  17  * SPDX-License-Identifier: Apache-2.0
\r 
  18  * ============LICENSE_END=========================================================
\r 
  21 package org.onap.dmaap.datarouter.provisioning.utils;
\r 
  23 import static org.mockito.ArgumentMatchers.anyString;
\r 
  24 import static org.mockito.ArgumentMatchers.eq;
\r 
  25 import static org.mockito.Mockito.times;
\r 
  26 import static org.mockito.Mockito.verify;
\r 
  27 import static org.mockito.Mockito.when;
\r 
  28 import static org.onap.dmaap.datarouter.provisioning.BaseServlet.BEHALF_HEADER;
\r 
  30 import javax.persistence.EntityManager;
\r 
  31 import javax.persistence.EntityManagerFactory;
\r 
  32 import javax.persistence.Persistence;
\r 
  33 import javax.servlet.FilterChain;
\r 
  34 import javax.servlet.http.HttpServletRequest;
\r 
  35 import javax.servlet.http.HttpServletResponse;
\r 
  36 import org.junit.Before;
\r 
  37 import org.junit.BeforeClass;
\r 
  38 import org.junit.Test;
\r 
  39 import org.junit.runner.RunWith;
\r 
  40 import org.mockito.Mock;
\r 
  41 import org.onap.aaf.cadi.PropAccess;
\r 
  42 import org.onap.aaf.cadi.filter.CadiFilter;
\r 
  43 import org.onap.dmaap.datarouter.provisioning.BaseServlet;
\r 
  44 import org.powermock.api.mockito.PowerMockito;
\r 
  45 import org.powermock.api.support.membermodification.MemberMatcher;
\r 
  46 import org.powermock.core.classloader.annotations.PowerMockIgnore;
\r 
  47 import org.powermock.core.classloader.annotations.PrepareForTest;
\r 
  48 import org.powermock.modules.junit4.PowerMockRunner;
\r 
  50 @RunWith(PowerMockRunner.class)
\r 
  51 @PrepareForTest({CadiFilter.class})
\r 
  52 @PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "org.w3c.*"})
\r 
  53 public class DRProvCadiFilterTest {
\r 
  56     private PropAccess access;
\r 
  59     private HttpServletRequest request;
\r 
  62     private HttpServletResponse response;
\r 
  65     private FilterChain chain;
\r 
  67     private DRProvCadiFilter cadiFilter;
\r 
  70     private static EntityManagerFactory emf;
\r 
  71     private static EntityManager em;
\r 
  75     public static void init() {
\r 
  76         emf = Persistence.createEntityManagerFactory("dr-unit-tests");
\r 
  77         em = emf.createEntityManager();
\r 
  79                 "org.onap.dmaap.datarouter.provserver.properties",
\r 
  80                 "src/test/resources/h2Database.properties");
\r 
  84     public void setUp() throws Exception {
\r 
  85         cadiFilter = new DRProvCadiFilter(false, access);
\r 
  89     public void Given_doFilter_Called_And_Path_Contains_subs_And_SubId_Is_Incorrectly_Set_Then_Not_Found_Response_Returned() throws Exception{
\r 
  90         setRequestMocking("PUT", "subs");
\r 
  92         cadiFilter.doFilter(request, response, chain);
\r 
  93         verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString());
\r 
  97     public void Given_doFilter_called_And_Path_Contains_subs_And_Is_AAF_Subscriber_then_call_Super_doFilter() throws Exception{
\r 
  98         setRequestMocking("PUT", "subs");
\r 
  99         when(request.getPathInfo()).thenReturn("/2");
\r 
 100         PowerMockito.suppress(MemberMatcher.methodsDeclaredIn(CadiFilter.class));
\r 
 101         cadiFilter.doFilter(request, response, chain);
\r 
 102         verify(chain, times(0)).doFilter(request, response);
\r 
 106     public void Given_doFilter_called_And_Path_Contains_subs_And_Is_Not_AAF_Subscriber_then_call_chain_doFilter() throws Exception{
\r 
 107         setRequestMocking("PUT", "subs");
\r 
 108         when(request.getPathInfo()).thenReturn("/5");
\r 
 110         cadiFilter.doFilter(request, response, chain);
\r 
 111         verify(chain, times(1)).doFilter(request, response);
\r 
 115     public void Given_doFilter_called_And_FeedId_Is_Incorrectly_Set_Then_Not_Found_Response_Returned () throws Exception{
\r 
 116         setRequestMocking("PUT", "feeds");
\r 
 118         cadiFilter.doFilter(request, response, chain);
\r 
 119         verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString());
\r 
 123     public void Given_doFilter_called_And_FeedId_Is_Correctly_Set_And_Is_AAF_Feed_Then_Call_Super_doFilter() throws Exception{
\r 
 124         setRequestMocking("PUT", "feeds");
\r 
 125         when(request.getPathInfo()).thenReturn("/2");
\r 
 126         PowerMockito.suppress(MemberMatcher.methodsDeclaredIn(CadiFilter.class));
\r 
 127         cadiFilter.doFilter(request, response, chain);
\r 
 128         verify(chain, times(0)).doFilter(request, response);
\r 
 132     public void Given_doFilter_called_And_FeedId_Is_Correctly_Set_And_Is_Not_AAF_Feed_then_call_chain_doFilter() throws Exception{
\r 
 133         setRequestMocking("PUT", "feeds");
\r 
 134         when(request.getPathInfo()).thenReturn("/1");
\r 
 136         cadiFilter.doFilter(request, response, chain);
\r 
 137         verify(chain, times(1)).doFilter(request, response);
\r 
 141     public void Given_doFilter_called_With_Get_Then_call_chain_doFilter() throws Exception{
\r 
 142         setRequestMocking("GET", "feeds");
\r 
 143         when(request.getPathInfo()).thenReturn("/5");
\r 
 145         cadiFilter.doFilter(request, response, chain);
\r 
 146         verify(chain, times(1)).doFilter(request, response);
\r 
 151     public void Given_doFilter_called_With_POST_Then_call_chain_doFilter() throws Exception{
\r 
 152         setRequestMocking("POST", "subscribe");
\r 
 154         cadiFilter.doFilter(request, response, chain);
\r 
 155         verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString());
\r 
 160     public void Given_doFilter_called_With_POST_And_FeedId_Is_Incorrectly_Set_Then_Not_Found_Response_Returned() throws Exception{
\r 
 161         setRequestMocking("POST", "subscribe");
\r 
 163         cadiFilter.doFilter(request, response, chain);
\r 
 164         verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString());
\r 
 169     public void Given_doFilter_called_With_POST_And_Exclude_AAF_Is_NULL_Then_Bad_Request_Response_Returned() throws Exception{
\r 
 170         setRequestMocking("POST", "subscribe");
\r 
 171         when(request.getPathInfo()).thenReturn("/2");
\r 
 173         cadiFilter.doFilter(request, response, chain);
\r 
 174         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), anyString());
\r 
 179     public void Given_doFilter_called_With_POST_And_Exclude_AAF_Equals_True_Then_Call_Chain_doFilter() throws Exception{
\r 
 180         setRequestMocking("POST", "subscribe");
\r 
 181         when(request.getPathInfo()).thenReturn("/2");
\r 
 182         when(request.getHeader("X-EXCLUDE-AAF")).thenReturn("true");
\r 
 184         cadiFilter.doFilter(request, response, chain);
\r 
 185         verify(chain, times(1)).doFilter(request, response);
\r 
 190     public void Given_doFilter_called_With_POST_And_Exclude_AAF_Equals_False_Then_Call_Super_doFilter() throws Exception{
\r 
 191         setRequestMocking("POST", "subscribe");
\r 
 192         when(request.getPathInfo()).thenReturn("/2");
\r 
 193         when(request.getHeader("X-EXCLUDE-AAF")).thenReturn("false");
\r 
 194         PowerMockito.suppress(MemberMatcher.methodsDeclaredIn(CadiFilter.class));
\r 
 196         cadiFilter.doFilter(request, response, chain);
\r 
 197         verify(chain, times(0)).doFilter(request, response);
\r 
 202     public void Given_doFilter_called_With_POST_And_Is_Not_AAF_Exclude_AAF_Equals_Then_Call_Chain_doFilter() throws Exception{
\r 
 203         setRequestMocking("POST", "subscribe");
\r 
 204         when(request.getPathInfo()).thenReturn("/5");
\r 
 205         when(request.getHeader("X-EXCLUDE-AAF")).thenReturn("false");
\r 
 207         cadiFilter.doFilter(request, response, chain);
\r 
 208         verify(chain, times(1)).doFilter(request, response);
\r 
 213     public void Given_doFilter_called_With_POST_And_Path_Not_Includes_subscribe_And_Exclude_AAF_Is_NULL_Then_Bad_Request_Response_Returned() throws Exception{
\r 
 214         setRequestMocking("POST", "other");
\r 
 215         when(request.getPathInfo()).thenReturn("/5");
\r 
 217         cadiFilter.doFilter(request, response, chain);
\r 
 218         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), anyString());
\r 
 223     public void Given_doFilter_called_With_POST_And_Path_Not_Includes_subscribe_And_Exclude_AAF_Equals_True_Then_Call_Chain_doFilter() throws Exception{
\r 
 224         setRequestMocking("POST", "other");
\r 
 225         when(request.getPathInfo()).thenReturn("/5");
\r 
 226         when(request.getHeader("X-EXCLUDE-AAF")).thenReturn("true");
\r 
 228         cadiFilter.doFilter(request, response, chain);
\r 
 229         verify(chain, times(1)).doFilter(request, response);
\r 
 234     public void Given_doFilter_called_With_POST_And_Path_Not_Includes_subscribe_And_Exclude_AAF_Equals_False_Then_Call_Super_doFilter() throws Exception{
\r 
 235         setRequestMocking("POST", "other");
\r 
 236         when(request.getPathInfo()).thenReturn("/5");
\r 
 237         when(request.getHeader("X-EXCLUDE-AAF")).thenReturn("false");
\r 
 238         PowerMockito.suppress(MemberMatcher.methodsDeclaredIn(CadiFilter.class));
\r 
 240         cadiFilter.doFilter(request, response, chain);
\r 
 241         verify(chain, times(0)).doFilter(request, response);
\r 
 246     public void Given_doFilter_Called_And_Path_Contains_subs_And_getSubId_Throws_NumberFormatException_then_Not_Found_response_returned() throws Exception{
\r 
 247             setRequestMocking("PUT", "subs");
\r 
 248             when(request.getPathInfo()).thenReturn("5/");
\r 
 249             cadiFilter.doFilter(request, response, chain);
\r 
 250             verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString());
\r 
 255     public void Given_doFilter_called_And_FeedId_Throws_Set_Then_Not_Found_Response_Returned () throws Exception{
\r 
 256         setRequestMocking("PUT", "feeds");
\r 
 257         when(request.getPathInfo()).thenReturn("//5");
\r 
 258         cadiFilter.doFilter(request, response, chain);
\r 
 259         verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString());
\r 
 262     private void setRequestMocking(String method, String servletPath)
\r 
 264         when(request.getRemoteAddr()).thenReturn(null);
\r 
 265         when(request.getHeader(BEHALF_HEADER)).thenReturn(null);
\r 
 266         when(request.getAttribute(BaseServlet.CERT_ATTRIBUTE)).thenReturn(null);
\r 
 267         when(request.getMethod()).thenReturn(method);
\r 
 268         when(request.getServletPath()).thenReturn(servletPath);
\r