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.Mockito.mock;
14 import static org.mockito.Mockito.never;
15 import static org.mockito.Mockito.times;
16 import static org.mockito.Mockito.verify;
17 import static org.mockito.Mockito.when;
20 import java.net.URISyntaxException;
22 import net.pterodactylus.sone.core.Core;
23 import net.pterodactylus.sone.data.Post;
24 import net.pterodactylus.sone.web.WebInterface;
25 import net.pterodactylus.sone.web.page.FreenetRequest;
27 import freenet.clients.http.HTTPRequestImpl;
28 import freenet.support.api.HTTPRequest;
29 import org.junit.Test;
32 * Tests for {@link BookmarkAjaxPage}.
34 * @author <a href="mailto:d.roden@xplosion.de">David Roden</a>
36 public class BookmarkAjaxPageTest {
39 public void testBookmarkingExistingPost() throws URISyntaxException {
41 Core core = mock(Core.class);
42 Post post = mock(Post.class);
43 when(core.getPost("abc")).thenReturn(of(post));
44 WebInterface webInterface = mock(WebInterface.class);
45 when(webInterface.getCore()).thenReturn(core);
46 HTTPRequest httpRequest = new HTTPRequestImpl(new URI("/ajax/bookmark.ajax?post=abc"), "GET");
47 FreenetRequest request = mock(FreenetRequest.class);
48 when(request.getHttpRequest()).thenReturn(httpRequest);
50 /* create JSON page. */
51 BookmarkAjaxPage bookmarkAjaxPage = new BookmarkAjaxPage(webInterface);
52 JsonReturnObject jsonReturnObject = bookmarkAjaxPage.createJsonObject(request);
54 /* verify response. */
55 assertThat(jsonReturnObject, notNullValue());
56 assertThat(jsonReturnObject.isSuccess(), is(true));
58 /* verify behaviour. */
59 verify(core).bookmarkPost(post);
63 public void testBookmarkingMissingPost() throws URISyntaxException {
65 Core core = mock(Core.class);
66 WebInterface webInterface = mock(WebInterface.class);
67 when(webInterface.getCore()).thenReturn(core);
68 HTTPRequest httpRequest = new HTTPRequestImpl(new URI("/ajax/bookmark.ajax"), "GET");
69 FreenetRequest request = mock(FreenetRequest.class);
70 when(request.getHttpRequest()).thenReturn(httpRequest);
72 /* create JSON page. */
73 BookmarkAjaxPage bookmarkAjaxPage = new BookmarkAjaxPage(webInterface);
74 JsonReturnObject jsonReturnObject = bookmarkAjaxPage.createJsonObject(request);
76 /* verify response. */
77 assertThat(jsonReturnObject, notNullValue());
78 assertThat(jsonReturnObject.isSuccess(), is(false));
79 assertThat(((JsonErrorReturnObject) jsonReturnObject).getError(), is("invalid-post-id"));
81 /* verify behaviour. */
82 verify(core, never()).bookmarkPost(any(Post.class));