Document and validate web-api constructs
[aaa.git] / web / api / src / test / java / org / opendaylight / aaa / web / WebContextApiTest.java
1 /*
2  * Copyright (c) 2018 Red Hat, 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 package org.opendaylight.aaa.web;
9
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;
17
18 import java.util.Map;
19 import javax.servlet.Filter;
20 import javax.servlet.Servlet;
21 import javax.servlet.ServletContextListener;
22 import org.junit.Test;
23
24 /**
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.
28  *
29  * @author Michael Vorburger.ch
30  */
31 public class WebContextApiTest {
32     @Test
33     public void testEmptyBuilder() {
34         final var builder = WebContext.builder();
35         assertThrows(IllegalStateException.class, builder::build);
36     }
37
38     @Test
39     public void testMinimalBuilder() {
40         assertTrue(WebContext.builder().contextPath("/test").build().supportsSessions());
41         assertEquals("/test", WebContext.builder().contextPath("/test").supportsSessions(false).build().contextPath());
42     }
43
44     @Test
45     public void testAddSimpleServlet() {
46         WebContext webContext = WebContext.builder().contextPath("/test")
47                 .addServlet(ServletDetails.builder().servlet(mock(Servlet.class)).addUrlPattern("/test").build())
48                 .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());
53     }
54
55     @Test
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())
59                 .build();
60     }
61
62     @Test
63     public void testAddFilter() {
64         WebContext.builder().contextPath("/test")
65             .addFilter(FilterDetails.builder().filter(mock(Filter.class)).addUrlPattern("/test").build()).build();
66     }
67
68     @Test
69     public void testAddListener() {
70         assertThat(WebContext.builder().contextPath("/test").addListener(mock(ServletContextListener.class)).build()
71                 .listeners(), hasSize(1));
72     }
73
74     @Test
75     public void testContextParam() {
76         assertEquals(Map.of("key", "value"),
77             WebContext.builder().contextPath("/test").putContextParam("key", "value").build().contextParams());
78     }
79
80     @Test
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/");
87     }
88
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());
93     }
94
95 }