View Javadoc
1   /**
2    * Copyright (c) 2013-2023, 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  package com.jcabi.github;
31  
32  import com.google.common.base.Optional;
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.mock.MkQuery;
39  import com.jcabi.http.request.ApacheRequest;
40  import com.jcabi.http.request.FakeRequest;
41  import java.io.IOException;
42  import java.io.StringReader;
43  import java.net.HttpURLConnection;
44  import javax.json.Json;
45  import javax.json.JsonObject;
46  import org.hamcrest.MatcherAssert;
47  import org.hamcrest.Matchers;
48  import org.junit.Rule;
49  import org.junit.Test;
50  
51  /**
52   * Testcase for {@link RtStatuses}.
53   *
54   * @author Marcin Cylke (marcin.cylke+github@gmail.com)
55   * @version $Id: 1eb971426bc151430cd18d04645373a21515e3b3 $
56   * @checkstyle MultipleStringLiterals (500 lines)
57   * @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
58   * @todo #1130:30min Write RtStatusesITCase, an integration test case for
59   *  RtStatuses/RtStatus against real GitHub commit status data.
60   * @todo #1490:30min Continue to close grizzle servers open on tests. Use
61   *  try-with-resource statement instead of try-catch whenever is possible.
62   */
63  public final class RtStatusesTest {
64      /**
65       * The rule for skipping test if there's BindException.
66       * @checkstyle VisibilityModifierCheck (3 lines)
67       */
68      @Rule
69      public final transient RandomPort resource = new RandomPort();
70  
71      /**
72       * RtStatuses can fetch its commit.
73       * @throws IOException If there is an I/O problem.
74       */
75      @Test
76      public void fetchesCommit() throws IOException {
77          final Commit original = new MkGithub().randomRepo().git()
78              .commits().get("5e8d65e0dbfab0716db16493e03a0baba480625a");
79          MatcherAssert.assertThat(
80              new RtStatuses(new FakeRequest(), original).commit(),
81              Matchers.equalTo(original)
82          );
83      }
84  
85      /**
86       * Tests creating a Status.
87       *
88       * @throws Exception when an Error occurs
89       */
90      @Test
91      public void createsStatus() throws Exception {
92          final String stateprop = "state";
93          final String urlprop = "target_url";
94          final String descriptionprop = "description";
95          final String contextprop = "context";
96          final String url = "https://ci.example.com/1000/output";
97          final String description = "Build has completed successfully";
98          final String context = "continuous-integration/jenkins";
99          final MkContainer container = new MkGrizzlyContainer().next(
100             new MkAnswer.Simple(
101                 HttpURLConnection.HTTP_CREATED,
102                 Json.createObjectBuilder().add(stateprop, "failure")
103                     .add(urlprop, url)
104                     .add(descriptionprop, description)
105                     .add(contextprop, context)
106                     .build().toString()
107             )
108         ).start(this.resource.port());
109         final Request entry = new ApacheRequest(container.home());
110         final Statuses statuses = new RtStatuses(
111             entry,
112             new RtCommit(
113                 entry,
114                 new MkGithub().randomRepo(),
115                 "0abcd89jcabitest"
116             )
117         );
118         try {
119             statuses.create(
120                 new Statuses.StatusCreate(Status.State.FAILURE)
121                     .withTargetUrl(Optional.of(url))
122                     .withDescription(description)
123                     .withContext(Optional.of(context))
124             );
125             final MkQuery request = container.take();
126             MatcherAssert.assertThat(
127                 request.method(),
128                 Matchers.equalTo(Request.POST)
129             );
130             final JsonObject obj = Json.createReader(
131                 new StringReader(request.body())
132             ).readObject();
133             MatcherAssert.assertThat(
134                 obj.getString(stateprop),
135                 Matchers.equalTo(Status.State.FAILURE.identifier())
136             );
137             MatcherAssert.assertThat(
138                 obj.getString(contextprop),
139                 Matchers.equalTo(context)
140             );
141             MatcherAssert.assertThat(
142                 obj.getString(descriptionprop),
143                 Matchers.equalTo(description)
144             );
145             MatcherAssert.assertThat(
146                 obj.getString(urlprop),
147                 Matchers.equalTo(url)
148             );
149         } finally {
150             container.stop();
151         }
152     }
153 }