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.jcabi.github.mock.MkGithub;
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.ApacheRequest;
37 import java.io.IOException;
38 import java.net.HttpURLConnection;
39 import javax.json.Json;
40 import javax.json.JsonObject;
41 import org.hamcrest.MatcherAssert;
42 import org.hamcrest.Matchers;
43 import org.junit.Rule;
44 import org.junit.Test;
45
46 /**
47 * Test case for {@link RtFork}.
48 *
49 * @author Andrej Istomin (andrej.istomin.ikeen@gmail.com)
50 * @version $Id: a7f335dea1ba6cb9a5d85b9cc875da09f8d66881 $
51 * @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
52 */
53 public final class RtForkTest {
54 /**
55 * The rule for skipping test if there's BindException.
56 * @checkstyle VisibilityModifierCheck (3 lines)
57 */
58 @Rule
59 public final transient RandomPort resource = new RandomPort();
60
61 /**
62 * RtFork can patch comment and return new json.
63 * @throws IOException if has some problems with json parsing.
64 */
65 @Test
66 public void patchAndCheckJsonFork() throws IOException {
67 final String original = "some organization";
68 final String patched = "some patched organization";
69 try (
70 final MkContainer container =
71 new MkGrizzlyContainer().next(this.answer(original))
72 .next(
73 this.answer(patched)
74 ).next(this.answer(original)).start(
75 this.resource.port()
76 );
77 final MkContainer forksContainer = new MkGrizzlyContainer().start(
78 this.resource.port()
79 )) {
80 final RtRepo repo =
81 new RtRepo(
82 new MkGithub(),
83 new ApacheRequest(forksContainer.home()),
84 new Coordinates.Simple("test_user", "test_repo")
85 );
86 final RtFork fork = new RtFork(
87 new ApacheRequest(container.home()), repo, 1
88 );
89 fork.patch(RtForkTest.fork(patched));
90 MatcherAssert.assertThat(
91 new Fork.Smart(fork).organization(),
92 Matchers.equalTo(patched)
93 );
94 MatcherAssert.assertThat(
95 new Fork.Smart(fork).name(),
96 Matchers.notNullValue()
97 );
98 }
99 }
100
101 /**
102 * Create and return success MkAnswer object to test.
103 * @param organization The organization of the fork
104 * @return Success MkAnswer
105 */
106 private MkAnswer.Simple answer(final String organization) {
107 return new MkAnswer.Simple(
108 HttpURLConnection.HTTP_OK,
109 RtForkTest.fork(organization).toString()
110 );
111 }
112
113 /**
114 * Create and return JsonObject to test.
115 * @param organization The organization of the fork
116 * @return JsonObject
117 */
118 private static JsonObject fork(final String organization) {
119 return Json.createObjectBuilder()
120 .add("organization", organization)
121 .add("name", "nm")
122 .build();
123 }
124
125 }