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