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.User;
8   import com.jcabi.github.UserOrganizations;
9   import java.io.IOException;
10  import java.time.Instant;
11  import java.time.temporal.ChronoUnit;
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.xembly.Directives;
17  
18  /**
19   * Unit tests for the MkUser class.
20   * @since 0.1
21   * @checkstyle MultipleStringLiteralsCheck (200 lines)
22   */
23  final class MkUserTest {
24  
25      /**
26       * Tests that MkUser.organizations() returns a value.
27       * @throws IOException when there is an error creating the MkUser begin tested
28       */
29      @Test
30      void testGetOrganizations() throws IOException {
31          final MkUser user = new MkUser(
32              new MkStorage.InFile(),
33              "orgTestIterate"
34          );
35          final UserOrganizations orgs = user.organizations();
36          MatcherAssert.assertThat(
37              "Value is null",
38              orgs,
39              Matchers.notNullValue()
40          );
41      }
42  
43      /**
44       * MkUser returns notifications.
45       * <p>
46       * There is no requirement for us to return actual mock data because our
47       * API does not provide a way to create notifications.
48       * @throws IOException If there is an error creating the user.
49       */
50      @Test
51      void returnsNotifications() throws IOException {
52          MatcherAssert.assertThat(
53              "Value is null",
54              new MkUser(
55                  new MkStorage.InFile(),
56                  "notifications"
57              ).notifications(),
58              Matchers.notNullValue()
59          );
60      }
61  
62      /**
63       * Must mark notifications as read only if their 'lastread' is equal or
64       * older than the given date.
65       * @throws IOException If any error occurs.
66       */
67      @Test
68      void marksNotificationsAsReadUpToDate() throws IOException {
69          final MkStorage storage = new MkStorage.InFile();
70          storage.apply(new Directives().xpath("/github").add("users"));
71          final User user = new MkUsers(storage, "joe").add("joe");
72          final Instant upto = Instant.now();
73          storage.apply(
74              new Directives()
75                  .xpath("//notifications")
76                  .add("notification")
77                      .add("id").set(1).up()
78                      .add("date").set(
79                          // @checkstyle MagicNumberCheck (1 line)
80                          upto.minus(30, ChronoUnit.MINUTES).toEpochMilli()
81                      ).up()
82                      .add("read").set(false).up()
83                      .up()
84                  .add("notification")
85                      .add("id").set(2).up()
86                      .add("date").set(
87                          // @checkstyle MagicNumberCheck (1 line)
88                          upto.plus(30, ChronoUnit.MINUTES).toEpochMilli()
89                      ).up()
90                      .add("read").set(false).up()
91                      .up()
92          );
93          user.markAsRead(Date.from(upto));
94          MatcherAssert.assertThat(
95              "Values are not equal",
96              storage.xml().xpath("//notification[id = 1]/read/text()").get(0),
97              Matchers.is("true")
98          );
99          MatcherAssert.assertThat(
100             "Values are not equal",
101             storage.xml().xpath("//notification[id = 2]/read/text()").get(0),
102             Matchers.is("false")
103         );
104     }
105 }