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.mock;
31
32 import com.jcabi.aspects.Tv;
33 import com.jcabi.github.Comment;
34 import com.jcabi.github.Coordinates;
35 import com.jcabi.github.Repos;
36 import java.net.URL;
37 import java.util.Date;
38 import org.hamcrest.MatcherAssert;
39 import org.hamcrest.Matchers;
40 import org.junit.Test;
41 import org.mockito.Mockito;
42
43 /**
44 * Test case for {@link MkComment}.
45 * @author Yegor Bugayenko (yegor256@gmail.com)
46 * @version $Id: dc827e33a59f1eb2121f4f9f91b97d37329909f8 $
47 */
48 public final class MkCommentTest {
49 /**
50 * MkComment can change body.
51 * @throws Exception If some problem inside
52 */
53 @Test
54 public void changesBody() throws Exception {
55 final Comment comment = this.comment("hey buddy");
56 new Comment.Smart(comment).body("hello, this is a new body");
57 MatcherAssert.assertThat(
58 new Comment.Smart(comment).body(),
59 Matchers.startsWith("hello, this ")
60 );
61 }
62
63 /**
64 * MkComment should be able to compare different instances.
65 *
66 * @throws Exception when a problem occurs.
67 */
68 @Test
69 public void canCompareInstances() throws Exception {
70 final MkComment less = new MkComment(
71 new MkStorage.InFile(),
72 "login-less",
73 Mockito.mock(Coordinates.class),
74 1,
75 1
76 );
77 final MkComment greater = new MkComment(
78 new MkStorage.InFile(),
79 "login-greater",
80 Mockito.mock(Coordinates.class),
81 2,
82 2
83 );
84 MatcherAssert.assertThat(
85 less.compareTo(greater),
86 Matchers.lessThan(0)
87 );
88 MatcherAssert.assertThat(
89 greater.compareTo(less),
90 Matchers.greaterThan(0)
91 );
92 }
93
94 /**
95 * MkComment should store all its data properly.
96 * We should get the proper data back when accessing its properties.
97 * @throws Exception when a problem occurs.
98 */
99 @Test
100 public void dataStoredProperly() throws Exception {
101 final String cmt = "what's up?";
102 final long before = MkCommentTest.now();
103 final Comment comment = this.comment(cmt);
104 final long after = MkCommentTest.now();
105 MatcherAssert.assertThat(
106 comment.number(),
107 Matchers.greaterThan(0)
108 );
109 final Comment.Smart smart = new Comment.Smart(comment);
110 MatcherAssert.assertThat(
111 smart.issue().number(),
112 Matchers.greaterThan(0)
113 );
114 MatcherAssert.assertThat(
115 smart.author().login(),
116 Matchers.equalTo("jeff")
117 );
118 MatcherAssert.assertThat(
119 smart.body(),
120 Matchers.equalTo(cmt)
121 );
122 MatcherAssert.assertThat(
123 smart.url(),
124 Matchers.equalTo(
125 new URL(
126 // @checkstyle LineLength (1 line)
127 "https://api.jcabi-github.invalid/repos/jeff/blueharvest/issues/comments/1"
128 )
129 )
130 );
131 MatcherAssert.assertThat(
132 smart.createdAt().getTime(),
133 Matchers.greaterThanOrEqualTo(before)
134 );
135 MatcherAssert.assertThat(
136 smart.createdAt().getTime(),
137 Matchers.lessThanOrEqualTo(after)
138 );
139 MatcherAssert.assertThat(
140 smart.updatedAt().getTime(),
141 Matchers.greaterThanOrEqualTo(before)
142 );
143 MatcherAssert.assertThat(
144 smart.updatedAt().getTime(),
145 Matchers.lessThanOrEqualTo(after)
146 );
147 }
148
149 /**
150 * Create a comment to work with.
151 * @param text Text of comment
152 * @return Comment just created
153 * @throws Exception If some problem inside
154 */
155 private Comment comment(final String text) throws Exception {
156 return new MkGithub().repos().create(
157 new Repos.RepoCreate("blueharvest", false)
158 ).issues().create("hey", "how are you?").comments().post(text);
159 }
160
161 /**
162 * Obtains the current time.
163 * @return Current time (in milliseconds since epoch) truncated to the nearest second
164 */
165 private static long now() {
166 final long sinceepoch = new Date().getTime();
167 return sinceepoch - sinceepoch % Tv.THOUSAND;
168 }
169 }