1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
44
45
46
47
48 public final class RtCounterTest {
49
50
51
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
62
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
88
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 }