2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2021 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=========================================================
21 package org.onap.policy.pdpx.main.rest;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.mockito.Mockito.lenient;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
28 import javax.servlet.FilterChain;
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.ArgumentCaptor;
35 import org.mockito.Mock;
36 import org.mockito.junit.MockitoJUnitRunner;
38 @RunWith(MockitoJUnitRunner.class)
39 public class TestXacmlPdpServiceFilter {
41 // pick an arbitrary service
42 private static final String PERM_SVC = XacmlPdpServiceFilter.PERMANENT_SERVICES.iterator().next();
45 private HttpServletRequest request;
48 private HttpServletResponse response;
50 private FilterChain filterChain;
52 private XacmlPdpServiceFilter filter;
56 * Initializes the fields.
60 XacmlPdpServiceFilter.disableApi();
62 filterChain = (req, resp) -> {
63 HttpServletResponse resp2 = (HttpServletResponse) resp;
64 resp2.setStatus(HttpServletResponse.SC_OK);
67 filter = new XacmlPdpServiceFilter();
71 public void testDoFilter() throws Exception {
72 XacmlPdpServiceFilter.enableApi();
73 lenient().when(request.getRequestURI()).thenReturn("/other");
74 assertThat(getFilterResponse()).isEqualTo(HttpServletResponse.SC_OK);
78 * Tests doFilter() when the API is disabled, but a permanent service is requested.
81 public void testDoFilter_DisabledPermanentServiceReq() throws Exception {
82 XacmlPdpServiceFilter.disableApi();
83 when(request.getRequestURI()).thenReturn(PERM_SVC);
84 assertThat(getFilterResponse()).isEqualTo(HttpServletResponse.SC_OK);
88 * Tests doFilter() when the API is disabled, but a permanent service is requested, with a leading slash.
91 public void testDoFilter_DisabledPermanentServiceReqLeadingSlash() throws Exception {
92 XacmlPdpServiceFilter.disableApi();
93 when(request.getRequestURI()).thenReturn("/" + PERM_SVC);
94 assertThat(getFilterResponse()).isEqualTo(HttpServletResponse.SC_OK);
98 * Tests doFilter() when the API is disabled, but a permanent service is requested, with extra URI prefix.
101 public void testDoFilter_DisabledPermanentServiceReqExtraUri() throws Exception {
102 XacmlPdpServiceFilter.disableApi();
103 when(request.getRequestURI()).thenReturn("/some/stuff/" + PERM_SVC);
104 assertThat(getFilterResponse()).isEqualTo(HttpServletResponse.SC_OK);
108 * Tests doFilter() when the API is disabled, but a permanent service is requested, with extra characters before
112 public void testDoFilter_DisabledPermanentServiceReqExtraChars() throws Exception {
113 XacmlPdpServiceFilter.disableApi();
114 when(request.getRequestURI()).thenReturn("/ExtraStuff" + PERM_SVC);
115 assertThat(getFilterResponse()).isEqualTo(HttpServletResponse.SC_CONFLICT);
119 * Tests doFilter() when the API is disabled and an API service is requested.
122 public void testDoFilter_DisabledApiReq() throws Exception {
123 XacmlPdpServiceFilter.disableApi();
124 when(request.getRequestURI()).thenReturn("/other");
125 assertThat(getFilterResponse()).isEqualTo(HttpServletResponse.SC_CONFLICT);
129 * Tests doFilter() when the API is disabled and an API service is requested.
132 public void testDoFilter_EnabledApiReq() throws Exception {
133 XacmlPdpServiceFilter.enableApi();
134 lenient().when(request.getRequestURI()).thenReturn("/other");
135 assertThat(getFilterResponse()).isEqualTo(HttpServletResponse.SC_OK);
139 public void testEnableApi_testDisableApi_testIsApiEnabled() {
141 XacmlPdpServiceFilter.enableApi();
142 assertThat(XacmlPdpServiceFilter.isApiEnabled()).isTrue();
144 XacmlPdpServiceFilter.disableApi();
145 assertThat(XacmlPdpServiceFilter.isApiEnabled()).isFalse();
149 * Invokes doFilter().
150 * @return the response code set by the filter
152 private int getFilterResponse() throws Exception {
153 filter.doFilter(request, response, filterChain);
155 // should only be called once
156 var responseCode = ArgumentCaptor.forClass(Integer.class);
157 verify(response).setStatus(responseCode.capture());
159 return responseCode.getValue();