2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.filtervalve.cors.jaxb;
11 import static org.hamcrest.core.Is.is;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertThat;
14 import static org.junit.Assert.assertTrue;
15 import static org.junit.Assert.fail;
16 import static org.junit.matchers.JUnitMatchers.containsString;
18 import com.google.common.base.Optional;
20 import javax.servlet.FilterConfig;
21 import org.apache.commons.io.FileUtils;
22 import org.junit.Test;
24 public class ParserTest {
27 public void testParsing() throws Exception {
28 File xmlFile = new File(getClass().getResource("/sample-cors-config.xml").getFile());
29 assertThat(xmlFile.canRead(), is(true));
30 String xmlFileContent = FileUtils.readFileToString(xmlFile);
31 Host host = Parser.parse(xmlFileContent, "fileName");
32 assertEquals(1, host.getContexts().size());
33 // check that MockedFilter has init params merged/replaced
34 Optional<Context> context = host.findContext("/restconf");
35 assertTrue(context.isPresent());
36 assertEquals(1, context.get().getFilters().size());
37 MockedFilter filter = (MockedFilter) context.get().getFilters().get(0).getActualFilter();
38 FilterConfig filterConfig = filter.getFilterConfig();
39 assertEquals("*", filterConfig.getInitParameter("cors.allowed.origins"));
40 assertEquals("11", filterConfig.getInitParameter("cors.preflight.maxage"));
45 public void testParsing_NoFilterDefined() throws Exception {
46 File xmlFile = new File(getClass().getResource("/no-filter-defined.xml").getFile());
47 assertThat(xmlFile.canRead(), is(true));
48 String xmlFileContent = FileUtils.readFileToString(xmlFile);
50 Parser.parse(xmlFileContent, "fileName");
53 assertThat(e.getMessage(), containsString("Cannot find filter for filter-mapping CorsFilter"));
58 public void testConflictingClass() throws Exception {
59 File xmlFile = new File(getClass().getResource("/conflicting-class.xml").getFile());
60 assertThat(xmlFile.canRead(), is(true));
61 String xmlFileContent = FileUtils.readFileToString(xmlFile);
63 Parser.parse(xmlFileContent, "fileName");
65 } catch (RuntimeException e) {
66 assertThat(e.getMessage(), containsString("Error while processing filter CorsFilter of context /restconf"));
67 assertThat(e.getCause().getMessage(), containsString("Conflict detected in template/filter filter-class definitions, filter name: CorsFilter"));