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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.plugins.event.carrier.restserver;
24 import static org.junit.Assert.assertEquals;
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;
40 @RunWith(MockitoJUnitRunner.class)
41 public class AccessControlFilterTest {
43 private AccessControlFilter acf;
46 private ContainerRequestContext requestContext;
48 private ContainerResponseContext responseContext;
51 public void beforeEach() {
52 acf = new AccessControlFilter();
56 public void filterAddToExisting() throws IOException {
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);
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");
73 acf.filter(requestContext, responseContext);
74 final MultivaluedMap<String, Object> actual = responseContext.getHeaders();
76 assertEquals(expected, actual);