Add unit test for dismiss notification page
[Sone.git] / src / test / java / net / pterodactylus / sone / web / WebPageTest.java
1 package net.pterodactylus.sone.web;
2
3 import static org.mockito.ArgumentMatchers.anyBoolean;
4 import static org.mockito.ArgumentMatchers.anyInt;
5 import static org.mockito.ArgumentMatchers.anyString;
6 import static org.mockito.ArgumentMatchers.eq;
7 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
8 import static org.mockito.Mockito.mock;
9 import static org.mockito.Mockito.when;
10
11 import java.net.URI;
12 import java.net.URISyntaxException;
13 import java.util.ArrayList;
14 import java.util.HashSet;
15 import java.util.List;
16 import java.util.Set;
17
18 import net.pterodactylus.sone.core.Core;
19 import net.pterodactylus.sone.core.Preferences;
20 import net.pterodactylus.sone.core.UpdateChecker;
21 import net.pterodactylus.sone.data.Album;
22 import net.pterodactylus.sone.data.Image;
23 import net.pterodactylus.sone.data.Post;
24 import net.pterodactylus.sone.data.Sone;
25 import net.pterodactylus.sone.data.SoneOptions.DefaultSoneOptions;
26 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
27 import net.pterodactylus.sone.web.page.FreenetRequest;
28 import net.pterodactylus.util.notify.Notification;
29 import net.pterodactylus.util.template.Template;
30 import net.pterodactylus.util.template.TemplateContext;
31 import net.pterodactylus.util.web.Method;
32
33 import freenet.clients.http.ToadletContext;
34 import freenet.support.api.HTTPRequest;
35
36 import com.google.common.base.Optional;
37 import com.google.common.eventbus.EventBus;
38 import org.junit.Before;
39 import org.junit.Rule;
40 import org.junit.rules.ExpectedException;
41 import org.mockito.invocation.InvocationOnMock;
42 import org.mockito.stubbing.Answer;
43
44 /**
45  * Base class for web page tests.
46  *
47  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
48  */
49 public abstract class WebPageTest {
50
51         @Rule
52         public final ExpectedException expectedException = ExpectedException.none();
53
54         protected final Template template = new Template();
55         protected final WebInterface webInterface = mock(WebInterface.class, RETURNS_DEEP_STUBS);
56         protected final EventBus eventBus = mock(EventBus.class);
57         protected final Core core = webInterface.getCore();
58
59         protected final Sone currentSone = mock(Sone.class);
60
61         protected final TemplateContext templateContext = new TemplateContext();
62         protected final HTTPRequest httpRequest = mock(HTTPRequest.class);
63         protected final FreenetRequest freenetRequest = mock(FreenetRequest.class);
64         protected final ToadletContext toadletContext = mock(ToadletContext.class);
65
66         private final Set<OwnIdentity> ownIdentities = new HashSet<>();
67         private final List<Sone> localSones = new ArrayList<>();
68
69         @Before
70         public final void setupFreenetRequest() {
71                 when(freenetRequest.getToadletContext()).thenReturn(toadletContext);
72                 when(freenetRequest.getHttpRequest()).thenReturn(httpRequest);
73                 when(httpRequest.getPartAsStringFailsafe(anyString(), anyInt())).thenAnswer(new Answer<String>() {
74                         @Override
75                         public String answer(InvocationOnMock invocation) throws Throwable {
76                                 return "";
77                         }
78                 });
79                 when(httpRequest.getParam(anyString())).thenReturn("");
80                 when(httpRequest.getParam(anyString(), anyString())).thenReturn("");
81         }
82
83         @Before
84         public final void setupCore() {
85                 UpdateChecker updateChecker = mock(UpdateChecker.class);
86                 when(core.getUpdateChecker()).thenReturn(updateChecker);
87                 when(core.getPreferences()).thenReturn(new Preferences(eventBus));
88                 when(core.getLocalSone(anyString())).thenReturn(null);
89                 when(core.getLocalSones()).thenReturn(localSones);
90                 when(core.getSone(anyString())).thenReturn(Optional.<Sone>absent());
91                 when(core.getPost(anyString())).thenReturn(Optional.<Post>absent());
92                 when(core.getAlbum(anyString())).thenReturn(null);
93                 when(core.getImage(anyString())).thenReturn(null);
94                 when(core.getImage(anyString(), anyBoolean())).thenReturn(null);
95         }
96
97         @Before
98         public final void setupIdentityManager() {
99                 when(core.getIdentityManager().getAllOwnIdentities()).thenReturn(ownIdentities);
100         }
101
102         @Before
103         public final void setupWebInterface() {
104                 when(webInterface.getCurrentSone(toadletContext)).thenReturn(currentSone);
105                 when(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(currentSone);
106                 when(webInterface.getNotification(anyString())).thenReturn(Optional.<Notification>absent());
107                 when(webInterface.getNotifications(currentSone)).thenReturn(new ArrayList<Notification>());
108         }
109
110         @Before
111         public void setupSone() {
112                 when(currentSone.getOptions()).thenReturn(new DefaultSoneOptions());
113         }
114
115         protected void unsetCurrentSone() {
116                 when(webInterface.getCurrentSone(toadletContext)).thenReturn(null);
117                 when(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(null);
118         }
119
120         protected void request(String uri, Method method) {
121                 try {
122                         when(freenetRequest.getUri()).thenReturn(new URI(uri));
123                 } catch (URISyntaxException e) {
124                         throw new RuntimeException(e);
125                 }
126                 when(freenetRequest.getMethod()).thenReturn(method);
127         }
128
129         protected void addHttpRequestParameter(String name, final String value) {
130                 when(httpRequest.getPartAsStringFailsafe(eq(name), anyInt())).thenAnswer(new Answer<String>() {
131                         @Override
132                         public String answer(InvocationOnMock invocation) throws Throwable {
133                                 int maxLength = invocation.getArgument(1);
134                                 return value.substring(0, Math.min(maxLength, value.length()));
135                         }
136                 });
137                 when(httpRequest.getParam(eq(name))).thenReturn(value);
138                 when(httpRequest.getParam(eq(name), anyString())).thenReturn(value);
139                 when(httpRequest.isPartSet(eq(name))).thenReturn(value != null && !value.isEmpty());
140         }
141
142         protected void addPost(String postId, Post post) {
143                 when(core.getPost(postId)).thenReturn(Optional.fromNullable(post));
144         }
145
146         protected void addSone(String soneId, Sone sone) {
147                 when(core.getSone(eq(soneId))).thenReturn(Optional.fromNullable(sone));
148         }
149
150         protected void addLocalSone(String soneId, Sone sone) {
151                 when(core.getLocalSone(eq(soneId))).thenReturn(sone);
152                 localSones.add(sone);
153         }
154
155         protected void addOwnIdentity(OwnIdentity ownIdentity) {
156                 ownIdentities.add(ownIdentity);
157         }
158
159         protected void addAlbum(String albumId, Album album) {
160                 when(core.getAlbum(eq(albumId))).thenReturn(album);
161         }
162
163         protected void addImage(String imageId, Image image) {
164                 when(core.getImage(eq(imageId))).thenReturn(image);
165                 when(core.getImage(eq(imageId), anyBoolean())).thenReturn(image);
166         }
167
168         protected void addNotification(String notificationId, Notification notification) {
169                 when(webInterface.getNotification(eq(notificationId))).thenReturn(Optional.of(notification));
170         }
171
172 }