Skip to content

Instantly share code, notes, and snippets.

@lmeadors
Created November 19, 2014 17:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lmeadors/e0bcefd23691ac6ec531 to your computer and use it in GitHub Desktop.
Save lmeadors/e0bcefd23691ac6ec531 to your computer and use it in GitHub Desktop.
package com.alexandria.www.stripes;
import com.alexandria.www.bean.*;
import com.alexandria.www.enums.KindName;
import com.alexandria.www.enums.RatingSystemName;
import com.alexandria.www.service.*;
import com.alexandria.www.stripes.extensions.PatronActionBeanContext;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
public class TitleActionTest extends AbstractBaseTest<TitleAction> {
public FavoritesService favoritesService;
public TitleService titleService;
public LibraryService libraryService;
public PatronService patronService;
public PlaybackService playbackService;
public RatingSystemService ratingSystemService;
@NamedValue("environment")
public String env = "DEV";
@Test
public void should_display_audiobook_detail() throws Exception {
common_test_for_should_display_detail_for_kind(KindName.AUDIOBOOK, TitleAction.AUDIOBOOK);
}
@Test
public void should_display_comic_detail() throws Exception {
common_test_for_should_display_detail_for_kind(KindName.COMIC, TitleAction.COMIC);
}
@Test
public void should_display_ebook_detail() throws Exception {
common_test_for_should_display_detail_for_kind(KindName.EBOOK, TitleAction.EBOOK);
}
@Test
public void should_display_movie_detail() throws Exception {
common_test_for_should_display_detail_for_kind(KindName.MOVIE, TitleAction.MOVIE);
}
@Test
public void should_display_music_detail() throws Exception {
common_test_for_should_display_detail_for_kind(KindName.MUSIC, TitleAction.MUSIC);
}
@Test
public void should_display_television_detail() throws Exception {
common_test_for_should_display_detail_for_kind(KindName.TELEVISION, TitleAction.TELEVISION);
}
private void common_test_for_should_display_detail_for_kind(
final KindName kindName,
final String expectedDestination
) throws Exception {
// set up test
final Title title = getTestTitle(kindName);
when(titleService.getTitle(eq(title.getId()), any(UserPrincipal.class))).thenReturn(title);
// run test
trip.addParameter("titleId", title.getId().toString());
trip.execute();
// describe and verify expected result
verify(titleService).getTitle(eq(title.getId()), any(UserPrincipal.class));
assertEquals(env, action.getEnvironment());
assertEquals(expectedDestination, trip.getDestination());
}
@Test
public void should_display_movie_for_anonymous_patron() throws Exception {
Response<RatingSystem> ratingSystemResponse = new Response<>();
RatingSystem ratingSystem = new RatingSystem();
ratingSystem.setName(RatingSystemName.MPAA.name());
ratingSystemResponse.setResults(ratingSystem);
when(ratingSystemService.fetch(1)).thenReturn(ratingSystemResponse);
final Title title = getTestTitle(KindName.MOVIE);
when(titleService.getTitle(eq(title.getId()), any(UserPrincipal.class))).thenReturn(title);
// remove the user in session to simulate not being logged in
trip.getRequest().getSession().removeAttribute(PatronActionBeanContext.USER_PRINCIPAL);
trip.addParameter("titleId", title.getId().toString());
trip.execute();
verify(titleService).getTitle(eq(title.getId()), any(UserPrincipal.class));
verify(patronService, never()).getBorrowedAssets(any(UserPrincipal.class));
assertEquals(TitleAction.MOVIE, trip.getDestination());
assertEquals(env, action.getEnvironment());
}
@Test
public void should_display_television_with_only_one_content_from_borrowed_title() throws Exception {
// set up test
final Title title = getTestTitle(KindName.TELEVISION);
title.getContents().remove(1);
when(titleService.getTitle(eq(title.getId()), any(UserPrincipal.class))).thenReturn(title);
// run test
trip.addParameter("titleId", title.getId().toString());
trip.execute();
// describe and verify expected result
verify(titleService).getTitle(eq(title.getId()), any(UserPrincipal.class));
assertEquals(env, action.getEnvironment());
assertEquals(TitleAction.MOVIE, trip.getDestination());
}
@Test
public void should_display_television_for_anonymous_patron() throws Exception {
final Title title = getTestTitle(KindName.TELEVISION);
Response<RatingSystem> response = new Response<>();
RatingSystem ratingSystem = new RatingSystem();
ratingSystem.setId(1);
ratingSystem.setName("TV");
ratingSystem.setKindId(title.getKind().getId());
response.setResults(ratingSystem);
when(titleService.getTitle(eq(title.getId()), any(UserPrincipal.class))).thenReturn(title);
when(ratingSystemService.fetch(anyInt())).thenReturn(response);
// remove the user in session to simulate not being logged in
trip.getRequest().getSession().removeAttribute(PatronActionBeanContext.USER_PRINCIPAL);
trip.addParameter("titleId", title.getId().toString());
trip.execute();
verify(titleService).getTitle(eq(title.getId()), any(UserPrincipal.class));
verify(patronService, never()).getBorrowedAssets(any(UserPrincipal.class));
verify(ratingSystemService).fetch(anyInt());
assertEquals(env, action.getEnvironment());
assertEquals(TitleAction.TELEVISION, trip.getDestination());
assertNotNull(action.getContentRating());
assertNotNull(action.getKindName());
}
@Test
public void should_redirect_to_the_home_page_for_invalid_titleId() throws Exception {
// set up test
final Integer titleId = -1;
when(titleService.getTitle(eq(titleId), any(UserPrincipal.class))).thenReturn(null);
// run test
trip.addParameter("titleId", titleId.toString());
trip.execute();
// describe and verify expected result
verify(titleService).getTitle(eq(titleId), any(UserPrincipal.class));
assertEquals(env, action.getEnvironment());
assertEquals(buildRedirectUrl(HomeAction.class), trip.getRedirectUrl());
}
@Test
public void should_borrow_content_success() throws Exception {
// set up test
final Integer contentId = 123;
final List<Title> borrowedTitles = new ArrayList<>();
borrowedTitles.add(getTestTitle(KindName.MOVIE));
final Response<List<Title>> borrowedTitlesResponse = new Response<>();
borrowedTitlesResponse.setResults(borrowedTitles);
borrowedTitlesResponse.setErrors(new ArrayList<ResponseError>());
final Response<Patron> response = new Response<>();
final Patron patron = new Patron();
response.setResults(patron);
when(patronService.authenticate(anyString(), anyString())).thenReturn(response);
when(patronService.borrowContent(any(UserPrincipal.class), eq(contentId))).thenReturn(borrowedTitlesResponse);
// run test
trip.addParameter("contentId", contentId.toString());
trip.execute("borrowContent");
// describe and verify expected result
verify(patronService).borrowContent(any(UserPrincipal.class), eq(contentId));
assertEquals(env, action.getEnvironment());
assertNotNull(action.getContentId());
}
@Test
public void should_borrow_content_with_patron_auth_errors_and_fail() throws Exception {
final Integer id = 123;
Response<Patron> response = new Response<>();
ArrayList<ResponseError> errors = new ArrayList<>();
ResponseError error = new ResponseError("xxx", "yyy", "zzz");
errors.add(error);
response.setResults(null);
response.setStatus("ERROR");
response.setErrors(errors);
when(patronService.authenticate(anyString(), anyString())).thenReturn(response);
trip.addParameter("contentId", id.toString());
trip.execute("borrowContent");
assertNotNull(action.getContentId());
verify(patronService).borrowContent(any(UserPrincipal.class), anyInt());
verify(patronService, never()).getBorrowedIds(any(UserPrincipal.class));
verify(favoritesService, never()).getFavoriteIds(any(UserPrincipal.class));
}
@Test
public void should_borrow_content_with_borrow_errors_and_fail() throws Exception {
final Integer contentId = -1;
final Patron patron = new Patron();
patron.setId(-2);
patron.setLibraryId(-3);
final Response<Patron> patronResponse = new Response<>();
patronResponse.setResults(patron);
patronResponse.setStatus("OK");
patronResponse.setErrors(new ArrayList<ResponseError>());
final Response<Library> libraryResponse = new Response<>();
libraryResponse.setResults(new Library());
when(libraryService.fetchLibrary(patron.getLibraryId())).thenReturn(libraryResponse);
final Response<List<Title>> response = new Response<>();
final ArrayList<ResponseError> errors = new ArrayList<>();
final ResponseError error = new ResponseError("xxx", "yyy", "zzz");
errors.add(error);
response.setResults(null);
response.setStatus("ERROR");
response.setErrors(errors);
when(patronService.authenticate(anyString(), anyString())).thenReturn(patronResponse);
when(patronService.borrowContent(any(UserPrincipal.class), anyInt())).thenReturn(response);
// remove the user in session to simulate not being logged in
trip.getRequest().getSession().removeAttribute(PatronActionBeanContext.USER_PRINCIPAL);
trip.addParameter("contentId", contentId.toString());
trip.execute("borrowContent");
// describe and verify expected result
verify(patronService).authenticate(anyString(), anyString());
verify(patronService).borrowContent(any(UserPrincipal.class), eq(contentId));
verify(patronService).getBorrowedIds(any(UserPrincipal.class));
verify(favoritesService).getFavoriteIds(any(UserPrincipal.class));
}
@Test
public void should_try_to_borrow_content_without_user_principal() throws Exception {
final Integer id = 123;
Response<Patron> response = new Response<>();
ArrayList<ResponseError> errors = new ArrayList<>();
ResponseError error = new ResponseError("xxx", "yyy", "zzz");
errors.add(error);
response.setResults(null);
response.setStatus("ERROR");
response.setErrors(errors);
when(patronService.authenticate(anyString(), anyString())).thenReturn(response);
// remove the user in session to simulate not being logged in
trip.getRequest().getSession().removeAttribute(PatronActionBeanContext.USER_PRINCIPAL);
trip.addParameter("contentId", id.toString());
trip.execute("borrowContent");
assertTrue(trip.getOutputString().contains("\"outcome\":\"invalidPatron\""));
verify(patronService, never()).getBorrowedIds(any(UserPrincipal.class));
verify(favoritesService, never()).getFavoriteIds(any(UserPrincipal.class));
}
@Test
public void should_borrow_content_without_user_principal() throws Exception {
final Integer id = 123;
final String username = "username";
final String password = "password";
Patron patron = new Patron();
patron.setLibraryId(id);
Title title = new Title();
ArrayList<Title> titles = new ArrayList<>();
titles.add(title);
Date due = new Date();
due.setTime(1234567890l);
Content content = new Content();
content.setId(id);
content.setDue(due);
ArrayList<Content> contents = new ArrayList<>();
contents.add(content);
title.setContents(contents);
Response<Patron> patronResponse = new Response<>();
patronResponse.setResults(patron);
when(patronService.authenticate(username, password)).thenReturn(patronResponse);
Response<Library> libraryResponse = new Response<>();
libraryResponse.setResults(new Library());
when(libraryService.fetchLibrary(id)).thenReturn(libraryResponse);
Response<List<Title>> titlesResponse = new Response<>();
titlesResponse.setResults(titles);
when(patronService.borrowContent(any(UserPrincipal.class), anyInt())).thenReturn(titlesResponse);
// remove the user in session to simulate not being logged in
trip.getRequest().getSession().removeAttribute(PatronActionBeanContext.USER_PRINCIPAL);
trip.addParameter("contentId", id.toString());
trip.addParameter("username", username);
trip.addParameter("password", password);
trip.execute("borrowContent");
assertNotNull(action.getUsername());
assertNotNull(action.getPassword());
// {"class":"com.alexandria.www.bean.Outcome","message":"Wed Jan 14 23:56:07 MST 1970","outcome":"success"}
assertTrue(trip.getOutputString().contains("\"outcome\":\"success\""));
verify(patronService).getBorrowedIds(any(UserPrincipal.class));
verify(favoritesService).getFavoriteIds(any(UserPrincipal.class));
}
@Test
public void should_return_content() throws Exception {
// set up test
final Title title = getTestTitle(KindName.MUSIC);
// run test
trip.addParameter("contentId", title.getId().toString());
trip.execute("returnContent");
// describe and verify expected result
verify(patronService).returnContent(any(UserPrincipal.class), eq(title.getId()));
assertEquals(buildRedirectUrl(MyTitlesAction.class), trip.getRedirectUrl());
}
@Test
public void should_throw_an_exception_while_trying_to_return_content() throws Exception {
// set up test
final Title title = getTestTitle(KindName.MUSIC);
doThrow(new RuntimeException()).when(patronService).returnContent(any(UserPrincipal.class), eq(title.getId()));
// run test
trip.addParameter("contentId", title.getId().toString());
trip.execute("returnContent");
// describe and verify expected result
verify(patronService).returnContent(any(UserPrincipal.class), eq(title.getId()));
assertEquals(buildRedirectUrl(MyTitlesAction.class), trip.getRedirectUrl());
}
@Test
public void should_play_audiobook() throws Exception {
// set up test
final Title title = getTestTitle(KindName.AUDIOBOOK);
final Response<List<Title>> borrowedTitlesResponse = new Response<>();
borrowedTitlesResponse.setResults(Arrays.asList(title));
when(patronService.getBorrowedAssets(any(UserPrincipal.class))).thenReturn(borrowedTitlesResponse);
when(playbackService.getPlaybackPosition(anyInt(), anyInt())).thenReturn(title.getId());
when(playbackService.getBookmarks(anyInt(), anyInt())).thenReturn(new ArrayList<Integer>());
when(playbackService.isPlaybackAllowed(anyInt(), anyInt(), anyInt(), anyString())).thenReturn(true);
// run test
trip.addParameter("titleId", title.getId().toString());
trip.addParameter("contentId", title.getId().toString());
trip.execute("play");
// describe and verify expected result
verify(patronService).getBorrowedAssets(any(UserPrincipal.class));
verify(playbackService).getPlaybackPosition(anyInt(), anyInt());
verify(playbackService).getBookmarks(anyInt(), anyInt());
verify(playbackService).isPlaybackAllowed(anyInt(), anyInt(), anyInt(), anyString());
assertNotNull(action.getTitleId());
assertNotNull(action.getTitle());
assertNotNull(action.getBookmarks());
assertEquals(title.getId(), action.getPlaybackPosition());
assertEquals(TitleAction.AUDIOBOOK_PLAYER, trip.getDestination());
assertTrue(action.isGeoLocationAllowed());
}
@Test
public void should_play_ebook() throws Exception {
// set up test
final Title title = getTestTitle(KindName.EBOOK);
final Response<List<Title>> borrowedTitlesResponse = new Response<>();
borrowedTitlesResponse.setResults(Arrays.asList(title));
when(patronService.getBorrowedAssets(any(UserPrincipal.class))).thenReturn(borrowedTitlesResponse);
when(playbackService.getPlaybackPosition(anyInt(), anyInt())).thenReturn(title.getId());
when(playbackService.getBookmarks(anyInt(), anyInt())).thenReturn(new ArrayList<Integer>());
when(playbackService.isPlaybackAllowed(anyInt(), anyInt(), anyInt(), anyString())).thenReturn(true);
// run test
trip.addParameter("titleId", title.getId().toString());
trip.addParameter("contentId", title.getId().toString());
trip.execute("play");
// describe and verify expected result
verify(patronService).getBorrowedAssets(any(UserPrincipal.class));
verify(playbackService).isPlaybackAllowed(anyInt(), anyInt(), anyInt(), anyString());
assertEquals(TitleAction.EBOOK_READER, trip.getDestination());
assertTrue(action.isGeoLocationAllowed());
}
@Test
public void should_play_music() throws Exception {
// set up test
final Title title = getTestTitle(KindName.MUSIC);
final Response<List<Title>> borrowedTitlesResponse = new Response<>();
borrowedTitlesResponse.setResults(Arrays.asList(title));
when(patronService.getBorrowedAssets(any(UserPrincipal.class))).thenReturn(borrowedTitlesResponse);
when(playbackService.isPlaybackAllowed(anyInt(), anyInt(), anyInt(), anyString())).thenReturn(true);
// run test
trip.addParameter("titleId", title.getId().toString());
trip.addParameter("contentId", title.getId().toString());
trip.addParameter("playlistIndex", title.getId().toString());
trip.execute("play");
// describe and verify expected result
verify(playbackService, never()).getPlaybackPosition(anyInt(), anyInt());
verify(playbackService).isPlaybackAllowed(anyInt(), anyInt(), anyInt(), anyString());
verify(patronService).getBorrowedAssets(any(UserPrincipal.class));
assertNotNull(action.getTitleId());
assertNotNull(action.getTitle());
assertEquals(title.getId(), action.getPlaylistIndex());
assertEquals(TitleAction.MUSIC_PLAYER, trip.getDestination());
assertTrue(action.isGeoLocationAllowed());
}
@Test
public void should_play_movie() throws Exception {
// set up test
final Title title = getTestTitle(KindName.MOVIE);
final Response<List<Title>> borrowedTitlesResponse = new Response<>();
borrowedTitlesResponse.setResults(Arrays.asList(title));
when(patronService.getBorrowedAssets(any(UserPrincipal.class))).thenReturn(borrowedTitlesResponse);
when(playbackService.isPlaybackAllowed(anyInt(), anyInt(), anyInt(), anyString())).thenReturn(true);
when(playbackService.getPlaybackPosition(anyInt(), anyInt())).thenReturn(title.getId());
// run test
trip.addParameter("titleId", title.getId().toString());
trip.addParameter("contentId", title.getId().toString());
trip.execute("play");
// describe and verify expected result
verify(patronService).getBorrowedAssets(any(UserPrincipal.class));
verify(playbackService).getPlaybackPosition(anyInt(), anyInt());
verify(playbackService).isPlaybackAllowed(anyInt(), anyInt(), anyInt(), anyString());
assertNotNull(action.getTitleId());
assertNotNull(action.getTitle());
assertEquals(title.getId(), action.getPlaybackPosition());
assertEquals(TitleAction.VIDEO_PLAYER, trip.getDestination());
}
@Test
public void should_play_television() throws Exception {
// set up test
final Title title = getTestTitle(KindName.TELEVISION);
final Response<List<Title>> borrowedTitlesResponse = new Response<>();
borrowedTitlesResponse.setResults(Arrays.asList(title));
when(patronService.getBorrowedAssets(any(UserPrincipal.class))).thenReturn(borrowedTitlesResponse);
when(playbackService.getPlaybackPosition(anyInt(), anyInt())).thenReturn(title.getId());
// run test
trip.addParameter("titleId", title.getId().toString());
trip.addParameter("contentId", title.getId().toString());
trip.execute("play");
// describe and verify expected result
verify(patronService).getBorrowedAssets(any(UserPrincipal.class));
verify(playbackService).getPlaybackPosition(anyInt(), anyInt());
verify(playbackService).isPlaybackAllowed(anyInt(), anyInt(), anyInt(), anyString());
assertNotNull(action.getTitleId());
assertNotNull(action.getTitle());
assertEquals(title.getId(), action.getPlaybackPosition());
assertEquals(TitleAction.VIDEO_PLAYER, trip.getDestination());
}
@Test
public void should_attempt_to_play_title_but_nothing_borrowed() throws Exception {
// set up test
// this test exercises a rare case that a patron attempts to play a title that they no longer have borrowed,
// because they have NO borrowed titles at the moment.
final Title title = getTestTitle(KindName.TELEVISION);
final Response<List<Title>> borrowedTitlesResponse = new Response<>();
when(patronService.getBorrowedAssets(any(UserPrincipal.class))).thenReturn(borrowedTitlesResponse);
when(playbackService.getPlaybackPosition(anyInt(), anyInt())).thenReturn(title.getId());
// run test
trip.addParameter("titleId", title.getId().toString());
trip.addParameter("contentId", title.getId().toString());
trip.execute("play");
// describe and verify expected result
verify(patronService).getBorrowedAssets(any(UserPrincipal.class));
verify(playbackService, never()).getPlaybackPosition(anyInt(), anyInt());
verify(playbackService, never()).isPlaybackAllowed(anyInt(), anyInt(), anyInt(), anyString());
assertEquals("/system/error/system", trip.getDestination());
}
@Test
public void should_attempt_to_play_title_but_title_no_longer_borrowed() throws Exception {
// set up test
// this test exercises a rare case that a patron attempts to play a title that they do not have borrowed.
final Integer titleIdPatronDoesNotHaveBorrowed = 45;
final Title title = getTestTitle(KindName.TELEVISION);
final Response<List<Title>> borrowedTitlesResponse = new Response<>();
borrowedTitlesResponse.setResults(Arrays.asList(title));
when(patronService.getBorrowedAssets(any(UserPrincipal.class))).thenReturn(borrowedTitlesResponse);
when(playbackService.getPlaybackPosition(anyInt(), anyInt())).thenReturn(title.getId());
// run test
trip.addParameter("titleId", titleIdPatronDoesNotHaveBorrowed.toString());
trip.addParameter("contentId", titleIdPatronDoesNotHaveBorrowed.toString());
trip.execute("play");
// describe and verify expected result
verify(patronService).getBorrowedAssets(any(UserPrincipal.class));
verify(playbackService, never()).getPlaybackPosition(anyInt(), anyInt());
verify(playbackService, never()).isPlaybackAllowed(anyInt(), anyInt(), anyInt(), anyString());
assertEquals("/system/error/system", trip.getDestination());
}
@Test
public void should_try_to_play_while_not_logged_in() throws Exception {
// set up test
final Title title = getTestTitle(KindName.MUSIC);
// run test
trip.addParameter("titleId", title.getId().toString());
trip.addParameter("contentId", title.getId().toString());
trip.getRequest().getSession().removeAttribute(PatronActionBeanContext.USER_PRINCIPAL);
trip.execute("play");
// describe and verify expected result
verify(patronService, never()).getBorrowedAssets(any(UserPrincipal.class));
assertEquals(buildRedirectUrl(MyTitlesAction.class), trip.getRedirectUrl());
}
@Test
public void should_add_bookmark() throws Exception {
// set up test
final Title title = getTestTitle(KindName.MOVIE);
when(titleService.getTitle(eq(title.getId()), any(UserPrincipal.class))).thenReturn(title);
// run test
trip.addParameter("titleId", title.getId().toString());
trip.addParameter("seconds", title.getId().toString());
trip.execute("addBookmark");
// describe and verify expected result
verify(playbackService).setBookmark(anyInt(), anyInt(), anyInt());
}
@Test
public void should_not_add_bookmark() throws Exception {
// set up test
final Title title = getTestTitle(KindName.MUSIC);
trip.getRequest().getSession().removeAttribute(PatronActionBeanContext.USER_PRINCIPAL);
// run test
trip.addParameter("titleId", title.getId().toString());
trip.addParameter("seconds", title.getId().toString());
trip.execute("addBookmark");
// describe and verify expected result
verifyNoMoreInteractions(playbackService);
}
@Test
public void should_delete_bookmark() throws Exception {
// set up test
final Integer titleId = -1;
final Integer seconds = 5;
// run test
trip.addParameter("titleId", titleId.toString());
trip.addParameter("seconds", seconds.toString());
trip.execute("removeBookmark");
// describe and verify expected result
verify(playbackService).deleteBookmark(CURRENT_PATRON_ID, titleId, seconds);
verify(playbackService).getBookmarks(CURRENT_PATRON_ID, titleId);
verifyNoMoreInteractions(playbackService);
}
@Test
public void should_delete_bookmark_without_session() throws Exception {
// set up test
final Integer contentId = -1;
final Integer seconds = 5;
trip.getRequest().getSession().removeAttribute(PatronActionBeanContext.USER_PRINCIPAL);
// run test
trip.addParameter("contentId", contentId.toString());
trip.addParameter("seconds", seconds.toString());
trip.execute("removeBookmark");
// describe and verify expected result
verify(playbackService, never()).deleteBookmark(anyInt(), anyInt(), anyInt());
}
@Test
public void should_add_play_count() throws Exception {
// set up test
final Integer circId = 1;
final Integer segmentId = 2;
final Integer titleId = 3;
// run test
trip.addParameter("circId", circId.toString());
trip.addParameter("segmentId", segmentId.toString());
trip.addParameter("titleId", titleId.toString());
trip.execute("addPlayCount");
// describe and verify expected result
verify(playbackService).addPlay(circId, segmentId);
}
@Test
public void should_add_play_count_without_session() throws Exception {
// set up test
final Integer circId = 1;
final Integer segmentId = 2;
final Integer titleId = 3;
trip.getRequest().getSession().removeAttribute(PatronActionBeanContext.USER_PRINCIPAL);
// run test
trip.addParameter("circId", circId.toString());
trip.addParameter("segmentId", segmentId.toString());
trip.addParameter("titleId", titleId.toString());
trip.execute("addPlayCount");
// describe and verify expected result
verify(playbackService, never()).addPlay(anyInt(), anyInt());
}
@Test
public void should_save_session() throws Exception {
// set up test
final Integer contentId = -2;
final Integer position = -3;
final String deviceId = "foo";
// run test
trip.addParameter("contentId", contentId.toString());
trip.addParameter("seconds", position.toString());
trip.addParameter("deviceId", deviceId);
trip.execute("saveSession");
// describe and verify expected result
verify(playbackService).saveSession(CURRENT_PATRON_ID, contentId, position, deviceId);
}
@Test
public void should_not_save_session() throws Exception {
// set up test
final Integer contentId = -2;
final Integer position = -3;
final String deviceId = "foo";
trip.getRequest().getSession().removeAttribute(PatronActionBeanContext.USER_PRINCIPAL);
// run test
trip.addParameter("contentId", contentId.toString());
trip.addParameter("seconds", position.toString());
trip.addParameter("deviceId", deviceId);
trip.execute("saveSession");
// describe and verify expected result
verify(playbackService, never()).saveSession(anyInt(), anyInt(), anyInt(), anyString());
}
@Test
public void should_delete_session() throws Exception {
// set up test
final Integer contentId = -1;
final Integer seconds = -2;
// run test
trip.addParameter("contentId", contentId.toString());
trip.addParameter("seconds", seconds.toString());
trip.execute("deleteSession");
// describe and verify expected result
verify(playbackService).deleteSession(CURRENT_PATRON_ID, contentId, seconds);
}
@Test
public void should_not_delete_session() throws Exception {
// set up test
final Integer contentId = -1;
final Integer seconds = -2;
trip.getRequest().getSession().removeAttribute(PatronActionBeanContext.USER_PRINCIPAL);
// run test
trip.addParameter("contentId", contentId.toString());
trip.addParameter("seconds", seconds.toString());
trip.execute("deleteSession");
// describe and verify expected result
verify(playbackService, never()).deleteSession(CURRENT_PATRON_ID, contentId, seconds);
}
@Test
public void should_add_ebookmark() throws Exception {
trip.addParameter("patronId", "1");
trip.addParameter("contentId", "1");
trip.addParameter("ebookmark.chapterId", "chaper_1");
trip.addParameter("ebookmark.epubcfi", "/6/4/4[chapter_1]!");
trip.addParameter("ebookmark.label", "Chapter One");
trip.execute("addEBookmark");
verify(playbackService).addEBookmark(eq(1), eq(1), notNull(EBookmark.class));
assertNotNull("contentId should not be null", action.getContentId());
assertNotNull("ebookmark should not be null", action.getEbookmark());
}
@Test
public void should_add_ebookmark_without_patron_provided() throws Exception {
trip.addParameter("contentId", "1");
trip.addParameter("ebookmark.chapterId", "chaper_1");
trip.addParameter("ebookmark.epubcfi", "/6/4/4[chapter_1]!");
trip.addParameter("ebookmark.label", "Chapter One");
trip.execute("addEBookmark");
verify(playbackService).addEBookmark(eq(1), eq(1), notNull(EBookmark.class));
assertNotNull("contentId should not be null", action.getContentId());
assertNotNull("ebookmark should not be null", action.getEbookmark());
}
@Test
public void should_remove_ebookmark() throws Exception {
trip.addParameter("patronId", "1");
trip.addParameter("contentId", "1");
trip.addParameter("ebookmark.cfiRangeStart", "/6/4/4[chapter_1]!/4/2/1:0");
trip.addParameter("ebookmark.cfiRangeEnd", "/6/4/4[chapter_1]!/4/2/1:10");
trip.execute("removeEBookmark");
verify(playbackService).deleteEBookmark(eq(1), eq(1), notNull(EBookmark.class));
}
@Test
public void should_remove_ebookmark_without_patron_provided() throws Exception {
trip.addParameter("contentId", "1");
trip.addParameter("ebookmark.cfiRangeStart", "/6/4/4[chapter_1]!/4/2/1:0");
trip.addParameter("ebookmark.cfiRangeEnd", "/6/4/4[chapter_1]!/4/2/1:10");
trip.execute("removeEBookmark");
verify(playbackService).deleteEBookmark(eq(1), eq(1), notNull(EBookmark.class));
}
@Test
public void should_get_ebook_position() throws Exception {
trip.addParameter("patronId", "1");
trip.addParameter("contentId", "1");
when(playbackService.getEbookPosition(1, 1)).thenReturn("/6/4/4[chapter_1]!");
trip.execute("getEBookPosition");
final String output = trip.getOutputString();
assertEquals("/6/4/4[chapter_1]!", output);
verify(playbackService).getEbookPosition(1, 1);
}
@Test
public void should_get_ebook_position_without_patron_provided() throws Exception {
trip.addParameter("contentId", "1");
when(playbackService.getEbookPosition(1, 1)).thenReturn("/6/4/4[chapter_1]!");
trip.execute("getEBookPosition");
final String output = trip.getOutputString();
assertEquals("/6/4/4[chapter_1]!", output);
verify(playbackService).getEbookPosition(1, 1);
}
@Test
public void should_set_ebook_position() throws Exception {
trip.addParameter("patronId", "1");
trip.addParameter("contentId", "1");
trip.addParameter("ebookPosition", "/6/4/4[chapter_1]!");
trip.execute("setEBookPosition");
verify(playbackService).setEbookPosition(1, 1, "/6/4/4[chapter_1]!");
}
@Test
public void should_set_ebook_position_without_patron_provided() throws Exception {
trip.addParameter("contentId", "1");
trip.addParameter("ebookPosition", "/6/4/4[chapter_1]!");
trip.execute("setEBookPosition");
verify(playbackService).setEbookPosition(1, 1, "/6/4/4[chapter_1]!");
}
@Test
public void should_get_ebookmarks() throws Exception {
trip.addParameter("patronId", "1");
trip.addParameter("contentId", "1");
trip.execute("getEBookmarks");
verify(playbackService).getEBookmarks(1, 1);
}
@Test
public void should_get_ebookmarks_without_patron_provided() throws Exception {
trip.addParameter("contentId", "1");
trip.execute("getEBookmarks");
verify(playbackService).getEBookmarks(1, 1);
}
private Title getTestTitle(final KindName kindName) {
final Kind kind = new Kind();
kind.setName(kindName.name());
final Title title = new Title();
title.setId(-1);
title.setKind(kind);
final List<Content> contents = new ArrayList<>();
if (kindName.equals(KindName.MOVIE)) {
addRating(title, "G");
} else if (kindName.equals(KindName.TELEVISION)) {
addRating(title, "TVG");
Content content = new Content();
content.setId(1);
Content content2 = new Content();
content2.setId(2);
contents.add(content);
contents.add(content2);
} else if (kindName.equals(KindName.EBOOK)) {
contents.add(new Content() {{
setId(1);
setMediaKey("testMediaKey");
setArtKey("testMediaKey");
}});
}
title.setContents(contents);
return title;
}
private void addRating(final Title title, final String ratingName) {
Rating rating = new Rating();
rating.setRatingSystemId(1);
rating.setRating(ratingName);
rating.setRank(1);
ArrayList<Rating> ratings = new ArrayList<>();
ratings.add(rating);
title.setRatings(ratings);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment