Package net.pterodactylus.sone.web.ajax

Source Code of net.pterodactylus.sone.web.ajax.BookmarkAjaxPageTest

/*
* © 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 <a href="mailto:d.roden@xplosion.de">David Roden</a>
*/
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());
  }

}
TOP

Related Classes of net.pterodactylus.sone.web.ajax.BookmarkAjaxPageTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.