View Javadoc
1   /**
2    * Copyright (c) 2013-2022, jcabi.com
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 jcabi.com 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  
31  package com.jcabi.github;
32  
33  import com.jcabi.github.mock.MkGithub;
34  import com.jcabi.http.Request;
35  import com.jcabi.http.mock.MkAnswer;
36  import com.jcabi.http.mock.MkContainer;
37  import com.jcabi.http.mock.MkGrizzlyContainer;
38  import com.jcabi.http.request.ApacheRequest;
39  import java.net.HttpURLConnection;
40  import javax.json.Json;
41  import javax.json.JsonObject;
42  import org.hamcrest.MatcherAssert;
43  import org.hamcrest.Matchers;
44  import org.junit.Rule;
45  import org.junit.Test;
46  
47  /**
48   * Testcase for RtTags.
49   * @author Mihai Andronache (amihaiemil@gmail.com)
50   * @version $Id: 8b4f61d66522859b02b4740d75c6fb9344e6a9bf $
51   * @checkstyle MultipleStringLiterals (500 lines)
52   */
53  public final class RtTagsTest {
54  
55      /**
56       * The rule for skipping test if there's BindException.
57       * @checkstyle VisibilityModifierCheck (3 lines)
58       */
59      @Rule
60      public final transient RandomPort resource = new RandomPort();
61  
62      /**
63       * RtTags can create a tag.
64       * @throws Exception - If something goes wrong.
65       * @checkstyle IndentationCheck (20 lines)
66       */
67      @Test
68      public void createsTag() throws Exception {
69          final MkContainer container = new MkGrizzlyContainer().next(
70              new MkAnswer.Simple(
71                  HttpURLConnection.HTTP_CREATED,
72                  "{\"sha\":\"0abcd89jcabitest\",\"tag\":\"v.0.1\"}"
73              )
74          ).next(
75              new MkAnswer.Simple(
76                  HttpURLConnection.HTTP_CREATED,
77                  "{\"ref\":\"refs/heads/feature-a\"}"
78              )
79          ).start(this.resource.port());
80          final Tags tags = new RtTags(
81              new ApacheRequest(container.home()),
82              new MkGithub().randomRepo()
83          );
84          final JsonObject tagger = Json.createObjectBuilder()
85              .add("name", "Scott").add("email", "scott@gmail.com")
86              .add("date", "2011-06-17T14:53:35-07:00").build();
87          final JsonObject input = Json.createObjectBuilder()
88              .add("tag", "v.0.1").add("message", "initial version")
89              .add("object", "07cd4r45Test444").add("type", "commit")
90              .add("tagger", tagger).build();
91          try {
92              MatcherAssert.assertThat(
93                  tags.create(input),
94                  Matchers.instanceOf(Tag.class)
95              );
96              MatcherAssert.assertThat(
97                  container.take().method(),
98                  Matchers.equalTo(Request.POST)
99              );
100             MatcherAssert.assertThat(
101                 container.take().method(),
102                 Matchers.equalTo(Request.POST)
103             );
104         } finally {
105             container.stop();
106         }
107     }
108 }