View Javadoc
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.google.common.base.Optional;
33  import com.jcabi.github.Event;
34  import com.jcabi.github.Label;
35  import com.jcabi.github.Repo;
36  import com.jcabi.github.Repos;
37  import org.hamcrest.MatcherAssert;
38  import org.hamcrest.Matchers;
39  import org.junit.Test;
40  
41  /**
42   * Test case for {@link MkEvent}.
43   * @author Andrej Istomin (andrej.istomin.ikeen@gmail.com)
44   * @version $Id: 34bae5b3315075319db6cf69ba14f101637cd64b $
45   */
46  public final class MkEventTest {
47      /**
48       * Can get created_at value from json object.
49       * @throws Exception If some problem inside
50       */
51      @Test
52      public void canGetCreatedAt() throws Exception {
53          final MkStorage storage = new MkStorage.InFile();
54          final String user = "test_user";
55          final Repo repo = new MkGithub(storage, user).randomRepo();
56          final MkIssueEvents events = (MkIssueEvents) repo.issueEvents();
57          final int eventnum = events.create(
58              "test_type",
59              1,
60              user,
61              Optional.of("test_label")
62          ).number();
63          MatcherAssert.assertThat(
64              new MkEvent(
65                  storage,
66                  user,
67                  repo.coordinates(),
68                  eventnum
69              )
70                  .json().getString("created_at"),
71              Matchers.notNullValue()
72          );
73      }
74  
75      /**
76       * MkEvent can get present label value from json object.
77       * @throws Exception If some problem inside
78       */
79      @Test
80      public void canGetPresentLabel() throws Exception {
81          final MkStorage storage = new MkStorage.InFile();
82          final String user = "ken";
83          final Repo repo = new MkGithub(storage, user).repos().create(
84              new Repos.RepoCreate("foo", false)
85          );
86          final MkIssueEvents events = (MkIssueEvents) repo.issueEvents();
87          final String label = "problem";
88          final int num = events.create(
89              Event.LABELED,
90              1,
91              user,
92              Optional.of(label)
93          ).number();
94          MatcherAssert.assertThat(
95              new Event.Smart(
96                  new MkEvent(
97                      storage,
98                      user,
99                      repo.coordinates(),
100                     num
101                 )
102             ).label().get().name(),
103             Matchers.equalTo(label)
104         );
105     }
106 
107     /**
108      * MkEvent can get absent label value from json object.
109      * @throws Exception If some problem inside
110      */
111     @Test
112     public void canGetAbsentLabel() throws Exception {
113         final MkStorage storage = new MkStorage.InFile();
114         final String user = "barbie";
115         final Repo repo = new MkGithub(storage, user).repos().create(
116             new Repos.RepoCreate("bar", false)
117         );
118         final int num = ((MkIssueEvents) repo.issueEvents()).create(
119             Event.LABELED,
120             1,
121             user,
122             Optional.<String>absent()
123         ).number();
124         MatcherAssert.assertThat(
125             new Event.Smart(
126                 new MkEvent(
127                     storage,
128                     user,
129                     repo.coordinates(),
130                     num
131                 )
132             ).label(),
133             Matchers.equalTo(Optional.<Label>absent())
134         );
135     }
136 }