View Javadoc
1   /**
2    * Copyright (c) 2014, stateful.co
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met: 1) Redistributions of source code must retain the above
8    * copyright notice, this list of conditions and the following
9    * disclaimer. 2) Redistributions in binary form must reproduce the above
10   * copyright notice, this list of conditions and the following
11   * disclaimer in the documentation and/or other materials provided
12   * with the distribution. 3) Neither the name of the stateful.co nor
13   * the names of its contributors may be used to endorse or promote
14   * products derived from this software without specific prior written
15   * permission.
16   *
17   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
19   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28   * OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  package co.stateful.rest;
31  
32  import com.jcabi.http.request.JdkRequest;
33  import com.jcabi.http.response.RestResponse;
34  import com.jcabi.http.response.XmlResponse;
35  import com.jcabi.matchers.NoBrokenLinks;
36  import com.jcabi.matchers.W3CMatchers;
37  import java.net.HttpURLConnection;
38  import java.net.URI;
39  import javax.ws.rs.core.HttpHeaders;
40  import javax.ws.rs.core.MediaType;
41  import org.junit.Test;
42  
43  /**
44   * Integration case for {@link IndexRs}.
45   * @author Yegor Bugayenko (yegor@tpc2.com)
46   * @version $Id$
47   */
48  @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
49  public final class IndexRsITCase {
50  
51      /**
52       * Tomcat home.
53       */
54      private static final String HOME = System.getProperty("tomcat.home");
55  
56      /**
57       * IndexRs can hit public pages.
58       * @throws Exception If some problem inside
59       */
60      @Test
61      public void hitsPublicPages() throws Exception {
62          final String[] pages = {
63              "/",
64              "/robots.txt",
65              "/xsl/layout.xsl",
66              "/xsl/index.xsl",
67              "/css/style.css",
68          };
69          for (final String page : pages) {
70              new JdkRequest(IndexRsITCase.HOME)
71                  .uri().path(page).back()
72                  .header("accept", "text/plain")
73                  .fetch()
74                  .as(RestResponse.class)
75                  .assertStatus(HttpURLConnection.HTTP_OK);
76          }
77      }
78  
79      /**
80       * IndexRs can mandatory page elements.
81       * @throws Exception If some problem inside
82       */
83      @Test
84      public void rendersPageElements() throws Exception {
85          new JdkRequest(IndexRsITCase.HOME)
86              .header(HttpHeaders.ACCEPT, MediaType.APPLICATION_XML)
87              .fetch()
88              .as(RestResponse.class)
89              .assertStatus(HttpURLConnection.HTTP_OK)
90              .assertThat(new NoBrokenLinks(URI.create(IndexRsITCase.HOME)))
91              .as(XmlResponse.class)
92              .assertXPath("/page/version")
93              .assertXPath("/page/documentation")
94              .assertXPath("/page/millis");
95      }
96  
97      /**
98       * IndexRs can render exception.
99       * @throws Exception If some problem inside
100      */
101     @Test
102     public void rendersException() throws Exception {
103         new JdkRequest(IndexRsITCase.HOME)
104             .uri().path("/trap").back()
105             .fetch()
106             .as(RestResponse.class)
107             .assertStatus(HttpURLConnection.HTTP_OK)
108             .as(XmlResponse.class)
109             .assertXPath("//xhtml:title[.='Internal application error']");
110     }
111 
112     /**
113      * IndexRs can render valid HTML.
114      * @throws Exception If some problem inside
115      */
116     @Test
117     public void rendersValidHtml() throws Exception {
118         new JdkRequest(IndexRsITCase.HOME)
119             .header(HttpHeaders.ACCEPT, MediaType.TEXT_XML)
120             .fetch()
121             .as(XmlResponse.class)
122             .rel("/page/links/link[@rel='menu:counters']/@href")
123             .header(HttpHeaders.ACCEPT, MediaType.TEXT_HTML)
124             .fetch()
125             .as(RestResponse.class)
126             .assertStatus(HttpURLConnection.HTTP_OK)
127             .assertBody(W3CMatchers.validHtml());
128     }
129 
130 }