Skip to content

Instantly share code, notes, and snippets.

@AlexGabor
Created April 15, 2016 06:39
Show Gist options
  • Save AlexGabor/6c5ea0350bed95ba73daffac50408cb4 to your computer and use it in GitHub Desktop.
Save AlexGabor/6c5ea0350bed95ba73daffac50408cb4 to your computer and use it in GitHub Desktop.
package ro.oneandonly.bookstore.service;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import ro.oneandonly.bookstore.domain.Book;
import ro.oneandonly.bookstore.domain.validators.ValidatorException;
import ro.oneandonly.bookstore.repository.Repository;
import java.util.*;
import java.util.concurrent.RunnableFuture;
import static org.junit.Assert.fail;
/**
* Created by alex on 3/17/16.
*/
public class BookServiceTest {
private static final Book BOOK1 = new Book("isbn3", "title1", "First Last");
private static final Book BOOK2 = new Book("isbn2", "title3", "First Smith");
private static final Book BOOK3 = new Book("isbn1", "title2", "Foo Bar");
private static final List<Book> BOOKS = Arrays.asList(BOOK1, BOOK2, BOOK3);
private static final List<Book> BOOKS_SORTED_BY_TITLE_REVERSED = Arrays.asList(BOOK2, BOOK3, BOOK1);
private static final Set<Book> SET_ISBN1 = new HashSet<>(Collections.singletonList(BOOK3));
private static final Set<Book> BOOK_SET = new HashSet<>(BOOKS);
private static final Set<Book> SET_AUTHORS = new HashSet<>(Arrays.asList(BOOK1, BOOK2));
private static final List<Book> BOOKS_SORTED_BY_PUBLISHER = Arrays.asList(BOOK3, BOOK2, BOOK1);
private BookService service;
@Before
public void setUp() throws Exception {
BOOK1.setPublisher("Manning");
BOOK2.setPublisher("Manning");
BOOK3.setPublisher("Publisher1");
BOOK2.setPrice(12);
BOOK3.setPrice(50);
service = new BookService(new Repository<Long, Book>() {
@Override
public Optional<Book> findOne(Long aLong) {
return null;
}
@Override
public Iterable<Book> findAll() {
return BOOKS;
}
@Override
public Optional<Book> save(Book entity) {
return null;
}
@Override
public Optional<Book> delete(Long aLong) {
return null;
}
@Override
public Optional<Book> update(Book entity) {
return null;
}
@Override
public void flush() {
}
});
}
@After
public void tearDown() throws Exception {
service = null;
}
@Ignore
@Test
public void testAddBook() throws Exception {
fail("Not tested");
}
@Test
public void testGetAllBooks() throws Exception {
assert service.getAllBooks().equals(BOOK_SET);
}
@Ignore
@Test
public void testUpdateBook() throws Exception {
fail("Not tested");
}
@Ignore
@Test
public void testDeleteBook() throws Exception {
fail("Not tested");
}
@Test
public void testFilterByAuthor() throws Exception {
assert service.filterByAuthor("FIRST").equals(SET_AUTHORS) : "Incorrectly filtered";
}
@Test
public void testFilterByTitle() throws Exception {
assert service.filterByTitle("title").equals(BOOK_SET) : "Incorrectly filtered";
}
@Test
public void testFilterByIsbn() throws Exception {
assert service.filterByIsbn("isbn1").equals(SET_ISBN1) : "Incorrectly filtered";
}
@Test
public void testSortByTitle() throws Exception {
assert service.sortByTitle(true).equals(BOOKS_SORTED_BY_TITLE_REVERSED) : "Incorrectly sorted";
}
@Test
public void testSortByAuthor() throws Exception {
assert service.sortByAuthor(false).equals(BOOKS) : "Incorrectly sorted";
}
@Test
public void testSortByPrice() throws Exception {
assert service.sortByPrice(false).equals(BOOKS_SORTED_BY_TITLE_REVERSED) : "Incorrectly sorted";
}
@Test
public void testSortByPublisher() throws Exception {
assert service.sortByPublisher(true).equals(BOOKS_SORTED_BY_PUBLISHER) : "Incorrectly sorted";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment