BUG-8665: fix memory leak around RangeSets
[controller.git] / opendaylight / commons / filter-valve / src / test / java / org / opendaylight / controller / filtervalve / cors / jaxb / ParserTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.filtervalve.cors.jaxb;
10
11 import static org.hamcrest.core.Is.is;
12 import static org.hamcrest.CoreMatchers.containsString;
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertThat;
15 import static org.junit.Assert.assertTrue;
16 import static org.junit.Assert.fail;
17
18 import com.google.common.base.Optional;
19 import java.io.File;
20 import javax.servlet.FilterConfig;
21 import org.apache.commons.io.FileUtils;
22 import org.junit.Test;
23
24 public class ParserTest {
25
26     @Test
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"));
41     }
42
43
44     @Test
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);
49         try {
50             Parser.parse(xmlFileContent, "fileName");
51             fail();
52         }catch(Exception e){
53             assertThat(e.getMessage(), containsString("Cannot find filter for filter-mapping CorsFilter"));
54         }
55     }
56
57     @Test
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);
62         try {
63             Parser.parse(xmlFileContent, "fileName");
64             fail();
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"));
68         }
69     }
70 }