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;
33 import com.jcabi.http.request.JdkRequest;
34 import com.jcabi.http.response.RestResponse;
35 import com.jcabi.http.response.XmlResponse;
36 import com.jcabi.matchers.W3CMatchers;
37 import java.net.HttpURLConnection;
38 import javax.ws.rs.core.HttpHeaders;
39 import javax.ws.rs.core.MediaType;
40 import org.junit.Test;
41
42 /**
43 * Integration case for {@link CountersRs}.
44 * @author Yegor Bugayenko (yegor@tpc2.com)
45 * @version $Id$
46 * @checkstyle MultipleStringLiteralsCheck (500 lines)
47 */
48 public final class CountersRsITCase {
49
50 /**
51 * Tomcat home.
52 */
53 private static final String HOME = System.getProperty("tomcat.home");
54
55 /**
56 * CountersRs can list counters.
57 * @throws Exception If some problem inside
58 */
59 @Test
60 public void listsCounters() throws Exception {
61 new JdkRequest(CountersRsITCase.HOME)
62 .header(HttpHeaders.ACCEPT, MediaType.TEXT_XML)
63 .fetch()
64 .as(XmlResponse.class)
65 .rel("/page/links/link[@rel='menu:counters']/@href")
66 .header(HttpHeaders.ACCEPT, MediaType.TEXT_XML)
67 .fetch()
68 .as(RestResponse.class)
69 .assertStatus(HttpURLConnection.HTTP_OK)
70 .as(XmlResponse.class)
71 .rel("/page/links/link[@rel='add']/@href")
72 .body().formParam("name", "foo-15").back()
73 .header(HttpHeaders.ACCEPT, MediaType.TEXT_XML)
74 .method(Request.POST)
75 .fetch()
76 .as(RestResponse.class)
77 .assertStatus(HttpURLConnection.HTTP_SEE_OTHER)
78 .follow()
79 .header(HttpHeaders.ACCEPT, MediaType.TEXT_XML)
80 .method(Request.GET)
81 .fetch()
82 .as(XmlResponse.class)
83 .assertXPath("/page/token")
84 .assertXPath("/page/flash")
85 .assertXPath("/page/counters/counter[name='foo-15']");
86 }
87
88 /**
89 * CountersRs can render valid HTML.
90 * @throws Exception If some problem inside
91 */
92 @Test
93 public void rendersValidHtml() throws Exception {
94 new JdkRequest(CountersRsITCase.HOME)
95 .header(HttpHeaders.ACCEPT, MediaType.TEXT_XML)
96 .fetch()
97 .as(XmlResponse.class)
98 .rel("/page/links/link[@rel='menu:counters']/@href")
99 .header(HttpHeaders.ACCEPT, MediaType.TEXT_HTML)
100 .fetch()
101 .as(RestResponse.class)
102 .assertStatus(HttpURLConnection.HTTP_OK)
103 .assertBody(W3CMatchers.validHtml());
104 }
105
106 }