package org.jhipster.web.rest;
import org.jhipster.BlogApp;
import org.jhipster.domain.Blog;
import org.jhipster.repository.BlogRepository;
import org.jhipster.repository.UserRepository;
import org.jhipster.web.rest.errors.ExceptionTranslator;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.hasItem;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
/**
* Test class for the BlogResource REST controller.
*
* @see BlogResource
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = BlogApp.class)
public class BlogResourceIntTest {
private static final String DEFAULT_NAME = "AAAAAAAAAA";
private static final String UPDATED_NAME = "BBBBBBBBBB";
private static final String DEFAULT_HANDLE = "AAAAAAAAAA";
private static final String UPDATED_HANDLE = "BBBBBBBBBB";
@Autowired
private BlogRepository blogRepository;
@Autowired
private UserRepository userRepository;
@Autowired
private MappingJackson2HttpMessageConverter jacksonMessageConverter;
@Autowired
private PageableHandlerMethodArgumentResolver pageableArgumentResolver;
@Autowired
private ExceptionTranslator exceptionTranslator;
@Autowired
private EntityManager em;
private MockMvc restBlogMockMvc;
private Blog blog;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
BlogResource blogResource = new BlogResource(blogRepository);
this.restBlogMockMvc = MockMvcBuilders.standaloneSetup(blogResource)
.setCustomArgumentResolvers(pageableArgumentResolver)
.setControllerAdvice(exceptionTranslator)
.setMessageConverters(jacksonMessageConverter).build();
}
/**
* Create an entity for this test.
*
* This is a static method, as tests for other entities might also need it,
* if they test an entity which requires the current entity.
*/
public Blog createEntity(EntityManager em) {
Blog blog = new Blog()
.name(DEFAULT_NAME)
.handle(DEFAULT_HANDLE)
.user(userRepository.findOneByLogin("user").get());
return blog;
}
@Before
public void initTest() {
blog = createEntity(em);
}
@Test
@Transactional
public void createBlog() throws Exception {
int databaseSizeBeforeCreate = blogRepository.findAll().size();
// Create the Blog
restBlogMockMvc.perform(post("/api/blogs")
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJsonBytes(blog)))
.andExpect(status().isCreated());
// Validate the Blog in the database
List<Blog> blogList = blogRepository.findAll();
assertThat(blogList).hasSize(databaseSizeBeforeCreate + 1);
Blog testBlog = blogList.get(blogList.size() - 1);
assertThat(testBlog.getName()).isEqualTo(DEFAULT_NAME);
assertThat(testBlog.getHandle()).isEqualTo(DEFAULT_HANDLE);
}
@Test
@Transactional
public void createBlogWithExistingId() throws Exception {
int databaseSizeBeforeCreate = blogRepository.findAll().size();
// Create the Blog with an existing ID
blog.setId(1L);
// An entity with an existing ID cannot be created, so this API call must fail
restBlogMockMvc.perform(post("/api/blogs")
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJsonBytes(blog)))
.andExpect(status().isBadRequest());
// Validate the Alice in the database
List<Blog> blogList = blogRepository.findAll();
assertThat(blogList).hasSize(databaseSizeBeforeCreate);
}
@Test
@Transactional
public void checkNameIsRequired() throws Exception {
int databaseSizeBeforeTest = blogRepository.findAll().size();
// set the field null
blog.setName(null);
// Create the Blog, which fails.
restBlogMockMvc.perform(post("/api/blogs")
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJsonBytes(blog)))
.andExpect(status().isBadRequest());
List<Blog> blogList = blogRepository.findAll();
assertThat(blogList).hasSize(databaseSizeBeforeTest);
}
@Test
@Transactional
public void checkHandleIsRequired() throws Exception {
int databaseSizeBeforeTest = blogRepository.findAll().size();
// set the field null
blog.setHandle(null);
// Create the Blog, which fails.
restBlogMockMvc.perform(post("/api/blogs")
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJsonBytes(blog)))
.andExpect(status().isBadRequest());
List<Blog> blogList = blogRepository.findAll();
assertThat(blogList).hasSize(databaseSizeBeforeTest);
}
@Test
@Transactional
@WithMockUser
public void getAllBlogs() throws Exception {
// Initialize the database
blogRepository.saveAndFlush(blog);
// Get all the blogList
restBlogMockMvc.perform(get("/api/blogs?sort=id,desc"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
.andExpect(jsonPath("$.[*].id").value(hasItem(blog.getId().intValue())))
.andExpect(jsonPath("$.[*].name").value(hasItem(DEFAULT_NAME.toString())))
.andExpect(jsonPath("$.[*].handle").value(hasItem(DEFAULT_HANDLE.toString())));
}
@Test
@Transactional
public void getBlog() throws Exception {
// Initialize the database
blogRepository.saveAndFlush(blog);
// Get the blog
restBlogMockMvc.perform(get("/api/blogs/{id}", blog.getId()))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
.andExpect(jsonPath("$.id").value(blog.getId().intValue()))
.andExpect(jsonPath("$.name").value(DEFAULT_NAME.toString()))
.andExpect(jsonPath("$.handle").value(DEFAULT_HANDLE.toString()));
}
@Test
@Transactional
public void getNonExistingBlog() throws Exception {
// Get the blog
restBlogMockMvc.perform(get("/api/blogs/{id}", Long.MAX_VALUE))
.andExpect(status().isNotFound());
}
@Test
@Transactional
public void updateBlog() throws Exception {
// Initialize the database
blogRepository.saveAndFlush(blog);
int databaseSizeBeforeUpdate = blogRepository.findAll().size();
// Update the blog
Blog updatedBlog = blogRepository.findOne(blog.getId());
updatedBlog
.name(UPDATED_NAME)
.handle(UPDATED_HANDLE);
restBlogMockMvc.perform(put("/api/blogs")
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJsonBytes(updatedBlog)))
.andExpect(status().isOk());
// Validate the Blog in the database
List<Blog> blogList = blogRepository.findAll();
assertThat(blogList).hasSize(databaseSizeBeforeUpdate);
Blog testBlog = blogList.get(blogList.size() - 1);
assertThat(testBlog.getName()).isEqualTo(UPDATED_NAME);
assertThat(testBlog.getHandle()).isEqualTo(UPDATED_HANDLE);
}
@Test
@Transactional
public void updateNonExistingBlog() throws Exception {
int databaseSizeBeforeUpdate = blogRepository.findAll().size();
// Create the Blog
// If the entity doesn't have an ID, it will be created instead of just being updated
restBlogMockMvc.perform(put("/api/blogs")
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJsonBytes(blog)))
.andExpect(status().isCreated());
// Validate the Blog in the database
List<Blog> blogList = blogRepository.findAll();
assertThat(blogList).hasSize(databaseSizeBeforeUpdate + 1);
}
@Test
@Transactional
public void deleteBlog() throws Exception {
// Initialize the database
blogRepository.saveAndFlush(blog);
int databaseSizeBeforeDelete = blogRepository.findAll().size();
// Get the blog
restBlogMockMvc.perform(delete("/api/blogs/{id}", blog.getId())
.accept(TestUtil.APPLICATION_JSON_UTF8))
.andExpect(status().isOk());
// Validate the database is empty
List<Blog> blogList = blogRepository.findAll();
assertThat(blogList).hasSize(databaseSizeBeforeDelete - 1);
}
@Test
@Transactional
public void equalsVerifier() throws Exception {
TestUtil.equalsVerifier(Blog.class);
}
}