Add test for SonePart
[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.Matchers.argThat;
8 import static org.mockito.Matchers.eq;
9 import static org.mockito.Mockito.mock;
10 import static org.mockito.Mockito.verify;
11 import static org.mockito.Mockito.when;
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  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
26  */
27 public class ListNotificationTest {
28
29         private static final String ID = "notification-id";
30         private static final String KEY = "element-key";
31         private static final String OTHER_KEY = "other-key";
32
33         private final Template template = mock(Template.class);
34         private final TemplateContext templateInitialContext = mock(TemplateContext.class);
35         private ListNotification<Object> listNotification;
36
37         public ListNotificationTest() {
38                 when(template.getInitialContext()).thenReturn(templateInitialContext);
39                 listNotification = new ListNotification<Object>(ID, KEY, template);
40         }
41
42         @Test
43         public void creatingAListNotificationSetsEmptyIterableOnElementKeyInTemplateContext() {
44                 verify(templateInitialContext).set(eq(KEY), argThat(emptyIterable()));
45         }
46
47         @Test
48         public void newListNotificationHasNoElement() {
49                 assertThat(listNotification.getElements(), emptyIterable());
50         }
51
52         @Test
53         public void newListNotificationIsEmpty() {
54                 assertThat(listNotification.isEmpty(), is(true));
55         }
56
57         @Test
58         public void listNotificationRetainsSetElements() {
59                 listNotification.setElements(Arrays.asList("a", "b", "c"));
60                 assertThat(listNotification.getElements(), Matchers.<Object>contains("a", "b", "c"));
61         }
62
63         @Test
64         public void listNotificationRetainsAddedElements() {
65                 listNotification.add("a");
66                 listNotification.add("b");
67                 listNotification.add("c");
68                 assertThat(listNotification.getElements(), Matchers.<Object>contains("a", "b", "c"));
69         }
70
71         @Test
72         public void listNotificationRemovesCorrectElement() {
73                 listNotification.setElements(Arrays.asList("a", "b", "c"));
74                 listNotification.remove("b");
75                 assertThat(listNotification.getElements(), Matchers.<Object>contains("a", "c"));
76         }
77
78         @Test
79         public void removingTheLastElementDismissesTheNotification() {
80                 NotificationListener notificationListener = mock(NotificationListener.class);
81                 listNotification.addNotificationListener(notificationListener);
82                 listNotification.add("a");
83                 listNotification.remove("a");
84                 verify(notificationListener).notificationDismissed(listNotification);
85         }
86
87         @Test
88         public void dismissingTheListNotificationRemovesAllElements() {
89                 listNotification.setElements(Arrays.asList("a", "b", "c"));
90                 listNotification.dismiss();
91                 assertThat(listNotification.getElements(), emptyIterable());
92         }
93
94         @Test
95         public void listNotificationWithDifferentElementsIsNotEqual() {
96                 ListNotification secondNotification = new ListNotification(ID, KEY, template);
97                 listNotification.add("a");
98                 secondNotification.add("b");
99                 assertThat(listNotification, not(is(secondNotification)));
100         }
101
102         @Test
103         public void listNotificationWithDifferentKeyIsNotEqual() {
104                 ListNotification secondNotification = new ListNotification(ID, OTHER_KEY, template);
105                 assertThat(listNotification, not(is(secondNotification)));
106         }
107
108         @Test
109         public void copiedNotificationsHaveTheSameHashCode() {
110                 ListNotification secondNotification = new ListNotification(listNotification);
111                 listNotification.add("a");
112                 secondNotification.add("a");
113                 assertThat(listNotification.hashCode(), is(secondNotification.hashCode()));
114         }
115
116         @Test
117         public void listNotificationIsNotEqualToOtherObjects() {
118             assertThat(listNotification, not(is(new Object())));
119         }
120
121 }