Add unit test for delete image 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.getNotifications(currentSone)).thenReturn(new ArrayList<Notification>());
107         }
108
109         @Before
110         public void setupSone() {
111                 when(currentSone.getOptions()).thenReturn(new DefaultSoneOptions());
112         }
113
114         protected void unsetCurrentSone() {
115                 when(webInterface.getCurrentSone(toadletContext)).thenReturn(null);
116                 when(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(null);
117         }
118
119         protected void request(String uri, Method method) {
120                 try {
121                         when(freenetRequest.getUri()).thenReturn(new URI(uri));
122                 } catch (URISyntaxException e) {
123                         throw new RuntimeException(e);
124                 }
125                 when(freenetRequest.getMethod()).thenReturn(method);
126         }
127
128         protected void addHttpRequestParameter(String name, final String value) {
129                 when(httpRequest.getPartAsStringFailsafe(eq(name), anyInt())).thenAnswer(new Answer<String>() {
130                         @Override
131                         public String answer(InvocationOnMock invocation) throws Throwable {
132                                 int maxLength = invocation.getArgument(1);
133                                 return value.substring(0, Math.min(maxLength, value.length()));
134                         }
135                 });
136                 when(httpRequest.getParam(eq(name))).thenReturn(value);
137                 when(httpRequest.getParam(eq(name), anyString())).thenReturn(value);
138                 when(httpRequest.isPartSet(eq(name))).thenReturn(value != null && !value.isEmpty());
139         }
140
141         protected void addPost(String postId, Post post) {
142                 when(core.getPost(postId)).thenReturn(Optional.fromNullable(post));
143         }
144
145         protected void addSone(String soneId, Sone sone) {
146                 when(core.getSone(eq(soneId))).thenReturn(Optional.fromNullable(sone));
147         }
148
149         protected void addLocalSone(String soneId, Sone sone) {
150                 when(core.getLocalSone(eq(soneId))).thenReturn(sone);
151                 localSones.add(sone);
152         }
153
154         protected void addOwnIdentity(OwnIdentity ownIdentity) {
155                 ownIdentities.add(ownIdentity);
156         }
157
158         protected void addAlbum(String albumId, Album album) {
159                 when(core.getAlbum(eq(albumId))).thenReturn(album);
160         }
161
162         protected void addImage(String imageId, Image image) {
163                 when(core.getImage(eq(imageId))).thenReturn(image);
164                 when(core.getImage(eq(imageId), anyBoolean())).thenReturn(image);
165         }
166
167 }