X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2FWebPageTest.java;h=c79f676a077449041eccff1e73fea1d618456af9;hb=76bbcf5d9baa5935ebd1fceb97ee59ac6a93c643;hp=4aef91742dbb2c60d93ef1518676d972f60ce875;hpb=f7e6c9dbec3fdcb77a54e245c9f2b7a9a851b5b0;p=Sone.git diff --git a/src/test/java/net/pterodactylus/sone/web/WebPageTest.java b/src/test/java/net/pterodactylus/sone/web/WebPageTest.java index 4aef917..c79f676 100644 --- a/src/test/java/net/pterodactylus/sone/web/WebPageTest.java +++ b/src/test/java/net/pterodactylus/sone/web/WebPageTest.java @@ -1,5 +1,7 @@ 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; @@ -15,6 +17,7 @@ import java.io.PipedOutputStream; import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; +import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -29,11 +32,13 @@ import net.pterodactylus.sone.core.UpdateChecker; import net.pterodactylus.sone.data.Album; import net.pterodactylus.sone.data.Image; import net.pterodactylus.sone.data.Post; +import net.pterodactylus.sone.data.PostReply; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.data.SoneOptions.DefaultSoneOptions; 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; @@ -44,11 +49,14 @@ import freenet.clients.http.ToadletContext; 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; import org.junit.Rule; import org.junit.rules.ExpectedException; +import org.mockito.ArgumentMatchers; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; @@ -80,7 +88,9 @@ public abstract class WebPageTest { protected final ToadletContext toadletContext = mock(ToadletContext.class); private final Set ownIdentities = new HashSet<>(); - private final List localSones = new ArrayList<>(); + private final Map sones = new HashMap<>(); + protected final List localSones = new ArrayList<>(); + private final ListMultimap postReplies = ArrayListMultimap.create(); protected WebPageTest() { try { @@ -109,7 +119,7 @@ public abstract class WebPageTest { return requestParameters.containsKey(parameter) ? requestParameters.get(parameter) : ""; } }); - when(httpRequest.getParam(anyString(), anyString())).thenAnswer(new Answer() { + when(httpRequest.getParam(anyString(), ArgumentMatchers.any())).thenAnswer(new Answer() { @Override public String answer(InvocationOnMock invocation) throws Throwable { String parameter = invocation.getArgument(0); @@ -119,7 +129,7 @@ public abstract class WebPageTest { when(httpRequest.isPartSet(anyString())).thenAnswer(new Answer() { @Override public Boolean answer(InvocationOnMock invocation) throws Throwable { - return requestParameters.containsKey(invocation.getArgument(0)); + return requestParameters.get(invocation.getArgument(0)) != null; } }); when(httpRequest.getParts()).thenAnswer(new Answer() { @@ -144,7 +154,25 @@ public abstract class WebPageTest { when(core.getLocalSone(anyString())).thenReturn(null); when(core.getLocalSones()).thenReturn(localSones); when(core.getSone(anyString())).thenReturn(Optional.absent()); + when(core.getSones()).thenAnswer(new Answer>() { + @Override + public Collection answer(InvocationOnMock invocation) throws Throwable { + return sones.values(); + } + }); + when(core.getSone(anyString())).thenAnswer(new Answer>() { + @Override + public Optional answer(InvocationOnMock invocation) throws Throwable { + return Optional.fromNullable(sones.get(invocation.getArgument(0))); + } + }); when(core.getPost(anyString())).thenReturn(Optional.absent()); + when(core.getReplies(anyString())).thenAnswer(new Answer>() { + @Override + public List answer(InvocationOnMock invocation) throws Throwable { + return postReplies.get(invocation.getArgument(0)); + } + }); when(core.getAlbum(anyString())).thenReturn(null); when(core.getImage(anyString())).thenReturn(null); when(core.getImage(anyString(), anyBoolean())).thenReturn(null); @@ -169,6 +197,10 @@ public abstract class WebPageTest { 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); @@ -195,8 +227,15 @@ public abstract class WebPageTest { when(core.getPost(postId)).thenReturn(Optional.fromNullable(post)); } + protected void addPostReply(String postReplyId, PostReply postReply) { + if (postReply.getPostId() != null) { + postReplies.put(postReply.getPostId(), postReply); + } + when(core.getPostReply(postReplyId)).thenReturn(Optional.fromNullable(postReply)); + } + protected void addSone(String soneId, Sone sone) { - when(core.getSone(eq(soneId))).thenReturn(Optional.fromNullable(sone)); + sones.put(soneId, sone); } protected void addLocalSone(String soneId, Sone sone) { @@ -233,4 +272,19 @@ public abstract class WebPageTest { 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(); + } + } + }