2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2023 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.pdpx.main.rest;
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.mockito.Mockito.lenient;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
29 import jakarta.servlet.FilterChain;
30 import jakarta.servlet.http.HttpServletRequest;
31 import jakarta.servlet.http.HttpServletResponse;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.ArgumentCaptor;
36 import org.mockito.Mock;
37 import org.mockito.junit.MockitoJUnitRunner;
39 @RunWith(MockitoJUnitRunner.class)
40 public class TestXacmlPdpServiceFilter {
42 // pick an arbitrary service
43 private static final String PERM_SVC = XacmlPdpServiceFilter.PERMANENT_SERVICES.iterator().next();
46 private HttpServletRequest request;
49 private HttpServletResponse response;
51 private FilterChain filterChain;
53 private XacmlPdpServiceFilter filter;
57 * Initializes the fields.
61 XacmlPdpServiceFilter.disableApi();
63 filterChain = (req, resp) -> {
64 HttpServletResponse resp2 = (HttpServletResponse) resp;
65 resp2.setStatus(HttpServletResponse.SC_OK);
68 filter = new XacmlPdpServiceFilter();
72 public void testDoFilter() throws Exception {
73 XacmlPdpServiceFilter.enableApi();
74 lenient().when(request.getRequestURI()).thenReturn("/other");
75 assertThat(getFilterResponse()).isEqualTo(HttpServletResponse.SC_OK);
79 * Tests doFilter() when the API is disabled, but a permanent service is requested.
82 public void testDoFilter_DisabledPermanentServiceReq() throws Exception {
83 XacmlPdpServiceFilter.disableApi();
84 when(request.getRequestURI()).thenReturn(PERM_SVC);
85 assertThat(getFilterResponse()).isEqualTo(HttpServletResponse.SC_OK);
89 * Tests doFilter() when the API is disabled, but a permanent service is requested, with a leading slash.
92 public void testDoFilter_DisabledPermanentServiceReqLeadingSlash() throws Exception {
93 XacmlPdpServiceFilter.disableApi();
94 when(request.getRequestURI()).thenReturn("/" + PERM_SVC);
95 assertThat(getFilterResponse()).isEqualTo(HttpServletResponse.SC_OK);
99 * Tests doFilter() when the API is disabled, but a permanent service is requested, with extra URI prefix.
102 public void testDoFilter_DisabledPermanentServiceReqExtraUri() throws Exception {
103 XacmlPdpServiceFilter.disableApi();
104 when(request.getRequestURI()).thenReturn("/some/stuff/" + PERM_SVC);
105 assertThat(getFilterResponse()).isEqualTo(HttpServletResponse.SC_OK);
109 * Tests doFilter() when the API is disabled, but a permanent service is requested, with extra characters before
113 public void testDoFilter_DisabledPermanentServiceReqExtraChars() throws Exception {
114 XacmlPdpServiceFilter.disableApi();
115 when(request.getRequestURI()).thenReturn("/ExtraStuff" + PERM_SVC);
116 assertThat(getFilterResponse()).isEqualTo(HttpServletResponse.SC_CONFLICT);
120 * Tests doFilter() when the API is disabled and an API service is requested.
123 public void testDoFilter_DisabledApiReq() throws Exception {
124 XacmlPdpServiceFilter.disableApi();
125 when(request.getRequestURI()).thenReturn("/other");
126 assertThat(getFilterResponse()).isEqualTo(HttpServletResponse.SC_CONFLICT);
130 * Tests doFilter() when the API is disabled and an API service is requested.
133 public void testDoFilter_EnabledApiReq() throws Exception {
134 XacmlPdpServiceFilter.enableApi();
135 lenient().when(request.getRequestURI()).thenReturn("/other");
136 assertThat(getFilterResponse()).isEqualTo(HttpServletResponse.SC_OK);
140 public void testEnableApi_testDisableApi_testIsApiEnabled() {
142 XacmlPdpServiceFilter.enableApi();
143 assertThat(XacmlPdpServiceFilter.isApiEnabled()).isTrue();
145 XacmlPdpServiceFilter.disableApi();
146 assertThat(XacmlPdpServiceFilter.isApiEnabled()).isFalse();
150 * Invokes doFilter().
151 * @return the response code set by the filter
153 private int getFilterResponse() throws Exception {
154 filter.doFilter(request, response, filterChain);
156 // should only be called once
157 var responseCode = ArgumentCaptor.forClass(Integer.class);
158 verify(response).setStatus(responseCode.capture());
160 return responseCode.getValue();