View Javadoc
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.mock.MkQuery;
37  import com.jcabi.http.request.JdkRequest;
38  import java.net.HttpURLConnection;
39  import org.apache.commons.lang3.StringUtils;
40  import org.hamcrest.MatcherAssert;
41  import org.hamcrest.Matchers;
42  import org.junit.Test;
43  
44  /**
45   * Test case for {@link RtLock}.
46   * @author Yegor Bugayenko (yegor@tpc2.com)
47   * @version $Id$
48   * @since 0.12.3
49   */
50  public final class RtLockTest {
51  
52      /**
53       * Front XML.
54       */
55      private static final String XML = StringUtils.join(
56          "<page><links><link rel='lock' href='#lock'/>",
57          "<link rel='unlock' href='#unlock'/></links></page>"
58      );
59  
60      /**
61       * RtLock can lock.
62       * @throws Exception If some problem inside
63       */
64      @Test
65      public void locksViaHttp() throws Exception {
66          final MkContainer container = new MkGrizzlyContainer()
67              .next(new MkAnswer.Simple(RtLockTest.XML))
68              .next(new MkAnswer.Simple(HttpURLConnection.HTTP_SEE_OTHER, ""))
69              .start();
70          final Lock lock = new RtLock("foo", new JdkRequest(container.home()));
71          try {
72              MatcherAssert.assertThat(lock.lock(""), Matchers.equalTo(true));
73          } finally {
74              container.stop();
75          }
76          MatcherAssert.assertThat(
77              container.take().method(),
78              Matchers.equalTo(Request.GET)
79          );
80          final MkQuery query = container.take();
81          MatcherAssert.assertThat(query.method(), Matchers.is(Request.POST));
82          MatcherAssert.assertThat(
83              query.body(), Matchers.containsString("name=foo")
84          );
85      }
86  
87      /**
88       * RtLock can unlock.
89       * @throws Exception If some problem inside
90       */
91      @Test
92      public void unlocksViaHttp() throws Exception {
93          final MkContainer container = new MkGrizzlyContainer()
94              .next(new MkAnswer.Simple(RtLockTest.XML))
95              .next(new MkAnswer.Simple(HttpURLConnection.HTTP_SEE_OTHER, ""))
96              .start();
97          final Lock lock = new RtLock("", new JdkRequest(container.home()));
98          try {
99              lock.unlock("");
100         } finally {
101             container.stop();
102         }
103         MatcherAssert.assertThat(
104             container.take().method(),
105             Matchers.equalTo(Request.GET)
106         );
107         MatcherAssert.assertThat(
108             container.take().method(),
109             Matchers.equalTo(Request.GET)
110         );
111     }
112 
113 }