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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.plugins.event.carrier.restserver;
23 import static org.junit.Assert.assertEquals;
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;
38 public class AccessControlFilterTest {
40 private AccessControlFilter acf;
43 private ContainerRequestContext requestContext;
45 private ContainerResponseContext responseContext;
48 public void beforeEach() {
49 MockitoAnnotations.initMocks(this);
50 acf = new AccessControlFilter();
54 public void filterAddToExisting() throws IOException {
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);
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");
71 acf.filter(requestContext, responseContext);
72 final MultivaluedMap<String, Object> actual = responseContext.getHeaders();
74 assertEquals(expected, actual);