package net.pterodactylus.sone.web;
+import static net.pterodactylus.sone.web.WebTestUtils.redirectsTo;
+import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import net.pterodactylus.sone.data.TemporaryImage;
import net.pterodactylus.sone.freenet.wot.OwnIdentity;
import net.pterodactylus.sone.web.page.FreenetRequest;
+import net.pterodactylus.sone.web.page.FreenetTemplatePage.RedirectException;
import net.pterodactylus.util.notify.Notification;
import net.pterodactylus.util.template.Template;
import net.pterodactylus.util.template.TemplateContext;
import freenet.support.api.HTTPRequest;
import com.google.common.base.Optional;
+import com.google.common.collect.ArrayListMultimap;
+import com.google.common.collect.ListMultimap;
import com.google.common.eventbus.EventBus;
import com.google.common.io.ByteStreams;
import org.junit.Before;
private final Set<OwnIdentity> ownIdentities = new HashSet<>();
private final Map<String, Sone> sones = new HashMap<>();
protected final List<Sone> localSones = new ArrayList<>();
+ private final ListMultimap<String, PostReply> postReplies = ArrayListMultimap.create();
protected WebPageTest() {
try {
when(httpRequest.isPartSet(anyString())).thenAnswer(new Answer<Boolean>() {
@Override
public Boolean answer(InvocationOnMock invocation) throws Throwable {
- return requestParameters.containsKey(invocation.<String>getArgument(0));
+ return requestParameters.get(invocation.<String>getArgument(0)) != null;
}
});
when(httpRequest.getParts()).thenAnswer(new Answer<String[]>() {
}
});
when(core.getPost(anyString())).thenReturn(Optional.<Post>absent());
+ when(core.getReplies(anyString())).thenAnswer(new Answer<List<PostReply>>() {
+ @Override
+ public List<PostReply> answer(InvocationOnMock invocation) throws Throwable {
+ return postReplies.get(invocation.<String>getArgument(0));
+ }
+ });
when(core.getAlbum(anyString())).thenReturn(null);
when(core.getImage(anyString())).thenReturn(null);
when(core.getImage(anyString(), anyBoolean())).thenReturn(null);
when(currentSone.getOptions()).thenReturn(new DefaultSoneOptions());
}
+ protected SoneTemplatePage getPage() {
+ return null;
+ }
+
protected void unsetCurrentSone() {
when(webInterface.getCurrentSone(toadletContext)).thenReturn(null);
when(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(null);
}
protected void addPostReply(String postReplyId, PostReply postReply) {
+ if (postReply.getPostId() != null) {
+ postReplies.put(postReply.getPostId(), postReply);
+ }
when(core.getPostReply(postReplyId)).thenReturn(Optional.fromNullable(postReply));
}
when(webInterface.getNotification(eq(notificationId))).thenReturn(Optional.of(notification));
}
+ protected void verifyRedirect(String target) throws RedirectException {
+ expectedException.expect(redirectsTo(target));
+ getPage().handleRequest(freenetRequest, templateContext);
+ }
+
+ protected void verifyRedirect(String target, Runnable verification) throws RedirectException {
+ expectedException.expect(redirectsTo(target));
+ try {
+ getPage().handleRequest(freenetRequest, templateContext);
+ fail();
+ } finally {
+ verification.run();
+ }
+ }
+
}