View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2013-2025 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.jcabi.github.mock;
6   
7   import com.jcabi.github.Comment;
8   import com.jcabi.github.Coordinates;
9   import com.jcabi.github.Repos;
10  import java.io.IOException;
11  import java.net.URI;
12  import java.util.Date;
13  import org.hamcrest.MatcherAssert;
14  import org.hamcrest.Matchers;
15  import org.junit.jupiter.api.Test;
16  import org.mockito.Mockito;
17  
18  /**
19   * Test case for {@link MkComment}.
20   * @since 0.1
21   */
22  @SuppressWarnings("PMD.AvoidDuplicateLiterals")
23  final class MkCommentTest {
24      /**
25       * MkComment can change body.
26       * @throws Exception If some problem inside
27       */
28      @Test
29      void changesBody() throws Exception {
30          final Comment comment = MkCommentTest.comment("hey buddy");
31          new Comment.Smart(comment).body("hello, this is a new body");
32          MatcherAssert.assertThat(
33              "String does not start with expected value",
34              new Comment.Smart(comment).body(),
35              Matchers.startsWith("hello, this ")
36          );
37      }
38  
39      @Test
40      void canCompareInstances() throws IOException {
41          final MkComment less = new MkComment(
42              new MkStorage.InFile(),
43              "login-less",
44              Mockito.mock(Coordinates.class),
45              1,
46              1
47          );
48          final MkComment greater = new MkComment(
49              new MkStorage.InFile(),
50              "login-greater",
51              Mockito.mock(Coordinates.class),
52              2,
53              2
54          );
55          MatcherAssert.assertThat(
56              "Value is not less than expected",
57              less.compareTo(greater),
58              Matchers.lessThan(0)
59          );
60          MatcherAssert.assertThat(
61              "Value is not greater than expected",
62              greater.compareTo(less),
63              Matchers.greaterThan(0)
64          );
65      }
66  
67      /**
68       * MkComment should store all its data properly.
69       * We should get the proper data back when accessing its properties.
70       * @throws Exception when a problem occurs.
71       */
72      @Test
73      void dataStoredProperly() throws Exception {
74          final String cmt = "what's up?";
75          final long before = MkCommentTest.now();
76          final Comment comment = MkCommentTest.comment(cmt);
77          final long after = MkCommentTest.now();
78          MatcherAssert.assertThat(
79              "Value is not greater than expected",
80              comment.number(),
81              Matchers.greaterThan(0L)
82          );
83          final Comment.Smart smart = new Comment.Smart(comment);
84          MatcherAssert.assertThat(
85              "Value is not greater than expected",
86              smart.issue().number(),
87              Matchers.greaterThan(0)
88          );
89          MatcherAssert.assertThat(
90              "Values are not equal",
91              smart.author().login(),
92              Matchers.equalTo("jeff")
93          );
94          MatcherAssert.assertThat(
95              "Values are not equal",
96              smart.body(),
97              Matchers.equalTo(cmt)
98          );
99          MatcherAssert.assertThat(
100             "Values are not equal",
101             smart.url(),
102             Matchers.equalTo(
103                 new URI(
104                     // @checkstyle LineLength (1 line)
105                     "https://api.jcabi-github.invalid/repos/jeff/blueharvest/issues/comments/1"
106                 ).toURL()
107             )
108         );
109         MatcherAssert.assertThat(
110             "Value is not greater than expected",
111             smart.createdAt().getTime(),
112             Matchers.greaterThanOrEqualTo(before)
113         );
114         MatcherAssert.assertThat(
115             "Value is not less than expected",
116             smart.createdAt().getTime(),
117             Matchers.lessThanOrEqualTo(after)
118         );
119         MatcherAssert.assertThat(
120             "Value is not greater than expected",
121             smart.updatedAt().getTime(),
122             Matchers.greaterThanOrEqualTo(before)
123         );
124         MatcherAssert.assertThat(
125             "Value is not less than expected",
126             smart.updatedAt().getTime(),
127             Matchers.lessThanOrEqualTo(after)
128         );
129     }
130 
131     /**
132      * Create a comment to work with.
133      * @param text Text of comment
134      * @return Comment just created
135      */
136     private static Comment comment(final String text) throws IOException {
137         return new MkGitHub().repos().create(
138             new Repos.RepoCreate("blueharvest", false)
139         ).issues().create("hey", "how are you?").comments().post(text);
140     }
141 
142     /**
143      * Obtains the current time.
144      * @return Current time (in milliseconds since epoch) truncated to the nearest second
145      */
146     private static long now() {
147         final long sinceepoch = new Date().getTime();
148         return sinceepoch - sinceepoch % 1000;
149     }
150 }