package com.dangdang.mypp.web.rest;
import com.dangdang.mypp.Application;
import com.dangdang.mypp.domain.Book;
import com.dangdang.mypp.repository.BookRepository;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.hamcrest.Matchers.hasItem;
import org.mockito.MockitoAnnotations;
import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
/**
* Test class for the BookResource REST controller.
*
* @see BookResource
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
@IntegrationTest
public class BookResourceTest {
private static final String DEFAULT_TITLE = "SAMPLE_TEXT";
private static final String UPDATED_TITLE = "UPDATED_TEXT";
@Inject
private BookRepository bookRepository;
private MockMvc restBookMockMvc;
private Book book;
@PostConstruct
public void setup() {
MockitoAnnotations.initMocks(this);
BookResource bookResource = new BookResource();
ReflectionTestUtils.setField(bookResource, "bookRepository", bookRepository);
this.restBookMockMvc = MockMvcBuilders.standaloneSetup(bookResource).build();
}
@Before
public void initTest() {
book = new Book();
book.setTitle(DEFAULT_TITLE);
}
@Test
@Transactional
public void createBook() throws Exception {
int databaseSizeBeforeCreate = bookRepository.findAll().size();
// Create the Book
restBookMockMvc.perform(post("/api/books")
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJsonBytes(book)))
.andExpect(status().isCreated());
// Validate the Book in the database
List<Book> books = bookRepository.findAll();
assertThat(books).hasSize(databaseSizeBeforeCreate + 1);
Book testBook = books.get(books.size() - 1);
assertThat(testBook.getTitle()).isEqualTo(DEFAULT_TITLE);
}
@Test
@Transactional
public void getAllBooks() throws Exception {
// Initialize the database
bookRepository.saveAndFlush(book);
// Get all the books
restBookMockMvc.perform(get("/api/books"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.[*].id").value(hasItem(book.getId().intValue())))
.andExpect(jsonPath("$.[*].title").value(hasItem(DEFAULT_TITLE.toString())));
}
@Test
@Transactional
public void getBook() throws Exception {
// Initialize the database
bookRepository.saveAndFlush(book);
// Get the book
restBookMockMvc.perform(get("/api/books/{id}", book.getId()))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.id").value(book.getId().intValue()))
.andExpect(jsonPath("$.title").value(DEFAULT_TITLE.toString()));
}
@Test
@Transactional
public void getNonExistingBook() throws Exception {
// Get the book
restBookMockMvc.perform(get("/api/books/{id}", Long.MAX_VALUE))
.andExpect(status().isNotFound());
}
@Test
@Transactional
public void updateBook() throws Exception {
// Initialize the database
bookRepository.saveAndFlush(book);
int databaseSizeBeforeUpdate = bookRepository.findAll().size();
// Update the book
book.setTitle(UPDATED_TITLE);
restBookMockMvc.perform(put("/api/books")
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJsonBytes(book)))
.andExpect(status().isOk());
// Validate the Book in the database
List<Book> books = bookRepository.findAll();
assertThat(books).hasSize(databaseSizeBeforeUpdate);
Book testBook = books.get(books.size() - 1);
assertThat(testBook.getTitle()).isEqualTo(UPDATED_TITLE);
}
@Test
@Transactional
public void deleteBook() throws Exception {
// Initialize the database
bookRepository.saveAndFlush(book);
int databaseSizeBeforeDelete = bookRepository.findAll().size();
// Get the book
restBookMockMvc.perform(delete("/api/books/{id}", book.getId())
.accept(TestUtil.APPLICATION_JSON_UTF8))
.andExpect(status().isOk());
// Validate the database is empty
List<Book> books = bookRepository.findAll();
assertThat(books).hasSize(databaseSizeBeforeDelete - 1);
}
}