Replace unnecessary type parameters with <>
[Sone.git] / src / test / java / net / pterodactylus / sone / notify / ListNotificationTest.java
1 package net.pterodactylus.sone.notify;
2
3 import static org.hamcrest.MatcherAssert.assertThat;
4 import static org.hamcrest.Matchers.emptyIterable;
5 import static org.hamcrest.Matchers.is;
6 import static org.hamcrest.Matchers.not;
7 import static org.mockito.ArgumentMatchers.eq;
8 import static org.mockito.Mockito.mock;
9 import static org.mockito.Mockito.verify;
10 import static org.mockito.Mockito.when;
11 import static org.mockito.hamcrest.MockitoHamcrest.argThat;
12
13 import java.util.Arrays;
14
15 import net.pterodactylus.util.notify.NotificationListener;
16 import net.pterodactylus.util.template.Template;
17 import net.pterodactylus.util.template.TemplateContext;
18
19 import org.hamcrest.Matchers;
20 import org.junit.Test;
21
22 /**
23  * Unit test for {@link ListNotification}.
24  */
25 public class ListNotificationTest {
26
27         private static final String ID = "notification-id";
28         private static final String KEY = "element-key";
29         private static final String OTHER_KEY = "other-key";
30
31         private final Template template = mock(Template.class);
32         private final TemplateContext templateInitialContext = mock(TemplateContext.class);
33         private ListNotification<Object> listNotification;
34
35         public ListNotificationTest() {
36                 when(template.getInitialContext()).thenReturn(templateInitialContext);
37                 listNotification = new ListNotification<>(ID, KEY, template);
38         }
39
40         @Test
41         public void creatingAListNotificationSetsEmptyIterableOnElementKeyInTemplateContext() {
42                 verify(templateInitialContext).set(eq(KEY), argThat(emptyIterable()));
43         }
44
45         @Test
46         public void newListNotificationHasNoElement() {
47                 assertThat(listNotification.getElements(), emptyIterable());
48         }
49
50         @Test
51         public void newListNotificationIsEmpty() {
52                 assertThat(listNotification.isEmpty(), is(true));
53         }
54
55         @Test
56         public void listNotificationRetainsSetElements() {
57                 listNotification.setElements(Arrays.asList("a", "b", "c"));
58                 assertThat(listNotification.getElements(), Matchers.<Object>contains("a", "b", "c"));
59         }
60
61         @Test
62         public void listNotificationRetainsAddedElements() {
63                 listNotification.add("a");
64                 listNotification.add("b");
65                 listNotification.add("c");
66                 assertThat(listNotification.getElements(), Matchers.<Object>contains("a", "b", "c"));
67         }
68
69         @Test
70         public void listNotificationRemovesCorrectElement() {
71                 listNotification.setElements(Arrays.asList("a", "b", "c"));
72                 listNotification.remove("b");
73                 assertThat(listNotification.getElements(), Matchers.<Object>contains("a", "c"));
74         }
75
76         @Test
77         public void removingTheLastElementDismissesTheNotification() {
78                 NotificationListener notificationListener = mock(NotificationListener.class);
79                 listNotification.addNotificationListener(notificationListener);
80                 listNotification.add("a");
81                 listNotification.remove("a");
82                 verify(notificationListener).notificationDismissed(listNotification);
83         }
84
85         @Test
86         public void dismissingTheListNotificationRemovesAllElements() {
87                 listNotification.setElements(Arrays.asList("a", "b", "c"));
88                 listNotification.dismiss();
89                 assertThat(listNotification.getElements(), emptyIterable());
90         }
91
92         @Test
93         public void listNotificationWithDifferentElementsIsNotEqual() {
94                 ListNotification secondNotification = new ListNotification(ID, KEY, template);
95                 listNotification.add("a");
96                 secondNotification.add("b");
97                 assertThat(listNotification, not(is(secondNotification)));
98         }
99
100         @Test
101         public void listNotificationWithDifferentKeyIsNotEqual() {
102                 ListNotification secondNotification = new ListNotification(ID, OTHER_KEY, template);
103                 assertThat(listNotification, not(is(secondNotification)));
104         }
105
106         @Test
107         public void copiedNotificationsHaveTheSameHashCode() {
108                 ListNotification secondNotification = new ListNotification(listNotification);
109                 listNotification.add("a");
110                 secondNotification.add("a");
111                 listNotification.setLastUpdateTime(secondNotification.getLastUpdatedTime());
112                 assertThat(listNotification.hashCode(), is(secondNotification.hashCode()));
113         }
114
115         @Test
116         public void listNotificationIsNotEqualToOtherObjects() {
117             assertThat(listNotification, not(is(new Object())));
118         }
119
120 }