<commit_before>package com.absontheweb.bookshop.book.controller;

import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.ConfigFileApplicationContextInitializer;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import com.absontheweb.bookshop.application.MessageSourceConfig;
import com.absontheweb.bookshop.book.model.Author;
import com.absontheweb.bookshop.book.model.Book;
import com.absontheweb.bookshop.book.model.Currency;
import com.absontheweb.bookshop.controller.exception.ErrorCode;
import com.absontheweb.bookshop.model.PaginatorResult;
import com.absontheweb.bookshop.model.SimplePaginator;
import com.absontheweb.bookshop.service.BookService;
import com.absontheweb.bookshop.service.exception.BookServiceException;
import com.absontheweb.bookshop.test.base.BookControllerTestConfig;
import com.google.common.collect.ImmutableMap;

@RunWith ( SpringJUnit4ClassRunner.class )
@ContextConfiguration ( classes = { BookControllerTestConfig.class, MessageSourceConfig.class }, 
	initializers = ConfigFileApplicationContextInitializer.class )
@WebAppConfiguration
public class BookControllerTest extends AbstractBookTest {
	
	private static final SimplePaginator ALL = null;
	private static final SimplePaginator PAGE_1 = new SimplePaginator(0, 10);
	
    @Autowired
    private WebApplicationContext wac;
    
    @Autowired
    private BookService bookService;

    private MockMvc mockMvc;
    
    @Before
    public void setUp() {
        this.mockMvc = MockMvcBuilders.webAppContextSetup( this.wac ).build();
        reset(bookService);
    }

	@Test
	public void testGetBooksOk() throws Exception {
		
		// prepare objects returned by the bookservice
		List<Author> authors = Arrays.asList(buildAuthor(1L, toDate("1978-09-25"), null));
		Book book1 = buildBook(1L, Currency.EUR, 10.0, authors);
		Book book2 = buildBook(2L, Currency.USD, 12.0, authors);
		List<Book> books = Arrays.asList(book1, book2);
		
		PaginatorResult<Book> paginatedBooks = new PaginatorResult<Book>();
		paginatedBooks.setCurrentPage(0);
		paginatedBooks.setPageSize(books.size());
		paginatedBooks.setResult(books);
		paginatedBooks.setTotalItems(books.size());
		paginatedBooks.setTotalPages(1);
		
		// program bookService mock to return a collection of books
		when(bookService.getBooks(ALL)).thenReturn(paginatedBooks);
    	
        this.mockMvc.perform( get( "/api/books" ).accept( MediaType.parseMediaType( "application/json;charset=UTF-8" ) ) )
	        .andExpect( status().isOk() )
	        .andExpect( content().contentType( "application/json;charset=UTF-8" ) )
	        .andDo( print() )
	        .andExpect( jsonPath( "$.result", hasSize(2)) )
	        .andExpect( jsonPath( "$.result[0].id" ).value( book1.getId().intValue() ) )
	        .andExpect( jsonPath( "$.result[0].title" ).value( book1.getTitle() ) )
	        .andExpect( jsonPath( "$.result[0].description" ).value( book1.getDescription() ) )
	        .andExpect( jsonPath( "$.result[0].currency" ).value( book1.getCurrency().toString() ) )
	        .andExpect( jsonPath( "$.result[0].isbn" ).value( book1.getIsbn() ) )
	        .andExpect( jsonPath( "$.result[0].price" ).value( book1.getPrice() ) )
	        .andExpect( jsonPath( "$.result[0].authors", hasSize(1) ))
	        .andExpect( jsonPath( "$.result[0].authors[0].name" ).value( book1.getAuthors().get(0).getName() ))
	        .andExpect( jsonPath( "$.result[0].authors[0].surname" ).value( book1.getAuthors().get(0).getSurname() ))
	        .andExpect( jsonPath( "$.result[0].authors[0].born" ).value( book1.getAuthors().get(0).getBorn().format(DateTimeFormatter.ISO_DATE) ))
	        .andExpect( jsonPath( "$.result[0].authors[0].birthplace" ).value( book1.getAuthors().get(0).getBirthplace() ));
	        
        verify(bookService).getBooks(ALL);
        
        assertEquals(true, true);
	}
	
	@Test
	public void testGetBooksPagedOk() throws Exception {
		
		// prepare objects returned by the bookservice
		List<Author> authors = Arrays.asList(buildAuthor(1L, toDate("1978-09-25"), null));
		Book book1 = buildBook(1L, Currency.EUR, 10.0, authors);
		Book book2 = buildBook(2L, Currency.USD, 12.0, authors);
		List<Book> books = Arrays.asList(book1, book2);
		
		PaginatorResult<Book> paginatedBooks = new PaginatorResult<Book>();
		paginatedBooks.setCurrentPage(0);
		paginatedBooks.setPageSize(books.size());
		paginatedBooks.setResult(books);<commit_msg>Update BookControllerTest<commit_after>package com.absontheweb.bookshop.book.controller;

import static org.hamcrest.Matchers.hasSize;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.ConfigFileApplicationContextInitializer;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import com.absontheweb.bookshop.application.MessageSourceConfig;
import com.absontheweb.bookshop.book.model.Author;
import com.absontheweb.bookshop.book.model.Book;
import com.absontheweb.bookshop.book.model.Currency;
import com.absontheweb.bookshop.controller.exception.ErrorCode;
import com.absontheweb.bookshop.model.PaginatorResult;
import com.absontheweb.bookshop.model.SimplePaginator;
import com.absontheweb.bookshop.service.BookService;
import com.absontheweb.bookshop.service.exception.BookServiceException;
import com.absontheweb.bookshop.test.base.BookControllerTestConfig;
import com.google.common.collect.ImmutableMap;

@RunWith ( SpringJUnit4ClassRunner.class )
@ContextConfiguration ( classes = { BookControllerTestConfig.class, MessageSourceConfig.class }, 
	initializers = ConfigFileApplicationContextInitializer.class )
@WebAppConfiguration
public class BookControllerTest extends AbstractBookTest {
	
	private static final SimplePaginator ALL = null;
	private static final SimplePaginator PAGE_1 = new SimplePaginator(0, 10);
	
    @Autowired
    private WebApplicationContext wac;
    
    @Autowired
    private BookService bookService;

    private MockMvc mockMvc;
    
    @Before
    public void setUp() {
        this.mockMvc = MockMvcBuilders.webAppContextSetup( this.wac ).build();
        reset(bookService);
    }

	@Test
	public void testGetBooksOk() throws Exception {
		
		// prepare objects returned by the bookservice
		List<Author> authors = Arrays.asList(buildAuthor(1L, toDate("1978-09-25"), null));
		Book book1 = buildBook(1L, Currency.EUR, 10.0, authors);
		Book book2 = buildBook(2L, Currency.USD, 12.0, authors);
		List<Book> books = Arrays.asList(book1, book2);
		
		PaginatorResult<Book> paginatedBooks = new PaginatorResult<Book>();
		paginatedBooks.setCurrentPage(0);
		paginatedBooks.setPageSize(books.size());
		paginatedBooks.setResult(books);
		paginatedBooks.setTotalItems(books.size());
		paginatedBooks.setTotalPages(1);
		
		// program bookService mock to return a collection of books
		when(bookService.getBooks(ALL)).thenReturn(paginatedBooks);
    	
        this.mockMvc.perform( get( "/api/books" ).accept( MediaType.parseMediaType( "application/json;charset=UTF-8" ) ) )
	        .andExpect( status().isOk() )
	        .andExpect( content().contentType( "application/json;charset=UTF-8" ) )
	        .andDo( print() )
	        .andExpect( jsonPath( "$.result", hasSize(2)) )
	        .andExpect( jsonPath( "$.result[0].id" ).value( book1.getId().intValue() ) )
	        .andExpect( jsonPath( "$.result[0].title" ).value( book1.getTitle() ) )
	        .andExpect( jsonPath( "$.result[0].description" ).value( book1.getDescription() ) )
	        .andExpect( jsonPath( "$.result[0].currency" ).value( book1.getCurrency().toString() ) )
	        .andExpect( jsonPath( "$.result[0].isbn" ).value( book1.getIsbn() ) )
	        .andExpect( jsonPath( "$.result[0].price" ).value( book1.getPrice() ) )
	        .andExpect( jsonPath( "$.result[0].authors", hasSize(1) ))
	        .andExpect( jsonPath( "$.result[0].authors[0].name" ).value( book1.getAuthors().get(0).getName() ))
	        .andExpect( jsonPath( "$.result[0].authors[0].surname" ).value( book1.getAuthors().get(0).getSurname() ))
	        .andExpect( jsonPath( "$.result[0].authors[0].born" ).value( book1.getAuthors().get(0).getBorn().format(DateTimeFormatter.ISO_DATE) ))
	        .andExpect( jsonPath( "$.result[0].authors[0].birthplace" ).value( book1.getAuthors().get(0).getBirthplace() ));
	        
        verify(bookService).getBooks(ALL);
        
	}
	
	@Test
	public void testGetBooksPagedOk() throws Exception {
		
		// prepare objects returned by the bookservice
		List<Author> authors = Arrays.asList(buildAuthor(1L, toDate("1978-09-25"), null));
		Book book1 = buildBook(1L, Currency.EUR, 10.0, authors);
		Book book2 = buildBook(2L, Currency.USD, 12.0, authors);
		List<Book> books = Arrays.asList(book1, book2);
		
		PaginatorResult<Book> paginatedBooks = new PaginatorResult<Book>();
		paginatedBooks.setCurrentPage(0);
		paginatedBooks.setPageSize(books.size());
		paginatedBooks.setResult(books);
{'fraction_non_alphanumeric': 0.12778500216731686, 'fraction_numerical': 0.014911140008669268, 'mean_word_length': 4.921971252566735, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6963957', 'n_tokens_mistral': 3699, 'n_tokens_neox': 3526, 'n_words': 601}