From: David ‘Bombe’ Roden Date: Sun, 2 Nov 2014 11:49:02 +0000 (+0100) Subject: Add unit test for get-page plugin. X-Git-Tag: 0.9-rc1^2~3^2~57 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=334798949fc7a0585f861c60ce93f4f2dc670d65;p=Sone.git Add unit test for get-page plugin. --- diff --git a/src/test/java/net/pterodactylus/sone/template/GetPagePluginTest.java b/src/test/java/net/pterodactylus/sone/template/GetPagePluginTest.java new file mode 100644 index 0000000..febd06a --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/template/GetPagePluginTest.java @@ -0,0 +1,79 @@ +package net.pterodactylus.sone.template; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.HashMap; +import java.util.Map; + +import net.pterodactylus.sone.web.page.FreenetRequest; +import net.pterodactylus.util.template.TemplateContext; + +import freenet.support.api.HTTPRequest; + +import org.junit.Before; +import org.junit.Test; + +/** + * Unit test for {@link GetPagePlugin}. + * + * @author David ‘Bombe’ Roden + */ +public class GetPagePluginTest { + + private final GetPagePlugin plugin = new GetPagePlugin(); + private final TemplateContext context = mock(TemplateContext.class); + private final FreenetRequest request = mock(FreenetRequest.class); + private final Map parameters = + new HashMap(); + private HTTPRequest httpRequest = mock(HTTPRequest.class); + + @Before + public void setupTemplateContext() { + when(context.get("request")).thenReturn(request); + when(request.getHttpRequest()).thenReturn(httpRequest); + when(httpRequest.getParam("page")).thenReturn("1"); + } + + @Test + public void fullySpecifiedPluginCallSetsCorrectValue() { + parameters.put("request", "request"); + parameters.put("parameter", "page"); + parameters.put("key", "page-key"); + plugin.execute(context, parameters); + verify(context).set("page-key", 1); + } + + @Test + public void missingRequestParameterStillSetsCorrectValue() { + parameters.put("parameter", "page"); + parameters.put("key", "page-key"); + plugin.execute(context, parameters); + verify(context).set("page-key", 1); + } + + @Test + public void missingParameterParameterStillSetsCorrectValue() { + parameters.put("request", "request"); + parameters.put("key", "page-key"); + plugin.execute(context, parameters); + verify(context).set("page-key", 1); + } + + @Test + public void missingKeyParameterStillSetsCorrectValue() { + parameters.put("request", "request"); + parameters.put("parameter", "page"); + plugin.execute(context, parameters); + verify(context).set("page", 1); + } + + @Test + public void unparseablePageSetsPageZero() { + parameters.put("parameter", "wrong-parameter"); + plugin.execute(context, parameters); + verify(context).set("page", 0); + } + +}