Add test for CreatePostPage
[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 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;
16
17 import java.net.URI;
18 import java.net.URISyntaxException;
19
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;
24
25 import freenet.clients.http.HTTPRequestImpl;
26 import freenet.support.api.HTTPRequest;
27 import org.junit.Test;
28
29 /**
30  * Tests for {@link BookmarkAjaxPage}.
31  *
32  * @author <a href="mailto:d.roden@xplosion.de">David Roden</a>
33  */
34 public class BookmarkAjaxPageTest {
35
36         @Test
37         public void testBookmarkingExistingPost() throws URISyntaxException {
38                 /* create mocks. */
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);
47
48                 /* create JSON page. */
49                 BookmarkAjaxPage bookmarkAjaxPage = new BookmarkAjaxPage(webInterface);
50                 JsonReturnObject jsonReturnObject = bookmarkAjaxPage.createJsonObject(request);
51
52                 /* verify response. */
53                 assertThat(jsonReturnObject, notNullValue());
54                 assertThat(jsonReturnObject.isSuccess(), is(true));
55
56                 /* verify behaviour. */
57                 verify(core).bookmarkPost(post);
58         }
59
60         @Test
61         public void testBookmarkingMissingPost() throws URISyntaxException {
62                 /* create mocks. */
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);
69
70                 /* create JSON page. */
71                 BookmarkAjaxPage bookmarkAjaxPage = new BookmarkAjaxPage(webInterface);
72                 JsonReturnObject jsonReturnObject = bookmarkAjaxPage.createJsonObject(request);
73
74                 /* verify response. */
75                 assertThat(jsonReturnObject, notNullValue());
76                 assertThat(jsonReturnObject.isSuccess(), is(false));
77                 assertThat(((JsonErrorReturnObject) jsonReturnObject).getError(), is("invalid-post-id"));
78
79                 /* verify behaviour. */
80                 verify(core, never()).bookmarkPost(any(Post.class));
81         }
82
83 }