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.jcabi.github.User;
33  import com.jcabi.github.UserOrganizations;
34  import java.io.IOException;
35  import java.time.Instant;
36  import java.time.temporal.ChronoUnit;
37  import java.util.Date;
38  import org.hamcrest.MatcherAssert;
39  import org.hamcrest.Matchers;
40  import org.junit.Test;
41  import org.xembly.Directives;
42  
43  /**
44   * Unit tests for the MkUser class.
45   *
46   * @author Ed Hillmann (edhillmann@yahoo.com)
47   * @version $Id: 45b760061125f834c59eff4d52a406743ce94721 $
48   * @checkstyle MultipleStringLiteralsCheck (200 lines)
49   */
50  public final class MkUserTest {
51  
52      /**
53       * Tests that MkUser.organizations() returns a value.
54       *
55       * @throws IOException when there is an error creating the MkUser begin tested
56       */
57      @Test
58      public void testGetOrganizations() throws IOException {
59          final MkUser user = new MkUser(
60              new MkStorage.InFile(),
61              "orgTestIterate"
62          );
63          final UserOrganizations orgs = user.organizations();
64          MatcherAssert.assertThat(
65              orgs,
66              Matchers.notNullValue()
67          );
68      }
69  
70      /**
71       * MkUser returns notifications.
72       * <p>
73       * There is no requirement for us to return actual mock data because our
74       * API does not provide a way to create notifications.
75       * @throws IOException If there is an error creating the user.
76       */
77      @Test
78      public void returnsNotifications() throws IOException {
79          MatcherAssert.assertThat(
80              new MkUser(
81                  new MkStorage.InFile(),
82                  "notifications"
83              ).notifications(),
84              Matchers.notNullValue()
85          );
86      }
87  
88      /**
89       * Must mark notifications as read only if their 'lastread' is equal or
90       * older than the given date.
91       * @throws IOException If any error occurs.
92       */
93      @Test
94      public void marksNotificationsAsReadUpToDate() throws IOException {
95          final MkStorage storage = new MkStorage.InFile();
96          storage.apply(new Directives().xpath("/github").add("users"));
97          final User user = new MkUsers(storage, "joe").add("joe");
98          final Instant upto = Instant.now();
99          storage.apply(
100             new Directives()
101                 .xpath("//notifications")
102                 .add("notification")
103                     .add("id").set(1).up()
104                     .add("date").set(
105                         // @checkstyle MagicNumberCheck (1 line)
106                         upto.minus(30, ChronoUnit.MINUTES).toEpochMilli()
107                     ).up()
108                     .add("read").set(false).up()
109                     .up()
110                 .add("notification")
111                     .add("id").set(2).up()
112                     .add("date").set(
113                          // @checkstyle MagicNumberCheck (1 line)
114                         upto.plus(30, ChronoUnit.MINUTES).toEpochMilli()
115                     ).up()
116                     .add("read").set(false).up()
117                     .up()
118         );
119         user.markAsRead(Date.from(upto));
120         MatcherAssert.assertThat(
121             storage.xml().xpath("//notification[id = 1]/read/text()").get(0),
122             Matchers.is("true")
123         );
124         MatcherAssert.assertThat(
125             storage.xml().xpath("//notification[id = 2]/read/text()").get(0),
126             Matchers.is("false")
127         );
128     }
129 }