2 * © 2013 xplosion interactive
5 package net.pterodactylus.sone.web.ajax;
7 import static com.google.common.base.Optional.of;
8 import static org.hamcrest.CoreMatchers.is;
9 import static org.hamcrest.CoreMatchers.notNullValue;
10 import static org.junit.Assert.assertThat;
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Matchers.anyString;
13 import static org.mockito.Matchers.argThat;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.never;
16 import static org.mockito.Mockito.times;
17 import static org.mockito.Mockito.verify;
18 import static org.mockito.Mockito.when;
21 import java.net.URISyntaxException;
23 import net.pterodactylus.sone.core.Core;
24 import net.pterodactylus.sone.data.Post;
25 import net.pterodactylus.sone.web.WebInterface;
26 import net.pterodactylus.sone.web.page.FreenetRequest;
28 import freenet.clients.http.HTTPRequestImpl;
29 import freenet.support.api.HTTPRequest;
30 import org.junit.Test;
33 * Tests for {@link BookmarkAjaxPage}.
35 * @author <a href="mailto:d.roden@xplosion.de">David Roden</a>
37 public class BookmarkAjaxPageTest {
40 public void testBookmarkingExistingPost() throws URISyntaxException {
42 Core core = mock(Core.class);
43 Post post = mock(Post.class);
44 when(core.getPost("abc")).thenReturn(of(post));
45 WebInterface webInterface = mock(WebInterface.class);
46 when(webInterface.getCore()).thenReturn(core);
47 HTTPRequest httpRequest = new HTTPRequestImpl(new URI("/ajax/bookmark.ajax?post=abc"), "GET");
48 FreenetRequest request = mock(FreenetRequest.class);
49 when(request.getHttpRequest()).thenReturn(httpRequest);
51 /* create JSON page. */
52 BookmarkAjaxPage bookmarkAjaxPage = new BookmarkAjaxPage(webInterface);
53 JsonReturnObject jsonReturnObject = bookmarkAjaxPage.createJsonObject(request);
55 /* verify response. */
56 assertThat(jsonReturnObject, notNullValue());
57 assertThat(jsonReturnObject.isSuccess(), is(true));
59 /* verify behaviour. */
60 verify(core).bookmarkPost(post);
64 public void testBookmarkingMissingPost() throws URISyntaxException {
66 Core core = mock(Core.class);
67 WebInterface webInterface = mock(WebInterface.class);
68 when(webInterface.getCore()).thenReturn(core);
69 HTTPRequest httpRequest = new HTTPRequestImpl(new URI("/ajax/bookmark.ajax"), "GET");
70 FreenetRequest request = mock(FreenetRequest.class);
71 when(request.getHttpRequest()).thenReturn(httpRequest);
73 /* create JSON page. */
74 BookmarkAjaxPage bookmarkAjaxPage = new BookmarkAjaxPage(webInterface);
75 JsonReturnObject jsonReturnObject = bookmarkAjaxPage.createJsonObject(request);
77 /* verify response. */
78 assertThat(jsonReturnObject, notNullValue());
79 assertThat(jsonReturnObject.isSuccess(), is(false));
80 assertThat(((JsonErrorReturnObject) jsonReturnObject).getError(), is("invalid-post-id"));
82 /* verify behaviour. */
83 verify(core, never()).bookmarkPost(any(Post.class));