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