Skip to content

Commit

Permalink
[Issue comixed#166] Refactor the Bookmark model.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcpierce committed Mar 1, 2020
1 parent 107b46e commit f869ac6
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ protected void importComic(
String currentPage = currentPages.get(comic.getFilename());
if (currentPage != null && importUser != null) {
Comic currentComic = this.comicRepository.findByFilename(comic.getFilename());
importUser.setBookmark(currentComic.getId(), currentPage);
importUser.setBookmark(currentComic, currentPage);
this.userRepository.save(importUser);
} else {
this.logger.debug("No import user defined, no bookmark saved");
Expand Down
22 changes: 11 additions & 11 deletions comixed-library/src/main/java/org/comixed/model/user/Bookmark.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,45 +18,45 @@

package org.comixed.model.user;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonView;
import javax.persistence.*;
import org.comixed.model.library.Comic;
import org.comixed.views.View.UserList;

@Entity
@Table(name = "user_bookmarks")
@Table(name = "bookmarks")
public class Bookmark {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne
@JoinColumn(name = "user_id")
@JsonIgnore
private ComiXedUser user;

@Column(name = "book", nullable = false)
@ManyToOne
@JoinColumn(name = "comic_id", nullable = false, updatable = false)
@JsonView(UserList.class)
private long book;
private Comic comic;

@Column(name = "mark", updatable = true, nullable = false)
@JsonView(UserList.class)
private String mark;

public Bookmark() {}

public Bookmark(ComiXedUser user, long book, String mark) {
public Bookmark(ComiXedUser user, Comic comic, String mark) {
this.user = user;
this.book = book;
this.comic = comic;
this.mark = mark;
}

public long getBook() {
return book;
public Comic getComic() {
return comic;
}

public void setBook(long book) {
this.book = book;
public void setComic(Comic comic) {
this.comic = comic;
}

public String getMark() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Date;
import java.util.List;
import javax.persistence.*;
import org.comixed.model.library.Comic;
import org.comixed.views.View;

@Entity
Expand Down Expand Up @@ -174,28 +175,28 @@ public List<Bookmark> getBookmarks() {
* Returns the user's mark with the given book.
*
* @param book the bookmark's book
* @return the value, or 0 if not found
* @return the value, or null if none is set
*/
public String getBookmark(long book) {
public String getBookmark(Comic comic) {
for (Bookmark bookmark : this.bookmarks) {
if (bookmark.getBook() == book) return bookmark.getMark();
if (bookmark.getComic().getId().equals(comic.getId())) return bookmark.getMark();
}
return "0";
return null;
}

/**
* Sets the user bookmark for the given book.
*
* @param book the bookmark book
* @param comic the comic
* @param mark the bookmark mark
*/
public void setBookmark(long book, String mark) {
public void setBookmark(Comic comic, String mark) {
for (Bookmark bookmark : this.bookmarks) {
if (bookmark.getBook() == book) {
if (bookmark.getComic() == comic) {
bookmark.setMark(mark);
return;
}
}
this.bookmarks.add(new Bookmark(this, book, mark));
this.bookmarks.add(new Bookmark(this, comic, mark));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,23 @@

package org.comixed.model.user;

import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;

import org.junit.Before;
import org.comixed.model.library.Comic;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class BookmarkTest {

private static final long BOOK_ID = 100;
private static final String MARK = "10";
private static final long TEST_COMIC_ID = 100;
private static final String TEST_BOOKMARK = "10";

private Bookmark bookmark;

@Before
public void setUp() {
ComiXedUser user = new ComiXedUser();
bookmark = new Bookmark(user, BOOK_ID, MARK);
}

@Test
public void testHasBook() {
assertEquals(BOOK_ID, bookmark.getBook());
}

@Test
public void testCanUpdateBook() {
long newBookId = 105;
bookmark.setBook(newBookId);
assertEquals(newBookId, bookmark.getBook());
}

@Test
public void testHasMark() {
assertEquals(MARK, bookmark.getMark());
}
@Mock private Comic comic;
@Mock private ComiXedUser user;
private Bookmark bookmark = new Bookmark(user, comic, TEST_BOOKMARK);

@Test
public void testCanUpdateMark() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@

package org.comixed.controller.opds;

import java.util.Optional;
import org.comixed.model.library.Comic;
import org.comixed.model.opds.OPDSBookmark;
import org.comixed.model.user.ComiXedUser;
import org.comixed.repositories.ComiXedUserRepository;
import org.comixed.repositories.library.ComicRepository;
import org.comixed.service.library.ComicException;
import org.comixed.service.library.ComicService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -47,38 +46,44 @@ public class OPDSBookmarkController {
protected final Logger logger = LoggerFactory.getLogger(this.getClass());

@Autowired private ComiXedUserRepository userRepository;

@Autowired private ComicRepository comicRepository;
@Autowired private ComicService comicService;

@GetMapping(value = "/bookmark", produces = MediaType.APPLICATION_JSON_VALUE)
public OPDSBookmark getBookmark(@RequestParam("docId") long docId) throws ComicException {
this.logger.debug("Getting book bookmark: id={}", docId);
public OPDSBookmark getBookmark(@RequestParam("docId") long comicId) throws ComicException {
this.logger.debug("Loading comic: id={}", comicId);
final Comic comic = this.comicService.getComic(comicId);
if (comic == null) {
throw new ComicException("No such comic: id=" + comicId);
}

this.logger.debug("Getting book bookmark: id={}", comicId);
String email = SecurityContextHolder.getContext().getAuthentication().getName();
ComiXedUser user = this.userRepository.findByEmail(email);
final String bookmark = user.getBookmark(comic);

if (user.getBookmark(docId).equalsIgnoreCase("0"))
throw new ComicException("Bookmark Not Found");
if (bookmark == null) throw new ComicException("Bookmark Not Found");
else {
String mark = user.getBookmark(docId);
Optional<Comic> record = this.comicRepository.findById(docId);
if (!record.isPresent()) {
this.logger.error("No such comic");
throw new ComicException("Bookmark Not Found");
}
String totalPages = String.valueOf(record.get().getPageCount());
return new OPDSBookmark(docId, mark, mark.equalsIgnoreCase(totalPages));
String totalPages = String.valueOf(comic.getPageCount());
return new OPDSBookmark(comic.getId(), bookmark, bookmark.equals(totalPages));
}
}

@PutMapping(value = "/bookmark", consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity setBookmark(
@RequestParam("docId") long docId, @RequestBody() final OPDSBookmark opdsBookmark) {
this.logger.debug("Setting book bookmark: id={}", docId);
@RequestParam("docId") long comicId, @RequestBody() final OPDSBookmark opdsBookmark)
throws ComicException {
this.logger.debug("Loading comic: id={}", comicId);
final Comic comic = this.comicService.getComic(comicId);

if (comic == null) {
throw new ComicException("No such comic: id=" + comicId);
}

this.logger.debug("Setting book bookmark: id={}", comicId);

String email = SecurityContextHolder.getContext().getAuthentication().getName();
ComiXedUser user = this.userRepository.findByEmail(email);
user.setBookmark(docId, opdsBookmark.getMark());
user.setBookmark(comic, opdsBookmark.getMark());
this.userRepository.save(user);

return new ResponseEntity<Void>(HttpStatus.NO_CONTENT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@

package org.comixed.controller.opds;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.*;

import java.util.Optional;
import org.comixed.model.library.Comic;
import org.comixed.model.opds.OPDSBookmark;
import org.comixed.model.user.ComiXedUser;
import org.comixed.repositories.ComiXedUserRepository;
import org.comixed.repositories.library.ComicRepository;
import org.comixed.service.library.ComicException;
import org.comixed.service.library.ComicService;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -55,69 +53,91 @@ public class OPDSBookmarkControllerTest {

private static final String TEST_USER_EMAIL = "reader@local";
private static final String EXPECTED_MESSAGE = "Bookmark Not Found";
private static final long DOC_ID = 100L;
private static final String MARK = "10";
private static final long TEST_COMIC_ID = 100L;
private static final int TEST_PAGE_COUNT = 10;
private static final String TEST_BOOKMARK = String.valueOf(TEST_PAGE_COUNT);

@InjectMocks private OPDSBookmarkController controller;

@Mock private Authentication autentication;

@Mock private SecurityContext securityContext;

@Mock private ComiXedUserRepository userRepository;

@Mock private ComicRepository comicRepository;

@Mock private ComicService comicService;
@Mock private ComiXedUser user;

@Mock private Comic comic;

@Rule public ExpectedException expectedException = ExpectedException.none();

@Before
public void setUp() {
public void setUp() throws ComicException {
PowerMockito.mockStatic(SecurityContextHolder.class);
Mockito.when(autentication.getName()).thenReturn(TEST_USER_EMAIL);
Mockito.when(securityContext.getAuthentication()).thenReturn(autentication);
BDDMockito.given(SecurityContextHolder.getContext()).willReturn(securityContext);
Mockito.when(userRepository.findByEmail(TEST_USER_EMAIL)).thenReturn(user);
Mockito.when(comicRepository.findById(DOC_ID)).thenReturn(Optional.of(comic));
}

@Test
public void testBookmarkNotFound() throws ComicException {
Mockito.when(user.getBookmark(DOC_ID)).thenReturn("0");
expectedException.expect(ComicException.class);
expectedException.expectMessage(EXPECTED_MESSAGE);
@Test(expected = ComicException.class)
public void testGetBookmarkComicNotfound() throws ComicException {
Mockito.when(comicService.getComic(Mockito.anyLong())).thenReturn(null);
Mockito.when(userRepository.findByEmail(TEST_USER_EMAIL)).thenReturn(user);

BDDMockito.given(controller.getBookmark(DOC_ID))
.willThrow(new ComicException(EXPECTED_MESSAGE));
try {
controller.getBookmark(TEST_COMIC_ID);
} finally {
Mockito.verify(comicService, Mockito.times(1)).getComic(TEST_COMIC_ID);
}
}

@Test(expected = ComicException.class)
public void testGetBookmarkNotFound() throws ComicException {
Mockito.when(comicService.getComic(Mockito.anyLong())).thenReturn(comic);
Mockito.when(userRepository.findByEmail(TEST_USER_EMAIL)).thenReturn(user);
Mockito.when(user.getBookmark(Mockito.any())).thenReturn(null);

try {
controller.getBookmark(TEST_COMIC_ID);
} finally {
Mockito.verify(comicService, Mockito.times(1)).getComic(TEST_COMIC_ID);
Mockito.verify(user, Mockito.times(1)).getBookmark(comic);
}
}

@Test
public void testGetBookmark() throws ComicException {
Mockito.when(comicService.getComic(Mockito.anyLong())).thenReturn(comic);
Mockito.when(userRepository.findByEmail(TEST_USER_EMAIL)).thenReturn(user);
Mockito.when(user.getBookmark(Mockito.any(Comic.class))).thenReturn(TEST_BOOKMARK);

OPDSBookmark result = controller.getBookmark(TEST_COMIC_ID);

Mockito.when(user.getBookmark(DOC_ID)).thenReturn(MARK);
OPDSBookmark result = controller.getBookmark(DOC_ID);
assertEquals(TEST_BOOKMARK, result.getMark());
assertFalse(result.getIsFinished());

assertEquals(result.getMark(), MARK);
assertEquals(result.getIsFinished(), false);
Mockito.verify(comicService, Mockito.times(1)).getComic(TEST_COMIC_ID);
Mockito.verify(user, Mockito.times(1)).getBookmark(comic);
}

@Test
public void testGetBookmarkBookFinished() throws ComicException {
Mockito.when(user.getBookmark(DOC_ID)).thenReturn(MARK);
Mockito.when(comic.getPageCount()).thenReturn(10);
OPDSBookmark result = controller.getBookmark(DOC_ID);
Mockito.when(comicService.getComic(Mockito.anyLong())).thenReturn(comic);
Mockito.when(userRepository.findByEmail(TEST_USER_EMAIL)).thenReturn(user);
Mockito.when(user.getBookmark(comic)).thenReturn(TEST_BOOKMARK);
Mockito.when(comic.getPageCount()).thenReturn(TEST_PAGE_COUNT);
OPDSBookmark result = controller.getBookmark(TEST_COMIC_ID);

assertEquals(TEST_BOOKMARK, result.getMark());
assertTrue(result.getIsFinished());

assertEquals(result.getMark(), MARK);
assertEquals(result.getIsFinished(), true);
Mockito.verify(comicService, Mockito.times(1)).getComic(TEST_COMIC_ID);
Mockito.verify(user, Mockito.times(1)).getBookmark(comic);
}

@Test
public void testSetBookmark() {
OPDSBookmark opdsBookmark = new OPDSBookmark(DOC_ID, "1", true);
ResponseEntity result = controller.setBookmark(DOC_ID, opdsBookmark);
public void testSetBookmark() throws ComicException {
Mockito.when(comicService.getComic(Mockito.anyLong())).thenReturn(comic);
Mockito.when(userRepository.findByEmail(TEST_USER_EMAIL)).thenReturn(user);
Mockito.when(user.getBookmark(comic)).thenReturn(TEST_BOOKMARK);

ResponseEntity result =
controller.setBookmark(TEST_COMIC_ID, new OPDSBookmark(TEST_COMIC_ID, "1", true));

assertNotNull(result);
}
Expand Down

0 comments on commit f869ac6

Please sign in to comment.