Upgrade Java 17 in xacml-pdp
[policy/xacml-pdp.git] / main / src / test / java / org / onap / policy / pdpx / main / rest / TestXacmlPdpServiceFilter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.pdpx.main.rest;
23
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;
28
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;
38
39 @RunWith(MockitoJUnitRunner.class)
40 public class TestXacmlPdpServiceFilter {
41
42     // pick an arbitrary service
43     private static final String PERM_SVC = XacmlPdpServiceFilter.PERMANENT_SERVICES.iterator().next();
44
45     @Mock
46     private HttpServletRequest request;
47
48     @Mock
49     private HttpServletResponse response;
50
51     private FilterChain filterChain;
52
53     private XacmlPdpServiceFilter filter;
54
55
56     /**
57      * Initializes the fields.
58      */
59     @Before
60     public void setUp() {
61         XacmlPdpServiceFilter.disableApi();
62
63         filterChain = (req, resp) -> {
64             HttpServletResponse resp2 = (HttpServletResponse) resp;
65             resp2.setStatus(HttpServletResponse.SC_OK);
66         };
67
68         filter = new XacmlPdpServiceFilter();
69     }
70
71     @Test
72     public void testDoFilter() throws Exception {
73         XacmlPdpServiceFilter.enableApi();
74         lenient().when(request.getRequestURI()).thenReturn("/other");
75         assertThat(getFilterResponse()).isEqualTo(HttpServletResponse.SC_OK);
76     }
77
78     /**
79      * Tests doFilter() when the API is disabled, but a permanent service is requested.
80      */
81     @Test
82     public void testDoFilter_DisabledPermanentServiceReq() throws Exception {
83         XacmlPdpServiceFilter.disableApi();
84         when(request.getRequestURI()).thenReturn(PERM_SVC);
85         assertThat(getFilterResponse()).isEqualTo(HttpServletResponse.SC_OK);
86     }
87
88     /**
89      * Tests doFilter() when the API is disabled, but a permanent service is requested, with a leading slash.
90      */
91     @Test
92     public void testDoFilter_DisabledPermanentServiceReqLeadingSlash() throws Exception {
93         XacmlPdpServiceFilter.disableApi();
94         when(request.getRequestURI()).thenReturn("/" + PERM_SVC);
95         assertThat(getFilterResponse()).isEqualTo(HttpServletResponse.SC_OK);
96     }
97
98     /**
99      * Tests doFilter() when the API is disabled, but a permanent service is requested, with extra URI prefix.
100      */
101     @Test
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);
106     }
107
108     /**
109      * Tests doFilter() when the API is disabled, but a permanent service is requested, with extra characters before
110      * the service name.
111      */
112     @Test
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);
117     }
118
119     /**
120      * Tests doFilter() when the API is disabled and an API service is requested.
121      */
122     @Test
123     public void testDoFilter_DisabledApiReq() throws Exception {
124         XacmlPdpServiceFilter.disableApi();
125         when(request.getRequestURI()).thenReturn("/other");
126         assertThat(getFilterResponse()).isEqualTo(HttpServletResponse.SC_CONFLICT);
127     }
128
129     /**
130      * Tests doFilter() when the API is disabled and an API service is requested.
131      */
132     @Test
133     public void testDoFilter_EnabledApiReq() throws Exception {
134         XacmlPdpServiceFilter.enableApi();
135         lenient().when(request.getRequestURI()).thenReturn("/other");
136         assertThat(getFilterResponse()).isEqualTo(HttpServletResponse.SC_OK);
137     }
138
139     @Test
140     public void testEnableApi_testDisableApi_testIsApiEnabled() {
141
142         XacmlPdpServiceFilter.enableApi();
143         assertThat(XacmlPdpServiceFilter.isApiEnabled()).isTrue();
144
145         XacmlPdpServiceFilter.disableApi();
146         assertThat(XacmlPdpServiceFilter.isApiEnabled()).isFalse();
147     }
148
149     /**
150      * Invokes doFilter().
151      * @return the response code set by the filter
152      */
153     private int getFilterResponse()  throws Exception {
154         filter.doFilter(request, response, filterChain);
155
156         // should only be called once
157         var responseCode = ArgumentCaptor.forClass(Integer.class);
158         verify(response).setStatus(responseCode.capture());
159
160         return responseCode.getValue();
161     }
162 }