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.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
53
54
55
56
57
58
59
60
61
62
63 public final class RtStatusesTest {
64
65
66
67
68 @Rule
69 public final transient RandomPort resource = new RandomPort();
70
71
72
73
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
87
88
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 }