2 * Copyright (c) 2018 Red Hat, 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
8 package org.opendaylight.aaa.web;
10 import static org.hamcrest.CoreMatchers.startsWith;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.hamcrest.Matchers.hasSize;
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertThrows;
15 import static org.junit.Assert.assertTrue;
16 import static org.mockito.Mockito.mock;
19 import javax.servlet.Filter;
20 import javax.servlet.Servlet;
21 import javax.servlet.ServletContextListener;
22 import org.junit.Test;
25 * Tests for Web Server {@link WebContext} API. These tests don't test an
26 * {@link WebServer} implementation; the purpose is just to compile time check
27 * against the API signatures, notably incl. their generated code.
29 * @author Michael Vorburger.ch
31 public class WebContextApiTest {
33 public void testEmptyBuilder() {
34 final var builder = WebContext.builder();
35 assertThrows(IllegalStateException.class, builder::build);
39 public void testMinimalBuilder() {
40 assertTrue(WebContext.builder().contextPath("/test").build().supportsSessions());
41 assertEquals("/test", WebContext.builder().contextPath("/test").supportsSessions(false).build().contextPath());
45 public void testAddSimpleServlet() {
46 WebContext webContext = WebContext.builder().contextPath("/test")
47 .addServlet(ServletDetails.builder().servlet(mock(Servlet.class)).addUrlPattern("/test").build())
49 assertThat(webContext.servlets(), hasSize(1));
50 ServletDetails firstServletDetail = webContext.servlets().get(0);
51 assertThat(firstServletDetail.name(), startsWith("org.mockito.codegen.Servlet$MockitoMock$"));
52 assertEquals(Map.of(), firstServletDetail.initParams());
56 public void testAddFullServlet() {
57 WebContext.builder().contextPath("/test").addServlet(ServletDetails.builder().servlet(mock(Servlet.class))
58 .addUrlPattern("/test").addUrlPattern("/another").name("custom").putInitParam("key", "value").build())
63 public void testAddFilter() {
64 WebContext.builder().contextPath("/test")
65 .addFilter(FilterDetails.builder().filter(mock(Filter.class)).addUrlPattern("/test").build()).build();
69 public void testAddListener() {
70 assertThat(WebContext.builder().contextPath("/test").addListener(mock(ServletContextListener.class)).build()
71 .listeners(), hasSize(1));
75 public void testContextParam() {
76 assertEquals(Map.of("key", "value"),
77 WebContext.builder().contextPath("/test").putContextParam("key", "value").build().contextParams());
81 public void testBadContextPath() {
82 assertBadContextPath("Context path is empty", "");
83 assertBadContextPath("Context path 'test/sub' does not start with '/'", "test/sub");
84 assertBadContextPath("Context path 'test space' does not start with '/'", "test space");
85 assertBadContextPath("Context path 'test/' does not start with '/'", "test/");
86 assertBadContextPath("Context path '/test/' ends with '/'", "/test/");
89 private static void assertBadContextPath(final String expectedMessage, final String contextPath) {
90 final var builder = WebContext.builder();
91 final var ex = assertThrows(IllegalArgumentException.class, () -> builder.contextPath(contextPath));
92 assertEquals(expectedMessage, ex.getMessage());