Add unit test for get-page plugin.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 2 Nov 2014 11:49:02 +0000 (12:49 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 2 Nov 2014 11:49:02 +0000 (12:49 +0100)
src/test/java/net/pterodactylus/sone/template/GetPagePluginTest.java [new file with mode: 0644]

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 (file)
index 0000000..febd06a
--- /dev/null
@@ -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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+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<String, String> parameters =
+                       new HashMap<String, String>();
+       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);
+       }
+
+}