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 com.jcabi.github;
31
32 import com.jcabi.github.mock.MkGithub;
33 import com.jcabi.http.Request;
34 import com.jcabi.http.mock.MkAnswer;
35 import com.jcabi.http.mock.MkContainer;
36 import com.jcabi.http.mock.MkGrizzlyContainer;
37 import com.jcabi.http.request.ApacheRequest;
38 import com.jcabi.http.request.FakeRequest;
39 import java.io.IOException;
40 import java.net.HttpURLConnection;
41 import javax.json.Json;
42 import org.hamcrest.MatcherAssert;
43 import org.hamcrest.Matchers;
44 import org.junit.Rule;
45 import org.junit.Test;
46
47
48
49
50
51
52
53 public class RtGistCommentTest {
54
55
56
57
58
59 @Rule
60 public final transient RandomPort resource = new RandomPort();
61
62
63
64
65
66 @Test
67 public final void patchAndCheckJsonGistComment() throws IOException {
68 final int identifier = 1;
69 final String idString = "id";
70 final String bodyString = "body";
71 final String body = "somebody";
72 final String patchedBody = "some patchedbody";
73 final MkAnswer first = new MkAnswer.Simple(
74 HttpURLConnection.HTTP_OK,
75 Json.createObjectBuilder()
76 .add(bodyString, body)
77 .add(idString, identifier)
78 .build().toString()
79 );
80 final MkAnswer second = new MkAnswer.Simple(
81 HttpURLConnection.HTTP_OK,
82 Json.createObjectBuilder()
83 .add(bodyString, patchedBody)
84 .add(idString, identifier)
85 .build().toString()
86 );
87 final MkAnswer third = new MkAnswer.Simple(
88 HttpURLConnection.HTTP_OK,
89 Json.createObjectBuilder()
90 .add(bodyString, body)
91 .add(idString, identifier)
92 .build().toString()
93 );
94 try (
95 final MkContainer container =
96 new MkGrizzlyContainer().next(first).next(second).next(third)
97 .start(this.resource.port());
98 final MkContainer gistContainer = new MkGrizzlyContainer()
99 .start(this.resource.port())) {
100 final RtGist gist =
101 new RtGist(
102 new MkGithub(),
103 new ApacheRequest(gistContainer.home()), "someName"
104 );
105 final RtGistComment comment = new RtGistComment(
106 new ApacheRequest(container.home()), gist, identifier
107 );
108 comment.patch(Json.createObjectBuilder()
109 .add(bodyString, patchedBody)
110 .add(idString, identifier)
111 .build()
112 );
113 MatcherAssert.assertThat(
114 comment.json().getString(bodyString),
115 Matchers.equalTo(patchedBody)
116 );
117 container.stop();
118 gistContainer.stop();
119 }
120 }
121
122
123
124
125
126 @Test
127 public final void removeGistComment() throws IOException {
128 final int identifier = 1;
129 try (
130 final MkContainer container = new MkGrizzlyContainer().next(
131 new MkAnswer.Simple(HttpURLConnection.HTTP_NO_CONTENT, "")
132 ).start(this.resource.port())) {
133 final RtGist gist = new RtGist(
134 new MkGithub(),
135 new FakeRequest().withStatus(HttpURLConnection.HTTP_NO_CONTENT),
136 "gistName"
137 );
138 final RtGistComment comment = new RtGistComment(
139 new ApacheRequest(container.home()), gist, identifier
140 );
141 comment.remove();
142 MatcherAssert.assertThat(
143 container.take().method(),
144 Matchers.equalTo(Request.DELETE)
145 );
146 container.stop();
147 }
148 }
149 }