aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/test/java/org/onap/policy/pdpx/main/rest/TestXacmlPdpServiceFilter.java
blob: 9f098f78176a429db92ba4a9bf2159fe97070c8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*-
 * ============LICENSE_START=======================================================
 * ONAP
 * ================================================================================
 * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.pdpx.main.rest;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import javax.servlet.FilterChain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class TestXacmlPdpServiceFilter {

    // pick an arbitrary service
    private static final String PERM_SVC = XacmlPdpServiceFilter.PERMANENT_SERVICES.iterator().next();

    @Mock
    private HttpServletRequest request;

    @Mock
    private HttpServletResponse response;

    private FilterChain filterChain;

    private XacmlPdpServiceFilter filter;


    /**
     * Initializes the fields.
     */
    @Before
    public void setUp() {
        XacmlPdpServiceFilter.disableApi();

        filterChain = (req, resp) -> {
            HttpServletResponse resp2 = (HttpServletResponse) resp;
            resp2.setStatus(HttpServletResponse.SC_OK);
        };

        filter = new XacmlPdpServiceFilter();
    }

    @Test
    public void testDoFilter() throws Exception {
        XacmlPdpServiceFilter.enableApi();
        lenient().when(request.getRequestURI()).thenReturn("/other");
        assertThat(getFilterResponse()).isEqualTo(HttpServletResponse.SC_OK);
    }

    /**
     * Tests doFilter() when the API is disabled, but a permanent service is requested.
     */
    @Test
    public void testDoFilter_DisabledPermanentServiceReq() throws Exception {
        XacmlPdpServiceFilter.disableApi();
        when(request.getRequestURI()).thenReturn(PERM_SVC);
        assertThat(getFilterResponse()).isEqualTo(HttpServletResponse.SC_OK);
    }

    /**
     * Tests doFilter() when the API is disabled, but a permanent service is requested, with a leading slash.
     */
    @Test
    public void testDoFilter_DisabledPermanentServiceReqLeadingSlash() throws Exception {
        XacmlPdpServiceFilter.disableApi();
        when(request.getRequestURI()).thenReturn("/" + PERM_SVC);
        assertThat(getFilterResponse()).isEqualTo(HttpServletResponse.SC_OK);
    }

    /**
     * Tests doFilter() when the API is disabled, but a permanent service is requested, with extra URI prefix.
     */
    @Test
    public void testDoFilter_DisabledPermanentServiceReqExtraUri() throws Exception {
        XacmlPdpServiceFilter.disableApi();
        when(request.getRequestURI()).thenReturn("/some/stuff/" + PERM_SVC);
        assertThat(getFilterResponse()).isEqualTo(HttpServletResponse.SC_OK);
    }

    /**
     * Tests doFilter() when the API is disabled, but a permanent service is requested, with extra characters before
     * the service name.
     */
    @Test
    public void testDoFilter_DisabledPermanentServiceReqExtraChars() throws Exception {
        XacmlPdpServiceFilter.disableApi();
        when(request.getRequestURI()).thenReturn("/ExtraStuff" + PERM_SVC);
        assertThat(getFilterResponse()).isEqualTo(HttpServletResponse.SC_CONFLICT);
    }

    /**
     * Tests doFilter() when the API is disabled and an API service is requested.
     */
    @Test
    public void testDoFilter_DisabledApiReq() throws Exception {
        XacmlPdpServiceFilter.disableApi();
        when(request.getRequestURI()).thenReturn("/other");
        assertThat(getFilterResponse()).isEqualTo(HttpServletResponse.SC_CONFLICT);
    }

    /**
     * Tests doFilter() when the API is disabled and an API service is requested.
     */
    @Test
    public void testDoFilter_EnabledApiReq() throws Exception {
        XacmlPdpServiceFilter.enableApi();
        lenient().when(request.getRequestURI()).thenReturn("/other");
        assertThat(getFilterResponse()).isEqualTo(HttpServletResponse.SC_OK);
    }

    @Test
    public void testEnableApi_testDisableApi_testIsApiEnabled() {

        XacmlPdpServiceFilter.enableApi();
        assertThat(XacmlPdpServiceFilter.isApiEnabled()).isTrue();

        XacmlPdpServiceFilter.disableApi();
        assertThat(XacmlPdpServiceFilter.isApiEnabled()).isFalse();
    }

    /**
     * Invokes doFilter().
     * @return the response code set by the filter
     */
    private int getFilterResponse()  throws Exception {
        filter.doFilter(request, response, filterChain);

        // should only be called once
        var responseCode = ArgumentCaptor.forClass(Integer.class);
        verify(response).setStatus(responseCode.capture());

        return responseCode.getValue();
    }
}