import net.pterodactylus.sone.data.Profile.Field;
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.freenet.fcp.Command.Response;
+import net.pterodactylus.sone.web.ajax.JsonErrorReturnObject;
+import net.pterodactylus.sone.web.ajax.JsonReturnObject;
import freenet.node.FSParseException;
import freenet.support.SimpleFieldSet;
}
}
+ public static void verifySuccessfulJsonResponse(JsonReturnObject jsonReturnObject) {
+ assertThat(jsonReturnObject, notNullValue());
+ assertThat(jsonReturnObject.isSuccess(), is(true));
+ }
+
+ public static void verifyJsonError(JsonReturnObject jsonReturnObject, String error) {
+ assertThat(jsonReturnObject, notNullValue());
+ assertThat(jsonReturnObject.isSuccess(), is(false));
+ assertThat(((JsonErrorReturnObject) jsonReturnObject).getError(), is(error));
+ }
+
}
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
+import java.net.URI;
+import java.net.URISyntaxException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import net.pterodactylus.sone.data.impl.DefaultPostReplyBuilder;
import net.pterodactylus.sone.database.Database;
import net.pterodactylus.sone.database.PostReplyBuilder;
+import net.pterodactylus.sone.web.WebInterface;
+import net.pterodactylus.sone.web.page.FreenetRequest;
+
+import freenet.clients.http.HTTPRequestImpl;
+import freenet.support.api.HTTPRequest;
import com.google.common.base.Function;
import com.google.common.base.Optional;
private final SetMultimap<PostReply, Sone> postReplyLikingSones = HashMultimap.create();
public final Database database;
public final Core core;
+ public final WebInterface webInterface;
public Mocks() {
database = mockDatabase();
core = mockCore(database);
+ webInterface = mockWebInterface(core);
when(database.getSone()).thenReturn(new Function<String, Optional<Sone>>() {
@Override
public Optional<Sone> apply(String soneId) {
return database;
}
+ private static WebInterface mockWebInterface(Core core) {
+ WebInterface webInterface = mock(WebInterface.class);
+ when(webInterface.getCore()).thenReturn(core);
+ return webInterface;
+ }
+
public SoneMocker mockSone(String id) {
return new SoneMocker(id);
}
return new PostReplyMocker(replyId, sone);
}
+ public FreenetRequest mockRequest(String path) throws URISyntaxException {
+ HTTPRequest httpRequest = new HTTPRequestImpl(new URI(path), "GET");
+ FreenetRequest request = mock(FreenetRequest.class);
+ when(request.getHttpRequest()).thenReturn(httpRequest);
+ return request;
+ }
+
public class SoneMocker {
private final Sone mockedSone = mock(Sone.class);
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 net.pterodactylus.sone.Verifiers.verifyJsonError;
+import static net.pterodactylus.sone.Verifiers.verifySuccessfulJsonResponse;
import static org.mockito.Matchers.anyString;
-import static org.mockito.Mockito.mock;
+import static org.mockito.Matchers.eq;
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.data.Mocks;
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;
/**
*/
public class BookmarkAjaxPageTest {
+ private final Mocks mocks = new Mocks();
+ private final Core core = mocks.core;
+ private final WebInterface webInterface = mocks.webInterface;
+ private final BookmarkAjaxPage bookmarkAjaxPage = new BookmarkAjaxPage(webInterface);
+
@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");
+ JsonReturnObject jsonReturnObject = performRequest("/ajax/bookmark.ajax?post=abc", bookmarkAjaxPage);
+ verifySuccessfulJsonResponse(jsonReturnObject);
+ verify(core, times(1)).bookmarkPost(eq("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. */
+ JsonReturnObject jsonReturnObject = performRequest("/ajax/bookmark.ajax", bookmarkAjaxPage);
+ verifyJsonError(jsonReturnObject, "invalid-post-id");
verify(core, never()).bookmarkPost(anyString());
}
+ private JsonReturnObject performRequest(String path, BookmarkAjaxPage bookmarkAjaxPage) throws URISyntaxException {
+ FreenetRequest request = mocks.mockRequest(path);
+ return bookmarkAjaxPage.createJsonObject(request);
+ }
+
}