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.Notification;
33 import org.hamcrest.MatcherAssert;
34 import org.hamcrest.Matchers;
35 import org.junit.Test;
36 import org.xembly.Directives;
37
38 /**
39 * Test case for {@link MkNotifications}.
40 *
41 * @author Piotr Pradzynski (prondzyn@gmail.com)
42 * @version $Id: 696eddff0f6ad99946b7d5c66d77a1e01baa637a $
43 * @checkstyle MultipleStringLiteralsCheck (500 lines)
44 */
45 @SuppressWarnings("PMD.AvoidDuplicateLiterals")
46 public final class MkNotificationsTest {
47
48 /**
49 * MkNotifications can fetch empty list of Notification.
50 * @throws Exception if some problem inside
51 */
52 @Test
53 public void fetchesEmptyListOfNotifications() throws Exception {
54 MatcherAssert.assertThat(
55 new MkNotifications(
56 new MkStorage.InFile(),
57 "notifications"
58 ).iterate(),
59 Matchers.emptyIterable()
60 );
61 }
62
63 /**
64 * MkNotifications can fetch non empty list of Notifications.
65 * @throws Exception If some problem inside
66 */
67 @Test
68 public void fetchesNonEmptyListOfNotifications() throws Exception {
69 final MkStorage storage = new MkStorage.InFile();
70 storage.apply(
71 new Directives().xpath("/github")
72 .add("notifications")
73 .add("notification")
74 .add("id").set("1").up().up()
75 .add("notification")
76 .add("id").set("2").up().up()
77 .add("notification")
78 .add("id").set("3").up().up()
79 );
80 MatcherAssert.assertThat(
81 new MkNotifications(
82 storage,
83 "/github/notifications/notification"
84 ).iterate(),
85 // @checkstyle MagicNumberCheck (1 line)
86 Matchers.<Notification>iterableWithSize(3)
87 );
88 }
89
90 /**
91 * MkNotifications can fetch a notification by id.
92 * @throws Exception If something goes wrong
93 */
94 @Test
95 public void fetchesNotificationById() throws Exception {
96 final MkStorage storage = new MkStorage.InFile();
97 storage.apply(
98 new Directives().xpath("/github")
99 .add("notifications")
100 .add("notification")
101 .add("id").set("1").up().up()
102 .add("notification")
103 .add("id").set("2").up().up()
104 );
105 MatcherAssert.assertThat(
106 new MkNotifications(
107 storage,
108 "/github/notifications/notification"
109 ).get(2),
110 Matchers.notNullValue()
111 );
112 }
113
114 /**
115 * MkNotifications can fetch a notification by id.
116 * @throws Exception If something goes wrong
117 */
118 @Test(expected = IndexOutOfBoundsException.class)
119 public void cannotFetchNotificationByNonExistentId() throws Exception {
120 final MkStorage storage = new MkStorage.InFile();
121 storage.apply(
122 new Directives().xpath("/github")
123 .add("notifications")
124 .add("notification")
125 .add("id").set("1").up().up()
126 .add("notification")
127 .add("id").set("3").up().up()
128 );
129 MatcherAssert.assertThat(
130 new MkNotifications(
131 storage,
132 "/github/notifications/notification"
133 ).get(2),
134 Matchers.notNullValue()
135 );
136 }
137 }