package fr.ekito.example.web.rest;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
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 javax.annotation.PostConstruct;
import javax.inject.Inject;
import org.joda.time.LocalDate;
import java.util.List;
import fr.ekito.example.Application;
import fr.ekito.example.domain.Author;
import fr.ekito.example.repository.AuthorRepository;
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 AuthorResource REST controller.
*
* @see AuthorResource
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
public class AuthorResourceTest {
private static final String DEFAULT_NAME = "SAMPLE_TEXT";
private static final String UPDATED_NAME = "UPDATED_TEXT";
private static final LocalDate DEFAULT_BIRTH_DATE = new LocalDate(0L);
private static final LocalDate UPDATED_BIRTH_DATE = new LocalDate();
@Inject
private AuthorRepository authorRepository;
private MockMvc restAuthorMockMvc;
private Author author;
@PostConstruct
public void setup() {
MockitoAnnotations.initMocks(this);
AuthorResource authorResource = new AuthorResource();
ReflectionTestUtils.setField(authorResource, "authorRepository", authorRepository);
this.restAuthorMockMvc = MockMvcBuilders.standaloneSetup(authorResource).build();
}
@Before
public void initTest() {
authorRepository.deleteAll();
author = new Author();
author.setName(DEFAULT_NAME);
author.setBirthDate(DEFAULT_BIRTH_DATE);
}
@Test
public void createAuthor() throws Exception {
// Validate the database is empty
assertThat(authorRepository.findAll()).hasSize(0);
// Create the Author
restAuthorMockMvc.perform(post("/app/rest/authors")
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJsonBytes(author)))
.andExpect(status().isOk());
// Validate the Author in the database
List<Author> authors = authorRepository.findAll();
assertThat(authors).hasSize(1);
Author testAuthor = authors.iterator().next();
assertThat(testAuthor.getName()).isEqualTo(DEFAULT_NAME);
assertThat(testAuthor.getBirthDate()).isEqualTo(DEFAULT_BIRTH_DATE);
}
@Test
public void getAllAuthors() throws Exception {
// Initialize the database
authorRepository.save(author);
// Get all the authors
restAuthorMockMvc.perform(get("/app/rest/authors"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.[0].id").value(author.getId()))
.andExpect(jsonPath("$.[0].name").value(DEFAULT_NAME.toString()))
.andExpect(jsonPath("$.[0].birthDate").value(DEFAULT_BIRTH_DATE.toString()));
}
@Test
public void getAuthor() throws Exception {
// Initialize the database
authorRepository.save(author);
// Get the author
restAuthorMockMvc.perform(get("/app/rest/authors/{id}", author.getId()))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.id").value(author.getId()))
.andExpect(jsonPath("$.name").value(DEFAULT_NAME.toString()))
.andExpect(jsonPath("$.birthDate").value(DEFAULT_BIRTH_DATE.toString()));
}
@Test
public void getNonExistingAuthor() throws Exception {
// Get the author
restAuthorMockMvc.perform(get("/app/rest/authors/{id}", 1L))
.andExpect(status().isNotFound());
}
@Test
public void updateAuthor() throws Exception {
// Initialize the database
authorRepository.save(author);
// Update the author
author.setName(UPDATED_NAME);
author.setBirthDate(UPDATED_BIRTH_DATE);
restAuthorMockMvc.perform(post("/app/rest/authors")
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJsonBytes(author)))
.andExpect(status().isOk());
// Validate the Author in the database
List<Author> authors = authorRepository.findAll();
assertThat(authors).hasSize(1);
Author testAuthor = authors.iterator().next();
assertThat(testAuthor.getName()).isEqualTo(UPDATED_NAME);
assertThat(testAuthor.getBirthDate()).isEqualTo(UPDATED_BIRTH_DATE);
}
@Test
public void deleteAuthor() throws Exception {
// Initialize the database
authorRepository.save(author);
// Get the author
restAuthorMockMvc.perform(delete("/app/rest/authors/{id}", author.getId())
.accept(TestUtil.APPLICATION_JSON_UTF8))
.andExpect(status().isOk());
// Validate the database is empty
List<Author> authors = authorRepository.findAll();
assertThat(authors).hasSize(0);
}
}