1 /**
2 * Copyright (c) 2014-2016, 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;
31
32 import com.jcabi.http.Request;
33 import com.jcabi.http.mock.MkAnswer;
34 import com.jcabi.http.mock.MkContainer;
35 import com.jcabi.http.mock.MkGrizzlyContainer;
36 import com.jcabi.http.request.JdkRequest;
37 import org.apache.commons.lang3.StringUtils;
38 import org.hamcrest.MatcherAssert;
39 import org.hamcrest.Matchers;
40 import org.junit.Test;
41
42 /**
43 * Test case for {@link RtCounter}.
44 * @author Yegor Bugayenko (yegor@tpc2.com)
45 * @version $Id$
46 * @since 0.12.3
47 */
48 public final class RtCounterTest {
49
50 /**
51 * Front XML.
52 */
53 private static final String XML = StringUtils.join(
54 "<page><counters><counter><name></name><links>",
55 "<link rel='set' href='#set'/>",
56 "<link rel='increment' href='#increment'/></links>",
57 "</counter></counters></page>"
58 );
59
60 /**
61 * RtCounter can set.
62 * @throws Exception If some problem inside
63 */
64 @Test
65 public void setsViaHttp() throws Exception {
66 final MkContainer container = new MkGrizzlyContainer()
67 .next(new MkAnswer.Simple(RtCounterTest.XML))
68 .next(new MkAnswer.Simple(""))
69 .start();
70 final Counter cnt = new RtCounter("", new JdkRequest(container.home()));
71 try {
72 cnt.set(1L);
73 } finally {
74 container.stop();
75 }
76 MatcherAssert.assertThat(
77 container.take().method(),
78 Matchers.equalTo(Request.GET)
79 );
80 MatcherAssert.assertThat(
81 container.take().method(),
82 Matchers.equalTo(Request.PUT)
83 );
84 }
85
86 /**
87 * RtCounter can increment.
88 * @throws Exception If some problem inside
89 */
90 @Test
91 public void incrementsViaHttp() throws Exception {
92 final MkContainer container = new MkGrizzlyContainer()
93 .next(new MkAnswer.Simple(RtCounterTest.XML))
94 .next(new MkAnswer.Simple("1"))
95 .start();
96 final Counter cnt = new RtCounter("", new JdkRequest(container.home()));
97 try {
98 MatcherAssert.assertThat(
99 cnt.incrementAndGet(1L), Matchers.equalTo(1L)
100 );
101 } finally {
102 container.stop();
103 }
104 MatcherAssert.assertThat(
105 container.take().method(),
106 Matchers.equalTo(Request.GET)
107 );
108 MatcherAssert.assertThat(
109 container.take().method(),
110 Matchers.equalTo(Request.GET)
111 );
112 }
113
114 }