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.http.mock.MkAnswer;
33 import com.jcabi.http.mock.MkContainer;
34 import com.jcabi.http.mock.MkGrizzlyContainer;
35 import com.jcabi.http.request.ApacheRequest;
36 import com.jcabi.http.request.FakeRequest;
37 import java.net.HttpURLConnection;
38 import org.hamcrest.MatcherAssert;
39 import org.hamcrest.Matchers;
40 import org.junit.Rule;
41 import org.junit.Test;
42 import org.mockito.Mockito;
43
44
45
46
47
48
49
50
51 public final class RtEventTest {
52
53
54
55
56
57 @Rule
58 public final transient RandomPort resource = new RandomPort();
59
60
61
62
63
64 @Test
65 public void canRetrieveOwnRepo() {
66 final Repo repo = this.repo();
67 final RtEvent event = new RtEvent(new FakeRequest(), repo, 1);
68 MatcherAssert.assertThat(
69 event.repo(),
70 Matchers.sameInstance(repo)
71 );
72 }
73
74
75
76
77
78 @Test
79 public void canRetrieveOwnNumber() {
80 final Repo repo = this.repo();
81 final RtEvent event = new RtEvent(new FakeRequest(), repo, 2);
82 MatcherAssert.assertThat(
83 event.number(),
84 Matchers.equalTo(2)
85 );
86 }
87
88
89
90
91
92
93 @Test
94 public void retrieveEventAsJson() throws Exception {
95 try (final MkContainer container = new MkGrizzlyContainer().next(
96 new MkAnswer.Simple(
97 HttpURLConnection.HTTP_OK,
98 "{\"test\":\"events\"}"
99 )
100 ).start(this.resource.port())) {
101 final RtEvent event = new RtEvent(
102 new ApacheRequest(container.home()),
103 this.repo(),
104 3
105 );
106 MatcherAssert.assertThat(
107 event.json().getString("test"),
108 Matchers.equalTo("events")
109 );
110 }
111 }
112
113
114
115
116
117 @Test
118 public void canCompareInstances() {
119 final RtEvent less = new RtEvent(new FakeRequest(), this.repo(), 1);
120 final RtEvent greater = new RtEvent(new FakeRequest(), this.repo(), 2);
121 MatcherAssert.assertThat(
122 less.compareTo(greater), Matchers.lessThan(0)
123 );
124 MatcherAssert.assertThat(
125 greater.compareTo(less), Matchers.greaterThan(0)
126 );
127 }
128
129
130
131
132
133 private Repo repo() {
134 final Repo repo = Mockito.mock(Repo.class);
135 Mockito.doReturn(new Coordinates.Simple("test", "event"))
136 .when(repo).coordinates();
137 return repo;
138 }
139
140 }