1
2
3
4
5 package com.jcabi.github.mock;
6
7 import java.io.IOException;
8 import org.hamcrest.MatcherAssert;
9 import org.hamcrest.Matchers;
10 import org.junit.jupiter.api.Assertions;
11 import org.junit.jupiter.api.Test;
12 import org.xembly.Directives;
13
14
15
16
17
18
19 @SuppressWarnings("PMD.AvoidDuplicateLiterals")
20 final class MkNotificationsTest {
21
22 @Test
23 void fetchesEmptyListOfNotifications() throws IOException {
24 MatcherAssert.assertThat(
25 "Collection is not empty",
26 new MkNotifications(
27 new MkStorage.InFile(),
28 "notifications"
29 ).iterate(),
30 Matchers.emptyIterable()
31 );
32 }
33
34 @Test
35 void fetchesNonEmptyListOfNotifications() throws IOException {
36 final MkStorage storage = new MkStorage.InFile();
37 storage.apply(
38 new Directives().xpath("/github")
39 .add("notifications")
40 .add("notification")
41 .add("id").set("1").up().up()
42 .add("notification")
43 .add("id").set("2").up().up()
44 .add("notification")
45 .add("id").set("3").up().up()
46 );
47 MatcherAssert.assertThat(
48 "Collection size is incorrect",
49 new MkNotifications(
50 storage,
51 "/github/notifications/notification"
52 ).iterate(),
53
54 Matchers.iterableWithSize(3)
55 );
56 }
57
58 @Test
59 void fetchesNotificationById() throws IOException {
60 final MkStorage storage = new MkStorage.InFile();
61 storage.apply(
62 new Directives().xpath("/github")
63 .add("notifications")
64 .add("notification")
65 .add("id").set("1").up().up()
66 .add("notification")
67 .add("id").set("2").up().up()
68 );
69 MatcherAssert.assertThat(
70 "Value is null",
71 new MkNotifications(
72 storage,
73 "/github/notifications/notification"
74 ).get(2),
75 Matchers.notNullValue()
76 );
77 }
78
79 @Test
80 void cannotFetchNotificationByNonExistentId() throws IOException {
81 final MkStorage storage = new MkStorage.InFile();
82 storage.apply(
83 new Directives().xpath("/github")
84 .add("notifications")
85 .add("notification")
86 .add("id").set("1").up().up()
87 .add("notification")
88 .add("id").set("3").up().up()
89 );
90 final MkNotifications notifs = new MkNotifications(
91 storage,
92 "/github/notifications/notification"
93 );
94 Assertions.assertThrows(
95 IndexOutOfBoundsException.class,
96 () -> notifs.get(2),
97 "Should throw when notification ID does not exist"
98 );
99 }
100 }