Add test for list notifications.
[Sone.git] / src / test / java / net / pterodactylus / sone / notify / ListNotificationTest.java
1 /*
2  * Sone - ListNotificationTest.java - Copyright © 2013 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.notify;
19
20 import static org.hamcrest.MatcherAssert.assertThat;
21 import static org.hamcrest.Matchers.empty;
22 import static org.hamcrest.Matchers.is;
23 import static org.hamcrest.Matchers.not;
24 import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.when;
27
28 import java.util.Arrays;
29 import java.util.Collection;
30
31 import net.pterodactylus.util.template.Template;
32 import net.pterodactylus.util.template.TemplateContext;
33
34 import org.junit.Before;
35 import org.junit.Test;
36
37 /**
38  * Unit test for {@link ListNotification}.
39  *
40  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
41  */
42 public class ListNotificationTest {
43
44         private final TemplateContext initialContext = new TemplateContext();
45         private final Template template = mock(Template.class);
46         private ListNotification<Object> listNotification;
47
48         @Before
49         public void setup() {
50                 when(template.getInitialContext()).thenReturn(initialContext);
51                 listNotification = new ListNotification<Object>("id", "key", template);
52         }
53
54         @Test
55         public void newListNotificationDoesNotContainElements() {
56                 assertThat(listNotification.getElements(), empty());
57                 assertThat(listNotification.isEmpty(), is(true));
58         }
59
60         @Test
61         public void addingAnElement() {
62                 Object newObject = new Object();
63                 listNotification.add(newObject);
64                 assertThat(listNotification.isEmpty(), is(false));
65                 assertThat(listNotification.getElements(), contains(newObject));
66         }
67
68         @Test
69         public void removingAnEelement() {
70                 Object newObject = new Object();
71                 listNotification.add(newObject);
72                 listNotification.remove(newObject);
73                 assertThat(listNotification.getElements(), empty());
74                 assertThat(listNotification.isEmpty(), is(true));
75         }
76
77         @Test
78         public void settingElements() {
79                 Object object1 = new Object();
80                 Object object2 = new Object();
81                 Collection<Object> elements = Arrays.asList(object1, object2);
82                 listNotification.setElements(elements);
83                 assertThat(listNotification.getElements(), contains(object1, object2));
84         }
85
86         @Test
87         public void addingElementsChangesHashCode() {
88                 int emptyListNoficiationHashCode=listNotification.hashCode();
89                 listNotification.add(new Object());
90                 assertThat(listNotification.hashCode(), not(is(emptyListNoficiationHashCode)));
91         }
92
93         @Test
94         public void listNotificationsWithDifferentIdsAreNotEqual() {
95                 ListNotification<Object> secondListNotification = new ListNotification<Object>("otherId", "key", template);
96                 assertThat(listNotification, not(is(secondListNotification)));
97         }
98
99         @Test
100         public void listNotificationsWithDifferentKeysAreNotEqual() {
101                 ListNotification<Object> secondListNotification = new ListNotification<Object>("id", "otherKey", template);
102                 assertThat(listNotification, not(is(secondListNotification)));
103         }
104
105 }