From c285f43c017dad11f77f13813cfe3837bf43baef Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Mon, 8 Jul 2013 15:07:18 +0200 Subject: [PATCH] Add test for BookmarkAjaxPage. --- .../sone/web/ajax/BookmarkAjaxPageTest.java | 81 ++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 src/test/java/net/pterodactylus/sone/web/ajax/BookmarkAjaxPageTest.java diff --git a/src/test/java/net/pterodactylus/sone/web/ajax/BookmarkAjaxPageTest.java b/src/test/java/net/pterodactylus/sone/web/ajax/BookmarkAjaxPageTest.java new file mode 100644 index 0000000..1db89d0 --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/web/ajax/BookmarkAjaxPageTest.java @@ -0,0 +1,81 @@ +/* + * © 2013 xplosion interactive + */ + +package net.pterodactylus.sone.web.ajax; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.junit.Assert.assertThat; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.net.URI; +import java.net.URISyntaxException; + +import net.pterodactylus.sone.core.Core; +import net.pterodactylus.sone.web.WebInterface; +import net.pterodactylus.sone.web.page.FreenetRequest; + +import freenet.clients.http.HTTPRequestImpl; +import freenet.support.api.HTTPRequest; +import org.junit.Test; + +/** + * Tests for {@link BookmarkAjaxPage}. + * + * @author David Roden + */ +public class BookmarkAjaxPageTest { + + @Test + public void testBookmarkingExistingPost() throws URISyntaxException { + /* create mocks. */ + Core core = mock(Core.class); + WebInterface webInterface = mock(WebInterface.class); + when(webInterface.getCore()).thenReturn(core); + HTTPRequest httpRequest = new HTTPRequestImpl(new URI("/ajax/bookmark.ajax?post=abc"), "GET"); + FreenetRequest request = mock(FreenetRequest.class); + when(request.getHttpRequest()).thenReturn(httpRequest); + + /* create JSON page. */ + BookmarkAjaxPage bookmarkAjaxPage = new BookmarkAjaxPage(webInterface); + JsonReturnObject jsonReturnObject = bookmarkAjaxPage.createJsonObject(request); + + /* verify response. */ + assertThat(jsonReturnObject, notNullValue()); + assertThat(jsonReturnObject.isSuccess(), is(true)); + + /* verify behaviour. */ + verify(core, times(1)).bookmarkPost(anyString()); + verify(core).bookmarkPost("abc"); + } + + @Test + public void testBookmarkingMissingPost() throws URISyntaxException { + /* create mocks. */ + Core core = mock(Core.class); + WebInterface webInterface = mock(WebInterface.class); + when(webInterface.getCore()).thenReturn(core); + HTTPRequest httpRequest = new HTTPRequestImpl(new URI("/ajax/bookmark.ajax"), "GET"); + FreenetRequest request = mock(FreenetRequest.class); + when(request.getHttpRequest()).thenReturn(httpRequest); + + /* create JSON page. */ + BookmarkAjaxPage bookmarkAjaxPage = new BookmarkAjaxPage(webInterface); + JsonReturnObject jsonReturnObject = bookmarkAjaxPage.createJsonObject(request); + + /* verify response. */ + assertThat(jsonReturnObject, notNullValue()); + assertThat(jsonReturnObject.isSuccess(), is(false)); + assertThat(((JsonErrorReturnObject) jsonReturnObject).getError(), is("invalid-post-id")); + + /* verify behaviour. */ + verify(core, never()).bookmarkPost(anyString()); + } + +} -- 2.7.4