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