3efe054ad416bfacebd9dc3c2ac35e5d7646db39
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.plugins.event.carrier.restserver;
22
23 import static org.junit.Assert.assertEquals;
24
25 import java.io.IOException;
26 import javax.ws.rs.container.ContainerRequestContext;
27 import javax.ws.rs.container.ContainerResponseContext;
28 import javax.ws.rs.core.MultivaluedHashMap;
29 import javax.ws.rs.core.MultivaluedMap;
30 import org.apache.commons.lang3.RandomStringUtils;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.mockito.Mock;
34 import org.mockito.Mockito;
35 import org.mockito.MockitoAnnotations;
36
37
38 public class AccessControlFilterTest {
39
40     private AccessControlFilter acf;
41
42     @Mock
43     private ContainerRequestContext requestContext;
44     @Mock
45     private ContainerResponseContext responseContext;
46
47     @Before
48     public void beforeEach() {
49         MockitoAnnotations.initMocks(this);
50         acf = new AccessControlFilter();
51     }
52
53     @Test
54     public void filterAddToExisting() throws IOException {
55         // prepare mocks
56         final String origin = RandomStringUtils.randomAlphanumeric(14, 16);
57         final MultivaluedHashMap<String, Object> map = new MultivaluedHashMap<>();
58         Mockito.when(requestContext.getHeaderString("Origin")).thenReturn(origin);
59         Mockito.when(responseContext.getHeaders()).thenReturn(map);
60
61         // prepare expected
62         final MultivaluedMap<String, Object> expected = new MultivaluedHashMap<>();
63         expected.add("Access-Control-Allow-Origin", origin);
64         expected.add("Access-Control-Expose-Headers", "Content-Type, Accept, Allow");
65         expected.add("Access-Control-Allow-Headers", "Origin, Content-Type, Accept");
66         expected.add("Access-Control-Allow-Credentials", "true");
67         expected.add("Access-Control-Allow-Methods", "OPTIONS, GET, POST, PUT");
68
69
70         // test
71         acf.filter(requestContext, responseContext);
72         final MultivaluedMap<String, Object> actual = responseContext.getHeaders();
73
74         assertEquals(expected, actual);
75     }
76 }