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