package org.jhipster.web.rest;
import org.jhipster.BlogApp;
import org.jhipster.domain.Entry;
import org.jhipster.repository.EntryRepository;
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.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.time.Instant;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.hasItem;
import static org.jhipster.web.rest.TestUtil.sameInstant;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
/**
* Test class for the EntryResource REST controller.
*
* @see EntryResource
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = BlogApp.class)
public class EntryResourceIntTest {
private static final String DEFAULT_TITLE = "AAAAAAAAAA";
private static final String UPDATED_TITLE = "BBBBBBBBBB";
private static final String DEFAULT_CONTENT = "AAAAAAAAAA";
private static final String UPDATED_CONTENT = "BBBBBBBBBB";
private static final ZonedDateTime DEFAULT_DATE = ZonedDateTime.ofInstant(Instant.ofEpochMilli(0L), ZoneOffset.UTC);
private static final ZonedDateTime UPDATED_DATE = ZonedDateTime.now(ZoneId.systemDefault()).withNano(0);
@Autowired
private EntryRepository entryRepository;
@Autowired
private MappingJackson2HttpMessageConverter jacksonMessageConverter;
@Autowired
private PageableHandlerMethodArgumentResolver pageableArgumentResolver;
@Autowired
private ExceptionTranslator exceptionTranslator;
@Autowired
private EntityManager em;
private MockMvc restEntryMockMvc;
private Entry entry;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
EntryResource entryResource = new EntryResource(entryRepository);
this.restEntryMockMvc = MockMvcBuilders.standaloneSetup(entryResource)
.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 static Entry createEntity(EntityManager em) {
Entry entry = new Entry()
.title(DEFAULT_TITLE)
.content(DEFAULT_CONTENT)
.date(DEFAULT_DATE);
return entry;
}
@Before
public void initTest() {
entry = createEntity(em);
}
@Test
@Transactional
public void createEntry() throws Exception {
int databaseSizeBeforeCreate = entryRepository.findAll().size();
// Create the Entry
restEntryMockMvc.perform(post("/api/entries")
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJsonBytes(entry)))
.andExpect(status().isCreated());
// Validate the Entry in the database
List<Entry> entryList = entryRepository.findAll();
assertThat(entryList).hasSize(databaseSizeBeforeCreate + 1);
Entry testEntry = entryList.get(entryList.size() - 1);
assertThat(testEntry.getTitle()).isEqualTo(DEFAULT_TITLE);
assertThat(testEntry.getContent()).isEqualTo(DEFAULT_CONTENT);
assertThat(testEntry.getDate()).isEqualTo(DEFAULT_DATE);
}
@Test
@Transactional
public void createEntryWithExistingId() throws Exception {
int databaseSizeBeforeCreate = entryRepository.findAll().size();
// Create the Entry with an existing ID
entry.setId(1L);
// An entity with an existing ID cannot be created, so this API call must fail
restEntryMockMvc.perform(post("/api/entries")
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJsonBytes(entry)))
.andExpect(status().isBadRequest());
// Validate the Alice in the database
List<Entry> entryList = entryRepository.findAll();
assertThat(entryList).hasSize(databaseSizeBeforeCreate);
}
@Test
@Transactional
public void checkTitleIsRequired() throws Exception {
int databaseSizeBeforeTest = entryRepository.findAll().size();
// set the field null
entry.setTitle(null);
// Create the Entry, which fails.
restEntryMockMvc.perform(post("/api/entries")
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJsonBytes(entry)))
.andExpect(status().isBadRequest());
List<Entry> entryList = entryRepository.findAll();
assertThat(entryList).hasSize(databaseSizeBeforeTest);
}
@Test
@Transactional
public void checkContentIsRequired() throws Exception {
int databaseSizeBeforeTest = entryRepository.findAll().size();
// set the field null
entry.setContent(null);
// Create the Entry, which fails.
restEntryMockMvc.perform(post("/api/entries")
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJsonBytes(entry)))
.andExpect(status().isBadRequest());
List<Entry> entryList = entryRepository.findAll();
assertThat(entryList).hasSize(databaseSizeBeforeTest);
}
@Test
@Transactional
public void checkDateIsRequired() throws Exception {
int databaseSizeBeforeTest = entryRepository.findAll().size();
// set the field null
entry.setDate(null);
// Create the Entry, which fails.
restEntryMockMvc.perform(post("/api/entries")
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJsonBytes(entry)))
.andExpect(status().isBadRequest());
List<Entry> entryList = entryRepository.findAll();
assertThat(entryList).hasSize(databaseSizeBeforeTest);
}
@Test
@Transactional
public void getAllEntries() throws Exception {
// Initialize the database
entryRepository.saveAndFlush(entry);
// Get all the entryList
restEntryMockMvc.perform(get("/api/entries?sort=id,desc"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
.andExpect(jsonPath("$.[*].id").value(hasItem(entry.getId().intValue())))
.andExpect(jsonPath("$.[*].title").value(hasItem(DEFAULT_TITLE.toString())))
.andExpect(jsonPath("$.[*].content").value(hasItem(DEFAULT_CONTENT.toString())))
.andExpect(jsonPath("$.[*].date").value(hasItem(sameInstant(DEFAULT_DATE))));
}
@Test
@Transactional
public void getEntry() throws Exception {
// Initialize the database
entryRepository.saveAndFlush(entry);
// Get the entry
restEntryMockMvc.perform(get("/api/entries/{id}", entry.getId()))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
.andExpect(jsonPath("$.id").value(entry.getId().intValue()))
.andExpect(jsonPath("$.title").value(DEFAULT_TITLE.toString()))
.andExpect(jsonPath("$.content").value(DEFAULT_CONTENT.toString()))
.andExpect(jsonPath("$.date").value(sameInstant(DEFAULT_DATE)));
}
@Test
@Transactional
public void getNonExistingEntry() throws Exception {
// Get the entry
restEntryMockMvc.perform(get("/api/entries/{id}", Long.MAX_VALUE))
.andExpect(status().isNotFound());
}
@Test
@Transactional
public void updateEntry() throws Exception {
// Initialize the database
entryRepository.saveAndFlush(entry);
int databaseSizeBeforeUpdate = entryRepository.findAll().size();
// Update the entry
Entry updatedEntry = entryRepository.findOne(entry.getId());
updatedEntry
.title(UPDATED_TITLE)
.content(UPDATED_CONTENT)
.date(UPDATED_DATE);
restEntryMockMvc.perform(put("/api/entries")
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJsonBytes(updatedEntry)))
.andExpect(status().isOk());
// Validate the Entry in the database
List<Entry> entryList = entryRepository.findAll();
assertThat(entryList).hasSize(databaseSizeBeforeUpdate);
Entry testEntry = entryList.get(entryList.size() - 1);
assertThat(testEntry.getTitle()).isEqualTo(UPDATED_TITLE);
assertThat(testEntry.getContent()).isEqualTo(UPDATED_CONTENT);
assertThat(testEntry.getDate()).isEqualTo(UPDATED_DATE);
}
@Test
@Transactional
public void updateNonExistingEntry() throws Exception {
int databaseSizeBeforeUpdate = entryRepository.findAll().size();
// Create the Entry
// If the entity doesn't have an ID, it will be created instead of just being updated
restEntryMockMvc.perform(put("/api/entries")
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJsonBytes(entry)))
.andExpect(status().isCreated());
// Validate the Entry in the database
List<Entry> entryList = entryRepository.findAll();
assertThat(entryList).hasSize(databaseSizeBeforeUpdate + 1);
}
@Test
@Transactional
public void deleteEntry() throws Exception {
// Initialize the database
entryRepository.saveAndFlush(entry);
int databaseSizeBeforeDelete = entryRepository.findAll().size();
// Get the entry
restEntryMockMvc.perform(delete("/api/entries/{id}", entry.getId())
.accept(TestUtil.APPLICATION_JSON_UTF8))
.andExpect(status().isOk());
// Validate the database is empty
List<Entry> entryList = entryRepository.findAll();
assertThat(entryList).hasSize(databaseSizeBeforeDelete - 1);
}
@Test
@Transactional
public void equalsVerifier() throws Exception {
TestUtil.equalsVerifier(Entry.class);
}
}