Mock the HTTP request, too.
[Sone.git] / src / test / java / net / pterodactylus / sone / web / ajax / BookmarkAjaxPageTest.java
1 /*
2  * © 2013 xplosion interactive
3  */
4
5 package net.pterodactylus.sone.web.ajax;
6
7 import static net.pterodactylus.sone.Verifiers.verifyJsonError;
8 import static net.pterodactylus.sone.Verifiers.verifySuccessfulJsonResponse;
9 import static org.hamcrest.MatcherAssert.assertThat;
10 import static org.hamcrest.Matchers.is;
11 import static org.mockito.Matchers.anyString;
12 import static org.mockito.Matchers.eq;
13 import static org.mockito.Mockito.never;
14 import static org.mockito.Mockito.times;
15 import static org.mockito.Mockito.verify;
16 import static org.mockito.Mockito.when;
17
18 import java.net.URISyntaxException;
19
20 import net.pterodactylus.sone.core.Core;
21 import net.pterodactylus.sone.data.Mocks;
22 import net.pterodactylus.sone.web.WebInterface;
23 import net.pterodactylus.sone.web.page.FreenetRequest;
24
25 import org.junit.Test;
26
27 /**
28  * Tests for {@link BookmarkAjaxPage}.
29  *
30  * @author <a href="mailto:d.roden@xplosion.de">David Roden</a>
31  */
32 public class BookmarkAjaxPageTest {
33
34         private final Mocks mocks = new Mocks();
35         private final Core core = mocks.core;
36         private final WebInterface webInterface = mocks.webInterface;
37         private final BookmarkAjaxPage bookmarkAjaxPage = new BookmarkAjaxPage(webInterface);
38
39         @Test
40         public void bookmarkingDoesNotRequireLogin() {
41                 assertThat(bookmarkAjaxPage.requiresLogin(), is(false));
42         }
43
44         @Test
45         public void testBookmarkingExistingPost() throws URISyntaxException {
46                 JsonReturnObject jsonReturnObject = performRequest(bookmarkAjaxPage, "abc");
47                 verifySuccessfulJsonResponse(jsonReturnObject);
48                 verify(core, times(1)).bookmarkPost(eq("abc"));
49         }
50
51         @Test
52         public void testBookmarkingMissingPost() throws URISyntaxException {
53                 JsonReturnObject jsonReturnObject = performRequest(bookmarkAjaxPage, null);
54                 verifyJsonError(jsonReturnObject, "invalid-post-id");
55                 verify(core, never()).bookmarkPost(anyString());
56         }
57
58         private JsonReturnObject performRequest(BookmarkAjaxPage bookmarkAjaxPage, String postId) throws URISyntaxException {
59                 FreenetRequest request = mocks.mockRequest("");
60                 when(request.getHttpRequest().getParam("post")).thenReturn(postId);
61                 when(request.getHttpRequest().getParam("post", null)).thenReturn(postId);
62                 return bookmarkAjaxPage.createJsonObject(request);
63         }
64
65 }