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